未验证 提交 d4bd5606 编写于 作者: W wuzewu 提交者: GitHub

Update docs.

上级 5acac161
...@@ -224,13 +224,12 @@ If you have any questions during the use of the model, you can join the official ...@@ -224,13 +224,12 @@ If you have any questions during the use of the model, you can join the official
If you fail to scan the code, please add WeChat 15704308458 and note "Hub", the operating class will invite you to join the group. If you fail to scan the code, please add WeChat 15704308458 and note "Hub", the operating class will invite you to join the group.
## Documentation Tutorial ## Documentation Tutorial
- [PIP Installation](./docs/docs_en/installation_en.md) - [PIP Installation](./docs/docs_en/get_start/installation.rst)
- Quick Start - Quick Start
- [Command Line](./docs/docs_en/quick_experience/cmd_quick_run_en.md) - [Python API](./docs/docs_en/get_start/python_use_hub.rst)
- [Python API](./docs/docs_en/quick_experience/python_use_hub_en.md) - [More Demos](./docs/docs_en/community/more_demos.md)
- [More Demos](./docs/docs_en/quick_experience/more_demos_en.md)
- Rich Pre-trained Models 274 - Rich Pre-trained Models 274
- [Boutique Featured Models](./docs/docs_en/figures_en.md) - [Boutique Featured Models](./docs/docs_en/figures.md)
- Computer Vision 141 - Computer Vision 141
- [Image Classification 64 ](./modules/image/classification/README_en.md) - [Image Classification 64 ](./modules/image/classification/README_en.md)
- [Object Detection 13 ](./modules/image/object_detection/README_en.md) - [Object Detection 13 ](./modules/image/object_detection/README_en.md)
...@@ -254,16 +253,13 @@ If you fail to scan the code, please add WeChat 15704308458 and note "Hub", the ...@@ -254,16 +253,13 @@ If you fail to scan the code, please add WeChat 15704308458 and note "Hub", the
- [Video Classification 5 ](./modules/video/README_en.md) - [Video Classification 5 ](./modules/video/README_en.md)
- [Video Repair 3 ](https://www.paddlepaddle.org.cn/hublist) - [Video Repair 3 ](https://www.paddlepaddle.org.cn/hublist)
- Deploy - Deploy
- [Local Inference Deployment](./docs/docs_en/quick_experience/python_use_hub_en.md) - [One Line of Code Service deployment](./docs/docs_en/tutorial/serving.md)
- [One Line of Code Service deployment](./docs/docs_en/tutorial/serving_en.md)
- [Mobile Device Deployment](https://paddle-lite.readthedocs.io/zh/latest/quick_start/tutorial.html) - [Mobile Device Deployment](https://paddle-lite.readthedocs.io/zh/latest/quick_start/tutorial.html)
- Advanced documentation - Advanced documentation
- [Command Line Interface Usage](./docs/docs_en/tutorial/cmdintro_en.md) - [Command Line Interface Usage](./docs/docs_en/tutorial/cmd_usage.rst)
- [How to Load Customized Dataset](./docs/docs_en/tutorial/how_to_load_data_en.md)
- Community - Community
- [Join Technical Group](#Welcome_joinus) - [Join Technical Group](#Welcome_joinus)
- [Contribute Pre-trained Models](./docs/docs_en/contribution/contri_pretrained_model_en.md) - [Contribute Code](./docs/docs_en/community/contribute_code.md)
- [Contribute Code](./docs/docs_en/contribution/contri_pr_en.md)
- [License](#License) - [License](#License)
- [Contribution](#Contribution) - [Contribution](#Contribution)
......
...@@ -227,11 +227,10 @@ ...@@ -227,11 +227,10 @@
## 文档教程 ## 文档教程
- [PIP安装](./docs/docs_ch/installation.md) - [PIP安装](./docs/docs_ch/get_start/installation.rst)
- 快速开始 - 快速开始
- [命令行调用](./docs/docs_ch/quick_experience/cmd_quick_run.md) - [Python API调用](./docs/docs_ch/get_start/python_use_hub.rst)
- [Python API调用](./docs/docs_ch/quick_experience/python_use_hub.md) - [示例体验项目demo](./docs/docs_ch/community/more_demos.md)
- [示例体验项目demo](./docs/docs_ch/quick_experience/more_demos.md)
- 丰富的预训练模型 274 - 丰富的预训练模型 274
- [精品特色模型](./docs/docs_ch/figures.md) - [精品特色模型](./docs/docs_ch/figures.md)
- 计算机视觉 141 个 - 计算机视觉 141 个
...@@ -257,16 +256,13 @@ ...@@ -257,16 +256,13 @@
- [视频分类 5 个](./modules/video/README.md) - [视频分类 5 个](./modules/video/README.md)
- [视频修复 3 个](https://www.paddlepaddle.org.cn/hublist) - [视频修复 3 个](https://www.paddlepaddle.org.cn/hublist)
- 部署 - 部署
- [本地Inference部署](./docs/docs_ch/quick_experience/python_use_hub.md)
- [一行代码服务化部署](./docs/docs_ch/tutorial/serving.md) - [一行代码服务化部署](./docs/docs_ch/tutorial/serving.md)
- [移动端 Lite 部署(跳转Lite教程)](https://paddle-lite.readthedocs.io/zh/latest/quick_start/tutorial.html) - [移动端 Lite 部署(跳转Lite教程)](https://paddle-lite.readthedocs.io/zh/latest/quick_start/tutorial.html)
- 进阶文档 - 进阶文档
- [命令行工具详解](./docs/docs_ch/tutorial/cmdintro.md) - [命令行工具详解](./docs/docs_ch/tutorial/cmd_usage.rst)
- [自定义数据迁移学习](./docs/docs_ch/tutorial/how_to_load_data.md)
- 社区交流 - 社区交流
- [加入技术交流群](#欢迎加入PaddleHub技术交流群) - [加入技术交流群](#欢迎加入PaddleHub技术交流群)
- [贡献预训练模型](./docs/docs_ch/contribution/contri_pretrained_model.md) - [贡献代码](./docs/docs_ch/contribution/contribute_code.md)
- [贡献代码](./docs/docs_ch/contribution/contri_pr.md)
- [FAQ](./docs/docs_ch/faq.md) - [FAQ](./docs/docs_ch/faq.md)
- [更新历史](./docs/docs_ch/release.md) - [更新历史](./docs/docs_ch/release.md)
- [许可证书](#许可证书) - [许可证书](#许可证书)
......
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
==============
Canvas
==============
.. code-block:: python
class paddlehub.datasets.Canvas(transform: Callable, mode: str = 'train'):
-----------------
Dataset for colorization. It contains 1193 and 400 pictures for Monet and Vango paintings style, respectively. We collected data from https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/.
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train or test). Default to 'train'.
\ No newline at end of file
==============
ChnSentiCorp
==============
.. code-block:: python
class paddlehub.datasets.ChnSentiCorp(tokenizer: Union[BertTokenizer, CustomTokenizer], max_seq_len: int = 128, mode: str = 'train'):
-----------------
ChnSentiCorp is a dataset for chinese sentiment classification, which was published by Tan Songbo at ICT of Chinese Academy of Sciences.
-----------------
* Args:
* tokenizer(:obj:`BertTokenizer` or `CustomTokenizer`)
It tokenizes the text and encodes the data as model needed.
* max_seq_len(:obj:`int`, `optional`, defaults to :128)
The maximum length (in number of tokens) for the inputs to the selected module, such as ernie, bert and so on.
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
\ No newline at end of file
==============
ESC50
==============
.. code-block:: python
class paddlehub.datasets.ESC50(mode: str = 'train', feat_type: str = 'mel'):)
-----------------
The ESC-50 dataset is a labeled collection of 2000 environmental audio recordings suitable for benchmarking methods of environmental sound classification.
-----------------
* Args:
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
* feat_type(:obj:`str`, `optional`, defaults to `mel`):
It identifies the input feature type (mel, or raw).
\ No newline at end of file
==============
Flowers
==============
.. code-block:: python
class paddlehub.datasets.Flowers(transform: Callable, mode: str = 'train'):
-----------------
Flower classification dataset.
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train, test or val). Default to 'train'.
\ No newline at end of file
==============
LCQMC
==============
.. code-block:: python
class paddlehub.datasets.LCQMC(tokenizer: Union[BertTokenizer, CustomTokenizer], max_seq_len: int = 128, mode: str = 'train'):
-----------------
A Large-scale Chinese Question Matching Corpus.
-----------------
* Args:
* tokenizer(:obj:`BertTokenizer` or `CustomTokenizer`)
It tokenizes the text and encodes the data as model needed.
* max_seq_len(:obj:`int`, `optional`, defaults to :128)
The maximum length (in number of tokens) for the inputs to the selected module, such as ernie, bert and so on.
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
\ No newline at end of file
==============
MiniCOCO
==============
.. code-block:: python
class paddlehub.datasets.MiniCOCO(transform: Callable, mode: str = 'train'):
-----------------
Dataset for Style transfer. The dataset contains 2001 images for training set and 200 images for testing set. They are derived form COCO2014. Meanwhile, it contains 21 different style pictures in file "21styles".
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train or test). Default to 'train'.
\ No newline at end of file
==============
MSRA_NER
==============
.. code-block:: python
class paddlehub.datasets.MSRA_NER(tokenizer: Union[BertTokenizer, CustomTokenizer], max_seq_len: int = 128, mode: str = 'train'):
-----------------
A set of manually annotated Chinese word-segmentation data and specifications for training and testing a Chinese word-segmentation system for research purposes. For more information please refer to https://www.microsoft.com/en-us/download/details.aspx?id=52531
-----------------
* Args:
* tokenizer(:obj:`BertTokenizer` or `CustomTokenizer`)
It tokenizes the text and encodes the data as model needed.
* max_seq_len(:obj:`int`, `optional`, defaults to :128)
The maximum length (in number of tokens) for the inputs to the selected module, such as ernie, bert and so on.
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
\ No newline at end of file
==============
OpticDiscSeg
==============
.. code-block:: python
class paddlehub.datasets.OpticDiscSeg(transform: Callable, mode: str = 'train'):
-----------------
OpticDiscSeg dataset is extraced from iChallenge-AMD(https://ai.baidu.com/broad/subordinate?dataset=amd).
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train, test or val). Default to 'train'.
\ No newline at end of file
==============
Datasets
==============
CV
==============
.. toctree::
:maxdepth: 2
:titlesonly:
Canvas<datasets/canvas.rst>
Flowers<datasets/flowers.rst>
OpticDiscSeg<datasets/opticdisc.rst>
MiniCOCO<datasets/minicoco.rst>
NLP
==============
.. toctree::
:maxdepth: 2
:titlesonly:
ChnSentiCorp<datasets/chnsenticorp.rst>
LCQMC<datasets/lcqmc.rst>
MSRA_NER<datasets/msra_ner.rst>
Audio
==============
.. toctree::
:maxdepth: 2
:titlesonly:
ESC50<datasets/esc50.rst>
\ No newline at end of file
================
Hub Environment
================
.. code-block:: console
HUB_HOME
├── MODULE_HOME
├── CACHE_HOME
├── DATA_HOME
├── CONF_HOME
├── THIRD_PARTY_HOME
├── TMP_HOME
└── LOG_HOME
paddlehub.env.HUB_HOME
=========================
The root directory for storing PaddleHub related data. Default to ~/.paddlehub. Users can change the default value through the HUB_HOME environment variable.
paddlehub.env.MODULE_HOME
=========================
Directory for storing the installed PaddleHub Module.
paddlehub.env.CACHE_HOME
=========================
Directory for storing the cached data.
paddlehub.env.DATA_HOME
=========================
Directory for storing the automatically downloaded datasets.
paddlehub.env.CONF_HOME
=========================
Directory for storing the default configuration files.
paddlehub.env.THIRD_PARTY_HOME
================================
Directory for storing third-party libraries.
paddlehub.env.TMP_HOME
=========================
Directory for storing the temporary files generated during running, such as intermediate products of installing modules, files in this directory will generally be automatically cleared.
paddlehub.env.LOG_HOME
=========================
Directory for storing the log files generated during operation, including some non-fatal errors. The log will be stored daily.
==============
Module
==============
.. code-block:: python
class paddlehub.Module(
name: str = None,
directory: str = None,
version: str = None,
ignore_env_mismatch: bool = False,
**kwargs)
-----------------
In PaddleHub, Module represents an executable module, which usually a pre-trained model that can be used for end-to-end prediction, such as a face detection model or a lexical analysis model, or a pre-trained model that requires finetuning, such as BERT/ERNIE. When loading a Module with a specified name, if the Module does not exist locally, PaddleHub will automatically request the server or the specified Git source to download the resource.
-----------------
* Args:
* name(str | optional)
Module name.
* directory(str | optional)
Directory of the module to be loaded, only takes effect when the `name` is not specified.
* version(str | optional)
The version limit of the module, only takes effect when the `name` is specified. When the local Module does not meet the specified version conditions, PaddleHub will re-request the server to download the appropriate Module. Default to None, This means that the local Module will be used. If the Module does not exist, PaddleHub will download the latest version available from the server according to the usage environment.
* ignore_env_mismatch(bool | optional)
Whether to ignore the environment mismatch when installing the Module. Default to False.
**member functions**
=====================
export_onnx_model
------------------
.. code-block:: python
def export_onnx_model(
dirname: str,
input_spec: List[paddle.static.InputSpec] = None,
include_sub_modules: bool = True,
**kwargs):
Export the model to ONNX format.
* Args:
* dirname(str)
The directory to save the onnx model.
* input_spec(list)
Describes the input of the saved model's forward method, which can be described by InputSpec or example Tensor. If None, all input variables of the original Layer's forward method would be the inputs of the saved model. Default None.
* include_sub_modules(bool)
Whether to export sub modules. Default to True.
* \*\*kwargs(dict|optional)
Other export configuration options for compatibility, some may be removed in the future. Don't use them If not necessary. Refer to https://github.com/PaddlePaddle/paddle2onnx for more information.
save_inference_model
----------------------
.. code-block:: python
def save_inference_model(
dirname: str,
model_filename: str = None,
params_filename: str = None,
input_spec: List[paddle.static.InputSpec] = None,
include_sub_modules: bool = True,
combined: bool = True):
Export the model to Paddle Inference format.
* Args:
* name(str | optional)
Module name.
* model_filename(str)
The name of the saved model file. Default to `__model__`.
* params_filename(str)
The name of the saved parameters file, only takes effect when `combined` is True. Default to `__params__`.
* input_spec(list)
Describes the input of the saved model's forward method, which can be described by InputSpec or example Tensor. If None, all input variables of the original Layer's forward method would be the inputs of the saved model. Default None.
* include_sub_modules(bool)
Whether to export sub modules. Default to True.
* combined(bool)
Whether to save all parameters in a combined file. Default to True.
sub_modules
----------------------
.. code-block:: python
def sub_modules(recursive: bool = True):
Get all sub modules.
* Args:
* recursive(bool):
Whether to get sub modules recursively. Default to True.
**classmethod**
=================
get_py_requirements
----------------------
.. code-block:: python
@classmethod
def get_py_requirements(cls) -> List[str]:
Get Module's python package dependency list.
load
----------------------
.. code-block:: python
@classmethod
def load(cls, directory: str) -> Generic:
Load the Module object defined in the specified directory.
* Args:
* directory(str):
Module directory.
load_module_info
----------------------
.. code-block:: python
@classmethod
def load_module_info(cls, directory: str) -> EasyDict:
Load the Module object defined in the specified directory.
* Args:
* directory(str):
Module directory.
**property**
=================
is_runnable
-----------------
.. code-block:: python
is_runnable
Whether the Module is runnable, in other words, whether can we execute the Module through the `hub run` command.
name
-----------------
.. code-block:: python
name
Module name.
directory
-----------------
.. code-block:: python
directory
Directory of Module.
version
-----------------
.. code-block:: python
version
Module name.
type
-----------------
.. code-block:: python
type
Module type.
summary
-----------------
.. code-block:: python
summary
Module summary.
author
-----------------
.. code-block:: python
author
The author of Module
author_email
-----------------
.. code-block:: python
author_email
The email of Module author
.. note::
Module is a factory class that is used to automatically download and load user-defined model classes. In addition to the above methods or property, each Module has other custom methods or property. The relevant definitions need to be viewed in the corresponding documentation
=================
Module Decorator
=================
moduleinfo
============
.. code-block:: python
def paddlehub.module.module.moduleinfo(
name: str,
version: str,
author: str = None,
author_email: str = None,
summary: str = None,
type: str = None,
meta=None) -> Callable:
-----------------
Mark Module information for a python class, and the class will automatically be extended to inherit HubModule. In other words, python classes marked with moduleinfo can be loaded through hub.Module.
-----------------
* Args:
* name(str)
Module name.
* version(str)
Module name.
* author(str)
The author of Module.
* author_email(str)
The email of Module author.
* summary(str)
Module summary.
* type(str)
Module type.
moduleinfo
============
.. code-block:: python
def paddlehub.module.module.runnable(func: Callable) -> Callable:
-----------------
Mark a Module method as runnable, when the command `hub run` is used, the method will be called.
-----------------
* Args:
* func(Callable)
member function of Module.
serving
============
.. code-block:: python
def paddlehub.module.module.serving(func: Callable) -> Callable:
-----------------
Mark a Module method as serving method, when the command `hub serving` is used, the method will be called.
-----------------
* Args:
* func(Callable)
member function of Module.
\ No newline at end of file
=======================
LocalModuleManager
=======================
.. code-block:: python
class paddlehub.module.manager.LocalModuleManager(home: str = MODULE_HOME):
-----------------
LocalModuleManager is used to manage PaddleHub's local Module, which supports the installation, uninstallation, and search of HubModule. LocalModuleManager is a singleton object related to the path, in other words, when the LocalModuleManager object of the same home directory is generated multiple times, the same object is returned.
-----------------
* Args:
* home(str)
The directory where PaddleHub modules are stored, the default is ~/.paddlehub/modules
**member functions**
=====================
install
------------------
.. code-block:: python
def install(
name: str = None,
directory: str = None,
archive: str = None,
url: str = None,
version: str = None,
ignore_env_mismatch: bool = False) -> HubModule:
Install a HubModule from name or directory or archive file or url. When installing with the name parameter, if a module that meets the conditions (both name and version) already installed, the installation step will be skipped. When installing with other parameter, The locally installed modules will be uninstalled.
* Args:
* name(str | optional)
module name to install
* directory(str | optional)
directory containing module code
* archive(str | optional)
archive file containing module code
* url(str|optional)
url points to a archive file containing module code
* version(str | optional)
module version, use with name parameter
* ignore_env_mismatch(str | optional)
Whether to ignore the environment mismatch when installing the Module.
uninstall
------------------
.. code-block:: python
def uninstall(name: str) -> bool:
Uninstall a HubModule from name.
* Args:
* name(str)
module name to uninstall
* Return:
True if uninstall successfully else False
list
------------------
.. code-block:: python
def list() -> List[HubModule]:
List all installed HubModule.
* Return:
List of installed HubModule.
search
------------------
.. code-block:: python
def search(name: str) -> HubModule:
search a HubModule with specified name.
* Args:
* name(str)
module name to search.
* Return:
None if not HubModule with specified name found else the specified HubModule.
\ No newline at end of file
==============
Trainer
==============
.. code-block:: python
class paddlehub.Trainer(
model: paddle.nn.Layer,
optimizer: paddle.optimizer.Optimizer,
use_gpu: bool = False,
use_vdl: bool = True,
checkpoint_dir: str = None,
compare_metrics: Callable = None):
-----------------
Model trainer.
-----------------
* Args:
* model(paddle.nn.Layer)
Model to train or evaluate.
* optimizer(paddle.optimizer.Optimizer)
Optimizer for loss.
* use_gpu(bool)
Whether to use gpu to run.
* use_vdl(bool)
Whether to use visualdl to record training data.
* checkpoint_dir(str)
Directory where the checkpoint is saved, and the trainer will restore the state and model parameters from the checkpoint.
* compare_metrics(Callable)
The method of comparing the model metrics. If not specified, the main metric return by `validation_step` will be used for comparison by default, the larger the value, the better the effect. This method will affect the saving of the best model. If the default behavior does not meet your requirements, please pass in a custom method.
**member functions**
=====================
train
------------------
.. code-block:: python
def train(
train_dataset: paddle.io.Dataset,
epochs: int = 1,
batch_size: int = 1,
num_workers: int = 0,
eval_dataset: paddle.io.Dataset = None,
log_interval: int = 10,
save_interval: int = 10,
collate_fn: Callable = None):
Train a model with specific config.
* Args:
* train_dataset(paddle.io.Dataset)
Dataset to train the model
* epochs(int)
Number of training loops, default is 1.
* batch_size(int)
Batch size of per step, default is 1.
* num_workers(int)
Number of subprocess to load data, default is 0.
* eval_dataset(paddle.io.Dataset)
The validation dataset, deafult is None. If set, the Trainer will execute evaluate function every `save_interval` epochs.
* log_interval(int)
Log the train infomation every `log_interval` steps.
* save_interval(int)
Save the checkpoint every `save_interval` epochs.
* collate_fn(Callable)
function to generate mini-batch data by merging the sample list. None for only stack each fields of sample in axis 0(same as :attr::`np.stack(..., axis=0)`). Default None.
evaluate
----------------------
.. code-block:: python
def evaluate(
eval_dataset: paddle.io.Dataset,
batch_size: int = 1,
num_workers: int = 0,
collate_fn: Callable = None):
Run evaluation and returns metrics.
* Args:
* eval_dataset(paddle.io.Dataset)
The validation dataset
* batch_size(int)
Batch size of per step, default is 1.
* num_workers(int)
Number of subprocess to load data, default is 0.
* collate_fn(Callable)
function to generate mini-batch data by merging the sample list. None for only stack each fields of sample in axis 0(same as :attr::`np.stack(..., axis=0)`). Default None.
\ No newline at end of file
==============
API Reference
==============
Module
==============
.. toctree::
:maxdepth: 2
:titlesonly:
Module<api/module.rst>
LocalModuleManager<api/module_manager.rst>
Module Decorator<api/module_decorator.rst>
Transfer Learning
==================
.. toctree::
:maxdepth: 2
:titlesonly:
Trainer<api/trainer.rst>
datasets<api/datasets_index.rst>
Environment
==============
.. toctree::
:maxdepth: 2
:titlesonly:
env<api/env.rst>
\ No newline at end of file
# PaddleHub更多体验Demos # 第三方趣味案例
## PaddleHub官方Demo全集【Offical】
官方Demo在AI Studio中均可在线运行,类似notebook的环境,只需通过浏览器即可访问,无需准备环境,非常方便开发者快速体验。
并将持续更新,建议收藏。
[https://aistudio.baidu.com/aistudio/personalcenter/thirdview/79927](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/79927)
## PaddleHub开发者趣味实践作品【ThirdParty】
以下为前期PaddleHub课程或活动中,开发者们基于PaddleHub创作的趣味实践作品,均收录在AI Studio中,可在线运行,欢迎访问,希望对您有所启发。 以下为前期PaddleHub课程或活动中,开发者们基于PaddleHub创作的趣味实践作品,均收录在AI Studio中,可在线运行,欢迎访问,希望对您有所启发。
1. [布剪刀石头【人脸识别切换本地窗口】](https://aistudio.baidu.com/aistudio/projectdetail/507630) 1. [布剪刀石头【人脸识别切换本地窗口】](https://aistudio.baidu.com/aistudio/projectdetail/507630)
......
===================
活跃社区
===================
.. toctree::
:maxdepth: 2
:titlesonly:
community/more_demos.md
community/contribute_code.md
------------
.. toctree::
:maxdepth: 2
:titlesonly:
第一期AI创意赛 <https://aistudio.baidu.com/aistudio/competition/detail/34>
第二期AI创意赛 <https://aistudio.baidu.com/aistudio/competition/detail/35>
第一期AI创造营 <https://aistudio.baidu.com/aistudio/competition/detail/72>
第一期AI ChatBot创意赛 <https://aistudio.baidu.com/aistudio/competition/detail/79>
\ No newline at end of file
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
from recommonmark.transform import AutoStructify
from recommonmark.parser import CommonMarkParser
# sys.path.insert(0, os.path.abspath('../../paddlehub'))
# import paddlehub as hub
# -- Project information -----------------------------------------------------
project = 'PaddleHub'
copyright = '2021, PaddlePaddle'
author = 'PaddlePaddle'
# The full version, including alpha/beta/rc tags
# release = 'v{}'.format(hub.__version__)
release = 'v2.1.0'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'sphinx.ext.coverage', 'sphinx.ext.viewcode', 'sphinx.ext.mathjax',
'sphinx.ext.githubpages', 'sphinx.ext.napoleon', 'recommonmark', 'sphinx_markdown_tables'
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', '.DS_Store']
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_title = project
html_theme = "sphinx_materialdesign_theme"
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
source_parsers = {'.md': CommonMarkParser}
source_suffix = ['.rst', '.md']
贡献代码
==================
PaddleHub非常欢迎贡献者,特别是代码和预训练模型贡献者。以下文档介绍了如何贡献代码和预训练模型。
.. toctree::
:maxdepth: 1
贡献代码<contri_pr.md>
贡献预训练模型<contri_pretrained_model>
\ No newline at end of file
# 如何编写一个PaddleHub Module并发布上线
## 一、准备工作
### 模型基本信息
我们准备编写一个PaddleHub Module,Module的基本信息如下:
```yaml
name: senta_test
version: 1.0.0
summary: This is a PaddleHub Module. Just for test.
author: anonymous
author_email:
type: nlp/sentiment_analysis
```
Module存在一个接口sentiment_classify,用于接收传入文本,并给出文本的情感倾向(正面/负面),支持python接口调用和命令行调用。
```python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
```
```cmd
hub run senta_test --input_text 这部电影太差劲了
```
<br/>
### 策略
为了示例代码简单起见,我们使用一个非常简单的情感判断策略,当输入文本中带有词表中指定单词时,则判断文本倾向为负向,否则为正向
<br/>
## 二、创建Module
### step 1. 创建必要的目录与文件
创建一个senta_test的目录,并在senta_test目录下分别创建module.py、processor.py、vocab.list,其中
|文件名|用途|
|-|-|
|module.py|主模块,提供Module的实现代码|
|processor.py|辅助模块,提供词表加载的方法|
|vocab.list|存放词表|
```cmd
➜ tree senta_test
senta_test/
├── vocab.list
├── module.py
└── processor.py
```
### step 2. 实现辅助模块processor
在processor.py中实现一个load_vocab接口用于读取词表
```python
def load_vocab(vocab_path):
with open(vocab_path) as file:
return file.read().split()
```
### step 3. 编写Module处理代码
module.py文件为Module的入口代码所在,我们需要在其中实现预测逻辑。
#### step 3_1. 引入必要的头文件
```python
import argparse
import os
import paddlehub as hub
from paddlehub.module.module import runnable, moduleinfo
from senta_test.processor import load_vocab
```
**NOTE:** 当引用Module中模块时,需要输入全路径,如senta_test.processor
#### step 3_2. 定义SentaTest类
module.py中需要有一个继承了hub.Module的类存在,该类负责实现预测逻辑,并使用moduleinfo填写基本信息。当使用hub.Module(name="senta_test")加载Module时,PaddleHub会自动创建SentaTest的对象并返回。
```python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
```
#### step 3_3. 执行必要的初始化
```python
def __init__(self):
# add arg parser
self.parser = argparse.ArgumentParser(
description="Run the senta_test module.",
prog='hub run senta_test',
usage='%(prog)s',
add_help=True)
self.parser.add_argument(
'--input_text', type=str, default=None, help="text to predict")
# load word dict
vocab_path = os.path.join(self.directory, "vocab.list")
self.vocab = load_vocab(vocab_path)
```
`注意`:执行类对象默认内置了directory属性,可以直接获取到Module所在路径
#### step 3_4. 完善预测逻辑
```python
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
```
#### step 3_5. 支持命令行调用
如果希望Module可以支持命令行调用,则需要提供一个经过runnable修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
如果不需要提供命令行预测功能,则可以不实现该接口,PaddleHub在用命令行执行时,会自动发现该Module不支持命令行方式,并给出提示。
```python
@runnable
def run_cmd(self, argvs):
args = self.parser.parse_args(argvs)
texts = [args.input_text]
return self.sentiment_classify(texts)
```
#### step 3_6. 支持serving调用
如果希望Module可以支持PaddleHub Serving部署预测服务,则需要提供一个经过serving修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
如果不需要提供PaddleHub Serving部署预测服务,则可以不需要加上serving修饰。
```python
@serving
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
```
### 完整代码
* [module.py](../../../modules/demo/senta_test/module.py)
* [processor.py](../../../modules/demo/senta_test/processor.py)
<br/>
## 三、安装并测试Module
完成Module编写后,我们可以通过以下方式测试该Module
### 调用方法1
将Module安装到本机中,再通过Hub.Module(name=...)加载
```shell
➜ hub install senta_test
```
```python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
```
### 调用方法2
直接通过Hub.Module(directory=...)加载
```python
import paddlehub as hub
senta_test = hub.Module(directory="senta_test/")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
```
### 调用方法3
将senta_test作为路径加到环境变量中,直接加载SentaTest对象
```shell
export PYTHONPATH=senta_test:$PYTHONPATH
```
```python
from senta_test.module import SentaTest
SentaTest.sentiment_classify(texts=["这部电影太差劲了"])
```
### 调用方法4
将Module安装到本机中,再通过hub run运行
```shell
➜ hub install senta_test
➜ hub run senta_test --input_text "这部电影太差劲了"
```
## 四、发布Module
在完成Module的开发和测试后,如果想要将模型分享给其他人使用,可以通过 **上传Module到PaddleHub官网** 的方式发布模型:
https://www.paddlepaddle.org.cn/hub
我们会在尽可能短的时间内完成Module的审核并给出反馈,通过审核并上线后,Module将展示在PaddleHub官网的`开发者贡献模型`中,用户可以像加载其他官方Module一样加载该Module。
...@@ -36,22 +36,9 @@ paddlehub.server_check() ...@@ -36,22 +36,9 @@ paddlehub.server_check()
# 如果无法连接远端PaddleHub-Server,则显示Request Hub-Server unsuccessfully. # 如果无法连接远端PaddleHub-Server,则显示Request Hub-Server unsuccessfully.
``` ```
## PaddleHub Module是否支持多线程,如何加快Module训练或预测的速度。 ## PaddleHub Module是否支持多线程加速预测?
PaddleHub Module不支持多线程,可以通过使用GPU、加大batch_size等措施加速训练或者预测。 由于PaddlePaddle自身的限制,PaddleHub无法通过多线程来加速模型预测。
以下示例为加速LAC Module分词的方法:
```python
results = lac.lexical_analysis(data=inputs, use_gpu=True, batch_size=10)
```
## 如何获取输入句子经过ERNIE编码后的句子表示Embedding?
具体参考[BERT Services](./tutorial/bert_service.md)使用说明
## 在虚拟机Python2环境中使用hub命令报错“Illegal instruction”
请先排除numpy的版本问题:使用pip show numpy检查numpy的版本,如果是1.16.0~1.16.3会出现[numpy错误](https://github.com/numpy/numpy/issues/9532),请pip uinstall numpy后再用pip install numpy<1.17.0安装新版本的numpy。
## 如何修改PaddleHub的修改预训练模型存放路径? ## 如何修改PaddleHub的修改预训练模型存放路径?
......
# 声音分类
本示例展示如何使用PaddleHub Fine-tune API以及CNN14等预训练模型完成声音分类和Tagging的任务。
CNN14等预训练模型的详情,请参考论文[PANNs: Large-Scale Pretrained Audio Neural Networks for Audio Pattern Recognition](https://arxiv.org/pdf/1912.10211.pdf)和代码[audioset_tagging_cnn](https://github.com/qiuqiangkong/audioset_tagging_cnn)
## 如何开始Fine-tune
我们以环境声音分类公开数据集[ESC50](https://github.com/karolpiczak/ESC-50)为示例数据集,可以运行下面的命令,在训练集(train.npz)上进行模型训练,并在开发集(dev.npz)验证。通过如下命令,即可启动训练。
```python
# 设置使用的GPU卡号
export CUDA_VISIBLE_DEVICES=0
python train.py
```
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 选择模型
```python
import paddle
import paddlehub as hub
from paddlehub.datasets import ESC50
model = hub.Module(name='panns_cnn14', version='1.0.0', task='sound-cls', num_class=ESC50.num_class)
```
其中,参数:
- `name`: 模型名称,可以选择`panns_cnn14``panns_cnn10``panns_cnn6`,具体的模型参数信息可见下表。
- `version`: module版本号
- `task`:模型的执行任务。`sound-cls`表示声音分类任务;`None`表示Audio Tagging任务。
- `num_classes`:表示当前声音分类任务的类别数,根据具体使用的数据集确定。
目前可选用的预训练模型:
模型名 | PaddleHub Module
-----------| :------:
CNN14 | `hub.Module(name='panns_cnn14')`
CNN10 | `hub.Module(name='panns_cnn10')`
CNN6 | `hub.Module(name='panns_cnn6')`
### Step2: 加载数据集
```python
train_dataset = ESC50(mode='train')
dev_dataset = ESC50(mode='dev')
```
### Step3: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.AdamW(learning_rate=5e-5, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir='./', use_gpu=True)
```
#### 优化策略
Paddle2.0提供了多种优化器选择,如`SGD`, `AdamW`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/Overview_cn.html)
其中`AdamW`:
- `learning_rate`: 全局学习率。默认为1e-3;
- `parameters`: 待优化模型参数。
其余可配置参数请参考[AdamW](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/adamw/AdamW_cn.html#cn-api-paddle-optimizer-adamw)
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
- `model`: 被优化模型;
- `optimizer`: 优化器选择;
- `use_vdl`: 是否使用vdl可视化训练过程;
- `checkpoint_dir`: 保存模型参数的地址;
- `compare_metrics`: 保存最优模型的衡量指标;
### Step4: 执行训练和模型评估
```python
trainer.train(
train_dataset,
epochs=50,
batch_size=16,
eval_dataset=dev_dataset,
save_interval=10,
)
trainer.evaluate(dev_dataset, batch_size=16)
```
`trainer.train`执行模型的训练,其参数可以控制具体的训练过程,主要的参数包含:
- `train_dataset`: 训练时所用的数据集;
- `epochs`: 训练轮数;
- `batch_size`: 训练时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size;
- `num_workers`: works的数量,默认为0;
- `eval_dataset`: 验证集;
- `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
- `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
`trainer.evaluate`执行模型的评估,主要的参数包含:
- `eval_dataset`: 模型评估时所用的数据集;
- `batch_size`: 模型评估时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
以下代码将本地的音频文件`./cat.wav`作为预测数据,使用训好的模型对它进行分类,输出结果。
```python
import os
import librosa
import paddlehub as hub
from paddlehub.datasets import ESC50
wav = './cat.wav' # 存储在本地的需要预测的wav文件
sr = 44100 # 音频文件的采样率
checkpoint = './best_model/model.pdparams' # 模型checkpoint
label_map = {idx: label for idx, label in enumerate(ESC50.label_list)}
model = hub.Module(name='panns_cnn14',
version='1.0.0',
task='sound-cls',
num_class=ESC50.num_class,
label_map=label_map,
load_checkpoint=checkpoint)
data = [librosa.load(wav, sr=sr)[0]]
result = model.predict(data, sample_rate=sr, batch_size=1, feat_type='mel', use_gpu=True)
print(result[0]) # result[0]包含音频文件属于各类别的概率值
```
## Audio Tagging
当前使用的模型是基于[Audioset数据集](https://research.google.com/audioset/)的预训练模型,除了以上的针对特定声音分类数据集的finetune任务,模型还支持基于Audioset 527个标签的Tagging功能。
以下代码将本地的音频文件`./cat.wav`作为预测数据,使用预训练模型对它进行打分,输出top 10的标签和对应的得分。
```python
import os
import librosa
import numpy as np
import paddlehub as hub
from paddlehub.env import MODULE_HOME
wav = './cat.wav' # 存储在本地的需要预测的wav文件
sr = 44100 # 音频文件的采样率
topk = 10 # 展示音频得分前10的标签和分数
# 读取audioset数据集的label文件
label_file = os.path.join(MODULE_HOME, 'panns_cnn14', 'audioset_labels.txt')
label_map = {}
with open(label_file, 'r') as f:
for i, l in enumerate(f.readlines()):
label_map[i] = l.strip()
model = hub.Module(name='panns_cnn14', version='1.0.0', task=None, label_map=label_map)
data = [librosa.load(wav, sr=sr)[0]]
result = model.predict(data, sample_rate=sr, batch_size=1, feat_type='mel', use_gpu=True)
# 打印topk的类别和对应得分
msg = ''
for label, score in list(result[0].items())[:topk]:
msg += f'{label}: {score}\n'
print(msg)
```
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.1.0
# 图像分类
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 命令行预测
```shell
$ hub run resnet50_vd_imagenet_ssld --input_path "/PATH/TO/IMAGE" --top_k 5
```
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用resnet50_vd_imagenet_ssld对[Flowers](../../docs/reference/datasets.md#class-hubdatasetsflowers)等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
import paddlehub.vision.transforms as T
transforms = T.Compose([T.Resize((256, 256)),
T.CenterCrop(224),
T.Normalize(mean=[0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])],
to_rgb=True)
```
`transforms` 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```python
from paddlehub.datasets import Flowers
flowers = Flowers(transforms)
flowers_validate = Flowers(transforms, mode='val')
```
* `transforms`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, `val`, 默认为`train`
数据集的准备代码可以参考 [flowers.py](../../paddlehub/datasets/flowers.py)`hub.datasets.Flowers()` 会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
module = hub.Module(name="resnet50_vd_imagenet_ssld", label_list=["roses", "tulips", "daisy", "sunflowers", "dandelion"])
```
* `name`: 选择预训练模型的名字。
* `label_list`: 设置输出分类类别,默认为Imagenet2012类别。
PaddleHub提供许多图像分类预训练模型,如xception、mobilenet、efficientnet等,详细信息参见[图像分类模型](https://www.paddlepaddle.org.cn/hub?filter=en_category&value=ImageClassification)
如果想尝试efficientnet模型,只需要更换Module中的`name`参数即可.
```python
# 更换name参数即可无缝切换efficientnet模型, 代码示例如下
module = hub.Module(name="efficientnetb7_imagenet")
```
**NOTE:**目前部分模型还没有完全升级到2.0版本,敬请期待。
### Step4: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='img_classification_ckpt')
trainer.train(flowers, epochs=100, batch_size=32, eval_dataset=flowers_validate, save_interval=1)
```
#### 优化策略
Paddle2.0-rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`Adam`:
* `learning_rate`: 全局学习率。默认为1e-3;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='resnet50_vd_imagenet_ssld', label_list=["roses", "tulips", "daisy", "sunflowers", "dandelion"], load_checkpoint='/PATH/TO/CHECKPOINT')
result = model.predict(['flower.jpg'])
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线分类任务服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m resnet50_vd_imagenet_ssld
```
这样就完成了一个分类任务服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)], 'top_k':2}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/resnet50_vd_imagenet_ssld"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
data =r.json()["results"]['data']
```
### 查看代码
https://github.com/PaddlePaddle/models/tree/develop/PaddleCV/image_classification
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 图像着色
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 命令行预测
```
$ hub run user_guided_colorization --input_path "/PATH/TO/IMAGE"
```
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用user_guided_colorization模型对[Canvas](../../docs/reference/datasets.md#class-hubdatasetsCanvas)等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
import paddlehub.vision.transforms as T
transform = T.Compose([T.Resize((256, 256), interpolation='NEAREST'),
T.RandomPaddingCrop(crop_size=176),
T.RGB2LAB()], to_rgb=True)
```
`transforms`数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
**NOTE:** 要将`T.Compose``to_rgb`设定为True.
### Step2: 下载数据集并使用
```python
from paddlehub.datasets import Canvas
color_set = Canvas(transform=transform, mode='train')
```
* `transform`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test` 默认为`train`
数据集的准备代码可以参考 [canvas.py](../../paddlehub/datasets/canvas.py)`hub.datasets.Canvas()` 会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
model = hub.Module(name='user_guided_colorization', load_checkpoint=None)
model.set_config(classification=True, prob=1)
```
* `name`:加载模型的名字。
* `load_checkpoint`: 是否加载自己训练的模型,若为None,则加载提供的模型默认参数。
* `classification`: 着色模型分两部分训练,开始阶段`classification`设置为True, 用于浅层网络训练。训练后期将`classification`设置为False, 用于训练网络的输出层。
* `prob`: 每张输入图不加一个先验彩色块的概率,默认为1,即不加入先验彩色块。例如,当`prob`设定为0.9时,一张图上有两个先验彩色块的概率为(1-0.9)*(1-0.9)*0.9=0.009.
### Step4: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.Adam(learning_rate=0.0001, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='img_colorization_ckpt_cls_1')
trainer.train(color_set, epochs=201, batch_size=25, eval_dataset=color_set, log_interval=10, save_interval=10)
```
#### 优化策略
Paddle2.0-rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`Adam`:
* `learning_rate`: 全局学习率。默认为1e-4;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证过程所用的数据集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='user_guided_colorization', load_checkpoint='/PATH/TO/CHECKPOINT')
model.set_config(prob=0.1)
result = model.predict(images=['house.png'])
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。若想获取油画风着色效果,请下载参数文件[油画着色](https://paddlehub.bj.bcebos.com/dygraph/models/canvas_rc.pdparams)
**Args**
* `images`:原始图像路径或者BGR格式图片;
* `visualization`: 是否可视化,默认为True;
* `save_path`: 保存结果的路径,默认为'result'。
## 服务部署
PaddleHub Serving可以部署一个在线着色任务服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m user_guided_colorization
```
这样就完成了一个着色任务服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/user_guided_colorization"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
data = base64_to_cv2(r.json()["results"]['data'][0]['fake_reg'])
cv2.imwrite('color.png', data)
```
### 查看代码
https://github.com/richzhang/colorization-pytorch
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 图像分割
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用ocrnet_hrnetw18_voc模型对OpticDiscSeg等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
from paddlehub.vision.segmentation_transforms import Compose, Resize, Normalize
transform = Compose([Resize(target_size=(512, 512)), Normalize()])
```
`segmentation_transforms` 数据增强模块定义了丰富的针对图像分割数据的预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```python
from paddlehub.datasets import OpticDiscSeg
train_reader = OpticDiscSeg(transform mode='train')
```
* `transform`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, `val`, 默认为`train`
数据集的准备代码可以参考 [opticdiscseg.py](../../paddlehub/datasets/opticdiscseg.py)`hub.datasets.OpticDiscSeg()`会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
model = hub.Module(name='ocrnet_hrnetw18_voc', num_classes=2, pretrained=None)
```
* `name`: 选择预训练模型的名字。
* `num_classes`: 分割模型的类别数目。
* `pretrained`: 是否加载自己训练的模型,若为None,则加载提供的模型默认参数。
### Step4: 选择优化策略和运行配置
```python
scheduler = paddle.optimizer.lr.PolynomialDecay(learning_rate=0.01, decay_steps=1000, power=0.9, end_lr=0.0001)
optimizer = paddle.optimizer.Adam(learning_rate=scheduler, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='test_ckpt_img_ocr', use_gpu=True)
```
#### 优化策略
其中`Adam`:
* `learning_rate`: 全局学习率。
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_gpu`: 是否使用gpu,默认为False;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import cv2
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='ocrnet_hrnetw18_voc', pretrained='/PATH/TO/CHECKPOINT')
img = cv2.imread("/PATH/TO/IMAGE")
model.predict(images=[img], visualization=True)
```
参数配置正确后,请执行脚本`python predict.py`
**Args**
* `images`:原始图像路径或BGR格式图片;
* `visualization`: 是否可视化,默认为True;
* `save_path`: 保存结果的路径,默认保存路径为'seg_result'。
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线图像分割服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m ocrnet_hrnetw18_voc
```
这样就完成了一个图像分割服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/ocrnet_hrnetw18_voc"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
mask = base64_to_cv2(r.json()["results"][0])
```
### 查看代码
https://github.com/PaddlePaddle/PaddleSeg
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
# 序列标注
在2017年之前,工业界和学术界对NLP文本处理依赖于序列模型[Recurrent Neural Network (RNN)](https://baike.baidu.com/item/%E5%BE%AA%E7%8E%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/23199490?fromtitle=RNN&fromid=5707183&fr=aladdin).
![](http://colah.github.io/posts/2015-09-NN-Types-FP/img/RNN-general.png)
近年来随着深度学习的发展,模型参数数量飞速增长,为了训练这些参数,需要更大的数据集来避免过拟合。然而,对于大部分NLP任务来说,构建大规模的标注数据集成本过高,非常困难,特别是对于句法和语义相关的任务。相比之下,大规模的未标注语料库的构建则相对容易。最近的研究表明,基于大规模未标注语料库的预训练模型(Pretrained Models, PTM) 能够习得通用的语言表示,将预训练模型Fine-tune到下游任务,能够获得出色的表现。另外,预训练模型能够避免从零开始训练模型。
![](https://ai-studio-static-online.cdn.bcebos.com/327f44ff3ed24493adca5ddc4dc24bf61eebe67c84a6492f872406f464fde91e)
本示例将展示如何使用PaddleHub Transformer模型(如 ERNIE、BERT、RoBERTa等模型)Module 以动态图方式fine-tune并完成预测任务。
## 如何开始Fine-tune
我们以微软亚洲研究院发布的中文实体识别数据集MSRA-NER为示例数据集,可以运行下面的命令,在训练集(train.tsv)上进行模型训练,并在开发集(dev.tsv)验证。通过如下命令,即可启动训练。
```shell
# 设置使用的GPU卡号
export CUDA_VISIBLE_DEVICES=0
python train.py
```
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 选择模型
在命名实体识别的任务中,因不同的数据集标识实体的标签不同,评测的方式也有所差异。因此,在初始化模型的之前,需要先确定实际标签的形式,下方的`label_list`则是MSRA-NER数据集中使用的标签类别。
如果用户使用的实体识别的数据集的标签方式与MSRA-NER不同,则需要自行根据数据集确定。
```python
label_list = hub.datasets.MSRA_NER.label_list
label_map = {
idx: label for idx, label in enumerate(label_list)
}
```
接下来创建任务所使用的`model`
```python
import paddlehub as hub
model = hub.Module(name='ernie_tiny', version='2.0.1', task='token-cls', label_map=label_map)
```
其中,参数:
* `name`:模型名称,可以选择`ernie``ernie_tiny``bert-base-cased``bert-base-chinese`, `roberta-wwm-ext``roberta-wwm-ext-large`等。
* `version`:module版本号
* `task`:fine-tune任务。此处为`token-cls`,表示序列标注任务。
* `label_map`:数据集中的标签信息,实体识别任务中需要根据不同标签种类对模型性能进行评价。
PaddleHub还提供BERT等模型可供选择, 当前支持序列标注任务的模型对应的加载示例如下:
模型名 | PaddleHub Module
---------------------------------- | :------:
ERNIE, Chinese | `hub.Module(name='ernie')`
ERNIE tiny, Chinese | `hub.Module(name='ernie_tiny')`
ERNIE 2.0 Base, English | `hub.Module(name='ernie_v2_eng_base')`
ERNIE 2.0 Large, English | `hub.Module(name='ernie_v2_eng_large')`
BERT-Base, English Cased | `hub.Module(name='bert-base-cased')`
BERT-Base, English Uncased | `hub.Module(name='bert-base-uncased')`
BERT-Large, English Cased | `hub.Module(name='bert-large-cased')`
BERT-Large, English Uncased | `hub.Module(name='bert-large-uncased')`
BERT-Base, Multilingual Cased | `hub.Module(nane='bert-base-multilingual-cased')`
BERT-Base, Multilingual Uncased | `hub.Module(nane='bert-base-multilingual-uncased')`
BERT-Base, Chinese | `hub.Module(name='bert-base-chinese')`
BERT-wwm, Chinese | `hub.Module(name='chinese-bert-wwm')`
BERT-wwm-ext, Chinese | `hub.Module(name='chinese-bert-wwm-ext')`
RoBERTa-wwm-ext, Chinese | `hub.Module(name='roberta-wwm-ext')`
RoBERTa-wwm-ext-large, Chinese | `hub.Module(name='roberta-wwm-ext-large')`
RBT3, Chinese | `hub.Module(name='rbt3')`
RBTL3, Chinese | `hub.Module(name='rbtl3')`
ELECTRA-Small, English | `hub.Module(name='electra-small')`
ELECTRA-Base, English | `hub.Module(name='electra-base')`
ELECTRA-Large, English | `hub.Module(name='electra-large')`
ELECTRA-Base, Chinese | `hub.Module(name='chinese-electra-base')`
ELECTRA-Small, Chinese | `hub.Module(name='chinese-electra-small')`
通过以上的一行代码,`model`初始化为一个适用于序列标注任务的模型,为ERNIE Tiny的预训练模型后拼接上一个输出token共享的全连接网络(Full Connected)。
![](https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=224484727,3049769188&fm=15&gp=0.jpg)
以上图片来自于:https://arxiv.org/pdf/1810.04805.pdf
### Step2: 下载并加载数据集
```python
train_dataset = hub.datasets.MSRA_NER(
tokenizer=model.get_tokenizer(), max_seq_len=128, mode='train')
dev_dataset = hub.datasets.MSRA_NER(
tokenizer=model.get_tokenizer(), max_seq_len=128, mode='dev')
```
* `tokenizer`:表示该module所需用到的tokenizer,其将对输入文本完成切词,并转化成module运行所需模型输入格式。
* `mode`:选择数据模式,可选项有 `train`, `test`, `val`, 默认为`train`
* `max_seq_len`:ERNIE/BERT模型使用的最大序列长度,若出现显存不足,请适当调低这一参数。
预训练模型ERNIE对中文数据的处理是以字为单位,tokenizer作用为将原始输入文本转化成模型model可以接受的输入数据形式。 PaddleHub 2.0中的各种预训练模型已经内置了相应的tokenizer,可以通过`model.get_tokenizer`方法获取。
![](https://bj.bcebos.com/paddlehub/paddlehub-img/ernie_network_1.png)
![](https://bj.bcebos.com/paddlehub/paddlehub-img/ernie_network_2.png)
### Step3: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.AdamW(learning_rate=5e-5, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir='test_ernie_token_cls', use_gpu=False)
trainer.train(train_dataset, epochs=3, batch_size=32, eval_dataset=dev_dataset)
# 在测试集上评估当前训练模型
trainer.evaluate(test_dataset, batch_size=32)
```
#### 优化策略
Paddle2.0-rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`, `AdamW`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`AdamW`:
* `learning_rate`: 全局学习率。默认为1e-3;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_gpu`: 是否使用GPU训练,默认为False;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: workers的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们以以下数据为待预测数据,使用该模型来进行预测
```text
去年十二月二十四日,市委书记张敬涛召集县市主要负责同志研究信访工作时,提出三问:『假如上访群众是我们的父母姐妹,你会用什么样的感情对待他们?
新华社北京5月7日电国务院副总理李岚清今天在中南海会见了美国前商务部长芭芭拉·弗兰克林。
根据测算,海卫1表面温度已经从“旅行者”号探测器1989年造访时的零下236摄氏度上升到零下234摄氏度。
华裔作家韩素音女士曾三次到大足,称“大足石窟是一座未被开发的金矿”。
```
```python
import paddlehub as hub
split_char = "\002"
label_list = ["B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "O"]
text_a = [
'去年十二月二十四日,市委书记张敬涛召集县市主要负责同志研究信访工作时,提出三问:『假如上访群众是我们的父母姐妹,你会用什么样的感情对待他们?',
'新华社北京5月7日电国务院副总理李岚清今天在中南海会见了美国前商务部长芭芭拉·弗兰克林。',
'根据测算,海卫1表面温度已经从“旅行者”号探测器1989年造访时的零下236摄氏度上升到零下234摄氏度。',
'华裔作家韩素音女士曾三次到大足,称“大足石窟是一座未被开发的金矿”。',
]
data = [[split_char.join(text)] for text in text_a]
label_map = {
idx: label for idx, label in enumerate(label_list)
}
model = hub.Module(
name='ernie_tiny',
version='2.0.1',
task='token-cls',
load_checkpoint='./token_cls_save_dir/best_model/model.pdparams',
label_map=label_map,
)
results = model.predict(data, max_seq_len=50, batch_size=1, use_gpu=False)
for idx, text in enumerate(text_a):
print(f'Data: {text} \t Lable: {", ".join(results[idx][1:len(text)+1])}')
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 风格迁移
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 命令行预测
```
$ hub run msgnet --input_path "/PATH/TO/ORIGIN/IMAGE" --style_path "/PATH/TO/STYLE/IMAGE"
```
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用msgnet模型对[MiniCOCO](../../docs/reference/datasets.md#class-hubdatasetsMiniCOCO)等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
import paddlehub.vision.transforms as T
transform = T.Compose([T.Resize((256, 256), interpolation='LINEAR')])
```
`transforms` 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```python
from paddlehub.datasets.minicoco import MiniCOCO
styledata = MiniCOCO(transform=transform, mode='train')
```
* `transforms`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, 默认为`train`
数据集的准备代码可以参考 [minicoco.py](../../paddlehub/datasets/flowers.py)`hub.datasets.MiniCOCO()`会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
model = hub.Module(name='msgnet', load_checkpoint=None)
```
* `name`: 选择预训练模型的名字。
* `load_checkpoint`: 是否加载自己训练的模型,若为None,则加载提供的模型默认参数。
### Step4: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.Adam(learning_rate=0.0001, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='test_style_ckpt')
trainer.train(styledata, epochs=101, batch_size=4, eval_dataset=styledata, log_interval=10, save_interval=10)
```
#### 优化策略
Paddle2.0rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`Adam`:
* `learning_rate`: 全局学习率。默认为1e-4;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='msgnet', load_checkpoint="/PATH/TO/CHECKPOINT")
result = model.predict(origin=["venice-boat.jpg"], style="candy.jpg", visualization=True, save_path ='style_tranfer')
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
**Args**
* `origin`:原始图像路径或BGR格式图片;
* `style`: 风格图像路径;
* `visualization`: 是否可视化,默认为True;
* `save_path`: 保存结果的路径,默认保存路径为'style_tranfer'。
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线风格迁移服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m msgnet
```
这样就完成了一个风格迁移服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/ORIGIN/IMAGE')
style_im = cv2.imread('/PATH/TO/STYLE/IMAGE')
data = {'images':[[cv2_to_base64(org_im)], cv2_to_base64(style_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/msgnet"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
data = base64_to_cv2(r.json()["results"]['data'][0])
cv2.imwrite('style.png', data)
```
### 查看代码
https://github.com/zhanghang1989/PyTorch-Multi-Style-Transfer
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 文本匹配
在2017年之前,工业界和学术界对NLP文本处理依赖于序列模型[Recurrent Neural Network (RNN)](https://baike.baidu.com/item/%E5%BE%AA%E7%8E%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/23199490?fromtitle=RNN&fromid=5707183&fr=aladdin).
![](http://colah.github.io/posts/2015-09-NN-Types-FP/img/RNN-general.png)
近年来随着深度学习的发展,模型参数数量飞速增长,为了训练这些参数,需要更大的数据集来避免过拟合。然而,对于大部分NLP任务来说,构建大规模的标注数据集成本过高,非常困难,特别是对于句法和语义相关的任务。相比之下,大规模的未标注语料库的构建则相对容易。最近的研究表明,基于大规模未标注语料库的预训练模型(Pretrained Models, PTM) 能够习得通用的语言表示,将预训练模型Fine-tune到下游任务,能够获得出色的表现。另外,预训练模型能够避免从零开始训练模型。
![](https://ai-studio-static-online.cdn.bcebos.com/327f44ff3ed24493adca5ddc4dc24bf61eebe67c84a6492f872406f464fde91e)
本示例将展示如何使用PaddleHub Transformer模型(如 ERNIE、BERT、RoBERTa等模型)Module 以动态图方式fine-tune并完成预测任务。
## 文本匹配
使用预训练模型ERNIE完成文本匹配任务,大家可能会想到将query和title文本拼接,之后输入ERNIE中,取`CLS`特征(pooled_output),之后输出全连接层,进行二分类。如下图ERNIE用于句对分类任务的用法:
![](https://camo.githubusercontent.com/5e1867ee2b6fc3a0f94c7b2c87a4d987fed4c440d4d9c80726e5798900880027/68747470733a2f2f61692d73747564696f2d7374617469632d6f6e6c696e652e63646e2e626365626f732e636f6d2f34353434303032396330373234306164383964363635633562313736653633323937653935383465316461323465303262373964643534666239393066373461)
然而,以上用法的问题在于,ERNIE的模型参数非常庞大,导致计算量非常大,预测的速度也不够理想。从而达不到线上业务的要求。针对该问题,使用Sentence Transformer网络可以优化计算量。
Sentence Transformer采用了双塔(Siamese)的网络结构。Query和Title分别输入Transformer网络,共享网络参数,得到各自的token embedding特征。之后对token embedding进行pooling(此处教程使用mean pooling操作),之后输出分别记作u,v。之后将三个表征(u,v,|u-v|)拼接起来,进行二分类。网络结构如下图所示。
![](https://camo.githubusercontent.com/80e65553f0c82886a27897a0a151ee9745e6e2def310d6649c8a68e2672c06c2/68747470733a2f2f61692d73747564696f2d7374617469632d6f6e6c696e652e63646e2e626365626f732e636f6d2f31303339393837303365313334613731383438383335313161353338363230653136666564303435653236313464636338616661636563343436363030343338)
更多关于Sentence Transformer的信息可以参考论文:https://arxiv.org/abs/1908.10084
## 如何开始Fine-tune
我们以中文文本匹配数据集LCQMC为示例数据集,可以运行下面的命令,在训练集(train.tsv)上进行模型训练,并在开发集(dev.tsv)验证和测试集测试(test.tsv)。通过如下命令,即可启动训练。
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 选择模型
```python
import paddlehub as hub
model = hub.Module(name='ernie_tiny', version='2.0.2', task='text-matching')
```
其中,参数:
* `name`:模型名称,可以选择`ernie``ernie_tiny``bert-base-cased``bert-base-chinese`, `roberta-wwm-ext``roberta-wwm-ext-large`等。
* `version`:module版本号
* `task`:fine-tune任务。此处为`text-matching`,表示文本匹配任务。
PaddleHub还提供BERT等模型可供选择, 当前支持文本分类任务的模型对应的加载示例如下:
模型名 | PaddleHub Module
---------------------------------- | :------:
ERNIE, Chinese | `hub.Module(name='ernie')`
ERNIE tiny, Chinese | `hub.Module(name='ernie_tiny')`
ERNIE 2.0 Base, English | `hub.Module(name='ernie_v2_eng_base')`
ERNIE 2.0 Large, English | `hub.Module(name='ernie_v2_eng_large')`
BERT-Base, English Cased | `hub.Module(name='bert-base-cased')`
BERT-Base, English Uncased | `hub.Module(name='bert-base-uncased')`
BERT-Large, English Cased | `hub.Module(name='bert-large-cased')`
BERT-Large, English Uncased | `hub.Module(name='bert-large-uncased')`
BERT-Base, Multilingual Cased | `hub.Module(nane='bert-base-multilingual-cased')`
BERT-Base, Multilingual Uncased | `hub.Module(nane='bert-base-multilingual-uncased')`
BERT-Base, Chinese | `hub.Module(name='bert-base-chinese')`
BERT-wwm, Chinese | `hub.Module(name='chinese-bert-wwm')`
BERT-wwm-ext, Chinese | `hub.Module(name='chinese-bert-wwm-ext')`
RoBERTa-wwm-ext, Chinese | `hub.Module(name='roberta-wwm-ext')`
RoBERTa-wwm-ext-large, Chinese | `hub.Module(name='roberta-wwm-ext-large')`
RBT3, Chinese | `hub.Module(name='rbt3')`
RBTL3, Chinese | `hub.Module(name='rbtl3')`
ELECTRA-Small, English | `hub.Module(name='electra-small')`
ELECTRA-Base, English | `hub.Module(name='electra-base')`
ELECTRA-Large, English | `hub.Module(name='electra-large')`
ELECTRA-Base, Chinese | `hub.Module(name='chinese-electra-base')`
ELECTRA-Small, Chinese | `hub.Module(name='chinese-electra-small')`
通过以上的一行代码,`model`初始化为一个适用于文本匹配任务的双塔(Siamese)结构模型,。
### Step2: 下载并加载数据集
```python
train_dataset = LCQMC(tokenizer=model.get_tokenizer(), max_seq_len=128, mode='train')
dev_dataset = LCQMC(tokenizer=model.get_tokenizer(), max_seq_len=128, mode='dev')
test_dataset = LCQMC(tokenizer=model.get_tokenizer(), max_seq_len=128, mode='test')
```
* `tokenizer`:表示该module所需用到的tokenizer,其将对输入文本完成切词,并转化成module运行所需模型输入格式。
* `mode`:选择数据模式,可选项有 `train`, `dev`, `test`,默认为`train`
* `max_seq_len`:ERNIE/BERT模型使用的最大序列长度,若出现显存不足,请适当调低这一参数。
预训练模型ERNIE对中文数据的处理是以字为单位,tokenizer作用为将原始输入文本转化成模型model可以接受的输入数据形式。 PaddleHub 2.0中的各种预训练模型已经内置了相应的tokenizer,可以通过`model.get_tokenizer`方法获取。
### Step3: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.AdamW(learning_rate=5e-5, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir='./', use_gpu=True)
```
#### 优化策略
Paddle2.0提供了多种优化器选择,如`SGD`, `AdamW`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/Overview_cn.html)
其中`AdamW`:
- `learning_rate`: 全局学习率。默认为1e-3;
- `parameters`: 待优化模型参数。
其余可配置参数请参考[AdamW](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/adamw/AdamW_cn.html#cn-api-paddle-optimizer-adamw)
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
- `model`: 被优化模型;
- `optimizer`: 优化器选择;
- `use_vdl`: 是否使用vdl可视化训练过程;
- `checkpoint_dir`: 保存模型参数的地址;
- `compare_metrics`: 保存最优模型的衡量指标;
### Step4: 执行训练和模型评估
```python
trainer.train(
train_dataset,
epochs=10,
batch_size=32,
eval_dataset=dev_dataset,
save_interval=2,
)
trainer.evaluate(test_dataset, batch_size=32)
```
`trainer.train`执行模型的训练,其参数可以控制具体的训练过程,主要的参数包含:
- `train_dataset`: 训练时所用的数据集;
- `epochs`: 训练轮数;
- `batch_size`: 训练时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size;
- `num_workers`: works的数量,默认为0;
- `eval_dataset`: 验证集;
- `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
- `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
`trainer.evaluate`执行模型的评估,主要的参数包含:
- `eval_dataset`: 模型评估时所用的数据集;
- `batch_size`: 模型评估时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
以下代码将使用最优模型来进行预测:
```python
import paddlehub as hub
data = [
['这个表情叫什么', '这个猫的表情叫什么'],
['什么是智能手环', '智能手环有什么用'],
['介绍几本好看的都市异能小说,要完结的!', '求一本好看点的都市异能小说,要完结的'],
['一只蜜蜂落在日历上(打一成语)', '一只蜜蜂停在日历上(猜一成语)'],
['一盒香烟不拆开能存放多久?', '一条没拆封的香烟能存放多久。'],
]
label_map = {0: 'similar', 1: 'dissimilar'}
model = hub.Module(
name='ernie_tiny',
version='2.0.2',
task='text-matching',
load_checkpoint='./checkpoint/best_model/model.pdparams',
label_map=label_map)
results = model.predict(data, max_seq_len=128, batch_size=1, use_gpu=True)
for idx, texts in enumerate(data):
print('TextA: {}\tTextB: {}\t Label: {}'.format(texts[0], texts[1], results[idx]))
```
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
============
安装
============
环境依赖
========================
* 操作系统:Windows/Mac/Linux
* Python>=3.6
* PaddlePaddle>=2.0.0
.. code-block:: shell
# 安装gpu版本的PaddlePaddle
pip install paddlepaddle-gpu -U
# 或者安装cpu版本的paddlepaddle
# pip install paddlepaddle -U
安装命令
========================
在安装PaddleHub之前,请先安装PaddlePaddle深度学习框架,更多安装说明请查阅`飞桨快速安装 <https://www.paddlepaddle.org.cn/install/quick>`_.
.. code-block:: shell
pip install paddlehub==2.1.0
除上述依赖外,PaddleHub的预训练模型和预置数据集需要连接服务端进行下载,请确保机器可以正常访问网络。若本地已存在相关的数据集和预训练模型,则可以离线运行PaddleHub。
.. note::
使用PaddleHub下载数据集、预训练模型等,要求机器可以访问外网。可以使用`server_check()`可以检查本地与远端PaddleHub-Server的连接状态,使用方法如下:
.. code-block:: Python
import paddlehub
paddlehub.server_check()
# 如果可以连接远端PaddleHub-Server,则显示Request Hub-Server successfully。
# 如果无法连接远端PaddleHub-Server,则显示Request Hub-Server unsuccessfully。
\ No newline at end of file
=================
快速体验
=================
在PaddleHub中,Module代表一个可执行模块,一般来讲就是一个可以端到端预测的预训练模型(例如目标检测模型、中文词法分析模型),又或者是一个需要根据下游任务进一步微调(迁移学习)的模型,例如BERT/ERNIE。
PaddleHub采用模型即软件的设计理念,所有的预训练模型与Python软件包类似,具备版本的概念。
本教程会带您快速体验如何使用PaddleHub提供的预训练模型来完成一些常见的AI任务。
下载测试图片
=============================================
首先,使用`wget`下载一张测试图片
.. code-block:: shell
# 下载图片
wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
.. image:: ../../imgs/humanseg_test.png
:width: 300px
人像分割
=============================================
人像分割任务旨在将输入图片中的人像和背景区分开来。该任务有很多的应用场景,例如背景虚化、背景替换、影视后期处理等等。我们使用`humanseg_lite <https://www.paddlepaddle.org.cn/hubdetail?name=humanseg_lite&en_category=ImageSegmentation>`_ 来展示这个功能。
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="humanseg_lite", version="1.1.1")
module = hub.Module(name="humanseg_lite")
res = module.segmentation(
paths = ["./test_image.jpg"],
visualization=True,
output_dir='humanseg_output')
.. image:: ../../imgs/output_8_3.png
:width: 300px
人体解析
=============================================
人体解析是人像分割的细粒度任务。该任务旨在提取输入图片中人体的不同部件。相关模型经常和新兴的GAN模型一起使用,应用场景包括美颜、换衣服等等。我们使用`ace2p <https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation>`_ 来展示这个功能。
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="ace2p", version="1.1.0")
module = hub.Module(name="ace2p")
res = module.segment(
paths = ["./test_image.jpg"],
visualization=True,
output_dir='ace2p_output')
.. image:: ../../imgs/output_12_3.png
:width: 300px
人脸检测
=============================================
人脸检测任务旨在检测出输入图片中的每一张人脸的位置。应用的场景包括视频监控、人流量估计等等场景。我们使用`ultra_light_fast_generic_face_detector_1mb_640 <https://www.paddlepaddle.org.cn/hubdetail?name=ultra_light_fast_generic_face_detector_1mb_640&en_category=FaceDetection>`_ 来展示这个功能。
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640", version="1.1.2")
module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
res = module.face_detection(
paths = ["./test_image.jpg"],
visualization=True,
output_dir='face_detection_output')
.. image:: ../../imgs/output_15_3.png
:width: 300px
关键点检测
=============================================
关键点检测任务旨在识别输入图片中每一个人体的不同关键点信息,例如头部、肩膀、关节等等。依赖于模型能力的不同,能够检测到的关键点数量也不同。该任务一般用于人体美型、人体姿态估计等等,我们使用`openpose_body_estimation <https://www.paddlepaddle.org.cn/hubdetail?name=openpose_body_estimation&en_category=KeyPointDetection>`_ 来展示这个功能。
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="openpose_body_estimation", version="1.0.0")
module = hub.Module(name="openpose_body_estimation")
res = module.predict(
img="./test_image.jpg",
visualization=True,
save_path='keypoint_output')
.. image:: ../../imgs/output_18_2.png
:width: 300px
中文词法分析
=============================================
中文词法分析旨在对输入的语句进行分词、词性分析、命名实体识别,我们使用`lac <https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis>`_ 来展示这个功能。
.. code-block:: python
import paddlehub as hub
# lac = hub.Module(name="lac", version="2.2.0")
lac = hub.Module(name="lac")
test_text = ["1996年,曾经是微软员工的加布·纽维尔和麦克·哈灵顿一同创建了Valve软件公司。他们在1996年下半年从id software取得了雷神之锤引擎的使用许可,用来开发半条命系列。"]
print(lac.lexical_analysis(texts = test_text))
----------------
[{'word': ['1996年', ',', '曾经', '是', '微软', '员工', '的', '加布·纽维尔', '和', '麦克·哈灵顿', '一同', '创建', '了', 'Valve软件公司', '。', '他们', '在', '1996年下半年', '从', 'id', ' ', 'software', '取得', '了', '雷神之锤', '引擎', '的', '使用', '许可', ',', '用来', '开发', '半条命', '系列', '。'], 'tag': ['TIME', 'w', 'd', 'v', 'ORG', 'n', 'u', 'PER', 'c', 'PER', 'd', 'v', 'u', 'ORG', 'w', 'r', 'p', 'TIME', 'p', 'nz', 'w', 'n', 'v', 'u', 'n', 'n', 'u', 'vn', 'vn', 'w', 'v', 'v', 'n', 'n', 'w']}]
中文情感分析
=============================================
中文情感分析旨在分析输入语句的情感倾向,我们使用 `senta_bilstm <https://www.paddlepaddle.org.cn/hubdetail?name=senta_bilstm&en_category=SentimentAnalysis>`_ 来展示这个功能。
.. code-block:: python
import paddlehub as hub
# senta = hub.Module(name="senta_bilstm", version="1.2.0")
senta = hub.Module(name="senta_bilstm")
test_text = ["味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。"]
print(senta.sentiment_classify(texts = test_text))
----------------
[{'text': '味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9771, 'negative_probs': 0.0229}]
\ No newline at end of file
===============================
快速入门PaddleHub
===============================
.. toctree::
:maxdepth: 2
get_start/installation.md
get_start/python_use_hub.md
\ No newline at end of file
######################################### ===========================
PaddleHub 文档 关于PaddleHub
######################################### ===========================
.. toctree:: 欢迎使用PaddleHub!这是一个基于飞桨框架的预训练模型应用工具,旨在降低AI模型的使用门槛并促动AI社区的发展。无论您是AI领域的资深开发者,还是对该领域不甚了解却非常感兴趣的用户,PaddleHub都可以对您产生帮助。ou are a senior developer in the AI ​​industry nor an interested person who knows nothing about the AI ​​field, you can benefit from PaddleHub.
:caption: 目录
:maxdepth: 2
:titlesonly:
概述<overview> * PaddleHub旨在为开发者提供丰富的、高质量的、直接可用的预训练模型。
安装<install> * **【无需深度学习背景、无需数据与训练过程】**,可快速使用AI模型,享受人工智能时代红利。
快速体验<quick_experience/quick_index> * 涵盖CV、NLP、Audio、Video主流四大品类,支持**一键预测**、**一键服务化部署**和**快速迁移学习**
部署预训练模型<tutorial/Serving_index> * 全部模型开源下载,**离线可运行**。
API<reference/ref_index>
命令行参考<tutorial/cmdintro> ------------
FAQ<faq>
社区贡献<contribution/contri_index> ===========================
更新历史<release> 概览
\ No newline at end of file ===========================
.. toctree::
:maxdepth: 2
:titlesonly:
快速入门PaddleHub<get_start_index>
教程<tutorial_index>
API<api_index>
常见问题<faq>
社区<community_index>
发版信息<release>
\ No newline at end of file
# 安装
## 环境依赖
* Python>=3.6
* PaddlePaddle>=2.0.0rc
```shell
# 安装gpu版本的PaddlePaddle
pip install paddlepaddle-gpu==2.0.0rc
# 或者安装cpu版本的paddlepaddle
# pip install paddlepaddle==2.0.0rc
```
* 操作系统:Windows/Mac/Linux
## 安装命令
在安装PaddleHub之前,请先安装PaddlePaddle深度学习框架,更多安装说明请查阅[飞桨快速安装](https://www.paddlepaddle.org.cn/install/quick/zh/2.0rc-windows-pip)
```shell
pip install paddlehub==2.0.0rc0
```
除上述依赖外,PaddleHub的预训练模型和预置数据集需要连接服务端进行下载,请确保机器可以正常访问网络。若本地已存在相关的数据集和预训练模型,则可以离线运行PaddleHub。
**NOTE:** 使用PaddleHub下载数据集、预训练模型等,要求机器可以访问外网。可以使用`server_check()`可以检查本地与远端PaddleHub-Server的连接状态,使用方法如下:
```python
import paddlehub
paddlehub.server_check()
# 如果可以连接远端PaddleHub-Server,则显示Request Hub-Server successfully。
# 如果无法连接远端PaddleHub-Server,则显示Request Hub-Server unsuccessfully。
```
@ECHO OFF
pushd %~dp0
REM Command file for Sphinx documentation
if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build
if "%1" == "" goto help
%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end
:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
:end
popd
# 概述
## PaddleHub是什么
PaddleHub是飞桨生态的预训练模型应用工具,开发者可以便捷地使用高质量的预训练模型结合Fine-tune API快速完成模型迁移到部署的全流程工作。PaddleHub提供的预训练模型涵盖了图像分类、目标检测、词法分析、语义模型、情感分析、视频分类、图像生成、图像分割、文本审核、关键点检测等主流模型。更多详情可查看[PaddleHub官网](https://www.paddlepaddle.org.cn/hub)
PaddleHub以预训练模型应用为核心具备以下特点:
* **模型即软件:** 通过Python API或命令行实现模型调用,可快速体验或集成飞桨特色预训练模型。
* **易用的迁移学习:** 通过Fine-tune API,只需少量代码即可完成预训练模型的Fine-tuning。
* **一键模型转服务:** 简单一行命令即可搭建属于自己的深度学习模型API服务完成部署。
![](./imgs/paddlehub_figure.jpg)
<p align='center'>
PaddleHub全景图
</p>
## PaddleHub特性
### 一、模型即软件
PaddleHub采用模型即软件的设计理念,所有的预训练模型与Python软件包类似,具备版本的概念,通过`hub install/uninstall` 可以便捷完成模型的升级和卸载。此外,还可以通过Python的API或命令行实现快速预测的软件集成,更方便地应用和管理深度学习模型,完成模型的搜索、下载、安装、升级、预测等功能。更加详细的使用说明可以参考[PaddleHub命令行工具](./tutorial/cmdintro.md)
目前的预训练模型覆盖了图像分类、语义模型、视频分类、图像生成、图像分割、文本审核、关键点检测等业界主流模型,更多PaddleHub已经发布的模型,请前往 [PaddleHub官网](https://www.paddlepaddle.org.cn/hub) 查看。也可以使用[快速体验](./quickstart.md)通过命令行即可调用预训练模型进行预测。
### 二、易用的迁移学习
迁移学习(Transfer Learning)通俗来讲,就是运用已有的知识来学习新的知识,核心是找到已有知识和新知识之间的相似性。PaddleHub提供了Fine-tune API,只需要少量代码即可完成深度学习模型在计算机视觉场景下的迁移学习,可以在更短的时间完成模型的训练,同时模型具备更好的泛化能力。下图所示是基于PaddleHub,通过数十行代码完成ERNIE工业级文本分类示例:
![](./imgs/paddlehub_finetune.gif)
<p align='center'>
十行代码完成图像风格迁移
</p>
PaddleHub提供了使用Finetune-API和预训练模型完成[图像分类](../demo/image_classification)[图像着色](../demo/colorization)[风格迁移](../demo/style_transfer)等迁移任务的使用示例,详细参见[demo](../demo)
* 场景化使用
PaddleHub在AI Studio上提供了IPython NoteBook形式的demo。用户可以直接在平台上在线体验,链接如下:
|预训练模型|任务类型|数据集|AIStudio链接|备注|
|-|-|-|-|-|
|chinese_text_detection_db_mobile|ocr中文识别|icdar2015数据集|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/507159)|超轻量9M模型,火爆|
|video_tag_lstm|短视频分类|百度自建数据集|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/509776)|大规模,多标签|
|resnet50_vd_animals|动物识别|百度自建动物数据集|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/437648)|八千种动物识别|
|face_landmark_localization|AI川剧变脸|AFW/AFLW|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/402892)||
|face_landmark_localization|人像美颜|AFW/AFLW|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/389512)||
|DeepLabv3+|人像抠图|百度自建数据集|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/341116)||
|ResNet|图像分类|猫狗数据集DogCat|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/147010)||
|ERNIE|文本分类|中文情感分类数据集ChnSentiCorp|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/147006)||
|ERNIE|文本分类|中文新闻分类数据集THUNEWS|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/221999)|本教程讲述了如何将自定义数据集加载,并利用Fine-tune API完成文本分类迁移学习。|
|ERNIE|序列标注|中文序列标注数据集MSRA_NER|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/147009)||
|ERNIE|序列标注|中文快递单数据集Express|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/184200)|本教程讲述了如何将自定义数据集加载,并利用Fine-tune API完成序列标注迁移学习。|
|ERNIE Tiny|文本分类|中文情感分类数据集ChnSentiCorp|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/186443)||
|Senta|文本分类|中文情感分类数据集ChnSentiCorp|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/216846)|本教程讲述了任何利用Senta和Fine-tune API完成情感分类迁移学习。|
|Senta|情感分析预测|N/A|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/215814)||
|LAC|词法分析|N/A|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/215711)||
|Ultra-Light-Fast-Generic-Face-Detector-1MB|人脸检测|N/A|[点击体验](https://aistudio.baidu.com/aistudio/projectdetail/215962)||
**NOTE:** [`飞桨PaddleHub`](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/79927)是PaddleHub的官方账号。
关于PaddleHub快捷完成迁移学习,更多信息参考:
[API](./reference/ref_index.rst)
[自定义数据集如何Fine-tune](tutorial/how_to_load_data.md)
### 三、一键模型转服务
PaddleHub Serving是基于PaddleHub的一键模型服务部署工具,能够通过简单的Hub命令行工具轻松启动一个模型预测在线服务。
其主要包括利用Bert Service实现embedding服务化,以及利用预测模型实现预训练模型预测服务化两大功能。未来还将支持开发者使用PaddleHub Fine-tune API的模型服务化。
关于服务化部署详细信息参见[PaddleHub Serving一键服务部署](tutorial/serving.md)
PaddlePaddle 提供了丰富的模型,使得用户可以采用模块化的方法解决各种学习问题。本文,我们将整体介绍PaddleHub中已经准备好的丰富的预训练模型。
* 如果是想了解具体预训练模型的使用可以继续学习本课程,也可以参考 [PaddleHub预训练模型库](https://www.paddlepaddle.org.cn/hublist)
* 如果想了解更多模型组网网络结构源代码请参考 [飞桨模型库](https://github.com/PaddlePaddle/models)
## PaddleHub预训练模型库
* [飞桨优势特色模型](#百度飞桨独有优势特色模型)
* [图像](#图像)
* [图像分类](#图像分类)
* [目标检测](#目标检测)
* [图像分割](#图像分割)
* [关键点检测](#关键点检测)
* [图像生成](#图像生成)
* [文本](#文本)
* [中文词法分析与词向量](#中文词法分析与词向量)
* [情感分析](#情感分析)
* [文本相似度计算](#文本相似度计算)
* [语义表示](#语义表示)
* [视频](#视频)
## 百度飞桨独有优势特色模型
| | **模型名称** | **Master模型推荐辞** |
| ---------- | :----------------------------------------------------------- | ---------------------------------------------------------- |
| 图像分类 | [菜品识别](https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_dishes&en_category=ImageClassification) | 私有数据集训练,支持8416种菜品的分类识别,适合进一步菜品方向微调 |
| 图像分类 | [动物识别](https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_animals&en_category=ImageClassification) | 私有数据集训练,支持7978种动物的分类识别,适合进一步动物方向微调 |
| 目标检测 | [YOLOv3](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_coco2017&en_category=ObjectDetection) | 实现精度相比原作者**提高5.9 个绝对百分点**,性能极致优化。 |
| 人脸检测 | [人脸检测](https://www.paddlepaddle.org.cn/hubdetail?name=pyramidbox_lite_server&en_category=FaceDetection) | 百度自研,18年3月WIDER Face 数据集**冠军模型**, |
| 人脸检测 | [口罩人脸检测与识别](https://www.paddlepaddle.org.cn/hubdetail?name=pyramidbox_lite_server_mask&en_category=FaceDetection) | 业界**首个开源口罩人脸检测与识别模型**,引起广泛关注。 |
| 目标检测 | [行人检测](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_pedestrian&en_category=ObjectDetection) | 百度自研模型,海量私有数据集训练,可以应用于智能视频监控,人体行为分析,客流统计系统,智能交通等领域 |
| 目标检测 | [车辆检测](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_vehicles&en_category=ObjectDetection) | 百度自研模型,支持car (汽车),truck (卡车),bus (公交车),motorbike (摩托车),tricycle (三轮车)等车型的识别 |
| 语义分割 | [人像分割](https://www.paddlepaddle.org.cn/hubdetail?name=deeplabv3p_xception65_humanseg&en_category=ImageSegmentation) | 百度**自建数据集**训练,人像分割效果卓越。 |
| 语义分割 | [人体解析](https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation) | CVPR2019 LIP挑战赛中**满贯三冠王**。人体解析任务必选。 |
| 语义分割 | [肺炎CT影像分析](https://www.paddlepaddle.org.cn/hubdetail?name=Pneumonia_CT_LKM_PP&en_category=ImageSegmentation) | 助力连心医疗开源**业界首个**肺炎CT影像分析模型 |
| GAN | [风格迁移](https://www.paddlepaddle.org.cn/hubdetail?name=stylepro_artistic&en_category=GANs) | 百度自研风格迁移模型,趣味模型,**推荐尝试** |
| OCR | [超轻量中英文OCR文字识别](https://www.paddlepaddle.org.cn/hubdetail?name=chinese_ocr_db_crnn_mobile&en_category=TextRecognition) | 业界开源最小,8.6M超轻量中英文识别模型。支持中英文识别;支持倾斜、竖排等多种方向文字识别,**强力推荐** |
| 视频分类 | [超大规模视频分类](https://www.paddlepaddle.org.cn/hubdetail?name=videotag_tsn_lstm&en_category=VideoClassification) | 百度自研模型,基于千万短视频预训练的视频分类模型,可直接预测短视频的中文标签 |
| 词法分析 | [LAC ](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis) | 百度**自研中文特色**模型词法分析任务。 |
| 情感分析 | [Senta](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis) | 百度自研情感分析模型,海量中文数据训练。 |
| 情绪识别 | [emotion_detection](https://www.paddlepaddle.org.cn/hubdetail?name=emotion_detection_textcnn&en_category=SentimentAnalysis) | 百度自研对话识别模型,海量中文数据训练。 |
| 文本相似度 | [simnet](https://www.paddlepaddle.org.cn/hubdetail?name=simnet_bow&en_category=SemanticModel) | 百度自研短文本相似度模型,海量中文数据训练。 |
| 文本审核 | [porn_detection](https://www.paddlepaddle.org.cn/hubdetail?name=porn_detection_gru&en_category=TextCensorship) | 百度自研色情文本审核模型,海量中文数据训练。 |
| 语义模型 | [ERNIE](https://www.paddlepaddle.org.cn/hubdetail?name=ERNIE&en_category=SemanticModel) | **SOTA语义模型,中文任务全面优于BERT**。 |
| 语音合成 | WaveFlow(即将开源) | 百度自研模型,海量私有数据集训练 |
## 图像
#### 图像分类
图像分类是根据图像的语义信息对不同类别图像进行区分,是计算机视觉中重要的基础问题,是物体检测、图像分割、物体跟踪、行为分析、人脸识别等其他高层视觉任务的基础,在许多领域都有着广泛的应用。如:安防领域的人脸识别和智能视频分析等,交通领域的交通场景识别,互联网领域基于内容的图像检索和相册自动归类,医学领域的图像识别等。
**注:** **如果你是资深开发者,那可以随意按需使用****假如你是新手,服务器端优先选择Resnet50,移动端优先选择MobileNetV2**
| **模型名称** | **模型简介** |
| - | - |
| [AlexNet](https://www.paddlepaddle.org.cn/hubdetail?name=alexnet_imagenet&en_category=ImageClassification) | 首次在 CNN 中成功的应用了 ReLU, Dropout 和 LRN,并使用 GPU 进行运算加速 |
| [VGG19](https://www.paddlepaddle.org.cn/hubdetail?name=vgg19_imagenet&en_category=ImageClassification) | 在 AlexNet 的基础上使用 3*3 小卷积核,增加网络深度,具有很好的泛化能力 |
| [GoogLeNet](https://github.com/PaddlePaddle/models/tree/release/1.7/PaddleCV/image_classification) | 在不增加计算负载的前提下增加了网络的深度和宽度,性能更加优越 |
| [ResNet50](https://www.paddlepaddle.org.cn/hubdetail?name=resnet_v2_50_imagenet&en_category=ImageClassification) | Residual Network,引入了新的残差结构,解决了随着网络加深,准确率下降的问题 |
| [Inceptionv4](https://www.paddlepaddle.org.cn/hubdetail?name=inception_v4_imagenet&en_category=ImageClassification) | 将 Inception 模块与 Residual Connection 进行结合,通过ResNet的结构极大地加速训练并获得性能的提升 |
| [MobileNetV2](https://www.paddlepaddle.org.cn/hubdetail?name=mobilenet_v2_imagenet&en_category=ImageClassification) | MobileNet结构的微调,直接在 thinner 的 bottleneck层上进行 skip learning 连接以及对 bottleneck layer 不进行 ReLu 非线性处理可取得更好的结果 |
| [se_resnext50](https://www.paddlepaddle.org.cn/hubdetail?name=se_resnext50_32x4d_imagenet&en_category=ImageClassification) | 在ResNeXt 基础、上加入了 SE(Sequeeze-and-Excitation) 模块,提高了识别准确率,在 ILSVRC 2017 的分类项目中取得了第一名 |
| [ShuffleNetV2](https://www.paddlepaddle.org.cn/hubdetail?name=shufflenet_v2_imagenet&en_category=ImageClassification) | ECCV2018,轻量级 CNN 网络,在速度和准确度之间做了很好地平衡。在同等复杂度下,比 ShuffleNet 和 MobileNetv2 更准确,更适合移动端以及无人车领域 |
| [efficientNetb7](https://www.paddlepaddle.org.cn/hubdetail?name=efficientnetb7_imagenet&en_category=ImageClassification) | 同时对模型的分辨率,通道数和深度进行缩放,用极少的参数就可以达到SOTA的精度。 |
| [xception71](https://www.paddlepaddle.org.cn/hubdetail?name=xception71_imagenet&en_category=ImageClassification) | 对inception-v3的改进,用深度可分离卷积代替普通卷积,降低参数量同时提高了精度。 |
| [dpn107](https://www.paddlepaddle.org.cn/hubdetail?name=dpn107_imagenet&en_category=ImageClassification) | 融合了densenet和resnext的特点。 |
| [DarkNet53](https://www.paddlepaddle.org.cn/hubdetail?name=darknet53_imagenet&en_category=ImageClassification) | 检测框架yolov3使用的backbone,在分类和检测任务上都有不错表现。 |
| [DenseNet161](https://www.paddlepaddle.org.cn/hubdetail?name=densenet161_imagenet&en_category=ImageClassification) | 提出了密集连接的网络结构,更加有利于信息流的传递。 |
| [ResNeXt152_vd](https://www.paddlepaddle.org.cn/hubdetail?name=resnext152_64x4d_imagenet&en_category=ImageClassification) | 提出了cardinatity的概念,用于作为模型复杂度的另外一个度量,有效地提升模型精度。 |
#### 目标检测
目标检测任务的目标是给定一张图像或是一个视频帧,让计算机找出其中所有目标的位置,并给出每个目标的具体类别。对于计算机而言,能够“看到”的是图像被编码之后的数字,但很难解图像或是视频帧中出现了人或是物体这样的高层语义概念,也就更加难以定位目标出现在图像中哪个区域。目标检测模型请参考 (https://github.com/PaddlePaddle/PaddleDetection)
| 模型名称 | 模型简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [SSD](https://www.paddlepaddle.org.cn/hubdetail?name=ssd_mobilenet_v1_pascal&en_category=ObjectDetection) | 很好的继承了 MobileNet 预测速度快,易于部署的特点,能够很好的在多种设备上完成图像目标检测任务 |
| [Faster-RCNN](https://www.paddlepaddle.org.cn/hubdetail?name=faster_rcnn_coco2017&en_category=ObjectDetection) | 创造性地采用卷积网络自行产生建议框,并且和目标检测网络共享卷积网络,建议框数目减少,质量提高 |
| [YOLOv3](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_coco2017&en_category=ObjectDetection) | 速度和精度均衡的目标检测网络,相比于原作者 darknet 中的 YOLO v3 实现,PaddlePaddle 实现增加了 mixup,label_smooth 等处理,精度 (mAP(0.50: 0.95)) 相比于原作者提高了 4.7 个绝对百分点,在此基础上加入 synchronize batch normalization, 最终精度相比原作者提高 5.9 个绝对百分点。 |
| [人脸检测](https://www.paddlepaddle.org.cn/hubdetail?name=pyramidbox_lite_server&en_category=FaceDetection) | **PyramidBox** **模型是百度自主研发的人脸检测模型**,利用上下文信息解决困难人脸的检测问题,网络表达能力高,鲁棒性强。于18年3月份在 WIDER Face 数据集上取得第一名 |
| [超轻量人脸检测](https://www.paddlepaddle.org.cn/hubdetail?name=ultra_light_fast_generic_face_detector_1mb_640&en_category=FaceDetection) | Ultra-Light-Fast-Generic-Face-Detector-1MB是针对边缘计算设备或低算力设备(如用ARM推理)设计的实时超轻量级通用人脸检测模型,可以在低算力设备中如用ARM进行实时的通用场景的人脸检测推理。该PaddleHub Module的预训练数据集为WIDER FACE数据集,可支持预测,在预测时会将图片输入缩放为640 * 480。 |
| [口罩人脸检测与识别](https://www.paddlepaddle.org.cn/hubdetail?name=pyramidbox_lite_server_mask&en_category=FaceDetection) | 基于PyramidBox而研发的轻量级模型,对于光照、口罩遮挡、表情变化、尺度变化等常见问题具有很强的鲁棒性。基于WIDER FACE数据集和百度自采人脸数据集进行训练,支持预测,可用于检测人脸是否佩戴口罩。 |
#### 图像分割
图像语义分割顾名思义是将图像像素按照表达的语义含义的不同进行分组/分割,图像语义是指对图像内容的理解,例如,能够描绘出什么物体在哪里做了什么事情等,分割是指对图片中的每个像素点进行标注,标注属于哪一类别。近年来用在无人车驾驶技术中分割街景来避让行人和车辆、医疗影像分析中辅助诊断等。
图像语义分割模型请参考语义分割库[PaddleSeg](https://github.com/PaddlePaddle/PaddleSeg)
| 模型名称 | 模型简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [deeplabv3](https://www.paddlepaddle.org.cn/hubdetail?name=deeplabv3p_xception65_humanseg&en_category=ImageSegmentation) |DeepLabv3+ 作者通过encoder-decoder进行多尺度信息的融合,同时保留了原来的空洞卷积和ASSP层, 其骨干网络使用了Xception模型,提高了语义分割的健壮性和运行速率,在 PASCAL VOC 2012 dataset取得新的state-of-art performance。本Module使用百度自建数据集进行训练,可用于人像分割,支持任意大小的图片输入。|
| [ACE2P](https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation) | 人体解析(Human Parsing)是细粒度的语义分割任务,其旨在识别像素级别的人类图像的组成部分(例如,身体部位和服装)。ACE2P通过融合底层特征,全局上下文信息和边缘细节,端到端地训练学习人体解析任务。该结构针对Intersection over Union指标进行针对性的优化学习,提升准确率。以ACE2P单人人体解析网络为基础的解决方案在CVPR2019第三届LIP挑战赛中赢得了全部三个人体解析任务的第一名。该PaddleHub Module采用ResNet101作为骨干网络,接受输入图片大小为473x473x3。 |
| [Pneumonia_CT_LKM_PP](https://www.paddlepaddle.org.cn/hubdetail?name=Pneumonia_CT_LKM_PP&en_category=ImageSegmentation) | 肺炎CT影像分析模型(Pneumonia-CT-LKM-PP)可以高效地完成对患者CT影像的病灶检测识别、病灶轮廓勾画,通过一定的后处理代码,可以分析输出肺部病灶的数量、体积、病灶占比等全套定量指标。值得强调的是,该系统采用的深度学习算法模型充分训练了所收集到的高分辨率和低分辨率的CT影像数据,能极好地适应不同等级CT影像设备采集的检查数据,有望为医疗资源受限和医疗水平偏低的基层医院提供有效的肺炎辅助诊断工具。 |
#### 关键点检测
人体骨骼关键点检测 (Pose Estimation) 主要检测人体的一些关键点,如关节,五官等,通过关键点描述人体骨骼信息。人体骨骼关键点检测对于描述人体姿态,预测人体行为至关重要。是诸多计算机视觉任务的基础,例如动作分类,异常行为检测,以及自动驾驶等等。
| 模型名称 | 模型简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [Pose Estimation](https://www.paddlepaddle.org.cn/hubdetail?name=human_pose_estimation_resnet50_mpii&en_category=KeyPointDetection) | 人体骨骼关键点检测(Pose Estimation) 是计算机视觉的基础性算法之一,在诸多计算机视觉任务起到了基础性的作用,如行为识别、人物跟踪、步态识别等相关领域。具体应用主要集中在智能视频监控,病人监护系统,人机交互,虚拟现实,人体动画,智能家居,智能安防,运动员辅助训练等等。 该模型的论文《Simple Baselines for Human Pose Estimation and Tracking》由 MSRA 发表于 ECCV18,使用 MPII 数据集训练完成。 |
#### 图像生成
图像生成是指根据输入向量,生成目标图像。这里的输入向量可以是随机的噪声或用户指定的条件向量。具体的应用场景有:手写体生成、人脸合成、风格迁移、图像修复等。[gan](https://github.com/PaddlePaddle/models/tree/release/1.7/PaddleCV/gan) 包含和图像生成相关的多个模型。
| 模型名称 | 模型简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [CycleGAN](https://www.paddlepaddle.org.cn/hubdetail?name=cyclegan_cityscapes&en_category=GANs) | 图像翻译,可以通过非成对的图片将某一类图片转换成另外一类图片,可用于风格迁移,支持图片从实景图转换为语义分割结果,也支持从语义分割结果转换为实景图。 |
| [StarGAN](https://www.paddlepaddle.org.cn/hubdetail?name=stargan_celeba&en_category=GANs) | 多领域属性迁移,引入辅助分类帮助单个判别器判断多个属性,可用于人脸属性转换。该 PaddleHub Module 使用 Celeba 数据集训练完成,目前支持 "Black_Hair", "Blond_Hair", "Brown_Hair", "Female", "Male", "Aged" 这六种人脸属性转换。 |
| [AttGAN](https://www.paddlepaddle.org.cn/hubdetail?name=attgan_celeba&en_category=GANs) | 利用分类损失和重构损失来保证改变特定的属性,可用于13种人脸特定属性转换。 |
| [STGAN](https://www.paddlepaddle.org.cn/hubdetail?name=stgan_celeba&en_category=GANs) | 人脸特定属性转换,只输入有变化的标签,引入 GRU 结构,更好的选择变化的属性,支持13种属性转换。 |
#### 文本
PaddleNLP 是基于 PaddlePaddle 深度学习框架开发的自然语言处理 (NLP) 工具,算法,模型和数据的开源项目。百度在 NLP 领域十几年的深厚积淀为 PaddleNLP 提供了强大的核心动力。
#### 中文词法分析与词向量
| 模型名称 | 简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [LAC 中文词法分析](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis) | Lexical Analysis of Chinese,简称LAC,是百度自主研发中文特色模型词法分析任务,集成了中文分词、词性标注和命名实体识别任务。输入是一个字符串,而输出是句子中的词边界和词性、实体类别。 |
| [word2vec词向量](https://www.paddlepaddle.org.cn/hubdetail?name=word2vec_skipgram&en_category=SemanticModel) | Word2vec是常用的词嵌入(word embedding)模型。该PaddleHub Module基于Skip-gram模型,在海量百度搜索数据集下预训练得到中文单词预训练词嵌入。其支持Fine-tune。Word2vec的预训练数据集的词汇表大小为1700249,word embedding维度为128。 |
#### 情感分析
| 模型名称 | 简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [Senta](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis) | 情感倾向分析(Sentiment Classification,简称Senta)针对带有主观描述的中文文本,可自动判断该文本的情感极性类别并给出相应的置信度,能够帮助企业理解用户消费习惯、分析热点话题和危机舆情监控,为企业提供有利的决策支持。该模型基于一个双向LSTM结构,情感类型分为积极、消极。该PaddleHub Module支持预测和Fine-tune。 |
| [emotion_detection](https://www.paddlepaddle.org.cn/hubdetail?name=emotion_detection_textcnn&en_category=SentimentAnalysis) | 对话情绪识别(Emotion Detection,简称EmoTect)专注于识别智能对话场景中用户的情绪,针对智能对话场景中的用户文本,自动判断该文本的情绪类别并给出相应的置信度,情绪类型分为积极、消极、中性。该模型基于TextCNN(多卷积核CNN模型),能够更好地捕捉句子局部相关性。该PaddleHub Module预训练数据集为百度自建数据集,支持预测和Fine-tune。 |
#### 文本相似度计算
[SimNet](https://github.com/PaddlePaddle/models/tree/release/1.7/PaddleNLP/similarity_net) (Similarity Net) 是一个计算短文本相似度的框架,主要包括 BOW、CNN、RNN、MMDNN 等核心网络结构形式。SimNet 框架在百度各产品上广泛应用,提供语义相似度计算训练和预测框架,适用于信息检索、新闻推荐、智能客服等多个应用场景,帮助企业解决语义匹配问题。
| 模型 | 简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [simnet_bow](https://www.paddlepaddle.org.cn/hubdetail?name=simnet_bow&en_category=SemanticModel) | SimNet是一个计算短文本相似度的模型,可以根据用户输入的两个文本,计算出相似度得分。该PaddleHub Module基于百度海量搜索数据进行训练,支持命令行和Python接口进行预测 |
#### 文本审核
文本审核也是NLP方向的一个常用任务,可以广泛应用在各种信息分发平台、论坛、讨论区的文本审核中。
| 模型 | 简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [porn_detection_gru](https://www.paddlepaddle.org.cn/hubdetail?name=porn_detection_gru&en_category=TextCensorship) | 色情检测模型可自动判别文本是否涉黄并给出相应的置信度,对文本中的色情描述、低俗交友、污秽文爱进行识别。porn_detection_gru采用GRU网络结构并按字粒度进行切词。该模型最大句子长度为256字,仅支持预测。 |
#### 语义表示
通过在大规模语料上训练得到的通用的语义表示模型,可以助益其他自然语言处理任务,是通用预训练 + 特定任务精调范式的体现。PaddleLARK 集成了 ELMO,BERT,ERNIE 1.0,ERNIE 2.0,XLNet 等热门中英文预训练模型。
| 模型 | 简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [ERNIE](https://www.paddlepaddle.org.cn/hubdetail?name=ERNIE&en_category=SemanticModel) | ERNIE通过建模海量数据中的词、实体及实体关系,学习真实世界的语义知识。相较于BERT学习原始语言信号,ERNIE直接对先验语义知识单元进行建模,增强了模型语义表示能力,以Transformer为网络基本组件,以Masked Bi-Language Model和Next Sentence Prediction为训练目标,通过预训练得到通用语义表示,再结合简单的输出层,应用到下游的NLP任务,在多个任务上取得了SOTA的结果。其可用于文本分类、序列标注、阅读理解等任务。预训练数据集为百科类、资讯类、论坛对话类数据等中文语料。该PaddleHub Module可支持Fine-tune。 |
| [BERT](https://www.paddlepaddle.org.cn/hubdetail?name=bert_multi_uncased_L-12_H-768_A-12&en_category=SemanticModel) | 一个迁移能力很强的通用语义表示模型, 以 Transformer 为网络基本组件,以双向 Masked Language Model和 Next Sentence Prediction 为训练目标,通过预训练得到通用语义表示,再结合简单的输出层,应用到下游的 NLP 任务,在多个任务上取得了 SOTA 的结果。 |
| [RoBERTa](https://www.paddlepaddle.org.cn/hubdetail?name=rbtl3&en_category=SemanticModel) | RoBERTa (a Robustly Optimized BERT Pretraining Approach) 是BERT通用语义表示模型的一个优化版,它在BERT模型的基础上提出了Dynamic Masking方法、去除了Next Sentence Prediction目标,同时在更多的数据上采用更大的batch size训练更长的时间,在多个任务中做到了SOTA。rbtl3以roberta_wwm_ext_chinese_L-24_H-1024_A-16模型参数初始化前三层Transformer以及词向量层并在此基础上继续训练了1M步,在仅损失少量效果的情况下大幅减少参数量,得到推断速度的进一步提升。当该PaddleHub Module用于Fine-tune时,其输入是单文本(如Fine-tune的任务为情感分类等)或文本对(如Fine-tune任务为文本语义相似度匹配等),可用于文本分类、序列标注、阅读理解等任务。 |
| [chinese-bert](https://www.paddlepaddle.org.cn/hubdetail?name=chinese-bert-wwm&en_category=SemanticModel) | chinese_bert_wwm是支持中文的BERT模型,它采用全词遮罩(Whole Word Masking)技术,考虑到了中文分词问题。预训练数据集为中文维基百科。该PaddleHub Module只支持Fine-tune。当该PaddleHub Module用于Fine-tune时,其输入是单文本(如Fine-tune的任务为情感分类等)或文本对(如Fine-tune任务为文本语义相似度匹配等),可用于文本分类、序列标注、阅读理解等任务。 |
## 视频
视频数据包含语音、图像等多种信息,因此理解视频任务不仅需要处理语音和图像,还需要提取视频帧时间序列中的上下文信息。视频分类模型提供了提取全局时序特征的方法,主要方式有卷积神经网络 (C3D, I3D, C2D等),神经网络和传统图像算法结合 (VLAD 等),循环神经网络等建模方法。视频动作定位模型需要同时识别视频动作的类别和起止时间点,通常采用类似于图像目标检测中的算法在时间维度上进行建模。
| 模型名称 | 模型简介 |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| videotag_tsn_lstm | videotag_tsn_lstm是一个基于千万短视频预训练的视频分类模型,可直接预测短视频的中文标签。模型分为视频特征抽取和序列建模两个阶段,前者使用TSN网络提取视频特征,后者基于前者输出使用AttentionLSTM网络进行序列建模实现分类。模型基于百度实际短视频场景中的大规模数据训练得到,在实际业务中取得89.9%的Top-1精度,同时具有良好的泛化能力,适用于多种短视频中文标签分类场景。该PaddleHub Module可支持预测。 |
| [TSN](https://www.paddlepaddle.org.cn/hubdetail?name=tsn_kinetics400&en_category=VideoClassification) | TSN(Temporal Segment Network)是视频分类领域经典的基于2D-CNN的解决方案。该方法主要解决视频的长时间行为判断问题,通过稀疏采样视频帧的方式代替稠密采样,既能捕获视频全局信息,也能去除冗余,降低计算量。最终将每帧特征平均融合后得到视频的整体特征,并用于分类。TSN的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。该PaddleHub Module可支持预测。 |
| [Non-Local](https://www.paddlepaddle.org.cn/hubdetail?name=tsn_kinetics400&en_category=VideoClassification) | Non-local Neural Networks是由Xiaolong Wang等研究者在2017年提出的模型,主要特点是通过引入Non-local操作来描述距离较远的像素点之间的关联关系。其借助于传统计算机视觉中的non-local mean的思想,并将该思想扩展到神经网络中,通过定义输出位置和所有输入位置之间的关联函数,建立全局关联特性。Non-local模型的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。该PaddleHub Module可支持预测。 |
| [StNet](https://www.paddlepaddle.org.cn/hubdetail?name=stnet_kinetics400&en_category=VideoClassification) | StNet模型框架为ActivityNet Kinetics Challenge 2018中夺冠的基础网络框架,是基于ResNet50实现的。该模型提出super-image的概念,在super-image上进行2D卷积,建模视频中局部时空相关性。另外通过temporal modeling block建模视频的全局时空依赖,最后用一个temporal Xception block对抽取的特征序列进行长时序建模。StNet的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。该PaddleHub Module可支持预测。 |
| [TSM](https://www.paddlepaddle.org.cn/hubdetail?name=tsm_kinetics400&en_category=VideoClassification) | TSM(Temporal Shift Module)是由MIT和IBM Watson AI Lab的JiLin,ChuangGan和SongHan等人提出的通过时间位移来提高网络视频理解能力的模块。TSM的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。该PaddleHub Module可支持预测。 |
# 通过命令行调用方式使用PaddleHub
本页面的代码/命令可在[AIStudio](https://aistudio.baidu.com/aistudio/projectdetail/643120)上在线运行,类似notebook的环境,只需通过浏览器即可访问,无需准备环境,非常方便开发者快速体验。
PaddleHub在设计时,为模型的管理和使用提供了命令行工具,也提供了通过命令行调用PaddleHub模型完成预测的方式。比如,前面章节中人像分割和文本分词的任务也可以通过命令行调用的方式实现。
### 体验前请提前安装好PaddleHub
```shell
# 安装最新版本,使用清华源更稳定、更迅速
$ pip install paddlehub --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
```
### 人像扣图
```shell
# 下载待测试图片
$ wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
# 通过命令行方式实现人像扣图任务
$ hub run deeplabv3p_xception65_humanseg --input_path test_image.jpg --visualization=True --output_dir="humanseg_output"
```
--2020-07-22 12:19:52-- https://paddlehub.bj.bcebos.com/resources/test_image.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 967120 (944K) [image/jpeg]
Saving to: ‘test_image.jpg’
test_image.jpg 100%[===================>] 944.45K 6.13MB/s in 0.2s
2020-07-22 12:19:53 (6.13 MB/s) - ‘test_image.jpg’ saved [967120/967120]
[{'save_path': 'humanseg_output/test_image.png', 'data': array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)}]
![png](../imgs/humanseg_test_res.png)
### 中文分词
```shell
#通过命令行方式实现文本分词任务
$ hub run lac --input_text "今天是个好日子"
```
Install Module lac
Downloading lac
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpjcskpj8x/lac
[==================================================] 100.00%
Successfully installed lac-2.1.1
[{'word': ['今天', '是', '个', '好日子'], 'tag': ['TIME', 'v', 'q', 'n']}]
上面的命令中包含四个部分,分别是:
- hub 表示PaddleHub的命令。
- run 调用run执行模型的预测。
- deeplabv3p_xception65_humanseg、lac 表示要调用的算法模型。
- --input_path/--input_text 表示模型的输入数据,图像和文本的输入方式不同。
另外,命令行中`visualization=True`表示将结果可视化输出,`output_dir="humanseg_output"`表示预测结果的保存目录,可以到该路径下查看输出的图片。
再看一个文字识别和一个口罩检测的例子。
### OCR文字识别
```shell
# 下载待测试的图片
$ wget https://paddlehub.bj.bcebos.com/model/image/ocr/test_ocr.jpg
# 该Module依赖于第三方库shapely和pyclipper,需提前安装
$ pip install shapely
$ pip install pyclipper
# 通过命令行方式实现文字识别任务
$ hub run chinese_ocr_db_crnn_mobile --input_path test_ocr.jpg --visualization=True --output_dir='ocr_result'
```
--2020-07-22 15:00:50-- https://paddlehub.bj.bcebos.com/model/image/ocr/test_ocr.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 48680 (48K) [image/jpeg]
Saving to: ‘test_ocr.jpg’
test_ocr.jpg 100%[===================>] 47.54K --.-KB/s in 0.02s
2020-07-22 15:00:51 (2.88 MB/s) - ‘test_ocr.jpg’ saved [48680/48680]
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/
Requirement already satisfied: shapely in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (1.7.0)
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/
Requirement already satisfied: pyclipper in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (1.2.0)
[{'save_path': 'ocr_result/ndarray_1595401261.294494.jpg', 'data': [{'text': '纯臻营养护发素', 'confidence': 0.9438689351081848, 'text_box_position': [[24, 36], [304, 34], [304, 72], [24, 74]]}, {'text': '产品信息/参数', 'confidence': 0.9843138456344604, 'text_box_position': [[24, 80], [172, 80], [172, 104], [24, 104]]}, {'text': '(45元/每公斤,100公斤起订)', 'confidence': 0.9210420250892639, 'text_box_position': [[24, 109], [333, 109], [333, 136], [24, 136]]}, {'text': '每瓶22元,1000瓶起订)', 'confidence': 0.9685984253883362, 'text_box_position': [[22, 139], [283, 139], [283, 166], [22, 166]]}, {'text': '【品牌】', 'confidence': 0.9527574181556702, 'text_box_position': [[22, 174], [85, 174], [85, 198], [22, 198]]}, {'text': ':代加工方式/OEMODM', 'confidence': 0.9442129135131836, 'text_box_position': [[90, 176], [301, 176], [301, 196], [90, 196]]}, {'text': '【品名】', 'confidence': 0.8793742060661316, 'text_box_position': [[23, 205], [85, 205], [85, 229], [23, 229]]}, {'text': ':纯臻营养护发素', 'confidence': 0.9230973124504089, 'text_box_position': [[95, 204], [235, 206], [235, 229], [95, 227]]}, {'text': '【产品编号】', 'confidence': 0.9311650395393372, 'text_box_position': [[24, 238], [120, 238], [120, 260], [24, 260]]}, {'text': 'J:YM-X-3011', 'confidence': 0.8866629004478455, 'text_box_position': [[110, 239], [239, 239], [239, 256], [110, 256]]}, {'text': 'ODMOEM', 'confidence': 0.9916308522224426, 'text_box_position': [[414, 233], [430, 233], [430, 304], [414, 304]]}, {'text': '【净含量】:220ml', 'confidence': 0.8709315657615662, 'text_box_position': [[23, 268], [181, 268], [181, 292], [23, 292]]}, {'text': '【适用人群】', 'confidence': 0.9589888453483582, 'text_box_position': [[24, 301], [118, 301], [118, 321], [24, 321]]}, {'text': ':适合所有肤质', 'confidence': 0.935418963432312, 'text_box_position': [[131, 300], [254, 300], [254, 323], [131, 323]]}, {'text': '【主要成分】', 'confidence': 0.9366627335548401, 'text_box_position': [[24, 332], [117, 332], [117, 353], [24, 353]]}, {'text': '鲸蜡硬脂醇', 'confidence': 0.9033458828926086, 'text_box_position': [[138, 331], [235, 331], [235, 351], [138, 351]]}, {'text': '燕麦B-葡聚', 'confidence': 0.8497812747955322, 'text_box_position': [[248, 332], [345, 332], [345, 352], [248, 352]]}, {'text': '椰油酰胺丙基甜菜碱、', 'confidence': 0.8935506939888, 'text_box_position': [[54, 363], [232, 363], [232, 383], [54, 383]]}, {'text': '糖、', 'confidence': 0.8750994205474854, 'text_box_position': [[25, 364], [62, 364], [62, 383], [25, 383]]}, {'text': '泛酯', 'confidence': 0.5581164956092834, 'text_box_position': [[244, 363], [281, 363], [281, 382], [244, 382]]}, {'text': '(成品包材)', 'confidence': 0.9566792845726013, 'text_box_position': [[368, 367], [475, 367], [475, 388], [368, 388]]}, {'text': '【主要功能】', 'confidence': 0.9493741393089294, 'text_box_position': [[24, 395], [119, 395], [119, 416], [24, 416]]}, {'text': ':可紧致头发磷层', 'confidence': 0.9692543745040894, 'text_box_position': [[128, 397], [273, 397], [273, 414], [128, 414]]}, {'text': '美,从而达到', 'confidence': 0.8662520051002502, 'text_box_position': [[265, 395], [361, 395], [361, 415], [265, 415]]}, {'text': '即时持久改善头发光泽的效果,给干燥的头', 'confidence': 0.9690631031990051, 'text_box_position': [[25, 425], [372, 425], [372, 448], [25, 448]]}, {'text': '发足够的滋养', 'confidence': 0.8946213126182556, 'text_box_position': [[26, 457], [136, 457], [136, 477], [26, 477]]}]}]
```shell
# 查看预测结果
```
![png](../imgs/ocr_res.jpg)
### 口罩检测
```shell
# 下载待测试的图片
$ wget https://paddlehub.bj.bcebos.com/resources/test_mask_detection.jpg
# 通过命令行方式实现文字识别任务
$ hub run pyramidbox_lite_mobile_mask --input_path test_mask_detection.jpg --visualization=True --output_dir='detection_result'
```
--2020-07-22 15:08:11-- https://paddlehub.bj.bcebos.com/resources/test_mask_detection.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.229, 182.61.200.195
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.229|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 299133 (292K) [image/jpeg]
Saving to: ‘test_mask_detection.jpg’
test_mask_detection 100%[===================>] 292.12K --.-KB/s in 0.06s
2020-07-22 15:08:11 (4.55 MB/s) - ‘test_mask_detection.jpg’ saved [299133/299133]
Install Module pyramidbox_lite_mobile_mask
Downloading pyramidbox_lite_mobile_mask
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmp8oes9jid/pyramidbox_lite_mobile_mask
[==================================================] 100.00%
Successfully installed pyramidbox_lite_mobile_mask-1.3.0
Downloading pyramidbox_lite_mobile
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpvhjhlr10/pyramidbox_lite_mobile
[==================================================] 100.00%
[{'data': [{'label': 'MASK', 'confidence': 0.9992434978485107, 'top': 181, 'bottom': 440, 'left': 457, 'right': 654}, {'label': 'MASK', 'confidence': 0.9224318265914917, 'top': 340, 'bottom': 578, 'left': 945, 'right': 1125}, {'label': 'NO MASK', 'confidence': 0.9996706247329712, 'top': 292, 'bottom': 500, 'left': 1166, 'right': 1323}], 'path': 'test_mask_detection.jpg'}]
```shell
# 查看预测结果
```
![png](../imgs/test_mask_detection_result.jpg)
### PaddleHub命令行工具简介
PaddleHub的命令行工具在开发时借鉴了Anaconda和PIP等软件包管理的理念,可以方便快捷的完成模型的搜索、下载、安装、升级、预测等功能。 下面概要介绍一下PaddleHub支持的12个命令,详细介绍可查看[命令行参考](../tutorial/cmdintro.md)章节。:
* install:用于将Module安装到本地,默认安装在{HUB_HOME}/.paddlehub/modules目录下;
* uninstall:卸载本地Module;
* show:用于查看本地已安装Module的属性或者指定目录下确定的Module的属性,包括其名字、版本、描述、作者等信息;
* download:用于下载百度飞桨PaddleHub提供的Module;
* search:通过关键字在服务端检索匹配的Module,当想要查找某个特定模型的Module时,使用search命令可以快速得到结果,例如hub search ssd命令,会查找所有包含了ssd字样的Module,命令支持正则表达式,例如hub search ^s.\*搜索所有以s开头的资源;
* list:列出本地已经安装的Module;
* run:用于执行Module的预测;
* version:显示PaddleHub版本信息;
* help:显示帮助信息;
* clear:PaddleHub在使用过程中会产生一些缓存数据,这部分数据默认存放在${HUB_HOME}/.paddlehub/cache目录下,用户可以通过clear命令来清空缓存;
* autofinetune:用于自动调整Fine-tune任务的超参数,具体使用详情参考[PaddleHub AutoDL Finetuner](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.5/docs/tutorial/autofinetune.md)使用教程;
* config:用于查看和设置Paddlehub相关设置,包括对server地址、日志级别的设置;
* serving:用于一键部署Module预测服务,详细用法见[PaddleHub Serving一键服务部署](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.5/docs/tutorial/serving.md)
## 小结
PaddleHub的产品理念是模型即软件,通过Python API或命令行实现模型调用,可快速体验或集成飞桨特色预训练模型。
此外,当用户想用少量数据来优化预训练模型时,PaddleHub也支持迁移学习,通过Fine-tune API,内置多种优化策略,只需少量代码即可完成预训练模型的Fine-tuning。具体可通过后面迁移学习的章节了解。
>值得注意的是,不是所有的Module都支持通过命令行预测 (例如BERT/ERNIE Transformer类模型,一般需要搭配任务进行Fine-tune), 也不是所有的Module都可用于Fine-tune(例如一般不建议用户使用词法分析LAC模型Fine-tune)。建议提前阅读[预训练模型的介绍文档](https://www.paddlepaddle.org.cn/hublist)了解使用场景。
# 通过Python代码调用方式使用PaddleHub
本页面的代码/命令可在[AIStudio](https://aistudio.baidu.com/aistudio/projectdetail/635335)上在线运行,类似notebook的环境,只需通过浏览器即可访问,无需准备环境,非常方便开发者快速体验。
## 计算机视觉任务的PaddleHub示例
先以计算机视觉任务为例,我们选用一张测试图片test.jpg,分别实现如下四项功能:
* 人像扣图([deeplabv3p_xception65_humanseg](https://www.paddlepaddle.org.cn/hubdetail?name=deeplabv3p_xception65_humanseg&en_category=ImageSegmentation)
* 人体部位分割([ace2p](https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation)
* 人脸检测([ultra_light_fast_generic_face_detector_1mb_640](https://www.paddlepaddle.org.cn/hubdetail?name=ultra_light_fast_generic_face_detector_1mb_640&en_category=FaceDetection)
* 关键点检测([human_pose_estimation_resnet50_mpii](https://www.paddlepaddle.org.cn/hubdetail?name=human_pose_estimation_resnet50_mpii&en_category=KeyPointDetection)
>注:如果需要查找PaddleHub中可以调用哪些预训练模型,获取模型名称(如deeplabv3p_xception65_humanseg,后续代码中通过该名称调用模型),请参考[官网文档](https://www.paddlepaddle.org.cn/hublist),文档中已按照模型类别分好类,方便查找,并且提供了详细的模型介绍。
### 体验前请提前安装好PaddleHub
```shell
# 安装最新版本,使用清华源更稳定、更迅速
$ pip install paddlehub --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
```
### 原图展示
```shell
# 下载待测试图片
$ wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
```
--2020-07-22 12:22:19-- https://paddlehub.bj.bcebos.com/resources/test_image.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 967120 (944K) [image/jpeg]
Saving to: ‘test_image.jpg.1’
test_image.jpg.1 100%[===================>] 944.45K 5.51MB/s in 0.2s
2020-07-22 12:22:19 (5.51 MB/s) - ‘test_image.jpg.1’ saved [967120/967120]
![png](../imgs/humanseg_test.png)
### 人像扣图
PaddleHub采用模型即软件的设计理念,所有的预训练模型与Python软件包类似,具备版本的概念,通过`hub install``hub uninstall`命令可以便捷地完成模型的安装、升级和卸载。
> 使用如下命令默认下载最新版本的模型,如果需要指定版本,可在后面接版本号,如`==1.1.1`。
```shell
#安装预训练模型,deeplabv3p_xception65_humanseg是模型名称
$ hub install deeplabv3p_xception65_humanseg
```
Downloading deeplabv3p_xception65_humanseg
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpo32jeve0/deeplabv3p_xception65_humanseg
[==================================================] 100.00%
Successfully installed deeplabv3p_xception65_humanseg-1.1.1
```python
# 导入paddlehub库
import paddlehub as hub
# 指定模型名称、待预测的图片路径、输出结果的路径,执行并输出预测结果
module = hub.Module(name="deeplabv3p_xception65_humanseg")
res = module.segmentation(paths = ["./test_image.jpg"], visualization=True, output_dir='humanseg_output')
```
[32m[2020-07-22 12:22:49,474] [ INFO] - Installing deeplabv3p_xception65_humanseg module [0m
Downloading deeplabv3p_xception65_humanseg
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpzrrl1duq/deeplabv3p_xception65_humanseg
[==================================================] 100.00%
[32m[2020-07-22 12:23:11,811] [ INFO] - Successfully installed deeplabv3p_xception65_humanseg-1.1.1 [0m
![png](../imgs/output_8_3.png)
可以看到,使用Python代码调用PaddleHub只需要三行代码即可实现:
```
import paddlehub as hub # 导入PaddleHub代码库
module = hub.Module(name="deeplabv3p_xception65_humanseg") # 指定模型名称
res = module.segmentation(paths = ["./test.jpg"], visualization=True, output_dir='humanseg_output') # 指定模型的输入和输出路径,执行并输出预测结果,其中visualization=True表示将结果可视化输出
```
* 模型名称均通过`hub.Module` API来指定;
* `module.segmentation`用于执行图像分割类的预测任务,不同类型任务设计了不同的预测API,比如人脸检测任务采用`face_detection`函数执行预测,建议调用预训练模型之前先仔细查阅对应的模型介绍文档。
* 预测结果保存在`output_dir='humanseg_output'`目录下,可以到该路径下查看输出的图片。
其他任务的实现方式,均可参考这个“套路”。看一下接下来几个任务如何实现。
### 人体部位分割
```shell
#安装预训练模型
$ hub install ace2p
```
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Downloading ace2p
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpfsovt3f8/ace2p
[==================================================] 100.00%
Successfully installed ace2p-1.1.0
```python
# 导入paddlehub库
import paddlehub as hub
# 指定模型名称、待预测的图片路径、输出结果的路径,执行并输出预测结果
module = hub.Module(name="ace2p")
res = module.segmentation(paths = ["./test_image.jpg"], visualization=True, output_dir='ace2p_output')
```
[32m[2020-07-22 12:23:58,027] [ INFO] - Installing ace2p module [0m
Downloading ace2p
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmptrogpj6j/ace2p
[==================================================] 100.00%
[32m[2020-07-22 12:24:22,575] [ INFO] - Successfully installed ace2p-1.1.0 [0m
![png](../imgs/output_12_3.png)
### 人脸检测
```shell
#安装预训练模型
$ hub install ultra_light_fast_generic_face_detector_1mb_640
```
Downloading ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpz82xnmy6/ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
Successfully installed ultra_light_fast_generic_face_detector_1mb_640-1.1.2
```python
# 导入paddlehub库
import paddlehub as hub
# 指定模型名称、待预测的图片路径、输出结果的路径,执行并输出预测结果
module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
res = module.face_detection(paths = ["./test_image.jpg"], visualization=True, output_dir='face_detection_output')
```
[32m[2020-07-22 12:25:12,948] [ INFO] - Installing ultra_light_fast_generic_face_detector_1mb_640 module [0m
Downloading ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpw44mo56p/ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
[32m[2020-07-22 12:25:14,698] [ INFO] - Successfully installed ultra_light_fast_generic_face_detector_1mb_640-1.1.2
![png](../imgs/output_15_3.png)
### 关键点检测
```shell
#安装预训练模型
$ hub install human_pose_estimation_resnet50_mpii
```
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Downloading human_pose_estimation_resnet50_mpii
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpn_ppwkzq/human_pose_estimation_resnet50_mpii
[======== ] 17.99%
```python
# 导入paddlehub库
import paddlehub as hub
# 指定模型名称、待预测的图片路径、输出结果的路径,执行并输出预测结果
module = hub.Module(name="human_pose_estimation_resnet50_mpii")
res = module.keypoint_detection(paths = ["./test_image.jpg"], visualization=True, output_dir='keypoint_output')
```
[32m[2020-07-23 11:27:33,989] [ INFO] - Installing human_pose_estimation_resnet50_mpii module [0m
[32m[2020-07-23 11:27:33,992] [ INFO] - Module human_pose_estimation_resnet50_mpii already installed in /home/aistudio/.paddlehub/modules/human_pose_estimation_resnet50_mpii [0m
image saved in keypoint_output/test_imagetime=1595474855.jpg
![png](../imgs/output_18_2.png)
## 自然语言处理任务的PaddleHub示例
再看两个自然语言处理任务的示例,下面以中文分词和情感分类的任务为例介绍。
* 中文分词([lac](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis)
* 情感分析([senta_bilstm](https://www.paddlepaddle.org.cn/hubdetail?name=senta_bilstm&en_category=SentimentAnalysis)
### 中文分词
```shell
#安装预训练模型
$ hub install lac
```
2020-07-22 10:03:09,866-INFO: font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
2020-07-22 10:03:10,208-INFO: generated new fontManager
Downloading lac
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmp8ukaz690/lac
[==================================================] 100.00%
Successfully installed lac-2.1.1
```python
# 导入paddlehub库
import paddlehub as hub
# 指定模型名称、待分词的文本,执行并输出预测结果
lac = hub.Module(name="lac")
test_text = ["1996年,曾经是微软员工的加布·纽维尔和麦克·哈灵顿一同创建了Valve软件公司。他们在1996年下半年从id software取得了雷神之锤引擎的使用许可,用来开发半条命系列。"]
res = lac.lexical_analysis(texts = test_text)
# 打印预测结果
print("中文词法分析结果:", res)
```
[32m[2020-07-22 10:03:18,439] [ INFO] - Installing lac module[0m
[32m[2020-07-22 10:03:18,531] [ INFO] - Module lac already installed in /home/aistudio/.paddlehub/modules/lac [0m
中文词法分析结果: [{'word': ['1996年', ',', '曾经', '是', '微软', '员工', '的', '加布·纽维尔', '和', '麦克·哈灵顿', '一同', '创建', '了', 'Valve软件公司', '。', '他们', '在', '1996年下半年', '从', 'id', ' ', 'software', '取得', '了', '雷神之锤', '引擎', '的', '使用', '许可', ',', '用来', '开发', '半条命', '系列', '。'], 'tag': ['TIME', 'w', 'd', 'v', 'ORG', 'n', 'u', 'PER', 'c', 'PER', 'd', 'v', 'u', 'ORG', 'w', 'r', 'p', 'TIME', 'p', 'nz', 'w', 'n', 'v', 'u', 'n', 'n', 'u', 'vn', 'vn', 'w', 'v', 'v', 'n', 'n', 'w']}]
可以看到,与计算机视觉任务相比,输入和输出接口(这里需要输入文本,以函数参数的形式传入)存在差异,这与任务类型相关,具体可查看对应预训练模型的API介绍。
### 情感分类
```shell
#安装预训练模型
$ hub install senta_bilstm
```
Module senta_bilstm-1.1.0 already installed in /home/aistudio/.paddlehub/modules/senta_bilstm
```python
import paddlehub as hub
senta = hub.Module(name="senta_bilstm")
test_text = ["味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。"]
res = senta.sentiment_classify(texts = test_text)
print("情感分析结果:", res)
```
[32m[2020-07-22 10:34:06,922] [ INFO] - Installing senta_bilstm module [0m
[32m[2020-07-22 10:34:06,984] [ INFO] - Module senta_bilstm already installed in /home/aistudio/.paddlehub/modules/senta_bilstm
[32m[2020-07-22 10:34:08,937] [ INFO] - Installing lac module[0m
[32m[2020-07-22 10:34:08,939] [ INFO] - Module lac already installed in /home/aistudio/.paddlehub/modules/lac [0m
情感分析结果: [{'text': '味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9771, 'negative_probs': 0.0229}]
## 总结
PaddleHub提供了丰富的预训练模型,包括图像分类、语义模型、视频分类、图像生成、图像分割、文本审核、关键点检测等主流模型,只需要3行Python代码即可快速调用,即时输出预测结果,非常方便。您可以尝试一下,从[预训练模型列表](https://www.paddlepaddle.org.cn/hublist)中选择一些模型体验一下。
快速体验
==================
PaddleHub有两种使用方式:Python代码调用和命令行调用。
命令行方式只需要一行命令即可快速体验PaddleHub提供的预训练模型的效果,是快速体验的绝佳选择;Python代码调用方式也仅需要三行代码,如果需要使用自己的数据Fine-tune并生成模型,则采用该方式。
命令行示例:
::
$ hub run chinese_ocr_db_crnn_mobile --input_path test_ocr.jpg
Python代码示例:
::
import paddlehub as hub
ocr = hub.Module(name="chinese_ocr_db_crnn_mobile")
result = ocr.recognize_text(paths = "./test_image.jpg"], visualization=True, output_dir='ocr_output')
样例结果示例:
.. image:: ../imgs/ocr_res.jpg
本章节提供这两种方法的快速体验方法,方便快速上手,同时提供了丰富的体验Demo,覆盖多场景领域,欢迎体验。具体使用时,当然还需要进一步了解详细的API接口参数、命令行参数解释,可参考后面的Python API接口和命令行参考章节。
.. toctree::
:maxdepth: 1
通过命令行调用方式使用PaddleHub<cmd_quick_run>
通过Python代码调用方式使用PaddleHub<python_use_hub>
PaddleHub更多体验Demos<more_demos>
# 快速体验
安装PaddleHub成功后,执行命令[hub run](tutorial/cmdintro.md),可以快速体验PaddleHub无需代码、一键预测的命令行功能,如下几个示例:
## 视觉领域场景体验
* 使用[文字识别](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=TextRecognition)轻量级中文OCR模型chinese_ocr_db_crnn_mobile即可一键快速识别图片中的文字。
```shell
$ wget https://paddlehub.bj.bcebos.com/model/image/ocr/test_ocr.jpg
$ hub run chinese_ocr_db_crnn_mobile --input_path test_ocr.jpg --visualization=True
```
预测结果图片保存在当前运行路径下ocr_result文件夹中,如下图所示。
![](./imgs/ocr_res.jpg)
* 使用[目标检测](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=ObjectDetection)模型Ultra-Light-Fast-Generic-Face-Detector-1MB对图片进行人脸识别
```shell
$ wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
$ hub run ultra_light_fast_generic_face_detector_1mb_640 --input_path test_image.jpg
```
![](./imgs/face_detection_result.jpeg)
* 使用[目标检测](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=ObjectDetection)模型pyramidbox_lite_mobile_mask对图片进行口罩检测
```shell
$ wget https://paddlehub.bj.bcebos.com/resources/test_mask_detection.jpg
$ hub run pyramidbox_lite_mobile_mask --input_path test_mask_detection.jpg
```
![](./imgs/test_mask_detection_result.jpg)
* 使用[图像分割](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=ImageSegmentation)模型进行人像扣图和人体部件识别
```shell
$ wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
$ hub run ace2p --input_path test_image.jpg
$ hub run deeplabv3p_xception65_humanseg --input_path test_image.jpg
```
![](./imgs/img_seg_result.jpeg) ![](./imgs/humanseg_test_res.png)
<p align='center'>
&#8194;&#8194;&#8194&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;ACE2P人体部件分割&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;&#8194;
HumanSeg人像分割&#8194;&#8194;&#8194;
</p>
## NLP领域场景体验
* 使用[词法分析](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=LexicalAnalysis)模型LAC进行分词
```shell
$ hub run lac --input_text "现在,慕尼黑再保险公司不仅是此类行动的倡议者,更是将其大量气候数据整合进保险产品中,并与公众共享大量天气信息,参与到新能源领域的保障中。"
[{
'word': ['现在', ',', '慕尼黑再保险公司', '不仅', '是', '此类', '行动', '的', '倡议者', ',', '更是', '将', '其', '大量', '气候', '数据', '整合', '进', '保险', '产品', '中', ',', '并', '与', '公众', '共享', '大量', '天气', '信息', ',', '参与', '到', '新能源', '领域', '的', '保障', '中', '。'],
'tag': ['TIME', 'w', 'ORG', 'c', 'v', 'r', 'n', 'u', 'n', 'w', 'd', 'p', 'r', 'a', 'n', 'n', 'v', 'v', 'n', 'n', 'f', 'w', 'c', 'p', 'n', 'v', 'a', 'n', 'n', 'w', 'v', 'v', 'n', 'n', 'u', 'vn', 'f', 'w']
}]
```
* 使用[情感分析](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=SentimentAnalysis)模型Senta对句子进行情感预测
```shell
$ hub run senta_bilstm --input_text "今天天气真好"
{'text': '今天天气真好', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9798, 'negative_probs': 0.0202}]
```
除了上述几类模型外,PaddleHub还发布了图像分类、语义模型、视频分类、图像生成、图像分割、文本审核、关键点检测等业界主流模型,更多PaddleHub已经发布的模型,请前往 [PaddleHub官网](https://www.paddlepaddle.org.cn/hub) 查看
# Class `hub.datasets.Canvas`
```python
hub.datasets.Canvas(
transforms: Callable,
mode: str = 'train')
```
Dataset for colorization. It contains 1193 and 400 pictures for Monet and Vango paintings style, respectively. We collected data from https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/.
**Args**
* transforms(callmethod) : The method of preprocess images.
* mode(str): The mode for preparing dataset.
# Class `hub.datasets.Flowers`
```python
hub.datasets.Flowers(
transforms: Callable,
mode: str = 'train')
```
Dataset for image classification. It contains 5 categories(roses, tulips, daisy, sunflowers, dandelion) and a total of 3667 pictures, of which 2914 are used for training, 382 are used for verification, and 371 are used for testing.
**Args**
* transforms(callmethod) : The method of preprocess images.
* mode(str): The mode for preparing dataset.
# Class `hub.datasets.MiniCOCO`
```python
hub.datasets.MiniCOCO(
transforms: Callable,
mode: str = 'train')
```
Dataset for Style transfer. The dataset contains 2001 images for training set and 200 images for testing set.They are derived form COCO2014. Meanwhile, it contains 21 different style pictures in file "21styles".
**Args**
* transforms(callmethod) : The method of preprocess images.
* mode(str): The mode for preparing dataset.
# Class `hub.datasets.OpticDiscSeg`
```python
hub.datasets.OpticDiscSeg(
transforms: Callable,
mode: str = 'train')
```
Dataset for semantic segmentation. The dataset contains 267 images for training set, 76 images for validation set and 38 images for testing set.
**Args**
* transforms(callmethod) : The method of preprocess images.
* mode(str): The mode for preparing dataset.
预置数据集
==================
PaddleHub预置了多种任务的预置数据集,用户可以通过预置数据集进行快速体验。
.. toctree::
:maxdepth: 1
:titlesonly:
hub.datasets.Canvas<datasets/canvas>
hub.datasets.Flowers<datasets/flowers>
hub.datasets.MiniCOCO<datasets/minicoco>
\ No newline at end of file
# Class `hub.finetune.Trainer`
```python
hub.finetune.Trainer(
model: paddle.nn.Layer,
optimizer: paddle.optimizer.Optimizer,
use_vdl: bool = True,
checkpoint_dir: str = None,
compare_metrics: Callable = None)
```
Model trainer
**Args**
* model(paddle.nn.Layer) : Model to train or evaluate.
* optimizer(paddle.optimizer.Optimizer) : Optimizer for loss.
* use_vdl(bool) : Whether to use visualdl to record training data.
* checkpoint_dir(str) : Directory where the checkpoint is saved, and the trainer will restore the state and model parameters from the checkpoint.
* compare_metrics(callable) : The method of comparing the model metrics. If not specified, the main metric return by `validation_step` will be used for comparison by default, the larger the value, the better the effect. This method will affect the saving of the best model. If the default behavior does not meet your requirements, please pass in a custom method.
----
```python
def train(
train_dataset: paddle.io.Dataset,
epochs: int = 1,
batch_size: int = 1,
num_workers: int = 0,
eval_dataset: paddle.io.Dataset = None,
log_interval: int = 10,
save_interval: int = 10)
```
Train a model with specific config.
**Args**
* train_dataset(paddle.io.Dataset) : Dataset to train the model
* epochs(int) : Number of training loops, default is 1.
* batch_size(int) : Batch size of per step, default is 1.
* num_workers(int) : Number of subprocess to load data, default is 0.
* eval_dataset(paddle.io.Dataset) : The validation dataset, deafult is None. If set, the Trainer will execute evaluate function every `save_interval` epochs.
* log_interval(int) : Log the train infomation every `log_interval` steps.
* save_interval(int) : Save the checkpoint every `save_interval` epochs.
----
```python
def evaluate(
eval_dataset: paddle.io.Dataset,
batch_size: int = 1,
num_workers: int = 0):
```
Run evaluation and returns metrics.
**Args**
* eval_dataset(paddle.io.Dataset) : The validation dataset
* batch_size(int) : Batch size of per step, default is 1.
* num_workers(int) : Number of subprocess to load data, default is 0.
# Class `hub.Module`
```python
hub.Module(
name: str = None,
directory: str = None,
version: str = None,
source: str = None,
update: bool = False,
branch: str = None,
**kwargs):)
```
In PaddleHub, Module represents an executable module, which usually a pre-trained model that can be used for end-to-end prediction, such as a face detection model or a lexical analysis model, or a pre-trained model that requires finetuning, such as BERT/ERNIE. When loading a Module with a specified name, if the Module does not exist locally, PaddleHub will automatically request the server or the specified Git source to download the resource.
**Args**
* name(str): Module name.
* directory(str|optional): Directory of the module to be loaded, only takes effect when the `name` is not specified.
* version(str|optional): The version limit of the module, only takes effect when the `name` is specified. When the local Module does not meet the specified version conditions, PaddleHub will re-request the server to download the appropriate Module. Default to None, This means that the local Module will be used. If the Module does not exist, PaddleHub will download the latest version available from the server according to the usage environment.
* source(str|optional): Url of a git repository. If this parameter is specified, PaddleHub will no longer download the specified Module from the default server, but will look for it in the specified repository. Default to None.
* update(bool|optional): Whether to update the locally cached git repository, only takes effect when the `source` is specified. Default to False.
* branch(str|optional): The branch of the specified git repository. Default to None.
参考
==================
以下介绍了PaddleHub 数据集(datasets)、训练器(finetune.Trainer)、预训练模型(Module)、数据增强模块(vision.transforms)等API介绍。
.. toctree::
:maxdepth: 1
hub.datasets<datasets_index>
hub.finetune.Trainer<finetune>
hub.Module<module>
hub.vision.transforms<vision>
\ No newline at end of file
# Class `hub.vision.transforms.Compose`
```python
hub.vision.transforms.Compose(
transforms: Callable,
to_rgb: bool = False)
```
Compose preprocessing operators for obtaining prepocessed data. The shape of input image for all operations is [H, W, C], where `H` is the image height, `W` is the image width, and `C` is the number of image channels.
**Args**
* transforms(callmethod) : The method of preprocess images.
* to_rgb(bool): Whether to transform the input from BGR mode to RGB mode, default is False.
# Class `hub.vision.transforms.RandomHorizontalFlip`
```python
hub.vision.transforms.RandomHorizontalFlip(prob: float = 0.5)
```
Randomly flip the image horizontally according to given probability.
**Args**
* prob(float): The probability for flipping the image horizontally, default is 0.5.
# Class `hub.vision.transforms.RandomVerticalFlip`
```python
hub.vision.transforms.RandomVerticalFlip(
prob: float = 0.5)
```
Randomly flip the image vertically according to given probability.
**Args**
* prob(float): The probability for flipping the image vertically, default is 0.5.
# Class `hub.vision.transforms.Resize`
```python
hub.vision.transforms.Resize(
target_size: Union[List[int], int],
interpolation: str = 'LINEAR')
```
Resize input image to target size.
**Args**
* target_size(List[int]|int]): Target image size.
* interpolation(str): Interpolation mode, default is 'LINEAR'. It support 6 modes: 'NEAREST', 'LINEAR', 'CUBIC', 'AREA', 'LANCZOS4' and 'RANDOM'.
# Class `hub.vision.transforms.ResizeByLong`
```python
hub.vision.transforms.ResizeByLong(long_size: int)
```
Resize the long side of the input image to the target size.
**Args**
* long_size(int|list[int]): The target size of long side.
# Class `hub.vision.transforms.ResizeRangeScaling`
```python
hub.vision.transforms.ResizeRangeScaling(
min_value: int = 400,
max_value: int = 600)
```
Randomly select a targeted size to resize the image according to given range.
**Args**
* min_value(int): The minimum value for targeted size.
* max_value(int): The maximum value for targeted size.
# Class `hub.vision.transforms.ResizeStepScaling`
```python
hub.vision.transforms.ResizeStepScaling(
min_scale_factor: float = 0.75,
max_scale_factor: float = 1.25,
scale_step_size: float = 0.25)
```
Randomly select a scale factor to resize the image according to given range.
**Args**
* min_scale_factor(float): The minimum scale factor for targeted scale.
* max_scale_factor(float): The maximum scale factor for targeted scale.
* scale_step_size(float): Scale interval.
# Class `hub.vision.transforms.Normalize`
```python
hub.vision.transforms.Normalize(
mean: list = [0.5, 0.5, 0.5],
std: list =[0.5, 0.5, 0.5])
```
Normalize the input image.
**Args**
* mean(list): Mean value for normalization.
* std(list): Standard deviation for normalization.
# Class `hub.vision.transforms.Padding`
```python
hub.vision.transforms.Padding(
target_size: Union[List[int], Tuple[int], int],
im_padding_value: list = [127.5, 127.5, 127.5])
```
Padding input into targeted size according to specific padding value.
**Args**
* target_size(Union[List[int], Tuple[int], int]): Targeted image size.
* im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5].
# Class `hub.vision.transforms.RandomPaddingCrop`
```python
hub.vision.transforms.RandomPaddingCrop(
crop_size(Union[List[int], Tuple[int], int]),
im_padding_value: list = [127.5, 127.5, 127.5])
```
Padding input image if crop size is greater than image size. Otherwise, crop the input image to given size.
**Args**
* crop_size(Union[List[int], Tuple[int], int]): Targeted image size.
* im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5].
# Class `hub.vision.transforms.RandomBlur`
```python
hub.vision.transforms.RandomBlur(prob: float = 0.1)
```
Random blur input image by Gaussian filter according to given probability.
**Args**
* prob(float): The probability to blur the image, default is 0.1.
# Class `hub.vision.transforms.RandomRotation`
```python
hub.vision.transforms.RandomRotation(
max_rotation: float = 15.,
im_padding_value: list = [127.5, 127.5, 127.5])
```
Rotate the input image at random angle. The angle will not exceed to max_rotation.
**Args**
* max_rotation(float): Upper bound of rotation angle.
* im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5].
# Class `hub.vision.transforms.RandomDistort`
```python
hub.vision.transforms.RandomDistort(
brightness_range: float = 0.5,
brightness_prob: float = 0.5,
contrast_range: float = 0.5,
contrast_prob: float = 0.5,
saturation_range: float = 0.5,
saturation_prob: float = 0.5,
hue_range: float= 18.,
hue_prob: float= 0.5)
```
Random adjust brightness, contrast, saturation and hue according to the given random range and probability, respectively.
**Args**
* brightness_range(float): Boundary of brightness.
* brightness_prob(float): Probability for disturb the brightness of image.
* contrast_range(float): Boundary of contrast.
* contrast_prob(float): Probability for disturb the contrast of image.
* saturation_range(float): Boundary of saturation.
* saturation_prob(float): Probability for disturb the saturation of image.
* hue_range(float): Boundary of hue.
* hue_prob(float): Probability for disturb the hue of image.
# Class `hub.vision.transforms.RGB2LAB`
```python
hub.vision.transforms.RGB2LAB()
```
Convert color space from RGB to LAB.
# Class `hub.vision.transforms.LAB2RGB`
```python
hub.vision.transforms.LAB2RGB()
```
Convert color space from LAB to RGB.
# Class `hub.vision.transforms.CenterCrop`
```python
hub.vision.transforms.CenterCrop(crop_size: int)
```
Crop the middle part of the image to the specified size.
**Args**
* crop_size(int): Target size for croped image.
文件模式从 100644 更改为 100755
==================
迁移学习
==================
迁移学习 (Transfer Learning) 是属于深度学习的一个子研究领域,该研究领域的目标在于利用数据、任务、或模型之间的相似性,将在旧领域学习过的知识,迁移应用于新领域中。通俗的来讲,迁移学习就是运用已有的知识来学习新的知识,例如学会了骑自行车的人也能较快的学会骑电动车。较为常用的一种迁移学习方式是利用预训练模型进行微调,即用户基于当前任务的场景从PaddleHub中选择已训练成功的模型进行新任务训练,且该模型曾经使用的数据集与新场景的数据集情况相近,此时仅需要在当前任务场景的训练过程中使用新场景的数据对模型参数进行微调(**Fine-tune**),即可完成训练任务。迁移学习吸引了很多研究者投身其中,因为它能够很好的解决深度学习中的以下几个问题:
* 一些研究领域只有少量标注数据,且数据标注成本较高,不足以训练一个足够鲁棒的神经网络。
* 大规模神经网络的训练依赖于大量的计算资源,这对于一般用户而言难以实现。
* 应对于普适化需求的模型,在特定应用上表现不尽如人意。
为了让开发者更便捷地应用迁移学习,飞桨开源了预训练模型管理工具 PaddleHub。开发者仅仅使用十余行的代码,就能完成迁移学习。本文将为读者全面介绍使用PaddleHub完成迁移学习的方法。
.. image:: ../imgs/paddlehub_finetune.gif
:width: 900px
:align: center
.. toctree::
:maxdepth: 2
finetune/sequence_labeling.md
finetune/text_matching.md
finetune/image_classification.md
finetune/image_colorization.md
finetune/style_transfer.md
finetune/semantic_segmentation.md
finetune/audio_classification.md
\ No newline at end of file
部署预训练模型
==================
本文介绍了如何生成可用于服务化部署的模型、如何实现服务化部署以及如何获取ERNIE/BERT Embedding服务的方法。
详细信息,参考以下教程:
.. toctree::
:maxdepth: 1
Fine-tune模型转化为PaddleHub Module<finetuned_model_to_module.md>
服务化部署<serving>
文本Embedding服务<bert_service>
# Bert Service
## 简介
### 为什么使用Bert Service
开发者在进行NLP(自然语言处理)的时候常常需要获取文本的embedding(词向量),需要专门编写相应代码,且只能在本地进行。使用`Bert Service`能够将此过程转化为远程调用API,完成远程获取embedding。
### 什么是Bert Service
`Bert Service`是基于[Paddle Serving](https://github.com/PaddlePaddle/Serving)框架的快速部署模型远程计算服务方案,可将获取embedding的过程通过调用API接口的方式实现,减少了对机器资源的依赖。使用PaddleHub可在服务器上一键部署`Bert Service`服务,在另外的普通机器上通过客户端接口即可轻松的获取文本对应的embedding数据。
整体流程图如下:
![](../imgs/bs.png)
### Bert Service的特点
使用`Bert Service`能够帮助开发者在限制性较强的环境中有效获取embedding,常用于以下情景:
* 算力有限的集群环境中,可利用一台或几台高性能机器部署`Bert Service`服务端,为全部机器提供在线embedding功能。
* 实际的生产服务器不适宜承担大批量embedding工作,通过API接口可减少资源占用。
* 专注下游深度学习任务,可利用PaddleHub的`Bert Service`大幅减少embedding代码。
`Bert Service`具有几个突出的优点:
* 代码精短,易于使用。简单的pip安装方式,服务端仅需一行命令即可启动,客户端仅需一行代码即可获取embedding结果。
* 更高性能,更高效率。通过Paddle AnalysisPredictor API对模型的计算图进行优化,提升了计算速度并减小了显存占用。
* 随"机"应变,灵活扩展。可根据机器资源选择不同数量的服务端,并根据实际需求快速、灵活地进行增减,同时支持各张显卡执行不同的模型计算任务。
* 删繁就简,专注任务。`Bert Service`基于PaddlePaddle和PaddleHub开发,将模型的下载和安装等管理工作交由PaddleHub,开发者可以专注于主要任务,还可以无缝对接PaddleHub继续进行文本分类、序列标注等下游任务。
使用Bert Service搭建服务主要分为下面三个步骤:
## Step1:准备环境
### 环境要求
下表是使用`Bert Service`的环境要求,带有*号标志项为非必需依赖,可根据实际使用需求选择安装。
|项目|版本|说明|
|:-:|:-:|:-:|
|操作系统|Linux|目前仅支持Linux操作系统|
|PaddleHub|>=1.4.0|无|
|PaddlePaddle|>=1.6.1|若使用GPU计算,则对应使用PaddlePaddle-gpu版本|
|GCC|>=4.8|无|
|paddle-gpu-serving*|>=0.8.2|在`Bert Service`服务端需依赖此包|
|ujson*|>=1.35|在`Bert Service`客户端需依赖此包|
### 安装步骤
a) 安装PaddlePaddle,利用pip下载CPU版本命令如下。GPU版本、Docker方式安装等其他更具体的安装过程见[开始使用PaddlePaddle](https://paddlepaddle.org.cn/install/quick)
```shell
$ # 安装paddlepaddle的CPU版本
$ pip install paddlepaddle
```
b) 安装PaddleHub
```shell
$ pip install paddlehub
```
c) server端,需另外安装`paddle-gpu-serving`,以获取快速部署服务的能力
```shell
$ pip install paddle-gpu-serving
```
d) client端,需另外安装ujson
```shell
$ pip install ujson
```
### 支持模型
目前`Bert Service`支持的语义模型如下表,可根据需要选择模型进行部署embedding服务,未来还将支持更多模型。
|模型|网络|
|:-|:-:|
|[ernie](https://paddlepaddle.org.cn/hubdetail?name=ERNIE&en_category=SemanticModel)|ERNIE|
|[ernie_tiny](https://paddlepaddle.org.cn/hubdetail?name=ernie_tiny&en_category=SemanticModel)|ERNIE tiny|
|[ernie_v2_eng_large](https://paddlepaddle.org.cn/hubdetail?name=ernie_v2_eng_large&en_category=SemanticModel)|ERNIE 2.0|
|[ernie_v2_eng_base](https://paddlepaddle.org.cn/hubdetail?name=ernie_v2_eng_base&en_category=SemanticModel)|ERNIE 2.0|
|[roberta_wwm_ext_chinese_L-12_H-768_A-12](https://paddlepaddle.org.cn/hubdetail?name=roberta_wwm_ext_chinese_L-12_H-768_A-12&en_category=SemanticModel)|BERT|
|[roberta_wwm_ext_chinese_L-24_H-1024_A-16](https://paddlepaddle.org.cn/hubdetail?name=roberta_wwm_ext_chinese_L-24_H-1024_A-16&en_category=SemanticModel)|BERT|
|[bert_wwm_ext_chinese_L-12_H-768_A-12](https://paddlepaddle.org.cn/hubdetail?name=bert_wwm_ext_chinese_L-12_H-768_A-12&en_category=SemanticModel)|BERT|
|[bert_uncased_L-12_H-768_A-12](https://paddlepaddle.org.cn/hubdetail?name=bert_uncased_L-12_H-768_A-12&en_category=SemanticModel)|BERT|
|[bert_uncased_L-24_H-1024_A-16](https://paddlepaddle.org.cn/hubdetail?name=bert_uncased_L-24_H-1024_A-16&en_category=SemanticModel)|BERT|
|[bert_cased_L-12_H-768_A-12](https://paddlepaddle.org.cn/hubdetail?name=bert_cased_L-12_H-768_A-12&en_category=SemanticModel)|BERT|
|[bert_cased_L-24_H-1024_A-16](https://paddlepaddle.org.cn/hubdetail?name=bert_cased_L-24_H-1024_A-16&en_category=SemanticModel)|BERT|
|[bert_multi_cased_L-12_H-768_A-12](https://paddlepaddle.org.cn/hubdetail?name=bert_multi_cased_L-12_H-768_A-12&en_category=SemanticModel)|BERT|
|[bert_chinese_L-12_H-768_A-12](https://paddlepaddle.org.cn/hubdetail?name=bert_chinese_L-12_H-768_A-12&en_category=SemanticModel)|BERT|
## Step2:启动服务端(server)
### 简介
server端接收client端发送的数据,执行模型计算过程并将计算结果返回给client端。
server端启动时会按照指定的模型名称从PaddleHub获取对应的模型文件进行加载,无需提前下载模型或指定模型路径,对模型的管理工作由PaddleHub负责。在加载模型后在指定的端口启动`BRPC`服务,保持端口监听,当接收到数据后便执行模型计算,并将计算结果通过`BRPC`返回并发送至client端。
### 启动
使用PaddleHub的命令行工具可一键启动`Bert Service`,命令如下:
```shell
$ hub serving start bert_service -m ernie_tiny -p 8866 --use_gpu --gpu 0
```
启动成功则显示
```shell
Server[baidu::paddle_serving::predictor::bert_service::BertServiceImpl] is serving on port=8866.
```
[整个启动过程动态图](https://github.com/ShenYuhan/ml-python/blob/master/short_start_fast.gif)
其中各参数说明如下表:
<div align="center">
|参数|说明|是否必填|
|:--:|:--:|:----:|
|hub serving start bert_service|启动`Bert Service`服务端。|必填项|
|--module/-m|指定启动的模型,如果指定的模型不存在,则自动通过PaddleHub下载指定模型。|必填项|
|--port/-p|指定启动的端口,每个端口对应一个模型,可基于不同端口进行多次启动,以实现多个模型的服务部署。|必填项|
|--use_gpu|若指定此项则使用GPU进行工作,反之仅使用CPU。注意需安装GPU版本的PaddlePaddle。|非必填项,默认为不指定|
|--gpu|指定使用的GPU卡号,如未指定use_gpu则填写此项无效,每个服务对应一张卡,部署多个服务时需指定不同卡|非必填项,默认为0号显卡|
</div>
### 关闭
通过在启动服务端的命令行页面使用Ctrl+C终止`Bert Service`运行,关闭成功则显示:
```shell
Paddle Inference Server exit successfully!
```
## Step3:启动客户端(client)
### 简介
client端接收文本数据,并获取server端返回的模型计算的embedding结果。
client端利用PaddleHub的语义理解任务将原始文本按照不同模型的数据预处理方案将文本ID化,并生成对应的sentence type、position、input masks数据,将这些信息封装成json数据,通过http协议按照指定的IP端口信息发送至server端,等待并获取模型生成结果。
### 启动
服务端类BSClient初始化方法原型为:
```python
BSClient.__init__(self,
module_name,
server,
max_seq_len=20,
show_ids=False,
do_lower_case=True,
retry=3)
# 获取embedding方法原型为
BSClient.get_result(self, input_text)
```
其中各参数说明如下表:
|参数|说明|类型|样例|
|:--:|:--:|:--:|:--:|
|module_name|指定使用的模型名称|string|"ernie"|
|server|要访问的server地址,包括ip地址及端口号|string|"127.0.0.1:8866"|
|max_seq_len|计算时的样例长度,样例长度不足时采用补零策略,超出此参数则超出部分会被截断|int|128|
|show_ids|是否展现数据预处理后的样例信息,指定为True则显示样例信息,反之则不显示|bool|False|
|do_lower_case|是否将英文字母转换成小写,指定为True则将所有英文字母转换为小写,反之则保持原状|bool|True|
|retry|连接失败后的最大重试次数|int|3|
|input_text|输入文本,要获取embedding的原始文本|二维list类型,内部元素为string类型的文本|[['样例1'],['样例2']]|
## Demo——利用Bert Service部署ernie_tiny在线embedding服务
在这里,我们将展示一个实际场景中可能使用的demo,我们利用PaddleHub在一台GPU机器上部署`ernie_tiny`模型服务,并在另一台CPU机器上尝试访问,获取一首七言绝句的embedding。
### Step1:安装环境依赖
首先需要安装环境依赖,根据第2节内容分别在两台机器上安装相应依赖。
### Step2:启动Bert Service服务端
确保环境依赖安装正确后,在要部署服务的GPU机器上使用PaddleHub命令行工具启动`Bert Service`服务端,命令如下:
```shell
$ hub serving start bert_service -m ernie_tiny --use_gpu --gpu 0 --port 8866
```
启动成功后打印
```shell
Server[baidu::paddle_serving::predictor::bert_service::BertServiceImpl] is serving on port=8866.
```
这样就启动了`ernie_tiny`的在线服务,监听8866端口,并在0号GPU上进行任务。
### Step3:使用Bert Service客户端进行远程调用
部署好服务端后,就可以用普通机器作为客户端测试在线embedding功能。
首先导入客户端依赖。
```python
from paddlehub.serving.bert_serving import bs_client
```
接着启动并初始化`bert service`客户端`BSClient`(这里的server为虚拟地址,需根据自己实际ip设置)
```python
bc = bs_client.BSClient(module_name="ernie_tiny", server="127.0.0.1:8866")
```
然后输入文本信息。
```python
input_text = [["西风吹老洞庭波"], ["一夜湘君白发多"], ["醉后不知天在水"], ["满船清梦压星河"], ]
```
最后利用客户端接口`get_result`发送文本到服务端,以获取embedding结果。
```python
result = bc.get_result(input_text=input_text)
```
这样,就得到了embedding结果(此处只展示部分结果)。
```python
[[0.9993321895599361, 0.9994612336158751, 0.9999646544456481, 0.732795298099517, -0.34387934207916204, ... ]]
```
客户端代码demo文件见[示例](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.7/demo/serving/bert_service/bert_service_client.py)
运行命令如下:
```shell
$ python bert_service_client.py
```
[运行过程动态图](https://github.com/ShenYuhan/ml-python/blob/master/short_client_fast.gif)
### Step4:关闭Bert Service服务端
如要停止`Bert Service`服务端程序,可在其启动命令行页面使用Ctrl+C方式关闭,关闭成功会打印如下日志:
```shell
Paddle Inference Server exit successfully!
```
这样,我们就利用一台GPU机器就完成了`Bert Service`的部署,并利用另一台普通机器进行了测试,可见通过`Bert Service`能够方便地进行在线embedding服务的快速部署。
## 预训练模型一键服务部署
除了`Bert Service`外,PaddleHub Serving还具有预训练模型一键服务部署功能,能够将预训练模型快捷部署上线,对外提供可靠的在线预测服务,具体信息请参见[Module Serving](./serving.md)
## FAQ
Q : 如何在一台服务器部署多个模型?
A : 可通过多次启动`Bert Service`,分配不同端口实现。如果使用GPU,需要指定不同的显卡。如同时部署`ernie``bert_chinese_L-12_H-768_A-12`,分别执行命令如下:
```shell
$ hub serving start bert_service -m ernie -p 8866
$ hub serving start bert_service -m bert_chinese_L-12_H-768_A-12 -p 8867
```
Q : 启动时显示"Check out http://yq01-gpu-255-129-12-00.epc.baidu.com:8887 in web
browser.",这个页面有什么作用。
A : 这是`BRPC`的内置服务,主要用于查看请求数、资源占用等信息,可对server端性能有大致了解,具体信息可查看[BRPC内置服务](https://github.com/apache/incubator-brpc/tree/master/docs/cn)
Q : 为什么输入文本的格式为[["文本1"], ["文本2"], ],而不是["文本1", "文本2", ]?
A : 因为Bert模型可以对一轮对话生成向量表示,例如[["问题1","回答1"],["问题2","回答2"]],为了防止使用时混乱,每个样本使用一个list表示,一个样本list内部可以是1条string或2条string,如下面的文本:
```python
input_text = [
["你今天吃饭了吗","我已经吃过饭了"],
["今天天气怎么样","今天天气不错"],
]
```
# PaddleHub命令行工具 ===========================
PaddleHub命令行工具
===========================
PaddleHub为预训练模型的管理和使用提供了命令行工具。 PaddleHub为预训练模型的管理和使用提供了命令行工具。
PaddleHub支持修改预训练模型存放路径: 我们一共提供了11个命令,涵盖了模型安装、卸载、预测等等各方面。
* 如已设置`${HUB_HOME}`环境变量,则预训练模型、配置等文件都存放在`${HUB_HOME}`指示的路径下
* 如未设置`${HUB_HOME}`环境变量,则存放在`$HOME`指示的路径下
目前命令行支持以下11个命令: hub install
==================
## `hub install`
用于将Module安装到本地,默认安装在`${HUB_HOME}/.paddlehub/modules`目录下,当一个Module安装到本地后,用户可以通过其他命令操作该Module(例如,使用该Module进行预测),也可以使用PaddleHub提供的python API,将Module应用到自己的任务中,实现迁移学习 用于将Module安装到本地,默认安装在`${HUB_HOME}/.paddlehub/modules`目录下,当一个Module安装到本地后,用户可以通过其他命令操作该Module(例如,使用该Module进行预测),也可以使用PaddleHub提供的python API,将Module应用到自己的任务中,实现迁移学习
## `hub uninstall` .. tip::
如果设置了环境变量 *${HUB_HOME}* ,则预训练模型和相关的配置文件都会保存到指定的 *${HUB_HOME}* 路径下。
如果未设置环境变量 *${HUB_HOME}* ,则预训练模型和相关的配置文件都会保存到用户的主目录 *$HOME* 下。
hub uninstall
==================
用于卸载本地Module 用于卸载本地Module
## `hub show` hub show
==================
用于查看本地已安装Module的属性或者指定目录下确定的Module的属性,包括其名字、版本、描述、作者等信息 用于查看本地已安装Module的属性或者指定目录下确定的Module的属性,包括其名字、版本、描述、作者等信息
## `hub download` hub download
==================
用于下载PaddleHub提供的Module 用于下载PaddleHub提供的Module
## `hub search` hub search
==================
通过关键字在服务端检索匹配的Module,当想要查找某个特定模型的Module时,使用search命令可以快速得到结果,例如`hub search ssd`命令,会查找所有包含了ssd字样的Module,命令支持正则表达式,例如`hub search ^s.*`搜索所有以s开头的资源。 通过关键字在服务端检索匹配的Module,当想要查找某个特定模型的Module时,使用search命令可以快速得到结果,例如`hub search ssd`命令,会查找所有包含了ssd字样的Module,命令支持正则表达式,例如`hub search ^s.*`搜索所有以s开头的资源。
`注意` .. tip::
如果想要搜索全部的Module,使用`hub search *`并不生效,这是因为shell会自行进行通配符展开,将*替换为当前目录下的文件名。为了进行全局搜索,用户可以直接键入`hub search`
如果想要搜索全部的Module,使用`hub search \*`并不生效,这是因为shell会自行进行通配符展开,将\*替换为当前目录下的文件名。为了进行全局搜索,用户可以直接键入`hub search`。
## `hub list` hub list
==================
列出本地已经安装的Module 列出本地已经安装的Module
## `hub run` hub run
==================
用于执行Module的预测,需要注意的是,并不是所有的模型都支持预测(同样,也不是所有的模型都支持迁移学习)。使用示例可以参考[hub run快速体验](../quick_experience/cmd_quick_run.md)。 用于执行Module的预测,需要注意的是,并不是所有的模型都支持预测(同样,也不是所有的模型都支持迁移学习)。使用示例可以参考[hub run快速体验](../quick_experience/cmd_quick_run.md)。
## `hub help` PaddleHub尽量简化了用户在使用命令行预测时的理解成本,一般来讲,我们将预测分为NLP和CV两大类
显示帮助信息 NLP类的任务
---------------
输入数据通过--input_text指定。以百度LAC模型(中文词法分析)为例,可以通过以下命令实现文本分析。
## `hub version`
显示PaddleHub版本信息 .. code-block:: console
## `hub clear` $ hub run lac --input_text "今天是个好日子"
PaddleHub在使用过程中会产生一些缓存数据,这部分数据默认存放在${HUB_HOME}/.paddlehub/cache目录下,用户可以通过clear命令来清空缓存
## `hub config` CV类的任务
用于查看和设置paddlehub相关设置,包括对server地址、日志级别的设置: ---------------
`示例` 输入数据通过`--input_path`指定。以SSD模型(单阶段目标检测)为例子,可以通过以下命令实现预测
* `hub config`: 显示当前paddlehub的设置
* `hub config reset`: 恢复当前paddlehub的设置为默认设置 .. code-block:: console
* `hub config server==[address]`: 设置当前paddlehub-server地址为[address],paddlehub客户端从此地址获取模型信息 $ hub run resnet_v2_50_imagenet --input_path test.jpg
* `hub config log.level==[level]`: 设置当前日志级别为[level], 可选值为CRITICAL, ERROR, WARNING, EVAL, TRAIN, INFO, DEBUG, 从左到右优先级从高到低 hub help
==================
* `hub config log.enable==True|False`: 设置当日志是否可用 显示帮助信息
## `hub serving` hub version
==================
用于一键部署Module预测服务,详细用法见[PaddleHub Serving一键服务部署](serving.md) 显示PaddleHub版本信息
**NOTE:** hub clear
==================
在PaddleHub中,Module表示一个`可执行的预训练模型`,一个Module可以支持直接命令行预测,也可以配合PaddleHub Finetune API,通过少量代码实现迁移学习。不是所有的Module都支持命令行预测 (例如BERT/ERNIE Transformer类模型,一般需要搭配任务进行finetune),也不是所有的Module都可用于finetune(例如LAC词法分析模型,我们不建议用户用于finetune)。 PaddleHub在使用过程中会产生一些缓存数据,这部分数据默认存放在${HUB_HOME}/.paddlehub/cache目录下,用户可以通过clear命令来清空缓存
PaddleHub尽量简化了用户在使用命令行预测时的理解成本,一般来讲,我们将预测分为NLP和CV两大类 hub config
==================
用于查看和设置paddlehub相关设置,包括对server地址、日志级别的设置:
## NLP类的任务 .. code-block:: console
输入数据通过--input_text或者--input_file指定。以百度LAC模型(中文词法分析)为例,可以通过以下两个命令实现单行文本和多行文本的分析。
$ # 显示当前paddlehub的设置
```shell $ hub config
# 单文本预测
$ hub run lac --input_text "今天是个好日子" $ # 恢复当前paddlehub的设置为默认设置
``` $ hub config reset
```shell
# 多文本分析 $ # 设置当前paddlehub-server地址为${HOST},paddlehub客户端从此地址获取模型信息
$ hub run lac --input_file test.txt $ hub config server==${HOST}
```
$ # 设置当前日志级别为${LEVEL}, 可选值为CRITICAL, ERROR, WARNING, EVAL, TRAIN, INFO, DEBUG, 从左到右优先级从高到低
其中test.txt的样例格式如下,每行是一个需要词法分析句子 $ hub config log.level==${LEVEL}
``` $ # 设置当日志是否可用
今天是个好日子 $ hub config log.enable==True|False
天气预报说今天要下雨
下一班地铁马上就要到了 hub serving
……更多行…… ==================
```
用于一键部署Module预测服务,详细用法见`PaddleHub Serving一键服务部署 <serving>`_
## CV类的任务 \ No newline at end of file
输入数据通过`--input_path`或者`--input_file`指定。以SSD模型(单阶段目标检测)为例子,可以通过以下两个命令实现单张图片和多张图片的预测
```shell
# 单张照片预测
$ hub run resnet_v2_50_imagenet --input_path test.jpg
```
```shell
# 多张照片预测
$ hub run resnet_v2_50_imagenet --input_file test.txt
```
其中test.txt的格式为
```
cat.jpg
dog.jpg
person.jpg
……更多行……
```
# 如何编写一个PaddleHub Module
## 模型基本信息
我们准备编写一个PaddleHub Module,Module的基本信息如下:
```yaml
name="openpose_body_estimation",
type="CV/image_editing",
author="paddlepaddle",
author_email="",
summary="Openpose_body_estimation is a body pose estimation model based on Realtime Multi-Person 2D Pose \
Estimation using Part Affinity Fields.",
version="1.0.0"
```
Module存在一个接口predict,用于接收传入图片,并得到最终输出的结果,支持python接口调用和命令行调用。
```python
import paddlehub as hub
model = hub.Module(name="openpose_body_estimation")
result = model.predict("demo.jpg")
```
```cmd
hub run openpose_body_estimation --input_path demo.jpg
```
## Module创建
### step 1. 创建必要的目录与文件
创建一个openpose_body_estimation的目录,并在openpose_body_estimation目录下分别创建module.py, processor.py。其中
|文件名|用途|
|-|-|
|module.py|主模块,提供Module的实现代码|
|processor.py|辅助模块,提供词表加载的方法|
```cmd
➜ tree openpose_body_estimation
openpose_body_estimation/
├── module.py
└── processor.py
```
### step 2. 实现辅助模块processor
在processor.py中实现一些在module.py里面需要调用到的类和函数。例如在processor.py 中实现ResizeScaling类:
```python
class ResizeScaling:
"""Resize images by scaling method.
Args:
target(int): Target image size.
interpolation(Callable): Interpolation method.
"""
def __init__(self, target: int = 368, interpolation: Callable = cv2.INTER_CUBIC):
self.target = target
self.interpolation = interpolation
def __call__(self, img, scale_search):
scale = scale_search * self.target / img.shape[0]
resize_img = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=self.interpolation)
return resize_img
```
### step 3. 编写Module处理代码
module.py文件为Module的入口代码所在,我们需要在其中实现预测逻辑。
#### step 3_1. 引入必要的头文件
```python
import os
import time
import copy
import base64
import argparse
from typing import Union
from collections import OrderedDict
import cv2
import paddle
import paddle.nn as nn
import numpy as np
from paddlehub.module.module import moduleinfo, runnable, serving
import paddlehub.vision.transforms as T
import openpose_body_estimation.processor as P
```
**NOTE:** `paddlehub.vision.transforms`有常见的图像处理方法,可以方便调用。
#### step 3_2. 定义BodyPoseModel类
module.py中需要有一个继承了nn.Layer,该类负责实现预测逻辑,并使用moduleinfo填写基本信息。当使用hub.Module(name="openpose_body_estimation")加载Module时,PaddleHub会自动创建openpose_body_estimation的对象并返回。
```python
@moduleinfo(
name="openpose_body_estimation",
type="CV/image_editing",
author="paddlepaddle",
author_email="",
summary="Openpose_body_estimation is a body pose estimation model based on Realtime Multi-Person 2D Pose \
Estimation using Part Affinity Fields.",
version="1.0.0")
class BodyPoseModel(nn.Layer):
...
```
#### step 3_3. 执行必要的初始化及模型搭建
模型的初始化主要完成几个功能:待使用的类的声明,模型使用的类的声明及参数加载。
```python
def __init__(self, load_checkpoint: str = None):
super(BodyPoseModel, self).__init__()
#将会使用到的类的声明
self.resize_func = P.ResizeScaling()
self.norm_func = T.Normalize(std=[1, 1, 1])
#模型声明
self.input_nc = 4
self.output_nc = 2
model1 = (
Conv2D(self.input_nc, 64, 3, 1, 1),
nn.ReLU(),
Conv2D(64, 64, 3, 1, 1),
nn.ReLU(),
nn.BatchNorm(64),
)
self.model1 = nn.Sequential(*model1)
#参数加载
if load_checkpoint is not None:
self.model_dict = paddle.load(load_checkpoint)
self.set_dict(self.model_dict)
print("load custom checkpoint success")
else:
checkpoint = os.path.join(self.directory, 'model.pdparams')
self.model_dict = paddle.load(checkpoint)
self.set_dict(self.model_dict)
print("load pretrained checkpoint success")
```
模型的搭建主要在`forward`里面实现:
```python
def forward(self, input: paddle.Tensor) -> paddle.Tensor:
result = self.model1(input)
return result
```
#### step 3_4. 完善预测逻辑
```python
def predict(self, img:Union(np.ndarray,str), visualization: bool = True):
self.eval()
self.visualization = visualization
if isinstance(img, str):
orgImg = cv2.imread(img)
else:
orgImg = img
data = self.resize_func(self.norm_func(orgImg))
output = self.forward(paddle.to_tensor(data.astype('float32')))
output = paddle.clip(output[0].transpose((1, 2, 0)), 0, 255).numpy()
output = output.astype(np.uint8)
if self.visualization:
style_name = "body_" + str(time.time()) + ".png"
if not os.path.exists(save_path):
os.mkdir(save_path)
path = os.path.join(save_path, style_name)
cv2.imwrite(path, output)
return output
```
#### step 3_5. 支持命令行调用
如果希望Module可以支持命令行调用,则需要提供一个经过runnable修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
```python
@runnable
def run_cmd(self, argvs):
"""
Run as a command.
"""
self.parser = argparse.ArgumentParser(
description="Run the {} module.".format(self.name),
prog='hub run {}'.format(self.name),
usage='%(prog)s',
add_help=True)
self.arg_input_group = self.parser.add_argument_group(
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group(
title="Config options",
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg()
self.add_module_input_arg()
args = self.parser.parse_args(argvs)
results = self.predict(
img=args.input_path,
save_path=args.output_dir,
visualization=args.visualization)
return results
def add_module_config_arg(self):
"""
Add the command config options.
"""
self.arg_config_group.add_argument(
'--output_dir',
type=str,
default='openpose_body',
help="The directory to save output images.")
self.arg_config_group.add_argument(
'--save_dir',
type=str,
default='openpose_model',
help="The directory to save model.")
self.arg_config_group.add_argument(
'--visualization',
type=bool,
default=True,
help="whether to save output as images.")
def add_module_input_arg(self):
"""
Add the command input options.
"""
self.arg_input_group.add_argument(
'--input_path', type=str, help="path to image.")
```
#### step 3_6. 支持serving调用
如果希望Module可以支持PaddleHub Serving部署预测服务,则需要提供一个经过serving修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
如果不需要提供PaddleHub Serving部署预测服务,则可以不需要加上serving修饰。
```python
@serving
def serving_method(self, images, **kwargs):
"""
Run as a service.
"""
images_decode = [base64_to_cv2(image) for image in images]
results = self.predict(img=images_decode[0], **kwargs)
final={}
final['data'] = P.cv2_to_base64(results)
return final
```
## 测试步骤
完成Module编写后,我们可以通过以下方式测试该Module
### 调用方法1
将Module安装到本机中,再通过Hub.Module(name=...)加载
```shell
hub install openpose_body_estimation
```
```python
import paddlehub as hub
if __name__ == "__main__":
model = hub.Module(name='openpose_hands_estimation')
result = model.predict("demo.jpg")
```
### 调用方法2
将Module安装到本机中,再通过hub run运行
```shell
hub install openpose_body_estimation
hub run openpose_body_estimation --input_path demo.jpg
```
### 测试serving方法
运行启动命令:
```shell
$ hub serving start -m openpose_body_estimation
```
发送预测请求,获取预测结果.
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/openpose_body_estimation"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
canvas = base64_to_cv2(r.json()["results"]['data'])
cv2.imwrite('keypoint_body.png', canvas)
```
# 如何编写一个PaddleHub Module
## 模型基本信息
我们准备编写一个PaddleHub ModuleModule的基本信息如下:
```yaml
name="openpose_body_estimation",
type="CV/image_editing",
author="paddlepaddle",
author_email="",
summary="Openpose_body_estimation is a body pose estimation model based on Realtime Multi-Person 2D Pose \
Estimation using Part Affinity Fields.",
version="1.0.0"
```
Module存在一个接口predict,用于接收传入图片,并得到最终输出的结果,支持python接口调用和命令行调用。
```python
import paddlehub as hub
model = hub.Module(name="openpose_body_estimation")
result = model.predict("demo.jpg")
```
```cmd
hub run openpose_body_estimation --input_path demo.jpg
```
## Module创建
### step 1. 创建必要的目录与文件
创建一个openpose_body_estimation的目录,并在openpose_body_estimation目录下分别创建module.py, processor.py。其中
|文件名|用途|
|-|-|
|module.py|主模块,提供Module的实现代码|
|processor.py|辅助模块,提供词表加载的方法|
```cmd
tree openpose_body_estimation
openpose_body_estimation/
├── module.py
└── processor.py
```
### step 2. 实现辅助模块processor
processor.py中实现一些在module.py里面需要调用到的类和函数。例如在processor.py 中实现ResizeScaling类:
```python
class ResizeScaling:
"""Resize images by scaling method.
Args:
target(int): Target image size.
interpolation(Callable): Interpolation method.
"""
def __init__(self, target: int = 368, interpolation: Callable = cv2.INTER_CUBIC):
self.target = target
self.interpolation = interpolation
def __call__(self, img, scale_search):
scale = scale_search * self.target / img.shape[0]
resize_img = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=self.interpolation)
return resize_img
```
### step 3. 编写Module处理代码
module.py文件为Module的入口代码所在,我们需要在其中实现预测逻辑。
#### step 3_1. 引入必要的头文件
```python
import os
import time
import copy
import base64
import argparse
from typing import Union
from collections import OrderedDict
import cv2
import paddle
import paddle.nn as nn
import numpy as np
from paddlehub.module.module import moduleinfo, runnable, serving
import paddlehub.vision.transforms as T
import openpose_body_estimation.processor as P
```
**NOTE:** `paddlehub.vision.transforms`有常见的图像处理方法,可以方便调用。
#### step 3_2. 定义BodyPoseModel
module.py中需要有一个继承了nn.Layer,该类负责实现预测逻辑,并使用moduleinfo填写基本信息。当使用hub.Module(name="openpose_body_estimation")加载Module时,PaddleHub会自动创建openpose_body_estimation的对象并返回。
```python
@moduleinfo(
name="openpose_body_estimation",
type="CV/image_editing",
author="paddlepaddle",
author_email="",
summary="Openpose_body_estimation is a body pose estimation model based on Realtime Multi-Person 2D Pose \
Estimation using Part Affinity Fields.",
version="1.0.0")
class BodyPoseModel(nn.Layer):
...
```
#### step 3_3. 执行必要的初始化及模型搭建
模型的初始化主要完成几个功能:待使用的类的声明,模型使用的类的声明及参数加载。
```python
def __init__(self, load_checkpoint: str = None):
super(BodyPoseModel, self).__init__()
#将会使用到的类的声明
self.resize_func = P.ResizeScaling()
self.norm_func = T.Normalize(std=[1, 1, 1])
#模型声明
self.input_nc = 4
self.output_nc = 2
model1 = (
Conv2D(self.input_nc, 64, 3, 1, 1),
nn.ReLU(),
Conv2D(64, 64, 3, 1, 1),
nn.ReLU(),
nn.BatchNorm(64),
)
self.model1 = nn.Sequential(*model1)
#参数加载
if load_checkpoint is not None:
self.model_dict = paddle.load(load_checkpoint)
self.set_dict(self.model_dict)
print("load custom checkpoint success")
else:
checkpoint = os.path.join(self.directory, 'model.pdparams')
self.model_dict = paddle.load(checkpoint)
self.set_dict(self.model_dict)
print("load pretrained checkpoint success")
```
模型的搭建主要在`forward`里面实现:
```python
def forward(self, input: paddle.Tensor) -> paddle.Tensor:
result = self.model1(input)
return result
```
#### step 3_4. 完善预测逻辑
```python
def predict(self, img:Union(np.ndarray,str), visualization: bool = True):
self.eval()
self.visualization = visualization
if isinstance(img, str):
orgImg = cv2.imread(img)
else:
orgImg = img
data = self.resize_func(self.norm_func(orgImg))
output = self.forward(paddle.to_tensor(data.astype('float32')))
output = paddle.clip(output[0].transpose((1, 2, 0)), 0, 255).numpy()
output = output.astype(np.uint8)
if self.visualization:
style_name = "body_" + str(time.time()) + ".png"
if not os.path.exists(save_path):
os.mkdir(save_path)
path = os.path.join(save_path, style_name)
cv2.imwrite(path, output)
return output
```
#### step 3_5. 支持命令行调用
如果希望Module可以支持命令行调用,则需要提供一个经过runnable修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
```python
@runnable
def run_cmd(self, argvs):
"""
Run as a command.
"""
self.parser = argparse.ArgumentParser(
description="Run the {} module.".format(self.name),
prog='hub run {}'.format(self.name),
usage='%(prog)s',
add_help=True)
self.arg_input_group = self.parser.add_argument_group(
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group(
title="Config options",
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg()
self.add_module_input_arg()
args = self.parser.parse_args(argvs)
results = self.predict(
img=args.input_path,
save_path=args.output_dir,
visualization=args.visualization)
return results
def add_module_config_arg(self):
"""
Add the command config options.
"""
self.arg_config_group.add_argument(
'--output_dir',
type=str,
default='openpose_body',
help="The directory to save output images.")
self.arg_config_group.add_argument(
'--save_dir',
type=str,
default='openpose_model',
help="The directory to save model.")
self.arg_config_group.add_argument(
'--visualization',
type=bool,
default=True,
help="whether to save output as images.")
def add_module_input_arg(self):
"""
Add the command input options.
"""
self.arg_input_group.add_argument(
'--input_path', type=str, help="path to image.")
```
#### step 3_6. 支持serving调用
如果希望Module可以支持PaddleHub Serving部署预测服务,则需要提供一个经过serving修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
如果不需要提供PaddleHub Serving部署预测服务,则可以不需要加上serving修饰。
```python
@serving
def serving_method(self, images, **kwargs):
"""
Run as a service.
"""
images_decode = [base64_to_cv2(image) for image in images]
results = self.predict(img=images_decode[0], **kwargs)
final={}
final['data'] = P.cv2_to_base64(results)
return final
```
## 测试步骤
完成Module编写后,我们可以通过以下方式测试该Module
### 调用方法1
Module安装到本机中,再通过Hub.Module(name=...)加载
```shell
hub install openpose_body_estimation
```
```python
import paddlehub as hub
if __name__ == "__main__":
model = hub.Module(name='openpose_hands_estimation')
result = model.predict("demo.jpg")
```
### 调用方法2
Module安装到本机中,再通过hub run运行
```shell
hub install openpose_body_estimation
hub run openpose_body_estimation --input_path demo.jpg
```
### 测试serving方法
运行启动命令:
```shell
$ hub serving start -m openpose_body_estimation
```
发送预测请求,获取预测结果.
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/openpose_body_estimation"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
canvas = base64_to_cv2(r.json()["results"]['data'])
cv2.imwrite('keypoint_body.png', canvas)
```
======================
如何创建自己的Module
======================
一、 准备工作
=======================
模型基本信息
------------------------
我们准备编写一个PaddleHub Module,Module的基本信息如下:
.. code-block:: yaml
name: senta_test
version: 1.0.0
summary: This is a PaddleHub Module. Just for test.
author: anonymous
author_email:
type: nlp/sentiment_analysis
Module存在一个接口predict,用于接收传入图片,并得到最终输出的结果,支持python接口调用和命令行调用。
.. code-block:: python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
.. code-block:: shell
hub run senta_test --input_text 这部电影太差劲了
策略
------------------------
为了示例代码简单起见,我们使用一个非常简单的情感判断策略,当输入文本中带有词表中指定单词时,则判断文本倾向为负向,否则为正向
二、创建Module
=======================
步骤1. 创建必要的目录与文件
----------------------------------------------------
创建一个senta_test的目录,并在senta_test目录下分别创建module.py、processor.py、vocab.list,其中
.. code-block:: shell
$ tree senta_test
senta_test/
├── vocab.list
├── module.py
└── processor.py
============ =========================================================================
File Name Purpose
------------ -------------------------------------------------------------------------
============ =========================================================================
module.py 主模块,提供Module的实现代码
processor.py 辅助模块,提供词表加载的方法
vocab.list 存放词表
============ =========================================================================
步骤2. 实现辅助模块processor
------------------------------------------------
在processor.py中实现一个load_vocab接口用于读取词表
.. code-block:: python
def load_vocab(vocab_path):
with open(vocab_path) as file:
return file.read().split()
步骤3. 编写Module处理代码
------------------------------------------------
module.py文件为Module的入口代码所在,我们需要在其中实现预测逻辑。
步骤 3_1. 引入必要的头文件
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
import argparse
import os
import paddlehub as hub
from paddlehub.module.module import runnable, moduleinfo
from senta_test.processor import load_vocab
.. note::
当引用Module中模块时,需要输入全路径,如senta_test.processor
步骤 3_2. 定义SentaTest类
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
module.py中需要有一个继承了hub.Module的类存在,该类负责实现预测逻辑,并使用moduleinfo填写基本信息。当使用hub.Module(name="senta_test")加载Module时,PaddleHub会自动创建SentaTest的对象并返回。
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
步骤 3_3. 执行必要的初始化
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
def __init__(self):
# add arg parser
self.parser = argparse.ArgumentParser(
description="Run the senta_test module.",
prog='hub run senta_test',
usage='%(prog)s',
add_help=True)
self.parser.add_argument(
'--input_text', type=str, default=None, help="text to predict")
# load word dict
vocab_path = os.path.join(self.directory, "vocab.list")
self.vocab = load_vocab(vocab_path)
...
.. note::
执行类对象默认内置了directory属性,可以直接获取到Module所在路径
步骤 3_4: 完善预测逻辑
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
...
步骤 3_5. 支持命令行调用
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
如果希望Module可以支持命令行调用,则需要提供一个经过runnable修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
如果不需要提供命令行预测功能,则可以不实现该接口,PaddleHub在用命令行执行时,会自动发现该Module不支持命令行方式,并给出提示。
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
@runnable
def run_cmd(self, argvs):
args = self.parser.parse_args(argvs)
texts = [args.input_text]
return self.sentiment_classify(texts)
...
步骤 3_6. 支持serving调用
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
如果希望Module可以支持PaddleHub Serving部署预测服务,则需要提供一个经过serving修饰的接口,接口负责解析传入数据并进行预测,将结果返回。
如果不需要提供PaddleHub Serving部署预测服务,则可以不需要加上serving修饰。
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
@serving
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
完整代码
------------------------------------------------
* `module.py <https://github.com/PaddlePaddle/PaddleHub/blob/release/v2.1/modules/demo/senta_test/module.py>`_
* `processor.py <https://github.com/PaddlePaddle/PaddleHub/blob/release/v2.1/modules/demo/senta_test/processor.py>`_
三、安装并测试Module
===================================
完成Module编写后,我们可以通过以下方式测试该Module
调用方法1
------------------------------------------------
将Module安装到本机中,再通过Hub.Module(name=...)加载
.. code-block:: console
$ hub install senta_test
.. code-block:: python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
调用方法2
------------------------------------------------
直接通过Hub.Module(directory=...)加载
.. code-block:: python
import paddlehub as hub
senta_test = hub.Module(directory="senta_test/")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
调用方法3
------------------------------------------------
将Module安装到本机中,再通过hub run运行
.. code-block:: console
$ hub install senta_test
$ hub run senta_test --input_text "这部电影太差劲了"
\ No newline at end of file
## **迁移学习**
### **概述**
迁移学习 (Transfer Learning) 是属于深度学习的一个子研究领域,该研究领域的目标在于利用数据、任务、或模型之间的相似性,将在旧领域学习过的知识,迁移应用于新领域中。通俗的来讲,迁移学习就是运用已有的知识来学习新的知识,例如学会了骑自行车的人也能较快的学会骑电动车。较为常用的一种迁移学习方式是利用预训练模型进行微调,即用户基于当前任务的场景从PaddleHub中选择已训练成功的模型进行新任务训练,且该模型曾经使用的数据集与新场景的数据集情况相近,此时仅需要在当前任务场景的训练过程中使用新场景的数据对模型参数进行微调(**Fine-tune**),即可完成训练任务。迁移学习吸引了很多研究者投身其中,因为它能够很好的解决深度学习中的以下几个问题:
* 一些研究领域只有少量标注数据,且数据标注成本较高,不足以训练一个足够鲁棒的神经网络。
* 大规模神经网络的训练依赖于大量的计算资源,这对于一般用户而言难以实现。
* 应对于普适化需求的模型,在特定应用上表现不尽如人意。
为了让开发者更便捷地应用迁移学习,飞桨开源了预训练模型管理工具 PaddleHub。开发者仅仅使用十余行的代码,就能完成迁移学习。本文将为读者全面介绍使用PaddleHub完成迁移学习的方法。
### **前置条件**
在开始迁移学习之前,用户需要做好如下工作:
* 用户已安装PaddleHub。
* 准备好用于迁移学习的数据,用户可以选择使用PaddleHub提供的数据集或者自定义数据集,如果是自定义数据,需要参考“自定义数据集如何Fine-tune”对数据集处理。
* 使用hub install命令安装或更新用于训练的module,以使用ERNIE模型为例,命令格式如下所示。用户可能在之前的任务中已经安装过相关的预训练模型,但是仍然推荐用户在开始训练前执行此步骤,这样可以保证预训练模型是最新版本。
```python
$ hub install ernie==1.2.0
```
### **迁移学习流程**
用户完成迁移学习前需要先编写好用于迁移学习的脚本。用户编写脚本的过程非常简单,仅需要十余行代码即可完成。整个脚本的编写过程,可以分为如下几个步骤:
1. 导入必要的包。
2. 加载预训练模型(Module),即加载PaddleHub提供的预训练模型。
3. 加载数据集(Dataset),用户可以选择使用dataset API加载PaddleHub自带的数据集或者自行编写加载数据集的类来加载自定义数据集。
4. 配置数据读取器(Reader),负责将dataset的数据进行预处理,以特定格式组织并输入给模型进行训练。
5. 选择优化策略(Strategy),优化策略包含了多种预训练参数,例如使用什么学习率变化策略,使用哪种类型的优化器,使用什么类型的正则化等。
6. 设置运行配置(RunConfig),RunConfig包含了一些训练相关的配置,包括是否使用GPU、训练的轮数(Epoch)、训练批次大小(batch_size)等。
7. 组建训练任务(Task),一个迁移学习训练任务中会包含与该任务相关的Program和上面设置好的数据读取器Reader、运行配置等内容。
8. 启动Fine-tune,使用Finetune_and_eval函数完成训练和评估。
### **学会编写迁移学习训练脚本**
PaddleHub提供了Finetune API和预训练模型完成多种不同任务场景的迁移学习,包括图像分类、文本分类、多标签分类、序列标注、检索式问答任务、回归任务、句子语义相似度计算、阅读理解任务等。本文将以文本分类为例介绍迁移学习脚本的编写方法。
#### **1. 导入必要的包。**
```python
import paddlehub as hub
```
#### **2. 加载预训练模型**
使用如下代码加载预训练模型,本例使用ERNIE预训练模型来完成文本分类任务。ERNIE(Enhanced Representation through kNowledge IntEgration)是百度提出的语义表示模型,以Transformer Encoder为网络基本组件,其预训练过程利用了更丰富的语义知识和更多的语义任务,用户可以使用该预训练模型随时逐步引入不同的自定义任务,例如命名实体预测、语篇关系识别、句子顺序预测任务、情感分析等。
```python
module = hub.Module(name="ernie")
```
PaddleHub还提供很多了其它可用于迁移学习的预训练模型, 在PaddleHub的官网上,图像分类、语义模型和情感分析几个目录下的预训练模型都支持迁移学习,用户仅需要将name的取值换成预训练模型名称即可,例如右侧红框中的示例。
![](../../imgs/Howtofinetune1.png)
#### **3. 加载数据集**
在加载好预训练模型后,我们来加载数据集。用于迁移学习的数据集来源可以分为两种,用户自定义数据集和PaddleHub提供的数据集,使用不同类型的数据集加载方式也有所不同。
##### **加载PaddleHub自带数据集**
如果用户使用的是PaddleHub自带数据集,则可以通过PaddleHub的数据集API编写一行代码完成加载数据集的动作。
```python
dataset = hub.dataset.ChnSentiCorp()
```
其中ChnSentiCorp是中文情感分析数据集,其目标是判断一段文本的情感态度。例如文本是“这道菜很好吃”,则对应的标签为“1”,表示正向评价,又例如“房间太小了”,对应标签为“0”,表示负面评价。PaddleHub还提供了其他的文本分类数据集,用户可以自行选择数据集对应的API替换上面代码中dataset的取值,具体信息如下表所示。
|数据集|名称|API|
|:--------:|:--------:|:--------:|
|ChnSentiCorp|中文情感分析数据集|hub.dataset.ChnSentiCorp()|
|LCQMC|哈尔滨工业大学在自然语言处理国际顶会 COLING2018 构建的问答匹配中文数据集,其目标是判断两个问题的语义是否相同。|hub.dataset.LCQMC()|
|NLPCC-DPQA|国际自然语言处理和中文计算会议NLPCC于2016年举办的评测任务数据集,,其目标是选择能够回答问题的答案。|hub.dataset.NLPCC_DPQA()|
|MSRA-NER|微软亚研院发布的数据集,其目标是命名实体识别|hub.dataset.MSRA-NER()|
|Toxic|英文多标签分类数据集|hub.dataset.Toxic()|
|SQUAD|英文阅读理解数据集|hub.dataset.SQUAD()|
|GLUE-CoLA|文本分类任务数据集|hub.dataset.GLUE("CoLA")|
|GLUE-SST-2|情感分析任务数据集|hub.dataset.GLUE("SST-2")|
|GLUE-MNLI|文本推理任务数据集|hub.dataset.GLUE("MNLI_m")|
|GLUE-QQP|句子对分类任务数据集|hub.dataset.GLUE("QQP")|
|GLUE-QNLI|问题推理任务数据集|hub.dataset.GLUE("QNLI")|
|GLUE-STS-B|回归任务数据集|hub.dataset.GLUE("STS-B")|
|GLUE-MRPC|句子对分类任务数据集|hub.dataset.GLUE("MRPC")|
|GLUE-RTE|文本蕴含任务数据集|hub.dataset.GLUE("RTE")|
|XNLI|跨语言自然语言推理数据集|hub.dataset.XNLI(language=zh)|
|ChineseGLUE-TNEWS|今日头条中文新闻(短文本)分类数据集|hub.dataset.TNews()|
|ChineseGLUE-INEWS|互联网情感分析任务数据集|hub.dataset.INews()|
|DRCD|台达阅读理解数据集,属于通用领域繁体中文机器阅读理解数据集|hub.dataset.DRCD()|
|CMRC2018|中文机器阅读理解的跨度提取数据集|hub.dataset.CMRC2018()|
|ChinesGLUE-BQ|智能客服中文问句匹配数据集|hub.dataset.BQ()|
|ChineseGLUE-IFLYTEK|中文长文本分类数据集,该数据集共有1.7万多条关于app应用描述的长文本标注数据|hub.dataset.IFLYTEK()|
|ChineseGLUE-THUCNEWS|中文长文本分类数据集,该数据集共有4万多条中文新闻长文本标注数据,共14个类别|hub.dataset.THUCNEWS()|
|DogCatDataset|由Kaggle提供的数据集,用于图像二分类|hub.dataset.DogCatDataset()|
|Food101|由Kaggle提供的食品图片数据集,含有101种类别|hub.dataset.Food101()|
|Indoor67|由麻省理工学院发布的数据集,其包含67种室内场景,其目标是识别一张室内图片的场景类别。|hub.dataset.Indoor67()|
|Flowers|花卉数据集,数据集有5种类型,包括"roses","tulips","daisy","sunflowers","dandelion"|hub.dataset.Flowers()|
|StanfordDogs|斯坦福大学发布的数据集,其包含120个种类的狗,用于做图像分类。|hub.dataset.StanfordDogs()|
##### **加载自定义数据集**
* 加载文本类自定义数据集。用户仅需要继承基类BaseNLPDatast,修改数据集存放地址以及类别即可,具体可以参考如下代码。
```python
from paddlehub.dataset.base_nlp_dataset import BaseNLPDataset
# 构建数据集的类
class DemoDataset(BaseNLPDataset):
def __init__(self):
# 数据集实际路径
self.dataset_dir = "path/to/dataset"
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_file="train.tsv", # 训练集存放地址
dev_file="dev.tsv", # 验证集存放地址
test_file="test.tsv", # 测试集存放地址
# 如果还有预测数据(不需要文本类别label),可以放在predict.tsv
predict_file="predict.tsv",
train_file_with_header=True, # 训练集文件是否有列说明
dev_file_with_header=True, # 验证集文件是否有列说明
test_file_with_header=True, # 测试集文件是否有列说明
predict_file_with_header=True, # 预测集文件是否有列说明
# 数据集类别集合
label_list=["0", "1"])
# 通过创建Dataset对象加载自定义文本数据集
dataset = DemoDataset()
```
然后就可以通过DemoDataset()获取自定义数据集了。进而配合数据预处理器以及预训练模型如ERNIE完成文本类任务。
* 加载图像类自定义数据集。用用户仅需要继承基类BaseCVDatast,修改数据集存放地址即可,具体可以参考如下代码。
```python
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
class DemoDataset(BaseCVDataset):
def __init__(self):
# 数据集存放位置
self.dataset_dir = "/test/data"
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_list_file="train_list.txt", # 训练集存放地址
validate_list_file="validate_list.txt", # 验证集存放地址
test_list_file="test_list.txt", # 测试集存放地址
predict_file="predict_list.txt", # 预测集存放地址
label_list_file="label_list.txt", # 数据集类别文件所在地址
# 如果您的数据集类别较少,可以不用定义label_list.txt,可以在最后设置label_list=["数据集所有类别"]。
)
# 通过创建Dataset对象加载图像类数据集
dataset = DemoDataset()
```
然后就可以通过DemoDataset()获取自定义数据集了。进而配合数据预处理器以及预训练模型完成视觉类的迁移学习任务。
#### **4. 配置数据预处理器**
通过使用PaddleHub的数据预处理器API来读取NLP或CV的数据集数据。
```python
reader = hub.reader.ClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path(), # 返回预训练模型对应的词表
max_seq_len=128, # 需要与Step1中context接口传入的序列长度保持一致
sp_model_path=module.get_spm_path(), # 若module为ernie_tiny则返回对应的子词切分模型,否则返回None
word_dict_path=module.get_word_dict_path()) # 若module为ernie_tiny则返回对应的词语切分模型,否则返回None
```
对于不同的任务类型,用户可以选择不同的Reader。
|数据读取器|描述|任务类型|API示例|
|:--------:|:--------:|:--------:|:--------|
|ClassifyReader|适用于Transformer预训练模型(ERNIE/BERT)的数据预处理器。|NLP|reader = hub.reader.ClassifyReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=128,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sp_model_path=module.get_spm_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word_dict_path=module.get_word_dict_path()) |
|LACClassifyReader|以LAC模块为切词器的数据预处理器,适用于Senta、ELMo等需要以词粒度分词的任务。|NLP|reader = hub.reader.LACClassifyReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path())|
|SequenceLabelReader|适用于Transformer类模型(ERNIE/BERT)的序列标注预处理器。|NLP|reader = hub.reader.SequenceLabelReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=128,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sp_model_path=module.get_spm_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word_dict_path=module.get_word_dict_path())|
|MultiLabelClassifyReader|适用于Transformer类模型(ERNIE/BERT)的多标签分类预处理器。|NLP|reader = hub.reader.MultiLabelClassifyReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=128)|
|ReadingComprehensionReader|适用于Transformer类模型(ERNIE/BERT)的阅读理解任务预处理器。|NLP|reader = hub.reader.ReadingComprehensionReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_length=384)|
|RegressionReader|适用于回归任务的数据预处理器。|NLP|reader = hub.reader.RegressionReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=args.max_seq_len)|
|ImageClassificationReader|适用于图像分类数据的预处理器。会修改输入图像的尺寸、进行标准化处理、图像增广处理等操作。|CV|reader = hub.reader.ImageClassificationReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image_width=module.get_expected_image_width(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image_height=module.get_expected_image_height(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;images_mean=module.get_pretrained_images_mean(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;images_std=module.get_pretrained_images_std(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset)|
#### **5. 选择优化策略**
在PaddleHub中,Strategy类封装了一系列适用于迁移学习的Fine-tuning策略。Strategy包含了对预训练参数使用什么学习率变化策略,使用哪种类型的优化器,使用什么类型的正则化等。在我们要做的文本分类任务中,我们使用AdamWeightDecayStrategy优化策略。具体可以参考如下代码:
```python
strategy = hub.AdamWeightDecayStrategy(
learning_rate=5e-5, # Fine-tune过程中的最大学习率
weight_decay=0.01, # 模型的正则项参数,默认0.01,如果模型有过拟合倾向,可适当调高这一参数
warmup_proportion=0.1, #如果warmup_proportion>0, 例如0.1, 则学习率会在前10%的steps中线性增长至最高值learning_rate
# 有两种策略可选:
# (1)linear_decay策略学习率会在最高点后以线性方式衰减;
# (2)noam_decay策略学习率会在最高点以多项式形式衰减;
lr_scheduler="linear_decay",
)
```
包括AdamWeightDecayStrategy在内,PaddleHub还提供了多种优化策略的API。
|优化策略|描述|API示例|
|:--------:|:--------|:--------|
|DefaultFinetuneStrategy|默认的优化策略。其对应参数如下:<br>* learning_rate: 全局学习率。默认为1e-4。<br>* optimizer_name: 优化器名称。默认adam。<br>* regularization_coeff: 正则化的λ参数。默认为1e-3。<br>在图像分类任务中推荐使用此优化策略。|strategy = hub.DefaultFinetuneStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regularization_coeff=1e-3)|
|AdamWeightDecayStrategy|基于Adam优化器的学习率衰减策略。其对应参数如下:<br>* learning_rate: 全局学习率,默认为1e-4。<br>* lr_scheduler: 学习率调度方法,默认为"linear_decay"。<br>* warmup_proportion: warmup所占比重。<br>* weight_decay: 学习率衰减率。<br>* optimizer_name: 优化器名称,默认为adam。<br>在文本分类、阅读理解等任务中推荐使用此优化策略。|strategy = hub.AdamWeightDecayStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lr_scheduler="linear_decay",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;warmup_proportion=0.0,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weight_decay=0.01,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam")|
|L2SPFinetuneStrategy|使用L2SP正则作为惩罚因子的Finetune策略。其对应参数如下:<br>* learning_rate: 全局学习率。默认为1e-4。<br>* optimizer_name: 优化器名称。默认adam。<br>* regularization_coeff: 正则化的λ参数。默认为1e-3。|strategy = hub.L2SPFinetuneStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regularization_coeff=1e-3)|
|ULMFiTStrategy|该策略实现了ULMFiT论文中提出的三种策略:<br> * Slanted triangular learning rates是一种学习率先上升再下降的策略。<br>* Discriminative fine-tuning是一种学习率逐层递减的策略,通过该策略可以减缓底层的更新速度。<br>* Gradual unfreezing是一种逐层解冻的策略,通过该策略可以优先更新上层,再慢慢解冻下层参与更新。<br>其对应参数如下:<br> * learning_rate: 全局学习率。默认为1e-4。<br> * optimizer_name: 优化器名称。默认为adam。<br> * cut_fraction: 设置Slanted triangular learning rates学习率上升的步数在整个训练总步数中的比例。默认为0.1,如果设置为0,则不采用Slanted triangular learning rates。<br> * ratio: 设置Slanted triangular learning rates下降的最小学习率与上升的最大学习率的比例关系,默认为32,表示最小学习率是最大学习率的1/32。<br> * dis_blocks: 设置 Discriminative fine-tuning中的块数。默认为3,如果设置为0,则不采用Discriminative fine-tuning。<br> * factor: 设置Discriminative fine-tuning的衰减率。默认为2.6,表示下一层的学习率是上一层的1/2.6。<br> * frz_blocks: 设置Gradual unfreezing中的块数。块的概念同“dis_blocks”中介绍的概念。|strategy = hub.ULMFiTStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cut_fraction=0.1,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ratio=32,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dis_blocks=3,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;factor=2.6,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frz_blocks=3)|
#### **6. 设置运行配置。**
在PaddleHub中,用户可以使用Finetune API中的RunConfig配置Task进行Finetune时使用的参数,包括运行的Epoch次数、batch的大小、是否使用GPU训练等。代码示例如下所示。
```python
config = hub.RunConfig(use_cuda=True, num_epoch=3, batch_size=32, strategy=strategy)
```
#### **7. 组建训练任务。**
有了合适的预训练模型,并加载好要迁移的数据集后,我们开始组建一个Task。在PaddleHub中,Task代表了一个Fine-tune的任务。任务中包含了执行该任务相关的Program、数据读取器Reader、运行配置等内容。在这里可以找到文本分类任务对应的Task说明[TextClassifierTask](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/reference/task/text_classify_task.md)。具体实现方案如下:
1. 获取module(PaddleHub的预训练模型)的上下文环境,包括输入和输出的变量,以及Paddle Program(可执行的模型格式)。
2. 从预训练模型的输出变量中找到特征图提取层feature_map,在feature_map后面接入一个全连接层,如下代码中通过hub.TextClassifierTask的pooled_output参数指定。
3. 网络的输入层保持不变,依然从输入层开始,如下代码中通过hub.TextClassifierTask的参数feed_list变量指定。
hub.TextClassifierTask就是通过这两个参数明确我们的截取模型网络的要求,按照这样的配置,我们截取的网络是从输入层一直到特征提取的最后一层“pooled_output”,表示我将使用截出的网络来进行迁移学习训练。
```python
# 获取Module的上下文信息,得到输入、输出以及预训练的Paddle Program副本。
# trainable设置为True时,Module中的参数在Fine-tune时也会随之训练,否则保持不变。
# 其中最大序列长度max_seq_len为可调整的参数,建议值为128,根据任务文本长度不同可以进行修改,但最大不超过512。
# 若序列长度不足,会通过padding方式补到max_seq_len, 若序列长度大于该值,则会以截断方式让序列长度为max_seq_len。
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
# 返回ERNIE/BERT模型对应的[CLS]向量,可以用于句子或句对的特征表达。
pooled_output = outputs["pooled_output"]
# feed_list的Tensor顺序不可以调整
# 指定ERNIE中的输入tensor的顺序,与ClassifyReader返回的结果一致
feed_list = [
inputs["input_ids"].name,
inputs["position_ids"].name,
inputs["segment_ids"].name,
inputs["input_mask"].name,
]
# 通过输入特征,label与迁移的类别数,可以生成适用于文本分类的迁移任务
cls_task = hub.TextClassifierTask(
data_reader=reader, # 读取数据的reader
feature=pooled_output, # 从预训练提取的特征矩阵
feed_list=feed_list, # 待feed变量的名字列表
num_classes=dataset.num_labels, # 数据集的类别数量
metrics_choices = ["acc"],
config=config) # 运行配置
```
PaddleHub预置了常见任务的Task,每种Task都有特定的应用场景并提供了对应的度量指标,满足用户的不同需求。
|Task类型|描述|任务类型|
|:--------:|:--------:|:--------:|
|ImageClassifierTask|该Task基于输入的特征,添加一个或多个全连接层来创建一个分类任务用于Fine-tune,度量指标为准确率,损失函数为交叉熵Loss。|图像分类任务|
|TextClassifierTask|该Task基于输入的特征,添加一个Dropout层,以及一个或多个全连接层来创建一个文本分类任务用于finetune,度量指标为准确率,损失函数为交叉熵Loss。|文本分类任务|
|SequenceLabelTask|该Task基于输入的特征,添加一个全连接层或者一个全连接层和CRF层来创建一个序列标注任务用于Fine-tune,度量指标为F1,损失函数为交叉熵Loss。|序列标注任务|
|MultiLabelClassifierTask|该Task基于输入的特征,添加一个或多个全连接层来创建一个多标签分类任务用于finetune,度量指标为多个标签的平均AUC,损失函数为多个标签的平均交叉熵。|多标签分类任务|
|RegressionTask|该Task基于输入的特征,添加一个Dropout层,以及一个或多个全连接层来创建一个文本回归任务用于finetune,度量指标为准确率,损失函数为均方差损失函数。|文本回归任务|
|ReadingComprehensionTask|该Task基于输入的特征,添加一个全连接层来创建一个阅读理解任务用于Fine-tune,损失函数为交叉熵Loss。|阅读理解任务|
在设定每个Task前,用户需要提前了解待迁移学习的预训练模型的输入与输出,即对应上面代码中的“feed_list”和“pooled_output”。具体的输入输出代码可以分为如下几类:
* 图像分类模型
```
input_dict, output_dict, program = module.context(trainable=True)
feature_map = output_dict["feature_map"]
feed_list = [input_dict["image"].name]
```
* 自然语言处理模型(不包括word2vec_skipgram、simnet_bow、文本匹配和文本生成几个模型)
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
pooled_output = outputs["pooled_output"]
feed_list = [
inputs["input_ids"].name,
inputs["position_ids"].name,
inputs["segment_ids"].name,
inputs["input_mask"].name,
]
```
* word2vec_skipgram模型
```
inputs, outputs, program = module.context(trainable=True)
word_ids = inputs["word_ids"]
embedding = outputs["word_embs"]
```
* simnet_bow模型
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=2)
query = outputs["emb"]
title = outputs['emb_2']
```
* Pairwise文本匹配模型
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=3)
query = outputs["emb"]
left = outputs['emb_2']
right = outputs['emb_3']
```
* Pointwise文本匹配
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=2)
query = outputs["emb"]
title = outputs['emb_2']
```
* 文本生成模型
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
pooled_output = outputs["pooled_output"]
sequence_output = outputs["sequence_output"]
```
#### **8 启动Fine-tune,使用Finetune_and_eval函数完成训练和评估。**
```python
cls_task.finetune_and_eval()
```
显示信息如下例所示。可以看到训练的评估结果,Loss值和准确率等等。
```
[2020-07-28 21:28:21,658] [ TRAIN] - step 810 / 900: loss=0.05022 acc=0.97813 [step/sec: 4.07]
[2020-07-28 21:28:24,115] [ TRAIN] - step 820 / 900: loss=0.04719 acc=0.98125 [step/sec: 4.07]
[2020-07-28 21:28:26,574] [ TRAIN] - step 830 / 900: loss=0.06895 acc=0.98125 [step/sec: 4.07]
[2020-07-28 21:28:29,035] [ TRAIN] - step 840 / 900: loss=0.07830 acc=0.97813 [step/sec: 4.07]
[2020-07-28 21:28:31,490] [ TRAIN] - step 850 / 900: loss=0.07279 acc=0.97500 [step/sec: 4.08]
[2020-07-28 21:28:33,939] [ TRAIN] - step 860 / 900: loss=0.03220 acc=0.99375 [step/sec: 4.09]
[2020-07-28 21:28:36,388] [ TRAIN] - step 870 / 900: loss=0.05016 acc=0.98750 [step/sec: 4.09]
[2020-07-28 21:28:38,840] [ TRAIN] - step 880 / 900: loss=0.05604 acc=0.98750 [step/sec: 4.08]
[2020-07-28 21:28:41,293] [ TRAIN] - step 890 / 900: loss=0.05622 acc=0.98125 [step/sec: 4.08]
[2020-07-28 21:28:43,748] [ TRAIN] - step 900 / 900: loss=0.06642 acc=0.97813 [step/sec: 4.08]
[2020-07-28 21:28:43,750] [ INFO] - Evaluation on dev dataset start
[2020-07-28 21:28:46,654] [ EVAL] - [dev dataset evaluation result] loss=0.17890 acc=0.94079 [step/sec: 13.23]
[2020-07-28 21:28:46,657] [ INFO] - Evaluation on dev dataset start
[2020-07-28 21:28:49,527] [ EVAL] - [dev dataset evaluation result] loss=0.17890 acc=0.94079 [step/sec: 13.39]
[2020-07-28 21:28:49,529] [ INFO] - Load the best model from ckpt_20200728212416/best_model
[2020-07-28 21:28:50,112] [ INFO] - Evaluation on test dataset start
[2020-07-28 21:28:52,987] [ EVAL] - [test dataset evaluation result] loss=0.14264 acc=0.94819 [step/sec: 13.36]
[2020-07-28 21:28:52,988] [ INFO] - Saving model checkpoint to ckpt_20200728212416/step_900
[2020-07-28 21:28:55,789] [ INFO] - PaddleHub finetune finished.
```
通过Fine-tune完成模型训练后,在对应的ckpt目录(CKPT_DIR)下,PaddleHub会自动保存验证集上效果最好的模型。用户可以参考如下代码进行预测,其中推理出的标签值0表示负向评价,1表示正向评价。。
```python
import numpy as np
# 待预测数据
data = [
["这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般"],
["交通方便;环境很好;服务态度很好 房间较小"],
["19天硬盘就罢工了,算上运来的一周都没用上15天,可就是不能换了。唉,你说这算什么事呀!"]
]
index = 0
run_states = cls_task.predict(data=data)
results = [run_state.run_results for run_state in run_states]
for batch_result in results:
# 预测类别取最大分类概率值
batch_result = np.argmax(batch_result[0], axis=1)
for result in batch_result:
print("%s\tpredict=%s" % (data[index][0], result))
index += 1
```
预测结果如下所示。
```
[2020-07-28 18:06:45,441] [ INFO] - PaddleHub predict start
[2020-07-28 18:06:45,442] [ INFO] - The best model has been loaded
[2020-07-28 18:06:48,406] [ INFO] - PaddleHub predict finished.
这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般 predict=0
交通方便;环境很好;服务态度很好 房间较小 predict=1
19天硬盘就罢工了,算上运来的一周都没用上15天,可就是不能换了。唉,你说这算什么事呀! predict=0
```
## **迁移学习**
### **概述**
迁移学习 (Transfer Learning) 是属于深度学习的一个子研究领域,该研究领域的目标在于利用数据、任务、或模型之间的相似性,将在旧领域学习过的知识,迁移应用于新领域中。通俗的来讲,迁移学习就是运用已有的知识来学习新的知识,例如学会了骑自行车的人也能较快的学会骑电动车。较为常用的一种迁移学习方式是利用预训练模型进行微调,即用户基于当前任务的场景从PaddleHub中选择已训练成功的模型进行新任务训练,且该模型曾经使用的数据集与新场景的数据集情况相近,此时仅需要在当前任务场景的训练过程中使用新场景的数据对模型参数进行微调(**Fine-tune**),即可完成训练任务。迁移学习吸引了很多研究者投身其中,因为它能够很好的解决深度学习中的以下几个问题:
* 一些研究领域只有少量标注数据,且数据标注成本较高,不足以训练一个足够鲁棒的神经网络。
* 大规模神经网络的训练依赖于大量的计算资源,这对于一般用户而言难以实现。
* 应对于普适化需求的模型,在特定应用上表现不尽如人意。
为了让开发者更便捷地应用迁移学习,飞桨开源了预训练模型管理工具 PaddleHub。开发者仅仅使用十余行的代码,就能完成迁移学习。本文将为读者全面介绍使用PaddleHub完成迁移学习的方法。
### **前置条件**
在开始迁移学习之前,用户需要做好如下工作:
* 用户已安装PaddleHub。
* 准备好用于迁移学习的数据,用户可以选择使用PaddleHub提供的数据集或者自定义数据集,如果是自定义数据,需要参考“自定义数据集如何Fine-tune”对数据集处理。
* 使用hub install命令安装或更新用于训练的module,以使用ERNIE模型为例,命令格式如下所示。用户可能在之前的任务中已经安装过相关的预训练模型,但是仍然推荐用户在开始训练前执行此步骤,这样可以保证预训练模型是最新版本。
```python
$ hub install ernie==1.2.0
```
### **迁移学习流程**
用户完成迁移学习前需要先编写好用于迁移学习的脚本。用户编写脚本的过程非常简单,仅需要十余行代码即可完成。整个脚本的编写过程,可以分为如下几个步骤:
1. 导入必要的包。
2. 加载预训练模型(Module),即加载PaddleHub提供的预训练模型。
3. 加载数据集(Dataset),用户可以选择使用dataset API加载PaddleHub自带的数据集或者自行编写加载数据集的类来加载自定义数据集。
4. 配置数据读取器(Reader),负责将dataset的数据进行预处理,以特定格式组织并输入给模型进行训练。
5. 选择优化策略(Strategy),优化策略包含了多种预训练参数,例如使用什么学习率变化策略,使用哪种类型的优化器,使用什么类型的正则化等。
6. 设置运行配置(RunConfig),RunConfig包含了一些训练相关的配置,包括是否使用GPU、训练的轮数(Epoch)、训练批次大小(batch_size)等。
7. 组建训练任务(Task),一个迁移学习训练任务中会包含与该任务相关的Program和上面设置好的数据读取器Reader、运行配置等内容。
8. 启动Fine-tune,使用Finetune_and_eval函数完成训练和评估。
### **学会编写迁移学习训练脚本**
PaddleHub提供了Finetune API和预训练模型完成多种不同任务场景的迁移学习,包括图像分类、文本分类、多标签分类、序列标注、检索式问答任务、回归任务、句子语义相似度计算、阅读理解任务等。本文将以文本分类为例介绍迁移学习脚本的编写方法。
#### **1. 导入必要的包。**
```python
import paddlehub as hub
```
#### **2. 加载预训练模型**
使用如下代码加载预训练模型,本例使用ERNIE预训练模型来完成文本分类任务。ERNIE(Enhanced Representation through kNowledge IntEgration)是百度提出的语义表示模型,以Transformer Encoder为网络基本组件,其预训练过程利用了更丰富的语义知识和更多的语义任务,用户可以使用该预训练模型随时逐步引入不同的自定义任务,例如命名实体预测、语篇关系识别、句子顺序预测任务、情感分析等。
```python
module = hub.Module(name="ernie")
```
PaddleHub还提供很多了其它可用于迁移学习的预训练模型, 在PaddleHub的官网上,图像分类、语义模型和情感分析几个目录下的预训练模型都支持迁移学习,用户仅需要将name的取值换成预训练模型名称即可,例如右侧红框中的示例。
![](../../imgs/Howtofinetune1.png)
#### **3. 加载数据集**
在加载好预训练模型后,我们来加载数据集。用于迁移学习的数据集来源可以分为两种,用户自定义数据集和PaddleHub提供的数据集,使用不同类型的数据集加载方式也有所不同。
##### **加载PaddleHub自带数据集**
如果用户使用的是PaddleHub自带数据集,则可以通过PaddleHub的数据集API编写一行代码完成加载数据集的动作。
```python
dataset = hub.dataset.ChnSentiCorp()
```
其中ChnSentiCorp是中文情感分析数据集,其目标是判断一段文本的情感态度。例如文本是“这道菜很好吃”,则对应的标签为“1”,表示正向评价,又例如“房间太小了”,对应标签为“0”,表示负面评价。PaddleHub还提供了其他的文本分类数据集,用户可以自行选择数据集对应的API替换上面代码中dataset的取值,具体信息如下表所示。
|数据集|名称|API|
|:--------:|:--------:|:--------:|
|ChnSentiCorp|中文情感分析数据集|hub.dataset.ChnSentiCorp()|
|LCQMC|哈尔滨工业大学在自然语言处理国际顶会 COLING2018 构建的问答匹配中文数据集,其目标是判断两个问题的语义是否相同。|hub.dataset.LCQMC()|
|NLPCC-DPQA|国际自然语言处理和中文计算会议NLPCC于2016年举办的评测任务数据集,,其目标是选择能够回答问题的答案。|hub.dataset.NLPCC_DPQA()|
|MSRA-NER|微软亚研院发布的数据集,其目标是命名实体识别|hub.dataset.MSRA-NER()|
|Toxic|英文多标签分类数据集|hub.dataset.Toxic()|
|SQUAD|英文阅读理解数据集|hub.dataset.SQUAD()|
|GLUE-CoLA|文本分类任务数据集|hub.dataset.GLUE("CoLA")|
|GLUE-SST-2|情感分析任务数据集|hub.dataset.GLUE("SST-2")|
|GLUE-MNLI|文本推理任务数据集|hub.dataset.GLUE("MNLI_m")|
|GLUE-QQP|句子对分类任务数据集|hub.dataset.GLUE("QQP")|
|GLUE-QNLI|问题推理任务数据集|hub.dataset.GLUE("QNLI")|
|GLUE-STS-B|回归任务数据集|hub.dataset.GLUE("STS-B")|
|GLUE-MRPC|句子对分类任务数据集|hub.dataset.GLUE("MRPC")|
|GLUE-RTE|文本蕴含任务数据集|hub.dataset.GLUE("RTE")|
|XNLI|跨语言自然语言推理数据集|hub.dataset.XNLI(language=zh)|
|ChineseGLUE-TNEWS|今日头条中文新闻(短文本)分类数据集|hub.dataset.TNews()|
|ChineseGLUE-INEWS|互联网情感分析任务数据集|hub.dataset.INews()|
|DRCD|台达阅读理解数据集,属于通用领域繁体中文机器阅读理解数据集|hub.dataset.DRCD()|
|CMRC2018|中文机器阅读理解的跨度提取数据集|hub.dataset.CMRC2018()|
|ChinesGLUE-BQ|智能客服中文问句匹配数据集|hub.dataset.BQ()|
|ChineseGLUE-IFLYTEK|中文长文本分类数据集,该数据集共有1.7万多条关于app应用描述的长文本标注数据|hub.dataset.IFLYTEK()|
|ChineseGLUE-THUCNEWS|中文长文本分类数据集,该数据集共有4万多条中文新闻长文本标注数据,共14个类别|hub.dataset.THUCNEWS()|
|DogCatDataset|由Kaggle提供的数据集,用于图像二分类|hub.dataset.DogCatDataset()|
|Food101|由Kaggle提供的食品图片数据集,含有101种类别|hub.dataset.Food101()|
|Indoor67|由麻省理工学院发布的数据集,其包含67种室内场景,其目标是识别一张室内图片的场景类别。|hub.dataset.Indoor67()|
|Flowers|花卉数据集,数据集有5种类型,包括"roses","tulips","daisy","sunflowers","dandelion"|hub.dataset.Flowers()|
|StanfordDogs|斯坦福大学发布的数据集,其包含120个种类的狗,用于做图像分类。|hub.dataset.StanfordDogs()|
##### **加载自定义数据集**
* 加载文本类自定义数据集。用户仅需要继承基类BaseNLPDatast,修改数据集存放地址以及类别即可,具体可以参考如下代码。
```python
from paddlehub.dataset.base_nlp_dataset import BaseNLPDataset
# 构建数据集的类
class DemoDataset(BaseNLPDataset):
def __init__(self):
# 数据集实际路径
self.dataset_dir = "path/to/dataset"
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_file="train.tsv", # 训练集存放地址
dev_file="dev.tsv", # 验证集存放地址
test_file="test.tsv", # 测试集存放地址
# 如果还有预测数据(不需要文本类别label),可以放在predict.tsv
predict_file="predict.tsv",
train_file_with_header=True, # 训练集文件是否有列说明
dev_file_with_header=True, # 验证集文件是否有列说明
test_file_with_header=True, # 测试集文件是否有列说明
predict_file_with_header=True, # 预测集文件是否有列说明
# 数据集类别集合
label_list=["0", "1"])
# 通过创建Dataset对象加载自定义文本数据集
dataset = DemoDataset()
```
然后就可以通过DemoDataset()获取自定义数据集了。进而配合数据预处理器以及预训练模型如ERNIE完成文本类任务。
* 加载图像类自定义数据集。用用户仅需要继承基类BaseCVDatast,修改数据集存放地址即可,具体可以参考如下代码。
```python
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
class DemoDataset(BaseCVDataset):
def __init__(self):
# 数据集存放位置
self.dataset_dir = "/test/data"
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_list_file="train_list.txt", # 训练集存放地址
validate_list_file="validate_list.txt", # 验证集存放地址
test_list_file="test_list.txt", # 测试集存放地址
predict_file="predict_list.txt", # 预测集存放地址
label_list_file="label_list.txt", # 数据集类别文件所在地址
# 如果您的数据集类别较少,可以不用定义label_list.txt,可以在最后设置label_list=["数据集所有类别"]。
)
# 通过创建Dataset对象加载图像类数据集
dataset = DemoDataset()
```
然后就可以通过DemoDataset()获取自定义数据集了。进而配合数据预处理器以及预训练模型完成视觉类的迁移学习任务。
#### **4. 配置数据预处理器**
通过使用PaddleHub的数据预处理器API来读取NLP或CV的数据集数据。
```python
reader = hub.reader.ClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path(), # 返回预训练模型对应的词表
max_seq_len=128, # 需要与Step1中context接口传入的序列长度保持一致
sp_model_path=module.get_spm_path(), # 若module为ernie_tiny则返回对应的子词切分模型,否则返回None
word_dict_path=module.get_word_dict_path()) # 若module为ernie_tiny则返回对应的词语切分模型,否则返回None
```
对于不同的任务类型,用户可以选择不同的Reader。
|数据读取器|描述|任务类型|API示例|
|:--------:|:--------:|:--------:|:--------|
|ClassifyReader|适用于Transformer预训练模型(ERNIE/BERT)的数据预处理器。|NLP|reader = hub.reader.ClassifyReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=128,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sp_model_path=module.get_spm_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word_dict_path=module.get_word_dict_path()) |
|LACClassifyReader|以LAC模块为切词器的数据预处理器,适用于Senta、ELMo等需要以词粒度分词的任务。|NLP|reader = hub.reader.LACClassifyReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path())|
|SequenceLabelReader|适用于Transformer类模型(ERNIE/BERT)的序列标注预处理器。|NLP|reader = hub.reader.SequenceLabelReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=128,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sp_model_path=module.get_spm_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word_dict_path=module.get_word_dict_path())|
|MultiLabelClassifyReader|适用于Transformer类模型(ERNIE/BERT)的多标签分类预处理器。|NLP|reader = hub.reader.MultiLabelClassifyReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=128)|
|ReadingComprehensionReader|适用于Transformer类模型(ERNIE/BERT)的阅读理解任务预处理器。|NLP|reader = hub.reader.ReadingComprehensionReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_length=384)|
|RegressionReader|适用于回归任务的数据预处理器。|NLP|reader = hub.reader.RegressionReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab_path=module.get_vocab_path(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_seq_len=args.max_seq_len)|
|ImageClassificationReader|适用于图像分类数据的预处理器。会修改输入图像的尺寸、进行标准化处理、图像增广处理等操作。|CV|reader = hub.reader.ImageClassificationReader(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image_width=module.get_expected_image_width(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image_height=module.get_expected_image_height(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;images_mean=module.get_pretrained_images_mean(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;images_std=module.get_pretrained_images_std(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset=dataset)|
#### **5. 选择优化策略**
在PaddleHub中,Strategy类封装了一系列适用于迁移学习的Fine-tuning策略。Strategy包含了对预训练参数使用什么学习率变化策略,使用哪种类型的优化器,使用什么类型的正则化等。在我们要做的文本分类任务中,我们使用AdamWeightDecayStrategy优化策略。具体可以参考如下代码:
```python
strategy = hub.AdamWeightDecayStrategy(
learning_rate=5e-5, # Fine-tune过程中的最大学习率
weight_decay=0.01, # 模型的正则项参数,默认0.01,如果模型有过拟合倾向,可适当调高这一参数
warmup_proportion=0.1, #如果warmup_proportion>0, 例如0.1, 则学习率会在前10%的steps中线性增长至最高值learning_rate
# 有两种策略可选:
# (1)linear_decay策略学习率会在最高点后以线性方式衰减;
# (2)noam_decay策略学习率会在最高点以多项式形式衰减;
lr_scheduler="linear_decay",
)
```
包括AdamWeightDecayStrategy在内,PaddleHub还提供了多种优化策略的API。
|优化策略|描述|API示例|
|:--------:|:--------|:--------|
|DefaultFinetuneStrategy|默认的优化策略。其对应参数如下:<br>* learning_rate: 全局学习率。默认为1e-4。<br>* optimizer_name: 优化器名称。默认adam。<br>* regularization_coeff: 正则化的λ参数。默认为1e-3。<br>在图像分类任务中推荐使用此优化策略。|strategy = hub.DefaultFinetuneStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regularization_coeff=1e-3)|
|AdamWeightDecayStrategy|基于Adam优化器的学习率衰减策略。其对应参数如下:<br>* learning_rate: 全局学习率,默认为1e-4。<br>* lr_scheduler: 学习率调度方法,默认为"linear_decay"。<br>* warmup_proportion: warmup所占比重。<br>* weight_decay: 学习率衰减率。<br>* optimizer_name: 优化器名称,默认为adam。<br>在文本分类、阅读理解等任务中推荐使用此优化策略。|strategy = hub.AdamWeightDecayStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lr_scheduler="linear_decay",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;warmup_proportion=0.0,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weight_decay=0.01,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam")|
|L2SPFinetuneStrategy|使用L2SP正则作为惩罚因子的Finetune策略。其对应参数如下:<br>* learning_rate: 全局学习率。默认为1e-4。<br>* optimizer_name: 优化器名称。默认adam。<br>* regularization_coeff: 正则化的λ参数。默认为1e-3。|strategy = hub.L2SPFinetuneStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regularization_coeff=1e-3)|
|ULMFiTStrategy|该策略实现了ULMFiT论文中提出的三种策略:<br> * Slanted triangular learning rates是一种学习率先上升再下降的策略。<br>* Discriminative fine-tuning是一种学习率逐层递减的策略,通过该策略可以减缓底层的更新速度。<br>* Gradual unfreezing是一种逐层解冻的策略,通过该策略可以优先更新上层,再慢慢解冻下层参与更新。<br>其对应参数如下:<br> * learning_rate: 全局学习率。默认为1e-4。<br> * optimizer_name: 优化器名称。默认为adam。<br> * cut_fraction: 设置Slanted triangular learning rates学习率上升的步数在整个训练总步数中的比例。默认为0.1,如果设置为0,则不采用Slanted triangular learning rates。<br> * ratio: 设置Slanted triangular learning rates下降的最小学习率与上升的最大学习率的比例关系,默认为32,表示最小学习率是最大学习率的1/32。<br> * dis_blocks: 设置 Discriminative fine-tuning中的块数。默认为3,如果设置为0,则不采用Discriminative fine-tuning。<br> * factor: 设置Discriminative fine-tuning的衰减率。默认为2.6,表示下一层的学习率是上一层的1/2.6。<br> * frz_blocks: 设置Gradual unfreezing中的块数。块的概念同“dis_blocks”中介绍的概念。|strategy = hub.ULMFiTStrategy(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning_rate=1e-4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer_name="adam",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cut_fraction=0.1,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ratio=32,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dis_blocks=3,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;factor=2.6,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frz_blocks=3)|
#### **6. 设置运行配置。**
在PaddleHub中,用户可以使用Finetune API中的RunConfig配置Task进行Finetune时使用的参数,包括运行的Epoch次数、batch的大小、是否使用GPU训练等。代码示例如下所示。
```python
config = hub.RunConfig(use_cuda=True, num_epoch=3, batch_size=32, strategy=strategy)
```
#### **7. 组建训练任务。**
有了合适的预训练模型,并加载好要迁移的数据集后,我们开始组建一个Task。在PaddleHub中,Task代表了一个Fine-tune的任务。任务中包含了执行该任务相关的Program、数据读取器Reader、运行配置等内容。在这里可以找到文本分类任务对应的Task说明[TextClassifierTask](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/reference/task/text_classify_task.md)。具体实现方案如下:
1. 获取module(PaddleHub的预训练模型)的上下文环境,包括输入和输出的变量,以及Paddle Program(可执行的模型格式)。
2. 从预训练模型的输出变量中找到特征图提取层feature_map,在feature_map后面接入一个全连接层,如下代码中通过hub.TextClassifierTask的pooled_output参数指定。
3. 网络的输入层保持不变,依然从输入层开始,如下代码中通过hub.TextClassifierTask的参数feed_list变量指定。
hub.TextClassifierTask就是通过这两个参数明确我们的截取模型网络的要求,按照这样的配置,我们截取的网络是从输入层一直到特征提取的最后一层“pooled_output”,表示我将使用截出的网络来进行迁移学习训练。
```python
# 获取Module的上下文信息,得到输入、输出以及预训练的Paddle Program副本。
# trainable设置为True时,Module中的参数在Fine-tune时也会随之训练,否则保持不变。
# 其中最大序列长度max_seq_len为可调整的参数,建议值为128,根据任务文本长度不同可以进行修改,但最大不超过512。
# 若序列长度不足,会通过padding方式补到max_seq_len, 若序列长度大于该值,则会以截断方式让序列长度为max_seq_len。
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
# 返回ERNIE/BERT模型对应的[CLS]向量,可以用于句子或句对的特征表达。
pooled_output = outputs["pooled_output"]
# feed_list的Tensor顺序不可以调整
# 指定ERNIE中的输入tensor的顺序,与ClassifyReader返回的结果一致
feed_list = [
inputs["input_ids"].name,
inputs["position_ids"].name,
inputs["segment_ids"].name,
inputs["input_mask"].name,
]
# 通过输入特征,label与迁移的类别数,可以生成适用于文本分类的迁移任务
cls_task = hub.TextClassifierTask(
data_reader=reader, # 读取数据的reader
feature=pooled_output, # 从预训练提取的特征矩阵
feed_list=feed_list, # 待feed变量的名字列表
num_classes=dataset.num_labels, # 数据集的类别数量
metrics_choices = ["acc"],
config=config) # 运行配置
```
PaddleHub预置了常见任务的Task,每种Task都有特定的应用场景并提供了对应的度量指标,满足用户的不同需求。
|Task类型|描述|任务类型|
|:--------:|:--------:|:--------:|
|ImageClassifierTask|该Task基于输入的特征,添加一个或多个全连接层来创建一个分类任务用于Fine-tune,度量指标为准确率,损失函数为交叉熵Loss。|图像分类任务|
|TextClassifierTask|该Task基于输入的特征,添加一个Dropout层,以及一个或多个全连接层来创建一个文本分类任务用于finetune,度量指标为准确率,损失函数为交叉熵Loss。|文本分类任务|
|SequenceLabelTask|该Task基于输入的特征,添加一个全连接层或者一个全连接层和CRF层来创建一个序列标注任务用于Fine-tune,度量指标为F1,损失函数为交叉熵Loss。|序列标注任务|
|MultiLabelClassifierTask|该Task基于输入的特征,添加一个或多个全连接层来创建一个多标签分类任务用于finetune,度量指标为多个标签的平均AUC,损失函数为多个标签的平均交叉熵。|多标签分类任务|
|RegressionTask|该Task基于输入的特征,添加一个Dropout层,以及一个或多个全连接层来创建一个文本回归任务用于finetune,度量指标为准确率,损失函数为均方差损失函数。|文本回归任务|
|ReadingComprehensionTask|该Task基于输入的特征,添加一个全连接层来创建一个阅读理解任务用于Fine-tune,损失函数为交叉熵Loss。|阅读理解任务|
在设定每个Task前,用户需要提前了解待迁移学习的预训练模型的输入与输出,即对应上面代码中的“feed_list”和“pooled_output”。具体的输入输出代码可以分为如下几类:
* 图像分类模型
```
input_dict, output_dict, program = module.context(trainable=True)
feature_map = output_dict["feature_map"]
feed_list = [input_dict["image"].name]
```
* 自然语言处理模型(不包括word2vec_skipgram、simnet_bow、文本匹配和文本生成几个模型)
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
pooled_output = outputs["pooled_output"]
feed_list = [
inputs["input_ids"].name,
inputs["position_ids"].name,
inputs["segment_ids"].name,
inputs["input_mask"].name,
]
```
* word2vec_skipgram模型
```
inputs, outputs, program = module.context(trainable=True)
word_ids = inputs["word_ids"]
embedding = outputs["word_embs"]
```
* simnet_bow模型
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=2)
query = outputs["emb"]
title = outputs['emb_2']
```
* Pairwise文本匹配模型
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=3)
query = outputs["emb"]
left = outputs['emb_2']
right = outputs['emb_3']
```
* Pointwise文本匹配
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=2)
query = outputs["emb"]
title = outputs['emb_2']
```
* 文本生成模型
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
pooled_output = outputs["pooled_output"]
sequence_output = outputs["sequence_output"]
```
#### **8 启动Fine-tune,使用Finetune_and_eval函数完成训练和评估。**
```python
cls_task.finetune_and_eval()
```
显示信息如下例所示。可以看到训练的评估结果,Loss值和准确率等等。
```
[2020-07-28 21:28:21,658] [ TRAIN] - step 810 / 900: loss=0.05022 acc=0.97813 [step/sec: 4.07]
[2020-07-28 21:28:24,115] [ TRAIN] - step 820 / 900: loss=0.04719 acc=0.98125 [step/sec: 4.07]
[2020-07-28 21:28:26,574] [ TRAIN] - step 830 / 900: loss=0.06895 acc=0.98125 [step/sec: 4.07]
[2020-07-28 21:28:29,035] [ TRAIN] - step 840 / 900: loss=0.07830 acc=0.97813 [step/sec: 4.07]
[2020-07-28 21:28:31,490] [ TRAIN] - step 850 / 900: loss=0.07279 acc=0.97500 [step/sec: 4.08]
[2020-07-28 21:28:33,939] [ TRAIN] - step 860 / 900: loss=0.03220 acc=0.99375 [step/sec: 4.09]
[2020-07-28 21:28:36,388] [ TRAIN] - step 870 / 900: loss=0.05016 acc=0.98750 [step/sec: 4.09]
[2020-07-28 21:28:38,840] [ TRAIN] - step 880 / 900: loss=0.05604 acc=0.98750 [step/sec: 4.08]
[2020-07-28 21:28:41,293] [ TRAIN] - step 890 / 900: loss=0.05622 acc=0.98125 [step/sec: 4.08]
[2020-07-28 21:28:43,748] [ TRAIN] - step 900 / 900: loss=0.06642 acc=0.97813 [step/sec: 4.08]
[2020-07-28 21:28:43,750] [ INFO] - Evaluation on dev dataset start
[2020-07-28 21:28:46,654] [ EVAL] - [dev dataset evaluation result] loss=0.17890 acc=0.94079 [step/sec: 13.23]
[2020-07-28 21:28:46,657] [ INFO] - Evaluation on dev dataset start
[2020-07-28 21:28:49,527] [ EVAL] - [dev dataset evaluation result] loss=0.17890 acc=0.94079 [step/sec: 13.39]
[2020-07-28 21:28:49,529] [ INFO] - Load the best model from ckpt_20200728212416/best_model
[2020-07-28 21:28:50,112] [ INFO] - Evaluation on test dataset start
[2020-07-28 21:28:52,987] [ EVAL] - [test dataset evaluation result] loss=0.14264 acc=0.94819 [step/sec: 13.36]
[2020-07-28 21:28:52,988] [ INFO] - Saving model checkpoint to ckpt_20200728212416/step_900
[2020-07-28 21:28:55,789] [ INFO] - PaddleHub finetune finished.
```
通过Fine-tune完成模型训练后,在对应的ckpt目录(CKPT_DIR)下,PaddleHub会自动保存验证集上效果最好的模型。用户可以参考如下代码进行预测,其中推理出的标签值0表示负向评价,1表示正向评价。。
```python
import numpy as np
# 待预测数据
data = [
["这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般"],
["交通方便;环境很好;服务态度很好 房间较小"],
["19天硬盘就罢工了,算上运来的一周都没用上15天,可就是不能换了。唉,你说这算什么事呀!"]
]
index = 0
run_states = cls_task.predict(data=data)
results = [run_state.run_results for run_state in run_states]
for batch_result in results:
# 预测类别取最大分类概率值
batch_result = np.argmax(batch_result[0], axis=1)
for result in batch_result:
print("%s\tpredict=%s" % (data[index][0], result))
index += 1
```
预测结果如下所示。
```
[2020-07-28 18:06:45,441] [ INFO] - PaddleHub predict start
[2020-07-28 18:06:45,442] [ INFO] - The best model has been loaded
[2020-07-28 18:06:48,406] [ INFO] - PaddleHub predict finished.
这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般 predict=0
交通方便;环境很好;服务态度很好 房间较小 predict=1
19天硬盘就罢工了,算上运来的一周都没用上15天,可就是不能换了。唉,你说这算什么事呀! predict=0
```
# 自定义数据
训练一个新任务时,如果从零开始训练时,这将是一个耗时的过程,并且效果可能达不到理想的效果,此时您可以利用PaddleHub提供的预训练模型进行具体任务的Fine-tune。您只需要对自定义数据进行相应的预处理,随后输入预训练模型中,即可得到相应的结果。请参考如下内容设置数据集的结构。
## 一、图像分类数据集
利用PaddleHub迁移分类任务使用自定义数据时,需要切分数据集,将数据集切分为训练集、验证集和测试集。
### 数据准备
需要三个文本文件来记录对应的图片路径和标签,此外还需要一个标签文件用于记录标签的名称。
```
├─data: 数据目录
  ├─train_list.txt:训练集数据列表
  ├─test_list.txt:测试集数据列表
  ├─validate_list.txt:验证集数据列表
├─label_list.txt:标签列表
  └─...
```
训练/验证/测试集的数据列表文件的格式如下
```
图片1路径 图片1标签
图片2路径 图片2标签
...
```
label_list.txt的格式如下
```
分类1名称
分类2名称
...
```
示例:
[Flower数据集](../reference/datasets.md)为示例,train_list.txt/test_list.txt/validate_list.txt内容如下示例
```
roses/8050213579_48e1e7109f.jpg 0
sunflowers/45045003_30bbd0a142_m.jpg 3
daisy/3415180846_d7b5cced14_m.jpg 2
```
label_list.txt内容如下:
```
roses
tulips
daisy
sunflowers
dandelion
```
### 数据集加载
数据集的准备代码可以参考 [flowers.py](../../paddlehub/datasets/flowers.py)`hub.datasets.Flowers()` 会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。具体使用如下:
```python
from paddlehub.datasets import Flowers
flowers = Flowers(transforms)
flowers_validate = Flowers(transforms, mode='val')
```
* `transforms`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, `val`, 默认为`train`
## 二、图像着色数据集
利用PaddleHub迁移着色任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
### 数据准备
需要将准备用于着色训练和测试的彩色图像分成训练集数据和测试集数据。
```
├─data: 数据目录
  ├─train:训练集数据
|-图片文件夹1
|-图片文件夹2
|-……
|-图片1
|-图片2
|-……
  ├─test:测试集数据
|-图片文件夹1
|-图片文件夹2
|-……
|-图片1
|-图片2
|-……
  └─……
```
示例:
PaddleHub为用户提供了用于着色的数据集`Canvas数据集`, 它由1193张莫奈风格和400张梵高风格的图像组成,以[Canvas数据集](../reference/datasets.md)为示例,train文件夹内容如下:
```
├─train:训练集数据
|-monet
|-图片1
|-图片2
|-……
|-vango
|-图片1
|-图片2
|-……
```
### 数据集加载
数据集的准备代码可以参考 [canvas.py](../../paddlehub/datasets/canvas.py)`hub.datasets.Canvas()` 会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。具体使用如下:
```python
from paddlehub.datasets import Canvas
color_set = Canvas(transforms, mode='train')
```
* `transforms`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, 默认为`train`
## 三、风格迁移数据集
利用PaddleHub进行风格迁移任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
### 数据准备
需要将准备用于风格迁移的彩色图像分成训练集和测试集数据。
```
├─data: 数据目录
  ├─train:训练集数据
|-图片文件夹1
|-图片文件夹2
|-...
|-图片1
|-图片2
|-...
  ├─test:测试集数据
|-图片文件夹1
|-图片文件夹2
|-...
|-图片1
|-图片2
|-...
|- 21styles
|-图片1
|-图片2
  └─...
```
示例:
PaddleHub为用户提供了用于风格迁移的数据集`MiniCOCO数据集`, 训练集数据和测试集数据来源于COCO2014, 其中训练集有2001张图片,测试集有200张图片。 `21styles`文件夹下存放着21张不同风格的图片,用户可以根据自己的需求更换不同风格的图片。以[MiniCOCO数据集](../reference/datasets.md)为示例,train文件夹内容如下:
```
├─train:训练集数据
|-train
|-图片1
|-图片2
|-……
|-test
|-图片1
|-图片2
|-……
|-21styles
|-图片1
|-图片2
|-……
```
### 数据集加载
数据集的准备代码可以参考 [minicoco.py](../../paddlehub/datasets/minicoco.py)`hub.datasets.MiniCOCO()` 会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。具体使用如下:
```python
from paddlehub.datasets import MiniCOCO
ccolor_set = MiniCOCO(transforms, mode='train')
```
* `transforms`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, 默认为`train`
## 四、文本分类数据集
利用PaddleHub进行文本分类任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
### 数据准备
#### 1. 设置数据集目录
用户需要将数据集目录设定为如下格式:
```shell
├──data: 数据目录
├── train.txt: 训练集数据
├── dev.txt: 验证集数据
└── test.txt: 测试集数据
```
#### 2. 设置文件格式和内容
训练/验证/测试集的数据文件的编码格式建议为utf8格式。内容的第一列是文本类别标签,第二列为文本内容,列与列之间以Tab键分隔。建议在数据集文件第一行填写列说明"label"和"text_a",中间以Tab键分隔,示例如下:
```shell
label text_a
房产 昌平京基鹭府10月29日推别墅1200万套起享97折
教育 贵州2011高考录取分数线发布理科一本448分
社会 众多白领因集体户口面临结婚难题
...
```
### 数据集加载
加载文本分类的自定义数据集,用户仅需要继承基类TextClassificationDataset,修改数据集存放地址以及类别即可,具体可以参考如下代码:
```python
from paddlehub.datasets.base_nlp_dataset import TextClassificationDataset
class MyDataset(TextClassificationDataset):
# 数据集存放目录
base_path = '/path/to/dataset'
# 数据集的标签列表
label_list=['体育', '科技', '社会', '娱乐', '股票', '房产', '教育', '时政', '财经', '星座', '游戏', '家居', '彩票', '时尚']
def __init__(self, tokenizer, max_seq_len: int = 128, mode: str = 'train'):
if mode == 'train':
data_file = 'train.txt'
elif mode == 'test':
data_file = 'test.txt'
else:
data_file = 'dev.txt'
super().__init__(
base_path=self.base_path,
tokenizer=tokenizer,
max_seq_len=max_seq_len,
mode=mode,
data_file=data_file,
label_list=self.label_list,
is_file_with_header=True)
# 选择所需要的模型,获取对应的tokenizer
import paddlehub as hub
model = hub.Module(name='ernie_tiny', task='seq-cls', num_classes=len(MyDataset.label_list))
tokenizer = model.get_tokenizer()
# 实例化训练集
train_dataset = MyDataset(tokenizer)
```
至此用户可以通过MyDataset实例化获取对应的数据集,可以通过hub.Trainer对预训练模型`model`完成文本分类任务,详情可参考[PaddleHub文本分类demo](https://github.com/PaddlePaddle/PaddleHub/tree/release/v2.0.0-beta/demo/text_classification)
## 五、序列标注数据集
利用PaddleHub进行序列标注任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
### 数据准备
#### 1. 设置数据集目录
用户需要将数据集目录设定为如下格式:
```shell
├──data: 数据目录
├── train.txt: 训练集数据
├── dev.txt: 验证集数据
└── test.txt: 测试集数据
```
#### 2. 设置文件格式和内容
训练/验证/测试集的数据文件的编码格式建议为utf8格式。内容的第一列是文本内容, 第二列为文本中每个token对应的标签。需要注意的是,在文本和标签中,都需使用分隔符(该例子中使用的是斜杠`/`)隔开不同的token。
列与列之间以Tab键分隔。建议在数据集文件第一行填写列说明"label"和"text_a",中间以Tab键分隔,示例如下:
```shell
text_a label
5/月/1/2/日/,/北/京/市/怀/柔/县/民/政/局/、/畜/牧/局/领/导/来/到/驻/守/在/偏/远/山/区/的/武/警/北/京/一/总/队/十/支/队/十/四/中/队/。 O/O/O/O/O/O/B-LOC/I-LOC/I-LOC/B-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/O/B-ORG/I-ORG/I-ORG/O/O/O/O/O/O/O/O/O/O/O/O/B-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/O
他/每/年/还/为/河/北/农/业/大/学/扶/助/多/名/贫/困/学/生/。 O/O/O/O/O/B-ORG/I-ORG/I-ORG/I-ORG/I-ORG/I-ORG/O/O/O/O/O/O/O/O/O
...
```
### 数据准备
加载文本分类的自定义数据集,用户仅需要继承基类SeqLabelingDataset,修改数据集存放地址、类别信息和分隔符即可,具体可以参考如下代码:
```python
from paddlehub.datasets.base_nlp_dataset import SeqLabelingDataset
class MyDataset(SeqLabelingDataset):
# 数据集存放目录
base_path = '/path/to/dataset'
# 数据集的标签列表
label_list = ["B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "O"]
label_map = {idx: label for idx, label in enumerate(label_list)}
# 数据文件使用的分隔符
split_char = '/'
def __init__(self, tokenizer, max_seq_len: int = 128, mode: str = 'train'):
if mode == 'train':
data_file = 'train.txt'
elif mode == 'test':
data_file = 'test.txt'
else:
data_file = 'dev.txt'
super().__init__(
base_path=self.base_path,
tokenizer=tokenizer,
max_seq_len=max_seq_len,
mode=mode,
data_file=data_file,
label_file=None,
label_list=self.label_list,
split_char=self.split_char,
is_file_with_header=True)
# 选择所需要的模型,获取对应的tokenizer
import paddlehub as hub
model = hub.Module(name='ernie_tiny', task='token-cls', label_map=MyDataset.label_map)
tokenizer = model.get_tokenizer()
# 实例化训练集
train_dataset = MyDataset(tokenizer)
```
至此用户可以通过MyDataset实例化获取对应的数据集,可以通过hub.Trainer对预训练模型`model`完成系列标注任务,详情可参考[PaddleHub序列标注demo](https://github.com/PaddlePaddle/PaddleHub/tree/release/v2.0.0-beta/demo/sequence_labeling)
\ No newline at end of file
# 使用Word2Vec进行文本语义相似度计算
本示例展示利用PaddleHub“端到端地”完成文本相似度计算。
## 一、准备文本数据
```
驾驶违章一次扣12分用两个驾驶证处理可以吗 一次性扣12分的违章,能用不满十二分的驾驶证扣分吗
水果放冰箱里储存好吗 中国银行纪念币网上怎么预约
电脑反应很慢怎么办 反应速度慢,电脑总是卡是怎么回事
```
## 二、分词
利用PaddleHub Module LAC对文本数据进行分词。
```python
# coding:utf-8
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""similarity between two sentences"""
import numpy as np
import scipy
from scipy.spatial import distance
import paddle
import paddlehub as hub
from paddlehub.text.utils import load_vocab
raw_data = [
["驾驶违章一次扣12分用两个驾驶证处理可以吗", "一次性扣12分的违章,能用不满十二分的驾驶证扣分吗"],
["水果放冰箱里储存好吗", "中国银行纪念币网上怎么预约"],
["电脑反应很慢怎么办", "反应速度慢,电脑总是卡是怎么回事"]
]
lac = hub.Module(name="lac")
processed_data = []
for text_pair in raw_data:
inputs = {"text" : text_pair}
results = lac.lexical_analysis(data=inputs, use_gpu=True, batch_size=2)
data = []
for result in results:
data.append(" ".join(result["word"]))
processed_data.append(data)
```
## 三、计算文本语义相似度
将分词文本中的单词相应替换为wordid,之后输入wor2vec module中计算两个文本语义相似度。
```python
def convert_tokens_to_ids(vocab, text):
wids = []
tokens = text.split(" ")
for token in tokens:
wid = vocab.get(token, None)
if not wid:
wid = vocab["unknown"]
wids.append(wid)
return wids
module = hub.Module(name="word2vec_skipgram", version='1.0.0')
inputs, outputs, program = module.context(trainable=False)
vocab = load_vocab(module.get_vocab_path())
word_ids = inputs["word_ids"]
embedding = outputs["word_embs"]
paddle.enable_static()
place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
feeder = paddle.fluid.DataFeeder(feed_list=[word_ids], place=place)
for item in processed_data:
text_a = convert_tokens_to_ids(vocab, item[0])
text_b = convert_tokens_to_ids(vocab, item[1])
vecs_a, = exe.run(
program,
feed=feeder.feed([[text_a]]),
fetch_list=[embedding.name],
return_numpy=False)
vecs_a = np.array(vecs_a)
vecs_b, = exe.run(
program,
feed=feeder.feed([[text_b]]),
fetch_list=[embedding.name],
return_numpy=False)
vecs_b = np.array(vecs_b)
sent_emb_a = np.sum(vecs_a, axis=0)
sent_emb_b = np.sum(vecs_b, axis=0)
cos_sim = 1 - distance.cosine(sent_emb_a, sent_emb_b)
print("text_a: %s; text_b: %s; cosine_similarity: %.5f" %
(item[0], item[1], cos_sim))
```
...@@ -234,4 +234,3 @@ $ PaddleHub Serving will stop. ...@@ -234,4 +234,3 @@ $ PaddleHub Serving will stop.
* [人脸检测-基于pyramidbox_lite_server_mask](../../../demo/serving/module_serving/object_detection_pyramidbox_lite_server_mask) * [人脸检测-基于pyramidbox_lite_server_mask](../../../demo/serving/module_serving/object_detection_pyramidbox_lite_server_mask)
&emsp;&emsp;该示例展示了利用pyramidbox_lite_server_mask完成人脸口罩检测,检测人脸位置以及戴口罩的置信度。 &emsp;&emsp;该示例展示了利用pyramidbox_lite_server_mask完成人脸口罩检测,检测人脸位置以及戴口罩的置信度。
迁移学习
==================
本文将介绍介绍了如何自定义数据、以及基于PaddleHub Module的一些应用场景。
详细信息,参考以下教程:
.. toctree::
:maxdepth: 1
自定义数据<how_to_load_data>
文本相似度计算<sentence_sim>
========
教程
========
.. toctree::
:maxdepth: 2
:titlesonly:
命令行使用方法<tutorial/cmd_usage>
一键服务化部署<tutorial/serving>
如何创建自己的Module<tutorial/custom_module>
迁移学习<transfer_learning_index>
\ No newline at end of file
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
==============
Canvas
==============
.. code-block:: python
class paddlehub.datasets.Canvas(transform: Callable, mode: str = 'train'):
-----------------
Dataset for colorization. It contains 1193 and 400 pictures for Monet and Vango paintings style, respectively. We collected data from https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/.
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train or test). Default to 'train'.
\ No newline at end of file
==============
ChnSentiCorp
==============
.. code-block:: python
class paddlehub.datasets.ChnSentiCorp(tokenizer: Union[BertTokenizer, CustomTokenizer], max_seq_len: int = 128, mode: str = 'train'):
-----------------
ChnSentiCorp is a dataset for chinese sentiment classification, which was published by Tan Songbo at ICT of Chinese Academy of Sciences.
-----------------
* Args:
* tokenizer(:obj:`BertTokenizer` or `CustomTokenizer`)
It tokenizes the text and encodes the data as model needed.
* max_seq_len(:obj:`int`, `optional`, defaults to :128)
The maximum length (in number of tokens) for the inputs to the selected module, such as ernie, bert and so on.
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
\ No newline at end of file
==============
ESC50
==============
.. code-block:: python
class paddlehub.datasets.ESC50(mode: str = 'train', feat_type: str = 'mel'):)
-----------------
The ESC-50 dataset is a labeled collection of 2000 environmental audio recordings suitable for benchmarking methods of environmental sound classification.
-----------------
* Args:
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
* feat_type(:obj:`str`, `optional`, defaults to `mel`):
It identifies the input feature type (mel, or raw).
\ No newline at end of file
==============
Flowers
==============
.. code-block:: python
class paddlehub.datasets.Flowers(transform: Callable, mode: str = 'train'):
-----------------
Flower classification dataset.
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train, test or val). Default to 'train'.
\ No newline at end of file
==============
LCQMC
==============
.. code-block:: python
class paddlehub.datasets.LCQMC(tokenizer: Union[BertTokenizer, CustomTokenizer], max_seq_len: int = 128, mode: str = 'train'):
-----------------
A Large-scale Chinese Question Matching Corpus.
-----------------
* Args:
* tokenizer(:obj:`BertTokenizer` or `CustomTokenizer`)
It tokenizes the text and encodes the data as model needed.
* max_seq_len(:obj:`int`, `optional`, defaults to :128)
The maximum length (in number of tokens) for the inputs to the selected module, such as ernie, bert and so on.
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
\ No newline at end of file
==============
MiniCOCO
==============
.. code-block:: python
class paddlehub.datasets.MiniCOCO(transform: Callable, mode: str = 'train'):
-----------------
Dataset for Style transfer. The dataset contains 2001 images for training set and 200 images for testing set. They are derived form COCO2014. Meanwhile, it contains 21 different style pictures in file "21styles".
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train or test). Default to 'train'.
\ No newline at end of file
==============
MSRA_NER
==============
.. code-block:: python
class paddlehub.datasets.MSRA_NER(tokenizer: Union[BertTokenizer, CustomTokenizer], max_seq_len: int = 128, mode: str = 'train'):
-----------------
A set of manually annotated Chinese word-segmentation data and specifications for training and testing a Chinese word-segmentation system for research purposes. For more information please refer to https://www.microsoft.com/en-us/download/details.aspx?id=52531
-----------------
* Args:
* tokenizer(:obj:`BertTokenizer` or `CustomTokenizer`)
It tokenizes the text and encodes the data as model needed.
* max_seq_len(:obj:`int`, `optional`, defaults to :128)
The maximum length (in number of tokens) for the inputs to the selected module, such as ernie, bert and so on.
* mode(:obj:`str`, `optional`, defaults to `train`):
It identifies the dataset mode (train, test or dev).
\ No newline at end of file
==============
OpticDiscSeg
==============
.. code-block:: python
class paddlehub.datasets.OpticDiscSeg(transform: Callable, mode: str = 'train'):
-----------------
OpticDiscSeg dataset is extraced from iChallenge-AMD(https://ai.baidu.com/broad/subordinate?dataset=amd).
-----------------
* Args:
* transform(Callable)
The method of preprocess images.
* mode(str)
The mode for preparing dataset(train, test or val). Default to 'train'.
\ No newline at end of file
==============
Datasets
==============
CV
==============
.. toctree::
:maxdepth: 2
:titlesonly:
Canvas<datasets/canvas.rst>
Flowers<datasets/flowers.rst>
OpticDiscSeg<datasets/opticdisc.rst>
MiniCOCO<datasets/minicoco.rst>
NLP
==============
.. toctree::
:maxdepth: 2
:titlesonly:
ChnSentiCorp<datasets/chnsenticorp.rst>
LCQMC<datasets/lcqmc.rst>
MSRA_NER<datasets/msra_ner.rst>
Audio
==============
.. toctree::
:maxdepth: 2
:titlesonly:
ESC50<datasets/esc50.rst>
\ No newline at end of file
================
Hub Environment
================
.. code-block:: console
HUB_HOME
├── MODULE_HOME
├── CACHE_HOME
├── DATA_HOME
├── CONF_HOME
├── THIRD_PARTY_HOME
├── TMP_HOME
└── LOG_HOME
paddlehub.env.HUB_HOME
=========================
The root directory for storing PaddleHub related data. Default to ~/.paddlehub. Users can change the default value through the HUB_HOME environment variable.
paddlehub.env.MODULE_HOME
=========================
Directory for storing the installed PaddleHub Module.
paddlehub.env.CACHE_HOME
=========================
Directory for storing the cached data.
paddlehub.env.DATA_HOME
=========================
Directory for storing the automatically downloaded datasets.
paddlehub.env.CONF_HOME
=========================
Directory for storing the default configuration files.
paddlehub.env.THIRD_PARTY_HOME
================================
Directory for storing third-party libraries.
paddlehub.env.TMP_HOME
=========================
Directory for storing the temporary files generated during running, such as intermediate products of installing modules, files in this directory will generally be automatically cleared.
paddlehub.env.LOG_HOME
=========================
Directory for storing the log files generated during operation, including some non-fatal errors. The log will be stored daily.
==============
Module
==============
.. code-block:: python
class paddlehub.Module(
name: str = None,
directory: str = None,
version: str = None,
ignore_env_mismatch: bool = False,
**kwargs)
-----------------
In PaddleHub, Module represents an executable module, which usually a pre-trained model that can be used for end-to-end prediction, such as a face detection model or a lexical analysis model, or a pre-trained model that requires finetuning, such as BERT/ERNIE. When loading a Module with a specified name, if the Module does not exist locally, PaddleHub will automatically request the server or the specified Git source to download the resource.
-----------------
* Args:
* name(str | optional)
Module name.
* directory(str | optional)
Directory of the module to be loaded, only takes effect when the `name` is not specified.
* version(str | optional)
The version limit of the module, only takes effect when the `name` is specified. When the local Module does not meet the specified version conditions, PaddleHub will re-request the server to download the appropriate Module. Default to None, This means that the local Module will be used. If the Module does not exist, PaddleHub will download the latest version available from the server according to the usage environment.
* ignore_env_mismatch(bool | optional)
Whether to ignore the environment mismatch when installing the Module. Default to False.
**member functions**
=====================
export_onnx_model
------------------
.. code-block:: python
def export_onnx_model(
dirname: str,
input_spec: List[paddle.static.InputSpec] = None,
include_sub_modules: bool = True,
**kwargs):
Export the model to ONNX format.
* Args:
* dirname(str)
The directory to save the onnx model.
* input_spec(list)
Describes the input of the saved model's forward method, which can be described by InputSpec or example Tensor. If None, all input variables of the original Layer's forward method would be the inputs of the saved model. Default None.
* include_sub_modules(bool)
Whether to export sub modules. Default to True.
* \*\*kwargs(dict|optional)
Other export configuration options for compatibility, some may be removed in the future. Don't use them If not necessary. Refer to https://github.com/PaddlePaddle/paddle2onnx for more information.
save_inference_model
----------------------
.. code-block:: python
def save_inference_model(
dirname: str,
model_filename: str = None,
params_filename: str = None,
input_spec: List[paddle.static.InputSpec] = None,
include_sub_modules: bool = True,
combined: bool = True):
Export the model to Paddle Inference format.
* Args:
* name(str | optional)
Module name.
* model_filename(str)
The name of the saved model file. Default to `__model__`.
* params_filename(str)
The name of the saved parameters file, only takes effect when `combined` is True. Default to `__params__`.
* input_spec(list)
Describes the input of the saved model's forward method, which can be described by InputSpec or example Tensor. If None, all input variables of the original Layer's forward method would be the inputs of the saved model. Default None.
* include_sub_modules(bool)
Whether to export sub modules. Default to True.
* combined(bool)
Whether to save all parameters in a combined file. Default to True.
sub_modules
----------------------
.. code-block:: python
def sub_modules(recursive: bool = True):
Get all sub modules.
* Args:
* recursive(bool):
Whether to get sub modules recursively. Default to True.
**classmethod**
=================
get_py_requirements
----------------------
.. code-block:: python
@classmethod
def get_py_requirements(cls) -> List[str]:
Get Module's python package dependency list.
load
----------------------
.. code-block:: python
@classmethod
def load(cls, directory: str) -> Generic:
Load the Module object defined in the specified directory.
* Args:
* directory(str):
Module directory.
load_module_info
----------------------
.. code-block:: python
@classmethod
def load_module_info(cls, directory: str) -> EasyDict:
Load the Module object defined in the specified directory.
* Args:
* directory(str):
Module directory.
**property**
=================
is_runnable
-----------------
.. code-block:: python
is_runnable
Whether the Module is runnable, in other words, whether can we execute the Module through the `hub run` command.
name
-----------------
.. code-block:: python
name
Module name.
directory
-----------------
.. code-block:: python
directory
Directory of Module.
version
-----------------
.. code-block:: python
version
Module name.
type
-----------------
.. code-block:: python
type
Module type.
summary
-----------------
.. code-block:: python
summary
Module summary.
author
-----------------
.. code-block:: python
author
The author of Module
author_email
-----------------
.. code-block:: python
author_email
The email of Module author
.. note::
Module is a factory class that is used to automatically download and load user-defined model classes. In addition to the above methods or property, each Module has other custom methods or property. The relevant definitions need to be viewed in the corresponding documentation
=================
Module Decorator
=================
moduleinfo
============
.. code-block:: python
def paddlehub.module.module.moduleinfo(
name: str,
version: str,
author: str = None,
author_email: str = None,
summary: str = None,
type: str = None,
meta=None) -> Callable:
-----------------
Mark Module information for a python class, and the class will automatically be extended to inherit HubModule. In other words, python classes marked with moduleinfo can be loaded through hub.Module.
-----------------
* Args:
* name(str)
Module name.
* version(str)
Module name.
* author(str)
The author of Module.
* author_email(str)
The email of Module author.
* summary(str)
Module summary.
* type(str)
Module type.
moduleinfo
============
.. code-block:: python
def paddlehub.module.module.runnable(func: Callable) -> Callable:
-----------------
Mark a Module method as runnable, when the command `hub run` is used, the method will be called.
-----------------
* Args:
* func(Callable)
member function of Module.
serving
============
.. code-block:: python
def paddlehub.module.module.serving(func: Callable) -> Callable:
-----------------
Mark a Module method as serving method, when the command `hub serving` is used, the method will be called.
-----------------
* Args:
* func(Callable)
member function of Module.
\ No newline at end of file
=======================
LocalModuleManager
=======================
.. code-block:: python
class paddlehub.module.manager.LocalModuleManager(home: str = MODULE_HOME):
-----------------
LocalModuleManager is used to manage PaddleHub's local Module, which supports the installation, uninstallation, and search of HubModule. LocalModuleManager is a singleton object related to the path, in other words, when the LocalModuleManager object of the same home directory is generated multiple times, the same object is returned.
-----------------
* Args:
* home(str)
The directory where PaddleHub modules are stored, the default is ~/.paddlehub/modules
**member functions**
=====================
install
------------------
.. code-block:: python
def install(
name: str = None,
directory: str = None,
archive: str = None,
url: str = None,
version: str = None,
ignore_env_mismatch: bool = False) -> HubModule:
Install a HubModule from name or directory or archive file or url. When installing with the name parameter, if a module that meets the conditions (both name and version) already installed, the installation step will be skipped. When installing with other parameter, The locally installed modules will be uninstalled.
* Args:
* name(str | optional)
module name to install
* directory(str | optional)
directory containing module code
* archive(str | optional)
archive file containing module code
* url(str|optional)
url points to a archive file containing module code
* version(str | optional)
module version, use with name parameter
* ignore_env_mismatch(str | optional)
Whether to ignore the environment mismatch when installing the Module.
uninstall
------------------
.. code-block:: python
def uninstall(name: str) -> bool:
Uninstall a HubModule from name.
* Args:
* name(str)
module name to uninstall
* Return:
True if uninstall successfully else False
list
------------------
.. code-block:: python
def list() -> List[HubModule]:
List all installed HubModule.
* Return:
List of installed HubModule.
search
------------------
.. code-block:: python
def search(name: str) -> HubModule:
search a HubModule with specified name.
* Args:
* name(str)
module name to search.
* Return:
None if not HubModule with specified name found else the specified HubModule.
\ No newline at end of file
==============
Trainer
==============
.. code-block:: python
class paddlehub.Trainer(
model: paddle.nn.Layer,
optimizer: paddle.optimizer.Optimizer,
use_gpu: bool = False,
use_vdl: bool = True,
checkpoint_dir: str = None,
compare_metrics: Callable = None):
-----------------
Model trainer.
-----------------
* Args:
* model(paddle.nn.Layer)
Model to train or evaluate.
* optimizer(paddle.optimizer.Optimizer)
Optimizer for loss.
* use_gpu(bool)
Whether to use gpu to run.
* use_vdl(bool)
Whether to use visualdl to record training data.
* checkpoint_dir(str)
Directory where the checkpoint is saved, and the trainer will restore the state and model parameters from the checkpoint.
* compare_metrics(Callable)
The method of comparing the model metrics. If not specified, the main metric return by `validation_step` will be used for comparison by default, the larger the value, the better the effect. This method will affect the saving of the best model. If the default behavior does not meet your requirements, please pass in a custom method.
**member functions**
=====================
train
------------------
.. code-block:: python
def train(
train_dataset: paddle.io.Dataset,
epochs: int = 1,
batch_size: int = 1,
num_workers: int = 0,
eval_dataset: paddle.io.Dataset = None,
log_interval: int = 10,
save_interval: int = 10,
collate_fn: Callable = None):
Train a model with specific config.
* Args:
* train_dataset(paddle.io.Dataset)
Dataset to train the model
* epochs(int)
Number of training loops, default is 1.
* batch_size(int)
Batch size of per step, default is 1.
* num_workers(int)
Number of subprocess to load data, default is 0.
* eval_dataset(paddle.io.Dataset)
The validation dataset, deafult is None. If set, the Trainer will execute evaluate function every `save_interval` epochs.
* log_interval(int)
Log the train infomation every `log_interval` steps.
* save_interval(int)
Save the checkpoint every `save_interval` epochs.
* collate_fn(Callable)
function to generate mini-batch data by merging the sample list. None for only stack each fields of sample in axis 0(same as :attr::`np.stack(..., axis=0)`). Default None.
evaluate
----------------------
.. code-block:: python
def evaluate(
eval_dataset: paddle.io.Dataset,
batch_size: int = 1,
num_workers: int = 0,
collate_fn: Callable = None):
Run evaluation and returns metrics.
* Args:
* eval_dataset(paddle.io.Dataset)
The validation dataset
* batch_size(int)
Batch size of per step, default is 1.
* num_workers(int)
Number of subprocess to load data, default is 0.
* collate_fn(Callable)
function to generate mini-batch data by merging the sample list. None for only stack each fields of sample in axis 0(same as :attr::`np.stack(..., axis=0)`). Default None.
\ No newline at end of file
==============
API Reference
==============
Module
==============
.. toctree::
:maxdepth: 2
:titlesonly:
Module<api/module.rst>
LocalModuleManager<api/module_manager.rst>
Module Decorator<api/module_decorator.rst>
Transfer Learning
==================
.. toctree::
:maxdepth: 2
:titlesonly:
Trainer<api/trainer.rst>
datasets<api/datasets_index.rst>
Environment
==============
.. toctree::
:maxdepth: 2
:titlesonly:
env<api/env.rst>
\ No newline at end of file
# Contribution Code # How to contribution code
PaddleHub welcomes contributors. PaddleHub welcomes contributors.
...@@ -108,8 +108,54 @@ If the reviewer gives you feedback that you need to continue fixing the codes, y ...@@ -108,8 +108,54 @@ If the reviewer gives you feedback that you need to continue fixing the codes, y
## Document ## Document
Documents are generated using [sphinx](http://sphinx-doc.org/). Files support the [Markdown](https://guides.github.com/features/mastering-markdown/) and [reStructuredText](http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html) formats. All documents are in the [docs/](../../) directory. Documents are generated using [sphinx](http://sphinx-doc.org/). Files support the [Markdown](https://guides.github.com/features/mastering-markdown/) and [reStructuredText](http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html) formats. All documents are in the [docs/](https://github.com/PaddlePaddle/PaddleHub/tree/release/v2.1/docs) directory.
* Before submitting document changes, please Generate documents locally: `cd docs/ && make clean && make html` and then, all generated pages can be found in `docs/_build/html` directory. Please carefully analyze Each WARNING in generated logs, which is very likely or Empty connections or other problems. * Before submitting document changes, please Generate documents locally: `cd docs/ && make clean && make html` and then, all generated pages can be found in `docs/_build/html` directory. Please carefully analyze Each WARNING in generated logs, which is very likely or Empty connections or other problems.
* Try to use Relative Path when you need links. * Try to use Relative Path when you need links.
## Thanks for contribution
<p align="center">
<a href="https://github.com/nepeplwu"><img src="https://avatars.githubusercontent.com/u/45024560?v=4" width=75 height=75></a>
<a href="https://github.com/Steffy-zxf"><img src="https://avatars.githubusercontent.com/u/48793257?v=4" width=75 height=75></a>
<a href="https://github.com/ZeyuChen"><img src="https://avatars.githubusercontent.com/u/1371212?v=4" width=75 height=75></a>
<a href="https://github.com/ShenYuhan"><img src="https://avatars.githubusercontent.com/u/28444161?v=4" width=75 height=75></a>
<a href="https://github.com/kinghuin"><img src="https://avatars.githubusercontent.com/u/11913168?v=4" width=75 height=75></a>
<a href="https://github.com/haoyuying"><img src="https://avatars.githubusercontent.com/u/35907364?v=4" width=75 height=75></a>
<a href="https://github.com/grasswolfs"><img src="https://avatars.githubusercontent.com/u/23690325?v=4" width=75 height=75></a>
<a href="https://github.com/sjtubinlong"><img src="https://avatars.githubusercontent.com/u/2063170?v=4" width=75 height=75></a>
<a href="https://github.com/KPatr1ck"><img src="https://avatars.githubusercontent.com/u/22954146?v=4" width=75 height=75></a>
<a href="https://github.com/jm12138"><img src="https://avatars.githubusercontent.com/u/15712990?v=4" width=75 height=75></a>
<a href="https://github.com/DesmonDay"><img src="https://avatars.githubusercontent.com/u/20554008?v=4" width=75 height=75></a>
<a href="https://github.com/adaxiadaxi"><img src="https://avatars.githubusercontent.com/u/58928121?v=4" width=75 height=75></a>
<a href="https://github.com/chunzhang-hub"><img src="https://avatars.githubusercontent.com/u/63036966?v=4" width=75 height=75></a>
<a href="https://github.com/linshuliang"><img src="https://avatars.githubusercontent.com/u/15993091?v=4" width=75 height=75></a>
<a href="https://github.com/eepgxxy"><img src="https://avatars.githubusercontent.com/u/15946195?v=4" width=75 height=75></a>
<a href="https://github.com/houj04"><img src="https://avatars.githubusercontent.com/u/35131887?v=4" width=75 height=75></a>
<a href="https://github.com/paopjian"><img src="https://avatars.githubusercontent.com/u/20377352?v=4" width=75 height=75></a>
<a href="https://github.com/zbp-xxxp"><img src="https://avatars.githubusercontent.com/u/58476312?v=4" width=75 height=75></a>
<a href="https://github.com/dxxxp"><img src="https://avatars.githubusercontent.com/u/15886898?v=4" width=75 height=75></a>
<a href="https://github.com/1084667371"><img src="https://avatars.githubusercontent.com/u/50902619?v=4" width=75 height=75></a>
<a href="https://github.com/Channingss"><img src="https://avatars.githubusercontent.com/u/12471701?v=4" width=75 height=75></a>
<a href="https://github.com/Austendeng"><img src="https://avatars.githubusercontent.com/u/16330293?v=4" width=75 height=75></a>
<a href="https://github.com/BurrowsWang"><img src="https://avatars.githubusercontent.com/u/478717?v=4" width=75 height=75></a>
<a href="https://github.com/cqvu"><img src="https://avatars.githubusercontent.com/u/37096589?v=4" width=75 height=75></a>
<a href="https://github.com/Haijunlv"><img src="https://avatars.githubusercontent.com/u/28926237?v=4" width=75 height=75></a>
<a href="https://github.com/holyseven"><img src="https://avatars.githubusercontent.com/u/13829174?v=4" width=75 height=75></a>
<a href="https://github.com/MRXLT"><img src="https://avatars.githubusercontent.com/u/16594411?v=4" width=75 height=75></a>
<a href="https://github.com/cclauss"><img src="https://avatars.githubusercontent.com/u/3709715?v=4" width=75 height=75></a>
<a href="https://github.com/hu-qi"><img src="https://avatars.githubusercontent.com/u/17986122?v=4" width=75 height=75></a>
<a href="https://github.com/jayhenry"><img src="https://avatars.githubusercontent.com/u/4285375?v=4" width=75 height=75></a>
</p>
* Many thanks to [肖培楷](https://github.com/jm12138), Contributed to street scene cartoonization, portrait cartoonization, gesture key point recognition, sky replacement, depth estimation, portrait segmentation and other modules
* Many thanks to [Austendeng](https://github.com/Austendeng) for fixing the SequenceLabelReader
* Many thanks to [cclauss](https://github.com/cclauss) optimizing travis-ci check
* Many thanks to [奇想天外](http://www.cheerthink.com/),Contributed a demo of mask detection
* Many thanks to [mhlwsk](https://github.com/mhlwsk),Contributed the repair sequence annotation prediction demo
* Many thanks to [zbp-xxxp](https://github.com/zbp-xxxp),Contributed modules for viewing pictures and writing poems
* Many thanks to [zbp-xxxp](https://github.com/zbp-xxxp) and [七年期限](https://github.com/1084667371),Jointly contributed to the Mid-Autumn Festival Special Edition Module
* Many thanks to [livingbody](https://github.com/livingbody),Contributed models for style transfer based on PaddleHub's capabilities and Mid-Autumn Festival WeChat Mini Program
* Many thanks to [BurrowsWang](https://github.com/BurrowsWang) for fixing Markdown table display problem
* Many thanks to [huqi](https://github.com/hu-qi) for fixing readme typo
# Interesting cases of third parties
The following are some of the interesting and practical works created by developers based on PaddleHub in previous PaddleHub courses or events, which are all included in AI Studio and can run online.
1. [Cloth Scissors Rock \[Face Recognition to Switch Local Window\] ](https://aistudio.baidu.com/aistudio/projectdetail/507630)
2. [Fish in the Autumn Water \[yesok dance background cutout conversion \& anime style migration\] ](http://aistudio.baidu.com/aistudio/projectdetail/517066)
3. [Ninetailskim \[play retro windows pinball on people's faces\] ](https://aistudio.baidu.com/aistudio/projectdetail/518861)
4. [Ura\_\_ \[Monitor Mask, Voice Alerts, Background Recording\] ](https://aistudio.baidu.com/aistudio/projectdetail/506931)
5. [Nine Alchemists \[Shadow stream's Green Frog Cai Xukun, Face Recognition + Headgear + Portrait Segmentation\] ](https://aistudio.baidu.com/aistudio/projectdetail/505168)
6. [Seven-year term \[style migration and local deployment\] ](https://aistudio.baidu.com/aistudio/projectdetail/520453)
7. [Fanas Invincible \[Lipstick Color Test Project\] ](https://aistudio.baidu.com/aistudio/projectdetail/516520)
8. [skywalk163 \[statistics of source code word frequency and word cloud \& portrait display with paddlehub\] ](https://aistudio.baidu.com/aistudio/projectdetail/519841)
9. [AIStudio261428 \[Face Recognition + Cartoon Emoticons\]](https://aistudio.baidu.com/aistudio/projectdetail/519616)
10. [Tudou Sprouts \[Invite Cartoon Characters to Visit on Children's Day\]](https://aistudio.baidu.com/aistudio/projectdetail/520925)
11. [Panda Feeling \[changing masks\] ](https://aistudio.baidu.com/aistudio/projectdetail/520996)
12. [Kly1997 \[One-key Travel + Sunglasses\]](https://aistudio.baidu.com/aistudio/projectdetail/518117)
13. [Loneliness\_Memo \[Crossing over to the painting\] ](https://aistudio.baidu.com/aistudio/projectdetail/516332)
14. [isse7 \[Creative Projects: Style "Ghostface" Change\]](https://aistudio.baidu.com/aistudio/projectdetail/515307)
15. [Pda \[Face Fun Change\] ](https://aistudio.baidu.com/aistudio/projectdetail/516306)
16. [Kgkzhiwen \[My New Clothes\]](https://aistudio.baidu.com/aistudio/projectdetail/516663)
17. [Ah Ah Ah Good Good Study \[Automatic face shape adjustment\]](https://aistudio.baidu.com/aistudio/projectdetail/513640)
18. [Tfboy \[ID Photo Replacement\]](https://aistudio.baidu.com/aistudio/projectdetail/509443)
19. [Leigangblog \[I am a star face\]](https://aistudio.baidu.com/aistudio/projectdetail/505537)
20. [wpb3dm \[Fashion Model Dressup\]](https://aistudio.baidu.com/aistudio/projectdetail/519349)
21. [lsvine\_bai \[Girlfriend turns into mysterious blonde goddess in seconds\]](https://aistudio.baidu.com/aistudio/projectdetail/521784)
22. [Lemonadeqk \[Easy Stargazing\]](https://aistudio.baidu.com/aistudio/projectdetail/520488)
23. [XM1436gr \[AI for Cartoon Face using PaddleHub Keypoint Detection\] ](https://aistudio.baidu.com/aistudio/projectdetail/514547)
24. [Wantzai \[Everyone is a round-eyed cute boy\] ](https://aistudio.baidu.com/aistudio/projectdetail/519222)
25. [Arrowarcher \[AI One Key Hair chane\] ](https://aistudio.baidu.com/aistudio/projectdetail/508270)
26. [WHY197598 \[Fundamentals of Shifting Objects\] ](https://aistudio.baidu.com/aistudio/projectdetail/517961)
27. [SIGNED BY JINGYI \[Fatigue detection based on paddlehub face key point detection\]](https://aistudio.baidu.com/aistudio/projectdetail/506024)
28. [thunder95 \[PaddleHub Eyes Emotion Poll\] ](https://aistudio.baidu.com/aistudio/projectdetail/514205)
29. [Windy Moon C \[Grave Bouncing Graduation Photo\]](https://aistudio.baidu.com/aistudio/projectdetail/511253)
30. [Ruyi\_Egg \[left looks like Chow Yun-Fat, right looks like Andy Lau\] ](https://aistudio.baidu.com/aistudio/projectdetail/507231)
===================
Active community
===================
.. toctree::
:maxdepth: 2
:titlesonly:
community/more_demos.md
community/contribute_code.md
------------
.. toctree::
:maxdepth: 2
:titlesonly:
AI Creative Competition I <https://aistudio.baidu.com/aistudio/competition/detail/34>
AI Creative Competition II <https://aistudio.baidu.com/aistudio/competition/detail/35>
AI Creative Competition III <https://aistudio.baidu.com/aistudio/competition/detail/72>
AI Creative Competition IV <https://aistudio.baidu.com/aistudio/competition/detail/79>
\ No newline at end of file
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
from recommonmark.transform import AutoStructify
from recommonmark.parser import CommonMarkParser
# sys.path.insert(0, os.path.abspath('../../paddlehub'))
# import paddlehub as hub
# -- Project information -----------------------------------------------------
project = 'PaddleHub'
copyright = '2021, PaddlePaddle'
author = 'PaddlePaddle'
# The full version, including alpha/beta/rc tags
# release = 'v{}'.format(hub.__version__)
release = 'v2.1.0'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'sphinx.ext.coverage', 'sphinx.ext.viewcode', 'sphinx.ext.mathjax',
'sphinx.ext.githubpages', 'sphinx.ext.napoleon', 'recommonmark', 'sphinx_markdown_tables'
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', '.DS_Store']
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_title = project
html_theme = "sphinx_materialdesign_theme"
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
source_parsers = {'.md': CommonMarkParser}
source_suffix = ['.rst', '.md']
Contribution Code
==================
Contributors, especially code and pre-training model contributors, are very welcome at PaddleHub. The following document describes how to contribute code and pre-training models.
... toctree::
:maxdepth: 1
Contribution code <contri_pr.md>
Contribute to the pre-training model <contri_pretrained_model>.
# How to Write a PaddleHub Module and Go Live
## I. Preparation
### Basic Model Information
We are going to write a PaddleHub Module with the following basic information about the module:
```yaml
name: senta_test
version: 1.0.0
summary: This is a PaddleHub Module. Just for test.
author: anonymous
author_email:
type: nlp/sentiment_analysis
```
**This sample code can be referred to as [senta\_module\_sample](../../demo/senta_module_sample/senta_test)**
The Module has an interface sentiment\_classify, which is used to receive incoming text and give it a sentiment preference (positive/negative). It supports python interface calls and command line calls.
```python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
```
```cmd
hub run senta_test --input_text 这部电影太差劲了
```
<br/>
### Strategy
For the sake of simplicity of the sample codes, we use a very simple sentiment strategy. When the input text has the word specified in the vocabulary list, the text tendency is judged to be negative; otherwise it is positive.
<br/>
## II. Create Module
### Step 1: Create the necessary directories and files.
Create a senta\_test directory. Then, create module.py, processor.py, and vocab.list in the senta\_test directory, respectively.
| File Name | Purpose |
| ------------ | ------------------------------------------------------------ |
| module.py | It is the main module that provides the implementation codes of Module. |
| processor.py | It is the helper module that provides a way to load the vocabulary list. |
| vocab.list | It stores the vocabulary. |
```cmd
➜ tree senta_test
senta_test/
├── vocab.list
├── module.py
└── processor.py
```
### Step 2: Implement the helper module processor.
Implement a load\_vocab interface in processor.py to read the vocabulary list.
```python
def load_vocab(vocab_path):
with open(vocab_path) as file:
return file.read().split()
```
### Step 3: Write Module processing codes.
The module.py file is the place where the Module entry code is located. We need to implement prediction logic on it.
#### Step 3\_1. Reference the necessary header files
```python
import argparse
import os
import paddlehub as hub
from paddlehub.module.module import runnable, moduleinfo
from senta_test.processor import load_vocab
```
**NOTE:** When referencing a module in Module, you need to enter the full path, for example, senta\_test. processor.
#### Step 3\_2. Define the SentaTest class.
Module.py needs to have a class that inherits hub. Module, and this class is responsible for implementing the prediction logic and filling in basic information with using moduleinfo. When the hub. Module(name="senta\_test") is used to load Module, PaddleHub automatically creates an object of SentaTest and return it.
```python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
```
#### Step 3\_3. Perform necessary initialization.
```python
def __init__(self):
# add arg parser
self.parser = argparse.ArgumentParser(
description="Run the senta_test module.",
prog='hub run senta_test',
usage='%(prog)s',
add_help=True)
self.parser.add_argument(
'--input_text', type=str, default=None, help="text to predict")
# load word dict
vocab_path = os.path.join(self.directory, "vocab.list")
self.vocab = load_vocab(vocab_path)
```
`注意`: The execution class object has a built-in directory attribute by default. You can directly get the path of the Module.
#### Step 3\_4: Refine the prediction logic.
```python
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
```
#### Step 3\_5. Support the command-line invoke.
If you want the module to support command-line invoke, you need to provide a runnable modified interface that parses the incoming data, makes prediction, and returns the results.
If you don't want to provide command-line prediction, you can leave the interface alone and PaddleHub automatically finds out that the module does not support command-line methods and gives a hint when PaddleHub executes in command lines.
```python
@runnable
def run_cmd(self, argvs):
args = self.parser.parse_args(argvs)
texts = [args.input_text]
return self.sentiment_classify(texts)
```
#### step 3\_6. Support the serving invoke.
If you want the module to support the PaddleHub Serving deployment prediction service, you need to provide a serving-modified interface that parses the incoming data, makes prediction, and returns the results.
If you do not want to provide the PaddleHub Serving deployment prediction service, you do not need to add the serving modification.
```python
@serving
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
```
### Complete Code
* [module.py](../../../modules/demo/senta_test/module.py)
* [processor.py](../../../modules/demo/senta_test/processor.py)
<br/>
## III. Install and test Module.
After writing a module, we can test it in the following ways:
### Call Method 1
Install the Module into the local machine, and then load it through Hub.Module(name=...)
```shell
➜ hub install senta_test
```
```python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
```
### Call Method 2
Load directly through Hub.Module(directory=...)
```python
import paddlehub as hub
senta_test = hub.Module(directory="senta_test/")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
```
### Call Method 3
Load SentaTest object directly by adding senta\_test as a path to the environment variable.
```shell
export PYTHONPATH=senta_test:$PYTHONPATH
```
```python
from senta_test.module import SentaTest
SentaTest.sentiment_classify(texts=["这部电影太差劲了"])
```
### Call Method 4
Install the Module on the local machine and run it through hub run.
```shell
➜ hub install senta_test
➜ hub run senta_test --input_text "这部电影太差劲了"
```
## IV. Release Module
After completing the development and testing of the module, if you want to share the model with others, you can release the model by **Upload the Module to the PaddleHub website**.
https://www.paddlepaddle.org.cn/hub
We will complete the review of the module and give feedback in the shortest possible time. After passing the review and going online, the module will be displayed on the PaddleHub website, and users can load it like any other official modules.
# FAQ
## Failed to install paddlehub via pip
`Could not find a version that satisfies the requirement paddlehub (from versions: )`
This may be because pip points to a pypi mirror source, which is not synchronized with the paddlehub version in time.
Try the following command:
```shell
$ pip install -i https://pypi.org/simple/ paddlehub
```
## When using paddlehub, raise an Exception like
`ModuleNotFoundError: No module named 'paddle'`
This is because PaddleHub depends on PaddlePaddle, and users need to install the appropriate PaddlePaddle version by themselves.
If the machine does not support GPU, use the following command to install the CPU version of PaddlePaddle:
```shell
$ pip install paddlepaddle
```
Or, use the following command to install the GPU version of PaddlePaddle:
```shell
$ pip install paddlepaddle-gpu
```
## Cannot download preset datasets and Modules.
You can use server_check() to check the connection status between the local and remote PaddleHub-Server
```python
import paddlehub
paddlehub.server_check()
# If the remote PaddleHub-Server can be connected, it will display Request Hub-Server successfully.
# Otherwise, it will display Request Hub-Server unsuccessfully.
```
## Does PaddleHub Module support multi-threading to speed up prediction?
Due to the limitations of PaddlePaddle itself, PaddleHub cannot speed up model prediction through multi-threaded concurrent execution.
## How to modify the default storage path of PaddleHub's Module?
Set environment variable ${HUB_HOME}.
# 声音分类
本示例展示如何使用PaddleHub Fine-tune API以及CNN14等预训练模型完成声音分类和Tagging的任务。
CNN14等预训练模型的详情,请参考论文[PANNs: Large-Scale Pretrained Audio Neural Networks for Audio Pattern Recognition](https://arxiv.org/pdf/1912.10211.pdf)和代码[audioset_tagging_cnn](https://github.com/qiuqiangkong/audioset_tagging_cnn)
## 如何开始Fine-tune
我们以环境声音分类公开数据集[ESC50](https://github.com/karolpiczak/ESC-50)为示例数据集,可以运行下面的命令,在训练集(train.npz)上进行模型训练,并在开发集(dev.npz)验证。通过如下命令,即可启动训练。
```python
# 设置使用的GPU卡号
export CUDA_VISIBLE_DEVICES=0
python train.py
```
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 选择模型
```python
import paddle
import paddlehub as hub
from paddlehub.datasets import ESC50
model = hub.Module(name='panns_cnn14', version='1.0.0', task='sound-cls', num_class=ESC50.num_class)
```
其中,参数:
- `name`: 模型名称,可以选择`panns_cnn14``panns_cnn10``panns_cnn6`,具体的模型参数信息可见下表。
- `version`: module版本号
- `task`:模型的执行任务。`sound-cls`表示声音分类任务;`None`表示Audio Tagging任务。
- `num_classes`:表示当前声音分类任务的类别数,根据具体使用的数据集确定。
目前可选用的预训练模型:
模型名 | PaddleHub Module
-----------| :------:
CNN14 | `hub.Module(name='panns_cnn14')`
CNN10 | `hub.Module(name='panns_cnn10')`
CNN6 | `hub.Module(name='panns_cnn6')`
### Step2: 加载数据集
```python
train_dataset = ESC50(mode='train')
dev_dataset = ESC50(mode='dev')
```
### Step3: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.AdamW(learning_rate=5e-5, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir='./', use_gpu=True)
```
#### 优化策略
Paddle2.0提供了多种优化器选择,如`SGD`, `AdamW`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/Overview_cn.html)
其中`AdamW`:
- `learning_rate`: 全局学习率。默认为1e-3;
- `parameters`: 待优化模型参数。
其余可配置参数请参考[AdamW](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/adamw/AdamW_cn.html#cn-api-paddle-optimizer-adamw)
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
- `model`: 被优化模型;
- `optimizer`: 优化器选择;
- `use_vdl`: 是否使用vdl可视化训练过程;
- `checkpoint_dir`: 保存模型参数的地址;
- `compare_metrics`: 保存最优模型的衡量指标;
### Step4: 执行训练和模型评估
```python
trainer.train(
train_dataset,
epochs=50,
batch_size=16,
eval_dataset=dev_dataset,
save_interval=10,
)
trainer.evaluate(dev_dataset, batch_size=16)
```
`trainer.train`执行模型的训练,其参数可以控制具体的训练过程,主要的参数包含:
- `train_dataset`: 训练时所用的数据集;
- `epochs`: 训练轮数;
- `batch_size`: 训练时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size;
- `num_workers`: works的数量,默认为0;
- `eval_dataset`: 验证集;
- `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
- `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
`trainer.evaluate`执行模型的评估,主要的参数包含:
- `eval_dataset`: 模型评估时所用的数据集;
- `batch_size`: 模型评估时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
以下代码将本地的音频文件`./cat.wav`作为预测数据,使用训好的模型对它进行分类,输出结果。
```python
import os
import librosa
import paddlehub as hub
from paddlehub.datasets import ESC50
wav = './cat.wav' # 存储在本地的需要预测的wav文件
sr = 44100 # 音频文件的采样率
checkpoint = './best_model/model.pdparams' # 模型checkpoint
label_map = {idx: label for idx, label in enumerate(ESC50.label_list)}
model = hub.Module(name='panns_cnn14',
version='1.0.0',
task='sound-cls',
num_class=ESC50.num_class,
label_map=label_map,
load_checkpoint=checkpoint)
data = [librosa.load(wav, sr=sr)[0]]
result = model.predict(data, sample_rate=sr, batch_size=1, feat_type='mel', use_gpu=True)
print(result[0]) # result[0]包含音频文件属于各类别的概率值
```
## Audio Tagging
当前使用的模型是基于[Audioset数据集](https://research.google.com/audioset/)的预训练模型,除了以上的针对特定声音分类数据集的finetune任务,模型还支持基于Audioset 527个标签的Tagging功能。
以下代码将本地的音频文件`./cat.wav`作为预测数据,使用预训练模型对它进行打分,输出top 10的标签和对应的得分。
```python
import os
import librosa
import numpy as np
import paddlehub as hub
from paddlehub.env import MODULE_HOME
wav = './cat.wav' # 存储在本地的需要预测的wav文件
sr = 44100 # 音频文件的采样率
topk = 10 # 展示音频得分前10的标签和分数
# 读取audioset数据集的label文件
label_file = os.path.join(MODULE_HOME, 'panns_cnn14', 'audioset_labels.txt')
label_map = {}
with open(label_file, 'r') as f:
for i, l in enumerate(f.readlines()):
label_map[i] = l.strip()
model = hub.Module(name='panns_cnn14', version='1.0.0', task=None, label_map=label_map)
data = [librosa.load(wav, sr=sr)[0]]
result = model.predict(data, sample_rate=sr, batch_size=1, feat_type='mel', use_gpu=True)
# 打印topk的类别和对应得分
msg = ''
for label, score in list(result[0].items())[:topk]:
msg += f'{label}: {score}\n'
print(msg)
```
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.1.0
# 图像分类
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 命令行预测
```shell
$ hub run resnet50_vd_imagenet_ssld --input_path "/PATH/TO/IMAGE" --top_k 5
```
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用resnet50_vd_imagenet_ssld对[Flowers](../../docs/reference/datasets.md#class-hubdatasetsflowers)等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
import paddlehub.vision.transforms as T
transforms = T.Compose([T.Resize((256, 256)),
T.CenterCrop(224),
T.Normalize(mean=[0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])],
to_rgb=True)
```
`transforms` 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```python
from paddlehub.datasets import Flowers
flowers = Flowers(transforms)
flowers_validate = Flowers(transforms, mode='val')
```
* `transforms`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, `val`, 默认为`train`
数据集的准备代码可以参考 [flowers.py](../../paddlehub/datasets/flowers.py)`hub.datasets.Flowers()` 会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
module = hub.Module(name="resnet50_vd_imagenet_ssld", label_list=["roses", "tulips", "daisy", "sunflowers", "dandelion"])
```
* `name`: 选择预训练模型的名字。
* `label_list`: 设置输出分类类别,默认为Imagenet2012类别。
PaddleHub提供许多图像分类预训练模型,如xception、mobilenet、efficientnet等,详细信息参见[图像分类模型](https://www.paddlepaddle.org.cn/hub?filter=en_category&value=ImageClassification)
如果想尝试efficientnet模型,只需要更换Module中的`name`参数即可.
```python
# 更换name参数即可无缝切换efficientnet模型, 代码示例如下
module = hub.Module(name="efficientnetb7_imagenet")
```
**NOTE:**目前部分模型还没有完全升级到2.0版本,敬请期待。
### Step4: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='img_classification_ckpt')
trainer.train(flowers, epochs=100, batch_size=32, eval_dataset=flowers_validate, save_interval=1)
```
#### 优化策略
Paddle2.0-rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`Adam`:
* `learning_rate`: 全局学习率。默认为1e-3;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='resnet50_vd_imagenet_ssld', label_list=["roses", "tulips", "daisy", "sunflowers", "dandelion"], load_checkpoint='/PATH/TO/CHECKPOINT')
result = model.predict(['flower.jpg'])
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线分类任务服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m resnet50_vd_imagenet_ssld
```
这样就完成了一个分类任务服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)], 'top_k':2}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/resnet50_vd_imagenet_ssld"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
data =r.json()["results"]['data']
```
### 查看代码
https://github.com/PaddlePaddle/models/tree/develop/PaddleCV/image_classification
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 图像着色
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 命令行预测
```
$ hub run user_guided_colorization --input_path "/PATH/TO/IMAGE"
```
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用user_guided_colorization模型对[Canvas](../../docs/reference/datasets.md#class-hubdatasetsCanvas)等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
import paddlehub.vision.transforms as T
transform = T.Compose([T.Resize((256, 256), interpolation='NEAREST'),
T.RandomPaddingCrop(crop_size=176),
T.RGB2LAB()], to_rgb=True)
```
`transforms`数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
**NOTE:** 要将`T.Compose``to_rgb`设定为True.
### Step2: 下载数据集并使用
```python
from paddlehub.datasets import Canvas
color_set = Canvas(transform=transform, mode='train')
```
* `transform`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test` 默认为`train`
数据集的准备代码可以参考 [canvas.py](../../paddlehub/datasets/canvas.py)`hub.datasets.Canvas()` 会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
model = hub.Module(name='user_guided_colorization', load_checkpoint=None)
model.set_config(classification=True, prob=1)
```
* `name`:加载模型的名字。
* `load_checkpoint`: 是否加载自己训练的模型,若为None,则加载提供的模型默认参数。
* `classification`: 着色模型分两部分训练,开始阶段`classification`设置为True, 用于浅层网络训练。训练后期将`classification`设置为False, 用于训练网络的输出层。
* `prob`: 每张输入图不加一个先验彩色块的概率,默认为1,即不加入先验彩色块。例如,当`prob`设定为0.9时,一张图上有两个先验彩色块的概率为(1-0.9)*(1-0.9)*0.9=0.009.
### Step4: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.Adam(learning_rate=0.0001, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='img_colorization_ckpt_cls_1')
trainer.train(color_set, epochs=201, batch_size=25, eval_dataset=color_set, log_interval=10, save_interval=10)
```
#### 优化策略
Paddle2.0-rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`Adam`:
* `learning_rate`: 全局学习率。默认为1e-4;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证过程所用的数据集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='user_guided_colorization', load_checkpoint='/PATH/TO/CHECKPOINT')
model.set_config(prob=0.1)
result = model.predict(images=['house.png'])
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。若想获取油画风着色效果,请下载参数文件[油画着色](https://paddlehub.bj.bcebos.com/dygraph/models/canvas_rc.pdparams)
**Args**
* `images`:原始图像路径或者BGR格式图片;
* `visualization`: 是否可视化,默认为True;
* `save_path`: 保存结果的路径,默认为'result'。
## 服务部署
PaddleHub Serving可以部署一个在线着色任务服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m user_guided_colorization
```
这样就完成了一个着色任务服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/user_guided_colorization"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
data = base64_to_cv2(r.json()["results"]['data'][0]['fake_reg'])
cv2.imwrite('color.png', data)
```
### 查看代码
https://github.com/richzhang/colorization-pytorch
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 图像分割
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用ocrnet_hrnetw18_voc模型对OpticDiscSeg等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
from paddlehub.vision.segmentation_transforms import Compose, Resize, Normalize
transform = Compose([Resize(target_size=(512, 512)), Normalize()])
```
`segmentation_transforms` 数据增强模块定义了丰富的针对图像分割数据的预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```python
from paddlehub.datasets import OpticDiscSeg
train_reader = OpticDiscSeg(transform mode='train')
```
* `transform`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, `val`, 默认为`train`
数据集的准备代码可以参考 [opticdiscseg.py](../../paddlehub/datasets/opticdiscseg.py)`hub.datasets.OpticDiscSeg()`会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
model = hub.Module(name='ocrnet_hrnetw18_voc', num_classes=2, pretrained=None)
```
* `name`: 选择预训练模型的名字。
* `num_classes`: 分割模型的类别数目。
* `pretrained`: 是否加载自己训练的模型,若为None,则加载提供的模型默认参数。
### Step4: 选择优化策略和运行配置
```python
scheduler = paddle.optimizer.lr.PolynomialDecay(learning_rate=0.01, decay_steps=1000, power=0.9, end_lr=0.0001)
optimizer = paddle.optimizer.Adam(learning_rate=scheduler, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='test_ckpt_img_ocr', use_gpu=True)
```
#### 优化策略
其中`Adam`:
* `learning_rate`: 全局学习率。
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_gpu`: 是否使用gpu,默认为False;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import cv2
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='ocrnet_hrnetw18_voc', pretrained='/PATH/TO/CHECKPOINT')
img = cv2.imread("/PATH/TO/IMAGE")
model.predict(images=[img], visualization=True)
```
参数配置正确后,请执行脚本`python predict.py`
**Args**
* `images`:原始图像路径或BGR格式图片;
* `visualization`: 是否可视化,默认为True;
* `save_path`: 保存结果的路径,默认保存路径为'seg_result'。
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线图像分割服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m ocrnet_hrnetw18_voc
```
这样就完成了一个图像分割服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/IMAGE')
data = {'images':[cv2_to_base64(org_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/ocrnet_hrnetw18_voc"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
mask = base64_to_cv2(r.json()["results"][0])
```
### 查看代码
https://github.com/PaddlePaddle/PaddleSeg
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
# 序列标注
在2017年之前,工业界和学术界对NLP文本处理依赖于序列模型[Recurrent Neural Network (RNN)](https://baike.baidu.com/item/%E5%BE%AA%E7%8E%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/23199490?fromtitle=RNN&fromid=5707183&fr=aladdin).
![](http://colah.github.io/posts/2015-09-NN-Types-FP/img/RNN-general.png)
近年来随着深度学习的发展,模型参数数量飞速增长,为了训练这些参数,需要更大的数据集来避免过拟合。然而,对于大部分NLP任务来说,构建大规模的标注数据集成本过高,非常困难,特别是对于句法和语义相关的任务。相比之下,大规模的未标注语料库的构建则相对容易。最近的研究表明,基于大规模未标注语料库的预训练模型(Pretrained Models, PTM) 能够习得通用的语言表示,将预训练模型Fine-tune到下游任务,能够获得出色的表现。另外,预训练模型能够避免从零开始训练模型。
![](https://ai-studio-static-online.cdn.bcebos.com/327f44ff3ed24493adca5ddc4dc24bf61eebe67c84a6492f872406f464fde91e)
本示例将展示如何使用PaddleHub Transformer模型(如 ERNIE、BERT、RoBERTa等模型)Module 以动态图方式fine-tune并完成预测任务。
## 如何开始Fine-tune
我们以微软亚洲研究院发布的中文实体识别数据集MSRA-NER为示例数据集,可以运行下面的命令,在训练集(train.tsv)上进行模型训练,并在开发集(dev.tsv)验证。通过如下命令,即可启动训练。
```shell
# 设置使用的GPU卡号
export CUDA_VISIBLE_DEVICES=0
python train.py
```
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 选择模型
在命名实体识别的任务中,因不同的数据集标识实体的标签不同,评测的方式也有所差异。因此,在初始化模型的之前,需要先确定实际标签的形式,下方的`label_list`则是MSRA-NER数据集中使用的标签类别。
如果用户使用的实体识别的数据集的标签方式与MSRA-NER不同,则需要自行根据数据集确定。
```python
label_list = hub.datasets.MSRA_NER.label_list
label_map = {
idx: label for idx, label in enumerate(label_list)
}
```
接下来创建任务所使用的`model`
```python
import paddlehub as hub
model = hub.Module(name='ernie_tiny', version='2.0.1', task='token-cls', label_map=label_map)
```
其中,参数:
* `name`:模型名称,可以选择`ernie``ernie_tiny``bert-base-cased``bert-base-chinese`, `roberta-wwm-ext``roberta-wwm-ext-large`等。
* `version`:module版本号
* `task`:fine-tune任务。此处为`token-cls`,表示序列标注任务。
* `label_map`:数据集中的标签信息,实体识别任务中需要根据不同标签种类对模型性能进行评价。
PaddleHub还提供BERT等模型可供选择, 当前支持序列标注任务的模型对应的加载示例如下:
模型名 | PaddleHub Module
---------------------------------- | :------:
ERNIE, Chinese | `hub.Module(name='ernie')`
ERNIE tiny, Chinese | `hub.Module(name='ernie_tiny')`
ERNIE 2.0 Base, English | `hub.Module(name='ernie_v2_eng_base')`
ERNIE 2.0 Large, English | `hub.Module(name='ernie_v2_eng_large')`
BERT-Base, English Cased | `hub.Module(name='bert-base-cased')`
BERT-Base, English Uncased | `hub.Module(name='bert-base-uncased')`
BERT-Large, English Cased | `hub.Module(name='bert-large-cased')`
BERT-Large, English Uncased | `hub.Module(name='bert-large-uncased')`
BERT-Base, Multilingual Cased | `hub.Module(nane='bert-base-multilingual-cased')`
BERT-Base, Multilingual Uncased | `hub.Module(nane='bert-base-multilingual-uncased')`
BERT-Base, Chinese | `hub.Module(name='bert-base-chinese')`
BERT-wwm, Chinese | `hub.Module(name='chinese-bert-wwm')`
BERT-wwm-ext, Chinese | `hub.Module(name='chinese-bert-wwm-ext')`
RoBERTa-wwm-ext, Chinese | `hub.Module(name='roberta-wwm-ext')`
RoBERTa-wwm-ext-large, Chinese | `hub.Module(name='roberta-wwm-ext-large')`
RBT3, Chinese | `hub.Module(name='rbt3')`
RBTL3, Chinese | `hub.Module(name='rbtl3')`
ELECTRA-Small, English | `hub.Module(name='electra-small')`
ELECTRA-Base, English | `hub.Module(name='electra-base')`
ELECTRA-Large, English | `hub.Module(name='electra-large')`
ELECTRA-Base, Chinese | `hub.Module(name='chinese-electra-base')`
ELECTRA-Small, Chinese | `hub.Module(name='chinese-electra-small')`
通过以上的一行代码,`model`初始化为一个适用于序列标注任务的模型,为ERNIE Tiny的预训练模型后拼接上一个输出token共享的全连接网络(Full Connected)。
![](https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=224484727,3049769188&fm=15&gp=0.jpg)
以上图片来自于:https://arxiv.org/pdf/1810.04805.pdf
### Step2: 下载并加载数据集
```python
train_dataset = hub.datasets.MSRA_NER(
tokenizer=model.get_tokenizer(), max_seq_len=128, mode='train')
dev_dataset = hub.datasets.MSRA_NER(
tokenizer=model.get_tokenizer(), max_seq_len=128, mode='dev')
```
* `tokenizer`:表示该module所需用到的tokenizer,其将对输入文本完成切词,并转化成module运行所需模型输入格式。
* `mode`:选择数据模式,可选项有 `train`, `test`, `val`, 默认为`train`
* `max_seq_len`:ERNIE/BERT模型使用的最大序列长度,若出现显存不足,请适当调低这一参数。
预训练模型ERNIE对中文数据的处理是以字为单位,tokenizer作用为将原始输入文本转化成模型model可以接受的输入数据形式。 PaddleHub 2.0中的各种预训练模型已经内置了相应的tokenizer,可以通过`model.get_tokenizer`方法获取。
![](https://bj.bcebos.com/paddlehub/paddlehub-img/ernie_network_1.png)
![](https://bj.bcebos.com/paddlehub/paddlehub-img/ernie_network_2.png)
### Step3: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.AdamW(learning_rate=5e-5, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir='test_ernie_token_cls', use_gpu=False)
trainer.train(train_dataset, epochs=3, batch_size=32, eval_dataset=dev_dataset)
# 在测试集上评估当前训练模型
trainer.evaluate(test_dataset, batch_size=32)
```
#### 优化策略
Paddle2.0-rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`, `AdamW`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`AdamW`:
* `learning_rate`: 全局学习率。默认为1e-3;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_gpu`: 是否使用GPU训练,默认为False;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: workers的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们以以下数据为待预测数据,使用该模型来进行预测
```text
去年十二月二十四日,市委书记张敬涛召集县市主要负责同志研究信访工作时,提出三问:『假如上访群众是我们的父母姐妹,你会用什么样的感情对待他们?
新华社北京5月7日电国务院副总理李岚清今天在中南海会见了美国前商务部长芭芭拉·弗兰克林。
根据测算,海卫1表面温度已经从“旅行者”号探测器1989年造访时的零下236摄氏度上升到零下234摄氏度。
华裔作家韩素音女士曾三次到大足,称“大足石窟是一座未被开发的金矿”。
```
```python
import paddlehub as hub
split_char = "\002"
label_list = ["B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "O"]
text_a = [
'去年十二月二十四日,市委书记张敬涛召集县市主要负责同志研究信访工作时,提出三问:『假如上访群众是我们的父母姐妹,你会用什么样的感情对待他们?',
'新华社北京5月7日电国务院副总理李岚清今天在中南海会见了美国前商务部长芭芭拉·弗兰克林。',
'根据测算,海卫1表面温度已经从“旅行者”号探测器1989年造访时的零下236摄氏度上升到零下234摄氏度。',
'华裔作家韩素音女士曾三次到大足,称“大足石窟是一座未被开发的金矿”。',
]
data = [[split_char.join(text)] for text in text_a]
label_map = {
idx: label for idx, label in enumerate(label_list)
}
model = hub.Module(
name='ernie_tiny',
version='2.0.1',
task='token-cls',
load_checkpoint='./token_cls_save_dir/best_model/model.pdparams',
label_map=label_map,
)
results = model.predict(data, max_seq_len=50, batch_size=1, use_gpu=False)
for idx, text in enumerate(text_a):
print(f'Data: {text} \t Lable: {", ".join(results[idx][1:len(text)+1])}')
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 风格迁移
本示例将展示如何使用PaddleHub对预训练模型进行finetune并完成预测任务。
## 命令行预测
```
$ hub run msgnet --input_path "/PATH/TO/ORIGIN/IMAGE" --style_path "/PATH/TO/STYLE/IMAGE"
```
## 如何开始Fine-tune
在完成安装PaddlePaddle与PaddleHub后,通过执行`python train.py`即可开始使用msgnet模型对[MiniCOCO](../../docs/reference/datasets.md#class-hubdatasetsMiniCOCO)等数据集进行Fine-tune。
## 代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```python
import paddlehub.vision.transforms as T
transform = T.Compose([T.Resize((256, 256), interpolation='LINEAR')])
```
`transforms` 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```python
from paddlehub.datasets.minicoco import MiniCOCO
styledata = MiniCOCO(transform=transform, mode='train')
```
* `transforms`: 数据预处理方式。
* `mode`: 选择数据模式,可选项有 `train`, `test`, 默认为`train`
数据集的准备代码可以参考 [minicoco.py](../../paddlehub/datasets/flowers.py)`hub.datasets.MiniCOCO()`会自动从网络下载数据集并解压到用户目录下`$HOME/.paddlehub/dataset`目录。
### Step3: 加载预训练模型
```python
model = hub.Module(name='msgnet', load_checkpoint=None)
```
* `name`: 选择预训练模型的名字。
* `load_checkpoint`: 是否加载自己训练的模型,若为None,则加载提供的模型默认参数。
### Step4: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.Adam(learning_rate=0.0001, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='test_style_ckpt')
trainer.train(styledata, epochs=101, batch_size=4, eval_dataset=styledata, log_interval=10, save_interval=10)
```
#### 优化策略
Paddle2.0rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html)
其中`Adam`:
* `learning_rate`: 全局学习率。默认为1e-4;
* `parameters`: 待优化模型参数。
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
* `model`: 被优化模型;
* `optimizer`: 优化器选择;
* `use_vdl`: 是否使用vdl可视化训练过程;
* `checkpoint_dir`: 保存模型参数的地址;
* `compare_metrics`: 保存最优模型的衡量指标;
`trainer.train` 主要控制具体的训练过程,包含以下可控制的参数:
* `train_dataset`: 训练时所用的数据集;
* `epochs`: 训练轮数;
* `batch_size`: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
* `num_workers`: works的数量,默认为0;
* `eval_dataset`: 验证集;
* `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
* `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```python
import paddle
import paddlehub as hub
if __name__ == '__main__':
model = hub.Module(name='msgnet', load_checkpoint="/PATH/TO/CHECKPOINT")
result = model.predict(origin=["venice-boat.jpg"], style="candy.jpg", visualization=True, save_path ='style_tranfer')
```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
**Args**
* `origin`:原始图像路径或BGR格式图片;
* `style`: 风格图像路径;
* `visualization`: 是否可视化,默认为True;
* `save_path`: 保存结果的路径,默认保存路径为'style_tranfer'。
**NOTE:** 进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线风格迁移服务。
### Step1: 启动PaddleHub Serving
运行启动命令:
```shell
$ hub serving start -m msgnet
```
这样就完成了一个风格迁移服务化API的部署,默认端口号为8866。
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
### Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
import cv2
import base64
import numpy as np
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data
# 发送HTTP请求
org_im = cv2.imread('/PATH/TO/ORIGIN/IMAGE')
style_im = cv2.imread('/PATH/TO/STYLE/IMAGE')
data = {'images':[[cv2_to_base64(org_im)], cv2_to_base64(style_im)]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/msgnet"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
data = base64_to_cv2(r.json()["results"]['data'][0])
cv2.imwrite('style.png', data)
```
### 查看代码
https://github.com/zhanghang1989/PyTorch-Multi-Style-Transfer
### 依赖
paddlepaddle >= 2.0.0rc
paddlehub >= 2.0.0
# 文本匹配
在2017年之前,工业界和学术界对NLP文本处理依赖于序列模型[Recurrent Neural Network (RNN)](https://baike.baidu.com/item/%E5%BE%AA%E7%8E%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/23199490?fromtitle=RNN&fromid=5707183&fr=aladdin).
![](http://colah.github.io/posts/2015-09-NN-Types-FP/img/RNN-general.png)
近年来随着深度学习的发展,模型参数数量飞速增长,为了训练这些参数,需要更大的数据集来避免过拟合。然而,对于大部分NLP任务来说,构建大规模的标注数据集成本过高,非常困难,特别是对于句法和语义相关的任务。相比之下,大规模的未标注语料库的构建则相对容易。最近的研究表明,基于大规模未标注语料库的预训练模型(Pretrained Models, PTM) 能够习得通用的语言表示,将预训练模型Fine-tune到下游任务,能够获得出色的表现。另外,预训练模型能够避免从零开始训练模型。
![](https://ai-studio-static-online.cdn.bcebos.com/327f44ff3ed24493adca5ddc4dc24bf61eebe67c84a6492f872406f464fde91e)
本示例将展示如何使用PaddleHub Transformer模型(如 ERNIE、BERT、RoBERTa等模型)Module 以动态图方式fine-tune并完成预测任务。
## 文本匹配
使用预训练模型ERNIE完成文本匹配任务,大家可能会想到将query和title文本拼接,之后输入ERNIE中,取`CLS`特征(pooled_output),之后输出全连接层,进行二分类。如下图ERNIE用于句对分类任务的用法:
![](https://camo.githubusercontent.com/5e1867ee2b6fc3a0f94c7b2c87a4d987fed4c440d4d9c80726e5798900880027/68747470733a2f2f61692d73747564696f2d7374617469632d6f6e6c696e652e63646e2e626365626f732e636f6d2f34353434303032396330373234306164383964363635633562313736653633323937653935383465316461323465303262373964643534666239393066373461)
然而,以上用法的问题在于,ERNIE的模型参数非常庞大,导致计算量非常大,预测的速度也不够理想。从而达不到线上业务的要求。针对该问题,使用Sentence Transformer网络可以优化计算量。
Sentence Transformer采用了双塔(Siamese)的网络结构。Query和Title分别输入Transformer网络,共享网络参数,得到各自的token embedding特征。之后对token embedding进行pooling(此处教程使用mean pooling操作),之后输出分别记作u,v。之后将三个表征(u,v,|u-v|)拼接起来,进行二分类。网络结构如下图所示。
![](https://camo.githubusercontent.com/80e65553f0c82886a27897a0a151ee9745e6e2def310d6649c8a68e2672c06c2/68747470733a2f2f61692d73747564696f2d7374617469632d6f6e6c696e652e63646e2e626365626f732e636f6d2f31303339393837303365313334613731383438383335313161353338363230653136666564303435653236313464636338616661636563343436363030343338)
更多关于Sentence Transformer的信息可以参考论文:https://arxiv.org/abs/1908.10084
## 如何开始Fine-tune
我们以中文文本匹配数据集LCQMC为示例数据集,可以运行下面的命令,在训练集(train.tsv)上进行模型训练,并在开发集(dev.tsv)验证和测试集测试(test.tsv)。通过如下命令,即可启动训练。
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 选择模型
```python
import paddlehub as hub
model = hub.Module(name='ernie_tiny', version='2.0.2', task='text-matching')
```
其中,参数:
* `name`:模型名称,可以选择`ernie``ernie_tiny``bert-base-cased``bert-base-chinese`, `roberta-wwm-ext``roberta-wwm-ext-large`等。
* `version`:module版本号
* `task`:fine-tune任务。此处为`text-matching`,表示文本匹配任务。
PaddleHub还提供BERT等模型可供选择, 当前支持文本分类任务的模型对应的加载示例如下:
模型名 | PaddleHub Module
---------------------------------- | :------:
ERNIE, Chinese | `hub.Module(name='ernie')`
ERNIE tiny, Chinese | `hub.Module(name='ernie_tiny')`
ERNIE 2.0 Base, English | `hub.Module(name='ernie_v2_eng_base')`
ERNIE 2.0 Large, English | `hub.Module(name='ernie_v2_eng_large')`
BERT-Base, English Cased | `hub.Module(name='bert-base-cased')`
BERT-Base, English Uncased | `hub.Module(name='bert-base-uncased')`
BERT-Large, English Cased | `hub.Module(name='bert-large-cased')`
BERT-Large, English Uncased | `hub.Module(name='bert-large-uncased')`
BERT-Base, Multilingual Cased | `hub.Module(nane='bert-base-multilingual-cased')`
BERT-Base, Multilingual Uncased | `hub.Module(nane='bert-base-multilingual-uncased')`
BERT-Base, Chinese | `hub.Module(name='bert-base-chinese')`
BERT-wwm, Chinese | `hub.Module(name='chinese-bert-wwm')`
BERT-wwm-ext, Chinese | `hub.Module(name='chinese-bert-wwm-ext')`
RoBERTa-wwm-ext, Chinese | `hub.Module(name='roberta-wwm-ext')`
RoBERTa-wwm-ext-large, Chinese | `hub.Module(name='roberta-wwm-ext-large')`
RBT3, Chinese | `hub.Module(name='rbt3')`
RBTL3, Chinese | `hub.Module(name='rbtl3')`
ELECTRA-Small, English | `hub.Module(name='electra-small')`
ELECTRA-Base, English | `hub.Module(name='electra-base')`
ELECTRA-Large, English | `hub.Module(name='electra-large')`
ELECTRA-Base, Chinese | `hub.Module(name='chinese-electra-base')`
ELECTRA-Small, Chinese | `hub.Module(name='chinese-electra-small')`
通过以上的一行代码,`model`初始化为一个适用于文本匹配任务的双塔(Siamese)结构模型,。
### Step2: 下载并加载数据集
```python
train_dataset = LCQMC(tokenizer=model.get_tokenizer(), max_seq_len=128, mode='train')
dev_dataset = LCQMC(tokenizer=model.get_tokenizer(), max_seq_len=128, mode='dev')
test_dataset = LCQMC(tokenizer=model.get_tokenizer(), max_seq_len=128, mode='test')
```
* `tokenizer`:表示该module所需用到的tokenizer,其将对输入文本完成切词,并转化成module运行所需模型输入格式。
* `mode`:选择数据模式,可选项有 `train`, `dev`, `test`,默认为`train`
* `max_seq_len`:ERNIE/BERT模型使用的最大序列长度,若出现显存不足,请适当调低这一参数。
预训练模型ERNIE对中文数据的处理是以字为单位,tokenizer作用为将原始输入文本转化成模型model可以接受的输入数据形式。 PaddleHub 2.0中的各种预训练模型已经内置了相应的tokenizer,可以通过`model.get_tokenizer`方法获取。
### Step3: 选择优化策略和运行配置
```python
optimizer = paddle.optimizer.AdamW(learning_rate=5e-5, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir='./', use_gpu=True)
```
#### 优化策略
Paddle2.0提供了多种优化器选择,如`SGD`, `AdamW`, `Adamax`等,详细参见[策略](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/Overview_cn.html)
其中`AdamW`:
- `learning_rate`: 全局学习率。默认为1e-3;
- `parameters`: 待优化模型参数。
其余可配置参数请参考[AdamW](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/adamw/AdamW_cn.html#cn-api-paddle-optimizer-adamw)
#### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
- `model`: 被优化模型;
- `optimizer`: 优化器选择;
- `use_vdl`: 是否使用vdl可视化训练过程;
- `checkpoint_dir`: 保存模型参数的地址;
- `compare_metrics`: 保存最优模型的衡量指标;
### Step4: 执行训练和模型评估
```python
trainer.train(
train_dataset,
epochs=10,
batch_size=32,
eval_dataset=dev_dataset,
save_interval=2,
)
trainer.evaluate(test_dataset, batch_size=32)
```
`trainer.train`执行模型的训练,其参数可以控制具体的训练过程,主要的参数包含:
- `train_dataset`: 训练时所用的数据集;
- `epochs`: 训练轮数;
- `batch_size`: 训练时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size;
- `num_workers`: works的数量,默认为0;
- `eval_dataset`: 验证集;
- `log_interval`: 打印日志的间隔, 单位为执行批训练的次数。
- `save_interval`: 保存模型的间隔频次,单位为执行训练的轮数。
`trainer.evaluate`执行模型的评估,主要的参数包含:
- `eval_dataset`: 模型评估时所用的数据集;
- `batch_size`: 模型评估时每一步用到的样本数目,如果使用GPU,请根据实际情况调整batch_size
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在`${CHECKPOINT_DIR}/best_model`目录下,其中`${CHECKPOINT_DIR}`目录为Fine-tune时所选择的保存checkpoint的目录。
以下代码将使用最优模型来进行预测:
```python
import paddlehub as hub
data = [
['这个表情叫什么', '这个猫的表情叫什么'],
['什么是智能手环', '智能手环有什么用'],
['介绍几本好看的都市异能小说,要完结的!', '求一本好看点的都市异能小说,要完结的'],
['一只蜜蜂落在日历上(打一成语)', '一只蜜蜂停在日历上(猜一成语)'],
['一盒香烟不拆开能存放多久?', '一条没拆封的香烟能存放多久。'],
]
label_map = {0: 'similar', 1: 'dissimilar'}
model = hub.Module(
name='ernie_tiny',
version='2.0.2',
task='text-matching',
load_checkpoint='./checkpoint/best_model/model.pdparams',
label_map=label_map)
results = model.predict(data, max_seq_len=128, batch_size=1, use_gpu=True)
for idx, texts in enumerate(data):
print('TextA: {}\tTextB: {}\t Label: {}'.format(texts[0], texts[1], results[idx]))
```
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
# Installation ============
Installation
============
## Environment Dependency
* Python>=3.6 Environment Dependency
* PaddlePaddle>=2.0.0rc ========================
```shell
# Install gpu version of paddlepaddle
pip install paddlepaddle-gpu==2.0.0rc
# Or install cpu version of paddlepaddle
# pip install paddlepaddle==2.0.0rc
```
* Operating System: Windows/Mac/Linux * Operating System: Windows/Mac/Linux
* Python>=3.6
* PaddlePaddle>=2.0.0
.. code-block:: shell
## Installation Command # Install gpu version of paddlepaddle
pip install paddlepaddle-gpu -U
Before installing the PaddleHub, install the PaddlePaddle deep learning framework first. For more installation instructions, refer to [PaddleQuickInstall](https://www.paddlepaddle.org.cn/install/quick/zh/2.0rc-windows-pip) # Or install cpu version of paddlepaddle
# pip install paddlepaddle -U
```shell Installation Command
pip install paddlehub==2.0.0rc0 ========================
```
Before installing the PaddleHub, install the PaddlePaddle deep learning framework first. For more installation instructions, refer to `PaddleQuickInstall <https://www.paddlepaddle.org.cn/install/quick>`_.
.. code-block:: shell
pip install paddlehub==2.1.0
In addition to the above dependences, PaddleHub's pre-training models and pre-set datasets need to be downloaded through connecting to the server. Make sure that the computer can access the network. You can run PaddleHub offline if the relevant datasets and pre-set models are already available locally. In addition to the above dependences, PaddleHub's pre-training models and pre-set datasets need to be downloaded through connecting to the server. Make sure that the computer can access the network. You can run PaddleHub offline if the relevant datasets and pre-set models are already available locally.
**NOTE:** Make sure that the computer can access the external network when the PaddleHub is used to download datasets and pre-training models. You can use `server_check()` to check the connection status between the local and remote PaddleHub-Server in the following methods: .. note::
Make sure that the computer can access the external network when the PaddleHub is used to download datasets and pre-training models. You can use `server_check()` to check the connection status between the local and remote PaddleHub-Server in the following methods:
.. code-block:: Python
```python import paddlehub
import paddlehub paddlehub.server_check()
paddlehub.server_check() # If OK, reply "Request Hub-Server successfully".
# If OK, reply "Request Hub-Server successfully". # If not OK, reply "Hub-Server unsuccessfully".
# If not OK, reply "Hub-Server unsuccessfully". \ No newline at end of file
```
=================
Quick experience
=================
In PaddleHub, the concept `Module` represents an executable module, which usually a pre-trained model that can be used for end-to-end prediction, such as a face detection model or a lexical analysis model, or a pre-trained model that requires finetuning, such as BERT/ERNIE.
PaddleHub adopts the model-based software design concept. All pre-training models are similar to Python packages, with the concept of version.
This tutorial will take you to quickly experience how to use the pre-trained model provided by PaddleHub to quickly complete several common AI tasks.
Download test image
=============================================
First of all, let's download a test image through `wget`.
.. code-block:: shell
# Download a picture
wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
.. image:: ../../imgs/humanseg_test.png
:width: 300px
Human segmentation
=============================================
Human segmentation aims to distinguish the person and the background in the input image. This task has many application scenarios, such as background blur, background replacement, film and television post-processing, etc. We use `humanseg_lite <https://www.paddlepaddle.org.cn/hubdetail?name=humanseg_lite&en_category=ImageSegmentation>`_ to show this feature.
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="humanseg_lite", version="1.1.1")
module = hub.Module(name="humanseg_lite")
res = module.segmentation(
paths = ["./test_image.jpg"],
visualization=True,
output_dir='humanseg_output')
.. image:: ../../imgs/output_8_3.png
:width: 300px
Human parsing
=============================================
Human parsing is a more detailed task of human segmentation. It aims to extract and distinguish different parts of the human body in the input photo. It is often used with many emerging GAN models. Application scenarios include beauty, changing clothes, etc. We use `ace2p <https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation>`_ to show this feature.
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="ace2p", version="1.1.0")
module = hub.Module(name="ace2p")
res = module.segment(
paths = ["./test_image.jpg"],
visualization=True,
output_dir='ace2p_output')
.. image:: ../../imgs/output_12_3.png
:width: 300px
Face Detection
=============================================
The task of face detection aims to detect all the faces in the input picture and give the specific coordinates and size of the faces (generally called the bounding box). Application scenarios include video surveillance, traffic analysis, etc. We use `ultra_light_fast_generic_face_detector_1mb_640 <https://www.paddlepaddle.org.cn/hubdetail?name=ultra_light_fast_generic_face_detector_1mb_640&en_category=FaceDetection>`_ to show this feature.
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640", version="1.1.2")
module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
res = module.face_detection(
paths = ["./test_image.jpg"],
visualization=True,
output_dir='face_detection_output')
.. image:: ../../imgs/output_15_3.png
:width: 300px
Key Point Detection
=============================================
The key point detection task aims to identify different key points of each human body in the input image, such as the head, shoulders, joints, etc. The number of key points that can be identified is also different depending on the model's ability. This task can be used for human body beauty, human body gesture recognition and other tasks. We use `openpose_body_estimation <https://www.paddlepaddle.org.cn/hubdetail?name=openpose_body_estimation&en_category=KeyPointDetection>`_ to show this feature.
.. code-block:: python
import paddlehub as hub
# module = hub.Module(name="openpose_body_estimation", version="1.0.0")
module = hub.Module(name="openpose_body_estimation")
res = module.predict(
img="./test_image.jpg",
visualization=True,
save_path='keypoint_output')
.. image:: ../../imgs/output_18_2.png
:width: 300px
Lexical analysis of Chinese
=============================================
Chinese lexical analysis aims to perform tasks such as word segmentation, part-of-speech analysis, and named entity recognition on the input Chinese sentences. We use `lac <https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis>`_ to show this feature.
.. code-block:: python
import paddlehub as hub
# lac = hub.Module(name="lac", version="2.2.0")
lac = hub.Module(name="lac")
test_text = ["1996年,曾经是微软员工的加布·纽维尔和麦克·哈灵顿一同创建了Valve软件公司。他们在1996年下半年从id software取得了雷神之锤引擎的使用许可,用来开发半条命系列。"]
print(lac.lexical_analysis(texts = test_text))
----------------
[{'word': ['1996年', ',', '曾经', '是', '微软', '员工', '的', '加布·纽维尔', '和', '麦克·哈灵顿', '一同', '创建', '了', 'Valve软件公司', '。', '他们', '在', '1996年下半年', '从', 'id', ' ', 'software', '取得', '了', '雷神之锤', '引擎', '的', '使用', '许可', ',', '用来', '开发', '半条命', '系列', '。'], 'tag': ['TIME', 'w', 'd', 'v', 'ORG', 'n', 'u', 'PER', 'c', 'PER', 'd', 'v', 'u', 'ORG', 'w', 'r', 'p', 'TIME', 'p', 'nz', 'w', 'n', 'v', 'u', 'n', 'n', 'u', 'vn', 'vn', 'w', 'v', 'v', 'n', 'n', 'w']}]
Sentiment analysis of Chinese
=============================================
Chinese sentiment analysis aims to analyze the sentiment tendency of input Chinese sentences. We use `senta_bilstm <https://www.paddlepaddle.org.cn/hubdetail?name=senta_bilstm&en_category=SentimentAnalysis>`_ to show this feature.
.. code-block:: python
import paddlehub as hub
# senta = hub.Module(name="senta_bilstm", version="1.2.0")
senta = hub.Module(name="senta_bilstm")
test_text = ["味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。"]
print(senta.sentiment_classify(texts = test_text))
----------------
[{'text': '味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9771, 'negative_probs': 0.0229}]
\ No newline at end of file
===============================
Get start with PaddleHub
===============================
.. toctree::
:maxdepth: 2
get_start/installation.md
get_start/python_use_hub.md
\ No newline at end of file
===========================
Introduction to PaddleHub
===========================
Welcome to PaddleHub! This is an Awesome pre-trained models toolkit based on PaddlePaddle which aimed at reducing the cost of using AI models and promoting the development of AI communities. No matter you are a senior developer in the AI ​​industry nor an interested person who knows nothing about the AI ​​field, you can benefit from PaddleHub.
* PaddleHub aims to provide developers with rich, high-quality, and directly usable pre-trained models.
* **No need for deep learning background**, you can use AI models quickly and enjoy the dividends of the artificial intelligence era.
* Covers 4 major categories of Image, Text, Audio, and Video, and supports **one-click prediction**, **easy service deployment** and **transfer learning**.
* All models are **OPEN SOURCE**, **FREE** to download and use them in offline scenario.
.. PaddleHub documentation master file, created by
sphinx-quickstart on Thu Apr 22 17:42:49 2021.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
------------
===========================
Overview
===========================
.. toctree::
:maxdepth: 2
:titlesonly:
Get start with PaddleHub<get_start_index>
Tutorial<tutorial_index>
API reference<api_index>
FAQ<faq>
Community<community_index>
Release note<release>
\ No newline at end of file
@ECHO OFF
pushd %~dp0
REM Command file for Sphinx documentation
if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build
if "%1" == "" goto help
%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end
:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
:end
popd
# Using PaddleHub through Command Line Execution
The codes/commands on this page can run online on [AIStudio](https://aistudio.baidu.com/aistudio/projectdetail/643120). It is similar to the notebook environment, which can be accessed through a browser, without environment preparation. This is a quick and easy experiences for developers.
PaddleHub is designed to provide the command line tool for model management and usage. It also provides a method of completing predictions by executing the PaddleHub model in the command line. For example, the tasks of portrait segmentation and text word segmentation in the previous sections can also be implemented through command line execution.
### Before experience, install the PaddleHub.
```shell
# Lastest version
$ pip install paddlehub --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
```
### Portrait Cutout
```shell
# Download picture
$ wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
# Inference with command line
$ hub run deeplabv3p_xception65_humanseg --input_path test_image.jpg --visualization=True --output_dir="humanseg_output"
```
--2020-07-22 12:19:52-- https://paddlehub.bj.bcebos.com/resources/test_image.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 967120 (944K) [image/jpeg]
Saving to: ‘test_image.jpg’
test_image.jpg 100%[===================>] 944.45K 6.13MB/s in 0.2s
2020-07-22 12:19:53 (6.13 MB/s) - ‘test_image.jpg’ saved [967120/967120]
[{'save_path': 'humanseg_output/test_image.png', 'data': array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)}]
![png](../../imgs/humanseg_test_res.png)
### Chinese word segmentation
```shell
# Inference with command line
$ hub run lac --input_text "今天是个好日子"
```
Install Module lac
Downloading lac
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpjcskpj8x/lac
[==================================================] 100.00%
Successfully installed lac-2.1.1
[{'word': ['今天', '是', '个', '好日子'], 'tag': ['TIME', 'v', 'q', 'n']}]
The above command contains four parts:
- Hub: indicates the PaddleHub command.
- Run: invokes run to execute the model prediction.
- deeplabv3p\_xception65\_humanseg and lac: Indicate the algorithm model to be executed.
- --input\_path/-input\_text: Indicates the input data for the model, with different input methods for images and text.
In addition, the command line `visualization=True` indicates the visual output of the results, and `output_dir="humanseg_output"` indicates the directory where the prediction results are saved. You can access this path to view the output images.
Let's look at an example of OCR and mask detection.
### OCR
```shell
# Download picture
$ wget https://paddlehub.bj.bcebos.com/model/image/ocr/test_ocr.jpg
# requirements
$ pip install shapely
$ pip install pyclipper
# Inference with command line
$ hub run chinese_ocr_db_crnn_mobile --input_path test_ocr.jpg --visualization=True --output_dir='ocr_result'
```
--2020-07-22 15:00:50-- https://paddlehub.bj.bcebos.com/model/image/ocr/test_ocr.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 48680 (48K) [image/jpeg]
Saving to: ‘test_ocr.jpg’
test_ocr.jpg 100%[===================>] 47.54K --.-KB/s in 0.02s
2020-07-22 15:00:51 (2.88 MB/s) - ‘test_ocr.jpg’ saved [48680/48680]
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/
Requirement already satisfied: shapely in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (1.7.0)
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/
Requirement already satisfied: pyclipper in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (1.2.0)
[{'save_path': 'ocr_result/ndarray_1595401261.294494.jpg', 'data': [{'text': '纯臻营养护发素', 'confidence': 0.9438689351081848, 'text_box_position': [[24, 36], [304, 34], [304, 72], [24, 74]]}, {'text': '产品信息/参数', 'confidence': 0.9843138456344604, 'text_box_position': [[24, 80], [172, 80], [172, 104], [24, 104]]}, {'text': '(45元/每公斤,100公斤起订)', 'confidence': 0.9210420250892639, 'text_box_position': [[24, 109], [333, 109], [333, 136], [24, 136]]}, {'text': '每瓶22元,1000瓶起订)', 'confidence': 0.9685984253883362, 'text_box_position': [[22, 139], [283, 139], [283, 166], [22, 166]]}, {'text': '【品牌】', 'confidence': 0.9527574181556702, 'text_box_position': [[22, 174], [85, 174], [85, 198], [22, 198]]}, {'text': ':代加工方式/OEMODM', 'confidence': 0.9442129135131836, 'text_box_position': [[90, 176], [301, 176], [301, 196], [90, 196]]}, {'text': '【品名】', 'confidence': 0.8793742060661316, 'text_box_position': [[23, 205], [85, 205], [85, 229], [23, 229]]}, {'text': ':纯臻营养护发素', 'confidence': 0.9230973124504089, 'text_box_position': [[95, 204], [235, 206], [235, 229], [95, 227]]}, {'text': '【产品编号】', 'confidence': 0.9311650395393372, 'text_box_position': [[24, 238], [120, 238], [120, 260], [24, 260]]}, {'text': 'J:YM-X-3011', 'confidence': 0.8866629004478455, 'text_box_position': [[110, 239], [239, 239], [239, 256], [110, 256]]}, {'text': 'ODMOEM', 'confidence': 0.9916308522224426, 'text_box_position': [[414, 233], [430, 233], [430, 304], [414, 304]]}, {'text': '【净含量】:220ml', 'confidence': 0.8709315657615662, 'text_box_position': [[23, 268], [181, 268], [181, 292], [23, 292]]}, {'text': '【适用人群】', 'confidence': 0.9589888453483582, 'text_box_position': [[24, 301], [118, 301], [118, 321], [24, 321]]}, {'text': ':适合所有肤质', 'confidence': 0.935418963432312, 'text_box_position': [[131, 300], [254, 300], [254, 323], [131, 323]]}, {'text': '【主要成分】', 'confidence': 0.9366627335548401, 'text_box_position': [[24, 332], [117, 332], [117, 353], [24, 353]]}, {'text': '鲸蜡硬脂醇', 'confidence': 0.9033458828926086, 'text_box_position': [[138, 331], [235, 331], [235, 351], [138, 351]]}, {'text': '燕麦B-葡聚', 'confidence': 0.8497812747955322, 'text_box_position': [[248, 332], [345, 332], [345, 352], [248, 352]]}, {'text': '椰油酰胺丙基甜菜碱、', 'confidence': 0.8935506939888, 'text_box_position': [[54, 363], [232, 363], [232, 383], [54, 383]]}, {'text': '糖、', 'confidence': 0.8750994205474854, 'text_box_position': [[25, 364], [62, 364], [62, 383], [25, 383]]}, {'text': '泛酯', 'confidence': 0.5581164956092834, 'text_box_position': [[244, 363], [281, 363], [281, 382], [244, 382]]}, {'text': '(成品包材)', 'confidence': 0.9566792845726013, 'text_box_position': [[368, 367], [475, 367], [475, 388], [368, 388]]}, {'text': '【主要功能】', 'confidence': 0.9493741393089294, 'text_box_position': [[24, 395], [119, 395], [119, 416], [24, 416]]}, {'text': ':可紧致头发磷层', 'confidence': 0.9692543745040894, 'text_box_position': [[128, 397], [273, 397], [273, 414], [128, 414]]}, {'text': '美,从而达到', 'confidence': 0.8662520051002502, 'text_box_position': [[265, 395], [361, 395], [361, 415], [265, 415]]}, {'text': '即时持久改善头发光泽的效果,给干燥的头', 'confidence': 0.9690631031990051, 'text_box_position': [[25, 425], [372, 425], [372, 448], [25, 448]]}, {'text': '发足够的滋养', 'confidence': 0.8946213126182556, 'text_box_position': [[26, 457], [136, 457], [136, 477], [26, 477]]}]}]
```shell
# check the result
```
![png](../../imgs/ocr_res.jpg)
### Mask Detection
```shell
# Download picture
$ wget https://paddlehub.bj.bcebos.com/resources/test_mask_detection.jpg
# Inference with command line
$ hub run pyramidbox_lite_mobile_mask --input_path test_mask_detection.jpg --visualization=True --output_dir='detection_result'
```
--2020-07-22 15:08:11-- https://paddlehub.bj.bcebos.com/resources/test_mask_detection.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.229, 182.61.200.195
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.229|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 299133 (292K) [image/jpeg]
Saving to: ‘test_mask_detection.jpg’
test_mask_detection 100%[===================>] 292.12K --.-KB/s in 0.06s
2020-07-22 15:08:11 (4.55 MB/s) - ‘test_mask_detection.jpg’ saved [299133/299133]
Install Module pyramidbox_lite_mobile_mask
Downloading pyramidbox_lite_mobile_mask
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmp8oes9jid/pyramidbox_lite_mobile_mask
[==================================================] 100.00%
Successfully installed pyramidbox_lite_mobile_mask-1.3.0
Downloading pyramidbox_lite_mobile
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpvhjhlr10/pyramidbox_lite_mobile
[==================================================] 100.00%
[{'data': [{'label': 'MASK', 'confidence': 0.9992434978485107, 'top': 181, 'bottom': 440, 'left': 457, 'right': 654}, {'label': 'MASK', 'confidence': 0.9224318265914917, 'top': 340, 'bottom': 578, 'left': 945, 'right': 1125}, {'label': 'NO MASK', 'confidence': 0.9996706247329712, 'top': 292, 'bottom': 500, 'left': 1166, 'right': 1323}], 'path': 'test_mask_detection.jpg'}]
```shell
# check the result
```
![png](../../imgs/test_mask_detection_result.jpg)
### This is the introduction to the PaddleHub command line tool.
The command line tool of PaddleHub is developed with introducing package management concepts such as Anaconda and PIP. It can be used to search, download, install, upgrade, and predict models quickly and conveniently. The following is an overview of the 12 commands supported by PaddleHub. For details, see Command Line Reference :
* install: Installs the module locally, by default, in {HUB\_HOME}/.paddlehub/modules directory.
* uninstall: Uninstalls the local module.
* show: Views the properties of locally installed module or the properties of a module identified in the specified directory, including its name, version, description, author and other information.
* download: Downloads the module provided by PaddleHub of Baidu.
* search: Searches for matching Modules on the server by keywords. When you want to find a Module of a specific model, run search command to get fast results. For example, hub search ssd: the command runs to search all Modules that contain ssd. The command supports regular expressions, for example, hub search \^s.\*: the command runs to search all resources beginning with s.
* list: Lists locally installed module.
* run: Executes the predictions of the module.
* version: displays PaddleHub version information.
* help: displays help information.
* clear: PaddleHub generates some cached data during operation, which is stored in ${HUB\_HOME}/.paddlehub/cache by default. Users can clear the cache by running the clear command.
* autofinetune: Automatically adjusts the hyper-parameters of Fine-tune tasks. For details, see [PaddleHub AutoDL Finetuner](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.5/docs/tutorial/autofinetune.md) Tutorial.
* config: Views and sets Paddlehub-related settings, including settings of server address and log level.
* serving: Deploys Module Prediction Services in one key. For details, see PaddleHub Serving One-Key Service Deployment.
## Summary
According to the PaddleHub product concept, the model is the software. It is executed through Python API or command line to allow you to quickly experience or integrate the pre-training models with the Paddle characteristics. In addition, when users want to optimize pre-training models with small amounts of data, PaddleHub also supports migration learning. Through the Fine-tune API and a variety of built-in optimization strategies, only a small number of codes is needed to complete the fine-tuning of pre-training models. You can learn more about this later in the chapter on Transfer Learning.
> It should be noted that not all modules support prediction through command line (for example, BERT/ERNIE Transformer class models, which generally require fine-tuning with tasks), and not all modules can be used for fine-tuning (for example, it is not recommended to use the fine-tune of lexical analysis LAC models). It is recommended to read the Introduction to the pre-training model to understand the usage scenarios.
# More Experience Demos in PaddleHub
## PaddleHub Official Demo Collection \[Offical].
The official demo is available online in AI Studio. It is similar to notebook environments, which can be accessed through a browser. You do not need to prepare an environment, and it is very convenient for developers to experience quickly. It will be updated continuously, and we recommend to bookmark it. [https://aistudio.baidu.com/aistudio/personalcenter/thirdview/79927](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/79927)
## PaddleHub Developer Fun Practice \[ThirdParty]
The following are some of the interesting and practical works created by developers based on PaddleHub in previous PaddleHub courses or events, which are all included in AI Studio and can run online.
1. [Cloth Scissors Rock \[Face Recognition to Switch Local Window] ](https://aistudio.baidu.com/aistudio/projectdetail/507630)
2. [Fish in the Autumn Water \[yesok dance background cutout conversion \& anime style migration] ](http://aistudio.baidu.com/aistudio/projectdetail/517066)
3. [Ninetailskim \[play retro windows pinball on people's faces] ](https://aistudio.baidu.com/aistudio/projectdetail/518861)
4. [Ura\_\_ \[Monitor Mask, Voice Alerts, Background Recording] ](https://aistudio.baidu.com/aistudio/projectdetail/506931)
5. [Nine Alchemists \[Shadow stream's Green Frog Cai Xukun, Face Recognition + Headgear + Portrait Segmentation ](https://aistudio.baidu.com/aistudio/projectdetail/505168)
6. [Seven-year term \[style migration and local deployment] ](https://aistudio.baidu.com/aistudio/projectdetail/520453)
7. [Fanas Invincible \[Lipstick Color Test Project]](https://aistudio.baidu.com/aistudio/projectdetail/516520)
8. [skywalk163 \[statistics of source code word frequency and word cloud \& portrait display with paddlehub] ](https://aistudio.baidu.com/aistudio/projectdetail/519841)
9. [AIStudio261428 \[Face Recognition + Cartoon Emoticons]](https://aistudio.baidu.com/aistudio/projectdetail/519616)
10. [Tudou Sprouts \[Invite Cartoon Characters to Visit on Children's Day]](https://aistudio.baidu.com/aistudio/projectdetail/520925)
11. [Panda Feeling \[changing masks] ](https://aistudio.baidu.com/aistudio/projectdetail/520996)
12. [Kly1997 \[One-key Travel + Sunglasses]](https://aistudio.baidu.com/aistudio/projectdetail/518117)
13. [Loneliness\_Memo \[Crossing over to the painting] ](https://aistudio.baidu.com/aistudio/projectdetail/516332)
14. [isse7 \[Creative Projects: Style "Ghostface" Change]](https://aistudio.baidu.com/aistudio/projectdetail/515307)
15. [Pda \[Face Fun Change] ](https://aistudio.baidu.com/aistudio/projectdetail/516306)
16. [Kgkzhiwen \[My New Clothes]](https://aistudio.baidu.com/aistudio/projectdetail/516663)
17. [Ah Ah Ah Good Good Study \[Automatic face shape adjustment]](https://aistudio.baidu.com/aistudio/projectdetail/513640)
18. [Tfboy \[ID Photo Replacement](https://aistudio.baidu.com/aistudio/projectdetail/509443)
19. [Leigangblog \[I am a star face]](https://aistudio.baidu.com/aistudio/projectdetail/505537)
20. [wpb3dm \[Fashion Model Dressup] wpb3dm](https://aistudio.baidu.com/aistudio/projectdetail/519349)
21. [lsvine\_bai \[Girlfriend turns into mysterious blonde goddess in seconds]](https://aistudio.baidu.com/aistudio/projectdetail/521784)
22. [Lemonadeqk \[Easy Stargazing]](https://aistudio.baidu.com/aistudio/projectdetail/520488)
23. [XM1436gr \[AI for Cartoon Face using PaddleHub Keypoint Detection] ](https://aistudio.baidu.com/aistudio/projectdetail/514547)
24. [Wantzai \[Everyone is a round-eyed cute boy] ](https://aistudio.baidu.com/aistudio/projectdetail/519222)
25. [Arrowarcher \[AI One Key Hair chane] ](https://aistudio.baidu.com/aistudio/projectdetail/508270)
26. [WHY197598 \[Fundamentals of Shifting Objects] ](https://aistudio.baidu.com/aistudio/projectdetail/517961)
27. [SIGNED BY JINGYI \[Fatigue detection based on paddlehub face key point detection]](https://aistudio.baidu.com/aistudio/projectdetail/506024)
28. [thunder95 \[PaddleHub Eyes Emotion Poll] ](https://aistudio.baidu.com/aistudio/projectdetail/514205)
29. [Windy Moon C \[Grave Bouncing Graduation Photo]](https://aistudio.baidu.com/aistudio/projectdetail/511253)
30. [Ruyi\_Egg \[left looks like Chow Yun-Fat, right looks like Andy Lau] ](https://aistudio.baidu.com/aistudio/projectdetail/507231)
# Using PaddleHub through Python Code Execution
The codes/commands on this page can run online on [AIStudio](https://aistudio.baidu.com/aistudio/projectdetail/635335). It is similar to the notebook environment, which can be accessed through a browser, without environment preparation. This is a quick and easy experiences for developers.
## PaddleHub example for computer vision tasks
Taking the computer vision task as an example, we choose a test image test.jpg to implement the following four functions:
* Portrait Cutout ([deeplabv3p\_xception65\_humanseg](https://www.paddlepaddle.org.cn/hubdetail?name=deeplabv3p_xception65_humanseg&en_category=ImageSegmentation))
* Body Part Segmentation ([ace2p](https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation))
* Face Detection ([ultra\_light\_fast\_generic\_face\_detector\_1mb\_640](https://www.paddlepaddle.org.cn/hubdetail?name=ultra_light_fast_generic_face_detector_1mb_640&en_category=FaceDetection))
* Key Point Detection ([human\_pose\_estimation\_resnet50\_mpii](https://www.paddlepaddle.org.cn/hubdetail?name=human_pose_estimation_resnet50_mpii&en_category=KeyPointDetection))
> Note: If you need to find out which pre-training models can be executed from PaddleHub, get the model name (for example, deeplabv3p\_xception65\_humanseg, by which the model will be executed in subsequent codes). For details, see official website document. In the documents, it is easy to search because models are classified according to model categories, with providing a detailed description of the models.
### Before experience, install the PaddleHub.
```shell
# install the latest version
$ pip install paddlehub --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
```
### Original Image Display
```shell
# Download a picture
$ wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
```
--2020-07-22 12:22:19-- https://paddlehub.bj.bcebos.com/resources/test_image.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 967120 (944K) [image/jpeg]
Saving to: ‘test_image.jpg.1’
test_image.jpg.1 100%[===================>] 944.45K 5.51MB/s in 0.2s
2020-07-22 12:22:19 (5.51 MB/s) - ‘test_image.jpg.1’ saved [967120/967120]
![png](../../imgs/humanseg_test.png)
### Portrait Cutout
PaddleHub adopts the model-based software design concept. All pre-training models are similar to Python packages, with the concept of version. You can install, upgrade, and remove the model conveniently by running the commands `hub install` and `hub uninstall`:
> By default, you can download the model of the latest version by running the following command. If you want to specify the version, you can add the version number after the `==1.1.1` command.
```shell
#intall pre-trained models
$ hub install deeplabv3p_xception65_humanseg
```
Downloading deeplabv3p_xception65_humanseg
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpo32jeve0/deeplabv3p_xception65_humanseg
[==================================================] 100.00%
Successfully installed deeplabv3p_xception65_humanseg-1.1.1
```python
# import paddlehub
import paddlehub as hub
module = hub.Module(name="deeplabv3p_xception65_humanseg")
res = module.segmentation(paths = ["./test_image.jpg"], visualization=True, output_dir='humanseg_output')
```
[32m[2020-07-22 12:22:49,474] [ INFO] - Installing deeplabv3p_xception65_humanseg module [0m
Downloading deeplabv3p_xception65_humanseg
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpzrrl1duq/deeplabv3p_xception65_humanseg
[==================================================] 100.00%
[32m[2020-07-22 12:23:11,811] [ INFO] - Successfully installed deeplabv3p_xception65_humanseg-1.1.1 [0m
![png](../../imgs/output_8_3.png)
As you can see, the execution of PaddleHub with Python codes requires only three lines of codes:
```
import paddlehub as hub
module = hub.Module(name="deeplabv3p_xception65_humanseg")
res = module.segmentation(paths = ["./test.jpg"], visualization=True, output_dir='humanseg_output')
```
* Model names are specified through the `hub.Module` API.
* `module.segmentation` is used to execute the image segmentation prediction tasks. Different prediction APIs are designed for different types of tasks. For example, the face detection task uses the `face_detection` function. It is recommended to view the corresponding model introduction document before the pre-training model is executed.
* The prediction results are saved in `output_dir='humanseg_output'` directory, and you can view the output images in this directory.
For the implementation of other tasks, refer to this pattern. Let's see how the next few tasks are implemented.
### Body Part Segmentation
```shell
$ hub install ace2p
```
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Downloading ace2p
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpfsovt3f8/ace2p
[==================================================] 100.00%
Successfully installed ace2p-1.1.0
```python
import paddlehub as hub
module = hub.Module(name="ace2p")
res = module.segmentation(paths = ["./test_image.jpg"], visualization=True, output_dir='ace2p_output')
```
[32m[2020-07-22 12:23:58,027] [ INFO] - Installing ace2p module [0m
Downloading ace2p
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmptrogpj6j/ace2p
[==================================================] 100.00%
[32m[2020-07-22 12:24:22,575] [ INFO] - Successfully installed ace2p-1.1.0 [0m
![png](../../imgs/output_12_3.png)
### Face Detection
```shell
#install pre-trained model
$ hub install ultra_light_fast_generic_face_detector_1mb_640
```
Downloading ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpz82xnmy6/ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
Successfully installed ultra_light_fast_generic_face_detector_1mb_640-1.1.2
```python
import paddlehub as hub
module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
res = module.face_detection(paths = ["./test_image.jpg"], visualization=True, output_dir='face_detection_output')
```
[32m[2020-07-22 12:25:12,948] [ INFO] - Installing ultra_light_fast_generic_face_detector_1mb_640 module [0m
Downloading ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpw44mo56p/ultra_light_fast_generic_face_detector_1mb_640
[==================================================] 100.00%
[32m[2020-07-22 12:25:14,698] [ INFO] - Successfully installed ultra_light_fast_generic_face_detector_1mb_640-1.1.2
![png](../../imgs/output_15_3.png)
### Key Point Detection
```shell
$ hub install human_pose_estimation_resnet50_mpii
```
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Downloading human_pose_estimation_resnet50_mpii
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpn_ppwkzq/human_pose_estimation_resnet50_mpii
[======== ] 17.99%
```python
import paddlehub as hub
module = hub.Module(name="human_pose_estimation_resnet50_mpii")
res = module.keypoint_detection(paths = ["./test_image.jpg"], visualization=True, output_dir='keypoint_output')
```
[32m[2020-07-23 11:27:33,989] [ INFO] - Installing human_pose_estimation_resnet50_mpii module [0m
[32m[2020-07-23 11:27:33,992] [ INFO] - Module human_pose_estimation_resnet50_mpii already installed in /home/aistudio/.paddlehub/modules/human_pose_estimation_resnet50_mpii [0m
image saved in keypoint_output/test_imagetime=1595474855.jpg
![png](../../imgs/output_18_2.png)
## PaddleHub Example for Natural Language Processing Tasks
Let's look at two more examples of natural language processing tasks: Chinese word segmentation and sentiment classification tasks.
* Chinese word segmentation ([lac](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis))
* Sentiment analysis ([senta\_bilstm](https://www.paddlepaddle.org.cn/hubdetail?name=senta_bilstm&en_category=SentimentAnalysis))
### Chinese word segmentation
```shell
$ hub install lac
```
2020-07-22 10:03:09,866-INFO: font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
2020-07-22 10:03:10,208-INFO: generated new fontManager
Downloading lac
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmp8ukaz690/lac
[==================================================] 100.00%
Successfully installed lac-2.1.1
```python
import paddlehub as hub
lac = hub.Module(name="lac")
test_text = ["1996年,曾经是微软员工的加布·纽维尔和麦克·哈灵顿一同创建了Valve软件公司。他们在1996年下半年从id software取得了雷神之锤引擎的使用许可,用来开发半条命系列。"]
res = lac.lexical_analysis(texts = test_text)
print("The resuls are: ", res)
```
[32m[2020-07-22 10:03:18,439] [ INFO] - Installing lac module[0m
[32m[2020-07-22 10:03:18,531] [ INFO] - Module lac already installed in /home/aistudio/.paddlehub/modules/lac [0m
The resuls are: [{'word': ['1996年', ',', '曾经', '是', '微软', '员工', '的', '加布·纽维尔', '和', '麦克·哈灵顿', '一同', '创建', '了', 'Valve软件公司', '。', '他们', '在', '1996年下半年', '从', 'id', ' ', 'software', '取得', '了', '雷神之锤', '引擎', '的', '使用', '许可', ',', '用来', '开发', '半条命', '系列', '。'], 'tag': ['TIME', 'w', 'd', 'v', 'ORG', 'n', 'u', 'PER', 'c', 'PER', 'd', 'v', 'u', 'ORG', 'w', 'r', 'p', 'TIME', 'p', 'nz', 'w', 'n', 'v', 'u', 'n', 'n', 'u', 'vn', 'vn', 'w', 'v', 'v', 'n', 'n', 'w']}]
As you can see, compared to computer vision tasks, there are differences between the input and output interfaces (where you need to enter text, executed as the function parameters), because it depends on the task types. For details, see the API description for the corresponding pre-training model.
### Sentiment classification
```shell
$ hub install senta_bilstm
```
Module senta_bilstm-1.1.0 already installed in /home/aistudio/.paddlehub/modules/senta_bilstm
```python
import paddlehub as hub
senta = hub.Module(name="senta_bilstm")
test_text = ["味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。"]
res = senta.sentiment_classify(texts = test_text)
print("情感分析结果:", res)
```
[32m[2020-07-22 10:34:06,922] [ INFO] - Installing senta_bilstm module [0m
[32m[2020-07-22 10:34:06,984] [ INFO] - Module senta_bilstm already installed in /home/aistudio/.paddlehub/modules/senta_bilstm
[32m[2020-07-22 10:34:08,937] [ INFO] - Installing lac module[0m
[32m[2020-07-22 10:34:08,939] [ INFO] - Module lac already installed in /home/aistudio/.paddlehub/modules/lac [0m
情感分析结果: [{'text': '味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9771, 'negative_probs': 0.0229}]
## Summary
PaddleHub provides a rich set of pre-training models, including image classification, semantic model, video classification, image generation, image segmentation, text review, key point detection and other mainstream models. These can be executed quickly and easily with only 3 lines of Python codes, with the instant output of prediction results. You can try it out by selecting some models from the Pre-training Model List.
Quick Experience
==================
The PaddleHub is used in two modes: Python code execution and command line execution.
The command line mode requires only one line of command. It is perfect for quickly experiencing the effects of the pre-training models provided by PaddleHub. The Python code mode requires only three lines of codes. If you need to use your own data Fine-tune to generate models, you can use this method.
Example of command line:
::
$ hub run chinese_ocr_db_crnn_mobile --input_path test_ocr.jpg
Example of Python Codes:
::
import paddlehub as hub
ocr = hub.Module(name="chinese_ocr_db_crnn_mobile")
result = ocr.recognize_text(paths = "./test_image.jpg"], visualization=True, output_dir='ocr_output')
Example of results:
... image:: ... /imgs/ocr_res.jpg
This section provides a quick way to experience these two methods, so that you can get started quickly. It also provides a rich experience Demo, covering multiple scenarios. For more detailed explanations of the API interface parameters and command line parameters, refer to Python API Interface and Command Line.
... toctree::
:maxdepth: 1
Command line execution: PaddleHub<cmd_quick_run>
Python code execution: PaddleHub<python_use_hub>
More Experience Demos of PaddleHub: <more_demos>
# Release Note
## `v2.1.0`
### [ 1. Improvements]
- Add supports for five new models, including two high-precision semantic segmentation models based on VOC dataset and three voice classification models.
- Enforce the transfer learning capabilities for image semantic segmentation, text semantic matching and voice classification on related datasets.
### [ 2. Upgrades of deployment capabilities]
- Add the export function APIs for two kinds of model formats, i.,e, ONNX and PaddleInference.
- **Important Open-Source Ecological Cooperation**: add the support for [BentoML](https://github.com/bentoml/BentoML/), which is a cloud native framework for serving deployment. Users can easily serve pre-trained models from [PaddleHub](https://github.com/PaddlePaddle/PaddleHub) by following the [Tutorial notebooks](https:// github.com/PaddlePaddle/PaddleHub/tree/release/v2.1/demo/serving/BentoML). Also, see this announcement and [Release note](https://github.com/bentoml/BentoML/releases/tag/v0.12.1) from BentoML. (Many thanks to @[parano](https://github.com/parano) @[cqvu](https://github.com/cqvu) @[deehrlic](https://github.com/deehrlic) for contributing this feature in PaddleHub)
### [ 3. Bug fixes ]
- [#7da1230](https://github.com/PaddlePaddle/PaddleHub/commit/7da12302dd77e3d739da72821d41715ad8a7c79c) Fixed the problem that the model cannot resume training if metrics is not recorded.
- [#b0b3144](https://github.com/PaddlePaddle/PaddleHub/commit/b0b3144eff34e47cac8fc450c8b7cb6c557f9b84) Fixed the problem that the thread did not exit normally when the evaluation process was abnormal.
- [#30aace4](https://github.com/PaddlePaddle/PaddleHub/commit/30aace46414bbeef02beb75b7128f48fada82150) Improve the model installation process.
## `v2.0.0`
* 发布 2.0版本,全面迁移动态图编程模式,模型开发调试更加方便,finetune接口更加灵活易用。
* 视觉类任务迁移学习能力全面升级,支持图像分类、图像着色、风格迁移等多种任务。
* BERT、ERNIE、RoBERTa等Transformer类模型升级至动态图,支持文本分类、序列标注的Fine-Tune能力。
* 新增词向量模型61个,其中包含中文模型51个,英文模型10个。
* 优化服务化部署Serving能力,支持多卡预测、自动负载均衡,性能大幅度提升。
* 新增自动数据增强能力Auto Augment,能高效地搜索适合数据集的数据增强策略组合。
## `v2.0.0-beta1`
* BERT、ERNIE、RoBERTa等Transformer类模型升级至动态图,增加[文本分类](../../demo/text_classification)的Fine-Tune能力
* 修复部分已知问题
## `v2.0.0-beta0`
* 全面迁移动态图编程模式,模型开发调试更加方便,finetune接口更加灵活易用。
* 优化服务化部署Serving能力,支持多卡预测、自动负载均衡,性能大幅度提升。
* 视觉类迁移学习能力全面升级,支持[图像分类](../../demo/image_classification)[图像着色](../../demo/colorization)[风格迁移](../../demo/style_transfer)等多种视觉任务。
## `v1.8.1`
*[图像分割](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=ImageSegmentation)』新增轻量级人像分割模型Humanseg,支持移动端实时分割
* 增强文本匹配任务性能,使用[EMNLP2019-Sentence-BERT](https://arxiv.org/abs/1908.10084)作为文本匹配任务网络,可同时大幅提升准确率和预测速度。配套教程:[pointwise文本语义匹配](https://aistudio.baidu.com/aistudio/projectdetail/705526)[pairwise文本语义匹配](https://aistudio.baidu.com/aistudio/projectdetail/709472)
* 修复文本分类选择F1作为评价指标,运行错误
## `v1.8.0`
* 预训练模型丰富,一键完成更多
*[文本生成](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=TextGeneration)』新增基于ERNIE-tiny和ERNIE-gen的对联和写诗生成模型,支持一键自动写诗和对对联。
*[词法分析](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=LexicalAnalysis)』新增jieba的paddle模式切词模型,可一键完成中文分词、关键词抽取等功能。
*[语义表示](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=SemanticModel)』新增基于网页、小说、新闻三类大规模文本数据的LDA主题模型及其语义相似度计算接口。
* Fine-tune API升级,提升灵活性并支持更多任务
* 新增Tokenizer API,支持更加灵活的切词、切字模式和自定义切词工具拓展。
* 新增[文本生成](https://github.com/PaddlePaddle/PaddleHub/tree/release/v1.8/demo/text_generation)任务,支持Seq2Seq任务的Fine-tuning。
* 新增文本匹配任务,支持[Pointwise](https://github.com/PaddlePaddle/PaddleHub/tree/release/v1.8/demo/pointwise_text_matching)[Pairwise](https://github.com/PaddlePaddle/PaddleHub/tree/release/v1.8/demo/pairwise_text_matching)两种文本匹配训练模式,更便捷完成语义匹配任务。
## `v1.7.0`
* 丰富预训练模型,提升应用性
* 新增VENUS系列视觉预训练模型[yolov3_darknet53_venus](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_venus&en_category=ObjectDetection)[faster_rcnn_resnet50_fpn_venus](https://www.paddlepaddle.org.cn/hubdetail?name=faster_rcnn_resnet50_fpn_venus&en_category=ObjectDetection),可大幅度提升图像分类和目标检测任务的Fine-tune效果
* 新增工业级短视频分类模型[videotag_tsn_lstm](https://paddlepaddle.org.cn/hubdetail?name=videotag_tsn_lstm&en_category=VideoClassification),支持3000类中文标签识别
* 新增轻量级中文OCR模型[chinese_ocr_db_rcnn](https://www.paddlepaddle.org.cn/hubdetail?name=chinese_ocr_db_rcnn&en_category=TextRecognition)[chinese_text_detection_db](https://www.paddlepaddle.org.cn/hubdetail?name=chinese_text_detection_db&en_category=TextRecognition),支持一键快速OCR识别
* 新增行人检测、车辆检测、动物识别、Object等工业级模型
* Fine-tune API升级
* 文本分类任务新增6个预置网络,包括CNN, BOW, LSTM, BiLSTM, DPCNN等
* 使用VisualDL可视化训练评估性能数据
## `v1.6.2`
* 修复图像分类在windows下运行错误
## `v1.6.1`
* 修复windows下安装PaddleHub缺失config.json文件
## `v1.6.0`
* NLP Module全面升级,提升应用性和灵活性
* lac、senta系列(bow、cnn、bilstm、gru、lstm)、simnet_bow、porn_detection系列(cnn、gru、lstm)升级高性能预测,性能提升高达50%
* ERNIE、BERT、RoBERTa等Transformer类语义模型新增获取预训练embedding接口get_embedding,方便接入下游任务,提升应用性
* 新增RoBERTa通过模型结构压缩得到的3层Transformer模型[rbt3](https://www.paddlepaddle.org.cn/hubdetail?name=rbt3&en_category=SemanticModel)[rbtl3](https://www.paddlepaddle.org.cn/hubdetail?name=rbtl3&en_category=SemanticModel)
* Task predict接口增加高性能预测模式accelerate_mode,性能提升高达90%
* PaddleHub Module创建流程开放,支持Fine-tune模型转化,全面提升应用性和灵活性
* [预训练模型转化为PaddleHub Module教程](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/contribution/contri_pretrained_model.md)
* [Fine-tune模型转化为PaddleHub Module教程](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/tutorial/finetuned_model_to_module.md)
* [PaddleHub Serving](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/tutorial/serving.md)优化启动方式,支持更加灵活的参数配置
## `v1.5.2`
* 优化pyramidbox_lite_server_mask、pyramidbox_lite_mobile_mask模型的服务化部署性能
## `v1.5.1`
* 修复加载module缺少cache目录的问题
## `v1.5.0`
* 升级PaddleHub Serving,提升性能和易用性
* 新增文本Embedding服务[Bert Service](./tutorial/bert_service.md), 轻松获取文本embedding;
* 代码精短,易于使用。服务端/客户端一行命令即可获取文本embedding;
* 更高性能,更高效率。通过Paddle AnalysisPredictor API优化计算图,提升速度减小显存占用
* 随"机"应变,灵活扩展。根据机器资源和实际需求可灵活增加服务端数量,支持多显卡多模型计算任务
* 优化并发方式,多核环境中使用多线程并发提高整体QPS
* 优化PaddleHub迁移学习组网Task功能,提升易用性
* 增加Hook机制,支持[修改Task内置方法](./tutorial/hook.md)
* 增加colorlog,支持日志彩色显示
* 改用save_inference_model接口保存模型,方便模型部署
* 优化predict接口,增加return_result参数,方便用户直接获取预测结果
* 优化PaddleHub Dataset基类,加载[自定义数据](./tutorial/how_to_load_data.md)代码更少、更简单
## `v1.4.1`
* 修复利用Transformer类模型完成序列标注任务适配paddle1.6版本的问题
* Windows下兼容性提升为python >= 3.6
## `v1.4.0`
* 新增预训练模型ERNIE tiny
* 新增数据集:INEWS、BQ、DRCD、CMRC2018、THUCNEWS,支持ChineseGLUE(CLUE)V0 所有任务
* 修复module与PaddlePaddle版本兼容性问题
* 优化Hub Serving启动过程和模型加载流程,提高服务响应速度
## `v1.3.0`
* 新增PaddleHub Serving服务部署
* 新增[hub serving](https://github.com/PaddlePaddle/PaddleHub/wiki/PaddleHub-Serving%E4%B8%80%E9%94%AE%E6%9C%8D%E5%8A%A1%E9%83%A8%E7%BD%B2)命令,支持一键启动Module预测服务部署
* 新增预训练模型:
* roberta_wwm_ext_chinese_L-24_H-1024_A-16
* roberta_wwm_ext_chinese_L-12_H-768_A-12
* bert_wwm_ext_chinese_L-12_H-768_A-12
* bert_wwm_chinese_L-12_H-768_A-12
* AutoDL Finetuner优化使用体验
* 支持通过接口方式回传模型性能
* 可视化效果优化,支持多trail效果显示
## `v1.2.1`
* 新增**超参优化Auto Fine-tune**,实现给定超参搜索空间,PaddleHub自动给出较佳的超参组合
* 支持两种超参优化算法:HAZero和PSHE2
* 支持两种评估方式:FullTrail和PopulationBased
* 新增Fine-tune**优化策略ULMFiT**,包括以下三种设置
* Slanted triangular learning rates:学习率先线性增加后缓慢降低
* Discriminative fine-tuning:将计算图划分为n段,不同的段设置不同学习率
* Gradual unfreezing:根据计算图的拓扑结构逐层unfreezing
* 新增支持用户自定义PaddleHub配置,包括
* 预训练模型管理服务器地址
* 日志记录级别
* Fine-tune API升级,灵活性与易用性提升
* 新增**阅读理解Fine-tune任务****回归Fine-tune任务**
* 新增多指标评测
* 优化predict接口
* 可视化工具支持使用tensorboard
## `v1.1.2`
* PaddleHub支持修改预训练模型存放路径${HUB_HOME}
## `v1.1.1`
* PaddleHub支持离线运行
* 修复python2安装PaddleHub失败问题
## `v1.1.0`
* PaddleHub **新增预训练模型ERNIE 2.0**
* 升级Reader, 支持自动传送数据给Ernie 1.0/2.0
* 新增数据集GLUE(MRPC、QQP、SST-2、CoLA、QNLI、RTE、MNLI)
## `v1.0.1`
* 安装模型时自动选择与paddlepaddle版本适配的模型
## `v1.0.0`
* 全新发布PaddleHub官网,易用性全面提升
* 新增网站 https://www.paddlepaddle.org.cn/hub 包含PaddlePaddle生态的预训练模型使用介绍
* 迁移学习Demo接入AI Studio与AI Book,无需安装即可快速体验
* 新增29个预训练模型,覆盖文本、图像、视频三大领域;目前官方提供40个预训练模型
* CV预训练模型:
* 新增图像分类预训练模型11个:SE_ResNeXt, GoogleNet, ShuffleNet等
* 新增目标检测模型Faster-RCNN和YOLOv3
* 新增图像生成模型CycleGAN
* 新增人脸检测模型Pyramidbox
* 新增视频分类模型4个: TSN, TSM, StNet, Non-Local
* NLP预训练模型
* 新增语义模型ELMo
* 新增情感分析模型5个: Senta-BOW, Senta-CNN, Senta-GRNN, , Senta-LSTM, EmoTect
* 新增中文语义相似度分析模型SimNet
* 升级LAC词法分析模型,新增词典干预功能,支持用户自定义分词
* Fine-tune API升级,灵活性与性能全面提升
* 支持多卡并行、PyReader多线程IO,Fine-tune速度提升60%
* 简化finetune、evaluate、predict等使用逻辑,提升易用性
* 增加事件回调功能,方便用户快速实现自定义迁移学习任务
* 新增多标签分类Fine-tune任务
## `v0.5.0`
正式发布PaddleHub预训练模型管理工具,旨在帮助用户更高效的管理模型并开展迁移学习的工作。
**预训练模型管理**: 通过hub命令行可完成PaddlePaddle生态的预训练模型下载、搜索、版本管理等功能。
**命令行一键使用**: 无需代码,通过命令行即可直接使用预训练模型进行预测,快速调研训练模型效果。目前版本支持以下模型:词法分析LAC;情感分析Senta;目标检测SSD;图像分类ResNet, MobileNet, NASNet等。
**迁移学习**: 提供了基于预训练模型的Fine-tune API,用户通过少量代码即可完成迁移学习,包括BERT/ERNIE文本分类、序列标注、图像分类迁移等。
==================
Transfer Learning
==================
Transfer Learning is a subfield of deep learning that aims to use similarities in data, tasks, or models to transfer knowledge learned in old fields to new fields. In other words, transfer learning refers to the use of existing knowledge to learn new knowledge. For example, people who have learned to ride a bicycle can learn to ride an electric bike faster. A common method of transfer learning is to perform fine-tune of a pre-training model. That is, the user selects a successfully trained model from PaddleHub for a new task based on the current task scenario, and the dataset used by the model is similar to the dataset of the new scenario. In this case, you only needs to perform fine-tune of the parameters of the model (**Fine-tune**) during the training of the current task scenario using the data of the new scenario. Transfer learning has attracted many researchers because it is a good solution to the following problems in deep learning:
* In some research areas, there are only a small amount of annotated data, and the cost of data annotation is high, which is not enough to train a sufficiently robust neural network.
* The training of large-scale neural networks relies on large computational resources, which is difficult to implement for a common user.
* Models that address generalized needs do not perform as well as expected for specific applications.
In order to make it easier for developers to apply transfer learning, Paddle has open-sourced PaddleHub – a pre-training model management tool. With just ten lines of codes, developers can complete the transfer learning process. This section describes comprehensive transfer learning by using the PaddleHub.
.. image:: ../imgs/paddlehub_finetune.gif
:width: 900px
:align: center
.. toctree::
:maxdepth: 2
finetune/sequence_labeling.md
finetune/text_matching.md
finetune/image_classification.md
finetune/image_colorization.md
finetune/style_transfer.md
finetune/semantic_segmentation.md
finetune/audio_classification.md
\ No newline at end of file
# PaddleHub Command Line Tool ===========================
PaddleHub Command Line Tool
===========================
PaddleHub provides the command line tool for the management and use of pre-training models. PaddleHub provides the command line tool for the management and use of pre-training models.
PaddleHub supports the change of the path of storing the pre-training models. There are 11 commands in total, covering various aspects such as model installation, uninstallation, prediction, etc.
* If `${HUB_HOME}`Environment Variable is set, the pre-training models and configuration files are stored in the path specified by `${HUB_HOME}`.
* If the `${HUB_HOME}`Environment variable is not set, these files are stored in the path specified by `$HOME`.
The following 11 commands are currently supported on the command line. hub install
==================
## `hub install`
Installs Module locally. By default, it is installed in the `${HUB_HOME}/.paddlehub/modules` directory. When a module is installed locally, users can operate the Module through other commands (e.g., use the Module for prediction), or use the python API provided by PaddleHub to apply the Module to their own tasks to achieve migration learning. Installs Module locally. By default, it is installed in the `${HUB_HOME}/.paddlehub/modules` directory. When a module is installed locally, users can operate the Module through other commands (e.g., use the Module for prediction), or use the python API provided by PaddleHub to apply the Module to their own tasks to achieve migration learning.
## `hub uninstall` .. tip::
If Environment Variable *${HUB_HOME}* is set, the pre-training models and configuration files are stored in the path specified by *${HUB_HOME}*.
If Environment Variable *${HUB_HOME}* is not set, these files are stored in the path specified by *$HOME*.
hub uninstall
==================
Uninstalls local Modules. Uninstalls local Modules.
## `hub show` hub show
==================
Views properties of locally installed modules or properties of modules in a specified directory, including name, version, description, author and other information. Views properties of locally installed modules or properties of modules in a specified directory, including name, version, description, author and other information.
## `hub download` hub download
==================
Downloads the Module provided by PaddleHub. Downloads the Module provided by PaddleHub.
## `hub search` hub search
==================
Search the matching Module on the server by keywords. When you want to find the Module of a specific model, you can run the search command to quickly get the result. For example, the `hub search ssd` command runs to search for all Modules containing the word ssd. The command supports regular expressions, for example, `hub search ^s.*` runs to search all resources beginning with s. Search the matching Module on the server by keywords. When you want to find the Module of a specific model, you can run the search command to quickly get the result. For example, the `hub search ssd` command runs to search for all Modules containing the word ssd. The command supports regular expressions, for example, `hub search ^s.*` runs to search all resources beginning with s.
`Note` If you want to search all Modules, the running of `hub search *` does not work. Because the shell expands its own wildcard, replacing \* with the filename in the current directory. For global search, users can type `hub search` directly. .. tip::
If you want to search all Modules, the running of `hub search *` does not work. Because the shell expands its own wildcard, replacing \* with the filename in the current directory. For global search, users can type `hub search` directly.
## `hub list` hub list
==================
Lists the locally installed Modules Lists the locally installed Modules
## `hub run` hub run
==================
Executes the prediction of Module. It should be noted that not all models support prediction (and likewise, not all models support migration learning). For more details please refer to the [Quick experience of *hub run*](../quick_experience/cmd_quick_run_en.md) Executes the prediction of Module. It should be noted that not all models support prediction (and likewise, not all models support migration learning). For more details please refer to the [Quick experience of *hub run*](../quick_experience/cmd_quick_run_en.md)
## `hub help`
Displays help information.
## `hub version`
Displays the PaddleHub version information.
## `hub clear`
PaddleHub generates some cached data in the operation, which is stored in ${HUB\_HOME}/.paddlehub/cache directory by default. Users can clear the cache by running the clear command.
## `hub config` PaddleHub tries to simplify the cost of understanding when using command line predictions. Generally, the predictions are classified into two categories: NLP and CV.
Views and configures paddlehub-related information, including server address and log level.
`Example`
* `hub config`: Displays the current paddlehub settings.
* `hub config reset`: Restores current paddlehub settings to default settings NLP Class Tasks
---------------
* `hub config server==[address]`: Sets the current paddlehub-server address to \[address], and paddlehub client gets model information from this address. You can input data specified by *--input_text*. Take the Baidu LAC model (Chinese lexical analysis) as an example. You can use the following command to analyze texts.
* `hub config log.level==[level]`: Sets the current log level to \[level]. Options are CRITICAL, ERROR, WARNING, EVAL, TRAIN, INFO, DEBUG, from left to right, from high priority to low priority.
* `hub config log.enable==True|False`: Sets whether the current log is available. .. code-block:: console
## `hub serving` $ hub run lac --input_text "今天是个好日子"
Deploys Module prediction service in one key. For details, see PaddleHub Serving One-Key Service Deployment. CV Class Tasks
---------------
**NOTE:** Input data is specified by *--input\_path*. Take the SSD model (single-stage object detection) as an example. Predictions can be performed by running the following command:
In PaddleHub, Module represents a executable pre-training model`可执行的预训练模型`. A Module can support direct command-line prediction, or with the PaddleHub Finetune API. It implements the migration learning through a small number of codes. Not all Modules support command line prediction (for example, for BERT/ERNIE Transformer model, finetune is performed generally with a task). Not all Modules can be used for fine-tune (for example, for the LAC lexical analysis model, we do not recommend users to use finetune). .. code-block:: console
PaddleHub tries to simplify the cost of understanding when using command line predictions. Generally, the predictions are classified into two categories: NLP and CV. $ hub run resnet_v2_50_imagenet --input_path test.jpg
## NLP Class Tasks .. note::
You can input data specified by --input\_text or --input\_file. Take the Baidu LAC model (Chinese lexical analysis) as an example. You can use the following two commands to analyze the single-line texts and multi-line texts. In PaddleHub, Module represents a executable pre-training model. A Module can support direct command-line prediction, or with the PaddleHub Finetune API. It implements the migration learning through a small number of codes. Not all Modules support command line prediction (for example, for BERT/ERNIE Transformer model, finetune is performed generally with a task). Not all Modules can be used for fine-tune (for example, for the LAC lexical analysis model, we do not recommend users to use finetune).
```shell hub help
# Single-line of sentence ==================
$ hub run lac --input_text "It is a nice day today!"
```
```shell Displays help information.
# multi-line of sentences
$ hub run lac --input_file test.txt
```
The sample format of test.txt is as follows (each line is a sentence requiring lexical analysis):
``` hub version
It is a nice day today! ==================
It is going to rain outside.
……more……
```
## CV Class Tasks Displays the PaddleHub version information.
Input data is specified by `--input_path` or `--input_file`. Take the SSD model (single-stage object detection) as an example. Single-image and multi-image predictions can be performed by running the following two commands: hub clear
==================
```shell PaddleHub generates some cached data in the operation, which is stored in ${HUB\_HOME}/.paddlehub/cache directory by default. Users can clear the cache by running the clear command.
# Single Picture
$ hub run resnet_v2_50_imagenet --input_path test.jpg
```
```shell hub config
# List of Pictures ==================
$ hub run resnet_v2_50_imagenet --input_file test.txt
```
The format of test.txt is Views and configures paddlehub-related information, including server address and log level.
``` .. code-block:: console
cat.jpg
dog.jpg $ # Displays the current paddlehub settings.
person.jpg $ hub config
……more……
``` $ # Restores current paddlehub settings to default settings.
$ hub config reset
$ # Sets the current paddlehub-server address to ${HOST}, and paddlehub client gets model information from this address.
$ hub config server==${HOST}
$ # Sets the current log level to ${LEVEL}. Options are CRITICAL, ERROR, WARNING, EVAL, TRAIN, INFO, DEBUG, from left to right, from high priority to low priority.
$ hub config log.level==${LEVEL}
$ # Sets whether the current log is available.
$ hub config log.enable==True|False
hub serving
==================
Deploys Module prediction service in one key. For details, see `PaddleHub Serving Deployment <serving>`_.
\ No newline at end of file
======================
How to custom Module
======================
I. Preparation
=======================
Basic Model Information
------------------------
We are going to write a PaddleHub Module with the following basic information about the module:
.. code-block:: yaml
name: senta_test
version: 1.0.0
summary: This is a PaddleHub Module. Just for test.
author: anonymous
author_email:
type: nlp/sentiment_analysis
The Module has an interface sentiment_classify, which is used to receive incoming text and give it a sentiment preference (positive/negative). It supports python interface calls and command line calls.
.. code-block:: python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
.. code-block:: shell
hub run senta_test --input_text 这部电影太差劲了
Strategy
------------------------
For the sake of simplicity of the sample codes, we use a very simple sentiment strategy. When the input text has the word specified in the vocabulary list, the text tendency is judged to be negative; otherwise it is positive.
II. Create Module
=======================
Step 1: Create the necessary directories and files.
----------------------------------------------------
Create a senta_test directory. Then, create module.py, processor.py, and vocab.list in the senta_test directory, respectively.
.. code-block:: shell
$ tree senta_test
senta_test/
├── vocab.list
├── module.py
└── processor.py
============ =========================================================================
File Name Purpose
------------ -------------------------------------------------------------------------
============ =========================================================================
module.py It is the main module that provides the implementation codes of Module.
processor.py It is the helper module that provides a way to load the vocabulary list.
vocab.list It stores the vocabulary.
============ =========================================================================
Step 2: Implement the helper module processor.
------------------------------------------------
Implement a load_vocab interface in processor.py to read the vocabulary list.
.. code-block:: python
def load_vocab(vocab_path):
with open(vocab_path) as file:
return file.read().split()
Step 3: Write Module processing codes.
------------------------------------------------
The module.py file is the place where the Module entry code is located. We need to implement prediction logic on it.
Step 3_1. Reference the necessary header files
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
import argparse
import os
import paddlehub as hub
from paddlehub.module.module import runnable, moduleinfo
from senta_test.processor import load_vocab
.. note::
When referencing a module in Module, you need to enter the full path, for example, senta_test. processor.
Step 3_2. Define the SentaTest class.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Module.py needs to have a class that inherits hub. Module, and this class is responsible for implementing the prediction logic and filling in basic information with using moduleinfo. When the hub. Module(name="senta\_test") is used to load Module, PaddleHub automatically creates an object of SentaTest and return it.
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
Step 3_3. Perform necessary initialization.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
def __init__(self):
# add arg parser
self.parser = argparse.ArgumentParser(
description="Run the senta_test module.",
prog='hub run senta_test',
usage='%(prog)s',
add_help=True)
self.parser.add_argument(
'--input_text', type=str, default=None, help="text to predict")
# load word dict
vocab_path = os.path.join(self.directory, "vocab.list")
self.vocab = load_vocab(vocab_path)
...
.. note::
The execution class object has a built-in directory attribute by default. You can directly get the path of the Module.
Step 3_4: Refine the prediction logic.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
...
Step 3_5. Support the command-line invoke.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you want the module to support command-line invoke, you need to provide a runnable modified interface that parses the incoming data, makes prediction, and returns the results.
If you don't want to provide command-line prediction, you can leave the interface alone and PaddleHub automatically finds out that the module does not support command-line methods and gives a hint when PaddleHub executes in command lines.
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
@runnable
def run_cmd(self, argvs):
args = self.parser.parse_args(argvs)
texts = [args.input_text]
return self.sentiment_classify(texts)
...
step 3_6. Support the serving invoke.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you want the module to support the PaddleHub Serving deployment prediction service, you need to provide a serving-modified interface that parses the incoming data, makes prediction, and returns the results.
If you do not want to provide the PaddleHub Serving deployment prediction service, you do not need to add the serving modification.
.. code-block:: python
@moduleinfo(
name="senta_test",
version="1.0.0",
summary="This is a PaddleHub Module. Just for test.",
author="anonymous",
author_email="",
type="nlp/sentiment_analysis",
)
class SentaTest:
...
@serving
def sentiment_classify(self, texts):
results = []
for text in texts:
sentiment = "positive"
for word in self.vocab:
if word in text:
sentiment = "negative"
break
results.append({"text":text, "sentiment":sentiment})
return results
Complete Code
------------------------------------------------
* `module.py <https://github.com/PaddlePaddle/PaddleHub/blob/release/v2.1/modules/demo/senta_test/module.py>`_
* `processor.py <https://github.com/PaddlePaddle/PaddleHub/blob/release/v2.1/modules/demo/senta_test/processor.py>`_
III. Install and test Module.
===================================
After writing a module, we can test it in the following ways:
Call Method 1
------------------------------------------------
Install the Module into the local machine, and then load it through Hub.Module(name=...)
.. code-block:: console
$ hub install senta_test
.. code-block:: python
import paddlehub as hub
senta_test = hub.Module(name="senta_test")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
Call Method 2
------------------------------------------------
Load directly through Hub.Module(directory=...)
.. code-block:: python
import paddlehub as hub
senta_test = hub.Module(directory="senta_test/")
senta_test.sentiment_classify(texts=["这部电影太差劲了"])
Call Method 3
------------------------------------------------
Install the Module on the local machine and run it through hub run.
.. code-block:: console
$ hub install senta_test
$ hub run senta_test --input_text "这部电影太差劲了"
\ No newline at end of file
## **Transfer Learning**
### **Overview**
Transfer Learning is a subfield of deep learning that aims to use similarities in data, tasks, or models to transfer knowledge learned in old fields to new fields. In other words, transfer learning refers to the use of existing knowledge to learn new knowledge. For example, people who have learned to ride a bicycle can learn to ride an electric bike faster. A common method of transfer learning is to perform fine-tune of a pre-training model. That is, the user selects a successfully trained model from PaddleHub for a new task based on the current task scenario, and the dataset used by the model is similar to the dataset of the new scenario. In this case, you only needs to perform fine-tune of the parameters of the model (**Fine-tune**) during the training of the current task scenario using the data of the new scenario. Transfer learning has attracted many researchers because it is a good solution to the following problems in deep learning:
* In some research areas, there are only a small amount of annotated data, and the cost of data annotation is high, which is not enough to train a sufficiently robust neural network.
* The training of large-scale neural networks relies on large computational resources, which is difficult to implement for a common user.
* Models that address generalized needs do not perform as well as expected for specific applications.
In order to make it easier for developers to apply transfer learning, Paddle has open-sourced PaddleHub – a pre-training model management tool. With just ten lines of codes, developers can complete the transfer learning process. This section describes comprehensive transfer learning by using the PaddleHub.
### **Prerequisites**
Before starting transfer learning, users need to do the following:
* The user has installed PaddleHub.
* To prepare the data for transfer learning, users can choose to use the dataset provided by PaddleHub or a custom dataset. For details of the custom data, refer to the "Custom dataset Fine-tune" to process the dataset.
* Run the hub install command to install or update the modules used for training. For example, for the ERNIE model, the command format is as follows: You may have already installed the relevant pre-training model in a previous task. However, it is still recommended that you perform this step before you start training, so that you can ensure that the pre-training model is the latest version.
```python
$ hub install ernie==1.2.0
```
### **Transfer Learning Process**
Before you can complete the transfer learning process, you need to write scripts for transfer learning. The scripting process is very simple, requiring only about ten lines of codes. The entire scripting process has the following steps:
1. Import the necessary packages.
2. Load a pre-training model, that is, load the pre-training model provided by PaddleHub.
3. Load Dataset. You can choose to load the dataset that comes with PaddleHub by using the dataset API or write your own class to load the dataset to load the custom dataset.
4. Configure the reader to pre-process the data in dataset. Organize in a specific format and input to the model for training.
5. Choose an optimization strategy that includes various pre-training parameters, such as, what learning rate variation strategy, what type of optimizer, and what type of regularization.
6. Set the RunConfig. It contains a number of training-related configurations, including whether to use the GPU, the number of training rounds (Epoch), training batch size (batch\_size), and so on.
7. Create a training task. A transfer learning training task contains the task-related Program, Reader and RunConfig set above.
8. Start the Fine-tune. Use the Fine-tune\_and\_eval function to complete the training and evaluation.
### **Learn to Write Transfer Learning Training Scripts**
PaddleHub provides a Finetune API and a pre-training model to perform transfer learning for a variety of different task scenarios, including image classification, text classification, multi-label classification, sequence annotation, retrieval quiz task, regression task, sentence semantic similarity computation, reading comprehension task, and so on. This section describes how to write transfer learning scripts by taking the text classification as an example.
#### **1\. Import the necessary packages.**
```python
import paddlehub as hub
```
#### **2\. Load the pre-training model.**
The following code is used to load the pre-training model. In this case, the ERNIE pre-training model is used to complete the text classification task. The Enhanced Representation through Knowledge Integration (ERNIE) is a semantic representation model proposed by Baidu, with Transformer Encoder as the basic component. The pre-training process uses richer semantic knowledge and more semantic tasks. Users can use the pre-training model to gradually introduce different custom tasks at any time, such as, naming entity prediction, semantic paragraph relationship recognition, sentence order prediction task, sentiment analysis, and so on.
```python
module = hub.Module(name="ernie")
```
PaddleHub also provides many other pre-training models for transfer learning. On the PaddleHub website, the pre-training models under Image Classification, Semantic Model, and Sentiment Analysis all support transfer learning. Users only need to replace the name value with the name of the pre-training model, for example, the red box on the right.
![](../imgs/Howtofinetune1.png)
#### **3\. Load the dataset.**
After loading the pre-training model, we load the dataset. The sources of datasets used for transfer learning can be divided into customized datasets and datasets provided by PaddleHub. Loading methods vary with datasets.
##### **Load PaddleHub's own datasets.**
If you are using PaddleHub's own dataset, you can use PaddleHub's dataset API to write a single line of code to load the dataset.
```python
dataset = hub.dataset.ChnSentiCorp()
```
ChnSentiCorp is a Chinese sentiment analysis dataset, and aims to determine the sentiment attitude of a paragraph of texts. For example, if the text is "The food is delicious", the corresponding label is "1", indicating a positive evaluation; or if the text is "The room is too small", the corresponding label is "0", indicating a negative evaluation. PaddleHub also provides other text classification datasets. You can choose the corresponding API to replace the values of the dataset in the above code, as listed in the table below.
| Dataset | Name | API |
| :------------------: | :----------------------------------------------------------: | :---------------------------: |
| ChnSentiCorp | Chinese Sentiment Analysis Dataset | hub.dataset.ChnSentiCorp() |
| LCQMC | A question-and-answer matching Chinese dataset constructed by Harbin Institute of Technology at the International Summit on Natural Language Processing COLING2018, with the goal of determining whether the semantics of two questions are the same. | hub.dataset.LCQMC() |
| NLPCC-DPQA | The goal of the evaluation task dataset held by the International Natural Language Processing and Chinese Computing Conference NLPCC in 2016 is to select answers that can answer the questions. | hub.dataset.NLPCC_DPQA() |
| MSRA-NER | A dataset released by Microsoft Asian Research Institute that aims to identify named entities | hub.dataset.MSRA-NER() |
| Toxic | English multi-labeled taxonomic datasets | hub.dataset.Toxic() |
| SQUAD | English reading comprehension dataset | hub.dataset.SQUAD() |
| GLUE-CoLA | Text Classification Task Dataset | hub.dataset.GLUE("CoLA") |
| GLUE-SST-2 | Sentiment Analysis Task Dataset | hub.dataset.GLUE("SST-2") |
| GLUE-MNLI | Text Reasoning Task Dataset | hub.dataset.GLUE("MNLI_m") |
| GLUE-QQP | Sentence pairs Classification Task Dataset | hub.dataset.GLUE("QQP") |
| GLUE-QNLI | Problem Inference Task Dataset | hub.dataset.GLUE("QNLI") |
| GLUE-STS-B | Regression Task Dataset | hub.dataset.GLUE("STS-B") |
| GLUE-MRPC | Sentence pairs Classification Task Dataset | hub.dataset.GLUE("MRPC") |
| GLUE-RTE | Text implication task dataset | hub.dataset.GLUE("RTE") |
| XNLI | Cross-language natural language inference dataset | hub.dataset.XNLI(language=zh) |
| ChineseGLUE-TNEWS | Today's Headlines Chinese News (Short text) Classified Dataset | hub.dataset.TNews() |
| ChineseGLUE-INEWS | Internet Sentiment Analysis Task Dataset | hub.dataset.INews() |
| DRCD | Delta Reading Comprehension Dataset, a General Domain Traditional Chinese Character Machine Reading Comprehension Dataset | hub.dataset.DRCD() |
| CMRC2018 | Span Extraction Dataset for Chinese Machine Reading Comprehension | hub.dataset.CMRC2018() |
| ChinesGLUE-BQ | Intelligent Customer Service Chinese Question Matching Dataset | hub.dataset.BQ() |
| ChineseGLUE-IFLYTEK | Chinese long-text classification dataset with over 17,000 text markups on app | hub.dataset.IFLYTEK() |
| ChineseGLUE-THUCNEWS | Chinese long text classification dataset, with more than 40,000 Chinese news long text markup data, with 14 categories in total. | hub.dataset.THUCNEWS() |
| DogCatDataset | Dataset provided by Kaggle for image binning. | hub.dataset.DogCatDataset() |
| Food101 | Food photo dataset provided by Kaggle, containing 101 categories | hub.dataset.Food101() |
| Indoor67 | A dataset of 67 indoor scenes released by MIT with the goal of identifying the scene category of an indoor image. | hub.dataset.Indoor67() |
| Flowers | Flower dataset. There are 5 types of datasets, including "roses", "tulips", "daisy", "sunflowers", and "dandelion". | hub.dataset.Flowers() |
| StanfordDogs | Dataset released by Stanford University, containing 120 species of dogs for image classification. | hub.dataset.StanfordDogs() |
##### **Load custom datasets**
* Load the text class custom dataset. The user only needs to inherit the base class BaseNLPDatast and modify the dataset address and category. Refer to the following codes.
```python
from paddlehub.dataset.base_nlp_dataset import BaseNLPDataset
# Define Class
class DemoDataset(BaseNLPDataset):
def __init__(self):
# path of dateset
self.dataset_dir = "path/to/dataset"
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_file="train.tsv", # training dataset
dev_file="dev.tsv", # dev dataset
test_file="test.tsv", # test dataset
predict_file="predict.tsv",
train_file_with_header=True,
dev_file_with_header=True,
test_file_with_header=True,
predict_file_with_header=True,
# Dataset Label
label_list=["0", "1"])
# Create Dataset Object
dataset = DemoDataset()
```
Then you can get the custom dataset by DemoDataset(). With the data preprocessor and pre-training models such as ERNIE, you can complete the text class task.
* Load the image class custom dataset. The user only needs to inherit the base class BaseCVDatast and modify the dataset storage address. Refer to the following codes.
```python
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
class DemoDataset(BaseCVDataset):
def __init__(self):
# Path of dataset
self.dataset_dir = "/test/data"
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_list_file="train_list.txt",
validate_list_file="validate_list.txt",
test_list_file="test_list.txt",
predict_file="predict_list.txt",
label_list_file="label_list.txt",
)
dataset = DemoDataset()
```
Then you can get the custom dataset by DemoDataset(). With the data preprocessor and pre-training models, you can complete the vision class transfer learning task.
#### **4\. Configure the Data Preprocessor.**
Read data of NLP or CV dataset by using PaddleHub's Data Preprocessor API.
```python
reader = hub.reader.ClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
max_seq_len=128,
sp_model_path=module.get_spm_path(),
word_dict_path=module.get_word_dict_path())
```
For different task types, users can select different Readers.
| Data Readers | Description | Task Type | API Examples |
| :------------------------: | :----------------------------------------------------------: | :-------: | :----------------------------------------------------------- |
| ClassifyReader | It is the data preprocessor suitable for the Transformer pre-training model (ERNIE/BERT). | NLP | reader = hub.reader.ClassifyReader( dataset=dataset, vocab_path=module.get_vocab_path(), max_seq_len=128, sp_model_path=module.get_spm_path(), word_dict_path=module.get_word_dict_path()) |
| LACClassifyReader | A data preprocessor using the LAC module as a word cutter, suitable for tasks such as Senta and ELMo that require granularity-based word segmentation. | NLP | reader = hub.reader.LACClassifyReader( dataset=dataset, vocab_path=module.get_vocab_path()) |
| SequenceLabelReader | Sequence annotation preprocessor for the Transformer class model (ERNIE/BERT). | NLP | reader = hub.reader.SequenceLabelReader( dataset=dataset, vocab_path=module.get_vocab_path(), max_seq_len=128, sp_model_path=module.get_spm_path(), word_dict_path=module.get_word_dict_path()) |
| MultiLabelClassifyReader | Multi-label classification preprocessor for the Transformer class model (ERNIE/BERT). | NLP | reader = hub.reader.MultiLabelClassifyReader( dataset=dataset, vocab_path=module.get_vocab_path(), max_seq_len=128) |
| ReadingComprehensionReader | Reading comprehension task preprocessor for the Transformer class model (ERNIE/BERT). | NLP | reader = hub.reader.ReadingComprehensionReader( dataset=dataset, vocab_path=module.get_vocab_path(), max_seq_length=384) |
| RegressionReader | A data preprocessor suitable for regression tasks. | NLP | reader = hub.reader.RegressionReader( dataset=dataset, vocab_path=module.get_vocab_path(), max_seq_len=args.max_seq_len) |
| ImageClassificationReader | Preprocessor suitable for image classification data. Modify the size of the input image. Perform standardization, image broadening, and other operations. | CV | reader = hub.reader.ImageClassificationReader( image_width=module.get_expected_image_width(), image_height=module.get_expected_image_height(), images_mean=module.get_pretrained_images_mean(), images_std=module.get_pretrained_images_std(), dataset=dataset) |
#### **5\. Choose an Optimization Strategy.**
In PaddleHub, the Strategy class encapsulates a set of fine-tuning strategies for transfer learning. Strategies include what learning rate variation strategy for pre-training parameters, what type of optimizer, what type of regularization, and so on. In the text classification task, we use AdamWeightDecayStrategy optimization strategy. Refer to the following code.
```python
strategy = hub.AdamWeightDecayStrategy(
learning_rate=5e-5, # Fine-tune max learning rate
weight_decay=0.01, # defaule 0.01
warmup_proportion=0.1, #warmup_proportion>0, for example: 0.1
lr_scheduler="linear_decay",
)
```
PaddleHub also provides a variety of APIs for various optimization strategies, in addition to AdamWeightDecayStrategy.
| Optimization Strategies | Description | API Examples |
| :---------------------: | :----------------------------------------------------------- | :----------------------------------------------------------- |
| DefaultFinetuneStrategy | The default optimization strategy. The corresponding parameters are as follows: <br/>\* learning\_rate: The global learning rate. The default value is 1e-4. <br/>\* optimizer\_name: Optimizer\_name: The default is adam. <br/>\* regularization\_coeff: The regularization λ parameter. The default value is 1e-3. <br/> This optimization strategy is recommended for the image classification task. | strategy = hub.DefaultFinetuneStrategy( learning_rate=1e-4, optimizer_name="adam", regularization_coeff=1e-3) |
| AdamWeightDecayStrategy | A learning rate decay strategy based on Adam optimizer. The corresponding parameters are as follows: <br/>\* learning\_rate: Global learning rate. The default value is 1e-4. <br/>\* lr\_scheduler: Learning rate scheduling method. The default value is "linear\_decay". <br/>\* warmup\_proportion: Warmup proportion. <br/>\* weight\_decay: Learning rate decay rate. <br/>\* optimizer\_name: Optimizer name. The default value is adam.<br/> This optimization strategy is recommended for text classification, reading comprehension, and other tasks. | strategy = hub.AdamWeightDecayStrategy( learning_rate=1e-4, lr_scheduler="linear_decay", warmup_proportion=0.0, weight_decay=0.01, optimizer_name="adam") |
| L2SPFinetuneStrategy | Finetune strategy using the L2SP regular as the penalty factor. The corresponding parameters are as follows: <br/>\* learning\_rate: The global learning rate. The default value is 1e-4. <br/>\* optimizer\_name: Optimizer\_name: The default is adam. <br/>\* regularization\_coeff: The regularization λ parameter. The default value is 1e-3. | strategy = hub.L2SPFinetuneStrategy( learning_rate=1e-4, optimizer_name="adam", regularization_coeff=1e-3) |
| ULMFiTStrategy | The strategy implements the three strategies proposed in the ULMFiT paper: <br/> \* Slanted triangular learning rates: a strategy of learning to rise and then fall. <br/>\* Discriminative fine-tuning: a strategy of decreasing learning rates layer by layer. It can slow down the underlying update rate. <br/>\* Gradual unfreezing: a layer-by-layer unfreezing strategy. It gives a priority to updating the upper layer and then slowly unfreezes the lower layer, to participate in the update. <br/> The corresponding parameters are as follows: <br/> \* learning\_rate: The global learning rate. The default value is 1e-4. <br/> \* optimizer\_name: Optimizer\_name: The default value is adam. <br/> \* cut\_fraction: Sets the ratio of number of steps of learning rate increase of “Slanted triangular learning rates” to the total number of training steps. The default value is 0.1. If it is set to 0, “Slanted triangular learning rates” is not used. <br/> \* ratio: Sets the ratio of the decrease minimum learning rate to the increase maximum learning rate of “Slanted triangular learning rates”. The default value is 32, indicating that the minimum learning rate is 1/32 of the maximum learning rate. <br/> \* dis\_blocks: Sets the number of blocks in the Discriminative fine-tuning. The default value is 3. If it is set to 0, the Discriminative fine-tuning is not used. <br/> \* factor: Sets the decay rate of the Discriminative fine-tuning. The default value is 2.6, indicating that the learning rate of the next layer is 1/2.6 of the upper layer. <br/> \* frz\_blocks: Sets the number of blocks in the gradual unfreezing. The concept of block is the same as that in "dis\_blocks". | strategy = hub.ULMFiTStrategy( learning_rate=1e-4, optimizer_name="adam", cut_fraction=0.1, ratio=32, dis_blocks=3, factor=2.6, frz_blocks=3) |
#### **6\. Set the RunConfig.**
In PaddleHub, users can use RunConfig in the Finetune API to configure the parameters when a Task is in Fine-tune, including Epochs, batch size, and whether to use GPU for training. The example codes are as follows:
```python
config = hub.RunConfig(use_cuda=True, num_epoch=3, batch_size=32, strategy=strategy)
```
#### **7\. Assemble the Training Task.**
Once we have a suitable pre-training model and load the dataset to be transferred, we start to assemble a Task. In PaddleHub, the Task represents a fine-tuned task. The Task contains the information related to the execution of the task, such as Program, Reader, and RunConfig. The task description corresponding to the [TextClassifierTask](https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/reference/task/text_classify_task.md) can be found here. The specific implementation solution is as follows:
1. Get the context of the module (PaddleHub's pre-training model), including the input and output variables, and Paddle Program (executable model format).
2. Find the feature\_map of the extraction layer from the output variables of the pre-training model, and access a full connection layer behind the feature\_map. In the following codes, it is specified by the pooled\_output parameter of the hub.TextClassifierTask.
3. The input layer of the network remains unchanged, still starting from the input layer, as specified in the following code via the feed\_list variable of the hub.TextClassifierTask. The hub.TextClassifierTask specifies our requirements for intercepting the model network with these two parameters. According to this configuration, the network we intercept is from the input layer to the last layer of feature extraction "pooled\_output", indicating that we will use the intercepted Network for transfer learning training.
```python
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
pooled_output = outputs["pooled_output"]
feed_list = [
inputs["input_ids"].name,
inputs["position_ids"].name,
inputs["segment_ids"].name,
inputs["input_mask"].name,
]
cls_task = hub.TextClassifierTask(
data_reader=reader,
feature=pooled_output,
feed_list=feed_list,
num_classes=dataset.num_labels,
metrics_choices = ["acc"],
config=config)
```
PaddleHub comes with pre-built Tasks for common tasks. Each task has a specific application scenario and corresponding metrics to meet the different user requirements.
| Task Type | Description | Task Type |
| :----------------------: | :----------------------------------------------------------: | :-----------------------------: |
| ImageClassifierTask | The Task is added with one or more full connection layers to create a classification task for Fine-tune based on the input features. The metric is the accuracy, and the loss function is the cross-entropy Loss. | Image classification task |
| TextClassifierTask | The Task is added with a Dropout layer and one or more full connection layers to create a text classification task for fine-tune based on the input features. The metric is accuracy, and the loss function is cross-entropy Loss. | Text classification task |
| SequenceLabelTask | The Task is added with one full connection layer or a full connection layer and CRF layer to create a sequence label task for Fine-tune based on the input features. The metric is F1, and the loss function is the cross-entropy Loss. | Sequence Annotation Task |
| MultiLabelClassifierTask | The Task is added with one or more full connection layers to create a multi-label classification task for fine-tune based on the input features. The metric is average AUC of multiple labels, and the loss function is the average cross-entropy of multiple labels. | Multi-label classification task |
| RegressionTask | The Task is added with a Dropout layer and one or more full connection layers to create a text regression task for fine-tune based on the input features. The metric is accuracy, and the loss function is the mean variance loss function. | Text Regression Task |
| ReadingComprehensionTask | The Task is added with one full connection layer to create a reading comprehension task for fine-tune based on the input features. The loss function is the cross-entropy Loss. | Reading Comprehension Task |
Before setting each Task, the user needs to know the input and output of the pre-training model of the transfer learning, that is, "feed\_list" and "pooled\_output" in the above codes. The specific input and output codes can be divided into the following:
* Image classification model
```
input_dict, output_dict, program = module.context(trainable=True)
feature_map = output_dict["feature_map"]
feed_list = [input_dict["image"].name]
```
* Natural language processing model (excluding word2vec\_skipgram, simnet\_bow, text matching, and text generation models).
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
pooled_output = outputs["pooled_output"]
feed_list = [
inputs["input_ids"].name,
inputs["position_ids"].name,
inputs["segment_ids"].name,
inputs["input_mask"].name,
]
```
* word2vec\_skipgram model
```
inputs, outputs, program = module.context(trainable=True)
word_ids = inputs["word_ids"]
embedding = outputs["word_embs"]
```
* simnet\_bow model
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=2)
query = outputs["emb"]
title = outputs['emb_2']
```
* Pairwise Text Matching Model
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=3)
query = outputs["emb"]
left = outputs['emb_2']
right = outputs['emb_3']
```
* Pointwise Text Matching
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=args.max_seq_len, num_slots=2)
query = outputs["emb"]
title = outputs['emb_2']
```
* Text Generation Model
```
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
pooled_output = outputs["pooled_output"]
sequence_output = outputs["sequence_output"]
```
#### **8\. Start the Fine-tune. Use the Finetune\_and\_eval Function to Complete the Training and Evaluation.**
```python
cls_task.finetune_and_eval()
```
The information is displayed as follows: You can see the evaluation results of the training, Loss value, accuracy, and so on.
```
[2020-07-28 21:28:21,658] [ TRAIN] - step 810 / 900: loss=0.05022 acc=0.97813 [step/sec: 4.07]
[2020-07-28 21:28:24,115] [ TRAIN] - step 820 / 900: loss=0.04719 acc=0.98125 [step/sec: 4.07]
[2020-07-28 21:28:26,574] [ TRAIN] - step 830 / 900: loss=0.06895 acc=0.98125 [step/sec: 4.07]
[2020-07-28 21:28:29,035] [ TRAIN] - step 840 / 900: loss=0.07830 acc=0.97813 [step/sec: 4.07]
[2020-07-28 21:28:31,490] [ TRAIN] - step 850 / 900: loss=0.07279 acc=0.97500 [step/sec: 4.08]
[2020-07-28 21:28:33,939] [ TRAIN] - step 860 / 900: loss=0.03220 acc=0.99375 [step/sec: 4.09]
[2020-07-28 21:28:36,388] [ TRAIN] - step 870 / 900: loss=0.05016 acc=0.98750 [step/sec: 4.09]
[2020-07-28 21:28:38,840] [ TRAIN] - step 880 / 900: loss=0.05604 acc=0.98750 [step/sec: 4.08]
[2020-07-28 21:28:41,293] [ TRAIN] - step 890 / 900: loss=0.05622 acc=0.98125 [step/sec: 4.08]
[2020-07-28 21:28:43,748] [ TRAIN] - step 900 / 900: loss=0.06642 acc=0.97813 [step/sec: 4.08]
[2020-07-28 21:28:43,750] [ INFO] - Evaluation on dev dataset start
[2020-07-28 21:28:46,654] [ EVAL] - [dev dataset evaluation result] loss=0.17890 acc=0.94079 [step/sec: 13.23]
[2020-07-28 21:28:46,657] [ INFO] - Evaluation on dev dataset start
[2020-07-28 21:28:49,527] [ EVAL] - [dev dataset evaluation result] loss=0.17890 acc=0.94079 [step/sec: 13.39]
[2020-07-28 21:28:49,529] [ INFO] - Load the best model from ckpt_20200728212416/best_model
[2020-07-28 21:28:50,112] [ INFO] - Evaluation on test dataset start
[2020-07-28 21:28:52,987] [ EVAL] - [test dataset evaluation result] loss=0.14264 acc=0.94819 [step/sec: 13.36]
[2020-07-28 21:28:52,988] [ INFO] - Saving model checkpoint to ckpt_20200728212416/step_900
[2020-07-28 21:28:55,789] [ INFO] - PaddleHub finetune finished.
```
After completing the model training with Fine-tune, PaddleHub will automatically save the best model in the validation set in the corresponding ckpt directory (CKPT\_DIR). The user can make predictions with reference to the following codes, where the inferred label value 0 indicates a negative evaluation, and 1 indicates a positive evaluation.
```python
import numpy as np
# 待预测数据
data = [
["这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般"],
["交通方便;环境很好;服务态度很好 房间较小"],
["19天硬盘就罢工了,算上运来的一周都没用上15天,可就是不能换了。唉,你说这算什么事呀!"]
]
index = 0
run_states = cls_task.predict(data=data)
results = [run_state.run_results for run_state in run_states]
for batch_result in results:
# 预测类别取最大分类概率值
batch_result = np.argmax(batch_result[0], axis=1)
for result in batch_result:
print("%s\tpredict=%s" % (data[index][0], result))
index += 1
```
The prediction results are as follows:
```
[2020-07-28 18:06:45,441] [ INFO] - PaddleHub predict start
[2020-07-28 18:06:45,442] [ INFO] - The best model has been loaded
[2020-07-28 18:06:48,406] [ INFO] - PaddleHub predict finished.
这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般 predict=0
交通方便;环境很好;服务态度很好 房间较小 predict=1
19天硬盘就罢工了,算上运来的一周都没用上15天,可就是不能换了。唉,你说这算什么事呀! predict=0
```
# Customized Data
In the training of a new task, it is a time-consuming process starting from zero, without producing the desired results probably. You can use the pre-training model provided by PaddleHub for fine-tune of a specific task. You just need to perform pre-processing of the customized data accordingly, and then input the pre-training model to get the corresponding results. Refer to the following for setting up the structure of the dataset.
## I. Image Classification Dataset
When migrating a classification task using custom data with PaddleHub, you need to slice the dataset into training set, validation set, and test set.
### Data Preparation
Three text files are needed to record the corresponding image paths and labels, plus a label file to record the name of the label.
```
├─data:
  ├─train_list.txt:
  ├─test_list.txt:
  ├─validate_list.txt:
├─label_list.txt:
  └─...
```
The format of the data list file for the training/validation/test set is as follows:
```
Path-1 label-1
Path-2 label-2
...
```
The format of label\_list.txt is:
```
Classification 1
Classification 2
...
```
Example: Take [Flower Dataset](../reference/dataset.md) as an example, train\_list.txt/test\_list.txt/validate\_list.txt:
```
roses/8050213579_48e1e7109f.jpg 0
sunflowers/45045003_30bbd0a142_m.jpg 3
daisy/3415180846_d7b5cced14_m.jpg 2
```
label\_list.txt reads as follows.
```
roses
tulips
daisy
sunflowers
dandelion
```
### Dataset Loading
For the preparation code of dataset, see [flowers.py](../../paddlehub/datasets/flowers.py). `hub.datasets.Flowers()` It automatically downloads the dataset from the network and unzip it into the `$HOME/.paddlehub/dataset` directory. Specific usage:
```python
from paddlehub.datasets import Flowers
flowers = Flowers(transforms)
flowers_validate = Flowers(transforms, mode='val')
```
* `transforms`: Data pre-processing mode.
* `mode`: Select data mode. Options are `train`, `test`, `val`. Default is `train`.
## II. Image Coloring Dataset
When migrating a coloring task using custom data with PaddleHub, you need to slice the dataset into training set and test set.
### Data Preparation
You need to divide the color images for coloring training and testing into training set data and test set data.
```
├─data:
  ├─train:
|-folder1
|-folder2
|-……
|-pic1
|-pic2
|-……
  ├─test:
|-folder1
|-folder2
|-……
|-pic1
|-pic2
|-……
  └─……
```
Example: PaddleHub provides users with a dataset for coloring `Canvas dataset. It consists of 1193 images in Monet style and 400 images in Van Gogh style. Take [Canvas Dataset](../reference/datasets.md) as an example, the contents of the train folder are as follows:
```
├─train:
|-monet
|-pic1
|-pic2
|-……
|-vango
|-pic1
|-pic2
|-……
```
### Dataset Loading
For dataset preparation codes, refer to [canvas.py](../../paddlehub/datasets/canvas.py). `hub.datasets.Canvas()` It automatically downloads the dataset from the network and unzip it into the `$HOME/.paddlehub/dataset` directory. Specific usage:
```python
from paddlehub.datasets import Canvas
color_set = Canvas(transforms, mode='train')
```
* `transforms`: Data pre-processing mode.
* `mode`: Select data mode. Options are `train`, `test`. The default is `train`.
## III. Style Migration Dataset
When using custom data for style migration tasks with PaddleHub, you need to slice the dataset into training set and test set.
### Data Preparation
You need to split the color images for style migration into training set and test set data.
```
├─data:
  ├─train:
|-folder1
|-folder2
|-...
|-pic1
|-pic2
|-...
  ├─test:
|-folder1
|-folder2
|-...
|-pic1
|-pic1
|-...
|- 21styles
|-pic1
|-pic1
  └─...
```
Example: PaddleHub provides users with data sets for style migration `MiniCOCO dataset`. Training set data and test set data is from COCO2014, in which there are 2001 images in the training set and 200 images in the test set. There are 21 images with different styles in `21styles` folder. Users can change the images of different styles as needed. Take [MiniCOCO Dataset](../reference/datasets.md) as an example. The content of train folder is as follows:
```
├─train:
|-train
|-pic1
|-pic2
|-……
|-test
|-pic1
|-pic2
|-……
|-21styles
|-pic1
|-pic2
|-……
```
### Dataset Loading
For the preparation codes of the dataset, refer to [minicoco.py](../../paddlehub/datasets/minicoco.py). `hub.datasets.MiniCOCO()` It automatically downloads the dataset from the network and unzip it into the `$HOME/.paddlehub/dataset` directory. Specific usage:
```python
from paddlehub.datasets import MiniCOCO
ccolor_set = MiniCOCO(transforms, mode='train')
```
* `transforms`: Data pre-processing mode.
* `mode`: Select data mode. Options are `train`, `test`. The default is `train`.
========
Tutorial
========
.. toctree::
:maxdepth: 2
:titlesonly:
Command Usage<tutorial/cmd_usage>
Hub Serving<tutorial/serving>
How to custom Module<tutorial/custom_module>
Transfer learning<transfer_learning_index>
\ No newline at end of file
sphinx==3.1.2 sphinx==3.1.2
sphinx-markdown-tables==0.0.15 sphinx-markdown-tables==0.0.15
sphinx-rtd-theme==0.5.0 sphinx_materialdesign_theme==0.1.11
recommonmark==0.6.0 recommonmark==0.6.0
sphinx-serve==1.0.1 sphinx-serve==1.0.1
sphinxcontrib-applehelp==1.0.2 sphinxcontrib-applehelp==1.0.2
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册