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 Rotate Python API 17""" 18import cv2 19 20import mindspore.dataset as ds 21import mindspore.dataset.vision.c_transforms as c_vision 22from mindspore import log as logger 23from mindspore.dataset.vision.utils import Inter 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_rotate_pipeline_with_expanding(plot=False): 32 """ 33 Test Rotate of c_transforms with expanding 34 """ 35 logger.info("test_rotate_pipeline_with_expanding") 36 37 # First dataset 38 dataset1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False) 39 decode_op = c_vision.Decode() 40 rotate_op = c_vision.Rotate(90, expand=True) 41 dataset1 = dataset1.map(operations=decode_op, input_columns=["image"]) 42 dataset1 = dataset1.map(operations=rotate_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 rotate_ms = data1["image"] 54 original = data2["image"] 55 rotate_cv = cv2.rotate(original, cv2.ROTATE_90_COUNTERCLOCKWISE) 56 mse = diff_mse(rotate_ms, rotate_cv) 57 logger.info("rotate_{}, mse: {}".format(num_iter + 1, mse)) 58 assert mse == 0 59 num_iter += 1 60 if plot: 61 visualize_image(original, rotate_ms, mse, rotate_cv) 62 63 64def test_rotate_pipeline_without_expanding(): 65 """ 66 Test Rotate of c_transforms without expanding 67 """ 68 logger.info("test_rotate_pipeline_without_expanding") 69 70 # Create a Dataset then decode and rotate the image 71 dataset = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False) 72 decode_op = c_vision.Decode() 73 resize_op = c_vision.Resize((64, 128)) 74 rotate_op = c_vision.Rotate(30) 75 dataset = dataset.map(operations=decode_op, input_columns=["image"]) 76 dataset = dataset.map(operations=resize_op, input_columns=["image"]) 77 dataset = dataset.map(operations=rotate_op, input_columns=["image"]) 78 79 for data in dataset.create_dict_iterator(num_epochs=1, output_numpy=True): 80 rotate_img = data["image"] 81 assert rotate_img.shape == (64, 128, 3) 82 83 84def test_rotate_eager(): 85 """ 86 Test Rotate with eager mode 87 """ 88 logger.info("test_rotate_eager") 89 img = cv2.imread(IMAGE_FILE) 90 resize_img = c_vision.Resize((32, 64))(img) 91 rotate_img = c_vision.Rotate(-90, expand=True)(resize_img) 92 assert rotate_img.shape == (64, 32, 3) 93 94 95def test_rotate_exception(): 96 """ 97 Test Rotate with invalid parameters 98 """ 99 logger.info("test_rotate_exception") 100 try: 101 _ = c_vision.Rotate("60") 102 except TypeError as e: 103 logger.info("Got an exception in Rotate: {}".format(str(e))) 104 assert "not of type [<class 'float'>, <class 'int'>]" in str(e) 105 try: 106 _ = c_vision.Rotate(30, Inter.BICUBIC, False, (0, 0, 0)) 107 except ValueError as e: 108 logger.info("Got an exception in Rotate: {}".format(str(e))) 109 assert "Value center needs to be a 2-tuple." in str(e) 110 try: 111 _ = c_vision.Rotate(-120, Inter.NEAREST, False, (-1, -1), (255, 255)) 112 except TypeError as e: 113 logger.info("Got an exception in Rotate: {}".format(str(e))) 114 assert "fill_value should be a single integer or a 3-tuple." in str(e) 115 116 117if __name__ == "__main__": 118 test_rotate_pipeline_with_expanding(False) 119 test_rotate_pipeline_without_expanding() 120 test_rotate_eager() 121 test_rotate_exception() 122