in evaluation/tiny_benchmark/maskrcnn_benchmark/data/transforms/transforms.py [0:0]
def __call__(self, image: Image.Image, target: BoxList):
"""
1. image gt box size is (s1 <= size <=s2), we choose a scale [s] r.s.t uniform(bbox_size_range[0]/s1, bbox_size_range[1]/s2)
to make sure all gt box's size in image is: bbox_size_range[0] <= s * s1 <= s * s2 <= bbox_size_range[1].
2. cal new_image's width and height respect tp scale [s].
3. set origin image set axi(left-top is (0, 0)), choose a crop(new_image_x1, new_image_y1, new_image_x2, new_image_y2)
respect ro new_image's width and height and include a gt box at least.
4. move and crop annotation
:param image:
:param target:
:return:
"""
if self.info_collector is not None:
self._analysis_info = {'function_call':
{'deal_background': 0, 'crop_constraint_type=="all"': 0,
'crop_constraint_type=="one"': 0,
'scale_constraint_type=="all"': 0, 'scale_constraint_type=="one"': 0,
'scale_constraint_type=="mean"': 0, 'success': 0,
'crop_no_scale_constraint': 0},
'statistic':
{'count(<max_size)': 0}
}
if not self.set_random_seed:
seed = int(time.time() + os.getpid())
np.random.seed(seed)
self.set_random_seed = True
# print('image w, h', image.width, image.height)
if np.random.uniform(0, 1) > self.transform_prob: # whether use expand
return self.deal_background(image, target)
old_image, old_target = copy.deepcopy(image), copy.deepcopy(target)
if True:
# try:
# 1. filter ignore and too big gt out, just to choose a scale and crop, final return will keep it.
# TODO: should be replace with other policy to remove most ignore.
boxes = target.bbox.cpu().numpy()
# non_ignore_boxes = np.all([boxes[:, 2] - boxes[:, 0] < self.MAX_GT_WH,
# boxes[:, 3] - boxes[:, 1] < self.MAX_GT_WH], axis=0)
# boxes = boxes[non_ignore_boxes]
# if len(boxes) == 0:
# logger("BG1, no non-ignore gt boxes found, boxes is {}".format(target.bbox.cpu().numpy()), 'DEBUG')
# return self.deal_background(old_image, old_target)
# 2. choose boxes as constraint
boxes = self.choose_boxes(boxes)
# 3. choose a scale and a crop r.s.t the scale
if self.constraint_auto: # constraint_auto will try ['all', 'mean', 'one'] one by one.
for constraint_type in ['all', 'mean', 'one']:
scale = self.choose_scale(boxes, constraint_type)
if scale is not None:
break
else:
scale = self.choose_scale(boxes, self.scale_constraint_type)
if scale is None:
return self.deal_background(old_image, old_target)
# 4. choose a crop
if self.crop_size_before_scale is None:
crop_w_before_scale, crop_h_before_scale = image.width, image.height
else:
crop_w_before_scale, crop_h_before_scale = self.crop_size_before_scale
if self.constraint_auto:
for constrain_type in ['all', 'one']:
crop = self.choose_crop(crop_w_before_scale, crop_h_before_scale, image.width, image.height,
scale, boxes, constrain_type)
if crop is not None:
break
else:
crop = self.choose_crop(crop_w_before_scale, crop_h_before_scale, image.width, image.height,
scale, boxes, self.crop_constrain_type) # crop can out of origin image
if crop is None:
return self.deal_background(old_image, old_target)
# print(scale, crop, crop[2]-crop[0], crop[3]-crop[1])
# 5. crop bbox and image r.s.t choose crop
# if scale < 1, we can scale image fist and then crop to speed up
image = self.crop_image(image, crop, scale if scale < 1 else None)
target = self.crop_bbox(target, crop, image.size)
# 6. scale image and bbox
need_scale = scale > 1
image_size = (np.array(image.size) * scale).astype(np.int32)
if image_size[0] > self.max_crop_size[0]: # for int get bigger input, like max_crop_size + 1
image_size[0] = self.max_crop_size[0]
need_scale = True
if image_size[1] > self.max_crop_size[1]:
image_size[1] = self.max_crop_size[1]
need_scale = True
if need_scale:
image = image.resize(image_size)
target.resize(image.size)
# except BaseException as e:
# # print(e)
# # warnings.warn("exception happened which should not happened, may be some bug in code.")
# # raise e
# return self.deal_background(old_image, old_target)
# print(crop[2]-crop[0], crop[3]-crop[1], image.size)
if self.info_collector is not None:
self._analysis_info['function_call']['success'] += 1
self.info_collector(self._analysis_info)
return image, target