-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_experiment.py
350 lines (283 loc) · 11.4 KB
/
main_experiment.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
from types import FunctionType
from fastai.vision import create_cnn, ImageDataBunch
from fastai.train import AdamW
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import time as t
from delve import CheckLayerSat
from models import *
from csv_logger import record_metrics, log_to_csv
from torchvision.models import vgg16 as vgg16real
from fastai.vision import DataBunch, Learner, create_cnn
from fastai import *
from fastai.vision import *
from alternative_loaders import get_n_fold_datasets_test, get_n_fold_datasets_train
from torch.utils.data.sampler import WeightedRandomSampler
from sklearn.metrics import accuracy_score
from tqdm import tqdm, trange
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
global SAMPLER
global IMBALANCE
SAMPLER = None
def _shuffle_classes(class_list):
c_samples = np.random.choice(class_list, len(class_list), replace=False)
class_list = np.asarray(c_samples)[class_list]
return class_list
def get_class_probas(class_list, skew_val=1.0):
class_list = _shuffle_classes(class_list)
probas = np.linspace(-len(class_list)//2, len(class_list)//2, len(class_list)) * 0.01 * skew_val
probas += 0.1
print(probas)
return probas
def get_sampler_with_random_imbalance(skew_val, num_samples, n_classes, labels):
classes = list(range(n_classes))
class_probas = get_class_probas(classes, skew_val)
weights = np.zeros(num_samples)
for cls in classes:
prob = class_probas[cls]
w = weights[np.asarray(labels) == cls]
weights[np.asarray(labels) == cls] = class_probas[cls]
weights = weights / np.linalg.norm(weights)
global IMBALANCE
print(class_probas)
return WeightedRandomSampler(weights, num_samples, replacement=True)
def train_set_cifar(transform, batch_size):
train_set = torchvision.datasets.CIFAR10(
root='./data', train=True, download=True, transform=transform
)
train_loader = torch.utils.data.DataLoader(
train_set, batch_size=batch_size, shuffle=True, num_workers=2
)
return train_loader
def test_set_cifar(transform, batch_size):
test_set = torchvision.datasets.CIFAR10(
root='./data', train=False, download=True, transform=transform
)
test_loader = torch.utils.data.DataLoader(
test_set, batch_size=batch_size, shuffle=False, num_workers=2
)
return test_loader
def train_set_cifar100(transform, batch_size):
train_set = torchvision.datasets.CIFAR100(
root='./data', train=True, download=True, transform=transform
)
train_loader = torch.utils.data.DataLoader(
train_set, batch_size=batch_size, shuffle=True, num_workers=2
)
return train_loader
def test_set_cifar100(transform, batch_size):
test_set = torchvision.datasets.CIFAR100(
root='./data', train=False, download=True, transform=transform
)
test_loader = torch.utils.data.DataLoader(
test_set, batch_size=batch_size, shuffle=False, num_workers=2
)
return test_loader
def train_set_imbalanced_cifar(transformer, batch_size, skew_val):
train_set = torchvision.datasets.CIFAR10(
root='./data', train=True, download=True, transform=transformer
)
global SAMPLER
if SAMPLER is None:
SAMPLER = get_sampler_with_random_imbalance(skew_val, len(train_set.targets), n_classes=10, labels=train_set.targets)
train_loader = torch.utils.data.DataLoader(
train_set, batch_size=batch_size, sampler=SAMPLER
)
return train_loader
def train(network, dataset, test_set, logging_dir, batch_size):
network.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(network.parameters())
#stats = CheckLayerSat(logging_dir, network, log_interval=len(dataset)//batch_size)
stats = CheckLayerSat(logging_dir, network, log_interval=60, sat_method='cumvar99', conv_method='mean')
epoch_acc = 0
thresh = 0.95
epoch = 0
total = 0
correct = 0
value_dict = None
while epoch <= 20:
print('Start Training Epoch', epoch, '\n')
start = t.time()
epoch_acc = 0
train_loss = 0
total = 0
correct = 0
network.train()
for i, data in enumerate(dataset):
step = epoch*len(dataset) + i
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = network(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()
#if i % 2000 == 1999: # print every 2000 mini-batches
print(i,'of', len(dataset),'acc:', correct/total)
# display layer saturation levels
end = t.time()
stats.saturation()
test_loss, test_acc = test(network, test_set, criterion, stats, epoch)
epoch_acc = correct / total
print('Epoch', epoch, 'finished', 'Acc:', epoch_acc, 'Loss:', train_loss / total,'\n')
stats.add_scalar('train_loss', train_loss / total, epoch) # optional
stats.add_scalar('train_acc', epoch_acc, epoch) # optional
value_dict = record_metrics(value_dict, stats.logs, epoch_acc, train_loss/total, test_acc, test_loss, epoch, (end-start) / total)
log_to_csv(value_dict, logging_dir)
epoch += 1
stats.close()
# test_stats.close()
return criterion
def test(network, dataset, criterion, stats, epoch):
network.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(dataset):
inputs, targets = inputs.to(device), targets.to(device)
outputs = network(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
#if batch_idx % 200 == 199: # print every 200 mini-batches
print(batch_idx,'of', len(dataset),'acc:', correct/total)
stats.saturation()
print('Test finished', 'Acc:', correct / total, 'Loss:', test_loss/total,'\n')
stats.add_scalar('test_loss', test_loss/total, epoch) # optional
stats.add_scalar('test_acc', correct/total, epoch) # optional
return test_loss/total, correct/total
def execute_experiment(network: nn.Module, in_channels: int, n_classes: int, l1: int, l2: int , l3: int, train_set: FunctionType, test_set: FunctionType):
print('Experiment has started')
batch_size = 128
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
)
check = 25
i = 0
for l1_config in l1:
for l2_config in l2:
for l3_config in l3:
i += 1
if i <= check:
continue
print('Creating Network')
net = network(in_channels=in_channels,
l1=l1_config,
l2=l2_config,
l3=l3_config,
n_classes=n_classes)
print('Network created')
train_loader = train_set(transform, batch_size)
test_loader = test_set(transform, batch_size)
print('Datasets fetched')
train(net, train_loader, test_loader, '{}_{}_{}'.format(l1_config, l2_config, l3_config), batch_size)
del net
def execute_experiment_vgg(network: nn.Module, net_name: str, train_set: FunctionType, test_set: FunctionType, n_claases=2):
print('Experiment has started')
batch_size = 128
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
#transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
#transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
print('Creating Network')
train_loader = train_set(transform_train, batch_size)
test_loader = test_set(transform_test, batch_size)
net = network(num_classes=n_claases)#create_cnn(data, vgg16).model#network()
print(net)
print('Network created')
print('Datasets fetched')
train(net, train_loader, test_loader, net_name, batch_size)
return
if '__main__' == __name__:
executions = [vgg11_XXXS, vgg13_XXXS, vgg16_XXXS, vgg19_XXXS,
vgg11_XXS, vgg13_XXS, vgg16_XXS, vgg19_XXS,
vgg11_XS, vgg13_XS, vgg16_XS, vgg19_XS,
vgg11_S, vgg13_S, vgg16_S, vgg19_S,
vgg11, vgg13, vgg16, vgg19,
]
names = ['11_XXXS', '13_XXXS', '16_XXXS', '19_XXXS',
'11_XXS', '13_XXS', '16_XXS', '19_XXS',
'11_XS', '13_XS', '16_XS', '19_XS',
'11_S', '13_S', '16_S', '19_S',
'11', '13', '16', '19',
]
train_set = lambda transform_train, batch_size: get_n_fold_datasets_train(transform_train, batch_size, ['automobile', 'frog'])
test_set = lambda transform_test, batch_size: get_n_fold_datasets_test(transform_test, batch_size, ['automobile', 'frog'])
#executions.reverse()
#names.reverse()
train_set_imbalanced_cifar
counter = 0
for j in [0, 1]:
#sampler = WeightedRandomSampler()
for i in range(len(names)):
counter += 1
print('COUNTER:',counter)
print(names[i])
#configVGG_cifar = {
# 'network': executions[i],
# 'train_set': train_set,
# 'test_set': test_set,
# 'net_name': 'automobilefrog_VGG' + names[i] + '_A' + str(j),
# 'n_claases': 2
#}
# execute_experiment_vgg(**configVGG_cifar)
configVGG_cifar = {
'network': executions[i],
'train_set': train_set_cifar100,#lambda t, batch_size: train_set_imbalanced_cifar(t, batch_size=batch_size, skew_val=1.0),
'test_set': test_set_cifar100,
'net_name': '100_VGG' + names[i] + '_A' + str(j),
'n_claases': 100
}
execute_experiment_vgg(**configVGG_cifar)
configCNN_cifar = {
'network': SimpleCNN,
'in_channels': 3,
'n_classes': 10,
'l1' : [4, 16, 64],
'l2' : [8, 32, 128],
'l3' : [16, 64, 256],
'train_set': train_set_cifar,
'test_set': test_set_cifar
}
configCNNKernel_cifar = {
'network': SimpleCNNKernel,
'in_channels': 3,
'n_classes': 10,
'l1': [3, 5, 7],
'l2': [3, 5, 7],
'l3': [3, 5, 7],
'train_set': train_set_cifar,
'test_set': test_set_cifar
}
configFCN_cifar = {
'network': SimpleFCNet,
'in_channels': 32*32*3,
'n_classes': 10,
'l1' : [4*3*3, 16*3*3, 64*3*3],
'l2' : [8*3*3, 32*3*3, 128*3*3],
'l3' : [16*3*3, 64*3*3, 256*3*3],
'train_set': train_set_cifar,
'test_set': test_set_cifar
}
#execute_experiment(**configCNN_cifar)