2022-05-30 21:12:12 +08:00
|
|
|
# Copyright 2019-2022 Huawei Technologies Co., Ltd
|
2020-03-27 14:49:12 +08:00
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
"""
|
|
|
|
|
Testing Pad op in DE
|
|
|
|
|
"""
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
import mindspore.dataset as ds
|
2022-05-26 04:30:20 +08:00
|
|
|
import mindspore.dataset.transforms
|
|
|
|
|
import mindspore.dataset.vision as vision
|
2020-03-27 14:49:12 +08:00
|
|
|
from mindspore import log as logger
|
2022-07-29 23:11:31 +08:00
|
|
|
from util import diff_mse, save_and_check_md5, save_and_check_md5_pil
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
|
DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
|
|
|
|
|
SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
|
|
|
|
|
|
2020-06-25 23:31:35 +08:00
|
|
|
GENERATE_GOLDEN = False
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
def test_pad_op():
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: Pad op
|
|
|
|
|
Description: Test Pad op between Python and Cpp implementation
|
|
|
|
|
Expectation: Both outputs are the same as expected
|
2020-03-27 14:49:12 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("test_random_color_jitter_op")
|
|
|
|
|
|
|
|
|
|
# First dataset
|
|
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
2022-05-03 02:50:47 +08:00
|
|
|
decode_op = vision.Decode()
|
2020-03-27 14:49:12 +08:00
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
pad_op = vision.Pad((100, 100, 100, 100))
|
2020-03-27 14:49:12 +08:00
|
|
|
ctrans = [decode_op,
|
|
|
|
|
pad_op,
|
|
|
|
|
]
|
|
|
|
|
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=ctrans, input_columns=["image"])
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
|
# Second dataset
|
|
|
|
|
transforms = [
|
2022-05-03 02:50:47 +08:00
|
|
|
vision.Decode(True),
|
|
|
|
|
vision.Pad(100),
|
|
|
|
|
vision.ToTensor(),
|
2020-03-27 14:49:12 +08:00
|
|
|
]
|
2022-05-26 04:30:20 +08:00
|
|
|
transform = mindspore.dataset.transforms.Compose(transforms)
|
2020-03-27 14:49:12 +08:00
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
2020-09-10 01:23:02 +08:00
|
|
|
data2 = data2.map(operations=transform, input_columns=["image"])
|
2020-03-27 14:49:12 +08:00
|
|
|
|
2020-09-05 10:56:38 +08:00
|
|
|
for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
|
|
|
|
|
data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
|
2020-03-27 14:49:12 +08:00
|
|
|
c_image = item1["image"]
|
|
|
|
|
py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
logger.info("shape of c_image: {}".format(c_image.shape))
|
|
|
|
|
logger.info("shape of py_image: {}".format(py_image.shape))
|
|
|
|
|
|
|
|
|
|
logger.info("dtype of c_image: {}".format(c_image.dtype))
|
|
|
|
|
logger.info("dtype of py_image: {}".format(py_image.dtype))
|
|
|
|
|
|
|
|
|
|
mse = diff_mse(c_image, py_image)
|
|
|
|
|
logger.info("mse is {}".format(mse))
|
|
|
|
|
assert mse < 0.01
|
|
|
|
|
|
|
|
|
|
|
2021-02-05 17:11:52 +08:00
|
|
|
def test_pad_op2():
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: Pad op
|
|
|
|
|
Description: Test Pad op parameter with size 2
|
|
|
|
|
Expectation: Output's shape is the same as expected output's shape
|
2021-02-05 17:11:52 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("test padding parameter with size 2")
|
|
|
|
|
|
|
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
2022-05-03 02:50:47 +08:00
|
|
|
decode_op = vision.Decode()
|
|
|
|
|
resize_op = vision.Resize([90, 90])
|
|
|
|
|
pad_op = vision.Pad((100, 9,))
|
2021-02-05 17:11:52 +08:00
|
|
|
ctrans = [decode_op, resize_op, pad_op]
|
|
|
|
|
|
|
|
|
|
data1 = data1.map(operations=ctrans, input_columns=["image"])
|
|
|
|
|
for data in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
|
|
|
|
|
logger.info(data["image"].shape)
|
2022-10-18 15:29:22 +08:00
|
|
|
# It pads left, right with 100 and top, bottom with 9,
|
|
|
|
|
# so the final size of image row is 90 + 9 + 9 = 108
|
|
|
|
|
# so the final size of image col is 90 + 100 + 100 = 290
|
|
|
|
|
assert data["image"].shape[0] == 108
|
|
|
|
|
assert data["image"].shape[1] == 290
|
2021-02-05 17:11:52 +08:00
|
|
|
|
2020-06-26 09:41:42 +08:00
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
def test_pad_grayscale():
|
2020-05-07 09:05:12 +08:00
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: Pad op
|
|
|
|
|
Description: Test Pad op for grayscale images
|
|
|
|
|
Expectation: Output's shape is the same as expected output
|
2020-05-07 09:05:12 +08:00
|
|
|
"""
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-06-26 09:41:42 +08:00
|
|
|
# Note: image.transpose performs channel swap to allow py transforms to
|
|
|
|
|
# work with c transforms
|
2020-05-07 09:05:12 +08:00
|
|
|
transforms = [
|
2022-05-03 02:50:47 +08:00
|
|
|
vision.Decode(True),
|
|
|
|
|
vision.Grayscale(1),
|
|
|
|
|
vision.ToTensor(),
|
2020-06-26 09:41:42 +08:00
|
|
|
(lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8))
|
2020-05-07 09:05:12 +08:00
|
|
|
]
|
|
|
|
|
|
2022-05-26 04:30:20 +08:00
|
|
|
transform = mindspore.dataset.transforms.Compose(transforms)
|
2020-05-07 09:05:12 +08:00
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=transform, input_columns=["image"])
|
2020-05-07 09:05:12 +08:00
|
|
|
|
2020-05-26 16:17:53 +08:00
|
|
|
# if input is grayscale, the output dimensions should be single channel
|
2022-05-03 02:50:47 +08:00
|
|
|
pad_gray = vision.Pad(100, fill_value=(20, 20, 20))
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=pad_gray, input_columns=["image"])
|
2020-05-07 09:05:12 +08:00
|
|
|
dataset_shape_1 = []
|
2020-09-05 10:56:38 +08:00
|
|
|
for item1 in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
|
2020-05-07 09:05:12 +08:00
|
|
|
c_image = item1["image"]
|
|
|
|
|
dataset_shape_1.append(c_image.shape)
|
|
|
|
|
|
2020-05-26 16:17:53 +08:00
|
|
|
# Dataset for comparison
|
2020-05-07 09:05:12 +08:00
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
2022-05-03 02:50:47 +08:00
|
|
|
decode_op = vision.Decode()
|
2020-05-07 09:05:12 +08:00
|
|
|
|
2020-05-26 16:17:53 +08:00
|
|
|
# we use the same padding logic
|
2020-05-07 09:05:12 +08:00
|
|
|
ctrans = [decode_op, pad_gray]
|
|
|
|
|
dataset_shape_2 = []
|
|
|
|
|
|
2020-09-10 01:23:02 +08:00
|
|
|
data2 = data2.map(operations=ctrans, input_columns=["image"])
|
2020-05-07 09:05:12 +08:00
|
|
|
|
2020-09-05 10:56:38 +08:00
|
|
|
for item2 in data2.create_dict_iterator(num_epochs=1, output_numpy=True):
|
2020-05-07 09:05:12 +08:00
|
|
|
c_image = item2["image"]
|
|
|
|
|
dataset_shape_2.append(c_image.shape)
|
|
|
|
|
|
|
|
|
|
for shape1, shape2 in zip(dataset_shape_1, dataset_shape_2):
|
|
|
|
|
# validate that the first two dimensions are the same
|
2022-05-03 02:50:47 +08:00
|
|
|
# we have a little inconsistency here because the third dimension is 1 after vision.Grayscale
|
2020-05-22 14:16:07 +08:00
|
|
|
assert shape1[0:1] == shape2[0:1]
|
2020-05-07 09:05:12 +08:00
|
|
|
|
|
|
|
|
|
2020-06-25 23:31:35 +08:00
|
|
|
def test_pad_md5():
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: Pad op
|
|
|
|
|
Description: Test Pad op with md5 check
|
|
|
|
|
Expectation: Passes the md5 check test
|
2020-06-25 23:31:35 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("test_pad_md5")
|
|
|
|
|
|
|
|
|
|
# First dataset
|
|
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
2022-05-03 02:50:47 +08:00
|
|
|
decode_op = vision.Decode()
|
|
|
|
|
pad_op = vision.Pad(150)
|
2020-06-25 23:31:35 +08:00
|
|
|
ctrans = [decode_op,
|
|
|
|
|
pad_op,
|
|
|
|
|
]
|
|
|
|
|
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=ctrans, input_columns=["image"])
|
2020-06-25 23:31:35 +08:00
|
|
|
|
|
|
|
|
# Second dataset
|
|
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
|
|
|
|
pytrans = [
|
2022-05-03 02:50:47 +08:00
|
|
|
vision.Decode(True),
|
|
|
|
|
vision.Pad(150),
|
|
|
|
|
vision.ToTensor(),
|
2020-06-25 23:31:35 +08:00
|
|
|
]
|
2022-05-26 04:30:20 +08:00
|
|
|
transform = mindspore.dataset.transforms.Compose(pytrans)
|
2020-09-10 01:23:02 +08:00
|
|
|
data2 = data2.map(operations=transform, input_columns=["image"])
|
2020-06-25 23:31:35 +08:00
|
|
|
# Compare with expected md5 from images
|
|
|
|
|
filename1 = "pad_01_c_result.npz"
|
|
|
|
|
save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN)
|
|
|
|
|
filename2 = "pad_01_py_result.npz"
|
2022-07-29 23:11:31 +08:00
|
|
|
save_and_check_md5_pil(data2, filename2, generate_golden=GENERATE_GOLDEN)
|
2020-06-25 23:31:35 +08:00
|
|
|
|
|
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
test_pad_op()
|
2020-05-07 09:05:12 +08:00
|
|
|
test_pad_grayscale()
|
2020-06-25 23:31:35 +08:00
|
|
|
test_pad_md5()
|