test_center_crop.py 4.3 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright 2019 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 mindspore.dataset.transforms.vision.c_transforms as vision
E
eric 已提交
16
import mindspore.dataset.transforms.vision.py_transforms as py_vision
Z
zhunaipan 已提交
17 18 19 20
import numpy as np
import matplotlib.pyplot as plt
import mindspore.dataset as ds
from mindspore import log as logger
E
eric 已提交
21 22 23
from util import diff_mse, visualize, save_and_check_md5

GENERATE_GOLDEN = False
Z
zhunaipan 已提交
24 25 26 27 28

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"


E
eric 已提交
29
def test_center_crop_op(height=375, width=375, plot=False):
Z
zhunaipan 已提交
30
    """
E
eric 已提交
31
    Test random_vertical
Z
zhunaipan 已提交
32
    """
E
eric 已提交
33 34 35 36 37 38 39 40 41
    logger.info("Test CenterCrop")

    # First dataset
    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
    decode_op = vision.Decode()
    # 3 images [375, 500] [600, 500] [512, 512]
    center_crop_op = vision.CenterCrop([height, width])
    data1 = data1.map(input_columns=["image"], operations=decode_op)
    data1 = data1.map(input_columns=["image"], operations=center_crop_op)
Z
zhunaipan 已提交
42

E
eric 已提交
43 44 45
    # Second dataset
    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
    data2 = data2.map(input_columns=["image"], operations=decode_op)
Z
zhunaipan 已提交
46

E
eric 已提交
47 48 49 50 51 52 53
    image_cropped = []
    image = []
    for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
        image_cropped.append(item1["image"].copy())
        image.append(item2["image"].copy())
    if plot:
        visualize(image, image_cropped)
Z
zhunaipan 已提交
54 55


E
eric 已提交
56
def test_center_crop_md5(height=375, width=375):
Z
zhunaipan 已提交
57 58 59 60 61 62
    """
    Test random_vertical
    """
    logger.info("Test CenterCrop")

    # First dataset
E
eric 已提交
63
    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle =False)
Z
zhunaipan 已提交
64 65
    decode_op = vision.Decode()
    # 3 images [375, 500] [600, 500] [512, 512]
E
eric 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    center_crop_op = vision.CenterCrop([height, width])
    data1 = data1.map(input_columns=["image"], operations=decode_op)
    data1 = data1.map(input_columns=["image"], operations=center_crop_op)
    # expected md5 from images 

    filename = "test_center_crop_01_result.npz"
    parameters = {"params": {}}
    save_and_check_md5(data1, parameters, filename, generate_golden=GENERATE_GOLDEN)


def test_center_crop_comp(height=375, width=375, plot=False):
    """
    Test random_vertical between python and c image augmentation
    """
    logger.info("Test CenterCrop")

    # First dataset
    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
    decode_op = vision.Decode()
    center_crop_op = vision.CenterCrop([height, width])
Z
zhunaipan 已提交
86 87 88 89
    data1 = data1.map(input_columns=["image"], operations=decode_op)
    data1 = data1.map(input_columns=["image"], operations=center_crop_op)

    # Second dataset
E
eric 已提交
90 91 92 93 94 95 96 97
    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
    transforms = [
        py_vision.Decode(),
        py_vision.CenterCrop([height, width]), 
        py_vision.ToTensor()
    ]
    transform = py_vision.ComposeOp(transforms)
    data2 = data2.map(input_columns=["image"], operations=transform())
Z
zhunaipan 已提交
98 99 100 101

    image_cropped = []
    image = []
    for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
E
eric 已提交
102 103 104 105
        c_image = item1["image"]
        py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
        # the images aren't exactly the same due to rouding error 
        assert (diff_mse(py_image, c_image) < 0.001)
Z
zhunaipan 已提交
106 107 108 109 110 111 112 113 114 115
        image_cropped.append(item1["image"].copy())
        image.append(item2["image"].copy())
    if plot:
        visualize(image, image_cropped)


if __name__ == "__main__":
    test_center_crop_op(600, 600)
    test_center_crop_op(300, 600)
    test_center_crop_op(600, 300)
E
eric 已提交
116 117
    test_center_crop_md5(600, 600)
    test_center_crop_comp()