From c8b5586c7fce37a3d9f5717037610da96fe1b894 Mon Sep 17 00:00:00 2001 From: Tinazhang Date: Thu, 7 May 2020 17:23:12 -0400 Subject: [PATCH] add unit test for HWC2CHWC --- .../dataset/golden/test_HWC2CHW_01_result.npz | Bin 0 -> 644 bytes tests/ut/python/dataset/test_HWC2CHW.py | 121 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 tests/ut/data/dataset/golden/test_HWC2CHW_01_result.npz create mode 100644 tests/ut/python/dataset/test_HWC2CHW.py diff --git a/tests/ut/data/dataset/golden/test_HWC2CHW_01_result.npz b/tests/ut/data/dataset/golden/test_HWC2CHW_01_result.npz new file mode 100644 index 0000000000000000000000000000000000000000..4ae74f44932fd61084f38aa544a5e6b5ff027c96 GIT binary patch literal 644 zcmWIWW@Zs#fB;2?6J=LL|1dHzfG{V62t#5~QM`d(UO^=zg8*0%q!1(t0+anheFGvH z8Oj){)l*W7lZ(`?6x3_{)pZoq)AEZ-iW2kU^NUhaLBei{ImM|!@#2icf>a=1!%#=T zNK;3lR)KuL)xeybSDIT;sh6Bzl&Y6onp2VqbZ=rMSA0=wa(-TMNl|HX30ENlL={(F z3PiS$(VL;Qkja@bsgSw7kR_;)Rl}PR2$|Xn*`P{ON-7IdxeD1Mn1J5&wgkx7IZ gSHc1M9|{`4X$78)0=!w-Kr)O#Xbz;!K>^GF0CaY&2LJ#7 literal 0 HcmV?d00001 diff --git a/tests/ut/python/dataset/test_HWC2CHW.py b/tests/ut/python/dataset/test_HWC2CHW.py new file mode 100644 index 0000000000..6aae3184dd --- /dev/null +++ b/tests/ut/python/dataset/test_HWC2CHW.py @@ -0,0 +1,121 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================== +import numpy as np +import mindspore.dataset.transforms.vision.c_transforms as c_vision +import mindspore.dataset.transforms.vision.py_transforms as py_vision +import mindspore.dataset as ds +from mindspore import log as logger +from util import diff_mse, visualize, save_and_check_md5 + +GENERATE_GOLDEN = False + +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" + + +def test_HWC2CHW(plot=False): + """ + Test HWC2CHW + """ + logger.info("Test HWC2CHW") + + # First dataset + data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + decode_op = c_vision.Decode() + hwc2chw_op = c_vision.HWC2CHW() + data1 = data1.map(input_columns=["image"], operations=decode_op) + data1 = data1.map(input_columns=["image"], operations=hwc2chw_op) + + # Second dataset + data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + data2 = data2.map(input_columns=["image"], operations=decode_op) + + image_transposed = [] + image = [] + for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): + image_transposed.append(item1["image"].copy()) + image.append(item2["image"].copy()) + + # check if the shape of data is transposed correctly + # transpose the original image from shape (H,W,C) to (C,H,W) + mse = diff_mse(item1['image'], item2['image'].transpose(2, 0, 1)) + assert mse == 0 + if plot: + visualize(image, image_transposed) + + +def test_HWC2CHW_md5(): + """ + Test HWC2CHW(md5) + """ + logger.info("Test HWC2CHW with md5 comparison") + + # First dataset + data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + decode_op = c_vision.Decode() + hwc2chw_op = c_vision.HWC2CHW() + data1 = data1.map(input_columns=["image"], operations=decode_op) + data1 = data1.map(input_columns=["image"], operations=hwc2chw_op) + + # expected md5 from images + filename = "test_HWC2CHW_01_result.npz" + save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN) + + +def test_HWC2CHW_comp(plot=False): + """ + Test HWC2CHW between python and c image augmentation + """ + logger.info("Test HWC2CHW with c_transform and py_transform comparison") + + # First dataset + data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + decode_op = c_vision.Decode() + hwc2chw_op = c_vision.HWC2CHW() + data1 = data1.map(input_columns=["image"], operations=decode_op) + data1 = data1.map(input_columns=["image"], operations=hwc2chw_op) + + # Second dataset + data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + transforms = [ + py_vision.Decode(), + py_vision.ToTensor(), + py_vision.HWC2CHW() + ] + transform = py_vision.ComposeOp(transforms) + data2 = data2.map(input_columns=["image"], operations=transform()) + + image_c_transposed = [] + image_py_transposed = [] + for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): + c_image = item1["image"] + py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) + + # compare images between that applying c_transform and py_transform + mse = diff_mse(py_image, c_image) + # the images aren't exactly the same due to rounding error + assert mse < 0.001 + + image_c_transposed.append(item1["image"].copy()) + image_py_transposed.append(item2["image"].copy()) + + if plot: + visualize(image_c_transposed, image_py_transposed) + + +if __name__ == '__main__': + test_HWC2CHW() + test_HWC2CHW_md5() + test_HWC2CHW_comp()