-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab_Presentation1.py
271 lines (214 loc) · 8.12 KB
/
Lab_Presentation1.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
import numpy as np
import torchvision.transforms as transforms
import torch
import torchvision
import torch.optim as optim
import os
import argparse
import time
from torch.utils.data.dataloader import DataLoader
from torchvision.datasets import CIFAR10
from torch.autograd import Variable
## Argument parser
parser = argparse.ArgumentParser(description='Lab EFFDL')
parser.add_argument('--model', default='PreActResNet18', type=str, help='Options: ResNet18, PreActResNet18, DenseNet121, VGG19')
parser.add_argument('--lr', default=0.05, type=float, help='Learning rate')
parser.add_argument('--epochs', default=5, type=int, help='Number of epochs')
parser.add_argument('--alpha', default=1.0, type=float, help='Alpha value for Mixup')
parser.add_argument('--dataaug', action='store_true', help='Use data augmentation or not')
parser.add_argument('--mixup', action='store_true', help='Use Mixup or not')
args = parser.parse_args()
## Test if there is GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
## Data loading
## Normalization adapted for CIFAR10
normalize_scratch = transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
# Transforms is a list of transformations applied on the 'raw' dataset before the data is fed to the network.
# Here, Data augmentation (RandomCrop and Horizontal Flip) are applied to each batch, differently at each epoch, on the training set data only
if args.dataaug:
transform_train = transforms.Compose([
transforms.Pad(4, padding_mode='reflect'),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ToTensor(),
normalize_scratch,
])
else:
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize_scratch,
])
transform_test = transforms.Compose([
transforms.ToTensor(),
normalize_scratch,
])
### The data from CIFAR10 will be downloaded in the following folder
rootdir = './data/cifar10'
c10train = CIFAR10(rootdir,train=True,download=True,transform=transform_train)
c10test = CIFAR10(rootdir,train=False,download=True,transform=transform_test)
trainloader = DataLoader(c10train,batch_size=32,shuffle=True)
testloader = DataLoader(c10test,batch_size=32)
## number of target samples for the final dataset
num_train_examples = len(c10train)
num_samples_subset = 15000
## We set a seed manually so as to reproduce the results easily
seed = 2147483647
## Generate a list of shuffled indices ; with the fixed seed, the permutation will always be the same, for reproducibility
indices = list(range(num_train_examples))
np.random.RandomState(seed=seed).shuffle(indices)## modifies the list in place
## We define the Subset using the generated indices
c10train_subset = torch.utils.data.Subset(c10train,indices[:num_samples_subset])
print(f"Initial CIFAR10 dataset has {len(c10train)} samples")
print(f"Subset of CIFAR10 dataset has {len(c10train_subset)} samples")
# Finally we can define anoter dataloader for the training data
trainloader_subset = DataLoader(c10train_subset,batch_size=32,shuffle=True)
## Model definition
## Choosing the model
## - ResNet
## - PreActResNet
## - DenseNet
## - VGG
## Example of usage of the models
from model_cifar100 import *
print("== Builind the model ==")
models = {'ResNet18': ResNet18(), 'PreActResNet18': PreActResNet18(), 'DenseNet121': DenseNet121(), 'VGG19': VGG('VGG19')}
if args.model in models:
print(f"\nModel found in the list, going to train :)")
print(f"Training {args.model} model")
model = models.get(args.model)
else:
model = models.get('PreActResNet18')
net = model
net = net.to(device)
## Hyperparameters to set
lr = args.lr
criterion = nn.CrossEntropyLoss()
max_epochs = args.epochs
best_acc = 0
optimizer = optim.SGD(net.parameters(), lr=lr)
alpha = args.alpha
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=200)
## Save results
results = []
params = []
train_losses = []
test_losses = []
## MixUp
def mixup_data(x, y, alpha=1.0, use_cuda=True):
'''Returns mixed inputs, pairs of targets, and lambda'''
if alpha > 0:
lam = np.random.beta(alpha, alpha)
else:
lam = 1
batch_size = x.size()[0]
if use_cuda:
index = torch.randperm(batch_size).cuda()
else:
index = torch.randperm(batch_size)
mixed_x = lam * x + (1 - lam) * x[index, :]
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lam
def mixup_criterion(criterion, pred, y_a, y_b, lam):
return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)
## Training the model
def trainMX(epoch):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
inputs, targets = inputs.to(device), targets.to(device)
inputs, targets_a, targets_b, lam = mixup_data(inputs, targets, alpha, True)
inputs, targets_a, targets_b = map(Variable, (inputs,targets_a, targets_b))
optimizer.zero_grad()
outputs = net(inputs)
loss = mixup_criterion(criterion, outputs, targets_a, targets_b, lam)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
train_losses.append(train_loss / len(trainloader))
print(f"Train Loss: {train_loss/(batch_idx+1)}")
def train(epoch):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
train_losses.append(train_loss / len(trainloader))
print(f"Train Loss: {train_loss/(batch_idx+1)}")
# Testing the model
def test(epoch):
global best_acc
net.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
test_losses.append(test_loss / len(testloader))
print(f"Test Loss: {test_loss/(batch_idx+1)}")
# Save checkpoint.
acc = 100.*correct/total
if acc > best_acc:
state = {
'net': net.state_dict(),
'acc': acc,
'epoch': epoch,
}
pathckpt = f'./ckpts/ckpt_{args.model}.pth'
torch.save(state, pathckpt)
best_acc = acc
# Function to count the parameters in the model
def contar_parametros(modelo):
return sum(p.numel() for p in modelo.parameters() if p.requires_grad)
start = time.time()
for i in range(max_epochs):
if args.mixup:
trainMX(i)
else:
train(i)
test(i)
scheduler.step()
print(f"Best accuracy: {best_acc}")
print(f"Number of parameters in the model: {contar_parametros(net)}")
end = time.time()
print(f"\nTime to train the model: {(end-start)/60} minutes")
with open('results.txt', 'a') as f:
f.write(f'\n{args.model}, {max_epochs}, {lr}, {best_acc}, {contar_parametros(net)}, {(end-start)/60}, {args.dataaug}, {args.mixup}' )
## Plotting the results
import matplotlib.pyplot as plt
epochs = range(1, max_epochs+1)
plt.plot(epochs, train_losses, label='Training Loss')
plt.plot(epochs, test_losses, label='Test Loss')
plt.title('Loss Plotting')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
save_path = f'images/loss_plot_{max_epochs}_{lr}_{args.mixup}_{args.dataaug}.png'
plt.savefig(save_path)