forked from mindspore-Ecosystem/mindspore
Added TCs to RandomCrop and RandomCropAndResize and modified visalize() calling
This commit is contained in:
parent
6cbde2b3bb
commit
17cecf2cf5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -13,12 +13,11 @@
|
|||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
import numpy as np
|
||||
from util import diff_mse, visualize, save_and_check_md5
|
||||
|
||||
import mindspore.dataset as ds
|
||||
import mindspore.dataset.transforms.vision.c_transforms as c_vision
|
||||
import mindspore.dataset.transforms.vision.py_transforms as py_vision
|
||||
from mindspore import log as logger
|
||||
from util import diff_mse, visualize, save_and_check_md5
|
||||
|
||||
GENERATE_GOLDEN = False
|
||||
|
||||
|
@ -46,12 +45,14 @@ def test_HWC2CHW(plot=False):
|
|||
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())
|
||||
transposed_item = item1["image"].copy()
|
||||
original_item = item2["image"].copy()
|
||||
image_transposed.append(transposed_item.transpose(1, 2, 0))
|
||||
image.append(original_item)
|
||||
|
||||
# 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))
|
||||
mse = diff_mse(transposed_item, original_item.transpose(2, 0, 1))
|
||||
assert mse == 0
|
||||
if plot:
|
||||
visualize(image, image_transposed)
|
||||
|
@ -108,15 +109,13 @@ def test_HWC2CHW_comp(plot=False):
|
|||
mse = diff_mse(py_image, c_image)
|
||||
# Note: 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())
|
||||
|
||||
image_c_transposed.append(c_image.transpose(1, 2, 0))
|
||||
image_py_transposed.append(py_image.transpose(1, 2, 0))
|
||||
if plot:
|
||||
visualize(image_c_transposed, image_py_transposed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_HWC2CHW()
|
||||
test_HWC2CHW(True)
|
||||
test_HWC2CHW_md5()
|
||||
test_HWC2CHW_comp()
|
||||
test_HWC2CHW_comp(True)
|
||||
|
|
|
@ -13,12 +13,11 @@
|
|||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
import numpy as np
|
||||
from util import diff_mse, visualize, save_and_check_md5
|
||||
|
||||
import mindspore.dataset as ds
|
||||
import mindspore.dataset.transforms.vision.c_transforms as vision
|
||||
import mindspore.dataset.transforms.vision.py_transforms as py_vision
|
||||
from mindspore import log as logger
|
||||
from util import diff_mse, visualize, save_and_check_md5
|
||||
|
||||
GENERATE_GOLDEN = False
|
||||
|
||||
|
@ -101,8 +100,8 @@ def test_center_crop_comp(height=375, width=375, plot=False):
|
|||
py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
|
||||
# Note: The images aren't exactly the same due to rounding error
|
||||
assert diff_mse(py_image, c_image) < 0.001
|
||||
image_cropped.append(item1["image"].copy())
|
||||
image.append(item2["image"].copy())
|
||||
image_cropped.append(c_image.copy())
|
||||
image.append(py_image.copy())
|
||||
if plot:
|
||||
visualize(image, image_cropped)
|
||||
|
||||
|
@ -141,9 +140,9 @@ def test_crop_grayscale(height=375, width=375):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_center_crop_op(600, 600)
|
||||
test_center_crop_op(600, 600, True)
|
||||
test_center_crop_op(300, 600)
|
||||
test_center_crop_op(600, 300)
|
||||
test_center_crop_md5()
|
||||
test_center_crop_comp()
|
||||
test_center_crop_comp(True)
|
||||
test_crop_grayscale()
|
||||
|
|
|
@ -13,37 +13,24 @@
|
|||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""
|
||||
Testing RandomCropAndResize op in DE
|
||||
Testing RandomCrop op in DE
|
||||
"""
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
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.transforms.vision.utils as mode
|
||||
import mindspore.dataset as ds
|
||||
import mindspore.dataset.transforms.vision.c_transforms as vision
|
||||
from mindspore import log as logger
|
||||
from util import save_and_check_md5, visualize
|
||||
|
||||
|
||||
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 visualize(a, mse, original):
|
||||
"""
|
||||
visualizes the image using DE op and Numpy op
|
||||
"""
|
||||
plt.subplot(141)
|
||||
plt.imshow(original)
|
||||
plt.title("Original image")
|
||||
|
||||
plt.subplot(142)
|
||||
plt.imshow(a)
|
||||
plt.title("DE random_crop image")
|
||||
|
||||
plt.subplot(143)
|
||||
plt.imshow(a - original)
|
||||
plt.title("Difference image, mse : {}".format(mse))
|
||||
plt.show()
|
||||
|
||||
|
||||
def test_random_crop_op():
|
||||
def test_random_crop_op(plot=False):
|
||||
"""
|
||||
Test RandomCrop Op
|
||||
"""
|
||||
|
@ -51,8 +38,9 @@ def test_random_crop_op():
|
|||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
random_crop_op = vision.RandomCrop([512, 512], [200, 200, 200, 200])
|
||||
decode_op = vision.Decode()
|
||||
random_crop_op = c_vision.RandomCrop([512, 512], [200, 200, 200, 200])
|
||||
decode_op = c_vision.Decode()
|
||||
|
||||
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||
data1 = data1.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
|
@ -60,10 +48,454 @@ def test_random_crop_op():
|
|||
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
data2 = data2.map(input_columns=["image"], operations=decode_op)
|
||||
|
||||
image_cropped = []
|
||||
image = []
|
||||
for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
|
||||
image1 = item1["image"]
|
||||
image2 = item2["image"]
|
||||
image_cropped.append(image1)
|
||||
image.append(image2)
|
||||
if plot:
|
||||
visualize(image, image_cropped)
|
||||
|
||||
def test_random_crop_01_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms: size is a single integer, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_01_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: If size is an int, a square crop of size (size, size) is returned.
|
||||
random_crop_op = c_vision.RandomCrop(512)
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
filename = "random_crop_01_c_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_01_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms: size is a single integer, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_01_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: If size is an int, a square crop of size (size, size) is returned.
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop(512),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename = "random_crop_01_py_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_02_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms: size is a list/tuple with length 2, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_02_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: If size is a sequence of length 2, it should be (height, width).
|
||||
random_crop_op = c_vision.RandomCrop([512, 375])
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
filename = "random_crop_02_c_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_02_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms: size is a list/tuple with length 2, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_02_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: If size is a sequence of length 2, it should be (height, width).
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop([512, 375]),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename = "random_crop_02_py_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_03_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms: input image size == crop size, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_03_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The size of the image is 4032*2268
|
||||
random_crop_op = c_vision.RandomCrop([2268, 4032])
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
filename = "random_crop_03_c_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_03_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms: input image size == crop size, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_03_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The size of the image is 4032*2268
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop([2268, 4032]),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename = "random_crop_03_py_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_04_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms: input image size < crop size, expected to fail
|
||||
"""
|
||||
logger.info("test_random_crop_04_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The size of the image is 4032*2268
|
||||
random_crop_op = c_vision.RandomCrop([2268, 4033])
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = item["image"]
|
||||
image_list.append(image.shape)
|
||||
except BaseException as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
|
||||
def test_random_crop_04_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms:
|
||||
input image size < crop size, expected to fail
|
||||
"""
|
||||
logger.info("test_random_crop_04_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The size of the image is 4032*2268
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop([2268, 4033]),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = (item["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
|
||||
image_list.append(image.shape)
|
||||
except BaseException as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
|
||||
def test_random_crop_05_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms:
|
||||
input image size < crop size but pad_if_needed is enabled,
|
||||
expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_05_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The size of the image is 4032*2268
|
||||
random_crop_op = c_vision.RandomCrop([2268, 4033], [200, 200, 200, 200], pad_if_needed=True)
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
filename = "random_crop_05_c_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_05_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms:
|
||||
input image size < crop size but pad_if_needed is enabled,
|
||||
expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_05_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The size of the image is 4032*2268
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop([2268, 4033], [200, 200, 200, 200], pad_if_needed=True),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename = "random_crop_05_py_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_06_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms:
|
||||
invalid size, expected to raise TypeError
|
||||
"""
|
||||
logger.info("test_random_crop_06_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: if size is neither an int nor a list of length 2, an exception will raise
|
||||
random_crop_op = c_vision.RandomCrop([512, 512, 375])
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = item["image"]
|
||||
image_list.append(image.shape)
|
||||
except TypeError as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
assert "Size" in str(e)
|
||||
|
||||
def test_random_crop_06_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms:
|
||||
invalid size, expected to raise TypeError
|
||||
"""
|
||||
logger.info("test_random_crop_06_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: if size is neither an int nor a list of length 2, an exception will raise
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop([512, 512, 375]),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = (item["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
|
||||
image_list.append(image.shape)
|
||||
except TypeError as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
assert "Size" in str(e)
|
||||
|
||||
def test_random_crop_07_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms:
|
||||
padding_mode is Border.CONSTANT and fill_value is 255 (White),
|
||||
expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_07_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The padding_mode is default as Border.CONSTANT and set filling color to be white.
|
||||
random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], fill_value=(255, 255, 255))
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
filename = "random_crop_07_c_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_07_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms:
|
||||
padding_mode is Border.CONSTANT and fill_value is 255 (White),
|
||||
expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_07_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The padding_mode is default as Border.CONSTANT and set filling color to be white.
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop(512, [200, 200, 200, 200], fill_value=(255, 255, 255)),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename = "random_crop_07_py_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_08_c():
|
||||
"""
|
||||
Test RandomCrop op with c_transforms: padding_mode is Border.EDGE,
|
||||
expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_08_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The padding_mode is Border.EDGE.
|
||||
random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=mode.Border.EDGE)
|
||||
decode_op = c_vision.Decode()
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
filename = "random_crop_08_c_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_08_py():
|
||||
"""
|
||||
Test RandomCrop op with py_transforms: padding_mode is Border.EDGE,
|
||||
expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_08_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
# Note: The padding_mode is Border.EDGE.
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=mode.Border.EDGE),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename = "random_crop_08_py_result.npz"
|
||||
save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_09():
|
||||
"""
|
||||
Test RandomCrop op: invalid type of input image (not PIL), expected to raise TypeError
|
||||
"""
|
||||
logger.info("test_random_crop_09")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.ToTensor(),
|
||||
# Note: if input is not PIL image, TypeError will raise
|
||||
py_vision.RandomCrop(512)
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
try:
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = item["image"]
|
||||
image_list.append(image.shape)
|
||||
except BaseException as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
assert "should be PIL Image" in str(e)
|
||||
|
||||
def test_random_crop_comp(plot=False):
|
||||
"""
|
||||
Test RandomCrop and compare between python and c image augmentation
|
||||
"""
|
||||
logger.info("Test RandomCrop with c_transform and py_transform comparison")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
cropped_size = 512
|
||||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
random_crop_op = c_vision.RandomCrop(cropped_size)
|
||||
decode_op = c_vision.Decode()
|
||||
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||
data1 = data1.map(input_columns=["image"], operations=random_crop_op)
|
||||
|
||||
# Second dataset
|
||||
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomCrop(cropped_size),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data2 = data2.map(input_columns=["image"], operations=transform())
|
||||
|
||||
image_c_cropped = []
|
||||
image_py_cropped = []
|
||||
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)
|
||||
image_c_cropped.append(c_image)
|
||||
image_py_cropped.append(py_image)
|
||||
if plot:
|
||||
visualize(image_c_cropped, image_py_cropped)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_random_crop_op()
|
||||
test_random_crop_01_c()
|
||||
test_random_crop_02_c()
|
||||
test_random_crop_03_c()
|
||||
test_random_crop_04_c()
|
||||
test_random_crop_05_c()
|
||||
test_random_crop_06_c()
|
||||
test_random_crop_07_c()
|
||||
test_random_crop_08_c()
|
||||
test_random_crop_01_py()
|
||||
test_random_crop_02_py()
|
||||
test_random_crop_03_py()
|
||||
test_random_crop_04_py()
|
||||
test_random_crop_05_py()
|
||||
test_random_crop_06_py()
|
||||
test_random_crop_07_py()
|
||||
test_random_crop_08_py()
|
||||
test_random_crop_09()
|
||||
test_random_crop_op(True)
|
||||
test_random_crop_comp(True)
|
||||
|
|
|
@ -16,36 +16,21 @@
|
|||
Testing RandomCropAndResize op in DE
|
||||
"""
|
||||
import cv2
|
||||
import matplotlib.pyplot as plt
|
||||
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.transforms.vision.utils as mode
|
||||
import mindspore.dataset as ds
|
||||
import mindspore.dataset.transforms.vision.c_transforms as vision
|
||||
from mindspore import log as logger
|
||||
from util import diff_mse, save_and_check_md5, visualize
|
||||
|
||||
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 visualize(a, mse, original):
|
||||
"""
|
||||
visualizes the image using DE op and Numpy op
|
||||
"""
|
||||
plt.subplot(141)
|
||||
plt.imshow(original)
|
||||
plt.title("Original image")
|
||||
|
||||
plt.subplot(142)
|
||||
plt.imshow(a)
|
||||
plt.title("DE random_crop_and_resize image")
|
||||
|
||||
plt.subplot(143)
|
||||
plt.imshow(a - original)
|
||||
plt.title("Difference image, mse : {}".format(mse))
|
||||
plt.show()
|
||||
GENERATE_GOLDEN = False
|
||||
|
||||
|
||||
def test_random_crop_and_resize_op():
|
||||
def test_random_crop_and_resize_op(plot=False):
|
||||
"""
|
||||
Test RandomCropAndResize op
|
||||
"""
|
||||
|
@ -53,30 +38,268 @@ def test_random_crop_and_resize_op():
|
|||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = vision.Decode()
|
||||
random_crop_and_resize_op = vision.RandomResizedCrop((256, 512), (1, 1), (0.5, 0.5))
|
||||
decode_op = c_vision.Decode()
|
||||
random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), (1, 1), (0.5, 0.5))
|
||||
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||
data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op)
|
||||
|
||||
# Second dataset
|
||||
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
data2 = data2.map(input_columns=["image"], operations=decode_op)
|
||||
|
||||
num_iter = 0
|
||||
crop_and_resize_images = []
|
||||
original_images = []
|
||||
for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
|
||||
|
||||
if num_iter > 0:
|
||||
break
|
||||
crop_and_resize = item1["image"]
|
||||
original = item2["image"]
|
||||
# Note: resize the original image with the same size as the one applied RandomResizedCrop()
|
||||
original = cv2.resize(original, (512, 256))
|
||||
diff = crop_and_resize - original
|
||||
mse = np.sum(np.power(diff, 2))
|
||||
mse = diff_mse(crop_and_resize, original)
|
||||
assert mse == 0
|
||||
logger.info("random_crop_and_resize_op_{}, mse: {}".format(num_iter + 1, mse))
|
||||
# Uncomment below line if you want to visualize images
|
||||
# visualize(crop_and_resize, mse, original)
|
||||
num_iter += 1
|
||||
crop_and_resize_images.append(crop_and_resize)
|
||||
original_images.append(original)
|
||||
if plot:
|
||||
visualize(original_images, crop_and_resize_images)
|
||||
|
||||
def test_random_crop_and_resize_01():
|
||||
"""
|
||||
Test RandomCropAndResize with md5 check, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_01")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = c_vision.Decode()
|
||||
random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), (0.5, 1), (0.5, 1))
|
||||
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||
data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op)
|
||||
|
||||
# Second dataset
|
||||
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomResizedCrop((256, 512), (0.5, 1), (0.5, 1)),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data2 = data2.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename1 = "random_crop_and_resize_01_c_result.npz"
|
||||
filename2 = "random_crop_and_resize_01_py_result.npz"
|
||||
save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN)
|
||||
save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_and_resize_02():
|
||||
"""
|
||||
Test RandomCropAndResize with md5 check:Image interpolation mode is Inter.NEAREST,
|
||||
expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_02")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = c_vision.Decode()
|
||||
random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), interpolation=mode.Inter.NEAREST)
|
||||
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||
data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op)
|
||||
|
||||
# Second dataset
|
||||
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomResizedCrop((256, 512), interpolation=mode.Inter.NEAREST),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data2 = data2.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename1 = "random_crop_and_resize_02_c_result.npz"
|
||||
filename2 = "random_crop_and_resize_02_py_result.npz"
|
||||
save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN)
|
||||
save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_and_resize_03():
|
||||
"""
|
||||
Test RandomCropAndResize with md5 check: max_attempts is 1, expected to pass
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_03")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = c_vision.Decode()
|
||||
random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), max_attempts=1)
|
||||
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||
data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op)
|
||||
|
||||
# Second dataset
|
||||
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomResizedCrop((256, 512), max_attempts=1),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data2 = data2.map(input_columns=["image"], operations=transform())
|
||||
|
||||
filename1 = "random_crop_and_resize_03_c_result.npz"
|
||||
filename2 = "random_crop_and_resize_03_py_result.npz"
|
||||
save_and_check_md5(data1, filename1, generate_golden=GENERATE_GOLDEN)
|
||||
save_and_check_md5(data2, filename2, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
def test_random_crop_and_resize_04_c():
|
||||
"""
|
||||
Test RandomCropAndResize with c_tranforms: invalid range of scale (max<min),
|
||||
expected to raise ValueError
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_04_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = c_vision.Decode()
|
||||
# If input range of scale is not in the order of (min, max), ValueError will be raised.
|
||||
random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), (1, 0.5), (0.5, 0.5))
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_and_resize_op)
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = item["image"]
|
||||
image_list.append(image.shape)
|
||||
except ValueError as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
assert "Input range is not valid" in str(e)
|
||||
|
||||
def test_random_crop_and_resize_04_py():
|
||||
"""
|
||||
Test RandomCropAndResize with py_transforms: invalid range of scale (max<min),
|
||||
expected to raise ValueError
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_04_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
# If input range of scale is not in the order of (min, max), ValueError will be raised.
|
||||
py_vision.RandomResizedCrop((256, 512), (1, 0.5), (0.5, 0.5)),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = item["image"]
|
||||
image_list.append(image.shape)
|
||||
except ValueError as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
assert "Input range is not valid" in str(e)
|
||||
|
||||
def test_random_crop_and_resize_05_c():
|
||||
"""
|
||||
Test RandomCropAndResize with c_transforms: invalid range of ratio (max<min),
|
||||
expected to raise ValueError
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_05_c")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = c_vision.Decode()
|
||||
random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), (1, 1), (1, 0.5))
|
||||
# If input range of ratio is not in the order of (min, max), ValueError will be raised.
|
||||
data = data.map(input_columns=["image"], operations=decode_op)
|
||||
data = data.map(input_columns=["image"], operations=random_crop_and_resize_op)
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = item["image"]
|
||||
image_list.append(image.shape)
|
||||
except ValueError as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
assert "Input range is not valid" in str(e)
|
||||
|
||||
def test_random_crop_and_resize_05_py():
|
||||
"""
|
||||
Test RandomCropAndResize with py_transforms: invalid range of ratio (max<min),
|
||||
expected to raise ValueError
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_05_py")
|
||||
ds.config.set_seed(0)
|
||||
ds.config.set_num_parallel_workers(1)
|
||||
|
||||
try:
|
||||
# Generate dataset
|
||||
data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
# If input range of ratio is not in the order of (min, max), ValueError will be raised.
|
||||
py_vision.RandomResizedCrop((256, 512), (1, 1), (1, 0.5)),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data = data.map(input_columns=["image"], operations=transform())
|
||||
image_list = []
|
||||
for item in data.create_dict_iterator():
|
||||
image = item["image"]
|
||||
image_list.append(image.shape)
|
||||
except ValueError as e:
|
||||
logger.info("Got an exception in DE: {}".format(str(e)))
|
||||
assert "Input range is not valid" in str(e)
|
||||
|
||||
def test_random_crop_and_resize_comp(plot=False):
|
||||
"""
|
||||
Test RandomCropAndResize and compare between python and c image augmentation
|
||||
"""
|
||||
logger.info("test_random_crop_and_resize_comp")
|
||||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = c_vision.Decode()
|
||||
random_crop_and_resize_op = c_vision.RandomResizedCrop(512, (1, 1), (0.5, 0.5))
|
||||
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||
data1 = data1.map(input_columns=["image"], operations=random_crop_and_resize_op)
|
||||
|
||||
# Second dataset
|
||||
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomResizedCrop(512, (1, 1), (0.5, 0.5)),
|
||||
py_vision.ToTensor()
|
||||
]
|
||||
transform = py_vision.ComposeOp(transforms)
|
||||
data2 = data2.map(input_columns=["image"], operations=transform())
|
||||
|
||||
image_c_cropped = []
|
||||
image_py_cropped = []
|
||||
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)
|
||||
image_c_cropped.append(c_image)
|
||||
image_py_cropped.append(py_image)
|
||||
if plot:
|
||||
visualize(image_c_cropped, image_py_cropped)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_random_crop_and_resize_op()
|
||||
test_random_crop_and_resize_op(True)
|
||||
test_random_crop_and_resize_01()
|
||||
test_random_crop_and_resize_02()
|
||||
test_random_crop_and_resize_03()
|
||||
test_random_crop_and_resize_04_c()
|
||||
test_random_crop_and_resize_04_py()
|
||||
test_random_crop_and_resize_05_c()
|
||||
test_random_crop_and_resize_05_py()
|
||||
test_random_crop_and_resize_comp(True)
|
||||
|
|
Loading…
Reference in New Issue