def test_pca_lighting(self): img = np.random.uniform(size=(3, 48, 32)) out = pca_lighting(img, 0.1) self.assertEqual(img.shape, out.shape) self.assertEqual(img.dtype, out.dtype) out = pca_lighting(img, 0) self.assertEqual(img.shape, out.shape) self.assertEqual(img.dtype, out.dtype) np.testing.assert_equal(out, img)
def transform(images): input, answer = copy.deepcopy(images) # rotate deg = np.random.choice(DEG_RANGE) input = rotate_image(input, deg) answer = rotate_image(answer, deg) # resize H, W = input.shape[1:] h_resize = int(np.random.uniform(240, H * 2.0)) w_resize = int(np.random.uniform(320, W * 2.0)) input = chainercv.transforms.resize(input, (h_resize, w_resize)) answer = chainercv.transforms.resize(answer, (h_resize, w_resize)) # crop input, slice = transforms.random_crop(input, (240, 320), return_param=True) answer = answer[:, slice["y_slice"], slice['x_slice']] # flip input, param = transforms.random_flip(input, x_random=True, return_param=True) if param['x_flip']: transforms.flip(answer, x_flip=True) # pca_lighting: input = transforms.pca_lighting(input, sigma=5) return resize((input, answer))
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 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, img): img = random_crop(img=img, size=self.resize_value) img = random_flip(img=img, x_random=True) img = pca_lighting(img=img, sigma=25.5) img = scale(img=img, size=self.resize_value) img = center_crop(img, self.input_image_size) img /= 255.0 img -= self.mean img /= self.std return img
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 transform(self, image, keypoints, bbox, is_labeled): _, H, W = image.shape # PCA Lighting image = transforms.pca_lighting(image, sigma=5) # Random rotate degree = np.random.uniform(-40, 40) image, keypoints, bbox = rotate(image, keypoints, bbox, degree) # Random flip image, param = transforms.random_flip(image, x_random=True, return_param=True) if param['x_flip']: keypoints = [ transforms.flip_point(points, (H, W), x_flip=True)[self.flip_indices] for points in keypoints ] is_labeled = [label[self.flip_indices] for label in is_labeled] new_bbox = [] for x, y, w, h in bbox: [[y, x]] = transforms.flip_point(np.array([[y, x + w]]), (H, W), x_flip=True) new_bbox.append([x, y, w, h]) bbox = new_bbox # Random resize scalew, scaleh = np.random.uniform(1.0, 2.0, 2) resizeW, resizeH = int(W * scalew), int(H * scalew) image, keypoints, bbox = self.resize(image, keypoints, bbox, (resizeH, resizeW)) # Random crop image, param = transforms.random_sized_crop(image, scale_ratio_range=(0.5, 5), return_param=True) keypoints = [ transforms.translate_point(points, x_offset=-param['x_slice'].start, y_offset=-param['y_slice'].start) for points in keypoints ] new_bbox = [] for x, y, w, h in bbox: new_bbox.append( [x - param['x_slice'].start, y - param['y_slice'].start, w, h]) bbox = new_bbox return image, keypoints, bbox, is_labeled
def augment_data(image, resize_width, resize_height, use_random_x_flip, use_random_y_flip, use_random_rotate, use_pca_lighting, crop_edit, crop_width, crop_height): image = transforms.random_flip(image, use_random_x_flip, use_random_y_flip) if use_random_rotate: image = transforms.random_rotate(image) image = transforms.pca_lighting(image, sigma=use_pca_lighting) if crop_edit == 'Center Crop': image = transforms.center_crop(image, (crop_width, crop_height)) elif crop_edit == 'Random Crop': image = transforms.random_crop(image, (crop_width, crop_height)) image = transforms.resize(image, (resize_width, resize_height)) return image
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 transform(in_data): img, label = in_data if img.shape[0] == 1: img = np.array([img[0], img[0], img[0]]) elif img.shape[0] == 4: img = np.array([img[0], img[1], img[2]]) img /= 255 img = random_sized_crop(img) img = T.resize(img, (224, 224), interpolation=PIL.Image.BICUBIC) img = color_jitter(img) img = T.pca_lighting(img, 0.1) img = (img - mean[:, None, None]) / std[:, None, None] img = T.random_flip(img, x_random=True) return img, label
def get_example(self, i): image, label = self.base[i] imgpath = self.base._pairs[i][0] image = image.copy().astype(np.float32) if self.train: image = rotate_image(image) image = transforms.resize(image, (random.choice(range(368, 512)), random.choice(range(368, 512)))) image = transforms.pca_lighting(image, 76.5) image = transforms.random_flip(image, x_random=True, y_random=True) if image.shape[1] >= 224 and image.shape[2] >= 224: image = transforms.random_crop(image, size=(224, 224)) image = transforms.resize(image, (224, 224)) image = preprocess(image, self.model_name) if image.shape[0] == 1: """ REMARK: all images are not color images. one may find there exists gray scale image e.g. food-101/images/steak/1340977.jpg Surprisingly, this is also not steak. Just photo of family lol. Who made this data :( """ logger.info("gray scale image found ={}".format(imgpath)) logger.info("We will convert RGB color format") if ENAVLE_CV2: image = image.transpose(1, 2, 0) image = cv2.cvtColor(image.astype( np.uint8), cv2.COLOR_GRAY2RGB) image = image.transpose(2, 0, 1) image = image.astype(np.float32) else: new_image = np.zeros((3, 224, 224)).astype(np.float32) plane = image[0] new_image[0] = plane new_image[1] = plane new_image[2] = plane image = new_image return image, label
def _transform(inputs, mean=None, crop_size=(512, 512), color_sigma=25.5, scale=[0.5, 2.0], rotate=False, fliplr=False, n_class=20): img, label = inputs # Scaling if scale: if isinstance(scale, (list, tuple)): scale = np.random.uniform(scale[0], scale[1]) scaled_h = int(img.shape[1] * scale) scaled_w = int(img.shape[2] * scale) img = transforms.resize(img, (scaled_h, scaled_w), Image.BICUBIC) label = transforms.resize(label[None, ...], (scaled_h, scaled_w), Image.NEAREST)[0] # Crop if crop_size is not None: if (img.shape[1] < crop_size[0]) or (img.shape[2] < crop_size[1]): shorter_side = min(img.shape[1:]) _crop_size = (shorter_side, shorter_side) img, param = transforms.random_crop(img, _crop_size, True) else: img, param = transforms.random_crop(img, crop_size, True) label = label[param['y_slice'], param['x_slice']] # Rotate if rotate: angle = np.random.uniform(-10, 10) rows, cols = img.shape[1:] img = img.transpose(1, 2, 0) r = cv.getRotationMatrix2D((cols // 2, rows // 2), angle, 1) img = cv.warpAffine(img, r, (cols, rows)).transpose(2, 0, 1) r = cv.getRotationMatrix2D((cols // 2, rows // 2), angle, 1) label = cv.warpAffine(label, r, (cols, rows), flags=cv.INTER_NEAREST, borderValue=-1) # Resize if crop_size is not None: if (img.shape[1] < crop_size[0]) or (img.shape[2] < crop_size[1]): img = transforms.resize(img, crop_size, Image.BICUBIC) if (label.shape[0] < crop_size[0]) or (label.shape[1] < crop_size[1]): label = transforms.resize(label[None, ...].astype(np.float32), crop_size, Image.NEAREST) label = label.astype(np.int32)[0] # Mean subtraction if mean is not None: img -= mean[:, None, None] # LR-flipping if fliplr: if np.random.rand() > 0.5: img = transforms.flip(img, x_flip=True) label = transforms.flip(label[None, ...], x_flip=True)[0] # Color augmentation if color_sigma is not None: img = transforms.pca_lighting(img, color_sigma) assert label.max() < n_class, '{}'.format(label.max()) if crop_size is not None: assert img.shape == (3, crop_size[0], crop_size[1]), \ '{} != {}'.format(img.shape, crop_size) assert label.shape == (crop_size[0], crop_size[1]), \ '{} != {}'.format(label.shape, crop_size) return img, label
def color_augmentation(img, pca_sigma): if pca_sigma != 0: img = transforms.pca_lighting(img, pca_sigma) return img