def prepare_cv_datasets(dataname, batch_size): if dataname == 'mnist': ordinary_train_dataset = dsets.MNIST(root='/DATA1/datasets/mnist', train=True, transform=transforms.ToTensor(), download=False) test_dataset = dsets.MNIST(root='/DATA1/datasets/mnist', train=False, transform=transforms.ToTensor()) elif dataname == 'kmnist': ordinary_train_dataset = dsets.KMNIST(root='/DATA1/datasets/kmnist', train=True, transform=transforms.ToTensor(), download=False) test_dataset = dsets.KMNIST(root='/DATA1/datasets/kmnist', train=False, transform=transforms.ToTensor()) elif dataname == 'fashion': ordinary_train_dataset = dsets.FashionMNIST( root='/DATA1/datasets/fashion_mnist', train=True, transform=transforms.ToTensor(), download=False) test_dataset = dsets.FashionMNIST(root='/DATA1/datasets/fashion_mnist', train=False, transform=transforms.ToTensor()) elif dataname == 'cifar10': train_transform = transforms.Compose([ transforms.ToTensor( ), # transforms.RandomHorizontalFlip(), transforms.RandomCrop(32,4), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)) ]) test_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)) ]) ordinary_train_dataset = dsets.CIFAR10(root='/DATA1/datasets/cifar10', train=True, transform=train_transform, download=False) test_dataset = dsets.CIFAR10(root='/DATA1/datasets/cifar10', train=False, transform=test_transform) train_loader = torch.utils.data.DataLoader(dataset=ordinary_train_dataset, batch_size=batch_size, shuffle=True, num_workers=0) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False, num_workers=0) full_train_loader = torch.utils.data.DataLoader( dataset=ordinary_train_dataset, batch_size=len(ordinary_train_dataset.data), shuffle=True, num_workers=0) num_classes = 10 return (full_train_loader, train_loader, test_loader, ordinary_train_dataset, test_dataset, num_classes)
import torch.nn.functional as F from torchvision import transforms, datasets #여러 환경에서 돌아가야 하는 코드를 공유할 때 USE_CUDA = torch.cuda.is_available() DEVICE = torch.device("cuda" if USE_CUDA else "cpu") #학습 데이터 전체를 총 몇 번이나 볼 것인가 EPOCHS = 30 BATCH_SIZE = 64 transform = transforms.Compose([transforms.ToTensor()]) #학습용 트레이닝셋, 성능 평가용 테스트셋 trainset = datasets.FashionMNIST(root='./.data/', train=True, download=True, transform=transform) testset = datasets.FashionMNIST(root='./.data/', train=False, download=True, transform=transform) #매개변수에 앞서 불러온 데이터셋을 넣어주고 배치 크기를 지정해준다. train_loader = torch.utils.data.DataLoader(dataset=trainset, batch_size=BATCH_SIZE, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=testset, batch_size=BATCH_SIZE, shuffle=True)
def _get_data(self): if self.args.dataset_name == 'rot_mnist': data_obj_train = datasets.MNIST(self.root, train=True, download=self.download, transform=transforms.ToTensor()) data_obj_test = datasets.MNIST(self.root, train=False, download=self.download, transform=transforms.ToTensor()) mnist_imgs = torch.cat((data_obj_train.data, data_obj_test.data)) mnist_labels = torch.cat( (data_obj_train.targets, data_obj_test.targets)) elif self.args.dataset_name == 'fashion_mnist': data_obj_train = datasets.FashionMNIST( self.root, train=True, download=self.download, transform=transforms.ToTensor()) data_obj_test = datasets.FashionMNIST( self.root, train=False, download=self.download, transform=transforms.ToTensor()) mnist_imgs = torch.cat((data_obj_train.data, data_obj_test.data)) mnist_labels = torch.cat( (data_obj_train.targets, data_obj_test.targets)) # Get total number of labeled examples sup_inds = self.load_inds() mnist_labels = mnist_labels[sup_inds] mnist_imgs = mnist_imgs[sup_inds] mnist_size = mnist_labels.shape[0] print(type(mnist_imgs), mnist_labels.shape, mnist_imgs.shape) to_pil = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((self.args.img_w, self.args.img_h)) ]) to_augment = transforms.Compose([ transforms.RandomResizedCrop(self.args.img_w, scale=(0.7, 1.0)), transforms.RandomHorizontalFlip(), ]) to_tensor = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) # Choose subsets that should be included into the training training_list_img = [] training_list_labels = [] training_list_idx = [] training_list_size = [] # key: class labels, val: data indices indices_dict = {} for i in range(len(mnist_imgs)): key = int(mnist_labels[i].numpy()) if key not in indices_dict.keys(): indices_dict[key] = [] indices_dict[key].append(i) # if self.data_case == 'test': # self.list_train_domains= ['30', '45'] for domain in self.list_train_domains: # Run transforms mnist_img_rot = torch.zeros( (mnist_size, self.args.img_w, self.args.img_h)) mnist_idx = [] # Shuffling the images to create random across domains curr_indices_dict = copy.deepcopy(indices_dict) for key in curr_indices_dict.keys(): random.shuffle(curr_indices_dict[key]) for i in range(len(mnist_imgs)): if domain == '0': mnist_img_rot[i] = to_tensor(to_pil(mnist_imgs[i])) else: if self.data_case == 'train' and self.args.dataset_name == "fashion_mnist": mnist_img_rot[i] = to_tensor( to_augment( transforms.functional.rotate( to_pil(mnist_imgs[i]), int(domain)))) else: mnist_img_rot[i] = to_tensor( transforms.functional.rotate( to_pil(mnist_imgs[i]), int(domain))) mnist_idx.append(i) print('Source Domain ', domain) training_list_img.append(mnist_img_rot) training_list_labels.append(mnist_labels) training_list_idx.append(mnist_idx) training_list_size.append(mnist_img_rot.shape[0]) # Making domain size equivalent everywhere by random sampling if self.data_case == 'train': num_classes = 10 for y_c in range(num_classes): base_class_size = 0 base_class_idx = -1 for d_idx, domain in enumerate(self.list_train_domains): class_idx = training_list_labels[d_idx] == y_c curr_class_size = training_list_labels[d_idx][ class_idx].shape[0] if base_class_size < curr_class_size: base_class_size = curr_class_size base_class_idx = d_idx self.base_domain_size += base_class_size print('Max Class Size: ', base_class_size, base_class_idx, y_c) # Stack train_imgs = torch.cat(training_list_img) train_labels = torch.cat(training_list_labels) train_indices = np.array(training_list_idx) train_indices = np.reshape( train_indices, (train_indices.shape[0] * train_indices.shape[1])) self.training_list_size = training_list_size print(train_imgs.shape, train_labels.shape, train_indices.shape) print(self.training_list_size) # Create domain labels train_domains = torch.zeros(train_labels.size()) for idx in range(len(self.list_train_domains)): train_domains[idx * mnist_size:(idx + 1) * mnist_size] += idx # Shuffle everything one more time inds = np.arange(train_labels.size()[0]) np.random.shuffle(inds) train_imgs = train_imgs[inds] train_labels = train_labels[inds] train_domains = train_domains[inds].long() train_indices = train_indices[inds] # Convert to onehot y = torch.eye(10) train_labels = y[train_labels] # Convert to onehot d = torch.eye(len(self.list_train_domains)) train_domains = d[train_domains] print(train_imgs.shape, train_labels.shape, train_domains.shape, train_indices.shape) return train_imgs.unsqueeze( 1), train_labels, train_domains, train_indices
def fashion_mnist(): return collect_download_configs( lambda: datasets.FashionMNIST(ROOT, download=True), name="FashionMNIST")
model.eval() ret = np.empty(shape=0) for data in input_loader: output = model(data) pred = output.max(1, keepdim=True)[1] pred = torch.Tensor.cpu(pred).detach().numpy()[:, :] ret = np.append(ret, pred) return ret transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0, ), (1, )), ]) # normalize - zero mean and 1 std batchSize = 64 train_loader = torch.utils.data.DataLoader(datasets.FashionMNIST( './data', train=True, download=True, transform=transform), batch_size=batchSize, shuffle=True) # create the model for the run, my best - model C model = Model_C(image_size=28 * 28) lr = 0.001 optimizer = optim.Adam(model.parameters(), lr=lr) epohcs_number = 10 for epoch in range(epohcs_number): train(epoch, model) # predict from a file if (len(sys.argv) > 1): fileName = sys.argv[1] train_x = np.loadtxt(fileName) / 255 # divided by 255 as the traning set
plt.xticks([]), plt.yticks([]) if title is not None: plt.title(title) plt.savefig(path, dpi=100) if __name__ == "__main__": ''' ================ Plot the dataset ================ ''' m_loader = torch.utils.data.DataLoader(datasets.MNIST(root='../data', train=False,\ download=False, transform=transforms.ToTensor()), batch_size=25) f_loader = torch.utils.data.DataLoader(datasets.FashionMNIST(root='../data', train=False,\ download=False, transform=transforms.ToTensor()), batch_size=25) m_data, m_label = iter(m_loader).next() plot(m_data, m_label) f_data, f_label = iter(f_loader).next() plot(f_data, f_label) ''' ======================== Plot the reuslt of model ======================== ''' data = np.load('../data/xs_mnist.npy') adv_data = np.load('../data/advs_mnist.npy') labels = np.load('../data/ys_mnist.npy') dataset = Dataset(data, adv_data, labels)
def __init__(self, args): # parameters self.epoch = args.epoch self.sample_num = 64 self.batch_size = args.batch_size self.save_dir = args.save_dir self.result_dir = args.result_dir self.dataset = args.dataset self.log_dir = args.log_dir self.gpu_mode = args.gpu_mode self.model_name = args.gan_type # EBGAN parameters self.pt_loss_weight = 0.1 self.margin = max(1, self.batch_size / 64.) # margin for loss function # usually margin of 1 is enough, but for large batch size it must be larger than 1 # networks init self.G = generator(self.dataset) self.D = discriminator(self.dataset) self.G_optimizer = optim.Adam(self.G.parameters(), lr=args.lrG, betas=(args.beta1, args.beta2)) self.D_optimizer = optim.Adam(self.D.parameters(), lr=args.lrD, betas=(args.beta1, args.beta2)) if self.gpu_mode: self.G.cuda() self.D.cuda() self.MSE_loss = nn.MSELoss().cuda() else: self.MSE_loss = nn.MSELoss() print('---------- Networks architecture -------------') utils.print_network(self.G) utils.print_network(self.D) print('-----------------------------------------------') # load dataset if self.dataset == 'mnist': self.data_loader = DataLoader(datasets.MNIST( 'data/mnist', train=True, download=True, transform=transforms.Compose([transforms.ToTensor()])), batch_size=self.batch_size, shuffle=True) elif self.dataset == 'fashion-mnist': self.data_loader = DataLoader(datasets.FashionMNIST( 'data/fashion-mnist', train=True, download=True, transform=transforms.Compose([transforms.ToTensor()])), batch_size=self.batch_size, shuffle=True) elif self.dataset == 'celebA': train_set = get_training_set( '/home/tmp_data_dir/zhaoyu/CelebA/img_align_celeba/', '/home/tmp_data_dir/zhaoyu/CelebA/img_align_celeba/') # train_set = get_training_set('/home/xujinchang/pytorch-CycleGAN-and-pix2pix/datasets/celeA_part/train/', '/home/xujinchang/pytorch-CycleGAN-and-pix2pix/datasets/celeA_part/train/') self.data_loader = DataLoader(dataset=train_set, batch_size=self.batch_size, shuffle=True) # self.data_loader = utils.load_celebA('data/celebA', transform=transforms.Compose( # [transforms.CenterCrop(160), transforms.Scale(64), transforms.ToTensor()]), batch_size=self.batch_size, # shuffle=True) self.z_dim = 62 # fixed noise if self.gpu_mode: self.sample_z_ = Variable(torch.rand( (self.batch_size, self.z_dim)).cuda(), volatile=True) else: self.sample_z_ = Variable(torch.rand( (self.batch_size, self.z_dim)), volatile=True)
def load_data(dataset: str, iid: str): """Loads a dataset. :param dataset: Name of the dataset :param iid: True if the dataset must not be splitted by target value :return: Train dataset, test dataset """ path_to_dataset = '{0}/dataset/'.format(get_path_to_datasets()) if dataset == "fake": transform = transforms.ToTensor() train_data = datasets.FakeData(size=200, transform=transform) test_data = datasets.FakeData(size=200, transform=transform) elif dataset == 'cifar10': normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) transform_train = transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.RandomCrop(32, 4), transforms.ToTensor(), normalize, ]) transform_test = transforms.Compose([ transforms.ToTensor(), normalize, ]) train_data = datasets.CIFAR10(root=path_to_dataset, train=True, download=True, transform=transform_train) test_data = datasets.CIFAR10(root=path_to_dataset, train=False, download=True, transform=transform_test) elif dataset == 'mnist': # Normalization see : https://stackoverflow.com/a/67233938 transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) train_data = datasets.MNIST(root=path_to_dataset, train=True, download=False, transform=transform) test_data = datasets.MNIST(root=path_to_dataset, train=False, download=False, transform=transform) elif dataset == "fashion_mnist": train_transforms = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ]) val_transforms = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ]) # Download and load the training data train_data = datasets.FashionMNIST(path_to_dataset, download=True, train=True, transform=train_transforms) # Download and load the test data test_data = datasets.FashionMNIST(path_to_dataset, download=True, train=False, transform=val_transforms) elif dataset == "femnist": transform = transforms.Compose([transforms.ToTensor()]) train_data = FEMNISTDataset(path_to_dataset, download=True, train=True, transform=transform) test_data = FEMNISTDataset(path_to_dataset, download=True, train=False, transform=transform) elif dataset == "a9a": train_data = A9ADataset(train=True, iid=iid) test_data = A9ADataset(train=False, iid=iid) elif dataset == "mushroom": train_data = MushroomDataset(train=True, iid=iid) test_data = MushroomDataset(train=False, iid=iid) elif dataset == "phishing": train_data = PhishingDataset(train=True, iid=iid) test_data = PhishingDataset(train=False, iid=iid) elif dataset == "quantum": train_data = QuantumDataset(train=True, iid=iid) test_data = QuantumDataset(train=False, iid=iid) return train_data, test_data
parser.add_argument('--repeat', type=int, default=200) parser.add_argument('--ngpu' , type=int, default=1, help='number of GPUs to use') parser.add_argument('--state_E', default='./saved_models/fmnist/netE_pixel.pth', help='path to encoder checkpoint') parser.add_argument('--state_G', default='./saved_models/fmnist/netG_pixel.pth', help='path to encoder checkpoint') parser.add_argument('--state_E_bg', default='./saved_models/fmnist/netE_pixel_bg.pth', help='path to encoder checkpoint') parser.add_argument('--state_G_bg', default='./saved_models/fmnist/netG_pixel_bg.pth', help='path to encoder checkpoint') opt = parser.parse_args() cudnn.benchmark = True device = 'cuda:0' if torch.cuda.is_available() else 'cpu' dataset_fmnist = dset.FashionMNIST(root=opt.dataroot, train=False, download=True, transform=transforms.Compose([ transforms.Resize(opt.imageSize), transforms.ToTensor() ])) dataloader_fmnist = torch.utils.data.DataLoader(dataset_fmnist, batch_size=opt.batch_size, shuffle=True, num_workers=int(opt.workers)) dataset_mnist = dset.MNIST(root=opt.dataroot, train=False, download=True, transform=transforms.Compose([ transforms.Resize(opt.imageSize), transforms.ToTensor() ])) dataloader_mnist = torch.utils.data.DataLoader(dataset_mnist, batch_size=opt.batch_size, shuffle=True, num_workers=int(opt.workers)) # # dataset_cifar_test = dset.CIFAR10(root=opt.dataroot, download=True,train = False, # transform=transforms.Compose([
_, predictions = scores.max( 1) # output shape is 64x10 --> take max among the cols num_correct += (predictions == y).sum() num_samples += predictions.size(0) acc = (float(num_correct) / float(num_samples)) * 100 print(f"Got {num_correct} / {num_samples} with accuracy {acc:.2f}") model.train() return acc device = "cuda" if torch.cuda.is_available() else "cpu" train_dataset = datasets.FashionMNIST(root='./dataset', train=True, transform=transforms.ToTensor(), download=True) train_loader = DataLoader(dataset=train_dataset, batch_size=NetworkParameters.batch_size, shuffle=True) test_dataset = datasets.FashionMNIST(root='./dataset', train=False, transform=transforms.ToTensor(), download=True) test_loader = DataLoader(dataset=test_dataset, batch_size=NetworkParameters.batch_size, shuffle=True) model = FFNN(input_size=NetworkParameters.input_size, num_classes=NetworkParameters.num_class).to(device)
def main(args): writer = SummaryWriter('./logs/{0}'.format(args.output_folder)) save_filename = './models/{0}'.format(args.output_folder) if args.dataset in ['mnist', 'fashion-mnist', 'cifar10']: transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) if args.dataset == 'mnist': # Define the train & test datasets train_dataset = datasets.MNIST(args.data_folder, train=True, download=True, transform=transform) test_dataset = datasets.MNIST(args.data_folder, train=False, transform=transform) num_channels = 1 elif args.dataset == 'fashion-mnist': # Define the train & test datasets train_dataset = datasets.FashionMNIST(args.data_folder, train=True, download=True, transform=transform) test_dataset = datasets.FashionMNIST(args.data_folder, train=False, transform=transform) num_channels = 1 elif args.dataset == 'cifar10': # Define the train & test datasets train_dataset = datasets.CIFAR10(args.data_folder, train=True, download=True, transform=transform) test_dataset = datasets.CIFAR10(args.data_folder, train=False, transform=transform) num_channels = 3 valid_dataset = test_dataset elif args.dataset == 'miniimagenet': transform = transforms.Compose([ transforms.RandomResizedCrop(128), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) # Define the train, valid & test datasets train_dataset = MiniImagenet(args.data_folder, train=True, download=True, transform=transform) valid_dataset = MiniImagenet(args.data_folder, valid=True, download=True, transform=transform) test_dataset = MiniImagenet(args.data_folder, test=True, download=True, transform=transform) num_channels = 3 # Define the data loaders train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True) valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=args.batch_size, shuffle=False, drop_last=True, num_workers=args.num_workers, pin_memory=True) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=16, shuffle=True) # Fixed images for Tensorboard fixed_images, _ = next(iter(test_loader)) fixed_grid = make_grid(fixed_images, nrow=8, range=(-1, 1), normalize=True) writer.add_image('original', fixed_grid, 0) model = VectorQuantizedVAE(num_channels, args.hidden_size, args.k).to(args.device) optimizer = torch.optim.Adam(model.parameters(), lr=args.lr) # Generate the samples first once reconstruction = generate_samples(fixed_images, model, args) grid = make_grid(reconstruction.cpu(), nrow=8, range=(-1, 1), normalize=True) writer.add_image('reconstruction', grid, 0) best_loss = -1. for epoch in range(args.num_epochs): print(epoch) train(train_loader, model, optimizer, args, writer) loss, _ = test(valid_loader, model, args, writer) reconstruction = generate_samples(fixed_images, model, args) grid = make_grid(reconstruction.cpu(), nrow=8, range=(-1, 1), normalize=True) writer.add_image('reconstruction', grid, epoch + 1) if (epoch == 0) or (loss < best_loss): best_loss = loss with open('{0}/best.pt'.format(save_filename), 'wb') as f: torch.save(model.state_dict(), f) with open('{0}/model_{1}.pt'.format(save_filename, epoch + 1), 'wb') as f: torch.save(model.state_dict(), f)
def load_data(dataset, batch_size, no_validation=False, shuffle=False, data_file=None): """ Loads training, validation and test data from the required dataset. :param dataset: name of the dataset :param batch_size: batch size for loaded dataset :param shuffle: true if training and validation datasets should be shuffled each iteration :param no_validation: true if empty validation set should be returned :param data_file: directory of data files [needed for omniglot only] :return: (train data loader, validation data loader, test data loader) """ if dataset == "omniglot": all_data = torch.load(data_file) x_train, x_val, x_test = all_data # dummy y values since no classes for omniglot y_train = torch.zeros(x_train.size(0), 1) y_val = torch.zeros(x_val.size(0), 1) y_test = torch.zeros(x_test.size(0), 1) train = torch.utils.data.TensorDataset(x_train, y_train) val = torch.utils.data.TensorDataset(x_val, y_val) test = torch.utils.data.TensorDataset(x_test, y_test) if no_validation: train = torch.utils.data.ConcatDataset([train, val]) val = torch.utils.data.TensorDataset(torch.tensor([]), torch.tensor([])) train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size, shuffle=shuffle, drop_last=True) val_loader = torch.utils.data.DataLoader(val, batch_size=batch_size, shuffle=shuffle, drop_last=True) test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size, shuffle=False) elif dataset == "mnist": train_dataset = datasets.MNIST('./data', train=True, download=True, transform=transforms.ToTensor()) train_examples, val_examples = (50000, 10000) if no_validation: train_examples, val_examples = (60000, 0) train_set, val_set = torch.utils.data.random_split(train_dataset, [train_examples, val_examples]) train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=shuffle, drop_last=True) val_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=shuffle, drop_last=True) test_loader = torch.utils.data.DataLoader( datasets.MNIST('./data', train=False, transform=transforms.ToTensor()), batch_size=batch_size, shuffle=False) elif dataset == "fashion_mnist": train_dataset = datasets.FashionMNIST('./data', train=True, download=True, transform=transforms.ToTensor()) train_examples, val_examples = (50000, 10000) if no_validation: train_examples, val_examples = (60000, 0) train_set, val_set = torch.utils.data.random_split(train_dataset, [train_examples, val_examples]) train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=shuffle, drop_last=True) val_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=shuffle, drop_last=True) test_loader = torch.utils.data.DataLoader( datasets.FashionMNIST('./data', train=False, transform=transforms.ToTensor()), batch_size=batch_size, shuffle=False) else: print("Error: {} dataset not supported!") quit() print('--------------------------------') print("{} dataset loaded".format(dataset)) print("batch size: {}".format(batch_size)) print("train batches: {}".format(len(train_loader))) print("validation batches: {}".format(len(val_loader))) print("test batches: {}".format(len(test_loader))) if shuffle: print("train and validation data will be shuffled") return train_loader, val_loader, test_loader
def main(): parser = argparse.ArgumentParser() parser.add_argument('--batchSize', type=int, default=64) parser.add_argument('--Epochs', type=int, default=175) parser.add_argument('--no-cuda', action='store_true') parser.add_argument('--save') parser.add_argument('--seed', type=int, default=1) parser.add_argument('--opt', type=str, default='sgd', choices=('sgd', 'adam', 'rmsprop')) args = parser.parse_args() #Check for cuda args.cuda = not args.no_cuda and torch.cuda.is_available() args.save = args.save or 'postprocessing' setproctitle.setproctitle(args.save) #manual seed on CPU or GPU torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) #Path for saving the progress if os.path.exists(args.save): shutil.rmtree(args.save) os.makedirs(args.save, exist_ok=True) # mean and std of the Fashion-MNIST train dataset images normMean = [0.2860405969887955] normStd = [0.35302424451492237] normTransform = transforms.Normalize(normMean, normStd) # Transforms : Random crop, random horizontal flip trainTransform = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normTransform ]) # # Transforms : RandomRotation, RandomVerticalFlip # trainTransform = transforms.Compose([ # transforms.RandomRotation(90), # transforms.RandomVerticalFlip(), # transforms.ToTensor(), # normTransform # ]) testTransform = transforms.Compose([transforms.ToTensor(), normTransform]) #Loading the datasets, if not found will be downloaded kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {} loader_train = DataLoader(dataset.FashionMNIST(root='Fashion-MNIST', train=True, download=True, transform=trainTransform), batch_size=args.batchSize, shuffle=True, **kwargs) loader_test = DataLoader(dataset.FashionMNIST(root='Fashion-MNIST', train=False, download=True, transform=testTransform), batch_size=args.batchSize, shuffle=False, **kwargs) # Calling the Densenet dense_net = densenet.DenseNet(growthRate=15, depth=100, reduction=0.5, bottleneck=True, nClasses=10) print(' + Number of params: {}'.format( sum([p.data.nelement() for p in dense_net.parameters()]))) if args.cuda: dense_net = dense_net.cuda() else: print("no cuda") #Choosing the optimizer if args.opt == 'sgd': optimizer = optim.SGD(dense_net.parameters(), lr=1e-1, momentum=0.9, weight_decay=1e-4) elif args.opt == 'adam': optimizer = optim.Adam(dense_net.parameters(), weight_decay=1e-4) elif args.opt == 'rmsprop': optimizer = optim.RMSprop(dense_net.parameters(), weight_decay=1e-4) #Progress being saved to csv files pfile_train = open(os.path.join(args.save, 'train.csv'), 'w') pfile_test = open(os.path.join(args.save, 'test.csv'), 'w') # running the training loop for epoch in range(1, args.Epochs + 1): adjust_optimizer(args.opt, optimizer, epoch) train(args, epoch, dense_net, loader_train, optimizer, pfile_train) test(args, epoch, dense_net, loader_test, pfile_test) torch.save(dense_net, os.path.join(args.save, 'latest.pth')) os.system('./plot.py {} &'.format(args.save)) pfile_train.close() pfile_test.close() end = time.time() print(end - start)
def get_dataset(name, data_dir, train=True, size=64, lsun_categories=None): transform = transforms.Compose([ transforms.Resize(size), transforms.CenterCrop(size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), transforms.Lambda(lambda x: x + 1. / 128 * torch.rand(x.size())), ]) transformMnist = transforms.Compose([ transforms.Resize(size), transforms.CenterCrop(size), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)), ]) if name == 'image': dataset = datasets.ImageFolder(data_dir, transform) nlabels = len(dataset.classes) elif name == 'npy': # Only support normalization for now dataset = datasets.DatasetFolder(data_dir, npy_loader, ['npy']) nlabels = len(dataset.classes) elif name == 'imagenet32': dataset = ImageNetDataset(dir=data_dir, img_size=size) nlabels = 1000 elif name == 'imagenet64': dataset = ImageNetDataset(dir=data_dir, img_size=size) nlabels = 1000 elif name == 'mnist': dataset = datasets.MNIST(root=data_dir, train=train, download=True, transform=transformMnist) nlabels = 10 elif name == 'cifar10': if train: transform_train = transforms.Compose([ transforms.Resize(size), transforms.RandomCrop(size, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) dataset = datasets.CIFAR10(root=data_dir, train=train, download=True, transform=transform_train) else: transform_test = transforms.Compose([ transforms.Resize(size), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) dataset = datasets.CIFAR10(root=data_dir, train=train, download=True, transform=transform_test) nlabels = 10 elif name == 'fashionmnist': dataset = datasets.FashionMNIST(root=data_dir, train=train, download=True, transform=transformMnist) nlabels = 10 elif name == 'lsun': if lsun_categories is None: lsun_categories = 'train' dataset = datasets.LSUN(data_dir, lsun_categories, transform) nlabels = len(dataset.classes) elif name == 'lsun_class': dataset = datasets.LSUNClass(data_dir, transform, target_transform=(lambda t: 0)) nlabels = 1 elif name == 'celeba': dataset = datasets.ImageFolder(root=data_dir, transform=transform) nlabels = 1 else: raise NotImplemented return dataset, nlabels
test_dataloader = torch.utils.data.DataLoader( datasets.MNIST('./data/mnist', train=False, download=True, transform=transforms.Compose([ transforms.Resize(args.img_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])), batch_size=args.batch_size, shuffle=True) elif args.dataset == 'fmnist': os.makedirs('./data/fmnist', exist_ok=True) train_dataloader = torch.utils.data.DataLoader( datasets.FashionMNIST('./data/fmnist', train=True, download=True, transform=transforms.Compose([ transforms.Resize(args.img_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])), batch_size=args.batch_size, shuffle=True) test_dataloader = torch.utils.data.DataLoader( datasets.FashionMNIST('./data/fmnist', train=False, download=True, transform=transforms.Compose([ transforms.Resize(args.img_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])), batch_size=args.batch_size, shuffle=True) elif args.dataset == 'cifar10': os.makedirs('./data/cifar10', exist_ok=True)
help='number of epochs to train (default: 100)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('--data_dir', type=str, default='data', help='path of data directory') parser.add_argument('--lr', type=float, default=1e-4, help='learning rate') parser.add_argument('--nf', type=int, default=32, help='number of features') parser.add_argument('--result', type=str, default='results', help='dir of results') args = parser.parse_args() # prepare dataset trans = transforms.Compose([ transforms.Resize((32, 32)), transforms.ToTensor() ]) train_dataset = datasets.FashionMNIST(root = args.data_dir, download=True, transform=trans) train_loader = data_utils.DataLoader(train_dataset, args.batch_size, shuffle=True, num_workers=1) test_dataset = datasets.FashionMNIST(root = args.data_dir, download=True, transform = trans, train=False) test_loader = data_utils.DataLoader(test_dataset, args.batch_size, shuffle=False, num_workers=1) # define the network def decode_unit(input_features, output_features): conv_unit = nn.Sequential(nn.ConvTranspose2d(input_features, output_features, 4, stride=2, padding=1), nn.BatchNorm2d(output_features), nn.LeakyReLU(0.2)) return conv_unit def encode_unit(input_features, output_features): conv_unit = nn.Sequential(nn.Conv2d(input_features, output_features, 6, stride=2, padding=2), nn.BatchNorm2d(output_features), nn.LeakyReLU(0.2))
def main(): # train args parser = argparse.ArgumentParser( description='Disributional Sliced Wasserstein Autoencoder') parser.add_argument('--datadir', default='./', help='path to dataset') parser.add_argument('--outdir', default='./result', help='directory to output images') parser.add_argument('--batch-size', type=int, default=512, metavar='N', help='input batch size for training (default: 512)') parser.add_argument('--epochs', type=int, default=200, metavar='N', help='number of epochs to train (default: 200)') parser.add_argument('--lr', type=float, default=0.0005, metavar='LR', help='learning rate (default: 0.0005)') parser.add_argument( '--num-workers', type=int, default=16, metavar='N', help='number of dataloader workers if device is CPU (default: 16)') parser.add_argument('--seed', type=int, default=16, metavar='S', help='random seed (default: 16)') parser.add_argument('--g', type=str, default='circular', help='g') parser.add_argument('--num-projection', type=int, default=1000, help='number projection') parser.add_argument('--lam', type=float, default=1, help='Regularization strength') parser.add_argument('--p', type=int, default=2, help='Norm p') parser.add_argument('--niter', type=int, default=10, help='number of iterations') parser.add_argument('--r', type=float, default=1000, help='R') parser.add_argument('--latent-size', type=int, default=32, help='Latent size') parser.add_argument('--dataset', type=str, default='MNIST', help='(MNIST|FMNIST)') parser.add_argument( '--model-type', type=str, required=True, help= '(SWD|MSWD|DSWD|GSWD|DGSWD|JSWD|JMSWD|JDSWD|JGSWD|JDGSWD|CRAMER|JCRAMER|SINKHORN|JSINKHORN)' ) args = parser.parse_args() torch.random.manual_seed(args.seed) if (args.g == 'circular'): g_function = circular_function model_type = args.model_type latent_size = args.latent_size num_projection = args.num_projection dataset = args.dataset model_dir = os.path.join(args.outdir, model_type) assert dataset in ['MNIST', 'FMNIST'] assert model_type in [ 'SWD', 'MSWD', 'DSWD', 'GSWD', 'DGSWD', 'JSWD', 'JMSWD', 'JDSWD', 'JGSWD', 'JDGSWD', 'CRAMER', 'JCRAMER', 'SINKHORN', 'JSINKHORN' ] if not (os.path.isdir(args.datadir)): os.makedirs(args.datadir) if not (os.path.isdir(args.outdir)): os.makedirs(args.outdir) if not (os.path.isdir(args.outdir)): os.makedirs(args.outdir) if not (os.path.isdir(model_dir)): os.makedirs(model_dir) use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") print('batch size {}\nepochs {}\nAdam lr {} \n using device {}\n'.format( args.batch_size, args.epochs, args.lr, device.type)) # build train and test set data loaders if (dataset == 'MNIST'): image_size = 28 num_chanel = 1 train_loader = torch.utils.data.DataLoader( datasets.MNIST(args.datadir, train=True, download=True, transform=transforms.Compose( [transforms.ToTensor()])), batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers) test_loader = torch.utils.data.DataLoader(datasets.MNIST( args.datadir, train=False, download=True, transform=transforms.Compose([transforms.ToTensor()])), batch_size=64, shuffle=False, num_workers=args.num_workers) model = MnistAutoencoder(image_size=28, latent_size=args.latent_size, hidden_size=100, device=device).to(device) elif (dataset == 'FMNIST'): image_size = 28 num_chanel = 1 train_loader = torch.utils.data.DataLoader( datasets.FashionMNIST(args.datadir, train=True, download=True, transform=transforms.Compose( [transforms.ToTensor()])), batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers) test_loader = torch.utils.data.DataLoader(datasets.FashionMNIST( args.datadir, train=False, download=True, transform=transforms.Compose([transforms.ToTensor()])), batch_size=64, shuffle=False, num_workers=args.num_workers) model = MnistAutoencoder(image_size=28, latent_size=args.latent_size, hidden_size=100, device=device).to(device) if (model_type == 'DSWD' or model_type == 'DGSWD'): transform_net = TransformNet(28 * 28).to(device) op_trannet = optim.Adam(transform_net.parameters(), lr=args.lr, betas=(0.5, 0.999)) # train_net(28 * 28, 1000, transform_net, op_trannet) elif (model_type == 'JDSWD' or model_type == 'JDSWD2' or model_type == 'JDGSWD'): transform_net = TransformNet(args.latent_size + 28 * 28).to(device) op_trannet = optim.Adam(transform_net.parameters(), lr=args.lr, betas=(0.5, 0.999)) # train_net(args.latent_size + 28 * 28, 1000, transform_net, op_trannet) optimizer = optim.Adam(model.parameters(), lr=args.lr, betas=(0.5, 0.999)) fixednoise = torch.randn((64, latent_size)).to(device) for epoch in range(args.epochs): total_loss = 0.0 for batch_idx, (data, y) in tqdm(enumerate(train_loader, start=0)): if (model_type == 'SWD'): loss = model.compute_loss_SWD(data, torch.randn, num_projection, p=args.p) elif (model_type == 'GSWD'): loss = model.compute_loss_GSWD(data, torch.randn, g_function, args.r, num_projection, p=args.p) elif (model_type == 'MSWD'): loss, v = model.compute_loss_MSWD(data, torch.randn, p=args.p, max_iter=args.niter) elif (model_type == 'DSWD'): loss = model.compute_lossDSWD(data, torch.randn, num_projection, transform_net, op_trannet, p=args.p, max_iter=args.niter, lam=args.lam) elif (model_type == 'DGSWD'): loss = model.compute_lossDGSWD(data, torch.randn, num_projection, transform_net, op_trannet, g_function, r=args.r, p=args.p, max_iter=args.niter, lam=args.lam) elif (model_type == 'JSWD'): loss = model.compute_loss_JSWD(data, torch.randn, num_projection, p=args.p) elif (model_type == 'JGSWD'): loss = model.compute_loss_JGSWD(data, torch.randn, g_function, args.r, num_projection, p=args.p) elif (model_type == 'JDSWD'): loss = model.compute_lossJDSWD(data, torch.randn, num_projection, transform_net, op_trannet, p=args.p, max_iter=args.niter, lam=args.lam) elif (model_type == 'JDGSWD'): loss = model.compute_lossJDGSWD(data, torch.randn, num_projection, transform_net, op_trannet, g_function, r=args.r, p=args.p, max_iter=args.niter, lam=args.lam) elif (model_type == 'JMSWD'): loss, v = model.compute_loss_MSWD(data, torch.randn, p=args.p, max_iter=args.niter) elif (model_type == 'CRAMER'): loss = model.compute_loss_cramer(data, torch.randn) elif (model_type == 'JCRAMER'): loss = model.compute_loss_join_cramer(data, torch.randn) elif (model_type == 'SINKHORN'): loss = model.compute_wasserstein_vi_loss(data, torch.randn, n_iter=args.niter, p=args.p, e=1) elif (model_type == 'JSINKHORN'): loss = model.compute_join_wasserstein_vi_loss( data, torch.randn, n_iter=args.niter, p=args.p, e=1) optimizer.zero_grad() loss.backward() optimizer.step() total_loss += loss.item() total_loss /= (batch_idx + 1) print("Epoch: " + str(epoch) + " Loss: " + str(total_loss)) if (epoch % 1 == 0): model.eval() sampling(model_dir + '/sample_epoch_' + str(epoch) + ".png", fixednoise, model.decoder, 64, image_size, num_chanel) if (model_type[0] == 'J'): for _, (input, y) in enumerate(test_loader, start=0): input = input.to(device) input = input.view(-1, image_size**2) reconstruct( model_dir + '/reconstruction_epoch_' + str(epoch) + ".png", input, model.encoder, model.decoder, image_size, num_chanel, device) break model.train()
def __init__(self, which_set='Cifar-10', root=None, train=True, download=True, return_idxs=False, num_classes=10, aug=('random_order', 'random_h_flip', 'random_crop', 'random_rot_10', 'random_scale_0.9_1.1', 'random_shear_5', 'cutout'), cut_n_holes=1, cut_length=16, ): assert which_set in ['Cifar-10', 'MNIST', 'Fashion-MNIST'], \ "Must be Cifar, MNIST, or Fashion MNIST, {} not allowed".format(which_set) image_length = 32 if which_set == 'Cifar-10' else 28 normalizer = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) if which_set == 'MNIST': normalizer = transforms.Normalize((0.1307,), (0.3081,)) elif which_set == 'Fashion-MNIST': normalizer = transforms.Normalize((0.1307,), (0.3081,)) transforms_list = [] for augment in aug: ############### First do the things that don't change where the image is in the box if augment == 'random_h_flip': transforms_list.append(transforms.RandomHorizontalFlip()) if augment == 'random_v_flip': transforms_list.append(transforms.RandomVerticalFlip()) ############### Then mess with brightness etc. if augment == 'color_jitter': transforms_list.append(transforms.ColorJitter(brightness=0.3, contrast=0.3, hue=0.0)) ############### Now do some sheering/cropping/rotation that changes where the images is if augment == 'affine': rot_degrees = 0 scale_low = None scale_high = None shear_degrees = None for augment_inner in aug: if 'random_rot' in augment_inner: rot_degrees = int(augment_inner.split('_')[-1]) if 'random_scale' in augment_inner: scale_low = float(augment_inner.split('_')[-2]) scale_high = float(augment_inner.split('_')[-1]) if 'random_shear' in augment_inner: shear_degrees = int(augment_inner.split('_')[-1]) transforms_list.append(transforms.RandomAffine(degrees=rot_degrees, scale=None if (scale_low == None) or (scale_high == None) else (scale_low, scale_high), shear=shear_degrees)) if augment == 'random_crop': transforms_list.append(transforms.RandomCrop(size=[image_length, image_length], padding=4)) transform = transforms.Compose(transforms_list) if 'random_order' not in aug else transforms.Compose([transforms.RandomOrder(transforms=transforms_list)]) transform.transforms.append(transforms.ToTensor()) transform.transforms.append(normalizer) for augment in aug: ############### Finally do things that are related to regularisation if augment == 'cutout': transform.transforms.append(Cutout(n_holes=cut_n_holes, length=cut_length)) if augment == 'rainbowout': transform.transforms.append(RainbowOut()) if augment == 'channelswap': transform.transforms.append(ChannelSwap()) if which_set == 'MNIST': self.dataset = datasets.MNIST(root='../data/MNIST' if root is None else root, train=train, download=download, transform=transform) elif which_set == 'Fashion-MNIST': self.dataset = datasets.FashionMNIST(root='../data/Fashion-MNIST' if root is None else root, train=train, download=download, transform=transform) else: self.dataset = datasets.CIFAR10(root='../data/Cifar-10' if root is None else root, train=train, download=download, transform=transform) self.return_idxs = return_idxs self.num_classes = num_classes self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
def get_data(dataset, batch_size): # Get MNIST dataset. if dataset == 'MNIST': transform = transforms.Compose([ transforms.Resize(28), transforms.CenterCrop(28), transforms.ToTensor() ]) dataset = dsets.MNIST(root + 'mnist/', train='train', download=True, transform=transform) # Get SVHN dataset. elif dataset == 'SVHN': transform = transforms.Compose([ transforms.Resize(32), transforms.CenterCrop(32), transforms.ToTensor() ]) dataset = dsets.SVHN(root + 'svhn/', split='train', download=True, transform=transform) # Get FashionMNIST dataset. elif dataset == 'FashionMNIST': transform = transforms.Compose([ transforms.Resize(28), transforms.CenterCrop(28), transforms.ToTensor() ]) dataset = dsets.FashionMNIST(root + 'fashionmnist/', train='train', download=True, transform=transform) # Get CelebA dataset. # MUST ALREADY BE DOWNLOADED IN THE APPROPRIATE DIRECTOR DEFINED BY ROOT PATH! elif dataset == 'CelebA': transform = transforms.Compose([ transforms.Resize(32), transforms.CenterCrop(32), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) dataset = dsets.CelebA(root + 'celeba/', download=True, transform=transform) # Create dataloader. dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True) return dataloader
def __init__(self, args): # parameters self.epoch = args.epoch self.sample_num = 19 self.batch_size = args.batch_size self.save_dir = args.save_dir self.result_dir = args.result_dir self.dataset = args.dataset self.dataroot_dir = args.dataroot_dir self.log_dir = args.log_dir self.gpu_mode = args.gpu_mode self.num_workers = args.num_workers self.model_name = args.gan_type self.loss_option = args.loss_option if len(args.loss_option) > 0: self.model_name = self.model_name + '_' + args.loss_option self.loss_option = args.loss_option.split(',') if len(args.comment) > 0: self.model_name = self.model_name + '_' + args.comment self.lambda_ = 0.25 if self.dataset == 'MultiPie' or self.dataset == 'miniPie': self.Nd = 337 # 200 self.Np = 9 self.Ni = 20 self.Nz = 50 elif self.dataset == 'Bosphorus': self.Nz = 50 elif self.dataset == 'CASIA-WebFace': self.Nd = 10885 self.Np = 13 self.Ni = 20 self.Nz = 50 if not os.path.exists(self.result_dir + '/' + self.dataset + '/' + self.model_name): os.makedirs(self.result_dir + '/' + self.dataset + '/' + self.model_name) if not os.path.exists(os.path.join(self.save_dir, self.dataset, self.model_name)): os.makedirs(os.path.join(self.save_dir, self.dataset, self.model_name)) # load dataset data_dir = os.path.join( self.dataroot_dir, self.dataset ) if self.dataset == 'mnist': self.data_loader = DataLoader(datasets.MNIST(data_dir, train=True, download=True, transform=transforms.Compose( [transforms.ToTensor()])), batch_size=self.batch_size, shuffle=True) elif self.dataset == 'fashion-mnist': self.data_loader = DataLoader( datasets.FashionMNIST(data_dir, train=True, download=True, transform=transforms.Compose( [transforms.ToTensor()])), batch_size=self.batch_size, shuffle=True) elif self.dataset == 'celebA': self.data_loader = utils.CustomDataLoader(data_dir, transform=transforms.Compose( [transforms.CenterCrop(160), transforms.Scale(64), transforms.ToTensor()]), batch_size=self.batch_size, shuffle=True) elif self.dataset == 'MultiPie' or self.dataset == 'miniPie': self.data_loader = DataLoader( utils.MultiPie(data_dir, transform=transforms.Compose( [transforms.Scale(100), transforms.RandomCrop(96), transforms.ToTensor()])), batch_size=self.batch_size, shuffle=True) elif self.dataset == 'CASIA-WebFace': self.data_loader = utils.CustomDataLoader(data_dir, transform=transforms.Compose( [transforms.Scale(100), transforms.RandomCrop(96), transforms.ToTensor()]), batch_size=self.batch_size, shuffle=True) elif self.dataset == 'Bosphorus': self.data_loader = DataLoader( utils.Bosphorus(data_dir, use_image=True, skipCodes=['YR','PR','CR'], transform=transforms.ToTensor(), shape=128, image_shape=256), batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers) self.Nid = 105 self.Npcode = len(self.data_loader.dataset.posecodemap) # fixed samples for reconstruction visualization print( 'Generating fixed sample for visualization...' ) nPcodes = self.Npcode//4 nSamples = self.sample_num-nPcodes sample_x2D_s = [] sample_x3D_s = [] for iB, (sample_x3D_,sample_y_,sample_x2D_) in enumerate(self.data_loader): sample_x2D_s.append( sample_x2D_ ) sample_x3D_s.append( sample_x3D_ ) if iB > nSamples // self.batch_size: break sample_x2D_s = torch.cat( sample_x2D_s )[:nSamples,:,:,:] sample_x3D_s = torch.cat( sample_x3D_s )[:nSamples,:,:,:] sample_x2D_s = torch.split( sample_x2D_s, 1 ) sample_x3D_s = torch.split( sample_x3D_s, 1 ) sample_x2D_s += (sample_x2D_s[0],)*nPcodes sample_x3D_s += (sample_x3D_s[0],)*nPcodes self.sample_x2D_ = torch.cat( sample_x2D_s ) self.sample_x3D_ = torch.cat( sample_x3D_s ) self.sample_pcode_ = torch.zeros( nSamples+nPcodes, self.Npcode ) self.sample_pcode_[:nSamples,0]=1 for iS in range( nPcodes ): ii = iS%self.Npcode self.sample_pcode_[iS+nSamples,ii] = 1 self.sample_z_ = torch.rand( nSamples+nPcodes, self.Nz ) fname = os.path.join( self.result_dir, self.dataset, self.model_name, 'samples.png' ) nSpS = int(math.ceil( math.sqrt( nSamples+nPcodes ) )) # num samples per side utils.save_images(self.sample_x2D_[:nSpS*nSpS,:,:,:].numpy().transpose(0,2,3,1), [nSpS,nSpS],fname) fname = os.path.join( self.result_dir, self.dataset, self.model_name, 'sampleGT.npy') self.sample_x3D_.numpy().squeeze().dump( fname ) if self.gpu_mode: self.sample_x2D_ = Variable(self.sample_x2D_.cuda(), volatile=True) self.sample_x3D_ = Variable(self.sample_x3D_.cuda(), volatile=True) self.sample_z_ = Variable(self.sample_z_.cuda(), volatile=True) self.sample_pcode_ = Variable(self.sample_pcode_.cuda(), volatile=True) else: self.sample_x2D_ = Variable(self.sample_x2D_, volatile=True) self.sample_x3D_ = Variable(self.sample_x3D_, volatile=True) self.sample_z_ = Variable(self.sample_z_, volatile=True) self.sample_pcode_ = Variable(self.sample_pcode_, volatile=True) # networks init self.G_3Dto2D = generator3Dto2D(self.Nid, self.Npcode, self.Nz) self.D_2D = discriminator2D(self.Nid, self.Npcode) self.G_3Dto2D_optimizer = optim.Adam(self.G_3Dto2D.parameters(), lr=args.lrG, betas=(args.beta1, args.beta2)) self.D_2D_optimizer = optim.Adam(self.D_2D.parameters(), lr=args.lrD, betas=(args.beta1, args.beta2)) if self.gpu_mode: self.G_3Dto2D.cuda() self.D_2D.cuda() self.CE_loss = nn.CrossEntropyLoss().cuda() self.BCE_loss = nn.BCELoss().cuda() self.MSE_loss = nn.MSELoss().cuda() self.L1_loss = nn.L1Loss().cuda() else: self.CE_loss = nn.CrossEntropyLoss() self.BCE_loss = nn.BCELoss() self.MSE_loss = nn.MSELoss() self.L1_loss = nn.L1Loss() print('init done')
def main(): # Training settings parser = argparse.ArgumentParser(description='PyTorch MNIST') parser.add_argument('-m', '--model-directory', type=str, required=True, help='location to store trained model') parser.add_argument('-d', '--diagram-directory', type=str, required=False, help='location to store homology info') parser.add_argument('--batch-size', type=int, default=64, metavar='N', help='input batch size for training (default: 64)') parser.add_argument('--up-to', type=int, default=500, metavar='N', help='How many testing exmaples for creating diagrams') parser.add_argument('--test-batch-size', type=int, default=64, metavar='N', help='input batch size for testing (default: 1000)') parser.add_argument('--epochs', type=int, default=10, metavar='N', help='number of epochs to train (default: 10)') parser.add_argument('--lr', type=float, default=0.01, metavar='LR', help='learning rate (default: 0.01)') parser.add_argument('--momentum', type=float, default=0.5, metavar='M', help='SGD momentum (default: 0.5)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('-ct', '--create-diagrams', action='store_true', default=False, help='Whether to compute homology on dynamic graph after training') parser.add_argument('-ht', '--homology-train', action='store_true', default=False, help='Whether to compute homology on static graph during training') parser.add_argument('-da', '--dataset', type=str, required=True, help='which dataset to train on (mnist or fashionmnist)') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) device = torch.device("cuda" if use_cuda else "cpu") kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} if args.dataset == 'mnist': train_loader = torch.utils.data.DataLoader( datasets.MNIST('../data', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), ])), batch_size=args.batch_size, shuffle=True, **kwargs) test_loader = torch.utils.data.DataLoader( datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([ transforms.ToTensor(), ])), batch_size=args.test_batch_size, shuffle=False, **kwargs) if args.dataset == 'fashion': train_loader = torch.utils.data.DataLoader( datasets.MNIST('../data/fashion', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), ])), batch_size=args.batch_size, shuffle=True, **kwargs) test_loader = torch.utils.data.DataLoader( datasets.FashionMNIST('../data/fashion', train=False, download=True, transform=transforms.Compose([ transforms.ToTensor(), ])), batch_size=args.test_batch_size, shuffle=False, **kwargs) model = FFF().to(device) # optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) optimizer = optim.SGD(model.parameters(), lr=args.lr) res_df = [] for epoch in range(1, args.epochs + 1): train(args, model, device, train_loader, optimizer, epoch) if args.homology_train: res_df = test_homology(args,model,device,test_loader, epoch, res_df) else: test(args, model, device, test_loader) if args.homology_train and args.diagram_directory is not None: df_filename = model.save_string() df_filename = 'train_homology_' + df_filename[:df_filename.find('.pt')] + '.pkl' df_loc = os.path.join(args.diagram_directory, df_filename) res_df = pd.DataFrame(res_df) res_df.to_pickle(df_loc) save_path = os.path.join(args.model_directory, model.save_string()) torch.save(model.state_dict(), save_path) if args.diagram_directory is not None and args.create_diagrams: create_diagrams(args, model)
import torch from torch import nn from torch.utils.data import DataLoader from torchvision import datasets from torchvision.transforms import ToTensor, Lambda training_data = datasets.FashionMNIST(root="data", train=True, download=False, transform=ToTensor()) test_data = datasets.FashionMNIST(root="data", train=False, download=False, transform=ToTensor()) train_dataloader = DataLoader(training_data, batch_size=64) test_dataloader = DataLoader(test_data, batch_size=64) class NeuralNetwork(nn.Module): def __init__(self): super(NeuralNetwork, self).__init__() self.flatten = nn.Flatten() self.linear_relu_stack = nn.Sequential(nn.Linear(28 * 28, 512), nn.ReLU(), nn.Linear(512, 512), nn.ReLU(), nn.Linear(512, 10), nn.ReLU()) def forward(self, x): x = self.flatten(x)
def get_dataset(data_name='mnist', data_dir='data', train=True, label_id=None, crop_flip=True): """ Get a dataset. :param data_name: str, name of dataset. :param data_dir: str, base directory of data. :param train: bool, return train set if True, or test set if False. :param label_id: None or int, return data with particular label_id. :param crop_flip: bool, whether use crop_flip as data augmentation. :return: pytorch dataset. """ transform_1d_crop_flip = transforms.Compose([ transforms.Resize((32, 32)), transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), # transforms.Normalize((0.5,), (0.5,)) # 1-channel, scale to [-1, 1] ]) transform_1d = transforms.Compose([ transforms.Resize((32, 32)), transforms.ToTensor(), #transforms.Normalize((0.5,), (0.5, )) ]) transform_3d_crop_flip = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), #transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) transform_3d = transforms.Compose([ transforms.ToTensor(), #transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) if data_name == 'mnist': #if train: # when train is True, we use transform_1d_crop_flip by default unless crop_flip is set to False # transform = transform_1d if crop_flip is False else transform_1d_crop_flip #else: # transform = transform_1d dataset = datasets.MNIST(data_dir, train=train, download=True, transform=transform_1d) elif data_name == 'fashion': if train: # when train is True, we use transform_1d_crop_flip by default unless crop_flip is set to False transform = transform_1d if crop_flip is False else transform_1d_crop_flip else: transform = transform_1d dataset = datasets.FashionMNIST(data_dir, train=train, download=True, transform=transform) elif data_name == 'cifar10': if train: # when train is True, we use transform_1d_crop_flip by default unless crop_flip is set to False transform = transform_3d if crop_flip is False else transform_3d_crop_flip else: transform = transform_3d dataset = datasets.CIFAR10(data_dir, train=train, download=True, transform=transform) elif data_name == 'svhn': if train: # when train is True, we use transform_1d_crop_flip by default unless crop_flip is set to False transform = transform_3d if crop_flip is False else transform_3d_crop_flip split = 'train' else: transform = transform_3d split = 'test' dataset = datasets.SVHN(data_dir, split=split, download=True, transform=transform) else: print('dataset {} is not available'.format(data_name)) if label_id is not None: # select samples with particular label if data_name == 'cifar10': #isinstance(dataset.targets, list): # for cifar10 targets = np.array(dataset.targets) idx = targets == label_id dataset.targets = list(targets[idx]) dataset.data = dataset.data[idx] elif data_name == 'svhn': idx = dataset.labels == label_id dataset.labels = dataset.labels[idx] dataset.data = dataset.data[idx] else: # for MNIST and FashionMNIST targets = dataset.targets data = dataset.data idx = targets == label_id dataset.targets = targets[idx] dataset.data = data[idx] return dataset
def load_fashion_mnist(batch_size, val_split=True, subset=[-1, -1, -1]): transformations = [transforms.ToTensor()] transformations.append(transforms.Normalize( (0.1307, ), (0.3081, ))) # This makes a huge difference! transform = transforms.Compose(transformations) if val_split: num_train = 50000 # Will split training set into 50,000 training and 10,000 validation images # Train set trainset = datasets.FashionMNIST(root='./data/fashion', train=True, download=True, transform=transform) trainset.data = trainset.train_data[:num_train, :, :] trainset.targets = trainset.train_labels[:num_train] # Validation set valset = datasets.FashionMNIST(root='./data/fashion', train=True, download=True, transform=transform) valset.data = valset.train_data[num_train:, :, :] valset.targets = valset.train_labels[num_train:] # Test set testset = datasets.FashionMNIST(root='./data/fashion', train=False, download=True, transform=transform) if subset[0] != -1: trainset = getSubset(trainset, subset[0]) if subset[2] != -1: testset = getSubset(testset, subset[2]) if subset[1] != -1: valset = getSubset(valset, subset[1]) train_dataloader = DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=0) # 50,000 images val_dataloader = DataLoader(valset, batch_size=batch_size, shuffle=True, num_workers=0) # 10,000 images test_dataloader = DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=0) # 10,000 images return train_dataloader, val_dataloader, test_dataloader else: trainset = datasets.FashionMNIST(root='./data/fashion', train=True, download=True, transform=transform) testset = datasets.FashionMNIST(root='./data/fashion', train=False, download=True, transform=transform) if subset[0] != -1: trainset = getSubset(trainset, subset[0]) if subset[2] != -1: testset = getSubset(testset, subset[2]) train_dataloader = DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=0) # 60,000 images test_dataloader = DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=0) # 10,000 images return train_dataloader, None, test_dataloader
(data.squeeze().detach().cpu().numpy(), target.item())) parser = argparse.ArgumentParser(description='PyTorch MNIST Example') parser.add_argument('--test-batch-size', type=int, default=1, metavar='N', help='input batch size for testing (default: 1)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') args = parser.parse_args() test_loader = torch.utils.data.DataLoader(datasets.FashionMNIST( '../data', train=False, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, ))])), batch_size=args.test_batch_size, shuffle=True) use_cuda = not args.no_cuda and torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") print("开始筛选----------------------------------") filter(args, BlackNet, device, test_loader) print("筛选完毕----------------------------------") print("得到{}个正确预测的样本".format(len(filter_data))) filter_data = np.array(filter_data) np.save('filter_data.npy', filter_data)
return x # model initalizatioin device = torch.device("cuda" if torch.cuda.is_available() else "cpu") hyperP = dict({'numEpoch': 10, 'lr': 1e-4, 'batchSize': 5}) if torch.cuda.is_available(): hyperP['batchSize'] = 600 myModel = VGG8() myLoss = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(myModel.parameters(), lr=hyperP['lr']) myModel.to(device) # Download and load train = DataLoader(datasets.FashionMNIST('data_fashionMNIST', train=True, download=True, transform=transforms.ToTensor()), shuffle=True, batch_size=hyperP['batchSize']) test = DataLoader(datasets.FashionMNIST('data_fashionMNIST', train=False, download=True, transform=transforms.ToTensor()), shuffle=False, batch_size=hyperP['batchSize']) print(sum(p.numel() for p in myModel.parameters() if p.requires_grad)) def testing(net, testloader): correct = 0
import torch from torchvision import datasets, transforms from torch import nn, optim import torch.nn.functional as F import helper import matplotlib.pyplot as plt # Define a transform to normalize the data transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) # Download and load the training data trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True) # Download and load the test data testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True) class Classifier(nn.Module): def __init__(self): super().__init__()
# 하이퍼파라미터 EPOCHS = 500 BATCH_SIZE = 100 USE_CUDA = torch.cuda.is_available() DEVICE = torch.device("cuda" if USE_CUDA else "cpu") print("Using Device:", DEVICE) # 학습에 필요한 데이터셋을 로딩합니다. # Fashion MNIST 데이터셋 trainset = datasets.FashionMNIST('./.data', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ])) train_loader = torch.utils.data.DataLoader( dataset = trainset, batch_size = BATCH_SIZE, shuffle = True) # 생성자는 64차원의 랜덤한 텐서를 입력받아 이에 행렬곱(Linear)과 활성화 함수(ReLU, Tanh) 연산을 실행합니다. 생성자의 결과값은 784차원, 즉 Fashion MNIST 속의 이미지와 같은 차원의 텐서입니다. # 생성자 (Generator) G = nn.Sequential( nn.Linear(64, 256), nn.ReLU(),
def add_noise(img): noise = torch.randn(img.size()) * 0.2 noisy_img = img + noise return noisy_img def plot_sample_img(img, name): img = img.view(1, 28, 28) save_image(img, './sample_{}.png'.format(name)) img_transform = transforms.Compose([ transforms.ToTensor(), ]) dataset = datasets.FashionMNIST('./data', transform=img_transform, download=True) dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True) class autoencoder(nn.Module): def __init__(self): super(autoencoder, self).__init__() self.encoder = nn.Sequential( nn.Linear(28 * 28, 500), nn.Tanh()) self.decoder = nn.Sequential( nn.Linear(500, 28*28), nn.Sigmoid()) def forward(self, x): x = self.encoder(x) x = self.decoder(x)
from torch import optim import torch.nn.functional as F from torchvision import datasets, transforms from torch.utils.data import Dataset, Subset, DataLoader, random_split MNIST_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize([0.1307], [0.3081]) ]) MNIST_train = datasets.MNIST('.', download=True, train = True, transform=MNIST_transform) MNIST_test = datasets.MNIST('.', download=True, train = False, transform=MNIST_transform) FASHION_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize([0.2859], [0.3530]) ]) FASHION_train = datasets.FashionMNIST('.', download=True, train=True, transform=MNIST_transform) FASHION_test = datasets.FashionMNIST('.', download=True, train=False, transform=FASHION_transform) class GridDataset(Dataset): def __init__(self, MNIST_dataset, FASHION_dataset): # pass in dataset assert len(MNIST_dataset) == len(FASHION_dataset) self.MNIST_dataset, self.FASHION_dataset = MNIST_dataset, FASHION_dataset self.targets = FASHION_dataset.targets torch.manual_seed(442) # Fix random seed for reproducibility N = len(MNIST_dataset) self.randpos = torch.randint(low=0,high=4,size=(N,)) # position of the FASHION-MNIST image self.randidx = torch.randint(low=0,high=N,size=(N,3)) # indices of MNIST images def __len__(self): return len(self.MNIST_dataset)