-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_seg.py
218 lines (179 loc) · 9.16 KB
/
train_seg.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
from torch import nn
import torch.nn.functional as F
import torch
from torch.utils.data import DataLoader, SubsetRandomSampler
from dataset import BIPEDDataset
from loss import *
from config_seg import Config
from cyclicLR import CyclicCosAnnealingLR, LearningRateWarmUP
from segmentation_models_pytorch import Unet
from seg_models.unetresnet import UNetResNet
import torchgeometry as tgm
import numpy as np
import time
import os
import cv2 as cv
import tqdm
import glob
from random import sample
from lookahead import Lookahead
import matplotlib.pyplot as plt
from utils.metrics import Metrics
import random
def weight_init(m):
if isinstance(m, (nn.Conv2d, )):
torch.nn.init.normal_(m.weight, mean=0, std=0.01)
if m.weight.data.shape[1] == torch.Size([1]):
torch.nn.init.normal_(m.weight, mean=0.0,)
if m.weight.data.shape == torch.Size([1,6,1,1]):
torch.nn.init.constant_(m.weight,0.2)
if m.bias is not None:
torch.nn.init.zeros_(m.bias)
# for fusion layer
if isinstance(m, (nn.ConvTranspose2d,)):
torch.nn.init.normal_(m.weight,mean=0, std=0.01)
if m.weight.data.shape[1] == torch.Size([1]):
torch.nn.init.normal_(m.weight, std=0.1)
if m.bias is not None:
torch.nn.init.zeros_(m.bias)
def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
class Trainer(object):
def __init__(self, cfg):
self.cfg = cfg
self.edge_weight = [0.5, 0.5, 0.8, 0.8, 0.5, 1.1] ### rcf
self.num_classes = 3
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.model = UNetResNet(num_classes=self.num_classes).to(self.device).apply(weight_init)
# self.awl = AutomaticWeightedLoss(num=7)
self.criterion_seg = WeightedFocalLoss2d()
self.criterion_edge = bdcn_lossV3
optimizer = torch.optim.AdamW([
{'params': self.model.parameters()},
# {'params': self.awl.parameters(), 'weight_decay': 0}
])
self.optimizer = Lookahead(optimizer)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=20, verbose=True)
self.scheduler = LearningRateWarmUP(optimizer=optimizer, target_iteration=10, target_lr=0.0005,
after_scheduler=scheduler)
mkdir(cfg.model_output)
def load_net(self, resume):
self.model = torch.load(resume, map_location=self.device)
print('load pre-trained model successfully')
def build_loader(self):
imglist = glob.glob(f'{self.cfg.train_root}/*')
indices = list(range(len(imglist)))
indices = sample(indices, len(indices))
split = int(np.floor(0.15 * len(imglist)))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
print(f'Total images {len(imglist)}')
print(f'No of train images {len(train_idx)}')
print(f'No of validation images {len(valid_idx)}')
train_dataset = BIPEDDataset(self.cfg.train_root, crop_size=self.cfg.img_width)
valid_dataset = BIPEDDataset(self.cfg.train_root, crop_size=self.cfg.img_width)
train_loader = DataLoader(train_dataset,
batch_size=self.cfg.batch_size,
num_workers=self.cfg.num_workers,
shuffle=False,
sampler=train_sampler,
drop_last=True)
valid_loader = DataLoader(valid_dataset,
batch_size=self.cfg.batch_size,
num_workers=self.cfg.num_workers,
shuffle=False,
sampler=valid_sampler,
drop_last=True)
return train_loader, valid_loader
def validation(self, epoch, dataloader):
self.model.eval()
running_loss = []
metrics = Metrics(range(self.num_classes))
for batch_id, sample_batched in enumerate(dataloader):
images = sample_batched['images'].to(self.device) # BxCxHxW
labels_seg = sample_batched['labels'].to(self.device) # BxHxW
labels_edge = sample_batched['edges'].to(self.device)
wm = sample_batched['weight_mapping'].to(self.device)
file_name = sample_batched['file_name']
segments = self.model(images)
loss_seg = self.criterion_seg(segments, labels_seg, wm)
# loss_edge = sum([self.criterion_edge(edge, labels_edge, l_weight=l_w, wm=wm) for edge, l_w
# in zip(edges, self.edge_weight)]) / len(edges)
loss = loss_seg
print(time.ctime(), 'validation, Epoch: {0} Sample {1}/{2} Loss: {3}' \
.format(epoch, batch_id, len(dataloader), loss.item()), end='\r')
self.save_image_bacth_to_disk(segments, file_name)
running_loss.append(loss.detach().item())
for mask, output in zip(labels_seg, segments):
prediction = output.detach()
metrics.add(mask, prediction)
return np.mean(np.array(running_loss)), metrics.get_precision(), metrics.get_miou()
def save_image_bacth_to_disk(self, tensor, file_names):
output_dir = self.cfg.valid_output_dir
mkdir(output_dir)
assert len(tensor.shape) == 4, tensor.shape
for tensor_image, file_name in zip(tensor, file_names):
image_vis = tgm.utils.tensor_to_image(torch.sigmoid(tensor_image))[..., 0]
image_vis = (255.0 * (1.0 - image_vis)).astype(np.uint8) #
output_file_name = os.path.join(output_dir, f"{file_name}.png")
cv.imwrite(output_file_name, image_vis)
def train(self):
train_loader, valid_loader = self.build_loader()
best_loss = 1000000
best_train_loss = 1000000
valid_losses = []
train_losses = []
running_loss = []
plt.ion()
x = list(range(1, self.cfg.num_epochs)) # epoch array
for epoch in range(1, self.cfg.num_epochs):
self.model.train()
for batch_id, sample_batched in enumerate(train_loader):
images = sample_batched['images'].to(self.device) # BxCxHxW
labels_seg = sample_batched['labels'].to(self.device) # BxHxW
labels_edge = sample_batched['edges'].to(self.device)
wm = sample_batched['weight_mapping'].to(self.device)
segments = self.model(images)
loss_seg = self.criterion_seg(segments, labels_seg, wm)
# loss_edge = sum([self.criterion_edge(edge, labels_edge, l_weight=l_w, wm=wm) for edge, l_w
# in zip(edges, self.edge_weight)]) / len(edges)
loss = loss_seg
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
print(time.ctime(), 'training, Epoch: {0} Sample {1}/{2} Loss: {3}'\
.format(epoch, batch_id, len(train_loader), loss.item()), end='\r')
running_loss.append(loss.detach().item())
train_loss = np.mean(np.array(running_loss))
valid_loss, val_acc, val_miou = self.validation(epoch, valid_loader)
if epoch > 10:
self.scheduler.after_scheduler.step(valid_loss)
else:
self.scheduler.step(epoch)
lr = float(self.scheduler.after_scheduler.optimizer.param_groups[0]['lr'])
if valid_loss < best_loss:
torch.save(self.model, os.path.join(self.cfg.model_output, f'epoch{epoch}_model.pth'))
print(f'find optimal model, loss {best_loss}==>{valid_loss} \n')
best_loss = valid_loss
# print(f'lr {lr:.8f} \n')
valid_losses.append([valid_loss, lr])
np.savetxt(os.path.join(self.cfg.model_output, 'valid_loss.txt'), valid_losses, fmt='%.6f')
if os.path.exists(self.cfg.acc_path):
os.remove(self.cfg.acc_path)
with open(self.cfg.acc_path, 'a+') as f:
f.write(f"precision: {val_acc:.4f}.. \n"
f"iou: {val_miou:.4f}.. \n")
if train_loss < best_train_loss:
torch.save(self.model, os.path.join(self.cfg.model_output, f'train_best_model.pth'))
best_train_loss = train_loss
train_losses.append([train_loss, lr])
np.savetxt(os.path.join(self.cfg.model_output, 'train_loss.txt'), train_losses, fmt='%.6f')
torch.save(self.model, os.path.join(self.cfg.model_output, f'last_model.pth'))
# plt.ioff()
# plt.show()
if __name__ == '__main__':
config = Config()
trainer = Trainer(config)
trainer.train()