-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
119 lines (95 loc) · 4.95 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
import logging
import os
import time, datetime, math
import torch
from torch.utils.data import DataLoader
from data import LoadDataset
from model import init_model
from optimizer import init_optimizer
from loss import init_loss
from utils import parse_opt, load_cfg, \
create_confusion_matrix, update_confusion_matrix, draw_confusion_matrix
def train(config):
os.makedirs(config['train']['model_savepath'], exist_ok=True)
logging.basicConfig(filename = os.path.join(config['log']['path'], config['log']['name']),
format = '%(asctime)s : %(levelname)s : %(name)s : %(message)s',
filemode = config['log']['mode'], )
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
config['logger'] = logger
model_type = config['modelname']
train_data = LoadDataset(config, phase = 'train')
valid_data = LoadDataset(config, phase = 'valid')
train_loader = DataLoader(train_data, batch_size=config['train']['batch_size'], num_workers=config['train']['num_workers'], shuffle=True)
valid_loader = DataLoader(valid_data, batch_size=config['valid']['batch_size'], num_workers=config['valid']['num_workers'])
model = init_model(config)
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
optimizer = init_optimizer(model, config)
loss_fn = init_loss(config)
model = model.to(device)
loss_fn = loss_fn.to(device)
highest_acc = 0
batch_number = math.ceil(len(train_loader.dataset) / config['train']['batch_size'])
logger.info("Start training loop")
for epoch in range(config['train']['epoch']):
# Start Training
model.train()
start = time.time()
train_correct = 0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
pred_ = output.argmax(dim=1, keepdim=True)
target_ = target.argmax(dim=1, keepdim=True)
train_correct += pred_.eq(target_.view_as(pred_)).sum().item()
if batch_number // 10 > 0:
print_fre = batch_number // 10
else:
print_fre = 1
if batch_idx % print_fre == print_fre - 1:
iter_num = batch_idx * len(data)
total_data = len(train_loader.dataset)
iter_num = str(iter_num).zfill(len(str(total_data)))
total_percent = 100. * batch_idx / len(train_loader)
logger.info(f'Train Epoch {epoch + 1}: [{iter_num}/{total_data} ({total_percent:2.0f}%)] | Loss: {loss.item():.6f}')
# Start Validating
logger.info(f"Validating {len(valid_loader.dataset)} images")
model.eval()
valid_correct = 0
c_matrix = create_confusion_matrix(config['class']['num'])
for (data, target) in valid_loader:
data, target = data.to(device), target.to(device)
output = model(data)
pred = output.argmax(dim=1, keepdim=True)
target = target.argmax(dim=1, keepdim=True)
valid_correct += pred.eq(target.view_as(pred)).sum().item()
c_matrix = update_confusion_matrix(c_matrix, pred, target)
logger.info('Validation completed\n')
train_accuracy = 100. * train_correct / len(train_loader.dataset)
valid_accuracy = 100. * valid_correct / len(valid_loader.dataset)
logger.info('Train set: Accuracy: {}/{} ({:.2f}%)'.format(
train_correct, len(train_loader.dataset), train_accuracy))
logger.info('Valid set: Accuracy: {}/{} ({:.2f}%)'.format(
valid_correct, len(valid_loader.dataset), valid_accuracy))
logger.info(f'\nConfusion matrix of valid set\n{c_matrix}')
stop = time.time()
runtime = stop - start
eta = int(runtime * (config['train']['epoch'] - epoch - 1))
eta = str(datetime.timedelta(seconds=eta))
logger.info(f'Runing time: Epoch {epoch + 1}: {str(datetime.timedelta(seconds=int(runtime)))} | ETA: {eta}')
torch.save(model.state_dict(), os.path.join(config['train']['model_savepath'], f'{model_type}_last.pth'))
logger.info(f"Saving last model to {os.path.join(config['train']['model_savepath'], f'{model_type}_last.pth')}\n")
if valid_accuracy >= highest_acc:
highest_acc = valid_accuracy
torch.save(model.state_dict(), os.path.join(config['train']['model_savepath'], f'{model_type}_best.pth'))
logger.info(f"Saving best model to {os.path.join(config['train']['model_savepath'], f'{model_type}_best.pth')}\n")
draw_confusion_matrix(config, c_matrix.cpu().numpy())
if __name__ == '__main__':
opt = parse_opt()
cfg = load_cfg(opt.cfg)
train(config = cfg)