-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
433 lines (360 loc) · 14.8 KB
/
helpers.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import json
import os
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.tensorboard.writer import SummaryWriter
import torchvision
import generation
import tempfile
import sys
import importlib
writer = SummaryWriter()
def getDevice():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.set_default_device(device)
return device
def Cifar10Splits(batchSize=64):
train_transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(10),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.4914, 0.4822, 0.4465],
std=[0.2470, 0.2435, 0.2616],
),
]
)
test_transform = transforms.Compose(
[
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.49139968, 0.48215827, 0.44653124],
std=[0.24703233, 0.24348505, 0.26158768],
),
]
)
trainDataset = datasets.CIFAR10(
train=True, root="./data", transform=train_transform, download=True
)
testDataset = datasets.CIFAR10(
train=False, root="./data", transform=test_transform, download=True
)
# Split train dataset to have 10% validation dataset
valid_size = 0.10
num_train = len(trainDataset)
indices = list(range(num_train))
np.random.shuffle(indices)
split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
trainSampler = SubsetRandomSampler(train_idx)
validSampler = SubsetRandomSampler(valid_idx)
trainLoader = DataLoader(trainDataset, batch_size=batchSize, sampler=trainSampler)
validLoader = DataLoader(trainDataset, batch_size=batchSize, sampler=validSampler)
testLoader = DataLoader(testDataset, batch_size=batchSize)
return trainLoader, validLoader, testLoader
def get_custom_dataloaders(dataset_name, batch_size=64, validation_split=0.1):
if dataset_name == "cifar10":
transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(10),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.4914, 0.4822, 0.4465],
std=[0.2470, 0.2435, 0.2616],
),
]
)
train_dataset = torchvision.datasets.CIFAR10(
root="./data", train=True, download=True, transform=transform
)
test_dataset = torchvision.datasets.CIFAR10(
root="./data", train=False, download=True, transform=transform
)
elif dataset_name == "cifar100":
transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(10),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.5071, 0.4866, 0.4409],
std=[0.2673, 0.2564, 0.2761],
),
]
)
train_dataset = torchvision.datasets.CIFAR100(
root="./data", train=True, download=True, transform=transform
)
test_dataset = torchvision.datasets.CIFAR100(
root="./data", train=False, download=True, transform=transform
)
elif dataset_name == "qmnist":
transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(10),
transforms.Resize((224, 224)),
transforms.ToTensor(),
]
)
train_dataset = torchvision.datasets.QMNIST(
root="./data", train=True, download=True, transform=transform
)
test_dataset = torchvision.datasets.QMNIST(
root="./data", train=False, download=True, transform=transform
)
elif dataset_name == "fashion-mnist":
transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(10),
transforms.Resize((224, 224)),
transforms.ToTensor(),
]
)
train_dataset = torchvision.datasets.FashionMNIST(
root="./data", train=True, download=True, transform=transform
)
test_dataset = torchvision.datasets.FashionMNIST(
root="./data", train=False, download=True, transform=transform
)
else:
raise ValueError(
"Dataset not supported. Please choose 'cifar10', 'cifar100', 'qmnist' or 'fashion-mnist'."
)
# Split the training dataset into training and validation sets
num_train = len(train_dataset)
indices = list(range(num_train))
split = int(validation_split * num_train)
train_indices, val_indices = indices[split:], indices[:split]
train_sampler = torch.utils.data.sampler.SubsetRandomSampler(train_indices)
val_sampler = torch.utils.data.sampler.SubsetRandomSampler(val_indices)
# Create data loaders
train_loader = DataLoader(
train_dataset, batch_size=batch_size, sampler=train_sampler
)
val_loader = DataLoader(train_dataset, batch_size=batch_size, sampler=val_sampler)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
return train_loader, val_loader, test_loader
# JSON Files should be a dictionary, with the index being the current inference number, first = 0, second = 1 etc.
# Each entry will have 3 values, the entropy, the accuracy (correct or not correct), and the time taken, exactly in that order
def graphFromJson(filePath):
with open(filePath, "r") as file:
results = json.load(file)
_, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(16, 24))
accuratePredictions = [
value["entropy"] for value in results if value["correct"] is True
]
totalPredictions = [value["entropy"] for value in results]
accurateCount, _ = np.histogram(accuratePredictions, bins=200)
totalCount, totalBins = np.histogram(totalPredictions, bins=200)
cumulativePercent = 100 * np.cumsum(accurateCount) / np.cumsum(totalCount)
ax1.plot(totalBins[:-1], cumulativePercent)
ax1.set_title("Accuracy vs Entropy")
ax1.set_ylabel("Cumulative Correct (%)")
ax2.plot(totalBins[:-1], 100 * np.cumsum(totalCount) / np.sum(totalCount))
ax2.set_title("Dataset Progress vs Entropy")
ax2.set_ylabel("Progress (%)")
plt.xlabel("Entropy")
plt.show()
def trainModel(model, trainLoader, validLoader, testLoader):
model.train()
device = getDevice()
model.to(device)
epoch = 20
learning_rate = 0.01
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(
model.parameters(), lr=learning_rate, weight_decay=0.001, momentum=0.9
)
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, 0.99)
for e in range(epoch):
start = time.time()
model.train()
running_loss = 0.0
for i, (images, labels) in enumerate(trainLoader):
# Move tensors to the configured device
images = images.to(device)
labels = labels.to(device)
exitNumber, outputs = model(images)
loss = criterion(outputs, labels)
writer.add_scalar("loss/batch_loss", loss.item(), e * len(trainLoader) + i)
running_loss += loss.item()
# Backward and optimize
optimizer.zero_grad()
try:
loss.backward()
except RuntimeError:
print("loss.backward failed")
print(f"outputs are\n{outputs}")
print(f"labels are\n{labels}")
print()
optimizer.step()
print(f"Epoch {e}: Inference {i} used exit {exitNumber}")
train_loss = running_loss / len(trainLoader)
print(f"Epoch {e} took {time.time() - start}, Loss: {train_loss}")
# Validation
model.eval()
with torch.no_grad():
correct = 0
total = 0
val_loss = 0.0
for images, labels in validLoader:
images = images.to(device)
labels = labels.to(device)
exitNumber, outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
val_loss += criterion(outputs, labels).item()
val_accuracy = 100 * correct / total
val_loss /= len(validLoader)
writer.add_scalar("accuracy/val", val_accuracy, e)
print(
f"Epoch {e}, Validation Loss: {val_loss:.4f}, Accuracy: {val_accuracy:.2f}%"
)
writer.add_scalars("loss", {"train": train_loss, "val": val_loss}, e)
# Update learning rate
lr_scheduler.step()
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in testLoader:
images = images.to(device)
labels = labels.to(device)
exitNumber, outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
test_accuracy = 100 * correct / total
writer.add_scalar("accuracy/test", test_accuracy)
print(
f"Accuracy of the network on the {len(testLoader)} test images: {test_accuracy:.2f}%"
)
# 1. Generate temporary results json
# 2. Read in the data from the results json
# 3. Use the same bin technique from the graph section
# 4. Find the bin for the target accuracy (unsure whether this should be the bin before, after or containing the accuracy)
def getAccDataset(model, testLoader, fileName):
fileFileName = generateJsonResults(model, fileName, testLoader)
with open(fileFileName, "r") as file:
results = json.load(file)
accuratePredictions = [
value["entropy"] for value in results if value["correct"] is True
]
totalPredictions = [value["entropy"] for value in results]
accurateCount, _ = np.histogram(accuratePredictions, bins=200)
totalCount, totalBins = np.histogram(totalPredictions, bins=200)
cumulativeAccuracy = 100 * np.cumsum(accurateCount) / np.cumsum(totalCount)
cumulativeDataset = 100 * np.cumsum(accurateCount) / np.sum(totalCount)
return cumulativeAccuracy, cumulativeDataset, totalBins
def getEntropyForAccuracy(model, testLoader, target):
acc, _, bins = getAccDataset(model, testLoader, "temp-results")
a = np.asarray(acc < target)
a = np.asarray(a[:-1] != a[1:])
indices = np.nonzero(a[:-1] != a[1:])
if len(indices[0]) == 0:
return 0.0
return bins[indices[0]][0]
def getAccuracy(model, testLoader):
return getAccDataset(model, testLoader, "temp-results")[0][-1]
def trainModelWithBranch(model, trainLoader, validLoader, testLoader, test):
exitTracker = generation.ExitTracker(model, 100)
exitTracker.saveAst()
exitTracker.reloadable_model.reload(False)
trainModel(
exitTracker.reloadable_model.getModel(), trainLoader, validLoader, testLoader
)
# Model now only contains full branch, get total accuracy
accuracy = getAccuracy(model.getModel(), test)
exitTracker.targetAccuracy = accuracy
exitTracker.useNextExit()
while not exitTracker.lastExitTrained():
exitTracker.reloadable_model.reload()
trainModel(
exitTracker.reloadable_model.getModel(),
trainLoader,
validLoader,
testLoader,
)
exitTracker.setCurrentExitCorrectly(test)
exitTracker.reloadable_model.reload()
exitTracker.useNextExit()
exitTracker.removeUnneededExits()
exitTracker.reloadable_model.getModel().eval()
def generateJsonResults(model, modelName, testLoader):
device = getDevice()
results = []
with torch.no_grad():
for _, (images, labels) in enumerate(testLoader):
images = images.to(device)
labels = labels.to(device)
start = time.time()
exitNumber, outputs = model(images)
end = time.time()
pk = F.softmax(outputs.data, dim=1)
entropy = -torch.sum(pk * torch.log(pk + 1e-20))
_, predicted = torch.max(outputs.data, 1)
correct = predicted == labels
result = {
"entropy": entropy.item(),
"correct": correct.item(),
"time_taken": end - start,
"exit_number": exitNumber,
}
results.append(result)
fileName = f"{modelName}-{int(time.time())}.json"
with open(fileName, "a") as file:
json.dump(results, file)
return fileName
def createModelsFolder(name):
script_directory = os.path.dirname(os.path.realpath(__file__))
folder_name = name
folder_path = os.path.join(script_directory, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
class ReloadableModel:
def __init__(self, dataset_channels, model_class, *args):
self.model = model_class(*args)
self.model.to(getDevice())
self.model_class = model_class
self.model_args = args
self.dc = dataset_channels
_ = self.model(torch.randn(1, self.dc, 224, 224))
def reload(self, grad=True):
with tempfile.TemporaryFile() as file:
torch.save(self.model.state_dict(), file)
file.seek(0)
module_name = self.model_class.__module__
importlib.reload(sys.modules[module_name])
reloaded_module = sys.modules[module_name]
self.model_class = getattr(reloaded_module, self.model_class.__name__)
self.model = self.model_class(*(self.model_args))
_ = self.model(torch.randn(1, self.dc, 224, 224))
saved_state_dict = torch.load(file)
self.model.load_state_dict(saved_state_dict, strict=False)
if grad:
for name, param in self.model.named_parameters():
if "exit" not in name:
param.requires_grad = False
def getModel(self):
return self.model