1# Copyright 2019 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 CutOut op in DE 17""" 18import numpy as np 19 20import mindspore.dataset as ds 21import mindspore.dataset.transforms.py_transforms 22import mindspore.dataset.vision.c_transforms as c 23import mindspore.dataset.vision.py_transforms as f 24from mindspore import log as logger 25from util import visualize_image, visualize_list, diff_mse, save_and_check_md5, \ 26 config_get_set_seed, config_get_set_num_parallel_workers 27 28DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"] 29SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json" 30 31GENERATE_GOLDEN = False 32 33 34def test_cut_out_op(plot=False): 35 """ 36 Test Cutout 37 """ 38 logger.info("test_cut_out") 39 40 # First dataset 41 data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 42 43 transforms_1 = [ 44 f.Decode(), 45 f.ToTensor(), 46 f.RandomErasing(value='random') 47 ] 48 transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1) 49 data1 = data1.map(operations=transform_1, input_columns=["image"]) 50 51 # Second dataset 52 data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 53 decode_op = c.Decode() 54 cut_out_op = c.CutOut(80) 55 56 transforms_2 = [ 57 decode_op, 58 cut_out_op 59 ] 60 61 data2 = data2.map(operations=transforms_2, input_columns=["image"]) 62 63 num_iter = 0 64 for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True), 65 data2.create_dict_iterator(num_epochs=1, output_numpy=True)): 66 num_iter += 1 67 image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8) 68 # C image doesn't require transpose 69 image_2 = item2["image"] 70 71 logger.info("shape of image_1: {}".format(image_1.shape)) 72 logger.info("shape of image_2: {}".format(image_2.shape)) 73 74 logger.info("dtype of image_1: {}".format(image_1.dtype)) 75 logger.info("dtype of image_2: {}".format(image_2.dtype)) 76 77 mse = diff_mse(image_1, image_2) 78 if plot: 79 visualize_image(image_1, image_2, mse) 80 81 82def test_cut_out_op_multicut(plot=False): 83 """ 84 Test Cutout 85 """ 86 logger.info("test_cut_out") 87 88 # First dataset 89 data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 90 91 transforms_1 = [ 92 f.Decode(), 93 f.ToTensor(), 94 ] 95 transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1) 96 data1 = data1.map(operations=transform_1, input_columns=["image"]) 97 98 # Second dataset 99 data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 100 decode_op = c.Decode() 101 cut_out_op = c.CutOut(80, num_patches=10) 102 103 transforms_2 = [ 104 decode_op, 105 cut_out_op 106 ] 107 108 data2 = data2.map(operations=transforms_2, input_columns=["image"]) 109 110 num_iter = 0 111 image_list_1, image_list_2 = [], [] 112 for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True), 113 data2.create_dict_iterator(num_epochs=1, output_numpy=True)): 114 num_iter += 1 115 image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8) 116 # C image doesn't require transpose 117 image_2 = item2["image"] 118 image_list_1.append(image_1) 119 image_list_2.append(image_2) 120 121 logger.info("shape of image_1: {}".format(image_1.shape)) 122 logger.info("shape of image_2: {}".format(image_2.shape)) 123 124 logger.info("dtype of image_1: {}".format(image_1.dtype)) 125 logger.info("dtype of image_2: {}".format(image_2.dtype)) 126 if plot: 127 visualize_list(image_list_1, image_list_2) 128 129 130def test_cut_out_md5(): 131 """ 132 Test Cutout with md5 check 133 """ 134 logger.info("test_cut_out_md5") 135 original_seed = config_get_set_seed(2) 136 original_num_parallel_workers = config_get_set_num_parallel_workers(1) 137 138 # First dataset 139 data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 140 decode_op = c.Decode() 141 cut_out_op = c.CutOut(100) 142 data1 = data1.map(operations=decode_op, input_columns=["image"]) 143 data1 = data1.map(operations=cut_out_op, input_columns=["image"]) 144 145 data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 146 transforms = [ 147 f.Decode(), 148 f.ToTensor(), 149 f.Cutout(100) 150 ] 151 transform = mindspore.dataset.transforms.py_transforms.Compose(transforms) 152 data2 = data2.map(operations=transform, input_columns=["image"]) 153 154 # Compare with expected md5 from images 155 filename1 = "cut_out_01_c_result.npz" 156 save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN) 157 filename2 = "cut_out_01_py_result.npz" 158 save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN) 159 160 # Restore config 161 ds.config.set_seed(original_seed) 162 ds.config.set_num_parallel_workers(original_num_parallel_workers) 163 164 165def test_cut_out_comp(plot=False): 166 """ 167 Test Cutout with c++ and python op comparison 168 """ 169 logger.info("test_cut_out_comp") 170 171 # First dataset 172 data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 173 174 transforms_1 = [ 175 f.Decode(), 176 f.ToTensor(), 177 f.Cutout(200) 178 ] 179 transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1) 180 data1 = data1.map(operations=transform_1, input_columns=["image"]) 181 182 # Second dataset 183 data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) 184 185 transforms_2 = [ 186 c.Decode(), 187 c.CutOut(200) 188 ] 189 190 data2 = data2.map(operations=transforms_2, input_columns=["image"]) 191 192 num_iter = 0 193 image_list_1, image_list_2 = [], [] 194 for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True), 195 data2.create_dict_iterator(num_epochs=1, output_numpy=True)): 196 num_iter += 1 197 image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8) 198 # C image doesn't require transpose 199 image_2 = item2["image"] 200 image_list_1.append(image_1) 201 image_list_2.append(image_2) 202 203 logger.info("shape of image_1: {}".format(image_1.shape)) 204 logger.info("shape of image_2: {}".format(image_2.shape)) 205 206 logger.info("dtype of image_1: {}".format(image_1.dtype)) 207 logger.info("dtype of image_2: {}".format(image_2.dtype)) 208 if plot: 209 visualize_list(image_list_1, image_list_2, visualize_mode=2) 210 211 212if __name__ == "__main__": 213 test_cut_out_op(plot=True) 214 test_cut_out_op_multicut(plot=True) 215 test_cut_out_md5() 216 test_cut_out_comp(plot=True) 217