-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
141 lines (106 loc) · 4.97 KB
/
main.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
import copy
import random
import numpy as np
import os
from tqdm import tqdm
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim
from torch.utils.data import DataLoader
import torchvision.models as models
from helper import TqdmUpTo, MetricMonitor, calculate_accuracy
from dataset.utils import get_train_test
from dataset.dataset import SmokeDataset
from visualize import display_image_grid, visualize_augmentations, plot_curves
from config import Config
from collections import defaultdict
import warnings
warnings.filterwarnings('ignore')
cudnn.benchmark = True
def train(train_loader, model, criterion, optimizer, epoch, cfg):
metric_monitor = MetricMonitor()
model.train()
stream = tqdm(train_loader, position=0, leave=True, colour='green')
for i, (images, target) in enumerate(stream, start=1):
images = images.to(cfg.device, non_blocking=True)
target = target.to(
cfg.device, non_blocking=True).float().view(-1, 1)
output = model(images)
loss = criterion(output, target)
accuracy = calculate_accuracy(output, target)
metric_monitor.update("Loss", loss.item())
metric_monitor.update("Accuracy", accuracy)
optimizer.zero_grad()
loss.backward()
optimizer.step()
stream.set_description(
"Epoch: {epoch}. Train. {metric_monitor}".format(
epoch=epoch, metric_monitor=metric_monitor)
)
return metric_monitor
def validate(val_loader, model, criterion, epoch, cfg):
metric_monitor = MetricMonitor()
model.eval()
stream = tqdm(val_loader, position=0, leave=True, colour='red')
with torch.no_grad():
for i, (images, target) in enumerate(stream, start=1):
images = images.to(cfg.device, non_blocking=True)
target = target.to(cfg.device, non_blocking=True).float().view(-1, 1)
output = model(images)
loss = criterion(output, target)
accuracy = calculate_accuracy(output, target)
metric_monitor.update("Loss", loss.item())
metric_monitor.update("Accuracy", accuracy)
stream.set_description(
"Epoch: {epoch}. Validation. {metric_monitor}".format(epoch=epoch, metric_monitor=metric_monitor)
)
return metric_monitor
def main():
cfg = Config()
history = defaultdict()
history['val_loss'], history['train_loss'] = list(), list()
history['val_acc'], history['train_acc'] = list(), list()
print('Creating training, validation set')
(train_images, train_labels), (val_images, val_labels), (test_images,
test_labels) = get_train_test(cfg.dataset_dir, validation_size=0.1)
#print(len(train_images), len(val_images), len(test_images))
## Visualize the training images
## some random samples are shown with the labels
#display_image_grid(train_images, train_labels)
train_dataset = SmokeDataset(
train_images, train_labels, resize=cfg.resize, transform=cfg.train_transform)
val_dataset = SmokeDataset(
val_images, val_labels, resize=cfg.resize, transform=cfg.val_transform)
## Visualize the augmented images after augmentation
#visualize_augmentations(train_dataset, idx=220, name='augments.jpg')
train_loader = DataLoader(
train_dataset, batch_size=cfg.batch_size, shuffle=True, num_workers=cfg.num_workers, pin_memory=cfg.pin_memory,
)
val_loader = DataLoader(
val_dataset, batch_size=cfg.batch_size, shuffle=True, num_workers=cfg.num_workers, pin_memory=cfg.pin_memory,
)
model = getattr(models, cfg.model_name)(
pretrained=False, num_classes=cfg.num_classes,)
if os.path.isfile(cfg.model_path):
model.load_state_dict(torch.load(cfg.model_path, map_location=cfg.device))
print('Saved Model found at',cfg.model_path ,' and loaded')
model = model.to(cfg.device)
criterion = nn.BCEWithLogitsLoss().to(cfg.device)
optimizer = torch.optim.Adam(model.parameters(), lr=cfg.learning_rate)
BEST_LOSS = np.inf
for epoch in range(1, cfg.epochs + 1):
hist = train(train_loader, model, criterion, optimizer, epoch, cfg)
history['train_loss'].append(hist.get('Loss'))
history['train_acc'].append(hist.get('Accuracy'))
hist = validate(val_loader, model, criterion, epoch, cfg)
if hist.get('Loss') < BEST_LOSS:
print('Saving model...')
torch.save(model.state_dict(), cfg.model_path)
BEST_LOSS = hist.get('Loss')
history['val_loss'].append(hist.get('Loss'))
history['val_acc'].append(hist.get('Accuracy'))
plot_curves(history['train_acc'], history['val_acc'], ylabel='Accuracy', name='accuracy.png')
plot_curves(history['train_loss'], history['val_loss'], ylabel='Loss', name='loss.png')
if __name__ == '__main__':
main()