• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Normalize op in DE
17"""
18import numpy as np
19import mindspore.dataset as ds
20import mindspore.dataset.transforms.py_transforms
21import mindspore.dataset.vision.c_transforms as c_vision
22import mindspore.dataset.vision.py_transforms as py_vision
23from mindspore import log as logger
24from util import diff_mse, save_and_check_md5, visualize_image
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"
28
29GENERATE_GOLDEN = False
30
31
32def normalize_np(image, mean, std):
33    """
34    Apply the Normalization
35    """
36    #  DE decodes the image in RGB by default, hence
37    #  the values here are in RGB
38    image = np.array(image, np.float32)
39    image = image - np.array(mean)
40    image = image * (1.0 / np.array(std))
41    return image
42
43
44def util_test_normalize(mean, std, op_type):
45    """
46    Utility function for testing Normalize. Input arguments are given by other tests
47    """
48    if op_type == "cpp":
49        # define map operations
50        decode_op = c_vision.Decode()
51        normalize_op = c_vision.Normalize(mean, std)
52        # Generate dataset
53        data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
54        data = data.map(operations=decode_op, input_columns=["image"])
55        data = data.map(operations=normalize_op, input_columns=["image"])
56    elif op_type == "python":
57        # define map operations
58        transforms = [
59            py_vision.Decode(),
60            py_vision.ToTensor(),
61            py_vision.Normalize(mean, std)
62        ]
63        transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
64        # Generate dataset
65        data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
66        data = data.map(operations=transform, input_columns=["image"])
67    else:
68        raise ValueError("Wrong parameter value")
69    return data
70
71
72def util_test_normalize_grayscale(num_output_channels, mean, std):
73    """
74    Utility function for testing Normalize. Input arguments are given by other tests
75    """
76    transforms = [
77        py_vision.Decode(),
78        py_vision.Grayscale(num_output_channels),
79        py_vision.ToTensor(),
80        py_vision.Normalize(mean, std)
81    ]
82    transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
83    # Generate dataset
84    data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
85    data = data.map(operations=transform, input_columns=["image"])
86    return data
87
88
89def test_normalize_op_c(plot=False):
90    """
91    Test Normalize in cpp transformations
92    """
93    logger.info("Test Normalize in cpp")
94    mean = [121.0, 115.0, 100.0]
95    std = [70.0, 68.0, 71.0]
96    # define map operations
97    decode_op = c_vision.Decode()
98    normalize_op = c_vision.Normalize(mean, std)
99
100    #  First dataset
101    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
102    data1 = data1.map(operations=decode_op, input_columns=["image"])
103    data1 = data1.map(operations=normalize_op, input_columns=["image"])
104
105    #  Second dataset
106    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
107    data2 = data2.map(operations=decode_op, input_columns=["image"])
108
109    num_iter = 0
110    for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
111                            data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
112        image_de_normalized = item1["image"]
113        image_original = item2["image"]
114        image_np_normalized = normalize_np(image_original, mean, std)
115        mse = diff_mse(image_de_normalized, image_np_normalized)
116        logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
117        assert mse < 0.01
118        if plot:
119            visualize_image(image_original, image_de_normalized, mse, image_np_normalized)
120        num_iter += 1
121
122
123def test_normalize_op_py(plot=False):
124    """
125    Test Normalize in python transformations
126    """
127    logger.info("Test Normalize in python")
128    mean = [0.475, 0.45, 0.392]
129    std = [0.275, 0.267, 0.278]
130    # define map operations
131    transforms = [
132        py_vision.Decode(),
133        py_vision.ToTensor()
134    ]
135    transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
136    normalize_op = py_vision.Normalize(mean, std)
137
138    #  First dataset
139    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
140    data1 = data1.map(operations=transform, input_columns=["image"])
141    data1 = data1.map(operations=normalize_op, input_columns=["image"])
142
143    #  Second dataset
144    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
145    data2 = data2.map(operations=transform, input_columns=["image"])
146
147    num_iter = 0
148    for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
149                            data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
150        image_de_normalized = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
151        image_np_normalized = (normalize_np(item2["image"].transpose(1, 2, 0), mean, std) * 255).astype(np.uint8)
152        image_original = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
153        mse = diff_mse(image_de_normalized, image_np_normalized)
154        logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
155        assert mse < 0.01
156        if plot:
157            visualize_image(image_original, image_de_normalized, mse, image_np_normalized)
158        num_iter += 1
159
160
161def test_decode_op():
162    """
163    Test Decode op
164    """
165    logger.info("Test Decode")
166
167    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image", "label"], num_parallel_workers=1,
168                               shuffle=False)
169
170    # define map operations
171    decode_op = c_vision.Decode()
172
173    # apply map operations on images
174    data1 = data1.map(operations=decode_op, input_columns=["image"])
175
176    num_iter = 0
177    for item in data1.create_dict_iterator(num_epochs=1):
178        logger.info("Looping inside iterator {}".format(num_iter))
179        _ = item["image"]
180        num_iter += 1
181
182
183def test_decode_normalize_op():
184    """
185    Test Decode op followed by Normalize op
186    """
187    logger.info("Test [Decode, Normalize] in one Map")
188
189    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image", "label"], num_parallel_workers=1,
190                               shuffle=False)
191
192    # define map operations
193    decode_op = c_vision.Decode()
194    normalize_op = c_vision.Normalize([121.0, 115.0, 100.0], [70.0, 68.0, 71.0])
195
196    # apply map operations on images
197    data1 = data1.map(operations=[decode_op, normalize_op], input_columns=["image"])
198
199    num_iter = 0
200    for item in data1.create_dict_iterator(num_epochs=1):
201        logger.info("Looping inside iterator {}".format(num_iter))
202        _ = item["image"]
203        num_iter += 1
204
205
206def test_normalize_md5_01():
207    """
208    Test Normalize with md5 check: valid mean and std
209    expected to pass
210    """
211    logger.info("test_normalize_md5_01")
212    data_c = util_test_normalize([121.0, 115.0, 100.0], [70.0, 68.0, 71.0], "cpp")
213    data_py = util_test_normalize([0.475, 0.45, 0.392], [0.275, 0.267, 0.278], "python")
214
215    # check results with md5 comparison
216    filename1 = "normalize_01_c_result.npz"
217    filename2 = "normalize_01_py_result.npz"
218    save_and_check_md5(data_c, filename1, generate_golden=GENERATE_GOLDEN)
219    save_and_check_md5(data_py, filename2, generate_golden=GENERATE_GOLDEN)
220
221
222def test_normalize_md5_02():
223    """
224    Test Normalize with md5 check: len(mean)=len(std)=1 with RGB images
225    expected to pass
226    """
227    logger.info("test_normalize_md5_02")
228    data_py = util_test_normalize([0.475], [0.275], "python")
229
230    # check results with md5 comparison
231    filename2 = "normalize_02_py_result.npz"
232    save_and_check_md5(data_py, filename2, generate_golden=GENERATE_GOLDEN)
233
234
235def test_normalize_exception_unequal_size_c():
236    """
237    Test Normalize in c transformation: len(mean) != len(std)
238    expected to raise ValueError
239    """
240    logger.info("test_normalize_exception_unequal_size_c")
241    try:
242        _ = c_vision.Normalize([100, 250, 125], [50, 50, 75, 75])
243    except ValueError as e:
244        logger.info("Got an exception in DE: {}".format(str(e)))
245        assert str(e) == "Length of mean and std must be equal."
246
247
248def test_normalize_exception_out_of_range_c():
249    """
250    Test Normalize in c transformation: mean, std out of range
251    expected to raise ValueError
252    """
253    logger.info("test_normalize_exception_out_of_range_c")
254    try:
255        _ = c_vision.Normalize([256, 250, 125], [50, 75, 75])
256    except ValueError as e:
257        logger.info("Got an exception in DE: {}".format(str(e)))
258        assert "not within the required interval" in str(e)
259    try:
260        _ = c_vision.Normalize([255, 250, 125], [0, 75, 75])
261    except ValueError as e:
262        logger.info("Got an exception in DE: {}".format(str(e)))
263        assert "not within the required interval" in str(e)
264
265
266def test_normalize_exception_unequal_size_py():
267    """
268    Test Normalize in python transformation: len(mean) != len(std)
269    expected to raise ValueError
270    """
271    logger.info("test_normalize_exception_unequal_size_py")
272    try:
273        _ = py_vision.Normalize([0.50, 0.30, 0.75], [0.18, 0.32, 0.71, 0.72])
274    except ValueError as e:
275        logger.info("Got an exception in DE: {}".format(str(e)))
276        assert str(e) == "Length of mean and std must be equal."
277
278
279def test_normalize_exception_invalid_size_py():
280    """
281    Test Normalize in python transformation: len(mean)=len(std)=2
282    expected to raise RuntimeError
283    """
284    logger.info("test_normalize_exception_invalid_size_py")
285    data = util_test_normalize([0.75, 0.25], [0.18, 0.32], "python")
286    try:
287        _ = data.create_dict_iterator(num_epochs=1).__next__()
288    except RuntimeError as e:
289        logger.info("Got an exception in DE: {}".format(str(e)))
290        assert "Length of mean and std must both be 1 or" in str(e)
291
292
293def test_normalize_exception_invalid_range_py():
294    """
295    Test Normalize in python transformation: value is not in range [0,1]
296    expected to raise ValueError
297    """
298    logger.info("test_normalize_exception_invalid_range_py")
299    try:
300        _ = py_vision.Normalize([0.75, 1.25, 0.5], [0.1, 0.18, 1.32])
301    except ValueError as e:
302        logger.info("Got an exception in DE: {}".format(str(e)))
303        assert "Input mean_value is not within the required interval of [0.0, 1.0]." in str(e)
304
305
306def test_normalize_grayscale_md5_01():
307    """
308    Test Normalize with md5 check: len(mean)=len(std)=1 with 1 channel grayscale images
309    expected to pass
310    """
311    logger.info("test_normalize_grayscale_md5_01")
312    data = util_test_normalize_grayscale(1, [0.5], [0.175])
313    # check results with md5 comparison
314    filename = "normalize_03_py_result.npz"
315    save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
316
317
318def test_normalize_grayscale_md5_02():
319    """
320    Test Normalize with md5 check: len(mean)=len(std)=3 with 3 channel grayscale images
321    expected to pass
322    """
323    logger.info("test_normalize_grayscale_md5_02")
324    data = util_test_normalize_grayscale(3, [0.5, 0.5, 0.5], [0.175, 0.235, 0.512])
325    # check results with md5 comparison
326    filename = "normalize_04_py_result.npz"
327    save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
328
329
330def test_normalize_grayscale_exception():
331    """
332    Test Normalize: len(mean)=len(std)=3 with 1 channel grayscale images
333    expected to raise RuntimeError
334    """
335    logger.info("test_normalize_grayscale_exception")
336    try:
337        _ = util_test_normalize_grayscale(1, [0.5, 0.5, 0.5], [0.175, 0.235, 0.512])
338    except RuntimeError as e:
339        logger.info("Got an exception in DE: {}".format(str(e)))
340        assert "Input is not within the required range" in str(e)
341
342
343def test_multiple_channels():
344    logger.info("test_multiple_channels")
345
346    def util_test(item, mean, std):
347        data = ds.NumpySlicesDataset([item], shuffle=False)
348        data = data.map(c_vision.Normalize(mean, std))
349        for d in data.create_tuple_iterator(num_epochs=1, output_numpy=True):
350            actual = d[0]
351            mean = np.array(mean, dtype=item.dtype)
352            std = np.array(std, dtype=item.dtype)
353            expected = item
354            if len(item.shape) != 1 and len(mean) == 1:
355                mean = [mean[0]] * expected.shape[-1]
356                std = [std[0]] * expected.shape[-1]
357            if len(item.shape) == 2:
358                expected = np.expand_dims(expected, 2)
359            for c in range(expected.shape[-1]):
360                expected[:, :, c] = (expected[:, :, c] - mean[c]) / std[c]
361            expected = expected.squeeze()
362
363            np.testing.assert_almost_equal(actual, expected, decimal=6)
364
365    util_test(np.ones(shape=[2, 2, 3]), mean=[0.5, 0.6, 0.7], std=[0.1, 0.2, 0.3])
366    util_test(np.ones(shape=[20, 45, 3]) * 1.3, mean=[0.5, 0.6, 0.7], std=[0.1, 0.2, 0.3])
367    util_test(np.ones(shape=[20, 45, 4]) * 1.3, mean=[0.5, 0.6, 0.7, 0.8], std=[0.1, 0.2, 0.3, 0.4])
368    util_test(np.ones(shape=[2, 2]), mean=[0.5], std=[0.1])
369    util_test(np.ones(shape=[2, 2, 5]), mean=[0.5], std=[0.1])
370    util_test(np.ones(shape=[6, 6, 129]), mean=[0.5]*129, std=[0.1]*129)
371    util_test(np.ones(shape=[6, 6, 129]), mean=[0.5], std=[0.1])
372
373
374
375if __name__ == "__main__":
376    test_decode_op()
377    test_decode_normalize_op()
378    test_normalize_op_c(plot=True)
379    test_normalize_op_py(plot=True)
380    test_normalize_md5_01()
381    test_normalize_md5_02()
382    test_normalize_exception_unequal_size_c()
383    test_normalize_exception_unequal_size_py()
384    test_normalize_exception_invalid_size_py()
385    test_normalize_exception_invalid_range_py()
386    test_normalize_grayscale_md5_01()
387    test_normalize_grayscale_md5_02()
388    test_normalize_grayscale_exception()
389