-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
219 lines (159 loc) · 7.46 KB
/
train.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
import os
import random
import shutil
import cv2
import time
import math
import pprint
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from tensorboardX import SummaryWriter
from models.model_factory import get_model
from factory.losses import get_loss
from factory.schedulers import get_scheduler
from factory.optimizers import get_optimizer
from factory.transforms import Normalize, ToTensor, Albu, CV2_Resize
from datasets.dataloader import get_dataloader
import utils.config
import utils.checkpoint
from utils.metrics import kappa, batch_cohen_kappa_score
from utils.tools import prepare_train_directories, AverageMeter
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
global device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def evaluate_single_epoch(config, model, dataloader, criterion, writer, epoch):
batch_time = AverageMeter()
losses = AverageMeter()
scores = AverageMeter()
model.eval()
with torch.no_grad():
end = time.time()
for i, (images, labels) in enumerate(dataloader):
images = images.to(device)
labels = labels.to(device)
logits = model(images)
loss = criterion(logits, labels)
losses.update(loss.item(), images.shape[0])
preds = logits.detach().cpu().numpy().squeeze()
labels = labels.detach().cpu().numpy().squeeze()
score = batch_cohen_kappa_score(preds, labels)
scores.update(score.item(), images.shape[0])
del images, labels, logits, preds
torch.cuda.empty_cache()
batch_time.update(time.time() - end)
end = time.time()
if i % config.PRINT_EVERY == 0:
print('[%2d/%2d] time: %.2f, val_loss: %.6f, val_score: %.4f'
% (i, len(dataloader), batch_time.sum, loss, score))
writer.add_scalar('val/loss', losses.avg, epoch)
writer.add_scalar('val/score', scores.avg, epoch)
print('average loss over VAL epoch: %f' % losses.avg)
return scores.avg, losses.avg
def train_single_epoch(config, model, dataloader, criterion, optimizer, writer, epoch):
batch_time = AverageMeter()
losses = AverageMeter()
scores = AverageMeter()
model.train()
end = time.time()
for i, (images, labels) in enumerate(dataloader):
optimizer.zero_grad()
images = images.to(device)
labels = labels.to(device)
logits = model(images)
loss = criterion(logits, labels)
losses.update(loss.item(), images.shape[0])
loss.backward()
optimizer.step()
preds = logits.detach().cpu().numpy().squeeze()
labels = labels.detach().cpu().numpy().squeeze()
score = batch_cohen_kappa_score(preds, labels)
scores.update(score.item(), images.shape[0])
if config.DEBUG_IMAGE:
save_dir = os.path.join(config.TRAIN_DIR, 'save_image')
os.makedirs(save_dir, exist_ok=True)
if i % config.PRINT_EVERY == 0:
images = images.detach().cpu().numpy()
cv2.imwrite(os.path.join(save_dir, '%s_image.png' % i), np.uint8((images[0][0]+1)/2 * 255))
del images, labels, logits, preds
torch.cuda.empty_cache()
batch_time.update(time.time() - end)
end = time.time()
if i % config.PRINT_EVERY == 0:
print("[%d/%d][%d/%d] time: %.2f, train_loss: %.6f, train_score: %.4f, lr: %f"
% (epoch, config.TRAIN.NUM_EPOCHS, i, len(dataloader), batch_time.sum, loss, score, optimizer.param_groups[0]['lr']))
writer.add_scalar('train/score', scores.avg, epoch)
writer.add_scalar('train/loss', losses.avg, epoch)
writer.add_scalar('train/lr', optimizer.param_groups[0]['lr'], epoch)
print('average loss over TRAIN epoch: %f' % losses.avg)
def train(config, model, train_loader, test_loader, criterion, optimizer, scheduler, writer, start_epoch, best_score, best_loss):
num_epochs = config.TRAIN.NUM_EPOCHS
model = model.to(device)
for epoch in range(start_epoch, num_epochs):
train_single_epoch(config, model, train_loader, criterion, optimizer, writer, epoch)
test_score, test_loss = evaluate_single_epoch(config, model, test_loader, criterion, writer, epoch)
print('Total Test Score: %.4f, Test Loss: %.4f' % (test_score, test_loss))
# if test_score > best_score:
# best_score = test_score
# print('Test score Improved! Save checkpoint')
# utils.checkpoint.save_checkpoint(config, model, epoch, test_score, test_loss)
utils.checkpoint.save_checkpoint(config, model, epoch, test_score, test_loss)
if config.SCHEDULER.NAME == 'reduce_lr_on_plateau':
scheduler.step(test_score)
else:
scheduler.step()
def run(config):
model = get_model(config).to(device)
criterion = get_loss(config.LOSS.NAME)
optimizer = get_optimizer(config, model.parameters())
checkpoint = utils.checkpoint.get_initial_checkpoint(config)
if checkpoint is not None:
last_epoch, score, loss = utils.checkpoint.load_checkpoint(config, model, checkpoint)
else:
print('[*] no checkpoint found')
last_epoch, score, loss = -1, -1, float('inf')
print('last epoch:{} score:{:.4f} loss:{:.4f}'.format(last_epoch, score, loss))
optimizer.param_groups[0]['initial_lr'] = config.OPTIMIZER.LR
scheduler = get_scheduler(config, optimizer, last_epoch)
if config.SCHEDULER.NAME == 'multi_step':
milestones = scheduler.state_dict()['milestones']
step_count = len([i for i in milestones if i < last_epoch])
optimizer.param_groups[0]['lr'] *= scheduler.state_dict()['gamma'] ** step_count
if last_epoch != -1:
scheduler.step()
writer = SummaryWriter(os.path.join(config.TRAIN_DIR, 'logs'))
train_loader = get_dataloader(config, 'train', transform=transforms.Compose([
Albu(),
CV2_Resize(config.DATA.IMG_W, config.DATA.IMG_H),
Normalize(),
ToTensor()]))
val_loader = get_dataloader(config, 'val', transform=transforms.Compose([CV2_Resize(config.DATA.IMG_W, config.DATA.IMG_H),
Normalize(),
ToTensor()]))
train(config, model, train_loader, val_loader, criterion, optimizer, scheduler, writer, last_epoch+1, score, loss)
def seed_everything():
seed = 2019
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def main():
import warnings
warnings.filterwarnings("ignore")
print('start training.')
seed_everything()
yml = 'configs/base.yml'
config = utils.config.load(yml)
prepare_train_directories(config)
pprint.pprint(config, indent=2)
shutil.copy(yml, os.path.join(config.TRAIN_DIR, 'config.yml'))
run(config)
print('success!')
if __name__ == '__main__':
main()