提交 a1b17bd2 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!2593 python unit tests for randomResizeWithBBox and ResizeWithBBox

Merge pull request !2593 from ava/python_ut_tests
...@@ -15,251 +15,180 @@ ...@@ -15,251 +15,180 @@
""" """
Testing the random resize with bounding boxes op in DE Testing the random resize with bounding boxes op in DE
""" """
from enum import Enum
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np import numpy as np
import mindspore.dataset as ds import mindspore.dataset as ds
from mindspore import log as logger
import mindspore.dataset.transforms.vision.c_transforms as c_vision import mindspore.dataset.transforms.vision.c_transforms as c_vision
from mindspore import log as logger
from util import visualize_with_bounding_boxes, InvalidBBoxType, check_bad_bbox, \
config_get_set_seed, config_get_set_num_parallel_workers, save_and_check_md5
GENERATE_GOLDEN = False GENERATE_GOLDEN = False
DATA_DIR = "../data/dataset/testVOC2012" DATA_DIR = "../data/dataset/testVOC2012_2"
def fix_annotate(bboxes): def fix_annotate(bboxes):
""" """
Fix annotations to format followed by mindspore.
:param bboxes: in [label, x_min, y_min, w, h, truncate, difficult] format :param bboxes: in [label, x_min, y_min, w, h, truncate, difficult] format
:return: annotation in [x_min, y_min, w, h, label, truncate, difficult] format :return: annotation in [x_min, y_min, w, h, label, truncate, difficult] format
""" """
for bbox in bboxes: for (i, box) in enumerate(bboxes):
tmp = bbox[0] bboxes[i] = np.roll(box, -1)
bbox[0] = bbox[1]
bbox[1] = bbox[2]
bbox[2] = bbox[3]
bbox[3] = bbox[4]
bbox[4] = tmp
return bboxes return bboxes
class BoxType(Enum): def test_random_resize_with_bbox_op_rand_c(plot_vis=False):
""" """
Defines box types for test cases Prints images and bboxes side by side with and without RandomResizeWithBBox Op applied,
tests with MD5 check, expected to pass
""" """
WidthOverflow = 1 logger.info("test_random_resize_with_bbox_rand_c")
HeightOverflow = 2 original_seed = config_get_set_seed(1)
NegativeXY = 3 original_num_parallel_workers = config_get_set_num_parallel_workers(1)
OnEdge = 4
WrongShape = 5
# Load dataset
dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
decode=True, shuffle=False)
class AddBadAnnotation: # pylint: disable=too-few-public-methods dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
""" decode=True, shuffle=False)
Used to add erroneous bounding boxes to object detection pipelines.
Usage:
>>> # Adds a box that covers the whole image. Good for testing edge cases
>>> de = de.map(input_columns=["image", "annotation"],
>>> output_columns=["image", "annotation"],
>>> operations=AddBadAnnotation(BoxType.OnEdge))
"""
def __init__(self, box_type): test_op = c_vision.RandomResizeWithBBox(200)
self.box_type = box_type
dataVoc1 = dataVoc1.map(input_columns=["annotation"],
def __call__(self, img, bboxes): output_columns=["annotation"],
""" operations=fix_annotate)
Used to generate erroneous bounding box examples on given img. dataVoc2 = dataVoc2.map(input_columns=["annotation"],
:param img: image where the bounding boxes are. output_columns=["annotation"],
:param bboxes: in [x_min, y_min, w, h, label, truncate, difficult] format operations=fix_annotate)
:return: bboxes with bad examples added # map to apply ops
""" dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"],
height = img.shape[0] output_columns=["image", "annotation"],
width = img.shape[1] columns_order=["image", "annotation"],
if self.box_type == BoxType.WidthOverflow: operations=[test_op])
# use box that overflows on width
return img, np.array([[0, 0, width + 1, height - 1, 0, 0, 0]]).astype(np.uint32) filename = "random_resize_with_bbox_op_01_c_result.npz"
save_and_check_md5(dataVoc2, filename, generate_golden=GENERATE_GOLDEN)
if self.box_type == BoxType.HeightOverflow:
# use box that overflows on height unaugSamp, augSamp = [], []
return img, np.array([[0, 0, width - 1, height + 1, 0, 0, 0]]).astype(np.uint32)
for unAug, Aug in zip(dataVoc1.create_dict_iterator(), dataVoc2.create_dict_iterator()):
if self.box_type == BoxType.NegativeXY: unaugSamp.append(unAug)
# use box with negative xy augSamp.append(Aug)
return img, np.array([[-10, -10, width - 1, height - 1, 0, 0, 0]]).astype(np.uint32)
if plot_vis:
if self.box_type == BoxType.OnEdge: visualize_with_bounding_boxes(unaugSamp, augSamp)
# use box that covers the whole image
return img, np.array([[0, 0, width - 1, height - 1, 0, 0, 0]]).astype(np.uint32) # Restore config setting
ds.config.set_seed(original_seed)
if self.box_type == BoxType.WrongShape: ds.config.set_num_parallel_workers(original_num_parallel_workers)
# use box that covers the whole image
return img, np.array([[0, 0, width - 1]]).astype(np.uint32)
return img, bboxes def test_random_resize_with_bbox_op_edge_c(plot_vis=False):
def check_bad_box(data, box_type, expected_error):
try:
test_op = c_vision.RandomResizeWithBBox(100) # DEFINE TEST OP HERE -- (PROB 1 IN CASE OF RANDOM)
data = data.map(input_columns=["annotation"],
output_columns=["annotation"],
operations=fix_annotate)
# map to use width overflow
data = data.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=AddBadAnnotation(box_type)) # Add column for "annotation"
# map to apply ops
data = data.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()):
break
except RuntimeError as e:
logger.info("Got an exception in DE: {}".format(str(e)))
assert expected_error in str(e)
def add_bounding_boxes(axis, bboxes):
"""
:param axis: axis to modify
:param bboxes: bounding boxes to draw on the axis
:return: None
"""
for bbox in bboxes:
rect = patches.Rectangle((bbox[0], bbox[1]),
bbox[2], bbox[3],
linewidth=1, edgecolor='r', facecolor='none')
# Add the patch to the Axes
axis.add_patch(rect)
def visualize(unaugmented_data, augment_data):
for idx, (un_aug_item, aug_item) in \
enumerate(zip(unaugmented_data.create_dict_iterator(), augment_data.create_dict_iterator())):
axis = plt.subplot(141)
plt.imshow(un_aug_item["image"])
add_bounding_boxes(axis, un_aug_item["annotation"]) # add Orig BBoxes
plt.title("Original" + str(idx + 1))
logger.info("Original ", str(idx + 1), " :", un_aug_item["annotation"])
axis = plt.subplot(142)
plt.imshow(aug_item["image"])
add_bounding_boxes(axis, aug_item["annotation"]) # add AugBBoxes
plt.title("Augmented" + str(idx + 1))
logger.info("Augmented ", str(idx + 1), " ", aug_item["annotation"], "\n")
plt.show()
def test_random_resize_with_bbox_op(plot=False):
""" """
Test random_resize_with_bbox_op Prints images and bboxes side by side with and without RandomresizeWithBBox Op applied,
applied on dynamically generated edge case, expected to pass. edge case is when bounding
box has dimensions as the image itself.
""" """
logger.info("Test random resize with bbox") logger.info("test_random_resize_with_bbox_op_edge_c")
dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
decode=True, shuffle=False)
# original images dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
data_original = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) decode=True, shuffle=False)
# augmented images test_op = c_vision.RandomResizeWithBBox(500)
data_augmented = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
data_original = data_original.map(input_columns=["annotation"], dataVoc1 = dataVoc1.map(input_columns=["annotation"],
output_columns=["annotation"], output_columns=["annotation"],
operations=fix_annotate) operations=fix_annotate)
dataVoc2 = dataVoc2.map(input_columns=["annotation"],
output_columns=["annotation"],
operations=fix_annotate)
data_augmented = data_augmented.map(input_columns=["annotation"], # maps to convert data into valid edge case data
output_columns=["annotation"], dataVoc1 = dataVoc1.map(input_columns=["image", "annotation"],
operations=fix_annotate) output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[lambda img, bboxes: (
img, np.array([[0, 0, img.shape[1], img.shape[0]]]).astype(bboxes.dtype))])
# define map operations dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"],
test_op = c_vision.RandomResizeWithBBox(100) # input value being the target size of resizeOp output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[lambda img, bboxes: (
img, np.array([[0, 0, img.shape[1], img.shape[0]]]).astype(bboxes.dtype)), test_op])
data_augmented = data_augmented.map(input_columns=["image", "annotation"], unaugSamp, augSamp = [], []
output_columns=["image", "annotation"],
columns_order=["image", "annotation"], operations=[test_op])
if plot:
visualize(data_original, data_augmented)
for unAug, Aug in zip(dataVoc1.create_dict_iterator(), dataVoc2.create_dict_iterator()):
unaugSamp.append(unAug)
augSamp.append(Aug)
def test_random_resize_with_bbox_invalid_bounds(): if plot_vis:
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) visualize_with_bounding_boxes(unaugSamp, augSamp)
check_bad_box(data_voc2, BoxType.WidthOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_box(data_voc2, BoxType.HeightOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_box(data_voc2, BoxType.NegativeXY, "min_x")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_box(data_voc2, BoxType.WrongShape, "4 features")
def test_random_resize_with_bbox_invalid_size(): def test_random_resize_with_bbox_op_invalid_c():
"""
Test RandomResizeWithBBox Op on invalid constructor parameters, expected to raise ValueError
""" """
Test random_resize_with_bbox_op logger.info("test_random_resize_with_bbox_op_invalid_c")
"""
logger.info("Test random resize with bbox with invalid target size")
# original images try:
data = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) # zero value for resize
c_vision.RandomResizeWithBBox(0)
data = data.map(input_columns=["annotation"], except ValueError as err:
output_columns=["annotation"], logger.info("Got an exception in DE: {}".format(str(err)))
operations=fix_annotate) assert "Input is not" in str(err)
# negative target size as input
try: try:
test_op = c_vision.RandomResizeWithBBox(-10) # DEFINE TEST OP HERE -- (PROB 1 IN CASE OF RANDOM) # one of the size values is zero
c_vision.RandomResizeWithBBox((0, 100))
# map to apply ops except ValueError as err:
data = data.map(input_columns=["image", "annotation"], logger.info("Got an exception in DE: {}".format(str(err)))
output_columns=["image", "annotation"], assert "Input is not" in str(err)
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()): try:
break # negative value for resize
c_vision.RandomResizeWithBBox(-10)
except ValueError as e: except ValueError as err:
logger.info("Got an exception in DE: {}".format(str(e))) logger.info("Got an exception in DE: {}".format(str(err)))
print(e) assert "Input is not" in str(err)
assert "Input is not" in str(e)
# zero target size as input
try: try:
test_op = c_vision.RandomResizeWithBBox(0) # DEFINE TEST OP HERE -- (PROB 1 IN CASE OF RANDOM) # invalid input shape
c_vision.RandomResizeWithBBox((100, 100, 100))
# map to apply ops except TypeError as err:
data = data.map(input_columns=["image", "annotation"], logger.info("Got an exception in DE: {}".format(str(err)))
output_columns=["image", "annotation"], assert "Size should be" in str(err)
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()):
break
except ValueError as e: def test_random_resize_with_bbox_op_bad_c():
logger.info("Got an exception in DE: {}".format(str(e))) """
assert "Input is not" in str(e) Tests RandomResizeWithBBox Op with invalid bounding boxes, expected to catch multiple errors
"""
# invalid input shape logger.info("test_random_resize_with_bbox_op_bad_c")
try: test_op = c_vision.RandomResizeWithBBox((400, 300))
test_op = c_vision.RandomResizeWithBBox((10, 10, 10)) # DEFINE TEST OP HERE -- (PROB 1 IN CASE OF RANDOM)
# map to apply ops
data = data.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()): data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
break check_bad_bbox(data_voc2, test_op, InvalidBBoxType.WidthOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_bbox(data_voc2, test_op, InvalidBBoxType.HeightOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_bbox(data_voc2, test_op, InvalidBBoxType.NegativeXY, "min_x")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_bbox(data_voc2, test_op, InvalidBBoxType.WrongShape, "4 features")
except TypeError as e:
logger.info("Got an exception in DE: {}".format(str(e)))
assert "Size should be" in str(e)
if __name__ == "__main__": if __name__ == "__main__":
test_random_resize_with_bbox_op(plot=False) test_random_resize_with_bbox_op_rand_c(plot_vis=False)
test_random_resize_with_bbox_invalid_bounds() test_random_resize_with_bbox_op_edge_c(plot_vis=False)
test_random_resize_with_bbox_invalid_size() test_random_resize_with_bbox_op_invalid_c()
test_random_resize_with_bbox_op_bad_c()
...@@ -15,281 +15,151 @@ ...@@ -15,281 +15,151 @@
""" """
Testing the resize with bounding boxes op in DE Testing the resize with bounding boxes op in DE
""" """
from enum import Enum
import numpy as np import numpy as np
import matplotlib.patches as patches import mindspore.dataset as ds
import matplotlib.pyplot as plt
import mindspore.dataset.transforms.vision.c_transforms as c_vision import mindspore.dataset.transforms.vision.c_transforms as c_vision
from mindspore import log as logger from mindspore import log as logger
import mindspore.dataset as ds from util import visualize_with_bounding_boxes, InvalidBBoxType, check_bad_bbox, \
save_and_check_md5
GENERATE_GOLDEN = False GENERATE_GOLDEN = False
DATA_DIR = "../data/dataset/testVOC2012" DATA_DIR = "../data/dataset/testVOC2012_2"
def fix_annotate(bboxes): def fix_annotate(bboxes):
""" """
Fix annotations to format followed by mindspore.
:param bboxes: in [label, x_min, y_min, w, h, truncate, difficult] format :param bboxes: in [label, x_min, y_min, w, h, truncate, difficult] format
:return: annotation in [x_min, y_min, w, h, label, truncate, difficult] format :return: annotation in [x_min, y_min, w, h, label, truncate, difficult] format
""" """
for bbox in bboxes: for (i, box) in enumerate(bboxes):
tmp = bbox[0] bboxes[i] = np.roll(box, -1)
bbox[0] = bbox[1]
bbox[1] = bbox[2]
bbox[2] = bbox[3]
bbox[3] = bbox[4]
bbox[4] = tmp
return bboxes return bboxes
class BoxType(Enum): def test_resize_with_bbox_op_c(plot_vis=False):
""" """
Defines box types for test cases Prints images and bboxes side by side with and without ResizeWithBBox Op applied,
tests with MD5 check, expected to pass
""" """
WidthOverflow = 1 logger.info("test_resize_with_bbox_op_c")
HeightOverflow = 2
NegativeXY = 3
OnEdge = 4
WrongShape = 5
# Load dataset
dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
decode=True, shuffle=False)
class AddBadAnnotation: # pylint: disable=too-few-public-methods dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
""" decode=True, shuffle=False)
Used to add erroneous bounding boxes to object detection pipelines.
Usage:
>>> # Adds a box that covers the whole image. Good for testing edge cases
>>> de = de.map(input_columns=["image", "annotation"],
>>> output_columns=["image", "annotation"],
>>> operations=AddBadAnnotation(BoxType.OnEdge))
"""
def __init__(self, box_type): test_op = c_vision.ResizeWithBBox(200)
self.box_type = box_type
def __call__(self, img, bboxes):
"""
Used to generate erroneous bounding box examples on given img.
:param img: image where the bounding boxes are.
:param bboxes: in [x_min, y_min, w, h, label, truncate, difficult] format
:return: bboxes with bad examples added
"""
height = img.shape[0]
width = img.shape[1]
if self.box_type == BoxType.WidthOverflow:
# use box that overflows on width
return img, np.array([[0, 0, width + 1, height - 1, 0, 0, 0]]).astype(np.uint32)
if self.box_type == BoxType.HeightOverflow:
# use box that overflows on height
return img, np.array([[0, 0, width - 1, height + 1, 0, 0, 0]]).astype(np.uint32)
if self.box_type == BoxType.NegativeXY:
# use box with negative xy
return img, np.array([[-10, -10, width - 1, height - 1, 0, 0, 0]]).astype(np.uint32)
if self.box_type == BoxType.OnEdge:
# use box that covers the whole image
return img, np.array([[0, 0, width - 1, height - 1, 0, 0, 0]]).astype(np.uint32)
if self.box_type == BoxType.WrongShape:
# use box that covers the whole image
return img, np.array([[0, 0, width - 1]]).astype(np.uint32)
return img, bboxes
def check_bad_box(data, box_type, expected_error):
try:
test_op = c_vision.ResizeWithBBox(100)
data = data.map(input_columns=["annotation"],
output_columns=["annotation"],
operations=fix_annotate)
# map to use width overflow
data = data.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=AddBadAnnotation(box_type)) # Add column for "annotation"
# map to apply ops
data = data.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()):
break
except RuntimeError as e:
logger.info("Got an exception in DE: {}".format(str(e)))
assert expected_error in str(e)
def add_bounding_boxes(axis, bboxes):
"""
:param axis: axis to modify
:param bboxes: bounding boxes to draw on the axis
:return: None
"""
for bbox in bboxes:
rect = patches.Rectangle((bbox[0], bbox[1]),
bbox[2], bbox[3],
linewidth=1, edgecolor='r', facecolor='none')
# Add the patch to the Axes
axis.add_patch(rect)
def visualize(unaugmented_data, augment_data):
for idx, (un_aug_item, aug_item) in enumerate(
zip(unaugmented_data.create_dict_iterator(), augment_data.create_dict_iterator())):
axis = plt.subplot(141)
plt.imshow(un_aug_item["image"])
add_bounding_boxes(axis, un_aug_item["annotation"]) # add Orig BBoxes
plt.title("Original" + str(idx + 1))
logger.info("Original ", str(idx + 1), " :", un_aug_item["annotation"])
axis = plt.subplot(142)
plt.imshow(aug_item["image"])
add_bounding_boxes(axis, aug_item["annotation"]) # add AugBBoxes
plt.title("Augmented" + str(idx + 1))
logger.info("Augmented ", str(idx + 1), " ", aug_item["annotation"], "\n")
plt.show()
def test_resize_with_bbox_op(plot=False):
"""
Test resize_with_bbox_op
"""
logger.info("Test resize with bbox")
# original images
data_original = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
# augmented images
data_augmented = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
data_original = data_original.map(input_columns=["annotation"],
output_columns=["annotation"],
operations=fix_annotate)
data_augmented = data_augmented.map(input_columns=["annotation"], dataVoc1 = dataVoc1.map(input_columns=["annotation"],
output_columns=["annotation"], output_columns=["annotation"],
operations=fix_annotate) operations=fix_annotate)
dataVoc2 = dataVoc2.map(input_columns=["annotation"],
output_columns=["annotation"],
operations=fix_annotate)
# map to apply ops
dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[test_op])
# define map operations filename = "resize_with_bbox_op_01_c_result.npz"
test_op = c_vision.ResizeWithBBox(100) # input value being the target size of resizeOp save_and_check_md5(dataVoc2, filename, generate_golden=GENERATE_GOLDEN)
data_augmented = data_augmented.map(input_columns=["image", "annotation"], unaugSamp, augSamp = [], []
output_columns=["image", "annotation"],
columns_order=["image", "annotation"], operations=[test_op])
if plot:
visualize(data_original, data_augmented)
for unAug, Aug in zip(dataVoc1.create_dict_iterator(), dataVoc2.create_dict_iterator()):
unaugSamp.append(unAug)
augSamp.append(Aug)
def test_resize_with_bbox_invalid_bounds(): if plot_vis:
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) visualize_with_bounding_boxes(unaugSamp, augSamp)
check_bad_box(data_voc2, BoxType.WidthOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_box(data_voc2, BoxType.HeightOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_box(data_voc2, BoxType.NegativeXY, "min_x")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_box(data_voc2, BoxType.WrongShape, "4 features")
def test_resize_with_bbox_invalid_size(): def test_resize_with_bbox_op_edge_c(plot_vis=False):
""" """
Test resize_with_bbox_op Prints images and bboxes side by side with and without ResizeWithBBox Op applied,
""" applied on dynamically generated edge case, expected to pass. edge case is when bounding
logger.info("Test resize with bbox with invalid target size") box has dimensions as the image itself.
"""
logger.info("test_resize_with_bbox_op_edge_c")
dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
decode=True, shuffle=False)
# original images dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
data = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) decode=True, shuffle=False)
data = data.map(input_columns=["annotation"], test_op = c_vision.ResizeWithBBox(500)
output_columns=["annotation"],
operations=fix_annotate)
# negative target size as input dataVoc1 = dataVoc1.map(input_columns=["annotation"],
try: output_columns=["annotation"],
test_op = c_vision.ResizeWithBBox(-10) operations=fix_annotate)
dataVoc2 = dataVoc2.map(input_columns=["annotation"],
output_columns=["annotation"],
operations=fix_annotate)
# map to apply ops # maps to convert data into valid edge case data
data = data.map(input_columns=["image", "annotation"], dataVoc1 = dataVoc1.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"], output_columns=["image", "annotation"],
columns_order=["image", "annotation"], columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation" operations=[lambda img, bboxes: (
img, np.array([[0, 0, img.shape[1], img.shape[0]]]).astype(bboxes.dtype))])
for _, _ in enumerate(data.create_dict_iterator()): # Test Op added to list of Operations here
break dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[lambda img, bboxes: (
img, np.array([[0, 0, img.shape[1], img.shape[0]]]).astype(bboxes.dtype)), test_op])
except ValueError as e: unaugSamp, augSamp = [], []
logger.info("Got an exception in DE: {}".format(str(e)))
assert "Input is not" in str(e)
# zero target size as input for unAug, Aug in zip(dataVoc1.create_dict_iterator(), dataVoc2.create_dict_iterator()):
try: unaugSamp.append(unAug)
test_op = c_vision.ResizeWithBBox(0) augSamp.append(Aug)
# map to apply ops if plot_vis:
data = data.map(input_columns=["image", "annotation"], visualize_with_bounding_boxes(unaugSamp, augSamp)
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()):
break
except ValueError as e: def test_resize_with_bbox_op_invalid_c():
logger.info("Got an exception in DE: {}".format(str(e))) """
assert "Input is not" in str(e) Test ResizeWithBBox Op on invalid constructor parameters, expected to raise ValueError
"""
logger.info("test_resize_with_bbox_op_invalid_c")
# invalid input shape
try: try:
test_op = c_vision.ResizeWithBBox((10, 10, 10)) # invalid interpolation value
c_vision.ResizeWithBBox(400, interpolation="invalid")
# map to apply ops except ValueError as err:
data = data.map(input_columns=["image", "annotation"], logger.info("Got an exception in DE: {}".format(str(err)))
output_columns=["image", "annotation"], assert "interpolation" in str(err)
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()):
break
except TypeError as e: def test_resize_with_bbox_op_bad_c():
logger.info("Got an exception in DE: {}".format(str(e)))
assert "Size should be" in str(e)
def test_resize_with_bbox_invalid_interpolation():
""" """
Test resize_with_bbox_op Tests ResizeWithBBox Op with invalid bounding boxes, expected to catch multiple errors
""" """
logger.info("Test resize with bbox with invalid interpolation size") logger.info("test_resize_with_bbox_op_bad_c")
test_op = c_vision.ResizeWithBBox((200, 300))
# original images
data = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
data = data.map(input_columns=["annotation"],
output_columns=["annotation"],
operations=fix_annotate)
# invalid interpolation
try:
test_op = c_vision.ResizeWithBBox(100, interpolation="invalid")
# map to apply ops
data = data.map(input_columns=["image", "annotation"],
output_columns=["image", "annotation"],
columns_order=["image", "annotation"],
operations=[test_op]) # Add column for "annotation"
for _, _ in enumerate(data.create_dict_iterator()): data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
break check_bad_bbox(data_voc2, test_op, InvalidBBoxType.WidthOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_bbox(data_voc2, test_op, InvalidBBoxType.HeightOverflow, "bounding boxes is out of bounds of the image")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_bbox(data_voc2, test_op, InvalidBBoxType.NegativeXY, "min_x")
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
check_bad_bbox(data_voc2, test_op, InvalidBBoxType.WrongShape, "4 features")
except ValueError as e:
logger.info("Got an exception in DE: {}".format(str(e)))
assert "interpolation" in str(e)
if __name__ == "__main__": if __name__ == "__main__":
test_resize_with_bbox_op(plot=False) test_resize_with_bbox_op_c(plot_vis=False)
test_resize_with_bbox_invalid_bounds() test_resize_with_bbox_op_edge_c(plot_vis=False)
test_resize_with_bbox_invalid_size() test_resize_with_bbox_op_invalid_c()
test_resize_with_bbox_invalid_interpolation() test_resize_with_bbox_op_bad_c()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册