def test_flip(self): trans = transforms.Compose([ transforms.RandomHorizontalFlip(1.0), transforms.RandomHorizontalFlip(0.0), transforms.RandomVerticalFlip(0.0), transforms.RandomVerticalFlip(1.0), ]) self.do_transform(trans)
def test_trans_resize(self): trans = transforms.Compose([ transforms.Resize(300, [0, 1]), transforms.RandomResizedCrop((280, 280)), transforms.Resize(280, [0, 1]), transforms.Resize((256, 200)), transforms.Resize((180, 160)), transforms.CenterCrop(128), transforms.CenterCrop((128, 128)), ]) self.do_transform(trans)
def test_trans_all(self): normalize = transforms.Normalize( mean=[123.675, 116.28, 103.53], std=[58.395, 57.120, 57.375]) trans = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.GaussianNoise(), transforms.ColorJitter( brightness=0.4, contrast=0.4, saturation=0.4, hue=0.4), transforms.RandomHorizontalFlip(), transforms.Permute(mode='CHW'), normalize ]) self.do_transform(trans)
def __init__(self, path, mode='train', image_size=224, resize_short_size=256): super(ImageNetDataset, self).__init__(path) self.mode = mode normalize = transforms.Normalize( mean=[123.675, 116.28, 103.53], std=[58.395, 57.120, 57.375]) if self.mode == 'train': self.transform = transforms.Compose([ transforms.RandomResizedCrop(image_size), transforms.RandomHorizontalFlip(), transforms.Permute(mode='CHW'), normalize ]) else: self.transform = transforms.Compose([ transforms.Resize(resize_short_size), transforms.CenterCrop(image_size), transforms.Permute(mode='CHW'), normalize ])
def load_image(image_path, max_size=400, shape=None): image = cv2.imread(image_path) image = image.astype('float32') / 255.0 size = shape if shape is not None else max_size if max( image.shape[:2]) > max_size else max(image.shape[:2]) transform = transforms.Compose([ transforms.Resize(size), transforms.Permute(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) image = transform(image)[np.newaxis, :3, :, :] image = fluid.dygraph.to_variable(image) return image
def test_exception(self): trans = transforms.Compose([transforms.Resize(-1)]) trans_batch = transforms.BatchCompose([transforms.Resize(-1)]) with self.assertRaises(Exception): self.do_transform(trans) with self.assertRaises(Exception): self.do_transform(trans_batch) with self.assertRaises(ValueError): transforms.ContrastTransform(-1.0) with self.assertRaises(ValueError): transforms.SaturationTransform(-1.0), with self.assertRaises(ValueError): transforms.HueTransform(-1.0) with self.assertRaises(ValueError): transforms.BrightnessTransform(-1.0)
def test_trans_centerCrop(self): trans = transforms.Compose([ transforms.CenterCropResize(224), transforms.CenterCropResize(128, 160), ]) self.do_transform(trans)
def test_info(self): str(transforms.Compose([transforms.Resize((224, 224))])) str(transforms.BatchCompose([transforms.Resize((224, 224))]))