@ -15,15 +15,12 @@
"""
Testing RandomVerticalFlipWithBBox op
"""
import numpy as np
import matplotlib . pyplot as plt
import matplotlib . patches as patches
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
# updated VOC dataset with correct annotations
DATA_DIR = " ../data/dataset/testVOC2012_2 "
@ -46,106 +43,11 @@ def fix_annotate(bboxes):
return bboxes
def add_bounding_boxes ( ax , bboxes ) :
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
ax . add_patch ( rect )
def vis_check ( orig , aug ) :
if not isinstance ( orig , list ) or not isinstance ( aug , list ) :
return False
if len ( orig ) != len ( aug ) :
return False
return True
def visualize ( orig , aug ) :
if not vis_check ( orig , aug ) :
return
plotrows = 3
compset = int ( len ( orig ) / plotrows )
orig , aug = np . array ( orig ) , np . array ( aug )
orig = np . split ( orig [ : compset * plotrows ] , compset ) + [ orig [ compset * plotrows : ] ]
aug = np . split ( aug [ : compset * plotrows ] , compset ) + [ aug [ compset * plotrows : ] ]
for ix , allData in enumerate ( zip ( orig , aug ) ) :
base_ix = ix * plotrows # will signal what base level we're on
fig , axs = plt . subplots ( len ( allData [ 0 ] ) , 2 )
fig . tight_layout ( pad = 1.5 )
for x , ( dataA , dataB ) in enumerate ( zip ( allData [ 0 ] , allData [ 1 ] ) ) :
cur_ix = base_ix + x
axs [ x , 0 ] . imshow ( dataA [ " image " ] )
add_bounding_boxes ( axs [ x , 0 ] , dataA [ " annotation " ] )
axs [ x , 0 ] . title . set_text ( " Original " + str ( cur_ix + 1 ) )
print ( " Original ** \n " , str ( cur_ix + 1 ) , " : " , dataA [ " annotation " ] )
axs [ x , 1 ] . imshow ( dataB [ " image " ] )
add_bounding_boxes ( axs [ x , 1 ] , dataB [ " annotation " ] )
axs [ x , 1 ] . title . set_text ( " Augmented " + str ( cur_ix + 1 ) )
print ( " Augmented ** \n " , str ( cur_ix + 1 ) , " " , dataB [ " annotation " ] , " \n " )
plt . show ( )
# Functions to pass to Gen for creating invalid bounding boxes
def gen_bad_bbox_neg_xy ( im , bbox ) :
im_h , im_w = im . shape [ 0 ] , im . shape [ 1 ]
bbox [ 0 ] [ : 4 ] = [ - 50 , - 50 , im_w - 10 , im_h - 10 ]
return im , bbox
def gen_bad_bbox_overflow_width ( im , bbox ) :
im_h , im_w = im . shape [ 0 ] , im . shape [ 1 ]
bbox [ 0 ] [ : 4 ] = [ 0 , 0 , im_w + 10 , im_h - 10 ]
return im , bbox
def gen_bad_bbox_overflow_height ( im , bbox ) :
im_h , im_w = im . shape [ 0 ] , im . shape [ 1 ]
bbox [ 0 ] [ : 4 ] = [ 0 , 0 , im_w - 10 , im_h + 10 ]
return im , bbox
def gen_bad_bbox_wrong_shape ( im , bbox ) :
bbox = np . array ( [ [ 0 , 0 , 0 ] ] ) . astype ( bbox . dtype )
return im , bbox
badGenFuncs = [ gen_bad_bbox_neg_xy ,
gen_bad_bbox_overflow_width ,
gen_bad_bbox_overflow_height ,
gen_bad_bbox_wrong_shape ]
assertVal = [ " min_x " ,
" is out of bounds of the image " ,
" is out of bounds of the image " ,
" 4 features " ]
# Gen Edge case BBox
def gen_bbox_edge ( im , bbox ) :
im_h , im_w = im . shape [ 0 ] , im . shape [ 1 ]
bbox [ 0 ] [ : 4 ] = [ 0 , 0 , im_w , im_h ]
return im , bbox
def c_random_vertical_flip_with_bbox_op ( plot_vis = False ) :
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
"""
# Load dataset
dataVoc1 = ds . VOCDataset ( DATA_DIR , task = " Detection " , mode = " train " ,
decode = True , shuffle = False )
@ -175,10 +77,10 @@ def c_random_vertical_flip_with_bbox_op(plot_vis=False):
augSamp . append ( Aug )
if plot_vis :
visualize ( unaugSamp , augSamp )
visualize _with_bounding_boxes ( unaugSamp , augSamp )
def c_random_vertical_flip_with_bbox_op_rand ( 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
@ -213,54 +115,12 @@ def c_random_vertical_flip_with_bbox_op_rand(plot_vis=False):
augSamp . append ( Aug )
if plot_vis :
visualize ( unaugSamp , augSamp )
def c_random_vertical_flip_with_bbox_op_edge ( plot_vis = False ) :
# Should Pass
# 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 . RandomVerticalFlipWithBBox ( 0.6 )
# maps to fix annotations to HQ standard
dataVoc1 = dataVoc1 . map ( input_columns = [ " annotation " ] ,
output_columns = [ " annotation " ] ,
operations = fix_annotate )
dataVoc2 = dataVoc2 . map ( input_columns = [ " annotation " ] ,
output_columns = [ " annotation " ] ,
operations = fix_annotate )
# Modify BBoxes to serve as valid edge cases
dataVoc2 = dataVoc2 . map ( input_columns = [ " image " , " annotation " ] ,
output_columns = [ " image " , " annotation " ] ,
columns_order = [ " image " , " annotation " ] ,
operations = [ gen_bbox_edge ] )
# map to apply ops
dataVoc2 = dataVoc2 . map ( input_columns = [ " image " , " annotation " ] ,
output_columns = [ " image " , " annotation " ] ,
columns_order = [ " image " , " annotation " ] ,
operations = [ 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 ( unaugSamp , augSamp )
visualize_with_bounding_boxes ( unaugSamp , augSamp )
def c_random_vertical_flip_with_bbox_op_invalid ( ) :
def test_random_vertical_flip_with_bbox_op_invalid_c ( ) :
# Should Fail
# Load dataset
dataVoc2 = ds . VOCDataset ( DATA_DIR , task = " Detection " , mode = " train " ,
decode = True , shuffle = False )
@ -286,41 +146,25 @@ def c_random_vertical_flip_with_bbox_op_invalid():
assert " Input is not " in str ( err )
def c_random_vertical_flip_with_bbox_op_bad ( ) :
# Should Fail - Errors logged to logger
for ix , badFunc in enumerate ( badGenFuncs ) :
try :
dataVoc2 = ds . VOCDataset ( DATA_DIR , task = " Detection " , mode = " train " ,
decode = True , shuffle = False )
def test_random_vertical_flip_with_bbox_op_bad_c ( ) :
"""
Test RandomHorizontalFlipWithBBox op with invalid bounding boxes
"""
logger . info ( " test_random_horizontal_bbox_invalid_bounds_c " )
test_op = c_vision . RandomVerticalFlipWithBBox ( 1 )
dataVoc2 = dataVoc2 . map ( input_columns = [ " annotation " ] ,
output_columns = [ " annotation " ] ,
operations = fix_annotate )
dataVoc2 = dataVoc2 . map ( input_columns = [ " image " , " annotation " ] ,
output_columns = [ " image " , " annotation " ] ,
columns_order = [ " image " , " annotation " ] ,
operations = [ badFunc ] )
# map to apply ops
dataVoc2 = dataVoc2 . map ( input_columns = [ " image " , " annotation " ] ,
output_columns = [ " image " , " annotation " ] ,
columns_order = [ " image " , " annotation " ] ,
operations = [ test_op ] )
for _ in dataVoc2 . create_dict_iterator ( ) :
break # first sample will cause exception
except RuntimeError as err :
logger . info ( " Got an exception in DE: {} " . format ( str ( err ) ) )
assert assertVal [ ix ] in str ( err )
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__ " :
c_random_vertical_flip_with_bbox_op ( False )
c_random_vertical_flip_with_bbox_op_rand ( False )
c_random_vertical_flip_with_bbox_op_edge ( False )
c_random_vertical_flip_with_bbox_op_invalid ( )
c_random_vertical_flip_with_bbox_op_bad ( )
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_invalid_c ( )
test_random_vertical_flip_with_bbox_op_bad_c ( )