-
Notifications
You must be signed in to change notification settings - Fork 15
/
train.py
351 lines (263 loc) · 10.2 KB
/
train.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST, CIFAR10
from torchvision.transforms import Compose, ToTensor, Normalize
from torchvision import transforms
from tensorboardX import SummaryWriter
from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
from ignite.metrics import Accuracy, Loss
from ignite.contrib.handlers import ProgressBar
from snip import SNIP
torch.manual_seed(42)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
LOG_INTERVAL = 20
INIT_LR = 0.1
WEIGHT_DECAY_RATE = 0.0005
EPOCHS = 250
REPEAT_WITH_DIFFERENT_SEED = 3
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def apply_prune_mask(net, keep_masks):
# Before I can zip() layers and pruning masks I need to make sure they match
# one-to-one by removing all the irrelevant modules:
prunable_layers = filter(
lambda layer: isinstance(layer, nn.Conv2d) or isinstance(
layer, nn.Linear), net.modules())
for layer, keep_mask in zip(prunable_layers, keep_masks):
assert (layer.weight.shape == keep_mask.shape)
def hook_factory(keep_mask):
"""
The hook function can't be defined directly here because of Python's
late binding which would result in all hooks getting the very last
mask! Getting it through another function forces early binding.
"""
def hook(grads):
return grads * keep_mask
return hook
# mask[i] == 0 --> Prune parameter
# mask[i] == 1 --> Keep parameter
# Step 1: Set the masked weights to zero (NB the biases are ignored)
# Step 2: Make sure their gradients remain zero
layer.weight.data[keep_mask == 0.] = 0.
layer.weight.register_hook(hook_factory(keep_mask))
class LeNet_300_100(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 300)
self.fc2 = nn.Linear(300, 100)
self.fc3 = nn.Linear(100, 10)
def forward(self, x):
x = F.relu(self.fc1(x.view(-1, 784)))
x = F.relu(self.fc2(x))
return F.log_softmax(self.fc3(x), dim=1)
class LeNet_5(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 6, 5, padding=2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc3 = nn.Linear(16 * 5 * 5, 120)
self.fc4 = nn.Linear(120, 84)
self.fc5 = nn.Linear(84, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.fc3(x.view(-1, 16 * 5 * 5)))
x = F.relu(self.fc4(x))
x = F.log_softmax(self.fc5(x))
return x
class LeNet_5_Caffe(nn.Module):
"""
This is based on Caffe's implementation of Lenet-5 and is slightly different
from the vanilla LeNet-5. Note that the first layer does NOT have padding
and therefore intermediate shapes do not match the official LeNet-5.
"""
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 20, 5, padding=0)
self.conv2 = nn.Conv2d(20, 50, 5)
self.fc3 = nn.Linear(50 * 4 * 4, 500)
self.fc4 = nn.Linear(500, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.fc3(x.view(-1, 50 * 4 * 4)))
x = F.log_softmax(self.fc4(x))
return x
VGG_CONFIGS = {
# M for MaxPool, Number for channels
'D': [
64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M',
512, 512, 512, 'M'
],
}
class VGG_SNIP(nn.Module):
"""
This is a base class to generate three VGG variants used in SNIP paper:
1. VGG-C (16 layers)
2. VGG-D (16 layers)
3. VGG-like
Some of the differences:
* Reduced size of FC layers to 512
* Adjusted flattening to match CIFAR-10 shapes
* Replaced dropout layers with BatchNorm
"""
def __init__(self, config, num_classes=10):
super().__init__()
self.features = self.make_layers(VGG_CONFIGS[config], batch_norm=True)
self.classifier = nn.Sequential(
nn.Linear(512, 512), # 512 * 7 * 7 in the original VGG
nn.ReLU(True),
nn.BatchNorm1d(512), # instead of dropout
nn.Linear(512, 512),
nn.ReLU(True),
nn.BatchNorm1d(512), # instead of dropout
nn.Linear(512, num_classes),
)
@staticmethod
def make_layers(config, batch_norm=False): # TODO: BN yes or no?
layers = []
in_channels = 3
for v in config:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
if batch_norm:
layers += [
conv2d,
nn.BatchNorm2d(v),
nn.ReLU(inplace=True)
]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
return nn.Sequential(*layers)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
x = F.log_softmax(x, dim=1)
return x
def get_mnist_dataloaders(train_batch_size, val_batch_size):
data_transform = Compose([transforms.ToTensor()])
# Normalise? transforms.Normalize((0.1307,), (0.3081,))
train_dataset = MNIST("_dataset", True, data_transform, download=True)
test_dataset = MNIST("_dataset", False, data_transform, download=False)
train_loader = DataLoader(
train_dataset,
train_batch_size,
shuffle=True,
num_workers=2,
pin_memory=True)
test_loader = DataLoader(
test_dataset,
val_batch_size,
shuffle=False,
num_workers=2,
pin_memory=True)
return train_loader, test_loader
def get_cifar10_dataloaders(train_batch_size, test_batch_size):
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
])
train_dataset = CIFAR10('_dataset', True, train_transform, download=True)
test_dataset = CIFAR10('_dataset', False, test_transform, download=False)
train_loader = DataLoader(
train_dataset,
train_batch_size,
shuffle=True,
num_workers=2,
pin_memory=True)
test_loader = DataLoader(
test_dataset,
test_batch_size,
shuffle=False,
num_workers=2,
pin_memory=True)
return train_loader, test_loader
def mnist_experiment():
BATCH_SIZE = 100
LR_DECAY_INTERVAL = 25000
# net = LeNet_300_100()
# net = LeNet_5()
net = LeNet_5_Caffe().to(device)
optimiser = optim.SGD(
net.parameters(),
lr=INIT_LR,
momentum=0.9,
weight_decay=WEIGHT_DECAY_RATE)
lr_scheduler = optim.lr_scheduler.StepLR(optimiser, 30000, gamma=0.1)
train_loader, val_loader = get_mnist_dataloaders(BATCH_SIZE, BATCH_SIZE)
return net, optimiser, lr_scheduler, train_loader, val_loader
def cifar10_experiment():
BATCH_SIZE = 128
LR_DECAY_INTERVAL = 30000
net = VGG_SNIP('D').to(device)
optimiser = optim.SGD(
net.parameters(),
lr=INIT_LR,
momentum=0.9,
weight_decay=WEIGHT_DECAY_RATE)
lr_scheduler = optim.lr_scheduler.StepLR(
optimiser, LR_DECAY_INTERVAL, gamma=0.1)
train_loader, val_loader = get_cifar10_dataloaders(BATCH_SIZE,
BATCH_SIZE) # TODO
return net, optimiser, lr_scheduler, train_loader, val_loader
def train():
writer = SummaryWriter()
net, optimiser, lr_scheduler, train_loader, val_loader = cifar10_experiment()
# Pre-training pruning using SKIP
keep_masks = SNIP(net, 0.05, train_loader, device) # TODO: shuffle?
apply_prune_mask(net, keep_masks)
trainer = create_supervised_trainer(net, optimiser, F.nll_loss, device)
evaluator = create_supervised_evaluator(net, {
'accuracy': Accuracy(),
'nll': Loss(F.nll_loss)
}, device)
pbar = ProgressBar()
pbar.attach(trainer)
@trainer.on(Events.ITERATION_COMPLETED)
def log_training_loss(engine):
lr_scheduler.step()
iter_in_epoch = (engine.state.iteration - 1) % len(train_loader) + 1
if engine.state.iteration % LOG_INTERVAL == 0:
# pbar.log_message("Epoch[{}] Iteration[{}/{}] Loss: {:.2f}"
# "".format(engine.state.epoch, iter_in_epoch, len(train_loader), engine.state.output))
writer.add_scalar("training/loss", engine.state.output,
engine.state.iteration)
@trainer.on(Events.EPOCH_COMPLETED)
def log_epoch(engine):
evaluator.run(val_loader)
metrics = evaluator.state.metrics
avg_accuracy = metrics['accuracy']
avg_nll = metrics['nll']
# pbar.log_message("Validation Results - Epoch: {} Avg accuracy: {:.2f} Avg loss: {:.2f}"
# .format(engine.state.epoch, avg_accuracy, avg_nll))
writer.add_scalar("validation/loss", avg_nll, engine.state.iteration)
writer.add_scalar("validation/accuracy", avg_accuracy,
engine.state.iteration)
trainer.run(train_loader, EPOCHS)
# Let's look at the final weights
# for name, param in net.named_parameters():
# if name.endswith('weight'):
# writer.add_histogram(name, param)
writer.close()
if __name__ == '__main__':
for _ in range(REPEAT_WITH_DIFFERENT_SEED):
train()