def test_sanity(): transform = ImageTransform.AffineTransform(seq[:6]) assert_no_exception(lambda: im.transform((100, 100), transform)) transform = ImageTransform.ExtentTransform(seq[:4]) assert_no_exception(lambda: im.transform((100, 100), transform)) transform = ImageTransform.QuadTransform(seq[:8]) assert_no_exception(lambda: im.transform((100, 100), transform)) transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])]) assert_no_exception(lambda: im.transform((100, 100), transform))
def test_sanity(self): im = Image.new("L", (100, 100)) seq = tuple(range(10)) transform = ImageTransform.AffineTransform(seq[:6]) im.transform((100, 100), transform) transform = ImageTransform.ExtentTransform(seq[:4]) im.transform((100, 100), transform) transform = ImageTransform.QuadTransform(seq[:8]) im.transform((100, 100), transform) transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])]) im.transform((100, 100), transform)
def __getitem__(self, index): """Return a data point and its metadata information. Parameters: index - - a random integer for data indexing Returns a dictionary that contains A, B, A_paths and B_paths A (tensor) - - an image in the input domain B (tensor) - - its corresponding image in the target domain A_paths (str) - - image paths B_paths (str) - - image paths (same as A_paths) """ # read a image given a random integer index AB_path = self.AB_paths[index] AB = Image.open(AB_path).convert('RGB') # split AB image into A and B w, h = AB.size w2 = int(w / 2) A = AB.crop((0, 0, w2, h)) B = AB.crop((w2, 0, w, h)) # apply random deformation if requested # TODO: merge this into the get_transform() function of base_dataset.py if 'deform' in self.opt.preprocess: points = self.opt.deform_points sigma = self.opt.deform_sigma # set up coordinate grid x = np.arange(points) * w2 / (points - 1) y = np.arange(points) * h / (points - 1) X, Y = np.meshgrid(x, y) # add deformations X += sigma * np.random.randn(points, points) Y += sigma * np.random.randn(points, points) # set up boxes and quads for the mesh transformation quads = [] boxw = int(w2 / (points - 1)) boxh = int(h / (points - 1)) for iqy in range(points - 1): for iqx in range(points - 1): # set up box (destination for this quadrilateral) box = (iqx * boxw, iqy * boxh, (iqx + 1) * boxw, (iqy + 1) * boxh) # set up coord list for this quadrilateral quad_x = X[iqy:iqy + 2, iqx:iqx + 2] quad_y = Y[iqy:iqy + 2, iqx:iqx + 2] # arrange coords in counter-clockwise fashion quad_x = np.expand_dims(quad_x.flatten()[[0, 2, 3, 1]], 1) quad_y = np.expand_dims(quad_y.flatten()[[0, 2, 3, 1]], 1) # arrange data for MeshTransform quad = tuple(np.concatenate((quad_x, quad_y), 1).flatten()) quads.append((box, quad)) transform = ImageTransform.MeshTransform(quads) A = A.transform((w2, h), transform) B = B.transform((w2, h), transform) # apply the same transform to both A and B transform_params = get_params(self.opt, A.size) A_transform = get_transform(self.opt, transform_params, grayscale=(self.input_nc == 1)) transform_params[ 'no_erasing'] = True # prevent random erasing from being applied to target image transform_params[ 'blur'] = False # prevent blur augmentation from being applied to target image B_transform = get_transform(self.opt, transform_params, grayscale=(self.output_nc == 1)) A = A_transform(A) B = B_transform(B) return {'A': A, 'B': B, 'A_paths': AB_path, 'B_paths': AB_path}