def __init__(self, network, learning_rate): """ Load Data, initialize a given network structure and set learning rate DO NOT MODIFY """ # Define a transform to normalize the data transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) # Download and load the training data trainset = datasets.KMNIST(root='./data', train=True, download=True, transform=transform) self.trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=False) # Download and load the test data testset = datasets.KMNIST(root='./data', train=False, download=True, transform=transform) self.testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False) self.model = network """ TODO: Set appropriate loss function such that learning is equivalent to minimizing the cross entropy loss. Note that we are outputting log-softmax values from our networks, not raw softmax values, so just using torch.nn.CrossEntropyLoss is incorrect. Hint: All networks output log-softmax values (i.e. log probabilities or.. likelihoods.). """ self.lossfn = nn.NLLLoss() self.optimizer = optim.Adam(self.model.parameters(), lr=learning_rate) self.num_train_samples = len(self.trainloader) self.num_test_samples = len(self.testloader)
def setup_data_loaders(self): if self.dataset == 'celeba': transform_list = [transforms.CenterCrop(140), transforms.Resize((64,64),PIL.Image.ANTIALIAS), transforms.ToTensor()] #if self.input_normalize_sym: #D = 64*64*3 #transform_list.append(transforms.LinearTransformation(2*torch.eye(D), -.5*torch.ones(D))) transform = transforms.Compose(transform_list) train_dataset = datasets.CelebA(self.datadir, split='train', target_type='attr', download=True, transform=transform) test_dataset = datasets.CelebA(self.datadir, split='test', target_type='attr', download=True, transform=transform) self.nlabels = 0 elif self.dataset == 'mnist': train_dataset = datasets.MNIST(self.datadir, train=True, target_transform=None, download=True, transform=transforms.Compose([transforms.ToTensor()])) test_dataset = datasets.MNIST(self.datadir, train=False, target_transform=None, download=True, transform=transforms.Compose([transforms.ToTensor()])) self.nlabels = 10 elif self.dataset == 'fashionmnist': train_dataset = datasets.FashionMNIST(self.datadir, train=True, target_transform=None, download=True, transform=transforms.Compose([transforms.ToTensor()])) test_dataset = datasets.FashionMNIST(self.datadir, train=False, target_transform=None, download=True, transform=transforms.Compose([transforms.ToTensor()])) self.nlabels = 10 elif self.dataset == 'kmnist': train_dataset = datasets.KMNIST(self.datadir, train=True, target_transform=None, download=True, transform=transforms.Compose([transforms.ToTensor()])) test_dataset = datasets.KMNIST(self.datadir, train=False, target_transform=None, download=True, transform=transforms.Compose([transforms.ToTensor()])) self.nlabels = 10 else: raise Exception("Dataset not found: " + dataset) if self.limit_train_size is not None: train_dataset = torch.utils.data.random_split(train_dataset, [self.limit_train_size, len(train_dataset)-self.limit_train_size])[0] self.train_loader = torch.utils.data.DataLoader(DatasetWithIndices(train_dataset, self.input_normalize_sym), batch_size=self.batch_size, shuffle=True, **self.dataloader_kwargs) self.test_loader = torch.utils.data.DataLoader(DatasetWithIndices(test_dataset, self.input_normalize_sym), batch_size=self.batch_size, shuffle=False, **self.dataloader_kwargs)
def load_kmnist(path="~/.datasets/", train_test_split=0.8, transform=transforms.ToTensor(), random_state=42): """Loads the Kuzushiji-MNIST dataset. Args: path (str): Path for the files. Returns: train_dataset, test_dataset, validation_dataset """ torch.manual_seed(random_state) ds = datasets.KMNIST(root=path, train=True, download=True, transform=transform) test_ds = datasets.KMNIST(root=path, train=False, download=True, transform=transform) train_size = int(train_test_split * len(ds)) test_size = len(ds) - train_size train_ds, val_ds = torch.utils.data.random_split(ds, [train_size, test_size]) return train_ds, val_ds, test_ds
def get_data(): data_dir = Path("/media/wwymak/Storage/kmnist") data_dir.mkdir(exist_ok=True) kmnist = datasets.KMNIST(data_dir, download=True) kmnist_test = datasets.KMNIST(data_dir, train=False) X_train = kmnist.data y_train = kmnist.targets X_test = kmnist_test.data y_test = kmnist_test.targets X_train = X_train.view(-1, 28 * 28).float() X_test = X_test.view(-1, 28 * 28).float() # train_mean = X_train.mean() # train_std = X_train.std() # # X_train = normalise(X_train, train_mean, train_std) # X_test = normalise(X_test, train_mean, train_std) # # X_train, X_test, y_train, y_test = map(from_numpy, (X_train, X_test, y_train, y_test)) # X_train, X_test = [x.float() for x in (X_train, X_test)] # y_train, y_test = [x.long() for x in (y_train, y_test)] # # X_train = X_train.reshape([X_train.shape[0], -1]) # X_test = X_test.reshape([X_test.shape[0], -1]) return X_train, X_test, y_train, y_test
def __init__(self, network, learning_rate): """ Load Data, initialize a given network structure and set learning rate """ # Define a transform to normalize the data transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, ))]) # Download and load the training data trainset = datasets.KMNIST(root='./data', train=True, download=True, transform=transform) self.trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=False) # Download and load the test data testset = datasets.KMNIST(root='./data', train=False, download=True, transform=transform) self.testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False) self.model = network self.lossfn = torch.nn.NLLLoss() self.optimizer = optim.Adam(self.model.parameters(), lr=learning_rate) self.num_train_samples = len(self.trainloader) self.num_test_samples = len(self.testloader)
def get_KMNIST( data_dir, image_size=28, mean=(0.19176215,), std=(0.33852664,), train_transform=None, test_transform=None, download=False ): if train_transform is None: train_transform = transforms.Compose([ transforms.RandomCrop(image_size, padding=4), transforms.RandomAffine(degrees=45, translate=(0.1, 0.1), scale=(0.8, 1.2)), transforms.ToTensor(), transforms.Normalize(mean, std) ]) if test_transform is None: test_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean, std) ]) in_channels = 1 classes = 10 dataset = datasets.KMNIST(data_dir, train=True, download=download, transform=train_transform) train_indices, validation_indices = split_train_and_validation_datasets(dataset, classes) train_dataset = KMNIST(data_dir, train_indices, train=True, download=download, transform=train_transform) validation_dataset = KMNIST(data_dir, validation_indices, train=True, download=download, transform=test_transform) test_dataset = datasets.KMNIST(data_dir, train=False, download=download, transform=test_transform) return train_dataset, validation_dataset, test_dataset, in_channels, classes
def get_dataset(dataset_name, model_name): train_dataset = [] test_dataset = [] trans = transforms.ToTensor() if model_name == "cnn" or model_name == "cnv": trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) if dataset_name == 'mnist': train_dataset = dsets.MNIST(root='./data', train=True, transform=trans, download=True) test_dataset = dsets.MNIST(root='./data', train=False, transform=trans) elif dataset_name == "kmnist": train_dataset = dsets.KMNIST(root='./data', train=True, transform=trans, download=True) test_dataset = dsets.KMNIST(root='./data', train=False, transform=trans) return train_dataset, test_dataset
def __init__(self, network, learning_rate): """ Load Data, initialize a given network structure and set learning rate DO NOT MODIFY """ # Define a transform to normalize the data transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, ))]) # Download and load the training data trainset = datasets.KMNIST(root='./data', train=True, download=True, transform=transform) self.trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=False) # Download and load the test data testset = datasets.KMNIST(root='./data', train=False, download=True, transform=transform) self.testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False) self.model = network """ TODO: Set appropriate loss function such that learning is equivalent to minimizing the cross entropy loss. Note that we are outputting log-softmax values from our networks, not raw softmax values, so just using torch.nn.CrossEntropyLoss is incorrect. Hint: All networks output log-softmax values (i.e. log probabilities or.. likelihoods.). """ # self.lossfn = lambda output, label: { # # 64,10 -> 64 # # all batch? # } # self.lossfn = lambda outputs, target: outputs[range(target.shape[0]), target].sum() # self.lossfn = lambda outputs, target: -1*outputs[range(target.shape[0]), target].mean() self.lossfn = F.nll_loss # this seems good # self.lossfn = torch.nn.NLLLoss() # difference? # or: # note: the cross entropy loss already includes the softmax # so the following loss functions are not correct # self.nNn = torch.nn.CrossEntropyLoss() # self.lossfn = lambda outputs, target: self.nNn(torch.exp(outputs), target) # or: try this # self.lossfn = lambda outputs, target: (torch.nn.CrossEntropyLoss())(torch.exp(outputs), target) # mean or sum? positive or negative? self.optimizer = optim.Adam(self.model.parameters(), lr=learning_rate) self.num_train_samples = len(self.trainloader) self.num_test_samples = len(self.testloader)
def get_kmnist_loaders(arguments): transformers = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, ))]) train_set = datasets.KMNIST(DATASET_PATH, train=True, download=True, transform=transformers) test_set = datasets.KMNIST(DATASET_PATH, train=False, download=True, transform=transformers) return load(arguments, test_set, train_set)
def main(): # command-line arguments parser = argparse.ArgumentParser() parser.add_argument('--net', type=str, default='full', help='lin, full or conv') parser.add_argument('--lr', type=float, default=0.01, help='learning rate') parser.add_argument('--mom', type=float, default=0.5, help='momentum') parser.add_argument('--epochs', type=int, default=10, help='number of training epochs') parser.add_argument('--no_cuda', action='store_true', default=False, help='disables CUDA') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() device = torch.device('cuda' if use_cuda else 'cpu') kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} # define a transform to normalize the data transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) # fetch and load training data trainset = datasets.KMNIST( root='./data', train=True, download=True, transform=transform) train_loader = torch.utils.data.DataLoader( trainset, batch_size=64, shuffle=False) # fetch and load test data testset = datasets.KMNIST( root='./data', train=False, download=True, transform=transform) test_loader = torch.utils.data.DataLoader( testset, batch_size=64, shuffle=False) # choose network architecture if args.net == 'lin': net = NetLin().to(device) elif args.net == 'full': net = NetFull().to(device) else: net = NetConv().to(device) if list(net.parameters()): # use SGD optimizer optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=args.mom) # training and testing loop for epoch in range(1, args.epochs + 1): train(args, net, device, train_loader, optimizer, epoch) test(args, net, device, test_loader)
def get_dataset(self): """ Uses torchvision.datasets.KMNIST to load dataset. Downloads dataset if doesn't exist already. Returns: torch.utils.data.TensorDataset: trainset, valset """ trainset = datasets.KMNIST('datasets/KMNIST/train/', train=True, transform=self.train_transforms, target_transform=None, download=True) valset = datasets.KMNIST('datasets/KMNIST/test/', train=False, transform=self.val_transforms, target_transform=None, download=True) return trainset, valset
def load_data(args, shuffle, droplast): # Transform to grayscale grayscale = transforms.Grayscale() transform = transforms.Compose([transforms.Lambda(lambda img: center_crop_square(img, min(*img.size))), transforms.Resize((args.imsize, args.imsize)), transforms.ToTensor(), grayscale]) # Make probability distribution? if args.prob: transform.transforms.append(transforms.Lambda(lambda x: x / x.sum())) # Dataset channels = 1 if args.data == 'mnist': # MNIST is already grayscale transform.transforms.remove(grayscale) train_data = datasets.MNIST('./', train=True, transform=transform, download=True) test_data = datasets.MNIST('./', train=False, transform=transform, download=True) elif args.data == 'kmnist': # KMNIST is already grayscale transform.transforms.remove(grayscale) train_data = datasets.KMNIST('./', train=True, transform=transform, download=True) test_data = datasets.KMNIST('./', train=False, transform=transform, download=True) elif args.data == 'cifar10': train_data = datasets.CIFAR10('./', train=True, transform=transform, download=True) test_data = datasets.CIFAR10('./', train=False, transform=transform, download=True) elif args.data == 'cifar100': train_data = datasets.CIFAR100('./', train=True, transform=transform, download=True) test_data = datasets.CIFAR100('./', train=False, transform=transform, download=True) elif args.data == 'yale': if not _check_exists('./CroppedYale'): _download('http://vision.ucsd.edu/extyaleb/CroppedYaleBZip/CroppedYale.zip', 'yale_DB') _unzip('./yale_DB.zip') dataset = datasets.ImageFolder('CroppedYale', transform = transform) train_data, test_data = torch.utils.data.random_split(dataset, lengths= [2000,452]) else: raise Exception(f'unknown data {args.data}') # Dataloader train_loader = data.DataLoader(train_data, batch_size=args.bsz, shuffle=shuffle, num_workers=4, pin_memory=True, drop_last=droplast) test_loader = data.DataLoader(test_data, batch_size=args.bsz, shuffle=shuffle, num_workers=4, pin_memory=True, drop_last=droplast) return train_loader, test_loader, channels
def prepare_kmnist(): m = (0.5, ) st = (0.5, ) normalize = tf.Normalize(m, st) train_set = datasets.KMNIST("../data", download=True, transform=tf.Compose( [tf.ToTensor(), normalize])) test_set = datasets.KMNIST("../data", download=True, train=False, transform=tf.Compose([tf.ToTensor(), normalize])) return train_set, test_set
def __init__(self, mnist_type='MNIST', train=True, flatten=True): if mnist_type == 'MNIST': self.dataset = datasets.MNIST('../../data', train=train, download=True) elif mnist_type == 'FashionMNIST': self.dataset = datasets.FashionMNIST('../../data', train=train, download=True) elif mnist_type == 'KMNIST': self.dataset = datasets.KMNIST('../../data', train=train, download=True) self.data = self.dataset.data self.base_transform = transforms.ToTensor() self.a_transform = transforms.Compose([ transforms.ToTensor(), # first, convert image to PyTorch tensor transforms.ToPILImage() ]) self.b_transform = transforms.Compose([ transforms.ToTensor(), transforms.Lambda(add_mnist_noise), transforms.Lambda(self.__threshold_func__) ]) self.targets = self.dataset.targets self.OHs = OH_digits(self.targets.numpy().astype(int)) self.filtered_classes = [] self.filtered_nums = [] for i in range(10): self.filtered_classes.append(self.data[self.targets == i]) self.filtered_nums.append(self.filtered_classes[i].shape[0]) self.flatten = flatten
def __init__(self, mnist_type='MNIST', train=True): if mnist_type == 'MNIST': self.dataset = datasets.MNIST('../../data', train=train, download=True) elif mnist_type == 'FashionMNIST': self.dataset = datasets.FashionMNIST('../../data', train=train, download=True) elif mnist_type == 'KMNIST': self.dataset = datasets.KMNIST('../../data', train=train, download=True) self.data = self.dataset.data self.base_transform = transforms.ToTensor() self.a_transform = transforms.Compose([ transforms.ToTensor(), # first, convert image to PyTorch tensor transforms.ToPILImage() # transforms.Normalize((self.mean,), (self.std,)) # normalize inputs ]) self.b_transform = transforms.Compose([ transforms.ToTensor(), transforms.Lambda(lambda x: x + torch.rand(28, 28)), transforms.Lambda(lambda x: self.__threshold_func__(x)) ]) self.targets = self.dataset.targets self.filtered_classes = [] self.filtered_nums = [] for i in range(10): self.filtered_classes.append(self.data[self.targets == i]) self.filtered_nums.append(self.filtered_classes[i].shape[0])
def get_dataset(data_name, data_root, image_size, train): transform = transforms.Compose( [transforms.Resize(image_size), transforms.ToTensor()]) if data_name == "mnist": dataset = datasets.MNIST(root=data_root, train=train, transform=transform, download=True) elif data_name == "fushion-mnist": dataset = datasets.FashionMNIST(root=data_root, train=train, transform=transform, download=True) elif data_name == "kmnist": dataset = datasets.KMNIST(root=data_root, train=train, transform=transform, download=True) elif data_name == "emnist": dataset = datasets.EMNIST(root=data_root, split="byclass", train=train, transform=transform, download=True) else: dataset = None return dataset
def __init__(self, mnist_type='MNIST', train=True, fixed=False, flatten=True): if mnist_type == 'MNIST': self.dataset = datasets.MNIST('../../data', train=train, download=True) elif mnist_type == 'FashionMNIST': self.dataset = datasets.FashionMNIST('../../data', train=train, download=True) elif mnist_type == 'KMNIST': self.dataset = datasets.KMNIST('../../data', train=train, download=True) self.data = self.dataset.data self.mean = torch.mean(self.data.float()) self.std = torch.std(self.data.float()) self.transform = transforms.Compose([transforms.ToTensor()]) self.targets = self.dataset.targets self.OHs = OH_digits(self.targets.numpy().astype(int)) self.fixed = fixed self.filtered_classes = [] self.filtered_nums = [] for i in range(10): self.filtered_classes.append(self.data[self.targets == i]) self.filtered_nums.append(self.filtered_classes[i].shape[0]) if fixed: self.view_b_indices = [] for i in range(10): self.view_b_indices.append(np.random.permutation(np.arange(len(self.data))[self.targets == i])) self.flatten = flatten
def get_data(name, path="./data", train=True): if name == "MNIST": dataset = datasets.MNIST(path, train=train, download=True) elif name == "KMNIST": dataset = datasets.KMNIST(path, train=train, download=True) images = dataset.data.float().unsqueeze(1) return TensorDataset(images / 255., dataset.targets)
def prepare_cv_datasets_hyper(dataname, batch_size): if dataname == 'mnist': ordinary_train_dataset = dsets.MNIST(root='~/datasets/classification', train=True, transform=transforms.ToTensor(), download=True) elif dataname == 'kmnist': ordinary_train_dataset = dsets.KMNIST(root='~/datasets/classification', train=True, transform=transforms.ToTensor(), download=True) elif dataname == 'fashion': ordinary_train_dataset = dsets.FashionMNIST( root='~/datasets/classification', train=True, transform=transforms.ToTensor(), download=True) 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)) ]) ordinary_train_dataset = dsets.CIFAR10( root='~/datasets/classification', train=True, transform=train_transform, download=True) dataset_size = len(ordinary_train_dataset) valid_proportion = 0.1 valid_size = int(np.floor(valid_proportion * dataset_size)) train_size = dataset_size - valid_size trainingset, validationset = torch.utils.data.random_split( ordinary_train_dataset, [train_size, valid_size]) train_loader = torch.utils.data.DataLoader(dataset=trainingset, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=0) valid_loader = torch.utils.data.DataLoader(dataset=validationset, batch_size=batch_size, shuffle=False, drop_last=False, num_workers=0) train_eval_loader = torch.utils.data.DataLoader(dataset=trainingset, batch_size=train_size, shuffle=False, drop_last=False, num_workers=0) num_classes = 10 return (train_eval_loader, train_loader, valid_loader, trainingset, validationset, num_classes)
def get_kmnist_loaders(arguments, mean=(0.5,), std=(0.5,)): print("Using mean", mean) # (0.1918,), (0.3483,) transformers = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean, std) ]) train_set = datasets.KMNIST( DATASET_PATH, train=True, download=True, transform=transformers ) test_set = datasets.KMNIST( DATASET_PATH, train=False, download=True, transform=transformers ) return load(arguments, test_set, train_set)
def axi_loader(batch_size): transform = transforms.Compose([ transforms.ToTensor(), ]) axi_data = datasets.KMNIST(root='./data', train=True, download=True, transform=transform) axi_loader = DataLoader(axi_data, batch_size=batch_size) axi_x, _ = next(iter(axi_loader)) return axi_x
def get_dataset(data_name, data_root, stage, max_stage, train): if data_name == "lsun": transform = transforms.Compose([ transforms.Resize(4 * 2 ** min(stage, max_stage)), transforms.CenterCrop(4 * 2 ** min(stage, max_stage)), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) else: transform = transforms.Compose([ transforms.Resize(4 * 2 ** min(stage, max_stage)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5])]) if data_name == "mnist": dataset = datasets.MNIST(root=data_root, train=train, transform=transform, download=True) elif data_name == "fushion-mnist": dataset = datasets.FashionMNIST(root=data_root, train=train, transform=transform, download=True) elif data_name == "kmnist": dataset = datasets.KMNIST(root=data_root, train=train, transform=transform, download=True) elif data_name == "emnist": dataset = datasets.EMNIST(root=data_root, split="balanced", train=train, transform=transform, download=True) elif data_name == "cifar10": dataset = datasets.CIFAR10(root=data_root, train=train, transform=transform, download=True) elif data_name == "cifar100": dataset = datasets.CIFAR100(root=data_root, train=train, transform=transform, download=True) elif data_name == "lsun": dataset = datasets.LSUN(root=data_root, classes="train", transform=transforms) elif data_name == "stl10": dataset = datasets.STL10(root=data_root, split="train", transform=transform, download=True) else: dataset = None return dataset
def __init__(self, mnist_type='MNIST', train=True, fixed=False): if mnist_type == 'MNIST': self.dataset = datasets.MNIST('../../data', train=train, download=True) elif mnist_type == 'FashionMNIST': self.dataset = datasets.FashionMNIST('../../data', train=train, download=True) elif mnist_type == 'KMNIST': self.dataset = datasets.KMNIST('../../data', train=train, download=True) self.data = self.dataset.data self.mean = torch.mean(self.data.float()) self.std = torch.std(self.data.float()) self.transform = transforms.Compose([ transforms.ToTensor( ), # first, convert image to PyTorch tensor # transforms.Lambda(lambda x: x/255.), # transforms.Normalize((self.mean,), (self.std,)) # normalize inputs ]) self.targets = self.dataset.targets self.fixed = fixed self.filtered_classes = [] self.filtered_nums = [] for i in range(10): self.filtered_classes.append(self.data[self.targets == i]) self.filtered_nums.append(self.filtered_classes[i].shape[0]) if fixed: self.view_b_indices = [] for i in range(10): self.view_b_indices.append( np.random.permutation( np.arange(len(self.data))[self.targets == i]))
def main(): # Training settings parser = argparse.ArgumentParser( description='Obtain confidence scores from trained networks') parser.add_argument('--test-batch-size', type=int, default=200, metavar='N', help='input batch size for testing (default: 200)') 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('--arch', type=str, default='wrn', metavar='ARC', help='neural network arch') parser.add_argument('--data', type=str, default='cifar10', metavar='DT', help='dataset the classifier was trained with') parser.add_argument('--test-data', type=str, default='fakedata', metavar='DT', help='dataset used to test the classifier') parser.add_argument('--lsun-path', type=str, help='path to LSUN dataset') parser.add_argument('--save_path', type=str, default='data', metavar='SP', help='path to save the produced confidence') parser.add_argument('--print-time', type=bool, default=False, metavar='PT', help='print elapsed time per batch') parser.add_argument( '--mode', type=str, default='msp', metavar='M', help= 'available modes: msp (maximum softmax probability), tmsp (t-softmax msp), tmsp0.5, tmsp5, tmsp10 (where number indicates nu value) odin. default: msp' ) parser.add_argument('--T', type=float, default=1000.0, metavar='T', help='ODIN temperature scaling') parser.add_argument('--eps', type=float, default=0.001, metavar='EPS', help='ODIN epsilon value') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") torch.manual_seed(args.seed) np.random.seed(args.seed) if device.type == 'cuda': torch.cuda.manual_seed(args.seed) kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} mean, std = get_normal(args.data) if args.test_data == "cifar10": test_transform = trn.Compose( [trn.ToTensor(), trn.Normalize(mean, std)]) test_loader = torch.utils.data.DataLoader( datasets.CIFAR10('../data', download=True, train=False, transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "cifar10_bw": test_transform = trn.Compose([ trn.Grayscale(), trn.Resize((28, 28)), trn.ToTensor(), trn.Normalize(mean, std) ]) test_loader = torch.utils.data.DataLoader( datasets.CIFAR10('../data', download=True, train=False, transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "svhn": test_transform = trn.Compose( [trn.ToTensor(), trn.Normalize(mean, std)]) test_loader = torch.utils.data.DataLoader( datasets.SVHN('../data', download=True, split='test', transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "fakedata": test_transform = trn.Compose( [trn.ToTensor(), trn.Normalize(mean, std)]) test_loader = torch.utils.data.DataLoader( datasets.FakeData(size=10000, image_size=(3, 32, 32), transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "fakedata_bw": test_transform = trn.Compose( [trn.ToTensor(), trn.Normalize(mean, std)]) test_loader = torch.utils.data.DataLoader( datasets.FakeData(size=10000, image_size=(1, 28, 28), transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "fakedata_wm": # wrong mean normalization mean = (50, 50, 50) test_transform = trn.Compose( [trn.ToTensor(), trn.Normalize(mean, std)]) test_loader = torch.utils.data.DataLoader( datasets.FakeData(size=10000, image_size=(3, 32, 32), transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "fmnist": test_transform = trn.Compose( [trn.ToTensor(), trn.Normalize(mean, std)]) test_loader = torch.utils.data.DataLoader( datasets.FashionMNIST('../data', download=True, train=False, transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "kmnist": test_transform = trn.Compose( [trn.ToTensor(), trn.Normalize(mean, std)]) test_loader = torch.utils.data.DataLoader( datasets.KMNIST('../data', download=True, train=False, transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "mnist": test_transform = trn.Compose([ trn.ToTensor(), # lambda x : x.repeat(3,1,1) * torch.rand(3,1,1), trn.Normalize(mean, std) ]) test_loader = torch.utils.data.DataLoader( datasets.MNIST('../data', download=True, train=False, transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) elif args.test_data == "lsun": test_transform = trn.Compose([ trn.Resize((32, 32)), #trn.CenterCrop(32), trn.ToTensor(), trn.Normalize(mean, std) ]) test_loader = torch.utils.data.DataLoader( datasets.LSUN(args.lsun_path, classes='test', transform=test_transform), batch_size=args.test_batch_size, shuffle=True, **kwargs) if args.data == "cifar10": Nc = 10 channels = 3 elif args.data == "svhn": Nc = 10 channels = 3 elif args.data == "fmnist": Nc = 10 channels = 1 elif args.data == "kmnist": Nc = 10 channels = 1 if (args.mode == 'msp') | (args.mode == 'odin') | (args.mode == 'cb'): nu = 0.0 elif args.mode == 'tmsp': nu = 1.0 elif args.mode == 'tmsp0.5': nu = 0.5 elif args.mode == 'tmsp5.0': nu = 5.0 elif args.mode == 'tmsp10.0': nu = 10.0 else: print('mode not recognized!') quit() model_path = 'models' model_path += '/' + args.data model_name = args.arch + 'nu{}'.format(nu) if args.arch == 'densenet': densenet_depth = 100 model = DenseNet(densenet_depth, Nc, nu=nu).to(device) elif args.arch == 'densenet_small': densenet_depth = 10 model = DenseNet(densenet_depth, Nc, nu=nu).to(device) elif args.arch == 'convnet': model = ConvNet(Nc, channels=channels, nu=nu).to(device) model.load_state_dict( torch.load(model_path + '/' + model_name + '.pt', map_location=device)) score, is_hit = test(args, model, device, test_loader) if not os.path.exists(args.save_path): os.makedirs(args.save_path) df = pd.DataFrame(data={'score': score, 'is_hit': is_hit}) df.to_csv('{}/{}_train{}_test{}_{}.csv'.format(args.save_path, args.arch, args.data, args.test_data, args.mode))
def main(): global args, best_err1, device, num_classes args = get_args() torch.manual_seed(args.random_seed) # most cases have 10 classes # if there are more, then it will be reassigned num_classes = 10 best_err1 = 100 # define datasets if args.target == "mnist": transform_train = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) train_set_all = datasets.MNIST('data', train=True, transform=transform_train, target_transform=None, download=True) # set aside 10000 examples from the training set for validation train_set, val_set = torch.utils.data.random_split( train_set_all, [50000, 10000]) # if we do experiments with variable target set size, this will take care of it # by default the target set size is 50000 target_set_size = min(50000, args.target_set_size) train_set, _ = torch.utils.data.random_split( train_set, [target_set_size, 50000 - target_set_size]) test_set = datasets.MNIST('data', train=False, transform=transform_test, target_transform=None, download=True) elif args.target == "kmnist": transform_train = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) train_set_all = datasets.KMNIST('data', train=True, transform=transform_train, target_transform=None, download=True) # set aside 10000 examples from the training set for validation train_set, val_set = torch.utils.data.random_split( train_set_all, [50000, 10000]) target_set_size = min(50000, args.target_set_size) # if we do experiments with variable target set size, this will take care of it train_set, _ = torch.utils.data.random_split( train_set, [target_set_size, 50000 - target_set_size]) test_set = datasets.KMNIST('data', train=False, transform=transform_test, target_transform=None, download=True) elif args.target == "k49": num_classes = 49 train_images = np.load('./data/k49-train-imgs.npz')['arr_0'] test_images = np.load('./data/k49-test-imgs.npz')['arr_0'] train_labels = np.load('./data/k49-train-labels.npz')['arr_0'] test_labels = np.load('./data/k49-test-labels.npz')['arr_0'] transform_train = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) # set aside about 10% of training data for validation train_set_all = K49Dataset(train_images, train_labels, transform=transform_train) train_set, val_set = torch.utils.data.random_split( train_set_all, [209128, 23237]) # currently we do not support variable target set size for k49 # enable this to use it # target_set_size = min(209128, args.target_set_size) # train_set, _ = torch.utils.data.random_split( # train_set, [target_set_size, 209128 - target_set_size]) test_set = K49Dataset(test_images, test_labels, transform=transform_test) elif args.target == "cifar10": normalize = transforms.Normalize( mean=[x / 255.0 for x in [125.3, 123.0, 113.9]], std=[x / 255.0 for x in [63.0, 62.1, 66.7]]) transform_train = transforms.Compose( [transforms.Resize((32, 32)), transforms.ToTensor(), normalize]) transform_test = transforms.Compose( [transforms.Resize((32, 32)), transforms.ToTensor(), normalize]) train_set_all = datasets.CIFAR10('data', train=True, transform=transform_train, target_transform=None, download=True) # set aside 5000 examples from the training set for validation train_set, val_set = torch.utils.data.random_split( train_set_all, [45000, 5000]) # if we do experiments with variable target set size, this will take care of it target_set_size = min(45000, args.target_set_size) train_set, _ = torch.utils.data.random_split( train_set, [target_set_size, 45000 - target_set_size]) test_set = datasets.CIFAR10('data', train=False, transform=transform_test, target_transform=None, download=True) elif args.target == "cifar100": num_classes = 100 normalize = transforms.Normalize( mean=[x / 255.0 for x in [125.3, 123.0, 113.9]], std=[x / 255.0 for x in [63.0, 62.1, 66.7]]) transform_train = transforms.Compose( [transforms.Resize((32, 32)), transforms.ToTensor(), normalize]) transform_test = transforms.Compose( [transforms.Resize((32, 32)), transforms.ToTensor(), normalize]) train_set_all = datasets.CIFAR100('data', train=True, transform=transform_train, target_transform=None, download=True) # set aside 5000 examples from the training set for validation train_set, val_set = torch.utils.data.random_split( train_set_all, [45000, 5000]) # if we do experiments with variable target set size, this will take care of it target_set_size = min(45000, args.target_set_size) train_set, _ = torch.utils.data.random_split( train_set, [target_set_size, 45000 - target_set_size]) test_set = datasets.CIFAR100('data', train=False, transform=transform_test, target_transform=None, download=True) # create data loaders if args.baseline: train_loader = torch.utils.data.DataLoader( train_set, batch_size=args.num_base_examples, shuffle=True) else: train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True) val_loader = torch.utils.data.DataLoader(val_set, batch_size=args.batch_size, shuffle=False) test_loader = torch.utils.data.DataLoader(test_set, batch_size=args.batch_size, shuffle=False) # create data loaders to get base examples if args.source == "emnist": train_set_source = datasets.EMNIST('data', 'letters', train=True, download=True, transform=transform_train, target_transform=None) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) elif args.source == "mnist": train_set_source = datasets.MNIST('data', train=True, download=True, transform=transform_train, target_transform=None) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) elif args.source == "kmnist": train_set_source = datasets.KMNIST('data', train=True, download=True, transform=transform_train, target_transform=None) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) elif args.source == "cifar10": train_set_source = datasets.CIFAR10('data', train=True, download=True, transform=transform_train, target_transform=None) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) elif args.source == "cifar100": train_set_source = datasets.CIFAR100('data', train=True, download=True, transform=transform_train, target_transform=None) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) elif args.source == "svhn": train_set_source = datasets.SVHN('data', split='train', download=True, transform=transform_train, target_transform=None) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) elif args.source == "cub": # modify the root depending on where you place the images cub_data_root = './data/CUB_200_2011/images' train_set_source = datasets.ImageFolder(cub_data_root, transform=transform_train, target_transform=None) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) elif args.source == "fake": # there is also an option to use random noise base examples if args.target == "mnist": num_channels = 1 dims = 28 else: num_channels = 3 dims = 32 train_set_source = datasets.FakeData(size=5000, image_size=(num_channels, dims, dims), num_classes=10, transform=transform_train, target_transform=None, random_offset=0) train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) else: # get the fixed images from the same dataset as the training data train_set_source = train_set train_loader_source = torch.utils.data.DataLoader( train_set_source, batch_size=args.num_base_examples, shuffle=True) if torch.cuda.is_available(): # checks whether a cuda gpu is available device = torch.cuda.current_device() print("use GPU", device) print("GPU ID {}".format(torch.cuda.current_device())) else: print("use CPU") device = torch.device('cpu') # sets the device to be CPU train_loader_source_iter = iter(train_loader_source) if args.balanced_source: # use a balanced set of fixed examples - same number of examples per class class_counts = {} fixed_input = [] fixed_target = [] for batch_fixed_i, batch_fixed_t in train_loader_source_iter: if sum(class_counts.values()) >= args.num_base_examples: break for fixed_i, fixed_t in zip(batch_fixed_i, batch_fixed_t): if len(class_counts.keys()) < num_classes: if int(fixed_t) in class_counts: if class_counts[ int(fixed_t )] < args.num_base_examples // num_classes: class_counts[int(fixed_t)] += 1 fixed_input.append(fixed_i) fixed_target.append(int(fixed_t)) else: class_counts[int(int(fixed_t))] = 1 fixed_input.append(fixed_i) fixed_target.append(int(fixed_t)) else: if int(fixed_t) in class_counts: if class_counts[ int(fixed_t )] < args.num_base_examples // num_classes: class_counts[int(fixed_t)] += 1 fixed_input.append(fixed_i) fixed_target.append(int(fixed_t)) fixed_input = torch.stack(fixed_input).to(device=device) fixed_target = torch.Tensor(fixed_target).to(device=device) else: # used for cross-dataset scenario - random selection of classes # not taking into accound the original classes fixed_input, fixed_target = next(train_loader_source_iter) fixed_input = fixed_input.to(device=device) fixed_target = fixed_target.to(device=device) # define loss function (criterion) criterion = nn.CrossEntropyLoss().to(device=device) # start at uniform labels and then learn them labels = torch.zeros((args.num_base_examples, num_classes), requires_grad=True, device=device) labels = labels.new_tensor( [[float(1.0 / num_classes) for e in range(num_classes)] for i in range(args.num_base_examples)], requires_grad=True, device=device) # define an optimizer for labels labels_opt = torch.optim.Adam([labels]) # enable using meta-architectures for second-order meta-learning # allows assigning fast weights cudnn.benchmark = True M.LeNetMeta.meta = True M.AlexCifarNetMeta.meta = True M.BasicBlockMeta.meta = True M.BottleneckMeta.meta = True M.ResNetMeta.meta = True # define the models to use if args.target == "cifar10" or args.target == "cifar100": if args.resnet: model = M.ResNetMeta(dataset=args.target, depth=18, num_classes=num_classes, bottleneck=False, device=device).to(device=device) model_name = 'resnet' else: model = M.AlexCifarNetMeta(args).to(device=device) model_name = 'alexnet' else: model = M.LeNetMeta(args).to(device=device) model_name = 'LeNet' optimizer = torch.optim.Adam(model.parameters()) if args.baseline: create_json_experiment_log(fixed_target) # remap the targets - only relevant in cross-dataset fixed_target = remap_targets(fixed_target, num_classes) # printing the labels helps ensure the seeds work print('The labels of the fixed examples are') print(fixed_target.tolist()) labels = one_hot(fixed_target.long(), num_classes) # add smoothing to the baseline if selected if args.label_smoothing > 0: labels = create_smooth_labels(labels, args.label_smoothing, num_classes) # use the validation set to find a suitable number of iterations for training num_baseline_steps, errors_list, num_steps_used = find_best_num_steps( val_loader, criterion, fixed_input, labels) print('Number of steps to use for the baseline: ' + str(num_baseline_steps)) experiment_update_dict = { 'num_baseline_steps': num_baseline_steps, 'errors_list': errors_list, 'num_steps_used': num_steps_used } update_json_experiment_log_dict(experiment_update_dict) if args.test_various_models: assert args.target == "cifar10", "test various models is only meant to be used for CIFAR-10" model_name_list = ['alexnet', 'LeNet', 'resnet'] for model_name_test in model_name_list: # do 20 repetitions of training from scratch for test_i in range(20): print('Test repetition ' + str(test_i)) test_err1, test_loss = test(test_loader, model_name_test, criterion, fixed_input, labels, num_baseline_steps) print('Test error (top-1 error):', test_err1) experiment_update_dict = { 'test_top_1_error_' + model_name_test: test_err1, 'test_loss_' + model_name_test: test_loss, 'num_test_steps_' + model_name_test: num_baseline_steps } update_json_experiment_log_dict(experiment_update_dict) else: # do 20 repetitions of training from scratch for test_i in range(20): print('Test repetition ' + str(test_i)) test_err1, test_loss = test(test_loader, model_name, criterion, fixed_input, labels, num_baseline_steps) print('Test error (top-1 error):', test_err1) experiment_update_dict = { 'test_top_1_error': test_err1, 'test_loss': test_loss, 'num_test_steps': num_baseline_steps } update_json_experiment_log_dict(experiment_update_dict) else: create_json_experiment_log(fixed_target) # start measuring time start_time = time.time() # initialize variables to decide when to restart a model ma_list = [] ma_sum = 0 lowest_ma_sum = 999999999 current_num_steps = 0 num_steps_list = [] num_steps_from_min = 0 val_err1 = 100.0 val_loss = 5.0 num_steps_val = 0 with tqdm.tqdm(total=args.epochs) as pbar_epochs: for epoch in range(0, args.epochs): train_err1, train_loss, labels, model_loss, ma_list, ma_sum, lowest_ma_sum, current_num_steps, num_steps_list, num_steps_from_min, model, optimizer = \ train(train_loader, model, fixed_input, labels, criterion, labels_opt, epoch, optimizer, ma_list, ma_sum, lowest_ma_sum, current_num_steps, num_steps_list, num_steps_from_min) # evaluate on the validation set only every 5 epochs as it can be quite expensive to train a new model from scratch if epoch % 5 == 4: # calculate the number of steps to use if len(num_steps_list) == 0: num_steps_val = current_num_steps else: num_steps_val = int(np.mean(num_steps_list[-3:])) val_err1, val_loss = validate(val_loader, model, criterion, epoch, fixed_input, labels, num_steps_val) if val_err1 <= best_err1: best_labels = labels.detach().clone() best_num_steps = num_steps_val best_err1 = min(val_err1, best_err1) print('Current best val error (top-1 error):', best_err1) pbar_epochs.update(1) experiment_update_dict = { 'train_top_1_error': train_err1, 'train_loss': train_loss, 'val_top_1_error': val_err1, 'val_loss': val_loss, 'model_loss': model_loss, 'epoch': epoch, 'num_val_steps': num_steps_val } # save the best labels so that we can analyse them if epoch == args.epochs - 1: experiment_update_dict['labels'] = best_labels.tolist() update_json_experiment_log_dict(experiment_update_dict) print('Best val error (top-1 error):', best_err1) # stop measuring time experiment_update_dict = {'total_train_time': time.time() - start_time} update_json_experiment_log_dict(experiment_update_dict) # this does number of steps analysis - what happens if we do more or fewer steps for test training if args.num_steps_analysis: num_steps_add = [-50, -20, -10, 0, 10, 20, 50, 100] for num_steps_add_item in num_steps_add: # start measuring time for testing start_time = time.time() local_errs = [] local_losses = [] local_num_steps = best_num_steps + num_steps_add_item print('Number of steps for training: ' + str(local_num_steps)) # each number of steps will have a robust estimate by using 20 repetitions for test_i in range(20): print('Test repetition ' + str(test_i)) test_err1, test_loss = test(test_loader, model_name, criterion, fixed_input, best_labels, local_num_steps) local_errs.append(test_err1) local_losses.append(test_loss) print('Test error (top-1 error):', test_err1) experiment_update_dict = { 'test_top_1_error': local_errs, 'test_loss': local_losses, 'total_test_time': time.time() - start_time, 'num_test_steps': local_num_steps } update_json_experiment_log_dict(experiment_update_dict) else: if args.test_various_models: assert args.target == "cifar10", "test various models is only meant to be used for CIFAR-10" model_name_list = ['alexnet', 'LeNet', 'resnet'] for model_name_test in model_name_list: for test_i in range(20): print(model_name_test) print('Test repetition ' + str(test_i)) test_err1, test_loss = test(test_loader, model_name_test, criterion, fixed_input, best_labels, best_num_steps) print('Test error (top-1 error):', test_err1) experiment_update_dict = { 'test_top_1_error_' + model_name_test: test_err1, 'test_loss_' + model_name_test: test_loss, 'total_test_time_' + model_name_test: time.time() - start_time, 'num_test_steps_' + model_name_test: best_num_steps } update_json_experiment_log_dict(experiment_update_dict) else: for test_i in range(20): print('Test repetition ' + str(test_i)) test_err1, test_loss = test(test_loader, model_name, criterion, fixed_input, best_labels, best_num_steps) print('Test error (top-1 error):', test_err1) experiment_update_dict = { 'test_top_1_error': test_err1, 'test_loss': test_loss, 'total_test_time': time.time() - start_time, 'num_test_steps': best_num_steps } update_json_experiment_log_dict(experiment_update_dict)
def kmnist(): return collect_download_configs( lambda: datasets.KMNIST(ROOT, download=True), name="KMNIST")
def load_datasets(name, root, batch_size): if name == "mnist": train_dataset = datasets.MNIST(root=root, download=True, train=True, transform=transforms.Compose([ transforms.Resize(28), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=8) test_dataset = datasets.MNIST(root=root, download=True, train=False, transform=transforms.Compose([ transforms.Resize(28), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=8) return train_dataloader, test_dataloader elif name == "fmnist": train_dataset = datasets.FashionMNIST( root=root, download=True, train=True, transform=transforms.Compose([ transforms.Resize(28), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=8) test_dataset = datasets.FashionMNIST(root=root, download=True, train=False, transform=transforms.Compose([ transforms.Resize(28), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=8) return train_dataloader, test_dataloader elif name == "kmnist": train_dataset = datasets.KMNIST(root=root, download=True, train=True, transform=transforms.Compose([ transforms.Resize(28), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=8) test_dataset = datasets.KMNIST(root=root, download=True, train=False, transform=transforms.Compose([ transforms.Resize(28), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=8) return train_dataloader, test_dataloader elif name == "qmnist": train_dataset = datasets.QMNIST(root=root, download=True, train=True, transform=transforms.Compose([ transforms.Resize(28), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=8) test_dataset = datasets.QMNIST(root=root, download=True, what="test50k", train=False, transform=transforms.Compose([ transforms.Resize(28), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ])) test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=8) return train_dataloader, test_dataloader elif name == "cifar10": train_dataset = datasets.CIFAR10(root=root, download=True, train=True, transform=transforms.Compose([ transforms.Resize(32), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize( (0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=8) test_dataset = datasets.CIFAR10(root=root, download=True, train=False, transform=transforms.Compose([ transforms.Resize(32), transforms.ToTensor(), transforms.Normalize( (0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=8) return train_dataloader, test_dataloader elif name == "cifar100": train_dataset = datasets.CIFAR100(root=root, download=True, train=True, transform=transforms.Compose([ transforms.Resize(32), transforms.ToTensor(), transforms.Normalize( (0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=8) test_dataset = datasets.CIFAR100(root=root, download=True, train=False, transform=transforms.Compose([ transforms.Resize(32), transforms.ToTensor(), transforms.Normalize( (0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=8) return train_dataloader, test_dataloader
def get_kmnist(path="./data", train=True): dataset = datasets.KMNIST(path, train=train, download=True) return ImageDataset(dataset.data.float().unsqueeze(1) / 255., dataset.targets, dataset.classes)
def build_datasets(self): if self.if_use_loacl_file: self.dataset_name = os.path.join(root, 'data', self.dataset_name) assert os.path.exists( self.dataset_name ), "There is no file named %s" % self.dataset_name def is_valid(img_path): """验证图像是否符合要求""" return (img_path.endswith('jpg') or img_path.endswith('jpeg') or img_path.endswith('png') ) and os.path.getsize(img_path) > 10 if self.if_train_val: if os.path.exists(os.path.join( self.dataset_name, 'train')) and os.path.exists( os.path.join(self.dataset_name, 'val')): if not isinstance(self.transform, dict): self.transform = { x: self.transform for x in ['train', 'val'] } datasets = { x: ImageFolder(os.path.join(self.dataset_name, x), self.transform[x]) for x in ['train', 'val'] } else: all_datasets = ImageFolder(self.dataset_name, transform=self.transform, is_valid_file=is_valid) train_size = int(self.hold_out_rate * len(all_datasets)) val_size = len(all_datasets) - train_size train_dataset, val_dataset = random_split( all_datasets, [train_size, val_size]) datasets = {'train': train_dataset, 'val': val_dataset} dataloaders = { x: DataLoader(datasets[x], batch_size=opt.batch_size, shuffle=opt.shuffle, num_workers=opt.num_workers) for x in ['train', 'val'] } dataset_sizes = {x: len(datasets[x]) for x in ['train', 'val']} try: class_names = datasets['train'].classes except: class_names = all_datasets.classes else: datasets = ImageFolder(self.dataset_name, transform=self.transform, is_valid_file=is_valid) dataloaders = DataLoader(datasets, batch_size=opt.batch_size, shuffle=opt.shuffle, num_workers=opt.num_workers) dataset_sizes = len(datasets) class_names = datasets.classes else: assert self.dataset_name in [ 'mnist', 'kmnist', 'cifa10' ], "Other datasets are not supported yet, you can load it local." # 其他的数据集暂时不支持 if not isinstance(self.transform, dict): self.transform = {x: self.transform for x in ['train', 'val']} if self.dataset_name == 'mnist': train_dataset = td.MNIST(root='.', train=True, transform=self.transform['train'], download=True) val_dataset = td.MNIST(root='.', train=False, transform=self.transform['val'], download=True) elif self.dataset_name == 'kmnist': train_dataset = td.KMNIST(root='.', train=True, transform=self.transform['train'], download=True) val_dataset = td.KMNIST(root='.', train=False, transform=self.transform['val'], download=True) else: train_dataset = td.CIFAR10(root='.', train=True, transform=self.transform['train'], download=True) val_dataset = td.CIFAR10(root='.', train=False, transform=self.transform['val'], download=True) datasets = {'train': train_dataset, 'val': val_dataset} dataloaders = { x: DataLoader(datasets[x], batch_size=opt.batch_size, shuffle=opt.shuffle, num_workers=opt.num_workers) for x in ['train', 'val'] } dataset_sizes = {x: len(datasets[x]) for x in ['train', 'val']} class_names = datasets['train'].classes class Result: def __init__(self, ds, dl, dn, cn): self.dataset = ds self.dataloader = dl self.dataset_size = dn self.class_name = cn res = Result(datasets, dataloaders, dataset_sizes, class_names) return res
dt = layer.backward(dt) def step(self, optimf): for layer in self.layers: layer.step(optimf) # load data numepoch = 100 batchsize = 128 import torch from torchvision import datasets from torchvision.transforms import ToTensor, Compose, Normalize train_loader = torch.utils.data.DataLoader(datasets.KMNIST('../data', train=True, transform=Compose( [ToTensor()])), batch_size=batchsize, shuffle=True) test_loader = torch.utils.data.DataLoader(datasets.KMNIST('../data', train=False, transform=Compose([ ToTensor(), ])), batch_size=batchsize, shuffle=True) net = Network() criterion = SoftwaxCrossEntropy() optimf = SGD()