/
cnn.py
595 lines (420 loc) 路 17.3 KB
/
cnn.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import os
import time
from typing import Iterable
from dataclasses import dataclass
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from torch.optim import lr_scheduler
from torch.optim.lr_scheduler import StepLR
training_data_path = "./data/training"
validation_data_path = "./data/validation"
classifier_name = "torsk_sei_classifier.pt"
def image_preprocess_transforms():
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor()
])
return preprocess
def image_common_transforms(mean=(0.4611, 0.4359, 0.3905), std=(0.2193, 0.2150, 0.2109)):
preprocess = image_preprocess_transforms()
common_transforms = transforms.Compose([
preprocess,
transforms.Normalize(mean, std)
])
return common_transforms
def get_mean_std(data_root, num_workers=4):
transform = image_preprocess_transforms()
loader = data_loader(data_root, transform)
mean = 0.
std = 0.
for images, _ in loader:
batch_samples = images.size(0) # batch size (the last batch can have smaller size!)
images = images.view(batch_samples, images.size(1), -1)
mean += images.mean(2).sum(0)
std += images.std(2).sum(0)
mean /= len(loader.dataset)
std /= len(loader.dataset)
print('mean: {}, std: {}'.format(mean, std))
return mean, std
def data_loader(data_root, transform, batch_size=16, shuffle=False, num_workers=2):
dataset = datasets.ImageFolder(root=data_root, transform=transform)
loader = torch.utils.data.DataLoader(dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=shuffle)
return loader
def get_data(batch_size, data_root, num_workers=4, data_augmentation=False):
train_data_path = os.path.join(data_root, 'training')
mean, std = get_mean_std(data_root=train_data_path, num_workers=num_workers)
common_transforms = image_common_transforms(mean, std)
# if data_augmentation is true
# data augmentation implementation
if data_augmentation:
train_transforms = data_augmentation_preprocess(mean, std)
# else do common transforms
else:
train_transforms = common_transforms
# train dataloader
train_loader = data_loader(train_data_path,
train_transforms,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers)
# test dataloader
test_data_path = os.path.join(data_root, 'validation')
test_loader = data_loader(test_data_path,
train_transforms,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers)
return train_loader, test_loader
@dataclass
class SystemConfiguration:
'''
Describes the common system setting needed for reproducible training
'''
seed: int = 21 # seed number to set the state of all random number generators
cudnn_benchmark_enabled: bool = True # enable CuDNN benchmark for the sake of performance
cudnn_deterministic: bool = True # make cudnn deterministic (reproducible training)
@dataclass
class TrainingConfiguration:
'''
Describes configuration of the training process
'''
batch_size: int = 10
epochs_count: int = 100
init_learning_rate: float = 0.1
log_interval: int = 5
test_interval: int = 1
data_root: str = "./data"
num_workers: int = 2
device: str = 'cuda'
def setup_system(system_config: SystemConfiguration) -> None:
torch.manual_seed(system_config.seed)
if torch.cuda.is_available():
torch.backends.cudnn_benchmark_enabled = system_config.cudnn_benchmark_enabled
torch.backends.cudnn.deterministic = system_config.cudnn_deterministic
def train(
train_config: TrainingConfiguration, model: nn.Module, optimizer: torch.optim.Optimizer,
train_loader: torch.utils.data.DataLoader, epoch_idx: int
) -> None:
# change model in training mood
model.train()
# to get batch loss
batch_loss = np.array([])
# to get batch accuracy
batch_acc = np.array([])
for batch_idx, (data, target) in enumerate(train_loader):
# clone target
indx_target = target.clone()
# send data to device (its is medatory if GPU has to be used)
data = data.to(train_config.device)
# send target to device
target = target.to(train_config.device)
# reset parameters gradient to zero
optimizer.zero_grad()
# forward pass to the model
output = model(data)
# cross entropy loss
loss = F.cross_entropy(output, target)
# find gradients w.r.t training parameters
loss.backward()
# Update parameters using gardients
optimizer.step()
batch_loss = np.append(batch_loss, [loss.item()])
# Score to probability using softmax
prob = F.softmax(output, dim=1)
# get the index of the max probability
pred = prob.data.max(dim=1)[1]
# correct prediction
correct = pred.cpu().eq(indx_target).sum()
# accuracy
acc = float(correct) / float(len(data))
batch_acc = np.append(batch_acc, [acc])
# Decay learning rate
scheduler.step()
print('Stepping scheduler this epoch. ', 'LR:', scheduler.get_lr())
epoch_loss = batch_loss.mean()
epoch_acc = batch_acc.mean()
print('Epoch: {} \nTrain Loss: {:.6f} Acc: {:.4f}'.format(epoch_idx, epoch_loss, epoch_acc))
return epoch_loss, epoch_acc
def validate(
train_config: TrainingConfiguration,
model: nn.Module,
test_loader: torch.utils.data.DataLoader,
) -> float:
#
model.eval()
test_loss = 0
count_corect_predictions = 0
for data, target in test_loader:
indx_target = target.clone()
data = data.to(train_config.device)
target = target.to(train_config.device)
output = model(data)
# add loss for each mini batch
test_loss += F.cross_entropy(output, target).item()
# Score to probability using softmax
prob = F.softmax(output, dim=1)
# get the index of the max probability
pred = prob.data.max(dim=1)[1]
# add correct prediction count
count_corect_predictions += pred.cpu().eq(indx_target).sum()
# average over number of mini-batches
test_loss = test_loss / len(test_loader)
# average over number of dataset
accuracy = 100. * count_corect_predictions / len(test_loader.dataset)
print(
'\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, count_corect_predictions, len(test_loader.dataset), accuracy
)
)
return test_loss, accuracy/100.0
def save_model(model, device, model_dir='models', model_file_name=classifier_name):
if not os.path.exists(model_dir):
os.makedirs(model_dir)
model_path = os.path.join(model_dir, model_file_name)
# make sure you transfer the model to cpu.
if device == 'cuda':
model.to('cpu')
# save the state_dict
torch.save(model.state_dict(), model_path)
if device == 'cuda':
model.to('cuda')
return
def load_model(model, model_dir='models', model_file_name=classifier_name):
model_path = os.path.join(model_dir, model_file_name)
# loading the model and getting model parameters by using load_state_dict
model.load_state_dict(torch.load(model_path))
return model
def main(model, optimizer, scheduler=None, system_configuration=SystemConfiguration(),
training_configuration=TrainingConfiguration(), data_augmentation=True):
# system configuration
setup_system(system_configuration)
# batch size
batch_size_to_set = training_configuration.batch_size
# num_workers
num_workers_to_set = training_configuration.num_workers
# epochs
epoch_num_to_set = training_configuration.epochs_count
# if GPU is available use training config,
# else lowers batch_size, num_workers and epochs count
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
batch_size_to_set = 16
num_workers_to_set = 4
# data loader
train_loader, test_loader = get_data(
batch_size=batch_size_to_set,
data_root=training_configuration.data_root,
num_workers=num_workers_to_set,
data_augmentation=data_augmentation
)
# Update training configuration
training_configuration = TrainingConfiguration(
device=device,
batch_size=batch_size_to_set,
num_workers=num_workers_to_set
)
# send model to device (GPU/CPU)
model.to(training_configuration.device)
best_loss = torch.tensor(np.inf)
# epoch train/test loss
epoch_train_loss = np.array([])
epoch_test_loss = np.array([])
# epch train/test accuracy
epoch_train_acc = np.array([])
epoch_test_acc = np.array([])
# trainig time measurement
t_begin = time.time()
for epoch in range(training_configuration.epochs_count):
# Calculate Initial Test Loss
init_val_loss, init_val_accuracy = validate(training_configuration, model, test_loader)
print("Initial Test Loss : {:.6f}, \nInitial Test Accuracy : {:.3f}%\n".format(init_val_loss, init_val_accuracy*100))
# Train
train_loss, train_acc = train(training_configuration, model, optimizer, train_loader, epoch)
epoch_train_loss = np.append(epoch_train_loss, [train_loss])
epoch_train_acc = np.append(epoch_train_acc, [train_acc])
elapsed_time = time.time() - t_begin
speed_epoch = elapsed_time / (epoch + 1)
speed_batch = speed_epoch / len(train_loader)
eta = speed_epoch * training_configuration.epochs_count - elapsed_time
print(
"Elapsed {:.2f}s, {:.2f} s/epoch, {:.2f} s/batch, ets {:.2f}s".format(
elapsed_time, speed_epoch, speed_batch, eta
)
)
# Validate
if epoch % training_configuration.test_interval == 0:
current_loss, current_accuracy = validate(training_configuration, model, test_loader)
epoch_test_loss = np.append(epoch_test_loss, [current_loss])
epoch_test_acc = np.append(epoch_test_acc, [current_accuracy])
if current_loss < best_loss:
best_loss = current_loss
print('Model Improved. Saving the Model...\n')
save_model(model, device=training_configuration.device)
print("Total time: {:.2f}, Best Loss: {:.3f}".format(time.time() - t_begin, best_loss))
return model, epoch_train_loss, epoch_train_acc, epoch_test_loss, epoch_test_acc
def plot_loss_accuracy(train_loss, val_loss, train_acc, val_acc, colors,
loss_legend_loc='upper center', acc_legend_loc='upper left',
fig_size=(20, 10), sub_plot1=(1, 2, 1), sub_plot2=(1, 2, 2)):
plt.rcParams["figure.figsize"] = fig_size
fig = plt.figure()
plt.subplot(sub_plot1[0], sub_plot1[1], sub_plot1[2])
for i in range(len(train_loss)):
x_train = range(len(train_loss[i]))
x_val = range(len(val_loss[i]))
min_train_loss = train_loss[i].min()
min_val_loss = val_loss[i].min()
plt.plot(x_train, train_loss[i], linestyle='-', color='tab:{}'.format(colors[i]),
label="TRAIN LOSS ({0:.4})".format(min_train_loss))
plt.plot(x_val, val_loss[i], linestyle='--' , color='tab:{}'.format(colors[i]),
label="VALID LOSS ({0:.4})".format(min_val_loss))
plt.xlabel('epoch no.')
plt.ylabel('loss')
plt.legend(loc=loss_legend_loc)
plt.title('Training and Validation Loss')
plt.subplot(sub_plot2[0], sub_plot2[1], sub_plot2[2])
for i in range(len(train_acc)):
x_train = range(len(train_acc[i]))
x_val = range(len(val_acc[i]))
max_train_acc = train_acc[i].max()
max_val_acc = val_acc[i].max()
plt.plot(x_train, train_acc[i], linestyle='-', color='tab:{}'.format(colors[i]),
label="TRAIN ACC ({0:.4})".format(max_train_acc))
plt.plot(x_val, val_acc[i], linestyle='--' , color='tab:{}'.format(colors[i]),
label="VALID ACC ({0:.4})".format(max_val_acc))
plt.xlabel('epoch no.')
plt.ylabel('accuracy')
plt.legend(loc=acc_legend_loc)
plt.title('Training and Validation Accuracy')
fig.savefig('sample_loss_acc_plot.png')
plt.show()
return
classes = 3
nodes = 128
class MyModel(nn.Module):
def __init__(self):
super().__init__()
# convolution layers
self._body = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=64, out_channels=nodes, kernel_size=5),
nn.BatchNorm2d(nodes),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2)
)
# Fully connected layers
self._head = nn.Sequential(
nn.Linear(in_features=nodes*52*52, out_features=1024),
nn.ReLU(inplace=True),
nn.Linear(in_features=1024, out_features=classes)
)
def forward(self, x):
self.drop_out = nn.Dropout()
# apply feature extractor
x = self._body(x)
# flatten the output of conv layers
# dimension should be batch_size * number_of weight_in_last conv_layer
x = x.view(x.size()[0], -1)
# apply classification head
x = self._head(x)
return x
def prediction(model, device, batch_input):
# send model to cpu/cuda according to your system configuration
model.to(device)
# it is important to do model.eval() before prediction
model.eval()
data = batch_input.to(device)
output = model(data)
# Score to probability using softmax
prob = F.softmax(output, dim=1)
# get the max probability
pred_prob = prob.data.max(dim=1)[0]
# get the index of the max probability
pred_index = prob.data.max(dim=1)[1]
return pred_index.cpu().numpy(), pred_prob.cpu().numpy()
def get_sample_prediction(model, data_root, mean, std):
batch_size = 15
if torch.cuda.is_available():
device = "cuda"
num_workers = 8
else:
device = "cpu"
num_workers = 2
# transformed data
test_dataset_trans = datasets.ImageFolder(root=data_root, transform=image_common_transforms(mean, std))
# original image dataset
test_dataset = datasets.ImageFolder(root=data_root, transform=image_preprocess_transforms())
data_len = test_dataset.__len__()
interval = int(data_len/batch_size)
imgs = []
inputs = []
targets = []
for i in range(batch_size):
index = i * interval
trans_input, target = test_dataset_trans.__getitem__(index)
img, _ = test_dataset.__getitem__(index)
imgs.append(img)
inputs.append(trans_input)
targets.append(target)
inputs = torch.stack(inputs)
cls, prob = prediction(model, device, batch_input=inputs)
plt.style.use('default')
plt.rcParams["figure.figsize"] = (15, 9)
fig = plt.figure()
for i, target in enumerate(targets):
plt.subplot(3, 5, i+1)
img = transforms.functional.to_pil_image(imgs[i])
plt.imshow(img)
plt.gca().set_title('P:{0}({1:.2}), T:{2}'.format(test_dataset.classes[cls[i]],
prob[i],
test_dataset.classes[targets[i]]))
fig.savefig('sample_prediction.png')
plt.show()
return
if __name__ == '__main__':
model = MyModel()
#model = torchvision.models.resnet50()
print(model)
# get optimizer
train_config = TrainingConfiguration()
optimizer = torch.optim.Adam(params=model.parameters(), lr=train_config.init_learning_rate, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)
# optimizer
#optimizer = optim.Adam(
# model.parameters(),
# lr = train_config.init_learning_rate
#)
#scheduler = StepLR(optimizer, step_size=1, gamma=0.1)
decayRate = 0.96
scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=decayRate)
#optimizer = optim.SGD(
# model.parameters(),
# lr=train_config.init_learning_rate
#)
plot_loss_accuracy(train_loss=[train_loss],
val_loss=[val_loss],
train_acc=[train_acc],
val_acc=[val_acc],
colors=['blue'],
loss_legend_loc='upper center',
acc_legend_loc='upper left')
m = MyModel()
m = load_model(m)
train_config = TrainingConfiguration()
test_data_path = os.path.join(train_config.data_root, 'validation')
train_data_path = os.path.join(train_config.data_root, 'training')
mean, std = get_mean_std(train_data_path)
get_sample_prediction(m, test_data_path, mean, std)