updating python unit tests based on floating point bounding boxes + adding coco dataset in unit tests
parent
9ba937b1af
commit
421b80bf8f
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,215 @@
|
||||
# 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.
|
||||
# ==============================================================================
|
||||
"""
|
||||
Testing the random resize with bounding boxes 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, \
|
||||
config_get_set_seed, config_get_set_num_parallel_workers, save_and_check_md5
|
||||
|
||||
GENERATE_GOLDEN = False
|
||||
|
||||
DATA_DIR = "../data/dataset/testVOC2012_2"
|
||||
DATA_DIR_2 = ["../data/dataset/testCOCO/train/",
|
||||
"../data/dataset/testCOCO/annotations/train.json"] # DATA_DIR, ANNOTATION_DIR
|
||||
|
||||
|
||||
def test_random_resize_with_bbox_op_voc_c(plot_vis=False):
|
||||
"""
|
||||
Prints images and bboxes side by side with and without RandomResizeWithBBox Op applied
|
||||
testing with VOC dataset
|
||||
"""
|
||||
logger.info("test_random_resize_with_bbox_op_voc_c")
|
||||
original_seed = config_get_set_seed(123)
|
||||
original_num_parallel_workers = config_get_set_num_parallel_workers(1)
|
||||
# Load dataset
|
||||
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.RandomResizeWithBBox(100)
|
||||
|
||||
# map to apply ops
|
||||
dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"],
|
||||
output_columns=["image", "annotation"],
|
||||
columns_order=["image", "annotation"],
|
||||
operations=[test_op])
|
||||
|
||||
filename = "random_resize_with_bbox_op_01_c_voc_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_resize_with_bbox_op_rand_coco_c(plot_vis=False):
|
||||
"""
|
||||
Prints images and bboxes side by side with and without RandomResizeWithBBox Op applied,
|
||||
tests with MD5 check, expected to pass
|
||||
testing with COCO dataset
|
||||
"""
|
||||
logger.info("test_random_resize_with_bbox_op_rand_coco_c")
|
||||
original_seed = config_get_set_seed(231)
|
||||
original_num_parallel_workers = config_get_set_num_parallel_workers(1)
|
||||
|
||||
# Load dataset
|
||||
dataCoco1 = ds.CocoDataset(DATA_DIR_2[0], annotation_file=DATA_DIR_2[1], task="Detection",
|
||||
decode=True, shuffle=False)
|
||||
|
||||
dataCoco2 = ds.CocoDataset(DATA_DIR_2[0], annotation_file=DATA_DIR_2[1], task="Detection",
|
||||
decode=True, shuffle=False)
|
||||
|
||||
test_op = c_vision.RandomResizeWithBBox(200)
|
||||
|
||||
# map to apply ops
|
||||
|
||||
dataCoco2 = dataCoco2.map(input_columns=["image", "bbox"],
|
||||
output_columns=["image", "bbox"],
|
||||
columns_order=["image", "bbox"],
|
||||
operations=[test_op])
|
||||
|
||||
filename = "random_resize_with_bbox_op_01_c_coco_result.npz"
|
||||
save_and_check_md5(dataCoco2, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
unaugSamp, augSamp = [], []
|
||||
|
||||
for unAug, Aug in zip(dataCoco1.create_dict_iterator(), dataCoco2.create_dict_iterator()):
|
||||
unaugSamp.append(unAug)
|
||||
augSamp.append(Aug)
|
||||
|
||||
if plot_vis:
|
||||
visualize_with_bounding_boxes(unaugSamp, augSamp, annot_name="bbox")
|
||||
|
||||
# Restore config setting
|
||||
ds.config.set_seed(original_seed)
|
||||
ds.config.set_num_parallel_workers(original_num_parallel_workers)
|
||||
|
||||
|
||||
def test_random_resize_with_bbox_op_edge_c(plot_vis=False):
|
||||
"""
|
||||
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_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.RandomResizeWithBBox(500)
|
||||
|
||||
# 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))])
|
||||
|
||||
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()):
|
||||
unaugSamp.append(unAug)
|
||||
augSamp.append(Aug)
|
||||
|
||||
if plot_vis:
|
||||
visualize_with_bounding_boxes(unaugSamp, augSamp)
|
||||
|
||||
|
||||
def test_random_resize_with_bbox_op_invalid_c():
|
||||
"""
|
||||
Test RandomResizeWithBBox Op on invalid constructor parameters, expected to raise ValueError
|
||||
"""
|
||||
logger.info("test_random_resize_with_bbox_op_invalid_c")
|
||||
|
||||
try:
|
||||
# zero value for resize
|
||||
c_vision.RandomResizeWithBBox(0)
|
||||
|
||||
except ValueError as err:
|
||||
logger.info("Got an exception in DE: {}".format(str(err)))
|
||||
assert "Input is not" in str(err)
|
||||
|
||||
try:
|
||||
# one of the size values is zero
|
||||
c_vision.RandomResizeWithBBox((0, 100))
|
||||
|
||||
except ValueError as err:
|
||||
logger.info("Got an exception in DE: {}".format(str(err)))
|
||||
assert "Input is not" in str(err)
|
||||
|
||||
try:
|
||||
# negative value for resize
|
||||
c_vision.RandomResizeWithBBox(-10)
|
||||
|
||||
except ValueError as err:
|
||||
logger.info("Got an exception in DE: {}".format(str(err)))
|
||||
assert "Input is not" in str(err)
|
||||
|
||||
try:
|
||||
# invalid input shape
|
||||
c_vision.RandomResizeWithBBox((100, 100, 100))
|
||||
|
||||
except TypeError as err:
|
||||
logger.info("Got an exception in DE: {}".format(str(err)))
|
||||
assert "Size should be" in str(err)
|
||||
|
||||
|
||||
def test_random_resize_with_bbox_op_bad_c():
|
||||
"""
|
||||
Tests RandomResizeWithBBox Op with invalid bounding boxes, expected to catch multiple errors
|
||||
"""
|
||||
logger.info("test_random_resize_with_bbox_op_bad_c")
|
||||
test_op = c_vision.RandomResizeWithBBox((400, 300))
|
||||
|
||||
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
|
||||
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")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_random_resize_with_bbox_op_voc_c(plot_vis=False)
|
||||
test_random_resize_with_bbox_op_rand_coco_c(plot_vis=False)
|
||||
test_random_resize_with_bbox_op_edge_c(plot_vis=False)
|
||||
test_random_resize_with_bbox_op_invalid_c()
|
||||
test_random_resize_with_bbox_op_bad_c()
|
@ -0,0 +1,180 @@
|
||||
# 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.
|
||||
# ==============================================================================
|
||||
"""
|
||||
Testing the resize with bounding boxes 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, \
|
||||
save_and_check_md5
|
||||
|
||||
GENERATE_GOLDEN = False
|
||||
|
||||
DATA_DIR = "../data/dataset/testVOC2012_2"
|
||||
DATA_DIR_2 = ["../data/dataset/testCOCO/train/",
|
||||
"../data/dataset/testCOCO/annotations/train.json"] # DATA_DIR, ANNOTATION_DIR
|
||||
|
||||
|
||||
def test_resize_with_bbox_op_voc_c(plot_vis=False):
|
||||
"""
|
||||
Prints images and bboxes side by side with and without ResizeWithBBox Op applied
|
||||
testing with VOC dataset
|
||||
"""
|
||||
logger.info("test_resize_with_bbox_op_voc_c")
|
||||
|
||||
# Load dataset
|
||||
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.ResizeWithBBox(100)
|
||||
|
||||
# map to apply ops
|
||||
dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"],
|
||||
output_columns=["image", "annotation"],
|
||||
columns_order=["image", "annotation"],
|
||||
operations=[test_op])
|
||||
|
||||
filename = "resize_with_bbox_op_01_c_voc_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)
|
||||
|
||||
|
||||
def test_resize_with_bbox_op_coco_c(plot_vis=False):
|
||||
"""
|
||||
Prints images and bboxes side by side with and without ResizeWithBBox Op applied,
|
||||
tests with MD5 check, expected to pass
|
||||
Testing with COCO dataset
|
||||
"""
|
||||
logger.info("test_resize_with_bbox_op_coco_c")
|
||||
|
||||
# Load dataset
|
||||
dataCOCO1 = ds.CocoDataset(DATA_DIR_2[0], annotation_file=DATA_DIR_2[1], task="Detection",
|
||||
decode=True, shuffle=False)
|
||||
|
||||
dataCOCO2 = ds.CocoDataset(DATA_DIR_2[0], annotation_file=DATA_DIR_2[1], task="Detection",
|
||||
decode=True, shuffle=False)
|
||||
|
||||
test_op = c_vision.ResizeWithBBox(200)
|
||||
|
||||
# map to apply ops
|
||||
|
||||
dataCOCO2 = dataCOCO2.map(input_columns=["image", "bbox"],
|
||||
output_columns=["image", "bbox"],
|
||||
columns_order=["image", "bbox"],
|
||||
operations=[test_op])
|
||||
|
||||
filename = "resize_with_bbox_op_01_c_coco_result.npz"
|
||||
save_and_check_md5(dataCOCO2, filename, generate_golden=GENERATE_GOLDEN)
|
||||
|
||||
unaugSamp, augSamp = [], []
|
||||
|
||||
for unAug, Aug in zip(dataCOCO1.create_dict_iterator(), dataCOCO2.create_dict_iterator()):
|
||||
unaugSamp.append(unAug)
|
||||
augSamp.append(Aug)
|
||||
|
||||
if plot_vis:
|
||||
visualize_with_bounding_boxes(unaugSamp, augSamp, annot_name="bbox")
|
||||
|
||||
|
||||
def test_resize_with_bbox_op_edge_c(plot_vis=False):
|
||||
"""
|
||||
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
|
||||
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)
|
||||
|
||||
dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train",
|
||||
decode=True, shuffle=False)
|
||||
|
||||
test_op = c_vision.ResizeWithBBox(500)
|
||||
|
||||
# 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))])
|
||||
|
||||
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()):
|
||||
unaugSamp.append(unAug)
|
||||
augSamp.append(Aug)
|
||||
|
||||
if plot_vis:
|
||||
visualize_with_bounding_boxes(unaugSamp, augSamp)
|
||||
|
||||
|
||||
def test_resize_with_bbox_op_invalid_c():
|
||||
"""
|
||||
Test ResizeWithBBox Op on invalid constructor parameters, expected to raise ValueError
|
||||
"""
|
||||
logger.info("test_resize_with_bbox_op_invalid_c")
|
||||
|
||||
try:
|
||||
# invalid interpolation value
|
||||
c_vision.ResizeWithBBox(400, interpolation="invalid")
|
||||
|
||||
except ValueError as err:
|
||||
logger.info("Got an exception in DE: {}".format(str(err)))
|
||||
assert "interpolation" in str(err)
|
||||
|
||||
|
||||
def test_resize_with_bbox_op_bad_c():
|
||||
"""
|
||||
Tests ResizeWithBBox Op with invalid bounding boxes, expected to catch multiple errors
|
||||
"""
|
||||
logger.info("test_resize_with_bbox_op_bad_c")
|
||||
test_op = c_vision.ResizeWithBBox((200, 300))
|
||||
|
||||
data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
|
||||
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")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_resize_with_bbox_op_voc_c(plot_vis=False)
|
||||
test_resize_with_bbox_op_coco_c(plot_vis=False)
|
||||
test_resize_with_bbox_op_edge_c(plot_vis=False)
|
||||
test_resize_with_bbox_op_invalid_c()
|
||||
test_resize_with_bbox_op_bad_c()
|
Loading…
Reference in new issue