-
Notifications
You must be signed in to change notification settings - Fork 48
/
Train_one_epoch.py
141 lines (115 loc) · 5.21 KB
/
Train_one_epoch.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
# -*- coding: utf-8 -*-
# @Time : 2021/6/19 2:14 下午
# @Author : Haonan Wang
# @File : Train_one_epoch.py
# @Software: PyCharm
import torch.optim
import os
import time
from utils import *
import Config as config
import warnings
warnings.filterwarnings("ignore")
def print_summary(epoch, i, nb_batch, loss, loss_name, batch_time,
average_loss, average_time, iou, average_iou,
dice, average_dice, acc, average_acc, mode, lr, logger):
'''
mode = Train or Test
'''
summary = ' [' + str(mode) + '] Epoch: [{0}][{1}/{2}] '.format(
epoch, i, nb_batch)
string = ''
string += 'Loss:{:.3f} '.format(loss)
string += '(Avg {:.4f}) '.format(average_loss)
# string += 'IoU:{:.3f} '.format(iou)
# string += '(Avg {:.4f}) '.format(average_iou)
string += 'Dice:{:.4f} '.format(dice)
string += '(Avg {:.4f}) '.format(average_dice)
# string += 'Acc:{:.3f} '.format(acc)
# string += '(Avg {:.4f}) '.format(average_acc)
if mode == 'Train':
string += 'LR {:.2e} '.format(lr)
# string += 'Time {:.1f} '.format(batch_time)
string += '(AvgTime {:.1f}) '.format(average_time)
summary += string
logger.info(summary)
# print summary
##################################################################################
#=================================================================================
# Train One Epoch
#=================================================================================
##################################################################################
def train_one_epoch(loader, model, criterion, optimizer, writer, epoch, lr_scheduler, model_type, logger):
logging_mode = 'Train' if model.training else 'Val'
end = time.time()
time_sum, loss_sum = 0, 0
dice_sum, iou_sum, acc_sum = 0.0, 0.0, 0.0
dices = []
for i, (sampled_batch, names) in enumerate(loader, 1):
try:
loss_name = criterion._get_name()
except AttributeError:
loss_name = criterion.__name__
# Take variable and put them to GPU
images, masks = sampled_batch['image'], sampled_batch['label']
images, masks = images.cuda(), masks.cuda()
# ====================================================
# Compute loss
# ====================================================
preds = model(images)
out_loss = criterion(preds, masks.float()) # Loss
if model.training:
optimizer.zero_grad()
out_loss.backward()
optimizer.step()
# print(masks.size())
# print(preds.size())
# train_iou = 0
train_iou = iou_on_batch(masks,preds)
train_dice = criterion._show_dice(preds, masks.float())
batch_time = time.time() - end
# train_acc = acc_on_batch(masks,preds)
if epoch % config.vis_frequency == 0 and logging_mode is 'Val':
vis_path = config.visualize_path+str(epoch)+'/'
if not os.path.isdir(vis_path):
os.makedirs(vis_path)
save_on_batch(images,masks,preds,names,vis_path)
dices.append(train_dice)
time_sum += len(images) * batch_time
loss_sum += len(images) * out_loss
iou_sum += len(images) * train_iou
# acc_sum += len(images) * train_acc
dice_sum += len(images) * train_dice
if i == len(loader):
average_loss = loss_sum / (config.batch_size*(i-1) + len(images))
average_time = time_sum / (config.batch_size*(i-1) + len(images))
train_iou_average = iou_sum / (config.batch_size*(i-1) + len(images))
# train_acc_average = acc_sum / (config.batch_size*(i-1) + len(images))
train_dice_avg = dice_sum / (config.batch_size*(i-1) + len(images))
else:
average_loss = loss_sum / (i * config.batch_size)
average_time = time_sum / (i * config.batch_size)
train_iou_average = iou_sum / (i * config.batch_size)
# train_acc_average = acc_sum / (i * config.batch_size)
train_dice_avg = dice_sum / (i * config.batch_size)
end = time.time()
torch.cuda.empty_cache()
if i % config.print_frequency == 0:
print_summary(epoch + 1, i, len(loader), out_loss, loss_name, batch_time,
average_loss, average_time, train_iou, train_iou_average,
train_dice, train_dice_avg, 0, 0, logging_mode,
lr=min(g["lr"] for g in optimizer.param_groups),logger=logger)
if config.tensorboard:
step = epoch * len(loader) + i
writer.add_scalar(logging_mode + '_' + loss_name, out_loss.item(), step)
# plot metrics in tensorboard
writer.add_scalar(logging_mode + '_iou', train_iou, step)
# writer.add_scalar(logging_mode + '_acc', train_acc, step)
writer.add_scalar(logging_mode + '_dice', train_dice, step)
torch.cuda.empty_cache()
if lr_scheduler is not None:
lr_scheduler.step()
# if epoch + 1 > 10: # Plateau
# if lr_scheduler is not None:
# lr_scheduler.step(train_dice_avg)
return average_loss, train_dice_avg