• 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 Pad 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_vision
23import mindspore.dataset.vision.py_transforms as py_vision
24from mindspore import log as logger
25from util import diff_mse, save_and_check_md5
26
27DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
28SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
29
30GENERATE_GOLDEN = False
31
32def test_pad_op():
33    """
34    Test Pad op
35    """
36    logger.info("test_random_color_jitter_op")
37
38    # First dataset
39    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
40    decode_op = c_vision.Decode()
41
42    pad_op = c_vision.Pad((100, 100, 100, 100))
43    ctrans = [decode_op,
44              pad_op,
45              ]
46
47    data1 = data1.map(operations=ctrans, input_columns=["image"])
48
49    # Second dataset
50    transforms = [
51        py_vision.Decode(),
52        py_vision.Pad(100),
53        py_vision.ToTensor(),
54    ]
55    transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
56    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
57    data2 = data2.map(operations=transform, input_columns=["image"])
58
59    for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
60                            data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
61        c_image = item1["image"]
62        py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
63
64        logger.info("shape of c_image: {}".format(c_image.shape))
65        logger.info("shape of py_image: {}".format(py_image.shape))
66
67        logger.info("dtype of c_image: {}".format(c_image.dtype))
68        logger.info("dtype of py_image: {}".format(py_image.dtype))
69
70        mse = diff_mse(c_image, py_image)
71        logger.info("mse is {}".format(mse))
72        assert mse < 0.01
73
74
75def test_pad_op2():
76    """
77    Test Pad op2
78    """
79    logger.info("test padding parameter with size 2")
80
81    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
82    decode_op = c_vision.Decode()
83    resize_op = c_vision.Resize([90, 90])
84    pad_op = c_vision.Pad((100, 9,))
85    ctrans = [decode_op, resize_op, pad_op]
86
87    data1 = data1.map(operations=ctrans, input_columns=["image"])
88    for data in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
89        logger.info(data["image"].shape)
90        # It pads left, top with 100 and right, bottom with 9,
91        # so the final size of image is 90 + 100 + 9 = 199
92        assert data["image"].shape[0] == 199
93        assert data["image"].shape[1] == 199
94
95
96def test_pad_grayscale():
97    """
98    Tests that the pad works for grayscale images
99    """
100
101    # Note: image.transpose performs channel swap to allow py transforms to
102    # work with c transforms
103    transforms = [
104        py_vision.Decode(),
105        py_vision.Grayscale(1),
106        py_vision.ToTensor(),
107        (lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8))
108    ]
109
110    transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
111    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
112    data1 = data1.map(operations=transform, input_columns=["image"])
113
114    # if input is grayscale, the output dimensions should be single channel
115    pad_gray = c_vision.Pad(100, fill_value=(20, 20, 20))
116    data1 = data1.map(operations=pad_gray, input_columns=["image"])
117    dataset_shape_1 = []
118    for item1 in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
119        c_image = item1["image"]
120        dataset_shape_1.append(c_image.shape)
121
122    # Dataset for comparison
123    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
124    decode_op = c_vision.Decode()
125
126    # we use the same padding logic
127    ctrans = [decode_op, pad_gray]
128    dataset_shape_2 = []
129
130    data2 = data2.map(operations=ctrans, input_columns=["image"])
131
132    for item2 in data2.create_dict_iterator(num_epochs=1, output_numpy=True):
133        c_image = item2["image"]
134        dataset_shape_2.append(c_image.shape)
135
136    for shape1, shape2 in zip(dataset_shape_1, dataset_shape_2):
137        # validate that the first two dimensions are the same
138        # we have a little inconsistency here because the third dimension is 1 after py_vision.Grayscale
139        assert shape1[0:1] == shape2[0:1]
140
141
142def test_pad_md5():
143    """
144    Test Pad with md5 check
145    """
146    logger.info("test_pad_md5")
147
148    # First dataset
149    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
150    decode_op = c_vision.Decode()
151    pad_op = c_vision.Pad(150)
152    ctrans = [decode_op,
153              pad_op,
154              ]
155
156    data1 = data1.map(operations=ctrans, input_columns=["image"])
157
158    # Second dataset
159    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
160    pytrans = [
161        py_vision.Decode(),
162        py_vision.Pad(150),
163        py_vision.ToTensor(),
164    ]
165    transform = mindspore.dataset.transforms.py_transforms.Compose(pytrans)
166    data2 = data2.map(operations=transform, input_columns=["image"])
167    # Compare with expected md5 from images
168    filename1 = "pad_01_c_result.npz"
169    save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN)
170    filename2 = "pad_01_py_result.npz"
171    save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN)
172
173
174if __name__ == "__main__":
175    test_pad_op()
176    test_pad_grayscale()
177    test_pad_md5()
178