def transform(self, Xb, yb): Xb, yb = super(AffineTransformPtsBatchIteratorMixin, self).transform(Xb, yb) # Skip if affine_p is 0. Setting affine_p may be useful for quickly # disabling affine transformation if self.affine_p == 0: return Xb, yb image_height = Xb.shape[2] image_width = Xb.shape[3] assert image_height == image_width idx = get_random_idx(Xb, self.affine_p) Xb_transformed = Xb.copy() yb_transformed = yb.copy() for i in idx: scale = np.random.choice(self.affine_scale_choices) rotation = np.random.choice(self.affine_rotation_choices) shear = np.random.choice(self.affine_shear_choices) translation_y = np.random.choice(self.affine_translation_choices) translation_x = np.random.choice(self.affine_translation_choices) transform_kwargs = dict( scale=scale, rotation=rotation, shear=shear, translation_y=translation_y, translation_x=translation_x, return_tform=True ) img_transformed, tform = im_affine_transform( Xb[i], **transform_kwargs ) Xb_transformed[i] = img_transformed pts = yb_transformed[i].reshape(-1, 2) * image_height pts = tform.inverse(pts).ravel() yb_transformed[i] = pts / image_height return Xb_transformed, yb_transformed
def transform(self, Xb, yb): Xb, yb = super(AffineTransformPtsBatchIteratorMixin, self).transform(Xb, yb) # Skip if affine_p is 0. Setting affine_p may be useful for quickly # disabling affine transformation if self.affine_p == 0: return Xb, yb image_height = Xb.shape[2] image_width = Xb.shape[3] assert image_height == image_width idx = get_random_idx(Xb, self.affine_p) Xb_transformed = Xb.copy() yb_transformed = yb.copy() for i in idx: scale = np.random.choice(self.affine_scale_choices) rotation = np.random.choice(self.affine_rotation_choices) shear = np.random.choice(self.affine_shear_choices) translation_y = np.random.choice(self.affine_translation_choices) translation_x = np.random.choice(self.affine_translation_choices) transform_kwargs = dict(scale=scale, rotation=rotation, shear=shear, translation_y=translation_y, translation_x=translation_x, return_tform=True) img_transformed, tform = im_affine_transform( Xb[i], **transform_kwargs) Xb_transformed[i] = img_transformed pts = yb_transformed[i].reshape(-1, 2) * image_height pts = tform.inverse(pts).ravel() yb_transformed[i] = pts / image_height return Xb_transformed, yb_transformed
def transform(self, Xb, yb): Xb, yb = super(AffineTransformBBoxBatchIteratorMixin, self).transform(Xb, yb) # Skip if affine_p is 0. Setting affine_p may be useful for quickly # disabling affine transformation if self.affine_p == 0: return Xb, yb image_height = Xb.shape[2] image_width = Xb.shape[3] assert image_height == image_width idx = get_random_idx(Xb, self.affine_p) Xb_transformed = Xb.copy() yb_transformed = yb.copy() for i in idx: scale = np.random.choice(self.affine_scale_choices) rotation = np.random.choice(self.affine_rotation_choices) shear = np.random.choice(self.affine_shear_choices) translation_y = np.random.choice(self.affine_translation_choices) translation_x = np.random.choice(self.affine_translation_choices) transform_kwargs = dict(scale=scale, rotation=rotation, shear=shear, translation_y=translation_y, translation_x=translation_x) img_transformed = im_affine_transform(Xb[i], **transform_kwargs) bbox_transformed = get_transformed_bbox(yb[i] * image_width, image_width, image_height, **transform_kwargs) Xb_transformed[i] = img_transformed yb_transformed[i] = np.array(bbox_transformed).astype( np.float32) / image_width return Xb_transformed, yb_transformed
def transform(self, Xb, yb): Xb, yb = super(AffineTransformBBoxBatchIteratorMixin, self).transform(Xb, yb) # Skip if affine_p is 0. Setting affine_p may be useful for quickly # disabling affine transformation if self.affine_p == 0: return Xb, yb image_height = Xb.shape[2] image_width = Xb.shape[3] assert image_height == image_width idx = get_random_idx(Xb, self.affine_p) Xb_transformed = Xb.copy() yb_transformed = yb.copy() for i in idx: scale = np.random.choice(self.affine_scale_choices) rotation = np.random.choice(self.affine_rotation_choices) shear = np.random.choice(self.affine_shear_choices) translation_y = np.random.choice(self.affine_translation_choices) translation_x = np.random.choice(self.affine_translation_choices) transform_kwargs = dict( scale=scale, rotation=rotation, shear=shear, translation_y=translation_y, translation_x=translation_x ) img_transformed = im_affine_transform( Xb[i], **transform_kwargs) bbox_transformed = get_transformed_bbox( yb[i] * image_width, image_width, image_height, **transform_kwargs) Xb_transformed[i] = img_transformed yb_transformed[i] = np.array(bbox_transformed).astype(np.float32) / image_width return Xb_transformed, yb_transformed
def get_transformed_bbox(bbox, image_width, image_height, **kwargs): l, t, w, h = bbox r = l + w b = t + h y_heatmap = np.zeros((image_height, image_width)).astype(bool) y_heatmap[t:b, l:r] = True y_heatmap = im_affine_transform(y_heatmap[np.newaxis, ...], **kwargs) y_heatmap = y_heatmap[0].astype(bool) dets = find_objects(y_heatmap) if len(dets) == 1: t = dets[0][0].start b = dets[0][0].stop l = dets[0][1].start r = dets[0][1].stop w = r - l h = b - t else: l, t, w, h = 0, 0, 0, 0 return l, t, w, h
def tta_transform(x, tf_args, return_tform=False): z, s, r, t = tf_args return im_affine_transform(x, z, r, s, t, t, return_tform=return_tform)