未验证 提交 8b31fef2 编写于 作者: S SunAhong1993 提交者: GitHub

fix the docs (#572)

上级 af4654b6
......@@ -97,7 +97,7 @@ x2paddle --convert_torch_project --project_dir=torch_project --save_dir=paddle_p
## 转换教程
1. [TensorFlow预测模型转换教程](./docs/inference_model_convertor/demo/tensorflow2paddle.ipynb)
2. [PyTorch预测模型转换教程](./docs/inference_model_convertor/demo/pytorch2paddle.ipynb)
3. [PyTorch训练项目转换教程](./docs/pytorch_project_convertor/demo.md)
3. [PyTorch训练项目转换教程](./docs/pytorch_project_convertor/demo/README.md)
## 更新历史
**2020.12.09**
......
......@@ -33,5 +33,5 @@ out =main(ipt)
```
> 若运行代码无误,则说明代码中有op不支持动转静,我们将会再未来支持;若报错,则说明pytorch2paddle转换出错,请提issue,我们将及时回复。
**Q5. 目前支持了哪些op的转换呢?**
A: 可详见[X2Paddle支持的op列表](./docs/introduction/op_list.md)
**Q5. 目前支持了哪些op的转换呢?**
A: 可详见[X2Paddle支持的op列表](./docs/inference_model_convertor/op_list.md)
......@@ -103,8 +103,7 @@
## PyTorch训练项目
| 模型 | 转换前代码 | 转换后代码 |
|------|----------|------|
| StaGAN | [code](https://github.com/yunjey/stargan)|[code](https://github.com/SunAhong1993/stargan/tree/paddle)|
| StarGAN | [code](https://github.com/yunjey/stargan)|[code](https://github.com/SunAhong1993/stargan/tree/paddle)|
| Ultra-Light-Fast-Generic-Face-Detector | [code](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB) |[code](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/tree/paddle)|
**注:** 受限于不同框架的差异,部分模型可能会存在目前无法转换的情况,如TensorFlow中包含控制流的模型,NLP模型等。对于CV常见的模型,如若您发现无法转换或转换失败,存在较大diff等问题,欢迎通过[ISSUE反馈](https://github.com/PaddlePaddle/X2Paddle/issues/new)的方式告知我们(模型名,代码实现或模型获取方式),我们会及时跟进:
**注:** 受限于不同框架的差异,部分预测模型可能会存在目前无法转换的情况,如TensorFlow中包含控制流的模型等。对于常见的预测模型或PyTorch项目,如若您发现无法转换或转换失败,存在较大diff等问题,欢迎通过[ISSUE反馈](https://github.com/PaddlePaddle/X2Paddle/issues/new)的方式告知我们(模型名,代码实现或模型获取方式),我们会及时跟进。
# PyTorch-PaddlePaddle API对应表
本文档梳理了常用PyTorch 1.8.1 API与PaddlePaddle 2.0.0 API对应关系和差异分析。根据文档对应关系,有PyTorch使用经验的用户,可根据对应关系,快速熟悉PaddlePaddle的API使用。
## [基础操作类](./ops/README.md)
## [组网类](./nn/README.md)
## [Loss类](./loss/README.md)
## [工具类](./utils/README.md)
## [视觉类](./vision/README.md)
***持续更新...***
| 类别 | 链接 |
| ---------- | ------------------------- |
| 基础操作类 | [映射表](./ops/README.md) |
| 组网类 | [映射表](./nn/README.md) |
| Loss类 |[映射表](./loss/README.md) |
| 工具类 | [映射表](./utils/README.md) |
| 视觉类 | [映射表](./vision/README.md) |
***持续更新...***## Loss类
## Loss类API映射列表
该文档梳理了计算loss相关的PyTorch-PaddlePaddle API映射列表。
| 序号 | PyTorch API | PaddlePaddle API | 备注 |
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| 1 | [torch.nn.L1Loss](https://pytorch.org/docs/stable/generated/torch.nn.L1Loss.html?highlight=l1loss#torch.nn.L1Loss) | [paddle.nn.loss.L1Loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/layer/loss/L1Loss_cn.html#l1loss) | 功能一致,PyTroch存在废弃参数`size_average``reduce`。 |
......
## 组网类
## 组网类API映射列表
该文档梳理了与构造网络相关的PyTorch-PaddlePaddle API映射列表。
| 序号 | PyTorch API | PaddlePaddle API | 备注 |
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
......
## PyTorch-PaddlePaddle 基础操作类API对应表
## 基础操作类API映射列表
该文档梳理了基础操作的PyTorch-PaddlePaddle API映射列表,主要包括了构造Tensor、数学计算、逻辑计算相关的API。
| 序号 | PyTorch API | PaddlePaddle API | 备注 |
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
......
## 工具类
## 工具类API映射列表
该文档梳理了与数据处理、分布式处理等相关的PyTorch-PaddlePaddle API映射列表。
| 序号 | PyTorch API | PaddlePaddle API | 备注 |
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| 1 | [torch.nn.DataParallel](https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html?highlight=dataparallel#torch.nn.DataParallel) | [paddle.DataParallel](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/fluid/dygraph/parallel/DataParallel_cn.html#dataparallel) | [差异对比](torch.nn.DataParallel.md) |
......
## 视觉类
## 视觉类API映射列表
该文档梳理了与视觉处理相关的PyTorch-PaddlePaddle API映射列表。
| 序号 | PyTorch API | PaddlePaddle API | 备注 |
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------- |
| 1 | [torchvision.transforms.Compose](https://pytorch.org/vision/stable/transforms.html?highlight=compose#torchvision.transforms.Compose) | [paddle.vision.transforms.Compose](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/vision/transforms/transforms/Compose_cn.html#compose) | 功能一致 |
......
......@@ -12,7 +12,7 @@
``` shell
x2paddle --convert_torch_project --project_dir=torch_project --save_dir=paddle_project --pretrain_model=model.pth
```
| 参数 | |
| 参数 | 作用|
|----------|--------------|
|--convert_torch_project | 当前方式为对PyTorch Project进行转换 |
|--project_dir | PyTorch的项目路径 |
......@@ -23,4 +23,4 @@ x2paddle --convert_torch_project --project_dir=torch_project --save_dir=paddle_p
### 第三步:转换后代码后处理
PaddlePaddle在使用上有部分限制(例如:自定义Dataset必须继承自`paddle.io.Dataset`、部分情况下DataLoader的num_worker只能为0等),用户需要手动修改代码,使代码运行,具体可参见[转换后代码后处理](./after_convert.md)
***[注意]*** 转换前后相应操作可以参考[转换示例](./demo.md)
***[注意]*** 转换前后相应操作可以参考[转换示例](./demo/README.md)
# PyTorch项目转换教程
| 模型 | 转换教程| 转换前代码 | 转换后代码 |
|------|-----|----------|------|
| StaGAN | [demo](stargan.md)| [code](https://github.com/yunjey/stargan)|[code](https://github.com/SunAhong1993/stargan/tree/paddle)|
| Ultra-Light-Fast-Generic-Face-Detector |[demo](ultra_light_fast_generic_face_detector.md)| [code](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB) |[code](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/tree/paddle)|
***持续更新...***
# PyTorch项目转换示例
## [StarGAN](https://github.com/yunjey/stargan)
### 准备工作
``` shell
......@@ -105,93 +104,3 @@ python main.py --mode train --dataset CelebA --image_size 128 --c_dim 5 --sample
```
***转换后的代码可在[这里](https://github.com/SunAhong1993/stargan/tree/paddle)进行查看。***
## [Ultra-Light-Fast-Generic-Face-Detector](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB)
### 准备工作
1. 下载项目
``` shell
# 下载项目
git clone https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB.git
git checkout 492a02471671b49c56be8d90cda54c94749d2980
```
2. 根据Generate VOC format training data set and training process的README.md所示下载数据集,并存放于Ultra-Light-Fast-Generic-Face-Detector-1MB/data/文件夹下。
### 第一步:转换前代码预处理
1. 将代码中的[或操作符](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/master/vision/utils/box_utils.py#L153)替换为如下代码:
``` python
...
def hard_negative_mining(loss, labels, neg_pos_ratio):
...
# return pos_mask | neg_mask
return torch.bitwise_or(pos_mask, neg_mask)
...
```
2. 使自定义的[`DataSet`](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/master/vision/datasets/voc_dataset.py#L10)继承`torch.utils.data.Dataset`,同时由于代码未导入torch,要添加相关导入的包,修改为如下代码:
``` python
...
# 导入torch
import torch
...
# class VOCDataset
class VOCDataset(torch.utils.data.Dataset):
...
...
```
3.[数据预处理](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/master/vision/utils/box_utils.py#L126)Tensor与int型对比,修改为Tensor与Tensor对比,修改如下:
``` python
...
def assign_priors(gt_boxes, gt_labels, corner_form_priors,
iou_threshold):
...
# labels[best_target_per_prior < iou_threshold] = 0 # the backgournd id
# 将原来的赋值修改为7-8行
iou_threshold_tensor = torch.full_like(best_target_per_prior, iou_threshold)
labels[best_target_per_prior < iou_threshold_tensor] = 0
boxes = gt_boxes[best_target_per_prior_index]
return boxes, labels
...
```
### 第二步:转换
```shell
x2paddle --convert_torch_project --project_dir=Ultra-Light-Fast-Generic-Face-Detector-1MB --save_dir=paddle_project
```
### 第三步:转换后代码后处理
**需要修改的文件位于paddle_project文件夹中,其中文件命名与原始Ultra-Light-Fast-Generic-Face-Detector-1MB文件夹中文件命名一致。**
1. DataLoader的`num_workers`设置为0,在转换后的[train-version-RFB.sh处](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/paddle/train-version-RFB.sh#L27)设置强制设置`num_workers`,具体添加代码如下:
```shell
...
--num_workers \
#4 \
0 \
...
```
2.修改自定义Dataset中的[\_\_getitem\_\_的返回值](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/paddle/vision/datasets/voc_dataset.py#L56),将Tensor修改为numpy,修改代码如下:
``` python
...
class VOCDataset(data.Dataset):
...
def __getitem__(self, index):
image_id = self.ids[index]
boxes, labels, is_difficult = self._get_annotation(image_id)
if not self.keep_difficult:
boxes = boxes[is_difficult == 0]
labels = labels[is_difficult == 0]
image = self._read_image(image_id)
if self.transform:
image, boxes, labels = self.transform(image, boxes, labels)
if self.target_transform:
boxes, labels = self.target_transform(boxes, labels)
# return image, boxes, labels
# 将原来的return替换为如下17行
return image.numpy(), boxes.numpy(), labels.numpy()
...
```
### 运行训练代码
``` shell
cd paddle_project/Ultra-Light-Fast-Generic-Face-Detector-1MB
sh train-version-RFB.sh
```
***转换后的代码可在[这里](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/tree/paddle)进行查看。***
## [Ultra-Light-Fast-Generic-Face-Detector](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB)
### 准备工作
1. 下载项目
``` shell
# 下载项目
git clone https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB.git
git checkout 492a02471671b49c56be8d90cda54c94749d2980
```
2. 根据Generate VOC format training data set and training process的README.md所示下载数据集,并存放于Ultra-Light-Fast-Generic-Face-Detector-1MB/data/文件夹下。
### 第一步:转换前代码预处理
1. 将代码中的[或操作符](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/master/vision/utils/box_utils.py#L153)替换为如下代码:
``` python
...
def hard_negative_mining(loss, labels, neg_pos_ratio):
...
# return pos_mask | neg_mask
return torch.bitwise_or(pos_mask, neg_mask)
...
```
2. 使自定义的[`DataSet`](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/master/vision/datasets/voc_dataset.py#L10)继承`torch.utils.data.Dataset`,同时由于代码未导入torch,要添加相关导入的包,修改为如下代码:
``` python
...
# 导入torch
import torch
...
# class VOCDataset
class VOCDataset(torch.utils.data.Dataset):
...
...
```
3.[数据预处理](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/master/vision/utils/box_utils.py#L126)Tensor与int型对比,修改为Tensor与Tensor对比,修改如下:
``` python
...
def assign_priors(gt_boxes, gt_labels, corner_form_priors,
iou_threshold):
...
# labels[best_target_per_prior < iou_threshold] = 0 # the backgournd id
# 将原来的赋值修改为7-8行
iou_threshold_tensor = torch.full_like(best_target_per_prior, iou_threshold)
labels[best_target_per_prior < iou_threshold_tensor] = 0
boxes = gt_boxes[best_target_per_prior_index]
return boxes, labels
...
```
### 第二步:转换
```shell
x2paddle --convert_torch_project --project_dir=Ultra-Light-Fast-Generic-Face-Detector-1MB --save_dir=paddle_project
```
### 第三步:转换后代码后处理
**需要修改的文件位于paddle_project文件夹中,其中文件命名与原始Ultra-Light-Fast-Generic-Face-Detector-1MB文件夹中文件命名一致。**
1. DataLoader的`num_workers`设置为0,在转换后的[train-version-RFB.sh处](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/paddle/train-version-RFB.sh#L27)设置强制设置`num_workers`,具体添加代码如下:
```shell
...
--num_workers \
#4 \
0 \
...
```
2.修改自定义Dataset中的[\_\_getitem\_\_的返回值](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/paddle/vision/datasets/voc_dataset.py#L56),将Tensor修改为numpy,修改代码如下:
``` python
...
class VOCDataset(data.Dataset):
...
def __getitem__(self, index):
image_id = self.ids[index]
boxes, labels, is_difficult = self._get_annotation(image_id)
if not self.keep_difficult:
boxes = boxes[is_difficult == 0]
labels = labels[is_difficult == 0]
image = self._read_image(image_id)
if self.transform:
image, boxes, labels = self.transform(image, boxes, labels)
if self.target_transform:
boxes, labels = self.target_transform(boxes, labels)
# return image, boxes, labels
# 将原来的return替换为如下17行
return image.numpy(), boxes.numpy(), labels.numpy()
...
```
### 运行训练代码
``` shell
cd paddle_project/Ultra-Light-Fast-Generic-Face-Detector-1MB
sh train-version-RFB.sh
```
***转换后的代码可在[这里](https://github.com/SunAhong1993/Ultra-Light-Fast-Generic-Face-Detector-1MB/tree/paddle)进行查看。***
# PyTorch训练项目转换支持API列表
> 目前PyTorch训练项目转换支持6个优化器相关API,40+的NN类API,5个Utils类API,2个Autograd类API,40+的基础操作API以及10+Torchvision API,我们在如下列表中给出了目前的全部API。
> 目前PyTorch训练项目转换支持6个优化器相关API,70+的NN类API,10+Utils类API,2个Autograd类API,40+的基础操作API以及30+Torchvision API,我们在如下列表中给出了目前的全部API。
## 优化器相关API
| 序号 | API | 序号 | API |
......@@ -35,7 +35,22 @@
| 41 | torch.nn.functional.softmax | 42 | torch.nn.init.xavier_uniform_ |
| 43 | torch.nn.functional.binary_cross_entropy_with_logits | 44 | torch.nn.functional.cross_entropy |
| 45 | torch.nn.functional.dropout | 46 | torch.nn.functional.relu |
| 47 | torch.nn.functional.smooth_l1_loss | | |
| 47 | torch.nn.functional.smooth_l1_loss | 48 | torch.nn.AdaptiveAvgPool1d |
| 49 | torch.nn.AdaptiveAvgPool2d | 50 | torch.nn.AdaptiveAvgPool3d |
| 51 | torch.nn.AvgPool1d | 52 | torch.nn.AvgPool2d |
| 53 | torch.nn.AvgPool3d | 54 | torch.nn.ConstantPad2d |
| 55 | torch.nn.Dropout2d | 56 | torch.nn.GELU |
| 57 | torch.nn.GroupNorm | 58 | torch.nn.Identity |
| 59 | torch.nn.LayerNorm | 60 | torch.nn.MaxUnpool2d |
| 61 | torch.nn.ReflectionPad2d | 62 | torch.nn.ReplicationPad2d |
| 63 | torch.nn.PReLU | 64 | torch.nn.SyncBatchNorm |
| 65 | torch.nn.ZeroPad2d | 66 | torch.nn.KLDivLoss |
| 67 | torch.nn.L1Loss | 68 | paddle.nn.functional.interpolate |
| 69 | torch.nn.functional.mse_loss | 70 | torch.nn.init.constant_ |
| 71 | torch.nn.init.normal_ | 72 | torch.nn.init.ones_ |
| 73 | torch.nn.init.zeros_ | 74 | torch.nn.init.orthogonal_ |
## Utils类API
......@@ -43,7 +58,11 @@
| ---- | ------------------------------ | ---- | --------------------------- |
| 1 | torch.utils.data | 2 | torch.utils.data.DataLoader |
| 3 | torch.utils.data.random_split | 4 | torch.utils.data.Dataset |
| 5 | torch.utils.data.ConcatDataset | | |
| 5 | torch.utils.data.ConcatDataset | 6 | torch.utils.data.distributed |
| 7 | torch.utils.data.distributed.DistributedSampler | 8 | torch.utils.model_zoo |
| 9 | torch.utils.model_zoo.load_url | 10 | torch.multiprocessing |
| 11 | torch.multiprocessing.spawn | 12 | torch.distributed |
| 13 | torch.distributed.init_process_group | 14 | |
## Autograd类API
......@@ -76,6 +95,9 @@
| 37 | torch.rand | 38 | torch.abs |
| 39 | torch.bitwise_or | 40 | torch.bitwise_xor |
| 41 | torch.bitwise_and | 42 | torch.bitwise_not |
| 43 | torch.randn | 44 | torch.add |
| 45 | torch.mul | 46 | torch.linspace |
| 47 | torch.einsum| | |
## Torchvision API
......@@ -86,5 +108,18 @@
| 5 | torchvision.transforms.ToTensor | 6 | torchvision.transforms.RandomHorizontalFlip |
| 7 | torchvision.transforms.CenterCrop | 8 | torchvision.transforms.Normalize |
| 9 | torchvision.utils.save_image | 10 | torchvision.datasets.ImageFolder |
| 11 | torchvision.transforms.RandomResizedCrop | 12 | torchvision.transforms.Lambda |
| 13 | torchvision.utils | 14 | torchvision.utils.save_image |
| 15 | torchvision.datasets | 16 | torchvision.datasets.ImageFolder |
| 17 | torchvision.models | 18 | torchvision.models.vgg_pth_urls |
| 19 | torchvision.models.vgg11 | 20 | torchvision.models.vgg13 |
| 21 | torchvision.models.vgg16 | 22 | torchvision.models.vgg19 |
| 23 | torchvision.models.vgg11_bn | 24 | torchvision.models.vgg13_bn |
| 25 | torchvision.models.vgg16_bn | 26 | torchvision.models.vgg19_bn |
| 27 | torchvision.models.resnet34 | 28 | torchvision.models.resnet50 |
| 29 | torchvision.models.resnet101 | 30 | torchvision.models.resnet152 |
| 31 | torchvision.models.resnext50_32x4d | 32 | torchvision.models.resnext101_32x8d |
| 33 | torchvision.models.wide_resnet50_2 | 34 | torchvision.models.wide_resnet101_2 |
***持续更新...***
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册