diff --git a/tests/ut/data/dataset/golden/random_crop_01_c_result.npz b/tests/ut/data/dataset/golden/random_crop_01_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..32e10a8d72f91fbc5b42eaf0097b4e15a17c28f3 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_01_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_01_py_result.npz b/tests/ut/data/dataset/golden/random_crop_01_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..15041f8e52f81daccb8ee1d9228697a912085563 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_01_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_02_c_result.npz b/tests/ut/data/dataset/golden/random_crop_02_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..054676d997d309e1b89d9523479aa471a6a8e27a Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_02_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_02_py_result.npz b/tests/ut/data/dataset/golden/random_crop_02_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..14cf210d3ae2866a4fbab4d89d1afcb91b3b8221 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_02_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_03_c_result.npz b/tests/ut/data/dataset/golden/random_crop_03_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..b3a52243a4d660ff8dfde41256c43e0c479a7e56 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_03_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_03_py_result.npz b/tests/ut/data/dataset/golden/random_crop_03_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..3333dd17841153a4453cb2a8e75dce6e8f10ce56 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_03_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_05_c_result.npz b/tests/ut/data/dataset/golden/random_crop_05_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..a5c914f5b7753c7bff98b59a9ddc423f49ac429a Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_05_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_05_py_result.npz b/tests/ut/data/dataset/golden/random_crop_05_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..35d187ac8a4c59aaef68ee0df1e349b1097f52e0 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_05_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_07_c_result.npz b/tests/ut/data/dataset/golden/random_crop_07_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..d4d08cb224c4b93b51cae2998b283f5bb443734b Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_07_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_07_py_result.npz b/tests/ut/data/dataset/golden/random_crop_07_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..68339c0aa46c2b362e1bd180bb1a17e6ca088ffa Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_07_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_08_c_result.npz b/tests/ut/data/dataset/golden/random_crop_08_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..d4d08cb224c4b93b51cae2998b283f5bb443734b Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_08_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_08_py_result.npz b/tests/ut/data/dataset/golden/random_crop_08_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..cc26b0322f806944446ead425aee540c2110844d Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_08_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_and_resize_01_c_result.npz b/tests/ut/data/dataset/golden/random_crop_and_resize_01_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..9deccf5eec53e39e47a1f6ccee341efb844d25bc Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_and_resize_01_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_and_resize_01_py_result.npz b/tests/ut/data/dataset/golden/random_crop_and_resize_01_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..67459b2e824bad588b2b0b7c322b95d742c0bba8 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_and_resize_01_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_and_resize_02_c_result.npz b/tests/ut/data/dataset/golden/random_crop_and_resize_02_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..6f1b0517698d5379f4a92a3d6f101462fa915f71 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_and_resize_02_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_and_resize_02_py_result.npz b/tests/ut/data/dataset/golden/random_crop_and_resize_02_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..dd30307bc5f4d7f2cededcf0d5ccf0402af10223 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_and_resize_02_py_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_and_resize_03_c_result.npz b/tests/ut/data/dataset/golden/random_crop_and_resize_03_c_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..90903ec22fb3ad3f2d72ec1819af12a342a57bbf Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_and_resize_03_c_result.npz differ diff --git a/tests/ut/data/dataset/golden/random_crop_and_resize_03_py_result.npz b/tests/ut/data/dataset/golden/random_crop_and_resize_03_py_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..621460e34ca7ba50d60de400e2d18d8e7ee93ef4 Binary files /dev/null and b/tests/ut/data/dataset/golden/random_crop_and_resize_03_py_result.npz differ diff --git a/tests/ut/python/dataset/test_HWC2CHW.py b/tests/ut/python/dataset/test_HWC2CHW.py index b15cedd726a9d3f7ff60fa36ba4e2f2c3fe71762..98e9ba72ba06cf8384be967c2f138ce600add0dd 100644 --- a/tests/ut/python/dataset/test_HWC2CHW.py +++ b/tests/ut/python/dataset/test_HWC2CHW.py @@ -13,12 +13,11 @@ # limitations under the License. # ============================================================================== import numpy as np -from util import diff_mse, visualize, save_and_check_md5 - import mindspore.dataset as ds import mindspore.dataset.transforms.vision.c_transforms as c_vision import mindspore.dataset.transforms.vision.py_transforms as py_vision from mindspore import log as logger +from util import diff_mse, visualize, save_and_check_md5 GENERATE_GOLDEN = False @@ -46,12 +45,14 @@ def test_HWC2CHW(plot=False): image_transposed = [] image = [] for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): - image_transposed.append(item1["image"].copy()) - image.append(item2["image"].copy()) + transposed_item = item1["image"].copy() + original_item = item2["image"].copy() + image_transposed.append(transposed_item.transpose(1, 2, 0)) + image.append(original_item) # check if the shape of data is transposed correctly # transpose the original image from shape (H,W,C) to (C,H,W) - mse = diff_mse(item1['image'], item2['image'].transpose(2, 0, 1)) + mse = diff_mse(transposed_item, original_item.transpose(2, 0, 1)) assert mse == 0 if plot: visualize(image, image_transposed) @@ -108,15 +109,13 @@ def test_HWC2CHW_comp(plot=False): mse = diff_mse(py_image, c_image) # Note: The images aren't exactly the same due to rounding error assert mse < 0.001 - - image_c_transposed.append(item1["image"].copy()) - image_py_transposed.append(item2["image"].copy()) - + image_c_transposed.append(c_image.transpose(1, 2, 0)) + image_py_transposed.append(py_image.transpose(1, 2, 0)) if plot: visualize(image_c_transposed, image_py_transposed) if __name__ == '__main__': - test_HWC2CHW() + test_HWC2CHW(True) test_HWC2CHW_md5() - test_HWC2CHW_comp() + test_HWC2CHW_comp(True) diff --git a/tests/ut/python/dataset/test_center_crop.py b/tests/ut/python/dataset/test_center_crop.py index c0731c855dbc3614089b1c3858afe95c6d9ab290..bbe284edc3f8c7d34180cb93c0be8ef4a4237e1b 100644 --- a/tests/ut/python/dataset/test_center_crop.py +++ b/tests/ut/python/dataset/test_center_crop.py @@ -13,12 +13,11 @@ # limitations under the License. # ============================================================================== import numpy as np -from util import diff_mse, visualize, save_and_check_md5 - import mindspore.dataset as ds import mindspore.dataset.transforms.vision.c_transforms as vision import mindspore.dataset.transforms.vision.py_transforms as py_vision from mindspore import log as logger +from util import diff_mse, visualize, save_and_check_md5 GENERATE_GOLDEN = False @@ -101,8 +100,8 @@ def test_center_crop_comp(height=375, width=375, plot=False): py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) # Note: The images aren't exactly the same due to rounding error assert diff_mse(py_image, c_image) < 0.001 - image_cropped.append(item1["image"].copy()) - image.append(item2["image"].copy()) + image_cropped.append(c_image.copy()) + image.append(py_image.copy()) if plot: visualize(image, image_cropped) @@ -141,9 +140,9 @@ def test_crop_grayscale(height=375, width=375): if __name__ == "__main__": - test_center_crop_op(600, 600) + test_center_crop_op(600, 600, True) test_center_crop_op(300, 600) test_center_crop_op(600, 300) test_center_crop_md5() - test_center_crop_comp() + test_center_crop_comp(True) test_crop_grayscale() diff --git a/tests/ut/python/dataset/test_random_crop.py b/tests/ut/python/dataset/test_random_crop.py index 9647f31a366aff7f516e2e45a196678be1d227de..de94986e48b9f51985a1ab8fbe63844e0af8b6e9 100644 --- a/tests/ut/python/dataset/test_random_crop.py +++ b/tests/ut/python/dataset/test_random_crop.py @@ -13,37 +13,24 @@ # limitations under the License. # ============================================================================== """ -Testing RandomCropAndResize op in DE +Testing RandomCrop op in DE """ -import matplotlib.pyplot as plt - +import numpy as np +import mindspore.dataset.transforms.vision.c_transforms as c_vision +import mindspore.dataset.transforms.vision.py_transforms as py_vision +import mindspore.dataset.transforms.vision.utils as mode import mindspore.dataset as ds -import mindspore.dataset.transforms.vision.c_transforms as vision from mindspore import log as logger +from util import save_and_check_md5, visualize -DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"] -SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json" - - -def visualize(a, mse, original): - """ - visualizes the image using DE op and Numpy op - """ - plt.subplot(141) - plt.imshow(original) - plt.title("Original image") - plt.subplot(142) - plt.imshow(a) - plt.title("DE random_crop image") +GENERATE_GOLDEN = False - plt.subplot(143) - plt.imshow(a - original) - plt.title("Difference image, mse : {}".format(mse)) - plt.show() +DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"] +SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json" -def test_random_crop_op(): +def test_random_crop_op(plot=False): """ Test RandomCrop Op """ @@ -51,8 +38,9 @@ def test_random_crop_op(): # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) - random_crop_op = vision.RandomCrop([512, 512], [200, 200, 200, 200]) - decode_op = vision.Decode() + random_crop_op = c_vision.RandomCrop([512, 512], [200, 200, 200, 200]) + decode_op = c_vision.Decode() + data1 = data1.map(input_columns=["image"], operations=decode_op) data1 = data1.map(input_columns=["image"], operations=random_crop_op) @@ -60,10 +48,454 @@ def test_random_crop_op(): data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) data2 = data2.map(input_columns=["image"], operations=decode_op) + image_cropped = [] + image = [] for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): image1 = item1["image"] image2 = item2["image"] + image_cropped.append(image1) + image.append(image2) + if plot: + visualize(image, image_cropped) + +def test_random_crop_01_c(): + """ + Test RandomCrop op with c_transforms: size is a single integer, expected to pass + """ + logger.info("test_random_crop_01_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: If size is an int, a square crop of size (size, size) is returned. + random_crop_op = c_vision.RandomCrop(512) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + + filename = "random_crop_01_c_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_01_py(): + """ + Test RandomCrop op with py_transforms: size is a single integer, expected to pass + """ + logger.info("test_random_crop_01_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: If size is an int, a square crop of size (size, size) is returned. + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop(512), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + + filename = "random_crop_01_py_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_02_c(): + """ + Test RandomCrop op with c_transforms: size is a list/tuple with length 2, expected to pass + """ + logger.info("test_random_crop_02_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: If size is a sequence of length 2, it should be (height, width). + random_crop_op = c_vision.RandomCrop([512, 375]) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + + filename = "random_crop_02_c_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_02_py(): + """ + Test RandomCrop op with py_transforms: size is a list/tuple with length 2, expected to pass + """ + logger.info("test_random_crop_02_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: If size is a sequence of length 2, it should be (height, width). + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop([512, 375]), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + + filename = "random_crop_02_py_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_03_c(): + """ + Test RandomCrop op with c_transforms: input image size == crop size, expected to pass + """ + logger.info("test_random_crop_03_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The size of the image is 4032*2268 + random_crop_op = c_vision.RandomCrop([2268, 4032]) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + + filename = "random_crop_03_c_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_03_py(): + """ + Test RandomCrop op with py_transforms: input image size == crop size, expected to pass + """ + logger.info("test_random_crop_03_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The size of the image is 4032*2268 + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop([2268, 4032]), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + + filename = "random_crop_03_py_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_04_c(): + """ + Test RandomCrop op with c_transforms: input image size < crop size, expected to fail + """ + logger.info("test_random_crop_04_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + try: + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The size of the image is 4032*2268 + random_crop_op = c_vision.RandomCrop([2268, 4033]) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + image_list = [] + for item in data.create_dict_iterator(): + image = item["image"] + image_list.append(image.shape) + except BaseException as e: + logger.info("Got an exception in DE: {}".format(str(e))) + +def test_random_crop_04_py(): + """ + Test RandomCrop op with py_transforms: + input image size < crop size, expected to fail + """ + logger.info("test_random_crop_04_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + try: + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The size of the image is 4032*2268 + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop([2268, 4033]), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + image_list = [] + for item in data.create_dict_iterator(): + image = (item["image"].transpose(1, 2, 0) * 255).astype(np.uint8) + image_list.append(image.shape) + except BaseException as e: + logger.info("Got an exception in DE: {}".format(str(e))) + +def test_random_crop_05_c(): + """ + Test RandomCrop op with c_transforms: + input image size < crop size but pad_if_needed is enabled, + expected to pass + """ + logger.info("test_random_crop_05_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The size of the image is 4032*2268 + random_crop_op = c_vision.RandomCrop([2268, 4033], [200, 200, 200, 200], pad_if_needed=True) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + filename = "random_crop_05_c_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_05_py(): + """ + Test RandomCrop op with py_transforms: + input image size < crop size but pad_if_needed is enabled, + expected to pass + """ + logger.info("test_random_crop_05_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The size of the image is 4032*2268 + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop([2268, 4033], [200, 200, 200, 200], pad_if_needed=True), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + + filename = "random_crop_05_py_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_06_c(): + """ + Test RandomCrop op with c_transforms: + invalid size, expected to raise TypeError + """ + logger.info("test_random_crop_06_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + try: + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: if size is neither an int nor a list of length 2, an exception will raise + random_crop_op = c_vision.RandomCrop([512, 512, 375]) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + image_list = [] + for item in data.create_dict_iterator(): + image = item["image"] + image_list.append(image.shape) + except TypeError as e: + logger.info("Got an exception in DE: {}".format(str(e))) + assert "Size" in str(e) + +def test_random_crop_06_py(): + """ + Test RandomCrop op with py_transforms: + invalid size, expected to raise TypeError + """ + logger.info("test_random_crop_06_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + try: + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: if size is neither an int nor a list of length 2, an exception will raise + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop([512, 512, 375]), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + image_list = [] + for item in data.create_dict_iterator(): + image = (item["image"].transpose(1, 2, 0) * 255).astype(np.uint8) + image_list.append(image.shape) + except TypeError as e: + logger.info("Got an exception in DE: {}".format(str(e))) + assert "Size" in str(e) + +def test_random_crop_07_c(): + """ + Test RandomCrop op with c_transforms: + padding_mode is Border.CONSTANT and fill_value is 255 (White), + expected to pass + """ + logger.info("test_random_crop_07_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The padding_mode is default as Border.CONSTANT and set filling color to be white. + random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], fill_value=(255, 255, 255)) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + + filename = "random_crop_07_c_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_07_py(): + """ + Test RandomCrop op with py_transforms: + padding_mode is Border.CONSTANT and fill_value is 255 (White), + expected to pass + """ + logger.info("test_random_crop_07_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The padding_mode is default as Border.CONSTANT and set filling color to be white. + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop(512, [200, 200, 200, 200], fill_value=(255, 255, 255)), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + + filename = "random_crop_07_py_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_08_c(): + """ + Test RandomCrop op with c_transforms: padding_mode is Border.EDGE, + expected to pass + """ + logger.info("test_random_crop_08_c") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The padding_mode is Border.EDGE. + random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=mode.Border.EDGE) + decode_op = c_vision.Decode() + data = data.map(input_columns=["image"], operations=decode_op) + data = data.map(input_columns=["image"], operations=random_crop_op) + + filename = "random_crop_08_c_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_08_py(): + """ + Test RandomCrop op with py_transforms: padding_mode is Border.EDGE, + expected to pass + """ + logger.info("test_random_crop_08_py") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + # Note: The padding_mode is Border.EDGE. + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=mode.Border.EDGE), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data = data.map(input_columns=["image"], operations=transform()) + + filename = "random_crop_08_py_result.npz" + save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_09(): + """ + Test RandomCrop op: invalid type of input image (not PIL), expected to raise TypeError + """ + logger.info("test_random_crop_09") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # Generate dataset + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + transforms = [ + py_vision.Decode(), + py_vision.ToTensor(), + # Note: if input is not PIL image, TypeError will raise + py_vision.RandomCrop(512) + ] + transform = py_vision.ComposeOp(transforms) + try: + data = data.map(input_columns=["image"], operations=transform()) + image_list = [] + for item in data.create_dict_iterator(): + image = item["image"] + image_list.append(image.shape) + except BaseException as e: + logger.info("Got an exception in DE: {}".format(str(e))) + assert "should be PIL Image" in str(e) + +def test_random_crop_comp(plot=False): + """ + Test RandomCrop and compare between python and c image augmentation + """ + logger.info("Test RandomCrop with c_transform and py_transform comparison") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + cropped_size = 512 + + # First dataset + data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + random_crop_op = c_vision.RandomCrop(cropped_size) + decode_op = c_vision.Decode() + data1 = data1.map(input_columns=["image"], operations=decode_op) + data1 = data1.map(input_columns=["image"], operations=random_crop_op) + + # Second dataset + data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + transforms = [ + py_vision.Decode(), + py_vision.RandomCrop(cropped_size), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data2 = data2.map(input_columns=["image"], operations=transform()) + + image_c_cropped = [] + image_py_cropped = [] + for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): + c_image = item1["image"] + py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) + image_c_cropped.append(c_image) + image_py_cropped.append(py_image) + if plot: + visualize(image_c_cropped, image_py_cropped) if __name__ == "__main__": - test_random_crop_op() + test_random_crop_01_c() + test_random_crop_02_c() + test_random_crop_03_c() + test_random_crop_04_c() + test_random_crop_05_c() + test_random_crop_06_c() + test_random_crop_07_c() + test_random_crop_08_c() + test_random_crop_01_py() + test_random_crop_02_py() + test_random_crop_03_py() + test_random_crop_04_py() + test_random_crop_05_py() + test_random_crop_06_py() + test_random_crop_07_py() + test_random_crop_08_py() + test_random_crop_09() + test_random_crop_op(True) + test_random_crop_comp(True) diff --git a/tests/ut/python/dataset/test_random_crop_and_resize.py b/tests/ut/python/dataset/test_random_crop_and_resize.py index 8138e9e4318b929445e93e0193df58c5dbf92c91..ce286ec43ba6a1f7006ffc8b06420787ca81302a 100644 --- a/tests/ut/python/dataset/test_random_crop_and_resize.py +++ b/tests/ut/python/dataset/test_random_crop_and_resize.py @@ -16,36 +16,21 @@ Testing RandomCropAndResize op in DE """ import cv2 -import matplotlib.pyplot as plt import numpy as np - +import mindspore.dataset.transforms.vision.c_transforms as c_vision +import mindspore.dataset.transforms.vision.py_transforms as py_vision +import mindspore.dataset.transforms.vision.utils as mode import mindspore.dataset as ds -import mindspore.dataset.transforms.vision.c_transforms as vision from mindspore import log as logger +from util import diff_mse, save_and_check_md5, visualize DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"] SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json" +GENERATE_GOLDEN = False -def visualize(a, mse, original): - """ - visualizes the image using DE op and Numpy op - """ - plt.subplot(141) - plt.imshow(original) - plt.title("Original image") - - plt.subplot(142) - plt.imshow(a) - plt.title("DE random_crop_and_resize image") - - plt.subplot(143) - plt.imshow(a - original) - plt.title("Difference image, mse : {}".format(mse)) - plt.show() - -def test_random_crop_and_resize_op(): +def test_random_crop_and_resize_op(plot=False): """ Test RandomCropAndResize op """ @@ -53,30 +38,268 @@ def test_random_crop_and_resize_op(): # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) - decode_op = vision.Decode() - random_crop_and_resize_op = vision.RandomResizedCrop((256, 512), (1, 1), (0.5, 0.5)) + decode_op = c_vision.Decode() + random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), (1, 1), (0.5, 0.5)) data1 = data1.map(input_columns=["image"], operations=decode_op) data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op) # Second dataset data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) data2 = data2.map(input_columns=["image"], operations=decode_op) - num_iter = 0 + crop_and_resize_images = [] + original_images = [] for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): - - if num_iter > 0: - break crop_and_resize = item1["image"] original = item2["image"] + # Note: resize the original image with the same size as the one applied RandomResizedCrop() original = cv2.resize(original, (512, 256)) - diff = crop_and_resize - original - mse = np.sum(np.power(diff, 2)) + mse = diff_mse(crop_and_resize, original) + assert mse == 0 logger.info("random_crop_and_resize_op_{}, mse: {}".format(num_iter + 1, mse)) - # Uncomment below line if you want to visualize images - # visualize(crop_and_resize, mse, original) num_iter += 1 + crop_and_resize_images.append(crop_and_resize) + original_images.append(original) + if plot: + visualize(original_images, crop_and_resize_images) + +def test_random_crop_and_resize_01(): + """ + Test RandomCropAndResize with md5 check, expected to pass + """ + logger.info("test_random_crop_and_resize_01") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # First dataset + data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + decode_op = c_vision.Decode() + random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), (0.5, 1), (0.5, 1)) + data1 = data1.map(input_columns=["image"], operations=decode_op) + data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op) + + # Second dataset + data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + transforms = [ + py_vision.Decode(), + py_vision.RandomResizedCrop((256, 512), (0.5, 1), (0.5, 1)), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data2 = data2.map(input_columns=["image"], operations=transform()) + + filename1 = "random_crop_and_resize_01_c_result.npz" + filename2 = "random_crop_and_resize_01_py_result.npz" + save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN) + save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_and_resize_02(): + """ + Test RandomCropAndResize with md5 check:Image interpolation mode is Inter.NEAREST, + expected to pass + """ + logger.info("test_random_crop_and_resize_02") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # First dataset + data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + decode_op = c_vision.Decode() + random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), interpolation=mode.Inter.NEAREST) + data1 = data1.map(input_columns=["image"], operations=decode_op) + data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op) + + # Second dataset + data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + transforms = [ + py_vision.Decode(), + py_vision.RandomResizedCrop((256, 512), interpolation=mode.Inter.NEAREST), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data2 = data2.map(input_columns=["image"], operations=transform()) + + filename1 = "random_crop_and_resize_02_c_result.npz" + filename2 = "random_crop_and_resize_02_py_result.npz" + save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN) + save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_and_resize_03(): + """ + Test RandomCropAndResize with md5 check: max_attempts is 1, expected to pass + """ + logger.info("test_random_crop_and_resize_03") + ds.config.set_seed(0) + ds.config.set_num_parallel_workers(1) + + # First dataset + data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + decode_op = c_vision.Decode() + random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), max_attempts=1) + data1 = data1.map(input_columns=["image"], operations=decode_op) + data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op) + + # Second dataset + data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + transforms = [ + py_vision.Decode(), + py_vision.RandomResizedCrop((256, 512), max_attempts=1), + py_vision.ToTensor() + ] + transform = py_vision.ComposeOp(transforms) + data2 = data2.map(input_columns=["image"], operations=transform()) + + filename1 = "random_crop_and_resize_03_c_result.npz" + filename2 = "random_crop_and_resize_03_py_result.npz" + save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN) + save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN) + +def test_random_crop_and_resize_04_c(): + """ + Test RandomCropAndResize with c_tranforms: invalid range of scale (max