test_random_erasing.py 3.7 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.
# ==============================================================================
"""
16
Testing RandomErasing op in DE
Z
zhunaipan 已提交
17 18 19 20
"""
import numpy as np

import mindspore.dataset as ds
N
nhussain 已提交
21 22
import mindspore.dataset.transforms.py_transforms
import mindspore.dataset.vision.py_transforms as vision
Z
zhunaipan 已提交
23
from mindspore import log as logger
24 25
from util import diff_mse, visualize_image, save_and_check_md5, \
    config_get_set_seed, config_get_set_num_parallel_workers
Z
zhunaipan 已提交
26 27 28 29

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"

30
GENERATE_GOLDEN = False
Z
zhunaipan 已提交
31

T
Tinazhang 已提交
32
def test_random_erasing_op(plot=False):
Z
zhunaipan 已提交
33 34 35 36 37 38 39 40 41 42 43 44
    """
    Test RandomErasing and Cutout
    """
    logger.info("test_random_erasing")

    # First dataset
    data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
    transforms_1 = [
        vision.Decode(),
        vision.ToTensor(),
        vision.RandomErasing(value='random')
    ]
N
nhussain 已提交
45 46
    transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
    data1 = data1.map(input_columns=["image"], operations=transform_1)
Z
zhunaipan 已提交
47 48 49 50 51 52 53 54

    # Second dataset
    data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
    transforms_2 = [
        vision.Decode(),
        vision.ToTensor(),
        vision.Cutout(80)
    ]
N
nhussain 已提交
55 56
    transform_2 = mindspore.dataset.transforms.py_transforms.Compose(transforms_2)
    data2 = data2.map(input_columns=["image"], operations=transform_2)
Z
zhunaipan 已提交
57 58

    num_iter = 0
59
    for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1), data2.create_dict_iterator(num_epochs=1)):
Z
zhunaipan 已提交
60 61 62 63 64 65 66 67 68 69
        num_iter += 1
        image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
        image_2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)

        logger.info("shape of image_1: {}".format(image_1.shape))
        logger.info("shape of image_2: {}".format(image_2.shape))

        logger.info("dtype of image_1: {}".format(image_1.dtype))
        logger.info("dtype of image_2: {}".format(image_2.dtype))

T
Tinazhang 已提交
70 71 72
        mse = diff_mse(image_1, image_2)
        if plot:
            visualize_image(image_1, image_2, mse)
Z
zhunaipan 已提交
73 74


75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
def test_random_erasing_md5():
    """
    Test RandomErasing with md5 check
    """
    logger.info("Test RandomErasing with md5 check")
    original_seed = config_get_set_seed(5)
    original_num_parallel_workers = config_get_set_num_parallel_workers(1)

    # Generate dataset
    data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
    transforms_1 = [
        vision.Decode(),
        vision.ToTensor(),
        vision.RandomErasing(value='random')
    ]
N
nhussain 已提交
90 91
    transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
    data = data.map(input_columns=["image"], operations=transform_1)
92 93 94 95 96 97 98 99 100
    # Compare with expected md5 from images
    filename = "random_erasing_01_result.npz"
    save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)

    # Restore configuration
    ds.config.set_seed(original_seed)
    ds.config.set_num_parallel_workers((original_num_parallel_workers))


Z
zhunaipan 已提交
101
if __name__ == "__main__":
T
Tinazhang 已提交
102
    test_random_erasing_op(plot=True)
103
    test_random_erasing_md5()