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 转换规则 #491

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
463 changes: 463 additions & 0 deletions paconvert/api_mapping.json

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4792,3 +4792,129 @@ def generate_code(self, kwargs):
self.kwargs_to_str(kwargs_bin_edges),
)
return code


class DatasetFolderMatcher(BaseMatcher):
def generate_code(self, kwargs):
if "root" in kwargs:
kwargs["root"] = "str(Path({}))".format(kwargs["root"])
API_TEMPLATE = textwrap.dedent(
"""
from pathlib import Path
{}({})
"""
)
return API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs))


class ImageFolderMatcher(BaseMatcher):
def generate_code(self, kwargs):
if "root" in kwargs:
kwargs["root"] = "str(Path({}))".format(kwargs["root"])
API_TEMPLATE = textwrap.dedent(
"""
from pathlib import Path
{}({})
"""
)
return API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs))


class Cifar10Matcher(BaseMatcher):
def generate_code(self, kwargs):
if "root" in kwargs:
root = kwargs.pop("root")
data_file = "cifar-10-python.tar.gz"
kwargs["data_file"] = "str(Path({}) / '{}')".format(root, data_file)
if "train" in kwargs:
kwargs["mode"] = "'train' if {} else 'test'".format(kwargs.pop("train"))

API_TEMPLATE = textwrap.dedent(
"""
from pathlib import Path
{}({})
"""
)
return API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs))


class Cifar100Matcher(BaseMatcher):
def generate_code(self, kwargs):
if "root" in kwargs:
root = kwargs.pop("root")
data_file = "cifar-100-python.tar.gz"
kwargs["data_file"] = "str(Path({}) / '{}')".format(root, data_file)
if "train" in kwargs:
kwargs["mode"] = "'train' if {} else 'test'".format(kwargs.pop("train"))

API_TEMPLATE = textwrap.dedent(
"""
from pathlib import Path
{}({})
"""
)
return API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs))


class MNISTMatcher(BaseMatcher):
def generate_code(self, kwargs):
train = True
if "train" in kwargs:
train = kwargs.pop("train")
kwargs["mode"] = "'train' if {} else 'test'".format(train)
if "root" in kwargs:
root = kwargs.pop("root")
file_paths = {
"train_image": "MNIST/raw/train-images-idx3-ubyte.gz",
"train_label": "MNIST/raw/train-labels-idx1-ubyte.gz",
"test_image": "MNIST/raw/t10k-images-idx3-ubyte.gz",
"test_label": "MNIST/raw/t10k-labels-idx1-ubyte.gz",
}
kwargs["image_path"] = (
f"str(Path({root}) / '{file_paths['train_image']}') if {train} else "
f"str(Path({root}) / '{file_paths['test_image']}')"
)
kwargs["label_path"] = (
f"str(Path({root}) / '{file_paths['train_label']}') if {train} else "
f"str(Path({root}) / '{file_paths['test_label']}')"
)

API_TEMPLATE = textwrap.dedent(
"""
from pathlib import Path
{}({})
"""
)
return API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs))


class FashionMNISTMatcher(BaseMatcher):
def generate_code(self, kwargs):
train = True
if "train" in kwargs:
train = kwargs.pop("train")
kwargs["mode"] = "'train' if {} else 'test'".format(train)
if "root" in kwargs:
root = kwargs.pop("root")
file_paths = {
"train_image": "FashionMNIST/raw/train-images-idx3-ubyte.gz",
"train_label": "FashionMNIST/raw/train-labels-idx1-ubyte.gz",
"test_image": "FashionMNIST/raw/t10k-images-idx3-ubyte.gz",
"test_label": "FashionMNIST/raw/t10k-labels-idx1-ubyte.gz",
}
kwargs["image_path"] = (
f"str(Path({root}) / '{file_paths['train_image']}') if {train} else "
f"str(Path({root}) / '{file_paths['test_image']}')"
)
kwargs["label_path"] = (
f"str(Path({root}) / '{file_paths['train_label']}') if {train} else "
f"str(Path({root}) / '{file_paths['test_label']}')"
)

API_TEMPLATE = textwrap.dedent(
"""
from pathlib import Path
{}({})
"""
)
return API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs))
14 changes: 14 additions & 0 deletions tests/vision/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
62 changes: 62 additions & 0 deletions tests/vision/image_apibase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
from apibase import APIBase
from PIL import Image


class ImageAPIBase(APIBase):
def compare(
self,
name,
pytorch_result,
paddle_result,
check_value=True,
check_dtype=True,
check_stop_gradient=True,
rtol=1.0e-6,
atol=0.0,
):
"""
Compare PIL Images for equality.
"""
if isinstance(pytorch_result, Image.Image) and isinstance(
paddle_result, Image.Image
):
pytorch_array = np.array(pytorch_result)
paddle_array = np.array(paddle_result)

assert (
pytorch_array.shape == paddle_array.shape
), "API ({}): shape mismatch, torch shape is {}, paddle shape is {}".format(
name, pytorch_array.shape, paddle_array.shape
)

if check_value:
assert np.array_equal(
pytorch_array, paddle_array
), "API ({}): image data mismatch".format(name)
return

super().compare(
name,
pytorch_result,
paddle_result,
check_value,
check_dtype,
check_stop_gradient,
rtol,
atol,
)
93 changes: 93 additions & 0 deletions tests/vision/test_CenterCrop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import textwrap

from apibase import APIBase
from vision.image_apibase import ImageAPIBase

obj = APIBase("torchvision.transforms.CenterCrop")
img_obj = ImageAPIBase("torchvision.transforms.CenterCrop")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
import torchvision.transforms as transforms
img = torch.tensor([[[0.5, 0.5], [0.5, 0.5]],
[[0.5, 0.5], [0.5, 0.5]],
[[0.5, 0.5], [0.5, 0.5]]])
center_crop = transforms.CenterCrop((1, 1))
result = center_crop(img)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
import torchvision.transforms as transforms
img = torch.tensor([[[0.1, 0.4], [0.7, 1.0]],
[[0.2, 0.5], [0.8, 1.0]],
[[0.3, 0.6], [0.9, 1.0]]])
center_crop = transforms.CenterCrop([1, 1])
result = center_crop(img)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
from PIL import Image
import torchvision.transforms as transforms
img = Image.new('RGB', (4, 4), color=(100, 100, 100))
center_crop = transforms.CenterCrop(2)
result = center_crop(img)
"""
)
img_obj.run(pytorch_code, ["result"])


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
import torchvision.transforms as transforms
img = torch.tensor([[[0.1, 0.2, 0.3, 0.4],
[0.5, 0.6, 0.7, 0.8]],
[[0.9, 1.0, 1.1, 1.2],
[1.3, 1.4, 1.5, 1.6]]])
center_crop = transforms.CenterCrop((2, 2))
result = center_crop(img)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_5():
pytorch_code = textwrap.dedent(
"""
from PIL import Image
import torchvision.transforms as transforms
img = Image.new('RGB', (3, 3), color=(50, 100, 150))
center_crop = transforms.CenterCrop((2, 2))
result = center_crop(img)
"""
)
img_obj.run(pytorch_code, ["result"])
Loading