forked from mindspore-Ecosystem/mindspore
!1910 RandomColorAdjust error for grayscale images
Merge pull request !1910 from MahdiRahmaniHanzaki/I1J9SQ-random-color-adjust-bug
This commit is contained in:
commit
0a95223f25
|
@ -376,8 +376,9 @@ Status HwcToChw(std::shared_ptr<Tensor> input, std::shared_ptr<Tensor> *output)
|
|||
*output = input;
|
||||
return Status::OK();
|
||||
}
|
||||
if (input_cv->shape().Size() != 3 && input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels is not equal 3");
|
||||
if (input_cv->shape().Size() < 2 || input_cv->shape().Size() > 3 ||
|
||||
(input_cv->shape().Size() == 3 && input_cv->shape()[2] != 3 && input_cv->shape()[2] != 1)) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels does not equal 3 nor 1");
|
||||
}
|
||||
cv::Mat output_img;
|
||||
|
||||
|
@ -401,8 +402,8 @@ Status HwcToChw(std::shared_ptr<Tensor> input, std::shared_ptr<Tensor> *output)
|
|||
Status SwapRedAndBlue(std::shared_ptr<Tensor> input, std::shared_ptr<Tensor> *output) {
|
||||
try {
|
||||
std::shared_ptr<CVTensor> input_cv = CVTensor::AsCVTensor(std::move(input));
|
||||
if (input_cv->shape().Size() != 3 && input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels is not equal 3");
|
||||
if (input_cv->shape().Size() != 3 || input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels does not equal 3");
|
||||
}
|
||||
auto output_cv = std::make_shared<CVTensor>(input_cv->shape(), input_cv->type());
|
||||
RETURN_UNEXPECTED_IF_NULL(output_cv);
|
||||
|
@ -422,7 +423,7 @@ Status CropAndResize(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tenso
|
|||
RETURN_STATUS_UNEXPECTED("Could not convert to CV Tensor");
|
||||
}
|
||||
if (input_cv->Rank() != 3 && input_cv->Rank() != 2) {
|
||||
RETURN_STATUS_UNEXPECTED("Ishape not <H,W,C> or <H,W>");
|
||||
RETURN_STATUS_UNEXPECTED("Shape not <H,W,C> or <H,W>");
|
||||
}
|
||||
// image too large or too small
|
||||
if (crop_height == 0 || crop_width == 0 || target_height == 0 || target_height > crop_height * 1000 ||
|
||||
|
@ -541,8 +542,8 @@ Status AdjustBrightness(const std::shared_ptr<Tensor> &input, std::shared_ptr<Te
|
|||
if (!input_cv->mat().data) {
|
||||
RETURN_STATUS_UNEXPECTED("Could not convert to CV Tensor");
|
||||
}
|
||||
if (input_cv->Rank() != 3 && input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("Shape not <H,W,3> or <H,W>");
|
||||
if (input_cv->Rank() != 3 || input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels does not equal 3");
|
||||
}
|
||||
auto output_cv = std::make_shared<CVTensor>(input_cv->shape(), input_cv->type());
|
||||
RETURN_UNEXPECTED_IF_NULL(output_cv);
|
||||
|
@ -561,8 +562,8 @@ Status AdjustContrast(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tens
|
|||
if (!input_cv->mat().data) {
|
||||
RETURN_STATUS_UNEXPECTED("Could not convert to CV Tensor");
|
||||
}
|
||||
if (input_cv->Rank() != 3 && input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("Shape not <H,W,3> or <H,W>");
|
||||
if (input_cv->Rank() != 3 || input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels does not equal 3");
|
||||
}
|
||||
cv::Mat gray, output_img;
|
||||
cv::cvtColor(input_img, gray, CV_RGB2GRAY);
|
||||
|
@ -587,8 +588,8 @@ Status AdjustSaturation(const std::shared_ptr<Tensor> &input, std::shared_ptr<Te
|
|||
if (!input_cv->mat().data) {
|
||||
RETURN_STATUS_UNEXPECTED("Could not convert to CV Tensor");
|
||||
}
|
||||
if (input_cv->Rank() != 3 && input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("Shape not <H,W,3> or <H,W>");
|
||||
if (input_cv->Rank() != 3 || input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels does not equal 3");
|
||||
}
|
||||
auto output_cv = std::make_shared<CVTensor>(input_cv->shape(), input_cv->type());
|
||||
RETURN_UNEXPECTED_IF_NULL(output_cv);
|
||||
|
@ -615,8 +616,8 @@ Status AdjustHue(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *
|
|||
if (!input_cv->mat().data) {
|
||||
RETURN_STATUS_UNEXPECTED("Could not convert to CV Tensor");
|
||||
}
|
||||
if (input_cv->Rank() != 3 && input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("Shape not <H,W,3> or <H,W>");
|
||||
if (input_cv->Rank() != 3 || input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("The shape is incorrect: number of channels does not equal 3");
|
||||
}
|
||||
auto output_cv = std::make_shared<CVTensor>(input_cv->shape(), input_cv->type());
|
||||
RETURN_UNEXPECTED_IF_NULL(output_cv);
|
||||
|
@ -644,7 +645,7 @@ Status Erase(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *outp
|
|||
uint8_t fill_g, uint8_t fill_b) {
|
||||
try {
|
||||
std::shared_ptr<CVTensor> input_cv = CVTensor::AsCVTensor(input);
|
||||
if (input_cv->mat().data == nullptr || (input_cv->Rank() != 3 && input_cv->shape()[2] != 3)) {
|
||||
if (input_cv->mat().data == nullptr || input_cv->Rank() != 3 || input_cv->shape()[2] != 3) {
|
||||
RETURN_STATUS_UNEXPECTED("bad CV Tensor input for erase");
|
||||
}
|
||||
cv::Mat input_img = input_cv->mat();
|
||||
|
|
|
@ -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,17 +47,48 @@ 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
|
||||
"""
|
||||
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.Grayscale(1),
|
||||
py_vision.ToTensor(),
|
||||
(lambda image: (image.transpose(1, 2, 0) * 255).astype(np.uint8))
|
||||
]
|
||||
|
||||
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())
|
||||
|
||||
# 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)
|
||||
|
||||
error_msg = "The shape is incorrect: number of channels does not equal 3"
|
||||
|
||||
assert error_msg in str(info.value)
|
||||
|
||||
|
||||
def util_test_random_color_adjust_op(brightness=(1, 1), contrast=(1, 1), saturation=(1, 1), hue=(0, 0), plot=False):
|
||||
"""
|
||||
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((0.8, 0.8), (1, 1), (1, 1), (0, 0))
|
||||
random_adjust_op = c_vision.RandomColorAdjust(brightness=brightness, contrast=contrast, saturation=saturation,
|
||||
hue=hue)
|
||||
|
||||
ctrans = [decode_op,
|
||||
random_adjust_op,
|
||||
|
@ -67,8 +99,9 @@ def test_random_color_adjust_op_brightness(plot=False):
|
|||
# Second dataset
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomColorAdjust((0.8, 0.8), (1, 1), (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)
|
||||
|
@ -91,206 +124,95 @@ def test_random_color_adjust_op_brightness(plot=False):
|
|||
|
||||
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)
|
||||
|
||||
|
||||
def test_random_color_adjust_op_brightness(plot=False):
|
||||
"""
|
||||
Test RandomColorAdjust op for brightness
|
||||
"""
|
||||
|
||||
logger.info("test_random_color_adjust_op_brightness")
|
||||
|
||||
util_test_random_color_adjust_op(brightness=(0.5, 0.5), plot=plot)
|
||||
|
||||
|
||||
def test_random_color_adjust_op_brightness_error():
|
||||
"""
|
||||
Test RandomColorAdjust error message with brightness input in case of grayscale image
|
||||
"""
|
||||
|
||||
logger.info("test_random_color_adjust_op_brightness_error")
|
||||
|
||||
util_test_random_color_adjust_error(brightness=(0.5, 0.5))
|
||||
|
||||
|
||||
def test_random_color_adjust_op_contrast(plot=False):
|
||||
"""
|
||||
Test RandomColorAdjust op
|
||||
Test RandomColorAdjust op for contrast
|
||||
"""
|
||||
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")
|
||||
|
||||
random_adjust_op = c_vision.RandomColorAdjust((1, 1), (0.5, 0.5), (1, 1), (0, 0))
|
||||
util_test_random_color_adjust_op(contrast=(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_contrast_error():
|
||||
"""
|
||||
Test RandomColorAdjust error message with contrast input in case of grayscale image
|
||||
"""
|
||||
|
||||
# Second dataset
|
||||
transforms = [
|
||||
py_vision.Decode(),
|
||||
py_vision.RandomColorAdjust((1, 1), (0.5, 0.5), (1, 1), (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_contrast_error")
|
||||
|
||||
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))
|
||||
|
||||
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))
|
||||
if plot:
|
||||
visualize(c_image, mse, py_image)
|
||||
util_test_random_color_adjust_error(contrast=(0.5, 0.5))
|
||||
|
||||
|
||||
def test_random_color_adjust_op_saturation(plot=False):
|
||||
"""
|
||||
Test RandomColorAdjust op
|
||||
Test RandomColorAdjust op for saturation
|
||||
"""
|
||||
logger.info("test_random_color_adjust_op")
|
||||
logger.info("test_random_color_adjust_op_saturation")
|
||||
|
||||
# First dataset
|
||||
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||
decode_op = c_vision.Decode()
|
||||
util_test_random_color_adjust_op(saturation=(0.5, 0.5), plot=plot)
|
||||
|
||||
random_adjust_op = c_vision.RandomColorAdjust((1, 1), (1, 1), (0.5, 0.5), (0, 0))
|
||||
|
||||
ctrans = [decode_op,
|
||||
random_adjust_op
|
||||
]
|
||||
def test_random_color_adjust_op_saturation_error():
|
||||
"""
|
||||
Test RandomColorAdjust error message with saturation input in case of grayscale image
|
||||
"""
|
||||
|
||||
data1 = data1.map(input_columns=["image"], operations=ctrans)
|
||||
logger.info("test_random_color_adjust_op_saturation_error")
|
||||
|
||||
# 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())
|
||||
|
||||
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))
|
||||
|
||||
logger.info("dtype of c_image: {}".format(c_image.dtype))
|
||||
logger.info("dtype of py_image: {}".format(py_image.dtype))
|
||||
|
||||
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_error(saturation=(0.5, 0.5))
|
||||
|
||||
|
||||
def test_random_color_adjust_op_hue(plot=False):
|
||||
"""
|
||||
Test RandomColorAdjust op
|
||||
Test RandomColorAdjust op for hue
|
||||
"""
|
||||
logger.info("test_random_color_adjust_op")
|
||||
logger.info("test_random_color_adjust_op_hue")
|
||||
|
||||
# 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), (1, 1), (1, 1), (0.2, 0.2))
|
||||
|
||||
ctrans = [decode_op,
|
||||
random_adjust_op,
|
||||
]
|
||||
|
||||
data1 = data1.map(input_columns=["image"], operations=ctrans)
|
||||
|
||||
# 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())
|
||||
|
||||
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))
|
||||
|
||||
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))
|
||||
|
||||
# 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_op(hue=(0.5, 0.5), plot=plot)
|
||||
|
||||
|
||||
# pylint: disable=unnecessary-lambda
|
||||
def test_random_color_adjust_grayscale():
|
||||
def test_random_color_adjust_op_hue_error():
|
||||
"""
|
||||
Tests that the random color adjust works for grayscale images
|
||||
Test RandomColorAdjust error message with hue input in case of grayscale image
|
||||
"""
|
||||
|
||||
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)
|
||||
logger.info("test_random_color_adjust_op_hue_error")
|
||||
|
||||
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())
|
||||
|
||||
# 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)))
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue