Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增 torchvision 和 paddle.vision 的对应关系 #6898

Merged
merged 10 commits into from
Oct 17, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
torch.diagonal(input,
offset=0,
dim1=0,
dim2=1))
dim2=1)
```

### [paddle.diagonal](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/diagonal_cn.html#diagonal)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## [输入参数用法不一致]torchvision.datasets.CIFAR10

### [torchvision.datasets.CIFAR10](https://pytorch.org/vision/main/generated/torchvision.datasets.CIFAR10.html)

```python
torchvision.datasets.CIFAR10(root: Union[str, Path], train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)
```

### [paddle.vision.datasets.Cifar10](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/vision/datasets/Cifar10_cn.html)

```python
paddle.vision.datasets.Cifar10(data_file: Optional[str] = None, mode: str = 'train', transform: Optional[Callable] = None, download: bool = True, backend: Optional[str] = None)
```

两者功能一致,指定数据集文件路径的参数 `root` 与指定训练集的参数 `train` 的用法不一致,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| ---------------------- | --------------------- | ---------------------------------------------------------- |
| root | data_file | 数据集文件路径,Paddle 参数 data_file 需含完整的文件名,如 PyTorch 参数 `/path/to/data`,对应 Paddle 参数 `/path/to/data/cifar-10-python.tar.gz`,需要转写。 |
| train | mode | 训练集或者数据集。PyTorch 参数 train=True 对应 Paddle 参数 mode='train',PyTorch 参数 train=False 对应 Paddle 参数 mode='test',需要转写。 |
| transform | transform | 图片数据的预处理。 |
| target_transform | - | 接受目标数据并转换,Paddle 无此参数,暂无转写方式。 |
| download | download | 是否自动下载数据集文件,参数默认值不一致。PyTorch 默认为 False,Paddle 默认为 True,Paddle 需设置为与 PyTorch 一致。 |
| - | backend | 指定图像类型,PyTorch 无此参数,Paddle 保持默认即可。 |

### 转写示例
#### root:数据集文件路径
```python
# PyTorch 写法
train_dataset = torchvision.datasets.CIFAR10(root='/path/to/data', train=True)

# Paddle 写法
train_dataset = paddle.vision.datasets.Cifar10(data_file=str(pathlib.Path('/path/to/data') / 'cifar-10-python.tar.gz'), mode='train')
guozixu2001 marked this conversation as resolved.
Show resolved Hide resolved
guozixu2001 marked this conversation as resolved.
Show resolved Hide resolved
```

#### train: 训练集或数据集
训练集
```python
# PyTorch 写法
train_dataset = torchvision.datasets.CIFAR10(train=True, download=True)

# Paddle 写法
train_dataset = paddle.vision.datasets.Cifar10(mode='train', download=True)
```

guozixu2001 marked this conversation as resolved.
Show resolved Hide resolved
测试集
```python
# PyTorch 写法
train_dataset = torchvision.datasets.CIFAR10(train=False, download=True)

# Paddle 写法
guozixu2001 marked this conversation as resolved.
Show resolved Hide resolved
train_dataset = paddle.vision.datasets.Cifar10(mode='test', download=True)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## [torch 参数更多]torchvision.datasets.ImageFolder

### [torchvision.datasets.ImageFolder](https://pytorch.org/vision/main/generated/torchvision.datasets.ImageFolder.html)

```python
torchvision.datasets.ImageFolder(root: Union[str, pathlib.Path],
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
loader: Callable[[str], Any] = default_loader,
is_valid_file: Optional[Callable[[str], bool]] = None,
allow_empty: bool = False)
```

### [paddle.vision.datasets.ImageFolder](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/vision/datasets/ImageFolder_cn.html)

```python
paddle.vision.datasets.ImageFolder(root: str,
loader: Optional[Callable] = None,
extensions: Optional[Union[list[str], tuple[str]]] = None,
transform: Optional[Callable] = None,
is_valid_file: Optional[Callable[[str], bool]] = None)
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| -------------------------------- | ---------------------------------- | ----------------------------------------- |
| root | root | 根目录路径。 |
| transform | transform | 图片数据的预处理。 |
| target_transform | - | 目标标签的预处理,Paddle 无此参数,暂无转写方式。 |
| loader | loader | 图片加载函数。 |
| is_valid_file | is_valid_file | 根据数据路径判断是否合法。 |
| allow_empty | - | 是否允许空文件夹,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| - | extensions | 限制数据集文件的格式,PyTorch 无此参数,Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## [输入参数用法不一致]torchvision.datasets.MNIST

### [torchvision.datasets.MNIST](https://pytorch.org/vision/main/generated/torchvision.datasets.MNIST.html)

```python
torchvision.datasets.MNIST(root: Union[str, Path], train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)
```

### [paddle.vision.datasets.MNIST](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/vision/datasets/MNIST_cn.html)

```python
paddle.vision.datasets.MNIST(image_path: str = None, label_path: str = None, mode: str = 'train', transform: Callable = None, download: bool = True, backend: str = None)
```

两者功能一致,指定数据集文件路径的参数 `root` 与指定训练集的参数 `train` 的用法不一致,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| -------------------------------- | ---------------------------------- | ---- |
| root | image_path, label_path| 指定数据集目录,Paddle 使用 image_path 和 label_path,等价的实现 PyTorch 的 root 的功能,需要转写。|
| train | mode | 训练集或者数据集。PyTorch 参数 train=True 对应 Paddle 参数 mode='train',PyTorch 参数 train=False 对应 Paddle 参数 mode='test',需要转写。 |
| transform | transform | 图片数据的预处理。|
| target_transform | - | 接受目标数据并转换,Paddle 无此参数,暂无转写方式。 |
| download | download | 是否自动下载数据集文件,参数默认值不一致。PyTorch 默认为 False,Paddle 默认为 True,Paddle 需设置为与 PyTorch 一致。 |
| - | backend | 指定图像类型,PyTorch 无此参数,Paddle 保持默认即可。 |

### 转写示例
#### root:数据集文件路径
```python
# PyTorch 写法
train_dataset = torchvision.datasets.MNIST(root='./data')

# Paddle 写法
train_dataset = paddle.vision.datasets.MNIST(
image_path=str(pathlib.Path('./data') / 'MNIST/raw/train-images-idx3-ubyte.gz'),
label_path=str(pathlib.Path('./data') / 'MNIST/raw/train-labels-idx1-ubyte.gz'))
```

#### train: 训练集或数据集
训练集
```python
# PyTorch 写法
train_dataset = torchvision.datasets.MNIST(train=True, download=True)

# Paddle 写法
train_dataset = paddle.vision.datasets.MNIST(mode='train', download=True)
```

测试集
```python
# PyTorch 写法
train_dataset = torchvision.datasets.MNIST(train=False, download=True)

# Paddle 写法
train_dataset = paddle.vision.datasets.MNIST(mode='test', download=True)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [paddle 参数更多]torchvision.transforms.RandomVerticalFlip

### [torchvision.transforms.RandomVerticalFlip](https://pytorch.org/vision/main/generated/torchvision.transforms.RandomVerticalFlip.html)

```python
torchvision.transforms.RandomVerticalFlip(p: float = 0.5)
```

### [paddle.vision.transforms.RandomVerticalFlip](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/vision/transforms/RandomVerticalFlip_cn.html)

```python
paddle.vision.transforms.RandomVerticalFlip(
prob: float = 0.5,
keys: Optional[Union[List[str], Tuple[str, ...]]] = None
)
```

Paddle 比 PyTorch 支持更多参数,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| ------------| ------------ | ------------------------------------------------------------ |
| p | prob | 翻转概率,仅参数名不一致。 |
| - | keys | 输入的类型,PyTorch 无此参数,Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## [torch 参数更多]torchvision.transforms.functional.normalize

### [torchvision.transforms.functional.normalize](https://pytorch.org/vision/stable/generated/torchvision.transforms.functional.normalize.html)

```python
torchvision.transforms.functional.normalize(img, mean, std, inplace = False)
```

### [paddle.vision.transforms.normalize](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/vision/transforms/normalize_cn.html)

```python
paddle.vision.transforms.normalize(img, mean = 0.0, std = 1.0, data_format = 'CHW', to_rgb = False)
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| --------- | ---------- | ---------------------------------------------------- |
| img | img | 用于归一化的数据。 |
| mean | mean | 用于每个通道归一化的均值。 |
| std | std | 用于每个通道归一化的标准差值。 |
| inplace | - | 是否原地修改,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| - | data_format| 用于指定数据格式,PyTorch 无此参数,Paddle 保持默认即可。 |
| - | to_rgb | 是否将图像转换为 RGB 格式,PyTorch 无此参数,Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [参数完全一致]torchvision.transforms.functional.pad

### [torchvision.transforms.functional.pad](https://pytorch.org/vision/main/generated/torchvision.transforms.functional.pad.html)

```python
torchvision.transforms.functional.pad(
img: Union[PIL.Image.Image, torch.Tensor],
padding: Union[int, List[int], Tuple[int, ...]],
fill: Union[int, List[int], Tuple[int, ...]] = 0,
padding_mode: Literal['constant', 'edge', 'reflect', 'symmetric'] = 'constant'
)
```

### [paddle.vision.transforms.pad](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/vision/transforms/pad_cn.html)

```python
paddle.vision.transforms.pad(
img: Union[PIL.Image.Image, np.ndarray, paddle.Tensor],
padding: Union[int, List[int], Tuple[int, ...]],
fill: Union[int, List[int], Tuple[int, ...]] = 0,
padding_mode: Literal['constant', 'edge', 'reflect', 'symmetric'] = 'constant'
)
```

功能一致,参数完全一致,具体如下:

### 参数映射

| torchvision | PaddlePaddle| 备注 |
| ---------------- | ---------------------------------------------------- | ------------------------------------------------------------ |
| img | img | 被填充的图像。|
| padding | padding | 在图像边界上进行填充的范围。 |
| fill | fill | 用于填充的像素值。 |
| padding_mode | padding_mode | 填充模式。|
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## [输入参数类型不一致]torchvision.transforms.functional.resize

### [torchvision.transforms.functional.resize](https://pytorch.org/vision/main/generated/torchvision.transforms.functional.resize.html)

```python
torchvision.transforms.functional.resize(img: Tensor, size: Optional[Union[int, Sequence[int]]],
interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
max_size: Optional[int] = None,
antialias: Optional[bool] = True)
```

### [paddle.vision.transforms.resize](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/vision/transforms/resize_cn.html)

```python
paddle.vision.transforms.resize(
img: Union[np.ndarray, paddle.Tensor, PIL.Image.Image],
size: Union[int, List[int], Tuple[int, ...]],
interpolation: Union[str, int] = 'bilinear',
)
```

两者功能一致,但输入参数类型不一致。

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| ------------ | ------------ | ---------------- |
| img | img | 输入数据。 |
| size | size | 输出图像大小。 |
| interpolation | interpolation | 插值的方法,两者类型不一致,PyTorch 为 InterpolationMode 枚举类, Paddle 为 int 或 string,需要转写。 |
| max_size | - | 允许的最长边的最大值,Paddle 无此参数,暂无转写方式。 |
| antialias | - | 是否应用抗锯齿处理,Paddle 无此参数,暂无转写方式。 |


### 转写示例
#### interpolation:插值的方法

```python
# PyTorch 写法
resized_img = torchvision.transforms.functional.resize(img, size=(224, 224), interpolation=torchvision.transforms.InterpolationMode.BILINEAR)

# Paddle 写法
resized_img = paddle.vision.transforms.resize(img=img, size=(224, 224), interpolation='bilinear')
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## [paddle 参数更多]torchvision.transforms.CenterCrop

### [torchvision.transforms.CenterCrop](https://pytorch.org/vision/main/generated/torchvision.transforms.CenterCrop.html)

```python
torchvision.transforms.CenterCrop(
size: Union[int, List[int], Tuple[int, ...]]
)
```

### [paddle.vision.transforms.CenterCrop](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/vision/transforms/CenterCrop_cn.html)

```python
paddle.vision.transforms.CenterCrop(
size: Union[int, List[int], Tuple[int, ...]],
keys: Optional[Union[List[str], Tuple[str, ...]]] = None
)
```

两者功能一致,但 Paddle 相比 torchvision 支持更多参数,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| ------------ | ------------ | ---------------------- |
| size | size | 输出图像的形状大小。 |
| - | keys | 输入的类型,PyTorch 无此参数,Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [参数完全一致]torchvision.transforms.Compose

### [torchvision.transforms.Compose](https://pytorch.org/vision/main/generated/torchvision.transforms.Compose.html)

```python
torchvision.transforms.Compose(
transforms: List[Transform]
)
```

### [paddle.vision.transforms.Compose](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/vision/transforms/Compose_cn.html)

```python
paddle.vision.transforms.Compose(
transforms: Union[List[Transform], Tuple[Transform, ...]]
)
```

功能一致,参数完全一致,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| ------------------- | ------------------------ | ---------------------------------------- |
| transforms | transforms | 用于组合的数据预处理接口实例列表。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [paddle 参数更多]torchvision.transforms.Grayscale

### [torchvision.transforms.Grayscale](https://pytorch.org/vision/main/generated/torchvision.transforms.Grayscale.html)

```python
torchvision.transforms.Grayscale(num_output_channels: int = 1)
```

### [paddle.vision.transforms.Grayscale](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/vision/transforms/Grayscale_cn.html)

```python
paddle.vision.transforms.Grayscale(
num_output_channels: int = 1,
keys: Optional[Union[List[str], Tuple[str, ...]]] = None
)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| torchvision | PaddlePaddle | 备注 |
| --------------------------- | ------------------------------ | --------------------- |
| num_output_channels | num_output_channels | 输出图像的通道数。 |
| - | keys | 输入的类型,PyTorch 无此参数,Paddle 保持默认即可。 |
Loading