1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15""" 16Testing Crop op in DE 17""" 18import cv2 19 20import mindspore.dataset as ds 21import mindspore.dataset.vision.c_transforms as c_vision 22 23from mindspore import log as logger 24from util import visualize_image, diff_mse 25 26DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"] 27SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json" 28IMAGE_FILE = "../data/dataset/apple.jpg" 29 30 31def test_crop_pipeline(plot=False): 32 """ 33 Test Crop of c_transforms 34 """ 35 logger.info("test_crop_pipeline") 36 37 # First dataset 38 dataset1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False) 39 decode_op = c_vision.Decode() 40 crop_op = c_vision.Crop((0, 0), (20, 25)) 41 dataset1 = dataset1.map(operations=decode_op, input_columns=["image"]) 42 dataset1 = dataset1.map(operations=crop_op, input_columns=["image"]) 43 44 # Second dataset 45 dataset2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 46 dataset2 = dataset2.map(operations=decode_op, input_columns=["image"]) 47 48 num_iter = 0 49 for data1, data2 in zip(dataset1.create_dict_iterator(num_epochs=1, output_numpy=True), 50 dataset2.create_dict_iterator(num_epochs=1, output_numpy=True)): 51 if num_iter > 0: 52 break 53 crop_ms = data1["image"] 54 original = data2["image"] 55 crop_expect = original[0:20, 0:25] 56 mse = diff_mse(crop_ms, crop_expect) 57 logger.info("crop_{}, mse: {}".format(num_iter + 1, mse)) 58 assert mse == 0 59 num_iter += 1 60 if plot: 61 visualize_image(original, crop_ms, mse, crop_expect) 62 63 64def test_crop_eager(): 65 """ 66 Test Crop with eager mode 67 """ 68 logger.info("test_crop_eager") 69 img = cv2.imread(IMAGE_FILE) 70 71 img_ms = c_vision.Crop((20, 50), (30, 50))(img) 72 img_expect = img[20:50, 50:100] 73 mse = diff_mse(img_ms, img_expect) 74 assert mse == 0 75 76 77def test_crop_exception(): 78 """ 79 Test Crop with invalid parameters 80 """ 81 logger.info("test_crop_exception") 82 try: 83 _ = c_vision.Crop([-10, 0], [20]) 84 except ValueError as e: 85 logger.info("Got an exception in Crop: {}".format(str(e))) 86 assert "not within the required interval of [0, 2147483647]" in str(e) 87 try: 88 _ = c_vision.Crop([0, 5.2], [10, 10]) 89 except TypeError as e: 90 logger.info("Got an exception in Crop: {}".format(str(e))) 91 assert "not of type [<class 'int'>]" in str(e) 92 try: 93 _ = c_vision.Crop([0], [28]) 94 except TypeError as e: 95 logger.info("Got an exception in Crop: {}".format(str(e))) 96 assert "Coordinates should be a list/tuple (y, x) of length 2." in str(e) 97 try: 98 _ = c_vision.Crop((0, 0), -1) 99 except ValueError as e: 100 logger.info("Got an exception in Crop: {}".format(str(e))) 101 assert "not within the required interval of [1, 16777216]" in str(e) 102 try: 103 _ = c_vision.Crop((0, 0), (10.5, 15)) 104 except TypeError as e: 105 logger.info("Got an exception in Crop: {}".format(str(e))) 106 assert "not of type [<class 'int'>]" in str(e) 107 try: 108 _ = c_vision.Crop((0, 0), (0, 10, 20)) 109 except TypeError as e: 110 logger.info("Got an exception in Crop: {}".format(str(e))) 111 assert "Size should be a single integer or a list/tuple (h, w) of length 2." in str(e) 112 113 114if __name__ == "__main__": 115 test_crop_pipeline(plot=False) 116 test_crop_eager() 117 test_crop_exception() 118