Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if arg.final:
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=normalize)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch, shuffle=True, num_workers=2)
test = torchvision.datasets.MNIST(root=data, train=False, download=True, transform=normalize)
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=normalize)
trainloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
shape = (1, 28, 28)
num_classes = 10
elif (arg.task == 'image-folder-bw'):
tr = transforms.Compose([transforms.Grayscale(), transforms.ToTensor()])
if arg.final:
train = torchvision.datasets.ImageFolder(root=arg.data + '/train/', transform=tr)
test = torchvision.datasets.ImageFolder(root=arg.data + '/test/', transform=tr)
trainloader = DataLoader(train, batch_size=arg.batch, shuffle=True)
testloader = DataLoader(train, batch_size=arg.batch, shuffle=True)
if arg.final:
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=normalize)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch, shuffle=True, num_workers=2)
test = torchvision.datasets.MNIST(root=data, train=False, download=True, transform=normalize)
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=normalize)
trainloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
shape = (1, 28, 28)
num_classes = 10
elif (arg.task == 'image-folder-bw'):
tr = transforms.Compose([transforms.Grayscale(), transforms.ToTensor()])
if arg.final:
train = torchvision.datasets.ImageFolder(root=arg.data + '/train/', transform=tr)
test = torchvision.datasets.ImageFolder(root=arg.data + '/test/', transform=tr)
trainloader = DataLoader(train, batch_size=arg.batch, shuffle=True)
testloader = DataLoader(train, batch_size=arg.batch, shuffle=True)
if arg.final:
train = torchvision.datasets.CIFAR10(root=data, train=True, download=True, transform=tfms)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch_size, shuffle=True, num_workers=2)
test = torchvision.datasets.CIFAR10(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch_size, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.CIFAR10(root=data, train=True, download=True, transform=tfms)
trainloader = DataLoader(train, batch_size=arg.batch_size, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.batch_size,
sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
elif (arg.task == 'imagenette'):
shape = (3, None, None)
num_classes = 10
if arg.augmentation:
tfms = transforms.Compose(
[transforms.RandomCrop(size=shape[1:], padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), Cutout(1, 8),]
)
if arg.final:
train = torchvision.datasets.ImageFolder(root=arg.data+os.sep+'train', transform=tfms)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch_size, shuffle=True, num_workers=2)
test = torchvision.datasets.ImageFolder(root=arg.data+os.sep+'val', transform=tfms)
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch_size, shuffle=False, num_workers=2)
if arg.final:
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=tfms)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch_size, shuffle=True, num_workers=0)
test = torchvision.datasets.MNIST(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch_size, shuffle=False, num_workers=0)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=tfms)
trainloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
elif (arg.task == 'cifar10'):
shape = (3, 32, 32)
num_classes = 10
data = arg.data + os.sep + arg.task
if arg.final:
train = torchvision.datasets.CIFAR10(root=data, train=True, download=True, transform=tfms)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch, shuffle=True, num_workers=2)
test = torchvision.datasets.CIFAR10(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch, shuffle=False, num_workers=2)
else:
)
if arg.final:
train = torchvision.datasets.ImageFolder(root=arg.data+os.sep+'train', transform=tfms)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch_size, shuffle=True, num_workers=2)
test = torchvision.datasets.ImageFolder(root=arg.data+os.sep+'val', transform=tfms)
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch_size, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 10_000
NUM_VAL = 3_000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.ImageFolder(root=arg.data+os.sep+'train', transform=tfms)
trainloader = DataLoader(train, batch_size=arg.batch_size, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.batch_size,
sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
else:
raise Exception('Task {} not recognized'.format(arg.task))
# Create model
if arg.model == 'efficient':
model = Classifier(shape, num_classes, k=arg.k, gadditional=arg.gadditional, radditional=arg.radditional, region=arg.region,
admode=arg.admode, sigma_scale=arg.sigma_scale, edges=arg.edges, constraints=arg.constraints)
sparse = True
elif arg.model == 'mini':
model = MiniClassifier(shape, num_classes, k=arg.k, gadditional=arg.gadditional, radditional=arg.radditional, region=arg.region,
admode=arg.admode, sigma_scale=arg.sigma_scale, edges=arg.edges, constraints=arg.constraints)
sparse = False
elif arg.model == '3c':
model = ThreeCClassifier(shape, num_classes, k=arg.k, gadditional=arg.gadditional, radditional=arg.radditional, region=arg.region,
data = arg.data + os.sep + arg.task
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=ToTensor())
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.bs, shuffle=True, num_workers=2)
test = torchvision.datasets.MNIST(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.bs, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.MNIST(root=arg.data, train=True, download=True, transform=ToTensor())
trainloader = DataLoader(train, batch_size=arg.bs, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.bs,
sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
elif (arg.task == 'cifar10'):
data = arg.data + os.sep + arg.task
if arg.final:
train = torchvision.datasets.CIFAR10(root=data, train=True, download=True, transform=ToTensor())
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.bs, shuffle=True, num_workers=2)
test = torchvision.datasets.CIFAR10(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.bs, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=ToTensor())
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.bs, shuffle=True, num_workers=2)
test = torchvision.datasets.MNIST(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.bs, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.MNIST(root=arg.data, train=True, download=True, transform=ToTensor())
trainloader = DataLoader(train, batch_size=arg.bs, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.bs,
sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
elif (arg.task == 'cifar10'):
data = arg.data + os.sep + arg.task
if arg.final:
train = torchvision.datasets.CIFAR10(root=data, train=True, download=True, transform=ToTensor())
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.bs, shuffle=True, num_workers=2)
test = torchvision.datasets.CIFAR10(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.bs, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
data = arg.data + os.sep + arg.task
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=ToTensor())
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.bs, shuffle=True, num_workers=2)
test = torchvision.datasets.MNIST(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.bs, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.MNIST(root=arg.data, train=True, download=True, transform=ToTensor())
trainloader = DataLoader(train, batch_size=arg.bs, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.bs,
sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
elif (arg.task == 'cifar10'):
data = arg.data + os.sep + arg.task
if arg.final:
train = torchvision.datasets.CIFAR10(root=data, train=True, download=True, transform=ToTensor())
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.bs, shuffle=True, num_workers=2)
test = torchvision.datasets.CIFAR10(root=data, train=False, download=True, transform=ToTensor())
testloader = torch.utils.data.DataLoader(test, batch_size=arg.bs, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
if arg.final:
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=normalize)
trainloader = torch.utils.data.DataLoader(train, batch_size=arg.batch, shuffle=True, num_workers=2)
test = torchvision.datasets.MNIST(root=data, train=False, download=True, transform=normalize)
testloader = torch.utils.data.DataLoader(test, batch_size=arg.batch, shuffle=False, num_workers=2)
else:
NUM_TRAIN = 45000
NUM_VAL = 5000
total = NUM_TRAIN + NUM_VAL
train = torchvision.datasets.MNIST(root=data, train=True, download=True, transform=normalize)
trainloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(0, NUM_TRAIN, total))
testloader = DataLoader(train, batch_size=arg.batch, sampler=util.ChunkSampler(NUM_TRAIN, NUM_VAL, total))
shape = (1, 28, 28)
num_classes = 10
elif (arg.task == 'image-folder-bw'):
tr = transforms.Compose([transforms.Grayscale(), transforms.ToTensor()])
if arg.final:
train = torchvision.datasets.ImageFolder(root=arg.data + '/train/', transform=tr)
test = torchvision.datasets.ImageFolder(root=arg.data + '/test/', transform=tr)
trainloader = DataLoader(train, batch_size=arg.batch, shuffle=True)
testloader = DataLoader(train, batch_size=arg.batch, shuffle=True)
else: