Merge pull request !8089 from guozhijian/enhance_py_resizetags/v1.1.0
| @@ -35,6 +35,7 @@ from .validators import check_prob, check_crop, check_resize_interpolation, chec | |||
| from .utils import Inter, Border | |||
| DE_PY_INTER_MODE = {Inter.NEAREST: Image.NEAREST, | |||
| Inter.ANTIALIAS: Image.ANTIALIAS, | |||
| Inter.LINEAR: Image.LINEAR, | |||
| Inter.CUBIC: Image.CUBIC} | |||
| @@ -374,12 +375,14 @@ class Resize: | |||
| the same image aspect ratio. | |||
| If size is a sequence of length 2, it should be (height, width). | |||
| interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BILINEAR). | |||
| It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC]. | |||
| - Inter.BILINEAR, means the interpolation method is bilinear interpolation. | |||
| It can be any of [Inter.NEAREST, Inter.ANTIALIAS, Inter.BILINEAR, Inter.BICUBIC]. | |||
| - Inter.NEAREST, means the interpolation method is nearest-neighbor interpolation. | |||
| - Inter.ANTIALIAS, means the interpolation method is antialias interpolation. | |||
| - Inter.BILINEAR, means the interpolation method is bilinear interpolation. | |||
| - Inter.BICUBIC, means the interpolation method is bicubic interpolation. | |||
| Examples: | |||
| @@ -421,12 +424,14 @@ class RandomResizedCrop: | |||
| to be cropped (default=(0.08, 1.0)). | |||
| ratio (tuple, optional): Range (min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)). | |||
| interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BILINEAR). | |||
| It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC]. | |||
| - Inter.BILINEAR, means the interpolation method is bilinear interpolation. | |||
| It can be any of [Inter.NEAREST, Inter.ANTIALIAS, Inter.BILINEAR, Inter.BICUBIC]. | |||
| - Inter.NEAREST, means the interpolation method is nearest-neighbor interpolation. | |||
| - Inter.ANTIALIAS, means the interpolation method is antialias interpolation. | |||
| - Inter.BILINEAR, means the interpolation method is bilinear interpolation. | |||
| - Inter.BICUBIC, means the interpolation method is bicubic interpolation. | |||
| max_attempts (int, optional): The maximum number of attempts to propose a valid | |||
| @@ -559,12 +564,14 @@ class RandomRotation: | |||
| If degrees is a sequence, it should be (min, max). | |||
| resample (Inter mode, optional): An optional resampling filter (default=Inter.NEAREST). | |||
| If omitted, or if the image has mode "1" or "P", it is set to be Inter.NEAREST. | |||
| It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC]. | |||
| - Inter.BILINEAR, means the resampling method is bilinear interpolation. | |||
| It can be any of [Inter.NEAREST, Inter.ANTIALIAS, Inter.BILINEAR, Inter.BICUBIC]. | |||
| - Inter.NEAREST, means the resampling method is nearest-neighbor interpolation. | |||
| - Inter.ANTIALIAS, means the resampling method is antialias interpolation. | |||
| - Inter.BILINEAR, means the resampling method is bilinear interpolation. | |||
| - Inter.BICUBIC, means the resampling method is bicubic interpolation. | |||
| expand (bool, optional): Optional expansion flag (default=False). If set to True, expand the output | |||
| @@ -19,8 +19,9 @@ from enum import Enum, IntEnum | |||
| class Inter(IntEnum): | |||
| NEAREST = 0 | |||
| BILINEAR = LINEAR = 1 | |||
| BICUBIC = CUBIC = 2 | |||
| ANTIALIAS = 1 | |||
| BILINEAR = LINEAR = 2 | |||
| BICUBIC = CUBIC = 3 | |||
| # Padding Mode, Border Type | |||
| @@ -23,6 +23,7 @@ import mindspore.dataset.vision.c_transforms as c_vision | |||
| import mindspore.dataset.vision.py_transforms as py_vision | |||
| import mindspore.dataset.vision.utils as mode | |||
| import mindspore.dataset as ds | |||
| from mindspore.dataset.vision.utils import Inter | |||
| from mindspore import log as logger | |||
| from util import diff_mse, save_and_check_md5, visualize_list, \ | |||
| config_get_set_seed, config_get_set_num_parallel_workers | |||
| @@ -111,6 +112,25 @@ def test_random_crop_and_resize_op_py(plot=False): | |||
| if plot: | |||
| visualize_list(original_images, crop_and_resize_images) | |||
| def test_random_crop_and_resize_op_py_ANTIALIAS(): | |||
| """ | |||
| Test RandomCropAndResize op in py transforms | |||
| """ | |||
| logger.info("test_random_crop_and_resize_op_py_ANTIALIAS") | |||
| # First dataset | |||
| data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) | |||
| # With these inputs we expect the code to crop the whole image | |||
| transforms1 = [ | |||
| py_vision.Decode(), | |||
| py_vision.RandomResizedCrop((256, 512), (2, 2), (1, 3), Inter.ANTIALIAS), | |||
| py_vision.ToTensor() | |||
| ] | |||
| transform1 = mindspore.dataset.transforms.py_transforms.Compose(transforms1) | |||
| data1 = data1.map(operations=transform1, input_columns=["image"]) | |||
| num_iter = 0 | |||
| for _ in data1.create_dict_iterator(num_epochs=1, output_numpy=True): | |||
| num_iter += 1 | |||
| logger.info("use RandomResizedCrop by Inter.ANTIALIAS process {} images.".format(num_iter)) | |||
| def test_random_crop_and_resize_01(): | |||
| """ | |||
| @@ -371,6 +391,7 @@ def test_random_crop_and_resize_06(): | |||
| if __name__ == "__main__": | |||
| test_random_crop_and_resize_op_c(True) | |||
| test_random_crop_and_resize_op_py(True) | |||
| test_random_crop_and_resize_op_py_ANTIALIAS() | |||
| test_random_crop_and_resize_01() | |||
| test_random_crop_and_resize_02() | |||
| test_random_crop_and_resize_03() | |||
| @@ -104,6 +104,26 @@ def test_random_rotation_op_py(plot=False): | |||
| if plot: | |||
| visualize_image(original, rotation_de, mse, rotation_cv) | |||
| def test_random_rotation_op_py_ANTIALIAS(): | |||
| """ | |||
| Test RandomRotation in python transformations op | |||
| """ | |||
| logger.info("test_random_rotation_op_py_ANTIALIAS") | |||
| # First dataset | |||
| data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False) | |||
| # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size | |||
| transform1 = mindspore.dataset.transforms.py_transforms.Compose([py_vision.Decode(), | |||
| py_vision.RandomRotation((90, 90), | |||
| expand=True, | |||
| resample=Inter.ANTIALIAS), | |||
| py_vision.ToTensor()]) | |||
| data1 = data1.map(operations=transform1, input_columns=["image"]) | |||
| num_iter = 0 | |||
| for _ in data1.create_dict_iterator(num_epochs=1, output_numpy=True): | |||
| num_iter += 1 | |||
| logger.info("use RandomRotation by Inter.ANTIALIAS process {} images.".format(num_iter)) | |||
| def test_random_rotation_expand(): | |||
| """ | |||
| @@ -219,6 +239,7 @@ def test_rotation_diff(plot=False): | |||
| if __name__ == "__main__": | |||
| test_random_rotation_op_c(plot=True) | |||
| test_random_rotation_op_py(plot=True) | |||
| test_random_rotation_op_py_ANTIALIAS() | |||
| test_random_rotation_expand() | |||
| test_random_rotation_md5() | |||
| test_rotation_diff(plot=True) | |||
| @@ -18,6 +18,7 @@ Testing Resize op in DE | |||
| import pytest | |||
| import mindspore.dataset as ds | |||
| import mindspore.dataset.vision.c_transforms as vision | |||
| import mindspore.dataset.vision.py_transforms as py_vision | |||
| from mindspore.dataset.vision.utils import Inter | |||
| from mindspore import log as logger | |||
| from util import visualize_list, save_and_check_md5, \ | |||
| @@ -59,6 +60,24 @@ def test_resize_op(plot=False): | |||
| test_resize_op_parameters("Test single int for size", 10, plot=False) | |||
| test_resize_op_parameters("Test tuple for size", (10, 15), plot=False) | |||
| def test_resize_op_ANTIALIAS(): | |||
| """ | |||
| Test resize_op | |||
| """ | |||
| logger.info("Test resize for ANTIALIAS") | |||
| data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) | |||
| # define map operations | |||
| decode_op = py_vision.Decode() | |||
| resize_op = py_vision.Resize(20, Inter.ANTIALIAS) | |||
| # apply map operations on images | |||
| data1 = data1.map(operations=[decode_op, resize_op, py_vision.ToTensor()], input_columns=["image"]) | |||
| num_iter = 0 | |||
| for _ in data1.create_dict_iterator(num_epochs=1, output_numpy=True): | |||
| num_iter += 1 | |||
| logger.info("use Resize by Inter.ANTIALIAS process {} images.".format(num_iter)) | |||
| def test_resize_md5(plot=False): | |||
| def test_resize_md5_parameters(test_name, size, filename, seed, plot): | |||
| @@ -115,5 +134,6 @@ def test_resize_op_invalid_input(): | |||
| if __name__ == "__main__": | |||
| test_resize_op(plot=True) | |||
| test_resize_op_ANTIALIAS() | |||
| test_resize_md5(plot=True) | |||
| test_resize_op_invalid_input() | |||