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 __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_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_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 build_model(self): """ DataLoader """ pad = int(30 * self.img_size // 256) train_transform = T.Compose([ T.RandomHorizontalFlip(), T.Resize((self.img_size + pad, self.img_size + pad)), T.RandomCrop(self.img_size), T.ToTensor(), T.Normalize(mean=0.5, std=0.5), ]) test_transform = T.Compose([ T.Resize((self.img_size, self.img_size)), T.ToTensor(), T.Normalize(mean=0.5, std=0.5) ]) self.trainA = ImageFolder('dataset/photo2cartoon/trainA', self.img_size, train_transform) self.trainB = ImageFolder('dataset/photo2cartoon/trainB', self.img_size, train_transform) self.testA = ImageFolder('dataset/photo2cartoon/testA', self.img_size, test_transform) self.testB = ImageFolder('dataset/photo2cartoon/testB', self.img_size, test_transform) self.trainA_loader = DataLoader(self.trainA, batch_size=self.batch_size, shuffle=True) self.trainB_loader = DataLoader(self.trainB, batch_size=self.batch_size, shuffle=True) self.testA_loader = DataLoader(self.testA, batch_size=1, shuffle=False) self.testB_loader = DataLoader(self.testB, batch_size=1, shuffle=False) """ Define Generator, Discriminator """ self.genA2B = ResnetGenerator(ngf=self.ch, img_size=self.img_size, light=self.light) self.genB2A = ResnetGenerator(ngf=self.ch, img_size=self.img_size, light=self.light) self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7) self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7) self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5) self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5) """ Define Loss """ self.L1_loss = nn.loss.L1Loss() self.MSE_loss = nn.loss.MSELoss() self.BCE_loss = nn.loss.BCEWithLogitsLoss() self.G_optim = paddle.optimizer.Adam( learning_rate=self.lr, beta1=0.5, beta2=0.999, weight_decay=0.0001, parameters=self.genA2B.parameters()+self.genB2A.parameters() ) self.D_optim = paddle.optimizer.Adam( learning_rate=self.lr, beta1=0.5, beta2=0.999, weight_decay=0.0001, parameters=self.disGA.parameters()+self.disGB.parameters()+self.disLA.parameters()+self.disLB.parameters() ) self.Rho_clipper = RhoClipper(0, self.rho_clipper) self.W_clipper = WClipper(0, self.w_clipper)
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 ])
train_image = self.transform(train_image) return train_image, 0 def __len__(self): return len(self.img_names) if __name__ == '__main__': from paddle.vision.transforms import transforms as T from paddle.io import DataLoader img_size = 256 pad = 30 train_transform = T.Compose([ T.RandomHorizontalFlip(), T.Resize((img_size + pad, img_size + pad)), T.RandomCrop(img_size), T.ToTensor(), T.Normalize(mean=0.5, std=0.5) ]) dataloader = ImageFolder('dataset/photo2cartoon/trainB', transform=train_transform) train_loader = DataLoader(dataloader, batch_size=1, shuffle=True) print('num: ', len(train_loader)) for i in range(300): print(i) try: real_A, _ = next(trainA_iter)