|
|
|
@ -13,14 +13,17 @@
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
"""
|
|
|
|
|
Testing RandomVerticalFlipWithBBox op
|
|
|
|
|
Testing RandomVerticalFlipWithBBox op in DE
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import mindspore.dataset as ds
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# updated VOC dataset with correct annotations
|
|
|
|
|
DATA_DIR = "../data/dataset/testVOC2012_2"
|
|
|
|
@ -28,10 +31,9 @@ DATA_DIR = "../data/dataset/testVOC2012_2"
|
|
|
|
|
|
|
|
|
|
def fix_annotate(bboxes):
|
|
|
|
|
"""
|
|
|
|
|
Update Current VOC dataset format to Proposed HQ BBox format
|
|
|
|
|
|
|
|
|
|
:param bboxes: as [label, x_min, y_min, w, h, truncate, difficult]
|
|
|
|
|
:return: annotation as [x_min, y_min, w, h, label, truncate, difficult]
|
|
|
|
|
Fix annotations to format followed by mindspore.
|
|
|
|
|
: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
|
|
|
|
|
"""
|
|
|
|
|
for bbox in bboxes:
|
|
|
|
|
tmp = bbox[0]
|
|
|
|
@ -45,9 +47,9 @@ def fix_annotate(bboxes):
|
|
|
|
|
|
|
|
|
|
def test_random_vertical_flip_with_bbox_op_c(plot_vis=False):
|
|
|
|
|
"""
|
|
|
|
|
Prints images side by side with and without Aug applied + bboxes to
|
|
|
|
|
compare and test
|
|
|
|
|
Prints images and bboxes side by side with and without RandomVerticalFlipWithBBox Op applied
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_random_vertical_flip_with_bbox_op_c")
|
|
|
|
|
# Load dataset
|
|
|
|
|
dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
|
|
|
|
|
decode=True, shuffle=False)
|
|
|
|
@ -57,7 +59,6 @@ def test_random_vertical_flip_with_bbox_op_c(plot_vis=False):
|
|
|
|
|
|
|
|
|
|
test_op = c_vision.RandomVerticalFlipWithBBox(1)
|
|
|
|
|
|
|
|
|
|
# maps to fix annotations to HQ standard
|
|
|
|
|
dataVoc1 = dataVoc1.map(input_columns=["annotation"],
|
|
|
|
|
output_columns=["annotation"],
|
|
|
|
|
operations=fix_annotate)
|
|
|
|
@ -82,9 +83,12 @@ def test_random_vertical_flip_with_bbox_op_c(plot_vis=False):
|
|
|
|
|
|
|
|
|
|
def test_random_vertical_flip_with_bbox_op_rand_c(plot_vis=False):
|
|
|
|
|
"""
|
|
|
|
|
Prints images side by side with and without Aug applied + bboxes to
|
|
|
|
|
compare and test
|
|
|
|
|
Prints images and bboxes side by side with and without RandomVerticalFlipWithBBox Op applied,
|
|
|
|
|
tests with MD5 check, expected to pass
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_random_vertical_flip_with_bbox_op_rand_c")
|
|
|
|
|
original_seed = config_get_set_seed(29847)
|
|
|
|
|
original_num_parallel_workers = config_get_set_num_parallel_workers(1)
|
|
|
|
|
|
|
|
|
|
# Load dataset
|
|
|
|
|
dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
|
|
|
|
@ -93,9 +97,8 @@ def test_random_vertical_flip_with_bbox_op_rand_c(plot_vis=False):
|
|
|
|
|
dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
|
|
|
|
|
decode=True, shuffle=False)
|
|
|
|
|
|
|
|
|
|
test_op = c_vision.RandomVerticalFlipWithBBox(0.6)
|
|
|
|
|
test_op = c_vision.RandomVerticalFlipWithBBox(0.8)
|
|
|
|
|
|
|
|
|
|
# maps to fix annotations to HQ standard
|
|
|
|
|
dataVoc1 = dataVoc1.map(input_columns=["annotation"],
|
|
|
|
|
output_columns=["annotation"],
|
|
|
|
|
operations=fix_annotate)
|
|
|
|
@ -108,6 +111,56 @@ def test_random_vertical_flip_with_bbox_op_rand_c(plot_vis=False):
|
|
|
|
|
columns_order=["image", "annotation"],
|
|
|
|
|
operations=[test_op])
|
|
|
|
|
|
|
|
|
|
filename = "random_vertical_flip_with_bbox_01_c_result.npz"
|
|
|
|
|
save_and_check_md5(dataVoc2, filename, generate_golden=GENERATE_GOLDEN)
|
|
|
|
|
|
|
|
|
|
unaugSamp, augSamp = [], []
|
|
|
|
|
|
|
|
|
|
for unAug, Aug in zip(dataVoc1.create_dict_iterator(), dataVoc2.create_dict_iterator()):
|
|
|
|
|
unaugSamp.append(unAug)
|
|
|
|
|
augSamp.append(Aug)
|
|
|
|
|
|
|
|
|
|
if plot_vis:
|
|
|
|
|
visualize_with_bounding_boxes(unaugSamp, augSamp)
|
|
|
|
|
|
|
|
|
|
# Restore config setting
|
|
|
|
|
ds.config.set_seed(original_seed)
|
|
|
|
|
ds.config.set_num_parallel_workers(original_num_parallel_workers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_random_vertical_flip_with_bbox_op_edge_c(plot_vis=False):
|
|
|
|
|
"""
|
|
|
|
|
Prints images and bboxes side by side with and without RandomVerticalFlipWithBBox Op applied,
|
|
|
|
|
applied on dynamically generated edge case, expected to pass
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_random_vertical_flip_with_bbox_op_edge_c")
|
|
|
|
|
dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
|
|
|
|
|
decode=True, shuffle=False)
|
|
|
|
|
|
|
|
|
|
dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
|
|
|
|
|
decode=True, shuffle=False)
|
|
|
|
|
|
|
|
|
|
test_op = c_vision.RandomVerticalFlipWithBBox(1)
|
|
|
|
|
|
|
|
|
|
dataVoc1 = dataVoc1.map(input_columns=["annotation"],
|
|
|
|
|
output_columns=["annotation"],
|
|
|
|
|
operations=fix_annotate)
|
|
|
|
|
dataVoc2 = dataVoc2.map(input_columns=["annotation"],
|
|
|
|
|
output_columns=["annotation"],
|
|
|
|
|
operations=fix_annotate)
|
|
|
|
|
|
|
|
|
|
# maps to convert data into valid edge case data
|
|
|
|
|
dataVoc1 = dataVoc1.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 added to list of Operations here
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
unaugSamp, augSamp = [], []
|
|
|
|
|
|
|
|
|
|
for unAug, Aug in zip(dataVoc1.create_dict_iterator(), dataVoc2.create_dict_iterator()):
|
|
|
|
@ -119,16 +172,15 @@ def test_random_vertical_flip_with_bbox_op_rand_c(plot_vis=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_random_vertical_flip_with_bbox_op_invalid_c():
|
|
|
|
|
# Should Fail
|
|
|
|
|
# Load dataset
|
|
|
|
|
"""
|
|
|
|
|
Test RandomVerticalFlipWithBBox Op on invalid constructor parameters, expected to raise ValueError
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_random_vertical_flip_with_bbox_op_invalid_c")
|
|
|
|
|
dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
|
|
|
|
|
decode=True, shuffle=False)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
test_op = c_vision.RandomVerticalFlipWithBBox(2)
|
|
|
|
|
|
|
|
|
|
# maps to fix annotations to HQ standard
|
|
|
|
|
|
|
|
|
|
dataVoc2 = dataVoc2.map(input_columns=["annotation"],
|
|
|
|
|
output_columns=["annotation"],
|
|
|
|
|
operations=fix_annotate)
|
|
|
|
@ -148,9 +200,9 @@ def test_random_vertical_flip_with_bbox_op_invalid_c():
|
|
|
|
|
|
|
|
|
|
def test_random_vertical_flip_with_bbox_op_bad_c():
|
|
|
|
|
"""
|
|
|
|
|
Test RandomHorizontalFlipWithBBox op with invalid bounding boxes
|
|
|
|
|
Tests RandomVerticalFlipWithBBox Op with invalid bounding boxes, expected to catch multiple errors
|
|
|
|
|
"""
|
|
|
|
|
logger.info("test_random_horizontal_bbox_invalid_bounds_c")
|
|
|
|
|
logger.info("test_random_vertical_flip_with_bbox_op_bad_c")
|
|
|
|
|
test_op = c_vision.RandomVerticalFlipWithBBox(1)
|
|
|
|
|
|
|
|
|
|
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
|
|
|
|
@ -166,5 +218,6 @@ def test_random_vertical_flip_with_bbox_op_bad_c():
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
test_random_vertical_flip_with_bbox_op_c(plot_vis=True)
|
|
|
|
|
test_random_vertical_flip_with_bbox_op_rand_c(plot_vis=True)
|
|
|
|
|
test_random_vertical_flip_with_bbox_op_edge_c(plot_vis=True)
|
|
|
|
|
test_random_vertical_flip_with_bbox_op_invalid_c()
|
|
|
|
|
test_random_vertical_flip_with_bbox_op_bad_c()
|
|
|
|
|