2022-05-25 04:40:18 +08:00
|
|
|
# Copyright 2019-2022 Huawei Technologies Co., Ltd
|
2020-03-27 14:49:12 +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 CenterCrop op in DE
|
|
|
|
|
"""
|
2020-05-12 02:44:24 +08:00
|
|
|
import numpy as np
|
2020-05-18 16:42:35 +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-03-27 14:49:12 +08:00
|
|
|
from mindspore import log as logger
|
2020-06-10 03:12:07 +08:00
|
|
|
from util import diff_mse, visualize_list, save_and_check_md5
|
2020-05-04 23:50:53 +08:00
|
|
|
|
|
|
|
|
GENERATE_GOLDEN = False
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
|
2020-05-04 23:50:53 +08:00
|
|
|
def test_center_crop_op(height=375, width=375, plot=False):
|
2020-03-27 14:49:12 +08:00
|
|
|
"""
|
2022-05-25 04:40:18 +08:00
|
|
|
Feature: CenterCrop op
|
|
|
|
|
Description: Test CenterCrop op basic usage
|
|
|
|
|
Expectation: Runs successfully
|
2020-03-27 14:49:12 +08:00
|
|
|
"""
|
2020-05-04 23:50:53 +08:00
|
|
|
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])
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=decode_op, input_columns=["image"])
|
|
|
|
|
data1 = data1.map(operations=center_crop_op, input_columns=["image"])
|
2020-03-27 14:49:12 +08:00
|
|
|
|
2020-05-04 23:50:53 +08:00
|
|
|
# Second dataset
|
|
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
|
2020-09-10 01:23:02 +08:00
|
|
|
data2 = data2.map(operations=decode_op, input_columns=["image"])
|
2020-03-27 14:49:12 +08:00
|
|
|
|
2020-05-04 23:50:53 +08:00
|
|
|
image_cropped = []
|
|
|
|
|
image = []
|
2020-09-05 10:56:38 +08:00
|
|
|
for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
|
|
|
|
|
data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
|
2020-05-04 23:50:53 +08:00
|
|
|
image_cropped.append(item1["image"].copy())
|
|
|
|
|
image.append(item2["image"].copy())
|
|
|
|
|
if plot:
|
2020-06-10 03:12:07 +08:00
|
|
|
visualize_list(image, image_cropped)
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
|
|
2020-05-04 23:50:53 +08:00
|
|
|
def test_center_crop_md5(height=375, width=375):
|
2020-03-27 14:49:12 +08:00
|
|
|
"""
|
2022-05-25 04:40:18 +08:00
|
|
|
Feature: CenterCrop op
|
|
|
|
|
Description: Test CenterCrop using md5 check test
|
|
|
|
|
Expectation: Passes the md5 check test
|
2020-03-27 14:49:12 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("Test CenterCrop")
|
|
|
|
|
|
|
|
|
|
# First dataset
|
2022-05-25 04:40:18 +08:00
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=[
|
|
|
|
|
"image"], shuffle=False)
|
2020-03-27 14:49:12 +08:00
|
|
|
decode_op = vision.Decode()
|
|
|
|
|
# 3 images [375, 500] [600, 500] [512, 512]
|
2020-05-04 23:50:53 +08:00
|
|
|
center_crop_op = vision.CenterCrop([height, width])
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=decode_op, input_columns=["image"])
|
|
|
|
|
data1 = data1.map(operations=center_crop_op, input_columns=["image"])
|
2020-05-12 02:44:24 +08:00
|
|
|
# Compare with expected md5 from images
|
|
|
|
|
filename = "center_crop_01_result.npz"
|
2020-05-06 03:35:09 +08:00
|
|
|
save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
|
2020-05-04 23:50:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_center_crop_comp(height=375, width=375, plot=False):
|
|
|
|
|
"""
|
2022-05-25 04:40:18 +08:00
|
|
|
Feature: CenterCrop op
|
|
|
|
|
Description: Test CenterCrop between Python and Cpp image augmentation
|
|
|
|
|
Expectation: Resulting outputs from both operations are expected to be equal
|
2020-05-04 23:50:53 +08:00
|
|
|
"""
|
|
|
|
|
logger.info("Test CenterCrop")
|
|
|
|
|
|
|
|
|
|
# First dataset
|
2022-05-25 04:40:18 +08:00
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=[
|
|
|
|
|
"image"], shuffle=False)
|
2020-05-04 23:50:53 +08:00
|
|
|
decode_op = vision.Decode()
|
|
|
|
|
center_crop_op = vision.CenterCrop([height, width])
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=decode_op, input_columns=["image"])
|
|
|
|
|
data1 = data1.map(operations=center_crop_op, input_columns=["image"])
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
|
# Second dataset
|
2022-05-25 04:40:18 +08:00
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=[
|
|
|
|
|
"image"], shuffle=False)
|
2020-05-04 23:50:53 +08:00
|
|
|
transforms = [
|
2022-05-03 02:50:47 +08:00
|
|
|
vision.Decode(True),
|
|
|
|
|
vision.CenterCrop([height, width]),
|
|
|
|
|
vision.ToTensor()
|
2020-05-04 23:50:53 +08:00
|
|
|
]
|
2022-05-26 04:30:20 +08:00
|
|
|
transform = mindspore.dataset.transforms.Compose(transforms)
|
2020-09-10 01:23:02 +08:00
|
|
|
data2 = data2.map(operations=transform, input_columns=["image"])
|
2020-03-27 14:49:12 +08:00
|
|
|
|
2020-06-10 03:12:07 +08:00
|
|
|
image_c_cropped = []
|
|
|
|
|
image_py_cropped = []
|
2020-09-05 10:56:38 +08:00
|
|
|
for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
|
|
|
|
|
data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
|
2020-05-04 23:50:53 +08:00
|
|
|
c_image = item1["image"]
|
|
|
|
|
py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
|
2020-05-12 02:44:24 +08:00
|
|
|
# Note: The images aren't exactly the same due to rounding error
|
|
|
|
|
assert diff_mse(py_image, c_image) < 0.001
|
2020-06-10 03:12:07 +08:00
|
|
|
image_c_cropped.append(c_image.copy())
|
|
|
|
|
image_py_cropped.append(py_image.copy())
|
2020-03-27 14:49:12 +08:00
|
|
|
if plot:
|
2020-06-10 03:12:07 +08:00
|
|
|
visualize_list(image_c_cropped, image_py_cropped, visualize_mode=2)
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
|
|
2020-05-12 02:44:24 +08:00
|
|
|
def test_crop_grayscale(height=375, width=375):
|
2020-05-07 09:05:12 +08:00
|
|
|
"""
|
2022-05-25 04:40:18 +08:00
|
|
|
Feature: CenterCrop op
|
|
|
|
|
Description: Test CenterCrop works with pad and grayscale images
|
|
|
|
|
Expectation: Runs successfully
|
2020-05-07 09:05:12 +08:00
|
|
|
"""
|
2020-05-12 02:44:24 +08:00
|
|
|
|
2020-06-26 09:41:42 +08:00
|
|
|
# Note: image.transpose performs channel swap to allow py transforms to
|
|
|
|
|
# work with c transforms
|
2020-05-07 09:05:12 +08:00
|
|
|
transforms = [
|
2022-05-03 02:50:47 +08:00
|
|
|
vision.Decode(True),
|
|
|
|
|
vision.Grayscale(1),
|
|
|
|
|
vision.ToTensor(),
|
2020-06-26 09:41:42 +08:00
|
|
|
(lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8))
|
2020-05-07 09:05:12 +08:00
|
|
|
]
|
|
|
|
|
|
2022-05-26 04:30:20 +08:00
|
|
|
transform = mindspore.dataset.transforms.Compose(transforms)
|
2022-05-25 04:40:18 +08:00
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=[
|
|
|
|
|
"image"], shuffle=False)
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=transform, input_columns=["image"])
|
2020-05-07 09:05:12 +08:00
|
|
|
|
2020-05-12 02:44:24 +08:00
|
|
|
# If input is grayscale, the output dimensions should be single channel
|
2020-05-07 09:05:12 +08:00
|
|
|
crop_gray = vision.CenterCrop([height, width])
|
2020-09-10 01:23:02 +08:00
|
|
|
data1 = data1.map(operations=crop_gray, input_columns=["image"])
|
2020-05-07 09:05:12 +08:00
|
|
|
|
2020-09-05 10:56:38 +08:00
|
|
|
for item1 in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
|
2020-05-07 09:05:12 +08:00
|
|
|
c_image = item1["image"]
|
2020-05-12 02:44:24 +08:00
|
|
|
|
|
|
|
|
# Check that the image is grayscale
|
|
|
|
|
assert (c_image.ndim == 3 and c_image.shape[2] == 1)
|
|
|
|
|
|
2020-05-07 09:05:12 +08:00
|
|
|
|
2020-07-21 01:28:54 +08:00
|
|
|
def test_center_crop_errors():
|
|
|
|
|
"""
|
2022-05-25 04:40:18 +08:00
|
|
|
Feature: CenterCrop op
|
|
|
|
|
Description: Test CenterCrop with bad inputs
|
|
|
|
|
Expectation: Error is raised as expected
|
2020-07-21 01:28:54 +08:00
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
test_center_crop_op(16777216, 16777216)
|
|
|
|
|
except RuntimeError as e:
|
2021-09-11 18:18:39 +08:00
|
|
|
assert "CenterCropOp padding size is more than 3 times the original size" in \
|
2020-07-21 01:28:54 +08:00
|
|
|
str(e)
|
|
|
|
|
|
|
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
if __name__ == "__main__":
|
2020-06-02 03:03:05 +08:00
|
|
|
test_center_crop_op(600, 600, plot=True)
|
2020-03-27 14:49:12 +08:00
|
|
|
test_center_crop_op(300, 600)
|
|
|
|
|
test_center_crop_op(600, 300)
|
2020-05-07 09:05:12 +08:00
|
|
|
test_center_crop_md5()
|
2020-06-02 03:03:05 +08:00
|
|
|
test_center_crop_comp(plot=True)
|
2020-05-07 09:05:12 +08:00
|
|
|
test_crop_grayscale()
|