You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mindspore/tests/ut/python/dataset/test_invert.py

102 lines
3.2 KiB

# Copyright 2020 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.
# ==============================================================================
import matplotlib.pyplot as plt
5 years ago
import numpy as np
import mindspore.dataset.engine as de
import mindspore.dataset.transforms.vision.py_transforms as F
5 years ago
from mindspore import log as logger
DATA_DIR = "../data/dataset/testImageNetData/train/"
5 years ago
def visualize(image_original, image_invert):
"""
visualizes the image using DE op and Numpy op
"""
num = len(image_invert)
for i in range(num):
plt.subplot(2, num, i + 1)
plt.imshow(image_original[i])
plt.title("Original image")
plt.subplot(2, num, i + num + 1)
plt.imshow(image_invert[i])
plt.title("DE Color Inverted image")
plt.show()
5 years ago
def test_invert(plot=False):
"""
Test Invert
"""
logger.info("Test Invert")
5 years ago
# Original Images
5 years ago
ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
transforms_original = F.ComposeOp([F.Decode(),
5 years ago
F.Resize((224, 224)),
F.ToTensor()])
ds_original = ds.map(input_columns="image",
operations=transforms_original())
5 years ago
ds_original = ds_original.batch(512)
5 years ago
for idx, (image, _) in enumerate(ds_original):
if idx == 0:
5 years ago
images_original = np.transpose(image, (0, 2, 3, 1))
else:
images_original = np.append(images_original,
5 years ago
np.transpose(image, (0, 2, 3, 1)),
axis=0)
# Color Inverted Images
ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
transforms_invert = F.ComposeOp([F.Decode(),
5 years ago
F.Resize((224, 224)),
F.Invert(),
5 years ago
F.ToTensor()])
ds_invert = ds.map(input_columns="image",
5 years ago
operations=transforms_invert())
ds_invert = ds_invert.batch(512)
for idx, (image, _) in enumerate(ds_invert):
if idx == 0:
5 years ago
images_invert = np.transpose(image, (0, 2, 3, 1))
else:
images_invert = np.append(images_invert,
5 years ago
np.transpose(image, (0, 2, 3, 1)),
axis=0)
5 years ago
num_samples = images_original.shape[0]
mse = np.zeros(num_samples)
for i in range(num_samples):
5 years ago
mse[i] = np.mean((images_invert[i] - images_original[i]) ** 2)
logger.info("MSE= {}".format(str(np.mean(mse))))
5 years ago
if plot:
visualize(images_original, images_invert)
5 years ago
if __name__ == "__main__":
test_invert(plot=True)