def __call__(self, in_data): img, bbox, label = in_data # 1. Color augumentation img = random_distort(img) # 2. Random expansion if np.random.randint(2): img, param = transforms.random_expand( img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox( bbox, y_offset=param["y_offset"], x_offset=param["x_offset"]) # 3. Random cropping img, param = random_crop_with_bbox_constraints( img, bbox, return_param=True) bbox, param = transforms.crop_bbox( bbox, y_slice=param["y_slice"], x_slice=param["x_slice"], allow_outside_center=False, return_param=True) label = label[param["index"]] # 4. Resizing with random interpolation _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. Transformation for SSD network input img -= self.mean mb_loc, mb_lab = self.coder.encode(bbox, label) return img, mb_loc, mb_lab
def transform( inputs, mean, std, random_angle=15., pca_sigma=25.5, expand_ratio=1.2, crop_size=(28, 28), train=True): img = inputs img = img.copy() # Random rotate if random_angle != 0: angle = np.random.uniform(-random_angle, random_angle) img = cv_rotate(img, angle) # Color augmentation if train and pca_sigma != 0: img = transforms.pca_lighting(img, pca_sigma) """ # Standardization img -= mean[:, None, None] img /= std[:, None, None] """ if train: # Random flip img = transforms.random_flip(img, x_random=True) # Random expand if expand_ratio > 1: img = transforms.random_expand(img, max_ratio=expand_ratio) # Random crop if tuple(crop_size) != (32, 32): img = transforms.random_crop(img, tuple(crop_size)) return img
def __call__(self, in_data): img, bbox, label = in_data # 1. Color augumentation img = random_distort(img) # 2. Random expansion if np.random.randint(2): img, param = transforms.random_expand( img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox( bbox, y_offset=param["y_offset"], x_offset=param["x_offset"]) # 3. Random cropping img, param = random_crop_with_bbox_constraints( img, bbox, return_param=True) bbox, param = transforms.crop_bbox( bbox, y_slice=param["y_slice"], x_slice=param["x_slice"], allow_outside_center=False, return_param=True) label = label[param["index"]] # 4. Resizing with random interpolation _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. Transformation for SSD network input img -= self.mean mb_loc, mb_lab = self.coder.encode(bbox, label) return img, mb_loc, mb_lab
def transform(inputs, mean, std, random_angle=15., expand_ratio=1.0, crop_size=(32, 32), train=True): img, label = inputs img = img.copy() # Random rotate if random_angle != 0: angle = np.random.uniform(-random_angle, random_angle) img = cv_rotate(img, angle) # Cut out img = cut_out(img) # Standardization img -= mean[:, None, None] img /= std[:, None, None] if train: # Random flip img = transforms.random_flip(img, x_random=True) # Random expand if expand_ratio > 1: img = transforms.random_expand(img, max_ratio=expand_ratio) # Random crop if tuple(crop_size) != (32, 32): img = transforms.random_crop(img, tuple(crop_size)) return img, label
def transform( inputs, mean, std, random_angle=15., pca_sigma=255., expand_ratio=1.0, crop_size=(32, 32), train=True): img, label = inputs img = img.copy() # Random rotate if random_angle != 0: angle = np.random.uniform(-random_angle, random_angle) img = cv_rotate(img, angle) # Color augmentation if train and pca_sigma != 0: img = transforms.pca_lighting(img, pca_sigma) # Standardization img -= mean[:, None, None] img /= std[:, None, None] if train: # Random flip img = transforms.random_flip(img, x_random=True) # Random expand if expand_ratio > 1: img = transforms.random_expand(img, max_ratio=expand_ratio) # Random crop if tuple(crop_size) != (32, 32): img = transforms.random_crop(img, tuple(crop_size)) return img, label
def __call__(self, in_data): # There are five data augmentation steps # 1. Color augmentation # 2. Random expansion # 3. Random cropping # 4. Resizing with random interpolation # 5. Random horizontal flipping # 6. Random vertical flipping img, bbox, label = in_data # 1. Color augmentation img = random_distort(img) # 2. Random expansion if np.random.randint(2): img, param = transforms.random_expand(img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox(bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) # 3. Random cropping img, param = random_crop_with_bbox_constraints(img, bbox, return_param=True) bbox, param = transforms.crop_bbox(bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) label = label[param['index']] # 4. Resizing with random interpolatation _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. Random horizontal flipping img, params = transforms.random_flip(img, x_random=True, return_param=True) bbox = transforms.flip_bbox(bbox, (self.size, self.size), x_flip=params['x_flip']) # 6. Random vertical flipping img, params = transforms.random_flip(img, y_random=True, return_param=True) bbox = transforms.flip_bbox(bbox, (self.size, self.size), y_flip=params['y_flip']) # Preparation for SSD network img -= self.mean mb_loc, mb_label = self.coder.encode(bbox, label) return img, mb_loc, mb_label
def __call__(self, in_data): # 5段階のステップでデータの水増しを行う # 1. 色の拡張 # 2. ランダムな拡大 # 3. ランダムなトリミング # 4. ランダムな補完の再補正 # 5. ランダムな水平反転 img, bbox, label = in_data # 1. 色の拡張 # 明るさ,コントラスト,彩度,色相を組み合わせ,データ拡張をする img = random_distort(img) # 2. ランダムな拡大 if np.random.randint(2): # キャンバスの様々な座標に入力画像を置いて,様々な比率の画像を生成し,bounding boxを更新 img, param = transforms.random_expand(img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox(bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) # 3. ランダムなトリミング img, param = random_crop_with_bbox_constraints(img, bbox, return_param=True) # トリミングされた画像内にbounding boxが入るように調整 bbox, param = transforms.crop_bbox(bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) label = label[param['index']] # 4. ランダムな補完の再補正 ## 画像とbounding boxのリサイズ _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. ランダムな水平反転 ## 画像とbounding boxをランダムに水平方向に反転 img, params = transforms.random_flip(img, x_random=True, return_param=True) bbox = transforms.flip_bbox(bbox, (self.size, self.size), x_flip=params['x_flip']) # SSDのネットワークに入力するための準備の処理 img -= self.mean ## SSDに入力するためのloc(デフォルトbounding boxのオフセットとスケール)と ## mb_label(クラスを表す配列)を出力 mb_loc, mb_label = self.coder.encode(bbox, label) return img, mb_loc, mb_label
def transform(inputs, mean=None, std=None, random_angle=15., pca_sigma=255., expand_ratio=1.0, crop_size=(32, 32), cutout=None, flip=True, train=True, old_test_method=False): img, label = inputs img = img.copy() if train: # Random rotate if random_angle != 0: angle = np.random.uniform(-random_angle, random_angle) img = cv_rotate(img, angle) # Color augmentation if pca_sigma != 0: img = transforms.pca_lighting(img, pca_sigma) elif old_test_method: # There was a bug in prior versions, here it is reactivated to preserve # the same test accuracy if random_angle != 0: angle = np.random.uniform(-random_angle, random_angle) img = cv_rotate(img, angle) # Standardization if mean is not None: img -= mean[:, None, None] img /= std[:, None, None] if train: # Random flip if flip: img = transforms.random_flip(img, x_random=True) # Random expand if expand_ratio > 1: img = transforms.random_expand(img, max_ratio=expand_ratio) # Random crop if tuple(crop_size) != (32, 32) or expand_ratio > 1: img = transforms.random_crop(img, tuple(crop_size)) # Cutout if cutout is not None: h0, w0 = np.random.randint(0, 32 - cutout, size=(2, )) img[:, h0:h0 + cutout, w0:w0 + cutout].fill(0.0) return img, label
def test_random_expand(self): img = np.random.uniform(-1, 1, size=(3, 64, 32)) out = random_expand(img) out = random_expand(img, max_ratio=1) np.testing.assert_equal(out, img) out, param = random_expand( img, max_ratio=self.max_ratio, return_param=True) ratio = param['ratio'] y_offset = param['y_offset'] x_offset = param['x_offset'] np.testing.assert_equal( out[:, y_offset:y_offset + 64, x_offset:x_offset + 32], img) self.assertGreaterEqual(ratio, 1) self.assertLessEqual(ratio, self.max_ratio) self.assertEqual(out.shape[1], int(64 * ratio)) self.assertEqual(out.shape[2], int(32 * ratio)) out = random_expand(img, max_ratio=2)
def test_random_expand(self): img = np.random.uniform(-1, 1, size=(3, 64, 32)) out = random_expand(img) out = random_expand(img, max_ratio=1) np.testing.assert_equal(out, img) out, param = random_expand( img, max_ratio=self.max_ratio, return_param=True) ratio = param['ratio'] y_offset = param['y_offset'] x_offset = param['x_offset'] np.testing.assert_equal( out[:, y_offset:y_offset + 64, x_offset:x_offset + 32], img) self.assertGreaterEqual(ratio, 1) self.assertLessEqual(ratio, self.max_ratio) self.assertEqual(out.shape[1], int(64 * ratio)) self.assertEqual(out.shape[2], int(32 * ratio)) out = random_expand(img, max_ratio=2)
def __call__(self, in_data): # There are five data augmentation steps # 1. Color augmentation # 2. Random expansion # 3. Random cropping # 4. Resizing with random interpolation # 5. Random horizontal flipping img, bbox, label = in_data bbox = np.array(bbox).astype(np.float32) if len(bbox) == 0: warnings.warn("No bounding box detected", RuntimeWarning) img = resize_with_random_interpolation(img, (self.size, self.size)) mb_loc, mb_label = self.coder.encode(bbox, label) return img, mb_loc, mb_label # 1. Color augmentation img = random_distort(img) # 2. Random expansion if np.random.randint(2): img, param = transforms.random_expand( img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox( bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) # 3. Random cropping img, param = random_crop_with_bbox_constraints( img, bbox, return_param=True) bbox, param = transforms.crop_bbox( bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) label = label[param['index']] # 4. Resizing with random interpolatation _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. Random horizontal flipping img, params = transforms.random_flip( img, x_random=True, return_param=True) bbox = transforms.flip_bbox( bbox, (self.size, self.size), x_flip=params['x_flip']) mb_loc, mb_label = self.coder.encode(bbox, label) return img, mb_loc, mb_label
def test_random_expand_fill(self): img = np.random.uniform(-1, 1, size=(3, 64, 32)) while True: out, param = random_expand(img, fill=self.fill, return_param=True) y_offset = param['y_offset'] x_offset = param['x_offset'] if y_offset > 0 or x_offset > 0: break if isinstance(self.fill, int): fill = (self.fill, ) * 3 else: fill = self.fill np.testing.assert_equal(out[:, 0, 0], np.array(fill).flatten())
def __call__(self, inputs, train=True): img, label = inputs img = img.copy() # Color augmentation if train and self.pca: img = transforms.pca_lighting(img, 76.5) # Standardization img -= self.mean img *= self.invstd # Random crop if train and self.trans: img = transforms.random_flip(img, x_random=True) img = transforms.random_expand(img, max_ratio=1.5) img = transforms.random_crop(img, (28, 28)) return img, label
def transform_img(inputs, mean, std, pca_sigma=0, random_angle=0, x_random_flip=False, y_random_flip=False, expand_ratio=1., random_crop_size=(224, 224), random_erase=False, output_size=(224, 224), train=False): x, lab = inputs x = x.copy() # Color augmentation if train and pca_sigma != 0: x = transforms.pca_lighting(x, pca_sigma) x -= mean[:, None, None] x /= std[:, None, None] x = x[::-1] if train: # Random rotate if random_angle != 0: angle = np.random.uniform(-random_angle, random_angle) x = cv_rotate(x, angle) # Random flip if x_random_flip or y_random_flip: x = transforms.random_flip(x, x_random=x_random_flip, y_random=y_random_flip) # Random expand if expand_ratio > 1: x = transforms.random_expand(x, max_ratio=expand_ratio) if all(random_crop_size) > 0: x = transforms.random_crop(x, random_crop_size) else: if random_erase: x = random_erasing(x) if all(random_crop_size) > 0: x = transforms.resize(x, random_crop_size) else: x = transforms.resize(x, output_size) return x, lab
def test_random_expand_fill(self): img = np.random.uniform(-1, 1, size=(3, 64, 32)) while True: out, param = random_expand(img, fill=self.fill, return_param=True) x_offset = param['x_offset'] y_offset = param['y_offset'] if x_offset > 0 or y_offset > 0: break if isinstance(self.fill, int): np.testing.assert_equal( out[:, 0, 0], (self.fill,) * 3) else: np.testing.assert_equal( out[:, 0, 0], self.fill)
def test_random_expand_fill(self): img = np.random.uniform(-1, 1, size=(3, 64, 32)) while True: out, param = random_expand(img, fill=self.fill, return_param=True) y_offset = param['y_offset'] x_offset = param['x_offset'] if y_offset > 0 or x_offset > 0: break if isinstance(self.fill, int): fill = (self.fill,) * 3 else: fill = self.fill np.testing.assert_equal( out[:, 0, 0], np.array(fill).flatten())
def __call__(self, in_data): # There are five data augmentation steps # 1. Color augmentation # 2. Random expansion # 3. Random cropping # 4. Resizing with random interpolation # 5. Random horizontal flipping img, bbox, label = in_data # 1. Color augmentation img = random_distort(img) # 2. Random expansion if np.random.randint(2): img, param = transforms.random_expand( img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox( bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) # 3. Random cropping img, param = random_crop_with_bbox_constraints( img, bbox, return_param=True) bbox, param = transforms.crop_bbox( bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) label = label[param['index']] # 4. Resizing with random interpolatation _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. Random horizontal flipping img, params = transforms.random_flip( img, x_random=True, return_param=True) bbox = transforms.flip_bbox( bbox, (self.size, self.size), x_flip=params['x_flip']) # Preparation for SSD network img -= self.mean mb_loc, mb_label = self.coder.encode(bbox, label) return img, mb_loc, mb_label
def __call__(self, in_data): img, bbox, label = in_data img = random_distort(img) if np.random.randint(2): img, param = transforms.random_expand(img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox(bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) img, param = random_crop_with_bbox_constraints(img, bbox, return_param=True) bbox, param = transforms.crop_bbox(bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) label = label[param['index']] _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) img, params = transforms.random_flip(img, x_random=True, return_param=True) bbox = transforms.flip_bbox(bbox, (self.size, self.size), x_flip=params['x_flip']) img -= self.mean mb_loc, mb_label = self.coder.encode(bbox, label) return img, mb_loc, mb_label
def random_expand(img, expand_ratio): if expand_ratio > 1: img = transforms.random_expand(img, max_ratio=expand_ratio) return img
def __call__(self, in_data): img, bbox, label = in_data _, H, W = img.shape # random brightness and contrast img = random_distort(img) # rotate image # return a tuple whose elements are rotated image, param. # k (int in param)represents the number of times the image is rotated by 90 degrees. img, params = transforms.random_rotate(img, return_param=True) # restore the new hight and width _, t_H, t_W = img.shape # rotate bbox based on renewed parameters bbox = rotate_bbox(bbox, (H, W), params['k']) # Random expansion:This method randomly place the input image on # a larger canvas. The size of the canvas is (rH,rW), r is a random ratio drawn from [1,max_ratio]. # The canvas is filled by a value fill except for the region where the original image is placed. if np.random.randint(2): fill_value = img.mean(axis=1).mean(axis=1).reshape(-1, 1, 1) img, param = transforms.random_expand(img, max_ratio=2, fill=fill_value, return_param=True) bbox = transforms.translate_bbox(bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) # Random crop # crops the image with bounding box constraints img, param = random_crop_with_bbox_constraints(img, bbox, min_scale=0.5, max_aspect_ratio=1.5, return_param=True) # this translates bounding boxes to fit within the cropped area of an image, bounding boxes whose centers are outside of the cropped area are removed. bbox, param = transforms.crop_bbox(bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) #assigning new labels to the bounding boxes after cropping label = label[param['index']] # if the bounding boxes are all removed, if bbox.shape[0] == 0: img, bbox, label = in_data # update the height and width of the image _, t_H, t_W = img.shape img = self.faster_rcnn.prepare(img) # prepares the image to match the size of the image to be input into the RCNN _, o_H, o_W = img.shape # resize the bounding box according to the image resize bbox = transforms.resize_bbox(bbox, (t_H, t_W), (o_H, o_W)) # horizontally & vertical flip # simutaneously flip horizontally and vertically of the image img, params = transforms.random_flip(img, x_random=True, y_random=True, return_param=True) # flip the bounding box with respect to the parameter bbox = transforms.flip_bbox(bbox, (o_H, o_W), x_flip=params['x_flip'], y_flip=params['y_flip']) scale = o_H / t_H return img, bbox, label, scale
def transform(in_data): img, label = in_data img = transforms.random_expand(img, max_ratio=3) img = transforms.resize(img, (28, 28)) return img, label
def __call__(self, in_data): # There are five data augmentation steps # 1. Color augmentation # 2. Random expansion # 3. Random cropping # 4. Resizing with random interpolation # 5. Random horizontal flipping # mask = None img, bbox, label, mask = in_data # TODO: show information # self._show_img(img) # self._show_mask(mask) # 1. Color augmentation img = random_distort(img) # self._show_img(img) # 2. Random expansion if np.random.randint(2): img, param = transforms.random_expand( img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox( bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) if mask is not None: _, new_height, new_width = img.shape param['new_height'] = new_height param['new_width'] = new_width mask = self._random_expand_mask(mask, param) # self._show_img(img) # self._show_mask(mask) # 3. Random cropping img, param = random_crop_with_bbox_constraints( img, bbox, return_param=True) # self._show_img(img) mask = self._fixed_crop_mask(mask, param['y_slice'], param['x_slice']) # self._show_mask(mask) bbox, param = transforms.crop_bbox( bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) label = label[param['index']] # 4. Resizing with random interpolatation _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) # self._show_img(img) if mask is not None: if mask.size == 0: raise RuntimeError mask = self._resize_with_nearest(mask, (self.size, self.size)) # self._show_mask(mask) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. Random horizontal flipping img, params = transforms.random_flip( img, x_random=True, return_param=True) bbox = transforms.flip_bbox( bbox, (self.size, self.size), x_flip=params['x_flip']) if mask is not None: mask = self._random_flip_mask(mask, x_flip=params['x_flip'], y_flip=params['y_flip']) # self._show_img(img) # self._show_mask(mask) # Preparation for SSD network img -= self.mean mb_loc, mb_label = self.coder.encode(bbox, label) if mask is None: mask = np.ones([self.size, self.size], dtype=np.int32) * -1 # print("Dtype is :"+str(mask.dtype)) data_type = str(mask.dtype) target_type = 'int32' if data_type != target_type: mask = mask.astype(np.int32) if img is None: raise RuntimeError return img, mb_loc, mb_label, mask