-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
308 lines (241 loc) · 13.3 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
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
# Pytorch
import torch
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
# Parameter Setup
from parameters import *
# My Libraries
from path import *
from networks import nets as net
from dataloader import dataset
from dataloader import dict_transforms
from functions import loss_F
from functions import utils
from functions import augmentation
from functions.plot import PlotGenerator
from networks.nets import *
# Basic Utility
import os
import time
from collections import OrderedDict
import matplotlib.pyplot as plt
import numpy as np
if __name__ == "__main__":
environment = {}
# ======================================= Directory Panel =============================================
data_man = DataManager(os.getcwd())
mode = 'overlay' # please set the mode. ['new', 'load', 'overlay', 'external_train']
load_branch = 7 # need 'load' or 'overlay' mode. you can set integer here.
load_num = 450 # need 'load' or 'overlay' mode. you can set integer here.
# external_directory = r'/home/user/codes/Python/models/Semantic-Segmentation/DeepLabV3_ResNet50_COCO2017.pth' # need 'external_train' mode.
# train
dir_man = DirectoryManager(model_name=model_name, mode=mode, branch_num=load_branch,
load_num=load_num)
# external train
# dir_man = DirectoryManager(model_name=model_name, mode=mode, external_weight=external_directory)
# =====================================================================================================
# ============================================== model definition ==============================================
if mode is 'new':
print('start training from epoch 1.')
params['resume_epoch'] = 1
print('constructing network...')
netend = net.NetEnd(num_classes=params['num_classes'])
network = net.ResNet50_DeeplabV3(netend, pretrain=permission['pretrain']) # <- model definition
elif mode is 'external_train':
print('start training from epoch 1.')
params['resume_epoch'] = 1
print('constructing network...')
# please define model here.
netend = net.NetEnd(num_classes=params['num_classes'])
network = net.ResNet50_DeeplabV3(netend, pretrain=True)
path = dir_man.load()
print(f'{dir_man.load()} loading...')
network.load_state_dict((torch.load(path)))
elif mode is 'load' or mode is 'overlay':
assert load_branch is not None, 'check load_branch.'
assert load_num is not None, 'check load_num.'
print('loading weights file...')
params['resume_epoch'] = load_num + 1
# model load
print('constructing network...')
netend = ClassifierEnd(num_classes=params['num_classes'])
path = dir_man.load()
network = ResNet101_DeeplabV3(end_module=netend, pretrain=permission['pretrain'], freeze=True) # <- model definition
print(f'{dir_man.load()} loading...')
network.load_state_dict((torch.load(path)), strict=False)
else:
print("please modify 'mode' variable.")
import sys
sys.exit(1)
assert params['total_epochs'] >= params['resume_epoch'], 'please check resume start point.'
# ==============================================================================================================
print('network has been constructed.')
print(f'branch number : {dir_man.branch_info()}')
print(f'model name: {dir_man.name_info()}')
# ================================= GPU setting =================================
if torch.cuda.is_available():
device = torch.device('cuda')
print(f'GPU {torch.cuda.get_device_name()} available.')
network.cuda()
environment['gpu'] = True
else:
device = torch.device("cpu")
print(f'GPU unable.')
environment['gpu'] = False
# ===============================================================================
# ========================== optimizer ==========================
optimizer = torch.optim.Adam(network.parameters(), lr=params['learning_rate'])
# ===============================================================================
# =========================================== image pre-processing & load ==========================================
toronto_setting = transforms.Compose([augmentation.AugManager(),
dict_transforms.DictResize(params['resized']),
dict_transforms.DictNormalize(gray=True,
mean=params['mean'],
std=params['std']),
dict_transforms.Dict2Tensor(two_dim=True)])
pix2pix_setting = transforms.Compose([augmentation.AugManager(),
dict_transforms.DictResize(params['resized']),
dict_transforms.DictNormalize(gray=True,
mean=params['mean'],
std=params['std']),
dict_transforms.Dict2Tensor(two_dim=True)])
# training set
train_set_Toronto = dataset.RoadDataset(data_dir=data_man.train(), label_dir=data_man.label('train'), transform=toronto_setting)
train_set_Pix = dataset.RoadDataset(data_dir=data_man.train(), label_dir=data_man.label('train'), transform=pix2pix_setting,
dataname_extension='*.jpg', labelname_extension='*.jpg')
train_set = train_set_Toronto + train_set_Pix
print(f'train data : {len(train_set)} files detected.')
train_loader = DataLoader(dataset=train_set, batch_size=params['train_batch'],
shuffle=permission['shuffle'], num_workers=user_setting['train_processes'])
# validating set
if permission['validation']:
valid_set_Toronto = dataset.RoadDataset(data_dir=data_man.validation(), label_dir=data_man.label('valid'), transform=toronto_setting)
valid_set_Pix = dataset.RoadDataset(data_dir=data_man.validation(), label_dir=data_man.label('valid'), transform=pix2pix_setting,
dataname_extension='*.jpg', labelname_extension='*.jpg')
valid_set = valid_set_Toronto + valid_set_Pix
print(f'validation data : {len(valid_set)} files detected.')
valid_loader = DataLoader(dataset=valid_set, batch_size=params['valid_batch'],
shuffle=False, num_workers=user_setting['valid_processes'])
# ==================================================================================================================
epoch_loss = 0.0
if permission['validation']:
print(f'your validation epoch setting : {user_setting["validation_intervals"]}')
else:
print(f'validation disabled.')
train_loss_dict = {}
iter_loss_dict = {}
val_loss_dict = {}
# =========================== training epoch ===============================
# training
print(f'training start.')
for epoch in range(params['resume_epoch'], params['total_epochs'] + 1):
network.train()
iter_loss = 0.0
epoch_start = time.perf_counter()
print(f'epoch: {epoch}')
# ========================================= training ==============================================
for i, data in enumerate(train_loader):
# load
image, label = data[tag_image], data[tag_label]
name = data[tag_name]
# gpu copy
if environment['gpu']:
image, label = image.cuda(), label.cuda()
# optimizer initialization
optimizer.zero_grad()
# Forwarding
output = network.forward(image)
# loss operation
loss = loss_F.binary_entropy_2d(output, label)
epoch_loss += loss.item()
iter_loss += loss.item()
loss.backward()
optimizer.step() # update
# iter loss operation & print
if i % user_setting['iter_print_intervals'] == user_setting['iter_print_intervals']-1:
iter_loss /= user_setting['iter_print_intervals']
if permission['iter_print']:
print(f'[{epoch}, {i+1}] iteration loss : {iter_loss : .3f}')
iter_loss_dict[str(epoch)+':'+str(i+1)] = iter_loss
if permission['loss_save']:
utils.write_line({str(epoch)+':'+str(i+1): iter_loss_dict[str(epoch)+':'+str(i+1)]},
os.path.join(dir_man.branch(), 'history', model_name+'_iter'+'.txt'))
iter_loss = 0.0 # reset
# ========================================= training ==============================================
# model prediction image store.
if permission['train_predict_store']:
if epoch % user_setting['img_save_intervals'] == 0:
utils.imgstore(output*255.0, nums=2, save_dir=dir_man.train_predict_store(), epoch=epoch,
cls='pred', filename=name)
utils.imgstore(label*255.0, nums=2, save_dir=dir_man.train_predict_store(), epoch=epoch,
cls='label', filename=name)
# epoch loss print
if epoch % user_setting['epoch_store_intervals'] == 0:
epoch_loss /= len(train_loader)
epoch_loss /= user_setting['epoch_store_intervals']
if permission['epoch_print']:
print(f'{epoch} epoch train loss : {epoch_loss : .3f}')
train_loss_dict[str(epoch)] = epoch_loss
if permission['loss_save']:
utils.write_line({str(epoch): train_loss_dict[str(epoch)]},
os.path.join(dir_man.branch(), 'history', model_name+'_epoch'+'.txt'))
epoch_loss = 0
# save the model weights
if epoch % user_setting['model_store_intervals'] == 0:
if permission['snapshot_save']:
utils.snapshot_maker(params, dir_man.save_dir()+str(epoch)+'.txt')
if permission['weight_save']:
torch.save(network.state_dict(), dir_man.save_dir()+str(epoch)+'.pth')
print(f'The model weights saved at epoch {epoch}.')
print(f'save directory : {dir_man.save_dir()+str(epoch)+".pth"}')
# ========================================= validating ==============================================
if permission['validation']:
if epoch % user_setting['validation_intervals'] == 0:
network.eval()
iter_loss = 0.0
# ==================================== validation iter =========================================
for i, val in enumerate(valid_loader):
image, label = val[tag_image], val[tag_label]
if environment['gpu']: # gpu copy
image, label = image.cuda(), label.cuda()
with torch.no_grad():
output = network.forward(image)
loss = loss_F.binary_entropy_2d(output, label)
iter_loss += loss.item()
# ==============================================================================================
iter_loss /= len(valid_loader)
if permission['valid_print']:
print(f'{epoch} epoch validation loss : {iter_loss:.3f}')
val_loss_dict[str(epoch)] = iter_loss
if permission['loss_save']:
utils.write_line({str(epoch): val_loss_dict[str(epoch)]},
os.path.join(dir_man.branch(), 'history', model_name+'_valid'+'.txt'))
iter_loss = 0.0
# ========================================= validating ==============================================
print(f'{time.perf_counter()-epoch_start:.3f} seconds spended.')
# =========================== training epoch ===============================
# =================================== plot =======================================
# train loss
plot1 = PlotGenerator(1, 'train', (20, 15), xlabel='epochs', ylabel='BCELoss')
plot1.add_data(train_loss_dict)
plot1.add_set(name='train loss', color='r')
# plot1.interval_remove(interval=3, idx=0, update=True)
plot1.plot()
plot1.save(os.path.join(dir_man.graph_store(), 'train.png'))
# validation loss
if permission['validation']:
plot2 = PlotGenerator(2, 'Validation', (20, 15), xlabel='epochs', ylabel='BCELoss')
plot2.add_data(val_loss_dict)
plot2.add_set(name='validation loss', color='g')
# plot2.interval_remove(interval=3, idx=0, update=True)
plot2.plot()
plot2.save(os.path.join(dir_man.graph_store(), 'validation.png'))
# train & validation loss
plot3 = PlotGenerator(3, 'Training', (20, 15), xlabel='epochs', ylabel='BCELoss')
plot3.add_data(plot1.data(0))
plot3.add_data(plot2.data(0))
plot3.add_set(data=plot1.set(0))
plot3.add_set(data=plot2.set(0))
plot3.plot()
plot3.save(os.path.join(dir_man.graph_store(), 'Training.png'))
print('training ends.')