-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclassify_cifar10.py
175 lines (153 loc) · 6 KB
/
classify_cifar10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2019 Apple Inc. All Rights Reserved.
#
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision.datasets as dset
import torchvision.transforms as transforms
import argparse
import os
from models import *
parser = argparse.ArgumentParser()
parser.add_argument('--dataroot', type=str, default='.')
parser.add_argument('--imageSize', type=str, default='32x32')
parser.add_argument('--batchSize', type=int, default=100)
parser.add_argument('--lr', type=float, default=1e-6)
parser.add_argument('--ndf', type=int, default=256)
parser.add_argument('--ngf', type=int, default=256)
parser.add_argument('--nz', type=int, default=256)
parser.add_argument('--netG', default='', help="path to netG (to continue training)")
parser.add_argument('--netD', default='', help="path to netD (to continue training)")
parser.add_argument('--dropout', type=float, default=0.5)
parser.add_argument('--sample', type=int, default=1)
opt = parser.parse_args()
imageSize = map(int, opt.imageSize.split('x'))
train_set = dset.CIFAR10(root=opt.dataroot, train=True, download=True,
transform=transforms.Compose([
transforms.RandomCrop(imageSize, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
test_set = dset.CIFAR10(root=opt.dataroot, train=False, download=True,
transform=transforms.Compose([
transforms.Resize(imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
dataloader_train = torch.utils.data.DataLoader(train_set, batch_size=1,
shuffle=True, num_workers=8)
dataloader_test = torch.utils.data.DataLoader(test_set, batch_size=1,
shuffle=True, num_workers=8)
Discriminator = disc_dict[opt.imageSize]
Generator = gen_dict[opt.imageSize]
device = torch.device("cuda:0")
netD = Discriminator(opt.ndf).to(device)
netD.load_state_dict(torch.load(opt.netD))
paramsD = [p for p in netD.parameters() if p.requires_grad]
netG = Generator(opt.nz, opt.ngf).to(device)
netG.load_state_dict(torch.load(opt.netG))
def flatten(tensor_list, sample):
return torch.cat([t.flatten() for t in tensor_list])[::sample]
class Clf(nn.Module):
def __init__(self, nparams, opt):
super(Clf, self).__init__()
self.main = nn.Sequential(
nn.Dropout(opt.dropout),
nn.Linear(nparams, 10, bias=False)
)
self.main[-1].weight.data.zero_()
def forward(self, x):
return self.main(x)
print('calculating sample mean....')
noise = torch.randn(1024, opt.nz, 1, 1).to(device)
with torch.no_grad():
fake = netG(noise)
nexamples = 0
mean = 0
for i in range(fake.size(0)):
nexamples += 1
data = fake[i:i+1]
output = netD(data)
grad = torch.autograd.grad([output.mean()], paramsD)
grad = flatten(grad, opt.sample)
mean = mean + grad
mean = mean / nexamples
print('done! mean mean %.6f' % mean.abs().mean().item())
print('calculating sample var....')
nexamples = 0
var = 0
for i in range(fake.size(0)):
nexamples += 1
data = fake[i:i+1]
output = netD(data)
grad = torch.autograd.grad([output.mean()], paramsD)
grad = flatten(grad, opt.sample)
var = var + (grad - mean)**2
print('done! mean std %.6f' % var.sqrt().mean().item())
var = var / nexamples
nparams = mean.size(0)
print('num parameters %d' % nparams)
def get_feat(data, netD, mean, var):
output = netD(data)
grad = torch.autograd.grad([output.mean()], paramsD)
grad = flatten(grad, opt.sample)
feat = (grad - mean ) / (var.sqrt() + 1e-6)
return feat
clf = Clf(nparams, opt).to(device)
optimizer = optim.Adam(clf.parameters(), lr=opt.lr, weight_decay=0)
def criterion(pred, labels):
eye = torch.eye(10).to(device)
labels_onehot = eye[labels]
labels_signed = 2 * (labels_onehot - 0.5)
return torch.mean(F.relu(1 - labels_signed * pred)**2)
lr_sched = optim.lr_scheduler.MultiStepLR(optimizer, [50, 100, 150], 0.5)
print('Starting training....')
for epoch in range(200):
lr_sched.step()
avg_loss = 0
avg_acc = 0
images = []
labels = []
nexamples = 0
clf.train()
for data in dataloader_train:
images.append(get_feat(data[0].to(device), netD, mean, var))
labels.append(data[1].to(device))
if len(images) == opt.batchSize:
images = torch.stack(images, dim=0)
labels = torch.cat(labels, dim=0)
pred = clf(images)
loss = criterion(pred, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
nexamples += opt.batchSize
acc = (labels == pred.max(dim=1)[1]).float().mean()
avg_acc += acc
images = []
labels = []
print('epoch %d train loss %.4f acc %.4f' % (epoch, loss.item(),
avg_acc.item() * opt.batchSize / nexamples))
avg_acc = 0
images = []
labels = []
nexamples = 0
clf.eval()
for data in dataloader_test:
images.append(get_feat(data[0].to(device), netD, mean, var))
labels.append(data[1].to(device))
if len(images) == opt.batchSize:
images = torch.stack(images, dim=0)
labels = torch.cat(labels, dim=0)
pred = clf(images)
loss = criterion(pred, labels)
nexamples += images.size(0)
avg_acc += (labels == pred.max(dim=1)[1]).sum().float().item()
images = []
labels = []
print('epoch %d test loss %.4f acc %.4f\n' % (epoch, loss.item(), avg_acc / nexamples))