def __call__(self, image, bbox, size, gray=False, mask=None): shape = image.shape crop_bbox = center2corner( Center(shape[0] // 2, shape[1] // 2, size - 1, size - 1)) # gray augmentation if gray: image = self._gray_aug(image) # shift scale augmentation image, bbox, mask = self._shift_scale_aug(image, bbox, mask, crop_bbox, size) # mask # color augmentation if self.color > np.random.random(): image = self._color_aug(image) # blur augmentation if self.blur > np.random.random(): image = self._blur_aug(image) # flip augmentation if self.flip and self.flip > np.random.random(): image, bbox, mask = self._flip_aug(image, bbox, mask) # return image, bbox return image, bbox, mask
def _get_bbox(self, image, shape): imh, imw = image.shape[:2] if len(shape) == 4: w, h = shape[2] - shape[0], shape[3] - shape[1] else: w, h = shape context_amount = 0.5 exemplar_size = cfg.TRAIN.EXEMPLAR_SIZE wc_z = w + context_amount * (w + h) hc_z = h + context_amount * (w + h) s_z = np.sqrt(wc_z * hc_z) scale_z = exemplar_size / s_z w = w * scale_z h = h * scale_z cx, cy = imw // 2, imh // 2 bbox = center2corner(Center(cx, cy, w, h)) return bbox
def _shift_scale_aug(self, image, bbox, mask, crop_bbox, size): im_h, im_w = image.shape[:2] # adjust crop bounding box crop_bbox_center = corner2center(crop_bbox) if self.scale: scale_x = (1.0 + Augmentation.random() * self.scale) scale_y = (1.0 + Augmentation.random() * self.scale) h, w = crop_bbox_center.h, crop_bbox_center.w scale_x = min(scale_x, float(im_w) / w) scale_y = min(scale_y, float(im_h) / h) crop_bbox_center = Center(crop_bbox_center.x, crop_bbox_center.y, crop_bbox_center.w * scale_x, crop_bbox_center.h * scale_y) crop_bbox = center2corner(crop_bbox_center) if self.shift: sx = Augmentation.random() * self.shift sy = Augmentation.random() * self.shift x1, y1, x2, y2 = crop_bbox sx = max(-x1, min(im_w - 1 - x2, sx)) sy = max(-y1, min(im_h - 1 - y2, sy)) crop_bbox = Corner(x1 + sx, y1 + sy, x2 + sx, y2 + sy) # adjust target bounding box x1, y1 = crop_bbox.x1, crop_bbox.y1 bbox = Corner(bbox.x1 - x1, bbox.y1 - y1, bbox.x2 - x1, bbox.y2 - y1) if self.scale: bbox = Corner(bbox.x1 / scale_x, bbox.y1 / scale_y, bbox.x2 / scale_x, bbox.y2 / scale_y) image = self._crop_roi(image, crop_bbox, size) if not mask is None: mask = self._crop_roi(mask, crop_bbox, size) return image, bbox, mask
def generate_all_anchors(self, im_c, size): """ im_c: image center size: image size """ if self.image_center == im_c and self.size == size: return False self.image_center = im_c self.size = size a0x = im_c - size // 2 * self.stride ori = np.array([a0x] * 4, dtype=np.float32) zero_anchors = self.anchors + ori x1 = zero_anchors[:, 0] y1 = zero_anchors[:, 1] x2 = zero_anchors[:, 2] y2 = zero_anchors[:, 3] x1, y1, x2, y2 = map(lambda x: x.reshape(self.anchor_num, 1, 1), [x1, y1, x2, y2]) cx, cy, w, h = corner2center([x1, y1, x2, y2]) disp_x = np.arange(0, size).reshape(1, 1, -1) * self.stride disp_y = np.arange(0, size).reshape(1, -1, 1) * self.stride cx = cx + disp_x cy = cy + disp_y # broadcast zero = np.zeros((self.anchor_num, size, size), dtype=np.float32) cx, cy, w, h = map(lambda x: x + zero, [cx, cy, w, h]) x1, y1, x2, y2 = center2corner([cx, cy, w, h]) self.all_anchors = (np.stack([x1, y1, x2, y2]).astype(np.float32), np.stack([cx, cy, w, h]).astype(np.float32)) return True