def __init__(self, path, mode='train', image_size=224, resize_short_size=256): super(ImageNetDataset, self).__init__(path) self.mode = mode self.samples = [] list_file = "train_list.txt" if self.mode == "train" else "val_list.txt" with open(os.path.join([path, list_file]), 'r') as f: for line in f: _image, _label = line.strip().split(" ") self.samples.append((_image, int(_label))) 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.Transpose(), normalize ]) else: self.transform = transforms.Compose([ transforms.Resize(resize_short_size), transforms.CenterCrop(image_size), transforms.Transpose(), normalize ])
def test_exception(self): trans = transforms.Compose([transforms.Resize(-1)]) trans_batch = transforms.Compose([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.Pad([1.0, 2.0, 3.0]) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, '1') with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, {}) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, padding_mode=-1) with self.assertRaises(ValueError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, [1.0, 2.0, 3.0]) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, '1') with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, {}) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, padding_mode=-1) with self.assertRaises(ValueError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, [1.0, 2.0, 3.0]) with self.assertRaises(ValueError): transforms.RandomRotation(-2) with self.assertRaises(ValueError): transforms.RandomRotation([1, 2, 3]) with self.assertRaises(ValueError): trans_gray = transforms.Grayscale(5) fake_img = self.create_image((100, 120, 3)) trans_gray(fake_img) with self.assertRaises(TypeError): transform = transforms.RandomResizedCrop(64) transform(1)
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.RandomHorizontalFlip(), normalize, ]) self.do_transform(trans)
def test_trans_resize(self): trans = transforms.Compose([ transforms.Resize(300), transforms.RandomResizedCrop((280, 280)), transforms.Resize(280), 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, data_dir, mode='train', image_size=224, resize_short_size=256): super(ImageNetDataset, self).__init__() train_file_list = os.path.join(data_dir, 'train_list.txt') val_file_list = os.path.join(data_dir, 'val_list.txt') test_file_list = os.path.join(data_dir, 'test_list.txt') self.data_dir = data_dir 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.Transpose(), normalize ]) else: self.transform = transforms.Compose([ transforms.Resize(resize_short_size), transforms.CenterCrop(image_size), transforms.Transpose(), normalize ]) if mode == 'train': with open(train_file_list) as flist: full_lines = [line.strip() for line in flist] np.random.shuffle(full_lines) if os.getenv('PADDLE_TRAINING_ROLE'): # distributed mode if the env var `PADDLE_TRAINING_ROLE` exits trainer_id = int(os.getenv("PADDLE_TRAINER_ID", "0")) trainer_count = int(os.getenv("PADDLE_TRAINERS_NUM", "1")) per_node_lines = len(full_lines) // trainer_count lines = full_lines[trainer_id * per_node_lines:( trainer_id + 1) * per_node_lines] print( "read images from %d, length: %d, lines length: %d, total: %d" % (trainer_id * per_node_lines, per_node_lines, len(lines), len(full_lines))) else: lines = full_lines self.data = [line.split() for line in lines] else: with open(val_file_list) as flist: lines = [line.strip() for line in flist] self.data = [line.split() for line in lines]
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.Transpose(), normalize ]) else: self.transform = transforms.Compose([ transforms.Resize(resize_short_size), transforms.CenterCrop(image_size), transforms.Transpose(), normalize ])
def test_exception(self): trans = transforms.Compose([transforms.Resize(-1)]) trans_batch = transforms.Compose([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.Pad([1.0, 2.0, 3.0]) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, '1') with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, {}) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, padding_mode=-1) with self.assertRaises(ValueError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, [1.0, 2.0, 3.0]) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, '1') with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, {}) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, padding_mode=-1) with self.assertRaises(ValueError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, [1.0, 2.0, 3.0]) with self.assertRaises(ValueError): transforms.RandomAffine(-10) with self.assertRaises(ValueError): transforms.RandomAffine([-30, 60], translate=[2, 2]) with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.2, 0.2], scale=[-2, -1]), with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.2, 0.2], scale=[1, 2, 3]), with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[1, 2, 3]), with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.5, 0.3], scale=[0.7, 1.3], shear=[-10, 10, 0, 20, 40]) with self.assertRaises(ValueError): transforms.RandomRotation(-2) with self.assertRaises(ValueError): transforms.RandomRotation([1, 2, 3]) with self.assertRaises(ValueError): trans_gray = transforms.Grayscale(5) fake_img = self.create_image((100, 120, 3)) trans_gray(fake_img) with self.assertRaises(TypeError): transform = transforms.RandomResizedCrop(64) transform(1)
def test_exception(self): trans = transforms.Compose([transforms.Resize(-1)]) trans_batch = transforms.Compose([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) with self.assertRaises(ValueError): transforms.Pad([1.0, 2.0, 3.0]) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, '1') with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, {}) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, padding_mode=-1) with self.assertRaises(ValueError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, [1.0, 2.0, 3.0]) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, '1') with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, {}) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, padding_mode=-1) with self.assertRaises(ValueError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, [1.0, 2.0, 3.0]) with self.assertRaises(ValueError): transforms.RandomAffine(-10) with self.assertRaises(ValueError): transforms.RandomAffine([-30, 60], translate=[2, 2]) with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.2, 0.2], scale=[1, 2, 3]), with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[1, 2, 3]), with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.5, 0.3], scale=[0.7, 1.3], shear=[-10, 10, 0, 20, 40]) with self.assertRaises(ValueError): transforms.RandomAffine(10, translate=[0.5, 0.3], scale=[0.7, 1.3], shear=[-10, 10, 20, 40], fill=114, center=(1, 2, 3)) with self.assertRaises(ValueError): transforms.RandomRotation(-2) with self.assertRaises(ValueError): transforms.RandomRotation([1, 2, 3]) with self.assertRaises(ValueError): trans_gray = transforms.Grayscale(5) fake_img = self.create_image((100, 120, 3)) trans_gray(fake_img) with self.assertRaises(TypeError): transform = transforms.RandomResizedCrop(64) transform(1) with self.assertRaises(ValueError): transform = transforms.BrightnessTransform([-0.1, -0.2]) with self.assertRaises(TypeError): transform = transforms.BrightnessTransform('0.1') with self.assertRaises(ValueError): transform = transforms.BrightnessTransform('0.1', keys=1) with self.assertRaises(NotImplementedError): transform = transforms.BrightnessTransform('0.1', keys='a') with self.assertRaises(Exception): transform = transforms.RandomErasing(scale=0.5) with self.assertRaises(Exception): transform = transforms.RandomErasing(ratio=0.8) with self.assertRaises(Exception): transform = transforms.RandomErasing(scale=(10, 0.4)) with self.assertRaises(Exception): transform = transforms.RandomErasing(ratio=(3.3, 0.3)) with self.assertRaises(Exception): transform = transforms.RandomErasing(prob=1.5) with self.assertRaises(Exception): transform = transforms.RandomErasing(value="0")
def test_exception(self): trans = transforms.Compose([transforms.Resize(-1)]) trans_batch = transforms.Compose([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) with self.assertRaises(ValueError): transforms.Pad([1.0, 2.0, 3.0]) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, '1') with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, {}) with self.assertRaises(TypeError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, 1, padding_mode=-1) with self.assertRaises(ValueError): fake_img = self.create_image((100, 120, 3)) F.pad(fake_img, [1.0, 2.0, 3.0]) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, '1') with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, {}) with self.assertRaises(TypeError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, 1, padding_mode=-1) with self.assertRaises(ValueError): tensor_img = paddle.rand((3, 100, 100)) F.pad(tensor_img, [1.0, 2.0, 3.0]) with self.assertRaises(ValueError): transforms.RandomRotation(-2) with self.assertRaises(ValueError): transforms.RandomRotation([1, 2, 3]) with self.assertRaises(ValueError): trans_gray = transforms.Grayscale(5) fake_img = self.create_image((100, 120, 3)) trans_gray(fake_img) with self.assertRaises(TypeError): transform = transforms.RandomResizedCrop(64) transform(1) with self.assertRaises(ValueError): transform = transforms.BrightnessTransform([-0.1, -0.2]) with self.assertRaises(TypeError): transform = transforms.BrightnessTransform('0.1') with self.assertRaises(ValueError): transform = transforms.BrightnessTransform('0.1', keys=1) with self.assertRaises(NotImplementedError): transform = transforms.BrightnessTransform('0.1', keys='a')