2022-05-07 03:45:20 +08:00
|
|
|
# Copyright 2020-2022 Huawei Technologies Co., Ltd
|
2020-04-22 07:54:32 +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.
|
|
|
|
|
# ==============================================================================
|
2020-06-10 03:12:07 +08:00
|
|
|
"""
|
|
|
|
|
Testing UniformAugment in DE
|
|
|
|
|
"""
|
2020-05-18 16:42:35 +08:00
|
|
|
import numpy as np
|
2022-06-28 04:27:06 +08:00
|
|
|
import pytest
|
2020-05-18 16:42:35 +08:00
|
|
|
|
2020-12-24 16:33:29 +08:00
|
|
|
import mindspore.dataset as ds
|
2022-05-26 04:30:20 +08:00
|
|
|
import mindspore.dataset.transforms
|
|
|
|
|
import mindspore.dataset.vision as vision
|
2020-05-18 16:42:35 +08:00
|
|
|
from mindspore import log as logger
|
2022-07-01 00:07:47 +08:00
|
|
|
from util import visualize_list, diff_mse, config_get_set_seed
|
2020-04-22 07:54:32 +08:00
|
|
|
|
|
|
|
|
DATA_DIR = "../data/dataset/testImageNetData/train/"
|
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-01-29 22:21:06 +08:00
|
|
|
def test_uniform_augment_callable(num_ops=2):
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: UniformAugment
|
2022-06-28 04:27:06 +08:00
|
|
|
Description: Test UniformAugment under execute mode
|
2022-05-30 21:12:12 +08:00
|
|
|
Expectation: Output's shape is the same as expected output's shape
|
2021-01-29 22:21:06 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("test_uniform_augment_callable")
|
|
|
|
|
img = np.fromfile("../data/dataset/apple.jpg", dtype=np.uint8)
|
|
|
|
|
logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape))
|
|
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
decode_op = vision.Decode()
|
2021-01-29 22:21:06 +08:00
|
|
|
img = decode_op(img)
|
2021-03-03 06:25:50 +08:00
|
|
|
assert img.shape == (2268, 4032, 3)
|
2021-01-29 22:21:06 +08:00
|
|
|
|
2022-06-28 04:27:06 +08:00
|
|
|
transforms_ua = [vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32]),
|
|
|
|
|
vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32])]
|
2022-05-03 02:50:47 +08:00
|
|
|
uni_aug = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
2021-03-03 06:25:50 +08:00
|
|
|
img = uni_aug(img)
|
2022-06-28 04:27:06 +08:00
|
|
|
assert img.shape == (2268, 4032, 3) or img.shape == (200, 400, 3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_uniform_augment_callable_pil(num_ops=2):
|
|
|
|
|
"""
|
|
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment under execute mode, with PIL input.
|
|
|
|
|
Expectation: Output's shape is the same as expected output's shape
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_uniform_augment_callable")
|
|
|
|
|
img = np.fromfile("../data/dataset/apple.jpg", dtype=np.uint8)
|
|
|
|
|
logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape))
|
|
|
|
|
|
|
|
|
|
decode_op = vision.Decode(to_pil=True)
|
|
|
|
|
img = decode_op(img)
|
|
|
|
|
assert img.size == (4032, 2268)
|
|
|
|
|
|
|
|
|
|
transforms_ua = [vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32]),
|
|
|
|
|
vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32])]
|
|
|
|
|
uni_aug = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
|
|
|
|
img = uni_aug(img)
|
|
|
|
|
assert img.size == (4032, 2268) or img.size == (400, 200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_uniform_augment_callable_pil_pyfunc(num_ops=3):
|
|
|
|
|
"""
|
|
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment under execute mode, with PIL input. Include pyfunc in transforms list.
|
|
|
|
|
Expectation: Output's shape is the same as expected output's shape
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_uniform_augment_callable")
|
|
|
|
|
img = np.fromfile("../data/dataset/apple.jpg", dtype=np.uint8)
|
|
|
|
|
logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape))
|
|
|
|
|
|
|
|
|
|
decode_op = vision.Decode(to_pil=True)
|
|
|
|
|
img = decode_op(img)
|
|
|
|
|
assert img.size == (4032, 2268)
|
|
|
|
|
|
|
|
|
|
transforms_ua = [vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32]),
|
|
|
|
|
lambda x: x,
|
|
|
|
|
vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32])]
|
|
|
|
|
uni_aug = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
|
|
|
|
img = uni_aug(img)
|
|
|
|
|
assert img.size == (4032, 2268) or img.size == (400, 200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_uniform_augment_callable_tuple(num_ops=2):
|
|
|
|
|
"""
|
|
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment under execute mode. Use tuple for transforms list argument.
|
|
|
|
|
Expectation: Output's shape is the same as expected output's shape
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_uniform_augment_callable")
|
|
|
|
|
img = np.fromfile("../data/dataset/apple.jpg", dtype=np.uint8)
|
|
|
|
|
logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape))
|
|
|
|
|
|
|
|
|
|
decode_op = vision.Decode()
|
|
|
|
|
img = decode_op(img)
|
|
|
|
|
assert img.shape == (2268, 4032, 3)
|
|
|
|
|
|
|
|
|
|
transforms_ua = (vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32]),
|
|
|
|
|
vision.RandomCrop(size=[200, 400], padding=[32, 32, 32, 32]))
|
|
|
|
|
uni_aug = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
|
|
|
|
img = uni_aug(img)
|
|
|
|
|
assert img.shape == (2268, 4032, 3) or img.shape == (200, 400, 3)
|
2021-01-29 22:21:06 +08:00
|
|
|
|
|
|
|
|
|
2020-04-22 07:54:32 +08:00
|
|
|
def test_uniform_augment(plot=False, num_ops=2):
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment using Python implementation
|
|
|
|
|
Expectation: Output is the same as expected output
|
2020-04-22 07:54:32 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("Test UniformAugment")
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-04-22 07:54:32 +08:00
|
|
|
# Original Images
|
2020-12-24 16:33:29 +08:00
|
|
|
data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False)
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2022-05-26 04:30:20 +08:00
|
|
|
transforms_original = mindspore.dataset.transforms.Compose([vision.Decode(True),
|
|
|
|
|
vision.Resize((224, 224)),
|
|
|
|
|
vision.ToTensor()])
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-12-24 16:33:29 +08:00
|
|
|
ds_original = data_set.map(operations=transforms_original, input_columns="image")
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-04-22 07:54:32 +08:00
|
|
|
ds_original = ds_original.batch(512)
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-05-26 16:17:53 +08:00
|
|
|
for idx, (image, _) in enumerate(ds_original):
|
2020-04-22 07:54:32 +08:00
|
|
|
if idx == 0:
|
2020-09-05 10:56:38 +08:00
|
|
|
images_original = np.transpose(image.asnumpy(), (0, 2, 3, 1))
|
2020-04-22 07:54:32 +08:00
|
|
|
else:
|
|
|
|
|
images_original = np.append(images_original,
|
2020-09-05 10:56:38 +08:00
|
|
|
np.transpose(image.asnumpy(), (0, 2, 3, 1)),
|
2020-05-18 10:31:46 +08:00
|
|
|
axis=0)
|
|
|
|
|
|
2022-06-28 04:27:06 +08:00
|
|
|
# UniformAugment Images
|
2020-12-24 16:33:29 +08:00
|
|
|
data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False)
|
2020-04-22 07:54:32 +08:00
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
transform_list = [vision.RandomRotation(45),
|
|
|
|
|
vision.RandomColor(),
|
|
|
|
|
vision.RandomSharpness(),
|
|
|
|
|
vision.Invert(),
|
|
|
|
|
vision.AutoContrast(),
|
|
|
|
|
vision.Equalize()]
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-08-28 03:30:21 +08:00
|
|
|
transforms_ua = \
|
2022-05-26 04:30:20 +08:00
|
|
|
mindspore.dataset.transforms.Compose([vision.Decode(True),
|
|
|
|
|
vision.Resize((224, 224)),
|
|
|
|
|
vision.UniformAugment(transforms=transform_list,
|
|
|
|
|
num_ops=num_ops),
|
|
|
|
|
vision.ToTensor()])
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-12-24 16:33:29 +08:00
|
|
|
ds_ua = data_set.map(operations=transforms_ua, input_columns="image")
|
2020-05-18 10:31:46 +08:00
|
|
|
|
|
|
|
|
ds_ua = ds_ua.batch(512)
|
|
|
|
|
|
2020-05-26 16:17:53 +08:00
|
|
|
for idx, (image, _) in enumerate(ds_ua):
|
2020-04-22 07:54:32 +08:00
|
|
|
if idx == 0:
|
2020-09-05 10:56:38 +08:00
|
|
|
images_ua = np.transpose(image.asnumpy(), (0, 2, 3, 1))
|
2020-04-22 07:54:32 +08:00
|
|
|
else:
|
|
|
|
|
images_ua = np.append(images_ua,
|
2020-09-05 10:56:38 +08:00
|
|
|
np.transpose(image.asnumpy(), (0, 2, 3, 1)),
|
2020-04-22 07:54:32 +08:00
|
|
|
axis=0)
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-04-22 07:54:32 +08:00
|
|
|
num_samples = images_original.shape[0]
|
|
|
|
|
mse = np.zeros(num_samples)
|
|
|
|
|
for i in range(num_samples):
|
2020-06-25 23:31:35 +08:00
|
|
|
mse[i] = diff_mse(images_ua[i], images_original[i])
|
2020-04-22 07:54:32 +08:00
|
|
|
logger.info("MSE= {}".format(str(np.mean(mse))))
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-04-22 07:54:32 +08:00
|
|
|
if plot:
|
2020-06-10 03:12:07 +08:00
|
|
|
visualize_list(images_original, images_ua)
|
2020-05-18 10:31:46 +08:00
|
|
|
|
|
|
|
|
|
2022-07-01 00:07:47 +08:00
|
|
|
def test_uniform_augment_pyfunc(num_ops=2, my_seed=1):
|
|
|
|
|
"""
|
|
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment using Python implementation. Include pyfunc in transforms list.
|
|
|
|
|
Expectation: Output is the same as expected output
|
|
|
|
|
"""
|
|
|
|
|
logger.info("Test UniformAugment with pyfunc")
|
|
|
|
|
original_seed = config_get_set_seed(my_seed)
|
|
|
|
|
logger.info("my_seed= {}".format(str(my_seed)))
|
|
|
|
|
|
|
|
|
|
# Original Images
|
|
|
|
|
data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False)
|
|
|
|
|
|
|
|
|
|
transforms_original = mindspore.dataset.transforms.Compose([vision.Decode(True)])
|
|
|
|
|
|
|
|
|
|
ds_original = data_set.map(operations=transforms_original, input_columns="image")
|
|
|
|
|
|
|
|
|
|
ds_original = ds_original.batch(512)
|
|
|
|
|
|
|
|
|
|
for idx, (image, _) in enumerate(ds_original):
|
|
|
|
|
if idx == 0:
|
|
|
|
|
images_original = np.transpose(image.asnumpy(), (0, 2, 3, 1))
|
|
|
|
|
else:
|
|
|
|
|
images_original = np.append(images_original,
|
|
|
|
|
np.transpose(image.asnumpy(), (0, 2, 3, 1)),
|
|
|
|
|
axis=0)
|
|
|
|
|
|
|
|
|
|
# UniformAugment Images
|
|
|
|
|
data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False)
|
|
|
|
|
|
|
|
|
|
transform_list = [vision.Invert(),
|
|
|
|
|
lambda x: x,
|
|
|
|
|
vision.AutoContrast(),
|
|
|
|
|
vision.Equalize()
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
transforms_ua = \
|
|
|
|
|
mindspore.dataset.transforms.Compose([vision.Decode(True),
|
|
|
|
|
vision.UniformAugment(transforms=transform_list, num_ops=num_ops)])
|
|
|
|
|
|
|
|
|
|
ds_ua = data_set.map(operations=transforms_ua, input_columns="image")
|
|
|
|
|
|
|
|
|
|
ds_ua = ds_ua.batch(512)
|
|
|
|
|
|
|
|
|
|
for idx, (image, _) in enumerate(ds_ua):
|
|
|
|
|
if idx == 0:
|
|
|
|
|
images_ua = np.transpose(image.asnumpy(), (0, 2, 3, 1))
|
|
|
|
|
else:
|
|
|
|
|
images_ua = np.append(images_ua,
|
|
|
|
|
np.transpose(image.asnumpy(), (0, 2, 3, 1)),
|
|
|
|
|
axis=0)
|
|
|
|
|
|
|
|
|
|
num_samples = images_original.shape[0]
|
|
|
|
|
mse = np.zeros(num_samples)
|
|
|
|
|
for i in range(num_samples):
|
|
|
|
|
mse[i] = diff_mse(images_ua[i], images_original[i])
|
|
|
|
|
logger.info("MSE= {}".format(str(np.mean(mse))))
|
|
|
|
|
|
|
|
|
|
# Restore configuration
|
|
|
|
|
ds.config.set_seed(original_seed)
|
|
|
|
|
|
|
|
|
|
|
2020-04-25 03:21:43 +08:00
|
|
|
def test_cpp_uniform_augment(plot=False, num_ops=2):
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment using Cpp implementation
|
|
|
|
|
Expectation: Output is the same as expected output
|
2020-04-25 03:21:43 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("Test CPP UniformAugment")
|
|
|
|
|
|
|
|
|
|
# Original Images
|
2020-12-24 16:33:29 +08:00
|
|
|
data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False)
|
2020-04-25 03:21:43 +08:00
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
transforms_original = [vision.Decode(), vision.Resize(size=[224, 224]),
|
|
|
|
|
vision.ToTensor()]
|
2020-04-25 03:21:43 +08:00
|
|
|
|
2020-12-24 16:33:29 +08:00
|
|
|
ds_original = data_set.map(operations=transforms_original, input_columns="image")
|
2020-04-25 03:21:43 +08:00
|
|
|
|
|
|
|
|
ds_original = ds_original.batch(512)
|
|
|
|
|
|
2020-05-26 16:17:53 +08:00
|
|
|
for idx, (image, _) in enumerate(ds_original):
|
2020-04-25 03:21:43 +08:00
|
|
|
if idx == 0:
|
2020-09-05 10:56:38 +08:00
|
|
|
images_original = np.transpose(image.asnumpy(), (0, 2, 3, 1))
|
2020-04-25 03:21:43 +08:00
|
|
|
else:
|
|
|
|
|
images_original = np.append(images_original,
|
2020-09-05 10:56:38 +08:00
|
|
|
np.transpose(image.asnumpy(), (0, 2, 3, 1)),
|
2020-04-25 03:21:43 +08:00
|
|
|
axis=0)
|
|
|
|
|
|
|
|
|
|
# UniformAugment Images
|
2020-12-24 16:33:29 +08:00
|
|
|
data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False)
|
2022-05-03 02:50:47 +08:00
|
|
|
transforms_ua = [vision.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]),
|
|
|
|
|
vision.RandomHorizontalFlip(),
|
|
|
|
|
vision.RandomVerticalFlip(),
|
|
|
|
|
vision.RandomColorAdjust(),
|
|
|
|
|
vision.RandomRotation(degrees=45)]
|
2020-04-25 03:21:43 +08:00
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
uni_aug = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
2020-04-25 03:21:43 +08:00
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
transforms_all = [vision.Decode(), vision.Resize(size=[224, 224]),
|
2020-04-25 03:21:43 +08:00
|
|
|
uni_aug,
|
2022-05-03 02:50:47 +08:00
|
|
|
vision.ToTensor()]
|
2020-04-25 03:21:43 +08:00
|
|
|
|
2020-12-24 16:33:29 +08:00
|
|
|
ds_ua = data_set.map(operations=transforms_all, input_columns="image", num_parallel_workers=1)
|
2020-04-25 03:21:43 +08:00
|
|
|
|
|
|
|
|
ds_ua = ds_ua.batch(512)
|
|
|
|
|
|
2020-05-26 16:17:53 +08:00
|
|
|
for idx, (image, _) in enumerate(ds_ua):
|
2020-04-25 03:21:43 +08:00
|
|
|
if idx == 0:
|
2020-09-05 10:56:38 +08:00
|
|
|
images_ua = np.transpose(image.asnumpy(), (0, 2, 3, 1))
|
2020-04-25 03:21:43 +08:00
|
|
|
else:
|
|
|
|
|
images_ua = np.append(images_ua,
|
2020-09-05 10:56:38 +08:00
|
|
|
np.transpose(image.asnumpy(), (0, 2, 3, 1)),
|
2020-04-25 03:21:43 +08:00
|
|
|
axis=0)
|
|
|
|
|
if plot:
|
2020-06-10 03:12:07 +08:00
|
|
|
visualize_list(images_original, images_ua)
|
2020-04-25 03:21:43 +08:00
|
|
|
|
|
|
|
|
num_samples = images_original.shape[0]
|
|
|
|
|
mse = np.zeros(num_samples)
|
|
|
|
|
for i in range(num_samples):
|
2020-06-25 23:31:35 +08:00
|
|
|
mse[i] = diff_mse(images_ua[i], images_original[i])
|
2020-04-25 03:21:43 +08:00
|
|
|
logger.info("MSE= {}".format(str(np.mean(mse))))
|
2020-04-22 07:54:32 +08:00
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-04-30 03:42:36 +08:00
|
|
|
def test_cpp_uniform_augment_exception_large_numops(num_ops=6):
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment using invalid large number of ops
|
|
|
|
|
Expectation: Exception is raised as expected
|
2020-04-30 03:42:36 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("Test CPP UniformAugment invalid large num_ops exception")
|
|
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
transforms_ua = [vision.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]),
|
|
|
|
|
vision.RandomHorizontalFlip(),
|
|
|
|
|
vision.RandomVerticalFlip(),
|
|
|
|
|
vision.RandomColorAdjust(),
|
|
|
|
|
vision.RandomRotation(degrees=45)]
|
2020-04-30 03:42:36 +08:00
|
|
|
|
2022-06-28 04:27:06 +08:00
|
|
|
with pytest.raises(ValueError) as error_info:
|
2022-05-03 02:50:47 +08:00
|
|
|
_ = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
2022-06-28 04:27:06 +08:00
|
|
|
logger.info("Got an exception in DE: {}".format(str(error_info)))
|
|
|
|
|
assert "num_ops is greater than transforms list size" in str(error_info)
|
2020-04-30 03:42:36 +08:00
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-04-30 03:42:36 +08:00
|
|
|
def test_cpp_uniform_augment_exception_nonpositive_numops(num_ops=0):
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment using invalid non-positive num_ops
|
|
|
|
|
Expectation: Exception is raised as expected
|
2020-04-30 03:42:36 +08:00
|
|
|
"""
|
2022-06-28 04:27:06 +08:00
|
|
|
logger.info("Test UniformAugment invalid non-positive num_ops exception")
|
2020-04-30 03:42:36 +08:00
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
transforms_ua = [vision.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]),
|
|
|
|
|
vision.RandomHorizontalFlip(),
|
|
|
|
|
vision.RandomVerticalFlip(),
|
|
|
|
|
vision.RandomColorAdjust(),
|
|
|
|
|
vision.RandomRotation(degrees=45)]
|
2020-04-30 03:42:36 +08:00
|
|
|
|
2022-06-28 04:27:06 +08:00
|
|
|
with pytest.raises(ValueError) as error_info:
|
2022-05-03 02:50:47 +08:00
|
|
|
_ = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
2022-06-28 04:27:06 +08:00
|
|
|
logger.info("Got an exception in DE: {}".format(str(error_info)))
|
|
|
|
|
assert "Input num_ops must be greater than 0" in str(error_info)
|
2020-04-30 03:42:36 +08:00
|
|
|
|
2020-06-10 03:12:07 +08:00
|
|
|
|
2020-05-20 05:57:17 +08:00
|
|
|
def test_cpp_uniform_augment_exception_float_numops(num_ops=2.5):
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment using invalid float num_ops
|
|
|
|
|
Expectation: Exception is raised as expected
|
2020-05-20 05:57:17 +08:00
|
|
|
"""
|
2022-06-28 04:27:06 +08:00
|
|
|
logger.info("Test UniformAugment invalid float num_ops exception")
|
2020-05-20 05:57:17 +08:00
|
|
|
|
2022-05-03 02:50:47 +08:00
|
|
|
transforms_ua = [vision.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]),
|
|
|
|
|
vision.RandomHorizontalFlip(),
|
|
|
|
|
vision.RandomVerticalFlip(),
|
|
|
|
|
vision.RandomColorAdjust(),
|
|
|
|
|
vision.RandomRotation(degrees=45)]
|
2020-05-20 05:57:17 +08:00
|
|
|
|
2022-06-28 04:27:06 +08:00
|
|
|
with pytest.raises(TypeError) as error_info:
|
2022-05-03 02:50:47 +08:00
|
|
|
_ = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
2022-06-28 04:27:06 +08:00
|
|
|
logger.info("Got an exception in DE: {}".format(str(error_info)))
|
|
|
|
|
assert "Argument num_ops with value 2.5 is not of type [<class 'int'>]" in str(error_info)
|
2020-05-20 05:57:17 +08:00
|
|
|
|
2020-06-10 03:12:07 +08:00
|
|
|
|
2020-05-20 05:57:17 +08:00
|
|
|
def test_cpp_uniform_augment_random_crop_badinput(num_ops=1):
|
|
|
|
|
"""
|
2022-05-30 21:12:12 +08:00
|
|
|
Feature: UniformAugment
|
|
|
|
|
Description: Test UniformAugment with greater crop size
|
|
|
|
|
Expectation: Exception is raised as expected
|
2020-05-20 05:57:17 +08:00
|
|
|
"""
|
2022-06-28 04:27:06 +08:00
|
|
|
logger.info("Test UniformAugment with random_crop bad input")
|
2020-05-26 16:17:53 +08:00
|
|
|
batch_size = 2
|
2020-05-20 05:57:17 +08:00
|
|
|
cifar10_dir = "../data/dataset/testCifar10Data"
|
2020-12-24 16:33:29 +08:00
|
|
|
ds1 = ds.Cifar10Dataset(cifar10_dir, shuffle=False) # shape = [32,32,3]
|
2020-05-20 05:57:17 +08:00
|
|
|
|
|
|
|
|
transforms_ua = [
|
2020-05-20 05:57:17 +08:00
|
|
|
# Note: crop size [224, 224] > image size [32, 32]
|
2022-05-03 02:50:47 +08:00
|
|
|
vision.RandomCrop(size=[224, 224]),
|
|
|
|
|
vision.RandomHorizontalFlip()
|
2020-05-20 05:57:17 +08:00
|
|
|
]
|
2022-05-03 02:50:47 +08:00
|
|
|
uni_aug = vision.UniformAugment(transforms=transforms_ua, num_ops=num_ops)
|
2020-09-10 01:23:02 +08:00
|
|
|
ds1 = ds1.map(operations=uni_aug, input_columns="image")
|
2020-05-20 05:57:17 +08:00
|
|
|
|
|
|
|
|
# apply DatasetOps
|
|
|
|
|
ds1 = ds1.batch(batch_size, drop_remainder=True, num_parallel_workers=1)
|
|
|
|
|
num_batches = 0
|
2022-06-28 04:27:06 +08:00
|
|
|
with pytest.raises(RuntimeError) as error_info:
|
2020-09-05 10:56:38 +08:00
|
|
|
for _ in ds1.create_dict_iterator(num_epochs=1, output_numpy=True):
|
2020-05-20 05:57:17 +08:00
|
|
|
num_batches += 1
|
2022-10-20 20:51:50 +08:00
|
|
|
assert "map operation: [UniformAugment] failed." in str(error_info.value)
|
2020-05-20 05:57:17 +08:00
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2020-04-22 07:54:32 +08:00
|
|
|
if __name__ == "__main__":
|
2022-06-28 04:27:06 +08:00
|
|
|
test_uniform_augment_callable()
|
|
|
|
|
test_uniform_augment_callable_pil()
|
|
|
|
|
test_uniform_augment_callable_pil_pyfunc()
|
|
|
|
|
test_uniform_augment_callable_tuple()
|
|
|
|
|
test_uniform_augment(num_ops=6, plot=True)
|
2022-07-01 00:07:47 +08:00
|
|
|
test_uniform_augment_pyfunc(num_ops=2, my_seed=1)
|
2020-06-10 03:12:07 +08:00
|
|
|
test_cpp_uniform_augment(num_ops=1, plot=True)
|
2020-04-30 03:42:36 +08:00
|
|
|
test_cpp_uniform_augment_exception_large_numops(num_ops=6)
|
|
|
|
|
test_cpp_uniform_augment_exception_nonpositive_numops(num_ops=0)
|
2020-05-20 05:57:17 +08:00
|
|
|
test_cpp_uniform_augment_exception_float_numops(num_ops=2.5)
|
|
|
|
|
test_cpp_uniform_augment_random_crop_badinput(num_ops=1)
|