|
|
|
@ -13,53 +13,96 @@
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
import mindspore.dataset.transforms.vision.c_transforms as vision
|
|
|
|
|
import mindspore.dataset.transforms.vision.py_transforms as py_vision
|
|
|
|
|
import numpy as np
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
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 visualize(image_original, image_cropped):
|
|
|
|
|
def test_center_crop_op(height=375, width=375, plot=False):
|
|
|
|
|
"""
|
|
|
|
|
visualizes the image using DE op and Numpy op
|
|
|
|
|
Test random_vertical
|
|
|
|
|
"""
|
|
|
|
|
num = len(image_cropped)
|
|
|
|
|
for i in range(num):
|
|
|
|
|
plt.subplot(2, num, i + 1)
|
|
|
|
|
plt.imshow(image_original[i])
|
|
|
|
|
plt.title("Original image")
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
plt.subplot(2, num, i + num + 1)
|
|
|
|
|
plt.imshow(image_cropped[i])
|
|
|
|
|
plt.title("DE center_crop image")
|
|
|
|
|
# Second dataset
|
|
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
|
|
|
|
|
data2 = data2.map(input_columns=["image"], operations=decode_op)
|
|
|
|
|
|
|
|
|
|
plt.show()
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_center_crop_op(height=375, width=375, plot=False):
|
|
|
|
|
def test_center_crop_md5(height=375, width=375):
|
|
|
|
|
"""
|
|
|
|
|
Test random_vertical
|
|
|
|
|
"""
|
|
|
|
|
logger.info("Test CenterCrop")
|
|
|
|
|
|
|
|
|
|
# First dataset
|
|
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
|
|
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle =False)
|
|
|
|
|
decode_op = vision.Decode()
|
|
|
|
|
# 3 images [375, 500] [600, 500] [512, 512]
|
|
|
|
|
center_crop_op = vision.CenterCrop(height, width)
|
|
|
|
|
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])
|
|
|
|
|
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
|
|
|
|
data1 = data1.map(input_columns=["image"], operations=center_crop_op)
|
|
|
|
|
|
|
|
|
|
# Second dataset
|
|
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
|
|
|
|
|
data2 = data2.map(input_columns=["image"], operations=decode_op)
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
|
|
image_cropped = []
|
|
|
|
|
image = []
|
|
|
|
|
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)
|
|
|
|
|
# the images aren't exactly the same due to rouding error
|
|
|
|
|
assert (diff_mse(py_image, c_image) < 0.001)
|
|
|
|
|
image_cropped.append(item1["image"].copy())
|
|
|
|
|
image.append(item2["image"].copy())
|
|
|
|
|
if plot:
|
|
|
|
@ -67,7 +110,8 @@ def test_center_crop_op(height=375, width=375, plot=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
test_center_crop_op()
|
|
|
|
|
test_center_crop_op(600, 600)
|
|
|
|
|
test_center_crop_op(300, 600)
|
|
|
|
|
test_center_crop_op(600, 300)
|
|
|
|
|
test_center_crop_md5(600, 600)
|
|
|
|
|
test_center_crop_comp()
|