diff --git a/README.md b/README.md
index 772846dbe68e03503db9667cc2b0f25e5a3fd036..3244070932a252c882f9632dfb7f300e27a231ee 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,6 @@
-
------------------------------------------------------------------------------------------
[](LICENSE)
@@ -24,10 +23,10 @@
- **2020.09.27**,新增文本生成模型6个,图像分割模型1个,预训练模型总量到达 **【154】** 个。
- **2020.08.13**,发布v1.8.1,新增人像分割模型Humanseg,支持EMNLP2019-Sentence-BERT作为文本匹配任务网络,预训练模型总量到达 **【147】** 个。
- **2020.07.29**,发布v1.8.0,新增AI对联和AI写诗、jieba切词,文本数据LDA、语义相似度计算,新增目标检测,短视频分类模型,超轻量中英文OCR,新增行人检测、车辆检测、动物识别等工业级模型,支持VisualDL可视化训练,预训练模型总量到达 **【135】** 个。
-- [More](./docs/release.md)
+- [More](./docs/docs_ch/release.md)
-## [特性](./docs/figures.md)
+## [特性](./docs/docs_ch/figures.md)
- **【丰富的预训练模型】**:涵盖CV、NLP、Audio、Video主流四大品类的 180+ 预训练模型,全部开源下载,离线可运行。
- **【一键模型快速预测】**:通过一行命令行或者极简的Python API实现模型调用,可快速体验模型效果。
- **【一键模型转服务化】**:一行命令,搭建深度学习模型API服务化部署能力。
@@ -193,13 +192,13 @@
## 文档教程
-- [PIP安装](./docs/installation.md)
+- [PIP安装](./docs/docs_ch/installation.md)
- 快速开始
- - [命令行调用](./docs/quick_experience/cmd_quick_run.md)
- - [Python API调用](./docs/quick_experience/python_use_hub.md)
- - [示例体验项目demo](./docs/quick_experience/more_demos.md)
-- 丰富的预训练模型 182 个
- - [精品特色模型](./docs/figures.md)
+ - [命令行调用](./docs/docs_ch/quick_experience/cmd_quick_run.md)
+ - [Python API调用](./docs/docs_ch/quick_experience/python_use_hub.md)
+ - [示例体验项目demo](./docs/docs_ch/quick_experience/more_demos.md)
+- 丰富的预训练模型 182
+ - [精品特色模型](./docs/docs_ch/figures.md)
- 计算机视觉 126 个
- [图像分类 64 个](./modules/image/classification/README.md)
- [目标检测 13 个](./modules/image/object_detection/README.md)
@@ -221,19 +220,18 @@
- 视频5个
- [视频分类 5 个](./modules/video/README.md)
- 部署
- - [本地Inference部署](./docs/quick_experience/python_use_hub.md)
- - [一行代码服务化部署](./docs/tutorial/serving.md)
+ - [本地Inference部署](./docs/docs_ch/quick_experience/python_use_hub.md)
+ - [一行代码服务化部署](./docs/docs_ch/tutorial/serving.md)
- [移动端 Lite 部署(跳转Lite教程)](https://paddle-lite.readthedocs.io/zh/latest/quick_start/tutorial.html)
- 进阶文档
- - [命令行工具详解](./docs/tutorial/cmdintro.md)
- - [自定义数据迁移学习](./docs/tutorial/how_to_load_data.md)
- - [文本Embedding任务](./docs/tutorial/bert_service.md)
+ - [命令行工具详解](./docs/docs_ch/tutorial/cmdintro.md)
+ - [自定义数据迁移学习](./docs/docs_ch/tutorial/how_to_load_data.md)
- 社区交流
- [加入技术交流群](#欢迎加入PaddleHub技术交流群)
- - [贡献预训练模型](./docs/contribution/contri_pretrained_model.md)
- - [贡献代码](./docs/contribution/contri_pr.md)
-- [FAQ](./docs/faq.md)
-- [更新历史](./docs/release.md)
+ - [贡献预训练模型](./docs/docs_ch/contribution/contri_pretrained_model.md)
+ - [贡献代码](./docs/docs_ch/contribution/contri_pr.md)
+- [FAQ](./docs/docs_ch/faq.md)
+- [更新历史](./docs/docs_ch/release.md)
- [许可证书](#许可证书)
- [致谢](#致谢)
diff --git a/README_en.md b/README_en.md
new file mode 100644
index 0000000000000000000000000000000000000000..31be49fc24194916d69b9770fb8fda906f47d61d
--- /dev/null
+++ b/README_en.md
@@ -0,0 +1,238 @@
+
+
+
+
+------------------------------------------------------------------------------------------
+
+[](LICENSE)
+[](https://github.com/PaddlePaddle/PaddleHub/releases)
+
+
+
+## Introduction
+- PaddleHub aims to provide developers with rich, high-quality, and directly usable pre-trained models.
+- **【No need for deep learning background, no data and training process】**,you can use AI models quickly and enjoy the dividends of the artificial intelligence era.
+- Covers the four major categories of CV, NLP, Audio, and Video, and supports **one-click prediction**, **one-click service deployment** and **migration learning**
+- All models are open source download, **offline can run**.
+
+**Recent updates**
+- 2020.11.20: Release 2.0-beta version, fully migrate the dynamic graph programming mode, and upgrade the service deployment Serving capability; add 1 hand key detection model, 12 image animation models, 3 image editing models, 3 speech synthesis models, syntax Analyzing one, the total number of pre-trained models reaches **【182】**.
+- 2020.10.09: Added 4 new OCR multi-language series models, 4 image editing models, and the total number of pre-trained models reached **【162】**.
+- 2020.09.27: 6 new text generation models and 1 image segmentation model were added, and the total number of pre-trained models reached **【154】**.
+- 2020.08.13: Released v1.8.1, added a segmentation model, and supports EMNLP2019-Sentence-BERT as a text matching task network. The total number of pre-training models reaches **【147】**.
+- 2020.07.29: Release v1.8.0, new AI couplets and AI writing poems, jieba word cutting, text data LDA, semantic similarity calculation, new target detection, short video classification model, ultra-lightweight Chinese and English OCR, new pedestrian detection, vehicle Industrial-grade models such as detection and animal recognition support VisualDL visualization training, and the total number of pre-training models reaches **【135】**.
+
+
+## Features
+- **【Abundant Pre-trained Models】**: 180+ pre-trained models covering the four major categories of CV, NLP, Audio, and Video, all open source downloads, and can be run offline.
+- **【Quick Model Prediction】**: Model calls can be realized through a one-line command line or a minimalist Python API to quickly experience the model effect.
+- **【Model As Service】**: A one-line command to build deep learning model API service deployment capabilities.
+- **【Ten Lines of Code for Transfer Learning】**: Ten lines of code complete the transfer-learning task of image classification and text classification
+- **【PIP installation 】**: Support PIP quick installation and use
+- **【Cross-platform Compatibility】**: Can run on Linux, Windows, MacOS and other operating systems
+
+## Visualization
+### Text recognition
+- Contains ultra-lightweight Chinese and English OCR models, high-precision Chinese and English, multilingual German, French, Japanese, Korean OCR recognition.
+
-
* 使用词法分析模型LAC进行分词
@@ -85,7 +85,7 @@ $ hub serving start -m chinese_ocr_db_crnn_mobile
* [Demo示例](../demo)提供丰富的Fine-tune API的使用代码,包括[图像分类](../demo/image_classification)、[图像着色](../demo/colorization)、[风格迁移](../demo/style_transfer)、等场景的模型迁移示例。
-
diff --git a/docs/index.rst b/docs/docs_ch/index.rst
similarity index 100%
rename from docs/index.rst
rename to docs/docs_ch/index.rst
diff --git a/docs/installation.md b/docs/docs_ch/installation.md
similarity index 100%
rename from docs/installation.md
rename to docs/docs_ch/installation.md
diff --git a/docs/overview.md b/docs/docs_ch/overview.md
similarity index 100%
rename from docs/overview.md
rename to docs/docs_ch/overview.md
diff --git a/docs/pretrained_models.md b/docs/docs_ch/pretrained_models.md
similarity index 100%
rename from docs/pretrained_models.md
rename to docs/docs_ch/pretrained_models.md
diff --git a/docs/quick_experience/cmd_quick_run.md b/docs/docs_ch/quick_experience/cmd_quick_run.md
similarity index 100%
rename from docs/quick_experience/cmd_quick_run.md
rename to docs/docs_ch/quick_experience/cmd_quick_run.md
diff --git a/docs/quick_experience/more_demos.md b/docs/docs_ch/quick_experience/more_demos.md
similarity index 100%
rename from docs/quick_experience/more_demos.md
rename to docs/docs_ch/quick_experience/more_demos.md
diff --git a/docs/quick_experience/python_use_hub.md b/docs/docs_ch/quick_experience/python_use_hub.md
similarity index 100%
rename from docs/quick_experience/python_use_hub.md
rename to docs/docs_ch/quick_experience/python_use_hub.md
diff --git a/docs/quick_experience/quick_index.rst b/docs/docs_ch/quick_experience/quick_index.rst
similarity index 100%
rename from docs/quick_experience/quick_index.rst
rename to docs/docs_ch/quick_experience/quick_index.rst
diff --git a/docs/quickstart.md b/docs/docs_ch/quickstart.md
similarity index 100%
rename from docs/quickstart.md
rename to docs/docs_ch/quickstart.md
diff --git a/docs/reference/datasets.md b/docs/docs_ch/reference/datasets.md
similarity index 100%
rename from docs/reference/datasets.md
rename to docs/docs_ch/reference/datasets.md
diff --git a/docs/reference/datasets_index.rst b/docs/docs_ch/reference/datasets_index.rst
similarity index 100%
rename from docs/reference/datasets_index.rst
rename to docs/docs_ch/reference/datasets_index.rst
diff --git a/docs/reference/finetune.md b/docs/docs_ch/reference/finetune.md
similarity index 100%
rename from docs/reference/finetune.md
rename to docs/docs_ch/reference/finetune.md
diff --git a/docs/reference/module.md b/docs/docs_ch/reference/module.md
similarity index 100%
rename from docs/reference/module.md
rename to docs/docs_ch/reference/module.md
diff --git a/docs/reference/ref_index.rst b/docs/docs_ch/reference/ref_index.rst
similarity index 100%
rename from docs/reference/ref_index.rst
rename to docs/docs_ch/reference/ref_index.rst
diff --git a/docs/reference/vision.md b/docs/docs_ch/reference/vision.md
similarity index 94%
rename from docs/reference/vision.md
rename to docs/docs_ch/reference/vision.md
index d5c9e0f29e1d309764d046cfad3b47ffe5bf7882..722e48a88ddbe1fdc7f5779b9cde534432b280d4 100644
--- a/docs/reference/vision.md
+++ b/docs/docs_ch/reference/vision.md
@@ -44,7 +44,7 @@ Randomly flip the image vertically according to given probability.
```python
hub.vision.transforms.Resize(
- target_size: Union[List[int], int],
+ target_size: Union[List[int], int],
interpolation: str = 'LINEAR')
```
@@ -73,7 +73,7 @@ Resize the long side of the input image to the target size.
```python
hub.vision.transforms.ResizeRangeScaling(
- min_value: int = 400,
+ min_value: int = 400,
max_value: int = 600)
```
@@ -89,7 +89,7 @@ Randomly select a targeted size to resize the image according to given range.
```python
hub.vision.transforms.ResizeStepScaling(
- min_scale_factor: float = 0.75,
+ min_scale_factor: float = 0.75,
max_scale_factor: float = 1.25,
scale_step_size: float = 0.25)
```
@@ -107,7 +107,7 @@ Randomly select a scale factor to resize the image according to given range.
```python
hub.vision.transforms.Normalize(
- mean: list = [0.5, 0.5, 0.5],
+ mean: list = [0.5, 0.5, 0.5],
std: list =[0.5, 0.5, 0.5])
```
@@ -120,10 +120,10 @@ Normalize the input image.
# Class `hub.vision.transforms.Padding`
-
+
```python
hub.vision.transforms.Padding(
- target_size: Union[List[int], Tuple[int], int],
+ target_size: Union[List[int], Tuple[int], int],
im_padding_value: list = [127.5, 127.5, 127.5])
```
@@ -136,13 +136,13 @@ Normalize the input image.
# Class `hub.vision.transforms.RandomPaddingCrop`
-
+
```python
hub.vision.transforms.RandomPaddingCrop(
- crop_size(Union[List[int], Tuple[int], int]),
+ 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**
@@ -152,11 +152,11 @@ Normalize the input image.
# 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**
@@ -165,13 +165,13 @@ Normalize the input image.
# Class `hub.vision.transforms.RandomRotation`
-
+
```python
hub.vision.transforms.RandomRotation(
- max_rotation: float = 15.,
+ 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**
@@ -181,7 +181,7 @@ Normalize the input image.
# Class `hub.vision.transforms.RandomDistort`
-
+
```python
hub.vision.transforms.RandomDistort(
brightness_range: float = 0.5,
@@ -192,8 +192,8 @@ Normalize the input image.
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**
@@ -209,31 +209,31 @@ Normalize the input 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.
\ No newline at end of file
+* crop_size(int): Target size for croped image.
diff --git a/docs/release.md b/docs/docs_ch/release.md
similarity index 98%
rename from docs/release.md
rename to docs/docs_ch/release.md
index b98aacd4eb789dd2390848cdcdfe1791eb20fa11..99dffaa9449cae9e39703fb5087f60ca65e815da 100644
--- a/docs/release.md
+++ b/docs/docs_ch/release.md
@@ -4,7 +4,7 @@
* 全面迁移动态图编程模式,模型开发调试更加方便,finetune接口更加灵活易用。
* 优化服务化部署Serving能力,支持多卡预测、自动负载均衡,性能大幅度提升。
-* 视觉类迁移学习能力全面升级,支持[图像分类](../demo/image_classification)、[图像着色](../demo/colorization)、[风格迁移](../demo/style_transfer)等多种视觉任务。
+* 视觉类迁移学习能力全面升级,支持[图像分类](../../demo/image_classification)、[图像着色](../../demo/colorization)、[风格迁移](../../demo/style_transfer)等多种视觉任务。
## `v1.8.1`
diff --git a/docs/tutorial/Serving_index.rst b/docs/docs_ch/tutorial/Serving_index.rst
similarity index 100%
rename from docs/tutorial/Serving_index.rst
rename to docs/docs_ch/tutorial/Serving_index.rst
diff --git a/docs/tutorial/bert_service.md b/docs/docs_ch/tutorial/bert_service.md
similarity index 100%
rename from docs/tutorial/bert_service.md
rename to docs/docs_ch/tutorial/bert_service.md
diff --git a/docs/tutorial/cmdintro.md b/docs/docs_ch/tutorial/cmdintro.md
similarity index 99%
rename from docs/tutorial/cmdintro.md
rename to docs/docs_ch/tutorial/cmdintro.md
index 9936b34f1f7e2d2e7fbc57e6e7febe7b5edaad6e..e3869dbd41e1189cfd2689f3a4ead18494314f7c 100644
--- a/docs/tutorial/cmdintro.md
+++ b/docs/docs_ch/tutorial/cmdintro.md
@@ -103,6 +103,7 @@ $ hub run lac --input_file test.txt
# 单张照片预测
$ hub run resnet_v2_50_imagenet --input_path test.jpg
```
+
```shell
# 多张照片预测
$ hub run resnet_v2_50_imagenet --input_file test.txt
diff --git a/docs/docs_ch/tutorial/contri_pretrained_model.md b/docs/docs_ch/tutorial/contri_pretrained_model.md
new file mode 100644
index 0000000000000000000000000000000000000000..d73038da6b9dc00aa004cf837239d65088a8c6d7
--- /dev/null
+++ b/docs/docs_ch/tutorial/contri_pretrained_model.md
@@ -0,0 +1,309 @@
+# 如何编写一个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)
+```
diff --git a/docs/docs_ch/tutorial/contri_pretrained_model.md.orig b/docs/docs_ch/tutorial/contri_pretrained_model.md.orig
new file mode 100644
index 0000000000000000000000000000000000000000..d73038da6b9dc00aa004cf837239d65088a8c6d7
--- /dev/null
+++ b/docs/docs_ch/tutorial/contri_pretrained_model.md.orig
@@ -0,0 +1,309 @@
+# 如何编写一个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)
+```
diff --git a/docs/docs_ch/tutorial/how_to_finetune.md b/docs/docs_ch/tutorial/how_to_finetune.md
new file mode 100644
index 0000000000000000000000000000000000000000..057872fea5b93547d456007f36cec771bdb2a214
--- /dev/null
+++ b/docs/docs_ch/tutorial/how_to_finetune.md
@@ -0,0 +1,376 @@
+## **迁移学习**
+
+### **概述**
+迁移学习 (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的取值换成预训练模型名称即可,例如右侧红框中的示例。
+
+
+
+
+
+#### **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(
+
+
+ Transfer Learning within Ten Lines of Codes
+
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|以LAC模块为切词器的数据预处理器,适用于Senta、ELMo等需要以词粒度分词的任务。|NLP|reader = hub.reader.LACClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path())|
+|SequenceLabelReader|适用于Transformer类模型(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|适用于Transformer类模型(ERNIE/BERT)的多标签分类预处理器。|NLP|reader = hub.reader.MultiLabelClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
max_seq_len=128)|
+|ReadingComprehensionReader|适用于Transformer类模型(ERNIE/BERT)的阅读理解任务预处理器。|NLP|reader = hub.reader.ReadingComprehensionReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
max_seq_length=384)|
+|RegressionReader|适用于回归任务的数据预处理器。|NLP|reader = hub.reader.RegressionReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
max_seq_len=args.max_seq_len)|
+|ImageClassificationReader|适用于图像分类数据的预处理器。会修改输入图像的尺寸、进行标准化处理、图像增广处理等操作。|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. 选择优化策略**
+在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|默认的优化策略。其对应参数如下:
* learning_rate: 全局学习率。默认为1e-4。
* optimizer_name: 优化器名称。默认adam。
* regularization_coeff: 正则化的λ参数。默认为1e-3。
在图像分类任务中推荐使用此优化策略。|strategy = hub.DefaultFinetuneStrategy(
learning_rate=1e-4,
optimizer_name="adam",
regularization_coeff=1e-3)|
+|AdamWeightDecayStrategy|基于Adam优化器的学习率衰减策略。其对应参数如下:
* learning_rate: 全局学习率,默认为1e-4。
* lr_scheduler: 学习率调度方法,默认为"linear_decay"。
* warmup_proportion: warmup所占比重。
* weight_decay: 学习率衰减率。
* optimizer_name: 优化器名称,默认为adam。
在文本分类、阅读理解等任务中推荐使用此优化策略。|strategy = hub.AdamWeightDecayStrategy(
learning_rate=1e-4,
lr_scheduler="linear_decay",
warmup_proportion=0.0,
weight_decay=0.01,
optimizer_name="adam")|
+|L2SPFinetuneStrategy|使用L2SP正则作为惩罚因子的Finetune策略。其对应参数如下:
* learning_rate: 全局学习率。默认为1e-4。
* optimizer_name: 优化器名称。默认adam。
* regularization_coeff: 正则化的λ参数。默认为1e-3。|strategy = hub.L2SPFinetuneStrategy(
learning_rate=1e-4,
optimizer_name="adam",
regularization_coeff=1e-3)|
+|ULMFiTStrategy|该策略实现了ULMFiT论文中提出的三种策略:
* Slanted triangular learning rates是一种学习率先上升再下降的策略。
* Discriminative fine-tuning是一种学习率逐层递减的策略,通过该策略可以减缓底层的更新速度。
* Gradual unfreezing是一种逐层解冻的策略,通过该策略可以优先更新上层,再慢慢解冻下层参与更新。
其对应参数如下:
* learning_rate: 全局学习率。默认为1e-4。
* optimizer_name: 优化器名称。默认为adam。
* cut_fraction: 设置Slanted triangular learning rates学习率上升的步数在整个训练总步数中的比例。默认为0.1,如果设置为0,则不采用Slanted triangular learning rates。
* ratio: 设置Slanted triangular learning rates下降的最小学习率与上升的最大学习率的比例关系,默认为32,表示最小学习率是最大学习率的1/32。
* dis_blocks: 设置 Discriminative fine-tuning中的块数。默认为3,如果设置为0,则不采用Discriminative fine-tuning。
* factor: 设置Discriminative fine-tuning的衰减率。默认为2.6,表示下一层的学习率是上一层的1/2.6。
* frz_blocks: 设置Gradual unfreezing中的块数。块的概念同“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. 设置运行配置。**
+在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
+```
diff --git a/docs/docs_ch/tutorial/how_to_finetune.md.orig b/docs/docs_ch/tutorial/how_to_finetune.md.orig
new file mode 100644
index 0000000000000000000000000000000000000000..057872fea5b93547d456007f36cec771bdb2a214
--- /dev/null
+++ b/docs/docs_ch/tutorial/how_to_finetune.md.orig
@@ -0,0 +1,376 @@
+## **迁移学习**
+
+### **概述**
+迁移学习 (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的取值换成预训练模型名称即可,例如右侧红框中的示例。
+
+
+
+
+
+#### **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(
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|以LAC模块为切词器的数据预处理器,适用于Senta、ELMo等需要以词粒度分词的任务。|NLP|reader = hub.reader.LACClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path())|
+|SequenceLabelReader|适用于Transformer类模型(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|适用于Transformer类模型(ERNIE/BERT)的多标签分类预处理器。|NLP|reader = hub.reader.MultiLabelClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
max_seq_len=128)|
+|ReadingComprehensionReader|适用于Transformer类模型(ERNIE/BERT)的阅读理解任务预处理器。|NLP|reader = hub.reader.ReadingComprehensionReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
max_seq_length=384)|
+|RegressionReader|适用于回归任务的数据预处理器。|NLP|reader = hub.reader.RegressionReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
max_seq_len=args.max_seq_len)|
+|ImageClassificationReader|适用于图像分类数据的预处理器。会修改输入图像的尺寸、进行标准化处理、图像增广处理等操作。|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. 选择优化策略**
+在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|默认的优化策略。其对应参数如下:
* learning_rate: 全局学习率。默认为1e-4。
* optimizer_name: 优化器名称。默认adam。
* regularization_coeff: 正则化的λ参数。默认为1e-3。
在图像分类任务中推荐使用此优化策略。|strategy = hub.DefaultFinetuneStrategy(
learning_rate=1e-4,
optimizer_name="adam",
regularization_coeff=1e-3)|
+|AdamWeightDecayStrategy|基于Adam优化器的学习率衰减策略。其对应参数如下:
* learning_rate: 全局学习率,默认为1e-4。
* lr_scheduler: 学习率调度方法,默认为"linear_decay"。
* warmup_proportion: warmup所占比重。
* weight_decay: 学习率衰减率。
* optimizer_name: 优化器名称,默认为adam。
在文本分类、阅读理解等任务中推荐使用此优化策略。|strategy = hub.AdamWeightDecayStrategy(
learning_rate=1e-4,
lr_scheduler="linear_decay",
warmup_proportion=0.0,
weight_decay=0.01,
optimizer_name="adam")|
+|L2SPFinetuneStrategy|使用L2SP正则作为惩罚因子的Finetune策略。其对应参数如下:
* learning_rate: 全局学习率。默认为1e-4。
* optimizer_name: 优化器名称。默认adam。
* regularization_coeff: 正则化的λ参数。默认为1e-3。|strategy = hub.L2SPFinetuneStrategy(
learning_rate=1e-4,
optimizer_name="adam",
regularization_coeff=1e-3)|
+|ULMFiTStrategy|该策略实现了ULMFiT论文中提出的三种策略:
* Slanted triangular learning rates是一种学习率先上升再下降的策略。
* Discriminative fine-tuning是一种学习率逐层递减的策略,通过该策略可以减缓底层的更新速度。
* Gradual unfreezing是一种逐层解冻的策略,通过该策略可以优先更新上层,再慢慢解冻下层参与更新。
其对应参数如下:
* learning_rate: 全局学习率。默认为1e-4。
* optimizer_name: 优化器名称。默认为adam。
* cut_fraction: 设置Slanted triangular learning rates学习率上升的步数在整个训练总步数中的比例。默认为0.1,如果设置为0,则不采用Slanted triangular learning rates。
* ratio: 设置Slanted triangular learning rates下降的最小学习率与上升的最大学习率的比例关系,默认为32,表示最小学习率是最大学习率的1/32。
* dis_blocks: 设置 Discriminative fine-tuning中的块数。默认为3,如果设置为0,则不采用Discriminative fine-tuning。
* factor: 设置Discriminative fine-tuning的衰减率。默认为2.6,表示下一层的学习率是上一层的1/2.6。
* frz_blocks: 设置Gradual unfreezing中的块数。块的概念同“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. 设置运行配置。**
+在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
+```
diff --git a/docs/tutorial/how_to_load_data.md b/docs/docs_ch/tutorial/how_to_load_data.md
similarity index 97%
rename from docs/tutorial/how_to_load_data.md
rename to docs/docs_ch/tutorial/how_to_load_data.md
index 4ec4c8a842e93e8acb404496b9f36bb40e8ede58..7bb10669c2d05f4c7fe8608c050cdfb364c77abc 100644
--- a/docs/tutorial/how_to_load_data.md
+++ b/docs/docs_ch/tutorial/how_to_load_data.md
@@ -31,7 +31,7 @@ label_list.txt的格式如下
```
示例:
-以[Flower数据集](../reference/dataset.md)为示例,train_list.txt/test_list.txt/validate_list.txt内容如下示例
+以[Flower数据集](../reference/datasets.md)为示例,train_list.txt/test_list.txt/validate_list.txt内容如下示例
```
roses/8050213579_48e1e7109f.jpg 0
sunflowers/45045003_30bbd0a142_m.jpg 3
@@ -62,7 +62,7 @@ flowers_validate = Flowers(transforms, mode='val')
## 二、图像着色数据集
-利用PaddleHub迁移着色任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
+利用PaddleHub迁移着色任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
### 数据准备
@@ -116,7 +116,7 @@ color_set = Canvas(transforms, mode='train')
## 三、风格迁移数据集
-利用PaddleHub进行风格迁移任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
+利用PaddleHub进行风格迁移任务使用自定义数据时,需要切分数据集,将数据集切分为训练集和测试集。
### 数据准备
diff --git a/docs/tutorial/sentence_sim.md b/docs/docs_ch/tutorial/sentence_sim.md
similarity index 100%
rename from docs/tutorial/sentence_sim.md
rename to docs/docs_ch/tutorial/sentence_sim.md
diff --git a/docs/tutorial/serving.md b/docs/docs_ch/tutorial/serving.md
similarity index 93%
rename from docs/tutorial/serving.md
rename to docs/docs_ch/tutorial/serving.md
index d69dec50daca6db4098a4dfa67000515664271ee..6cb87eec4aba4880e0ec1415ddc8d026561e8b75 100644
--- a/docs/tutorial/serving.md
+++ b/docs/docs_ch/tutorial/serving.md
@@ -99,7 +99,7 @@ $ hub serving start --config config.json
### Step3:利用PaddleHub Serving进行个性化开发
使用PaddleHub Serving进行模型服务部署后,可以利用得到的接口进行开发,如对外提供web服务,或接入到应用程序中,以降低客户端预测压力,提高性能,下面展示了一个web页面demo:
-
+
### Step4:关闭serving
@@ -147,7 +147,7 @@ $ hub serving start -c serving_config.json
```
启动成功界面如图:
-
+
这样我们就在8866端口成功部署了lac的在线分词服务。
*此处warning为Flask提示,不影响使用*
@@ -227,13 +227,11 @@ $ PaddleHub Serving will stop.
## Demo——其他模型的一键部署服务
-* [中文词法分析-基于lac](../../demo/serving/module_serving/lexical_analysis_lac)
+* [中文词法分析-基于lac](../../../demo/serving/module_serving/lexical_analysis_lac)
该示例展示了利用lac完成中文文本分词服务化部署和在线预测,获取文本的分词结果,并可通过用户自定义词典干预分词结果。
-* [人脸检测-基于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)
该示例展示了利用pyramidbox_lite_server_mask完成人脸口罩检测,检测人脸位置以及戴口罩的置信度。
-## Bert Service
-除了预训练模型一键服务部署功能之外,PaddleHub Serving还具有`Bert Service`功能,支持ernie_tiny、bert等模型快速部署,对外提供可靠的在线embedding服务,具体信息请参见[Bert Service](./bert_service.md)。
diff --git a/docs/tutorial/tutorial_index.rst b/docs/docs_ch/tutorial/tutorial_index.rst
similarity index 100%
rename from docs/tutorial/tutorial_index.rst
rename to docs/docs_ch/tutorial/tutorial_index.rst
diff --git a/docs/docs_en/contribution/contri_index_en.rst b/docs/docs_en/contribution/contri_index_en.rst
new file mode 100644
index 0000000000000000000000000000000000000000..0f2bee1df4994a0be87152777cd34aaedf2ab30a
--- /dev/null
+++ b/docs/docs_en/contribution/contri_index_en.rst
@@ -0,0 +1,10 @@
+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
+### 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.
+
+| 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)
+
+
+## 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 in the following ways.
+
+### Method 1: 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.
+
+### Method 2: Upload the Module to the remote code hosting platform.
+
+PaddleHub also supports loading Modules directly to the remote code hosting platforms. The steps are as follows:
+
+#### Step 1: Create a new repository.
+
+To create a new Git repository on the code hosting platform, add the codes of the module we wrote earlier. To make it easier to manage different modules, we create a modules directory and put senta\_test in the modules directory.
+
+#### Step 2: Add a new configuration file`hubconf.py`.
+
+In the root directory, add a new configuration `hubconf.py` file, which references a class modified by `moduleinfo` as follows:
+
+```python
+from modules.senta_test.module import SentaTest
+```
+
+*The structure of the file at this point is as follows:*
+
+```
+hubconf.py
+modules
+├── senta_test/
+ ├── vocab.list
+ ├── module.py
+ └── processor.py
+```
+
+#### Step 3: Complete the commit and push to the remote repository.
+
+#### Step 4: Load Module in the remote repository locally.
+
+To facilitate the experience, we have stored the SentaTest codes on GitHub and Gitee. So you can directly experience the effect in the following ways:
+
+```python
+import paddlehub as hub
+
+senta_test = hub.Module(name='senta_test', source='https://github.com/nepeplwu/myhub.git')
+# senta_test = hub.Module(name='senta_test', source='https://gitee.com/nepeplwu/myhub.git')
+print(senta_test.sentiment_classify(texts=["这部电影太差劲了"]))
+```
diff --git a/docs/docs_en/figures_en.md b/docs/docs_en/figures_en.md
new file mode 100644
index 0000000000000000000000000000000000000000..dcc58204d022967109bcc762a60336b8cf89d6ab
--- /dev/null
+++ b/docs/docs_en/figures_en.md
@@ -0,0 +1,98 @@
+## Detailed Features
+
+
+
+### 1\. Various Pre-training Models
+
+#### 1.1. Image
+
+| | **Examples of Boutique Models** |
+| -------------------- | :----------------------------------------------------------- |
+| Image Classification | [Dish Identification](https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_dishes&en_category=ImageClassification), [Animal Identification](https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_animals&en_category=ImageClassification), [Animal Identification](https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_animals&en_category=ImageClassification), [-->More](../modules/image/classification/README.md) |
+| Object Detection | [Universal Detection](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_coco2017&en_category=ObjectDetection), [Pedestrian Detection](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_pedestrian&en_category=ObjectDetection), [Vehicle Detection](https://www.paddlepaddle.org.cn/hubdetail?name=yolov3_darknet53_vehicles&en_category=ObjectDetection), [-->More](../modules/image/object_detection/README.md) |
+| Face Detection | [Face Detection](https://www.paddlepaddle.org.cn/hubdetail?name=pyramidbox_lite_server&en_category=FaceDetection), [Mask Detection](https://www.paddlepaddle.org.cn/hubdetail?name=pyramidbox_lite_server_mask&en_category=FaceDetection), [-->More](../modules/image/face_detection/README.md) |
+| Image Segmentation | [Portrait Segmentation](https://www.paddlepaddle.org.cn/hubdetail?name=deeplabv3p_xception65_humanseg&en_category=ImageSegmentation), [Body Analysis](https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation), [Pneumonia CT Imaging Analysis](https://www.paddlepaddle.org.cn/hubdetail?name=Pneumonia_CT_LKM_PP&en_category=ImageSegmentation), [-->More](../modules/image/semantic_segmentation/README.md) |
+| Key Point Detection | [Body Key Points](https://www.paddlepaddle.org.cn/hubdetail?name=human_pose_estimation_resnet50_mpii&en_category=KeyPointDetection), [Face Key Points](https://www.paddlepaddle.org.cn/hubdetail?name=face_landmark_localization&en_category=KeyPointDetection), [Hands Key Points](https://www.paddlepaddle.org.cn/hubdetail?name=hand_pose_localization&en_category=KeyPointDetection), [-->More](./modules/image/keypoint_detection/README.md) |
+| Text Recognition | [Ultra Lightweight Chinese \& English OCR Text Recognition](https://www.paddlepaddle.org.cn/hubdetail?name=chinese_ocr_db_crnn_mobile&en_category=TextRecognition), [-->More](../modules/image/text_recognition/README.md) |
+| Image Generation | [Style Migration](https://www.paddlepaddle.org.cn/hubdetail?name=stylepro_artistic&en_category=GANs), [Street View Cartoon](https://www.paddlepaddle.org.cn/hubdetail?name=animegan_v2_hayao_99&en_category=GANs), [-->More](../modules/image/Image_gan/README.md) |
+| Image Editing | [Super Resolution](https://www.paddlepaddle.org.cn/hubdetail?name=realsr&en_category=ImageEditing), [B\&W Color](https://www.paddlepaddle.org.cn/hubdetail?name=deoldify&en_category=ImageEditing), [-->More](../modules/image/Image_editing/README.md) |
+
+#### 1.2 Text
+
+| | **Examples of Boutique Models** |
+| ------------------ | :----------------------------------------------------------- |
+| Word Analysis | [Linguistic Analysis](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis), [Syntactic Analysis](https://www.paddlepaddle.org.cn/hubdetail?name=ddparser&en_category=SyntacticAnalysis), [-->More](../modules/text/lexical_analysis/README.md) |
+| Sentiment Analysis | [Emotion Judgment](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis), [Emotion Analysis](https://www.paddlepaddle.org.cn/hubdetail?name=emotion_detection_textcnn&en_category=SentimentAnalysis), [-->More](../modules/text/sentiment_analysis/README.md) |
+| Text Review | [Porn Review](https://www.paddlepaddle.org.cn/hubdetail?name=porn_detection_gru&en_category=TextCensorship), [-->More](../modules/text/text_review/README.md) |
+| Text Generation | [Poetic Couplet Generation](https://www.paddlepaddle.org.cn/hubdetail?name=ernie_tiny_couplet&en_category=TextGeneration), [Love Letters Generation](https://www.paddlepaddle.org.cn/hubdetail?name=ernie_gen_poetry&en_category=TextGeneration), [Popular Love Letters](https://www.paddlepaddle.org.cn/hubdetail?name=ernie_gen_lover_words&en_category=TextGeneration), [-->More](../modules/text/text_generation/README.md) |
+| Semantic Models | [ERNIE](https://www.paddlepaddle.org.cn/hubdetail?name=ERNIE&en_category=SemanticModel), [Text Similarity](https://www.paddlepaddle.org.cn/hubdetail?name=simnet_bow&en_category=SemanticModel), [-->More](../modules/text/language_model/README.md) |
+
+#### 1.3. Speech
+
+| | **Examples of Boutique Models** |
+| -------------- | :-------------------------------------------------------- |
+| Text-to-speech | [Text-to-speech](https://www.paddlepaddle.org.cn/hubdetail?name=deepvoice3_ljspeech&en_category=TextToSpeech), [-->More](../modules/audio/README.md) |
+
+#### 1.4. Video
+
+| | **Examples of Boutique Models** |
+| -------------------- | :----------------------------------------------------------- |
+| Video Classification | [ Video Classification](https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=VideoClassification), [-->More](../modules/video/README.md) |
+
+
+
+### 2\. One-key Model Prediction
+
+* For example, if you use the lightweight Chinese OCR model chinese\_ocr\_db\_crnn\_mobile for text recognition, you can quickly recognize the text in an image with pressing one key.
+
+```shell
+$ pip install paddlehub
+$ 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
+```
+
+* The prediction results images are stored in the ocr\_result folder under the current path, as shown in the following figure.
+
+
+* Use the lexical analysis model LAC for word segmentation.
+
+```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']
+}]
+```
+
+In addition to one-line code prediction, PaddleHub also supports the use of API to revoke the model. For details, refer to the detailed documentation of each model.
+
+
+
+### 3\. One-Key to deploy Models as Services
+
+PaddleHub provides convenient model-to-service capability to deploy HTTP services for models with one simple command. The LAC lexical analysis service can quickly start with the following commands:
+
+```shell
+$ hub serving start -m chinese_ocr_db_crnn_mobile
+```
+
+For more instructions on using Model Serving, See PaddleHub Model One-Key Model Serving Deployment.
+
+
+
+### 4\. Transfer Learning within Ten Lines of Codes
+
+With the Fine-tune API, deep learning models can be migrated and learned in computer vision scenarios with a small number of codes.
+
+* The [Demo Examples](../demo) provides rich codes for using Fine-tune API, including [Image Classification](../demo/image_classification), [Image Coloring](../demo/colorization), [Style Migration](../demo/style_transfer), and other scenario model migration examples.
+
+
+
\* learning\_rate: The global learning rate. The default value is 1e-4.
\* optimizer\_name: Optimizer\_name: The default is adam.
\* regularization\_coeff: The regularization λ parameter. The default value is 1e-3.
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:
\* learning\_rate: Global learning rate. The default value is 1e-4.
\* lr\_scheduler: Learning rate scheduling method. The default value is "linear\_decay".
\* warmup\_proportion: Warmup proportion.
\* weight\_decay: Learning rate decay rate.
\* optimizer\_name: Optimizer name. The default value is adam.
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:
\* learning\_rate: The global learning rate. The default value is 1e-4.
\* optimizer\_name: Optimizer\_name: The default is adam.
\* 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:
\* Slanted triangular learning rates: a strategy of learning to rise and then fall.
\* Discriminative fine-tuning: a strategy of decreasing learning rates layer by layer. It can slow down the underlying update rate.
\* 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.
The corresponding parameters are as follows:
\* learning\_rate: The global learning rate. The default value is 1e-4.
\* optimizer\_name: Optimizer\_name: The default value is adam.
\* 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.
\* 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.
\* 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.
\* 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.
\* 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
+```
diff --git a/docs/docs_en/tutorial/how_to_load_data_en.md b/docs/docs_en/tutorial/how_to_load_data_en.md
new file mode 100644
index 0000000000000000000000000000000000000000..79970ec9980a20db4f7765c3674d3df14a663ff2
--- /dev/null
+++ b/docs/docs_en/tutorial/how_to_load_data_en.md
@@ -0,0 +1,185 @@
+# 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`.
diff --git a/docs/docs_en/tutorial/serving_en.md b/docs/docs_en/tutorial/serving_en.md
new file mode 100644
index 0000000000000000000000000000000000000000..788f19e06d834931126ab4043305679f215d8c75
--- /dev/null
+++ b/docs/docs_en/tutorial/serving_en.md
@@ -0,0 +1,252 @@
+# PaddleHub Serving : One-Key Deploy Models as Services
+
+## Introduction
+
+### Background
+
+PaddleHub enables the rapid model prediction. Developers are often faced with the need to migrate the local prediction process to go online. PaddleHub's ability to rapidly deploy model prediction services is required, whether to open public service ports or to build the prediction services on a LAN. In this background, the PaddleHub Serving, the model one-key service deployment tool, emerges. Developers can quickly launch a model prediction online service with using a single line of command, without having to concern about the network framework selection and implementation.
+
+### One-Key Service Deployment
+
+PaddleHub Serving is a one-key model service deployment tool based on PaddleHub. It can easily start a model prediction online service through a simple Hub command line tool. The front-end completes the processing of network requests through Flask and Gunicorn. The background directly invokes the PaddleHub prediction interface and supports multi-process to improve the concurrency by using multi-core to ensure the performance of the prediction service.
+
+### Supported Models
+
+Currently, PaddleHub Serving supports the service deployment for all direct prediction PaddleHub models, including `lac`, `senta_bilstm` and other NLP models, as well as `yolov3_darknet53_coco2017`, `vgg16_imagenet` and other CV models. For more models, refer to [PaddleHub Supported Models List](https://paddlepaddle.org.cn/hublist). In the future, developers will also be able to use the models from PaddleHub Fine-tune API for rapid service deployment.
+
+## Usage
+
+### Step 1: Start the server deployment.
+
+PaddleHub Serving starts in two ways: Starting in the command line, and Starting by using the configuration file.
+
+#### Start in command line
+
+Command
+
+```shell
+$ hub serving start --modules [Module1==Version1, Module2==Version2, ...] \
+ --port XXXX \
+ --use_gpu \
+ --use_multiprocess \
+ --workers \
+ --gpu \
+```
+
+Parameter:
+
+| Parameter | Purpose |
+| ------------------ | ------------------------------------------------------------ |
+| --modules/-m | PaddleHub Serving pre-installed model. It is listed as multiple Module==Version key value pairs
|
+| --port/-p | Service port. By default, it is 8866. |
+| --use_gpu | paddlepaddle-gpu must be installed when the GPU is used for prediction. |
+| --use_multiprocess | Whether to enable concurrent mode. By default, it is single process. This is recommended for multi-core CPUs
*`Only single process is supported in Windows system`* |
+| --workers | The number of concurrent tasks specified in the concurrent mode. By default, it is `2*cpu_count-1`, where `cpu_count` is the number of CPU cores. |
+| --gpu | Specify the card number of the GPU to be used. For example, `1,2` means using VGA card 1 and VGA card 2. By default, only VGA card 0 is used. |
+
+**NOTE:** --use\_gpu should not be used together with --use\_multiprocess.
+
+#### Start by using the configuration file
+
+Command
+
+```shell
+$ hub serving start --config config.json
+```
+
+`config.json`The format is as follows:
+
+```json
+{
+ "modules_info": {
+ "yolov3_darknet53_coco2017": {
+ "init_args": {
+ "version": "1.0.0"
+ },
+ "predict_args": {
+ "batch_size": 1,
+ "use_gpu": false
+ }
+ },
+ "lac": {
+ "init_args": {
+ "version": "1.1.0"
+ },
+ "predict_args": {
+ "batch_size": 1,
+ "use_gpu": false
+ }
+ }
+ },
+ "port": 8866,
+ "use_multiprocess": false,
+ "workers": 2,
+ "gpu": "0,1,2"
+}
+
+```
+
+Parameter:
+
+
+
+### Step 2: Access the server
+
+After deploying the server model prediction service using PaddleHub Serving, you can access the prediction interface on the client to get the results. The interface url format is:
+
+`http://127.0.0.1:8866/predict/