def __call__(self, in_data): img, label = in_data img = transforms.random_sized_crop(img) img = transforms.resize(img, (224, 224)) img = transforms.random_flip(img, x_random=True) img -= self.mean return img.astype(chainer.get_dtype()), label
def __call__(self, in_data): img, label = in_data img = random_sized_crop(img) img = resize(img, (224, 224)) img = random_flip(img, x_random=True) img -= self.mean return img, label
def random_sized_crop(image, keypoints, bbox): image, param = transforms.random_sized_crop( image, scale_ratio_range=(0.5, 5), aspect_ratio_range=(0.75, 1.3333333333333333), return_param=True ) keypoints = [ transforms.translate_point(points, x_offset=-param['x_slice'].start, y_offset=-param['y_slice'].start ) for points in keypoints ] _, cropped_H, cropped_W = image.shape bbox = translate_bbox( bbox, size=(cropped_H, cropped_W), x_offset=-param['x_slice'].start, y_offset=-param['y_slice'].start, ) return image, keypoints, bbox, {random_sized_crop.__name__: param}
def test_random_sized_crop(self): img = np.random.uniform(size=(3, self.H, self.W)) scale_ratio_interval = (0.08, 1) aspect_ratio_interval = (3 / 4, 4 / 3) out, params = random_sized_crop(img, scale_ratio_interval, aspect_ratio_interval, return_param=True) expected = img[:, params['y_slice'], params['x_slice']] np.testing.assert_equal(out, expected) _, H_crop, W_crop = out.shape scale_ratio = params['scale_ratio'] aspect_ratio = params['aspect_ratio'] area = scale_ratio * self.H * self.W expected_H_crop = int(math.floor(np.sqrt(area * aspect_ratio))) expected_W_crop = int(math.floor(np.sqrt(area / aspect_ratio))) self.assertEqual(H_crop, expected_H_crop) self.assertEqual(W_crop, expected_W_crop) self.assertTrue((aspect_ratio_interval[0] <= aspect_ratio) and (aspect_ratio <= aspect_ratio_interval[1])) self.assertTrue(scale_ratio <= scale_ratio_interval[1]) scale_ratio_max = min( (scale_ratio_interval[1], self.H / (self.W * aspect_ratio), (aspect_ratio * self.W) / self.H)) self.assertTrue( min((scale_ratio_max, scale_ratio_interval[0])) <= scale_ratio)
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 train_transform(sample): img, label = sample img = np.transpose(img, (2, 0, 1)) img = transforms.random_sized_crop(img) img = transforms.resize(img, patchsize) img = transforms.random_flip(img, x_random=True) if not no_autoaugment: img = np.transpose(img, (1, 2, 0)) img = Image.fromarray(img) img = policy(img) img = np.asarray(img) img = np.transpose(img, (2, 0, 1)) img = img - mean img = img.astype(dtype) if soft: label = hard_to_soft(label, dtype=dtype) return img, label
def _transform(in_data): img, label = in_data img = random_sized_crop(img, scale_ratio_range=(0.3, 1)) img = random_flip(img, x_random=True) img = chainer.links.model.vision.vgg.prepare(img) return img, label