|
|
|
@@ -15,6 +15,7 @@ |
|
|
|
""" |
|
|
|
Testing RandomColorAdjust op in DE |
|
|
|
""" |
|
|
|
import pytest |
|
|
|
import matplotlib.pyplot as plt |
|
|
|
import numpy as np |
|
|
|
from util import diff_mse |
|
|
|
@@ -46,71 +47,51 @@ def visualize(first, mse, second): |
|
|
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
def test_random_color_adjust_op_brightness(plot=False): |
|
|
|
def util_test_random_color_adjust_error(brightness=(1, 1), contrast=(1, 1), saturation=(1, 1), hue=(0, 0)): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust op |
|
|
|
Util function that tests the error message in case of grayscale images |
|
|
|
""" |
|
|
|
logger.info("test_random_color_adjust_op") |
|
|
|
|
|
|
|
# First dataset |
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
decode_op = c_vision.Decode() |
|
|
|
|
|
|
|
random_adjust_op = c_vision.RandomColorAdjust((0.8, 0.8), (1, 1), (1, 1), (0, 0)) |
|
|
|
|
|
|
|
ctrans = [decode_op, |
|
|
|
random_adjust_op, |
|
|
|
] |
|
|
|
|
|
|
|
data1 = data1.map(input_columns=["image"], operations=ctrans) |
|
|
|
|
|
|
|
# Second dataset |
|
|
|
transforms = [ |
|
|
|
py_vision.Decode(), |
|
|
|
py_vision.RandomColorAdjust((0.8, 0.8), (1, 1), (1, 1), (0, 0)), |
|
|
|
py_vision.Grayscale(1), |
|
|
|
py_vision.ToTensor(), |
|
|
|
(lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8)) |
|
|
|
] |
|
|
|
transform = py_vision.ComposeOp(transforms) |
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
data2 = data2.map(input_columns=["image"], operations=transform()) |
|
|
|
|
|
|
|
num_iter = 0 |
|
|
|
for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): |
|
|
|
num_iter += 1 |
|
|
|
c_image = item1["image"] |
|
|
|
py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) |
|
|
|
|
|
|
|
logger.info("shape of c_image: {}".format(c_image.shape)) |
|
|
|
logger.info("shape of py_image: {}".format(py_image.shape)) |
|
|
|
transform = py_vision.ComposeOp(transforms) |
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
data1 = data1.map(input_columns=["image"], operations=transform()) |
|
|
|
|
|
|
|
logger.info("dtype of c_image: {}".format(c_image.dtype)) |
|
|
|
logger.info("dtype of py_image: {}".format(py_image.dtype)) |
|
|
|
# if input is grayscale, the output dimensions should be single channel, the following should fail |
|
|
|
random_adjust_op = c_vision.RandomColorAdjust(brightness=brightness, contrast=contrast, saturation=saturation, |
|
|
|
hue=hue) |
|
|
|
with pytest.raises(RuntimeError) as info: |
|
|
|
data1 = data1.map(input_columns=["image"], operations=random_adjust_op) |
|
|
|
dataset_shape_1 = [] |
|
|
|
for item1 in data1.create_dict_iterator(): |
|
|
|
c_image = item1["image"] |
|
|
|
dataset_shape_1.append(c_image.shape) |
|
|
|
|
|
|
|
mse = diff_mse(c_image, py_image) |
|
|
|
logger.info("mse is {}".format(mse)) |
|
|
|
error_msg = "The shape is incorrect: number of channels does not equal 3" |
|
|
|
|
|
|
|
logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse)) |
|
|
|
assert mse < 0.01 |
|
|
|
# if mse != 0: |
|
|
|
# logger.info("mse is: {}".format(mse)) |
|
|
|
if plot: |
|
|
|
visualize(c_image, mse, py_image) |
|
|
|
assert error_msg in str(info.value) |
|
|
|
|
|
|
|
|
|
|
|
def test_random_color_adjust_op_contrast(plot=False): |
|
|
|
def util_test_random_color_adjust_op(brightness=(1, 1), contrast=(1, 1), saturation=(1, 1), hue=(0, 0), plot=False): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust op |
|
|
|
Util function that tests RandomColorAdjust for a specific argument |
|
|
|
""" |
|
|
|
logger.info("test_random_color_adjust_op") |
|
|
|
|
|
|
|
# First dataset |
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
decode_op = c_vision.Decode() |
|
|
|
|
|
|
|
random_adjust_op = c_vision.RandomColorAdjust((1, 1), (0.5, 0.5), (1, 1), (0, 0)) |
|
|
|
random_adjust_op = c_vision.RandomColorAdjust(brightness=brightness, contrast=contrast, saturation=saturation, |
|
|
|
hue=hue) |
|
|
|
|
|
|
|
ctrans = [decode_op, |
|
|
|
random_adjust_op |
|
|
|
random_adjust_op, |
|
|
|
] |
|
|
|
|
|
|
|
data1 = data1.map(input_columns=["image"], operations=ctrans) |
|
|
|
@@ -118,8 +99,9 @@ def test_random_color_adjust_op_contrast(plot=False): |
|
|
|
# Second dataset |
|
|
|
transforms = [ |
|
|
|
py_vision.Decode(), |
|
|
|
py_vision.RandomColorAdjust((1, 1), (0.5, 0.5), (1, 1), (0, 0)), |
|
|
|
py_vision.ToTensor(), |
|
|
|
py_vision.RandomColorAdjust(brightness=brightness, contrast=contrast, saturation=saturation, |
|
|
|
hue=hue), |
|
|
|
py_vision.ToTensor() |
|
|
|
] |
|
|
|
transform = py_vision.ComposeOp(transforms) |
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
@@ -136,161 +118,101 @@ def test_random_color_adjust_op_contrast(plot=False): |
|
|
|
|
|
|
|
logger.info("dtype of c_image: {}".format(c_image.dtype)) |
|
|
|
logger.info("dtype of py_image: {}".format(py_image.dtype)) |
|
|
|
diff = c_image - py_image |
|
|
|
logger.info("contrast difference c is : {}".format(c_image[0][0])) |
|
|
|
logger.info("contrast difference py is : {}".format(py_image[0][0])) |
|
|
|
diff = c_image - py_image |
|
|
|
logger.info("contrast difference is : {}".format(diff[0][0])) |
|
|
|
# mse = (np.sum(np.power(diff, 2))) / (c_image.shape[0] * c_image.shape[1]) |
|
|
|
|
|
|
|
mse = diff_mse(c_image, py_image) |
|
|
|
logger.info("mse is {}".format(mse)) |
|
|
|
# assert mse < 0.01 |
|
|
|
# logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse)) |
|
|
|
# if mse != 0: |
|
|
|
# logger.info("mse is: {}".format(mse)) |
|
|
|
|
|
|
|
logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse)) |
|
|
|
assert mse < 0.01 |
|
|
|
|
|
|
|
if plot: |
|
|
|
visualize(c_image, mse, py_image) |
|
|
|
|
|
|
|
|
|
|
|
def test_random_color_adjust_op_saturation(plot=False): |
|
|
|
def test_random_color_adjust_op_brightness(plot=False): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust op |
|
|
|
Test RandomColorAdjust op for brightness |
|
|
|
""" |
|
|
|
logger.info("test_random_color_adjust_op") |
|
|
|
|
|
|
|
# First dataset |
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
decode_op = c_vision.Decode() |
|
|
|
logger.info("test_random_color_adjust_op_brightness") |
|
|
|
|
|
|
|
random_adjust_op = c_vision.RandomColorAdjust((1, 1), (1, 1), (0.5, 0.5), (0, 0)) |
|
|
|
util_test_random_color_adjust_op(brightness=(0.5, 0.5), plot=plot) |
|
|
|
|
|
|
|
ctrans = [decode_op, |
|
|
|
random_adjust_op |
|
|
|
] |
|
|
|
|
|
|
|
data1 = data1.map(input_columns=["image"], operations=ctrans) |
|
|
|
def test_random_color_adjust_op_brightness_error(): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust error message with brightness input in case of grayscale image |
|
|
|
""" |
|
|
|
|
|
|
|
# Second dataset |
|
|
|
transforms = [ |
|
|
|
py_vision.Decode(), |
|
|
|
py_vision.RandomColorAdjust((1, 1), (1, 1), (0.5, 0.5), (0, 0)), |
|
|
|
py_vision.ToTensor(), |
|
|
|
] |
|
|
|
transform = py_vision.ComposeOp(transforms) |
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
data2 = data2.map(input_columns=["image"], operations=transform()) |
|
|
|
logger.info("test_random_color_adjust_op_brightness_error") |
|
|
|
|
|
|
|
num_iter = 0 |
|
|
|
util_test_random_color_adjust_error(brightness=(0.5, 0.5)) |
|
|
|
|
|
|
|
for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): |
|
|
|
num_iter += 1 |
|
|
|
c_image = item1["image"] |
|
|
|
py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) |
|
|
|
|
|
|
|
logger.info("shape of c_image: {}".format(c_image.shape)) |
|
|
|
logger.info("shape of py_image: {}".format(py_image.shape)) |
|
|
|
def test_random_color_adjust_op_contrast(plot=False): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust op for contrast |
|
|
|
""" |
|
|
|
|
|
|
|
logger.info("dtype of c_image: {}".format(c_image.dtype)) |
|
|
|
logger.info("dtype of py_image: {}".format(py_image.dtype)) |
|
|
|
logger.info("test_random_color_adjust_op_contrast") |
|
|
|
|
|
|
|
mse = diff_mse(c_image, py_image) |
|
|
|
logger.info("mse is {}".format(mse)) |
|
|
|
assert mse < 0.01 |
|
|
|
# logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse)) |
|
|
|
# if mse != 0: |
|
|
|
# logger.info("mse is: {}".format(mse)) |
|
|
|
if plot: |
|
|
|
visualize(c_image, mse, py_image) |
|
|
|
util_test_random_color_adjust_op(contrast=(0.5, 0.5), plot=plot) |
|
|
|
|
|
|
|
|
|
|
|
def test_random_color_adjust_op_hue(plot=False): |
|
|
|
def test_random_color_adjust_op_contrast_error(): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust op |
|
|
|
Test RandomColorAdjust error message with contrast input in case of grayscale image |
|
|
|
""" |
|
|
|
logger.info("test_random_color_adjust_op") |
|
|
|
|
|
|
|
# First dataset |
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
decode_op = c_vision.Decode() |
|
|
|
logger.info("test_random_color_adjust_op_contrast_error") |
|
|
|
|
|
|
|
random_adjust_op = c_vision.RandomColorAdjust((1, 1), (1, 1), (1, 1), (0.2, 0.2)) |
|
|
|
util_test_random_color_adjust_error(contrast=(0.5, 0.5)) |
|
|
|
|
|
|
|
ctrans = [decode_op, |
|
|
|
random_adjust_op, |
|
|
|
] |
|
|
|
|
|
|
|
data1 = data1.map(input_columns=["image"], operations=ctrans) |
|
|
|
def test_random_color_adjust_op_saturation(plot=False): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust op for saturation |
|
|
|
""" |
|
|
|
logger.info("test_random_color_adjust_op_saturation") |
|
|
|
|
|
|
|
# Second dataset |
|
|
|
transforms = [ |
|
|
|
py_vision.Decode(), |
|
|
|
py_vision.RandomColorAdjust((1, 1), (1, 1), (1, 1), (0.2, 0.2)), |
|
|
|
py_vision.ToTensor(), |
|
|
|
] |
|
|
|
transform = py_vision.ComposeOp(transforms) |
|
|
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
data2 = data2.map(input_columns=["image"], operations=transform()) |
|
|
|
util_test_random_color_adjust_op(saturation=(0.5, 0.5), plot=plot) |
|
|
|
|
|
|
|
num_iter = 0 |
|
|
|
for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): |
|
|
|
num_iter += 1 |
|
|
|
c_image = item1["image"] |
|
|
|
py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) |
|
|
|
|
|
|
|
# logger.info("shape of img: {}".format(img.shape)) |
|
|
|
logger.info("shape of c_image: {}".format(c_image.shape)) |
|
|
|
logger.info("shape of py_image: {}".format(py_image.shape)) |
|
|
|
def test_random_color_adjust_op_saturation_error(): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust error message with saturation input in case of grayscale image |
|
|
|
""" |
|
|
|
|
|
|
|
logger.info("dtype of c_image: {}".format(c_image.dtype)) |
|
|
|
logger.info("dtype of py_image: {}".format(py_image.dtype)) |
|
|
|
# logger.info("dtype of img: {}".format(img.dtype)) |
|
|
|
logger.info("test_random_color_adjust_op_saturation_error") |
|
|
|
|
|
|
|
# mse = (np.sum(np.power(diff, 2))) / (c_image.shape[0] * c_image.shape[1]) |
|
|
|
mse = diff_mse(c_image, py_image) |
|
|
|
logger.info("mse is {}".format(mse)) |
|
|
|
assert mse < 0.01 |
|
|
|
if plot: |
|
|
|
visualize(c_image, mse, py_image) |
|
|
|
util_test_random_color_adjust_error(saturation=(0.5, 0.5)) |
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unnecessary-lambda |
|
|
|
def test_random_color_adjust_grayscale(): |
|
|
|
def test_random_color_adjust_op_hue(plot=False): |
|
|
|
""" |
|
|
|
Tests that the random color adjust works for grayscale images |
|
|
|
Test RandomColorAdjust op for hue |
|
|
|
""" |
|
|
|
logger.info("test_random_color_adjust_op_hue") |
|
|
|
|
|
|
|
def channel_swap(image): |
|
|
|
""" |
|
|
|
Py func hack for our pytransforms to work with c transforms |
|
|
|
""" |
|
|
|
return (image.transpose(1, 2, 0) * 255).astype(np.uint8) |
|
|
|
util_test_random_color_adjust_op(hue=(0.5, 0.5), plot=plot) |
|
|
|
|
|
|
|
transforms = [ |
|
|
|
py_vision.Decode(), |
|
|
|
py_vision.Grayscale(1), |
|
|
|
py_vision.ToTensor(), |
|
|
|
(lambda image: channel_swap(image)) |
|
|
|
] |
|
|
|
|
|
|
|
transform = py_vision.ComposeOp(transforms) |
|
|
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) |
|
|
|
data1 = data1.map(input_columns=["image"], operations=transform()) |
|
|
|
def test_random_color_adjust_op_hue_error(): |
|
|
|
""" |
|
|
|
Test RandomColorAdjust error message with hue input in case of grayscale image |
|
|
|
""" |
|
|
|
|
|
|
|
# if input is grayscale, the output dimensions should be single channel, the following should fail |
|
|
|
random_adjust_op = c_vision.RandomColorAdjust((1, 1), (1, 1), (1, 1), (0.2, 0.2)) |
|
|
|
try: |
|
|
|
data1 = data1.map(input_columns=["image"], operations=random_adjust_op) |
|
|
|
dataset_shape_1 = [] |
|
|
|
for item1 in data1.create_dict_iterator(): |
|
|
|
c_image = item1["image"] |
|
|
|
dataset_shape_1.append(c_image.shape) |
|
|
|
except Exception as e: |
|
|
|
logger.info("Got an exception in DE: {}".format(str(e))) |
|
|
|
logger.info("test_random_color_adjust_op_hue_error") |
|
|
|
|
|
|
|
util_test_random_color_adjust_error(hue=(0.5, 0.5)) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
test_random_color_adjust_op_brightness() |
|
|
|
test_random_color_adjust_op_contrast() |
|
|
|
test_random_color_adjust_op_saturation() |
|
|
|
test_random_color_adjust_op_hue() |
|
|
|
test_random_color_adjust_grayscale() |
|
|
|
test_random_color_adjust_op_brightness(plot=True) |
|
|
|
test_random_color_adjust_op_brightness_error() |
|
|
|
test_random_color_adjust_op_contrast(plot=True) |
|
|
|
test_random_color_adjust_op_contrast_error() |
|
|
|
test_random_color_adjust_op_saturation(plot=True) |
|
|
|
test_random_color_adjust_op_saturation_error() |
|
|
|
test_random_color_adjust_op_hue(plot=True) |
|
|
|
test_random_color_adjust_op_hue_error() |