-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
226 lines (186 loc) · 7.39 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
'''
train scripts
author: zacario li
date: 2020-04-02
'''
import os
import random
import time
import cv2
import numpy as np
import logging
import argparse
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.multiprocessing as mp
import torch.distributed as dist
import torch.nn.init as initer
from tensorboardX import SummaryWriter
import sys
from utils import dataset, transform, common
from models import fastscnn
from loss import diceloss
numClasses = 21
dataRoot = 'voc2012'
trainList = 'voc2012/train.txt'
valList = 'voc2012/val.txt'
globalEpoch = 2000
baseLr = 0.01
inputHW = [320, 320]
cv2.ocl.setUseOpenCL(True)
cv2.setNumThreads(32)
def poly_learning_rate(base_lr, curr_iter, max_iter, power=0.9):
"""poly learning rate policy"""
lr = base_lr * (1 - float(curr_iter) / max_iter) ** power
return lr
def weightsInit(model):
for m in model.modules():
if isinstance(m, (nn.modules.conv._ConvNd)):
initer.kaiming_normal_(m.weight)
if m.bias is not None:
initer.constant_(m.bias, 0)
elif isinstance(m, (nn.modules.batchnorm._BatchNorm)):
initer.normal_(m.weight, 1.0, 0.02)
initer.constant_(m.bias, 0.0)
elif isinstance(m, nn.Linear):
initer.kaiming_normal_(m.weight)
if m.bias is not None:
initer.constant_(m.bias, 0)
def getMeanStd():
value_scale = 255
mean = [0.485, 0.456, 0.406]
mean = [item * value_scale for item in mean]
std = [0.229, 0.224, 0.225]
std = [item * value_scale for item in std]
return mean, std
def prepareDataset(rootpath, trainlist, vallist, mean, std):
# prepare dataset transform before training
trans = transform.Compose([
transform.RandScale([0.5,2]),
transform.RandomGaussianBlur(),
transform.RandomHorizontalFlip(),
transform.Crop(inputHW,crop_type='rand',padding=mean, ignore_label=255),
transform.ToTensor(),
transform.Normalize(mean=mean,std=std)
])
# val transform
valTrans = transform.Compose([
transform.Crop(inputHW, crop_type='center', padding=mean, ignore_label=255),
transform.ToTensor(),
transform.Normalize(mean=mean, std=std)
])
# training data
trainData = dataset.SemData(split='train', data_root=rootpath, data_list=trainlist, transform=trans)
trainDataLoader = torch.utils.data.DataLoader(trainData,
batch_size=160,
shuffle=True,
num_workers=32,
pin_memory=True,
drop_last=True)
# val data
valData = dataset.SemData(split='val', data_root=rootpath, data_list=vallist, transform=valTrans)
valDataLoader = torch.utils.data.DataLoader(valData,
batch_size=4,
shuffle=False,
num_workers=4,
pin_memory=True)
# return datasets
return trainDataLoader, valDataLoader
def subTrain(model, optimizer, criterion, dataLoader, currentepoch, maxIter, device):
# set to train mode to enable dropout and bn
model.train()
intersectionMeter = common.AverageMeter()
unionMeter = common.AverageMeter()
targetMeter = common.AverageMeter()
lossMeter = common.AverageMeter()
for i, (x, y) in enumerate(dataLoader):
x = x.to(device)
y = y.to(device)
out = model(x)
mainLoss = criterion(out[0], y)
auxLoss = criterion(out[1], y)
# whole loss
loss = 0.4*auxLoss + mainLoss
lossMeter.update(loss.item(), x.shape[0])
#print('loss is:', loss.item())
# step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# ajust lr
curIter = currentepoch * len(dataLoader) + i + 1
newLr = poly_learning_rate(baseLr, curIter, maxIter)
optimizer.param_groups[0]['lr'] = newLr
# compute IoU/accuracy
result = out[0].max(1)[1]
intersection, union, target = common.intersectionAndUnionGPU(result, y, numClasses, 255)
intersection, union, target = intersection.cpu().numpy(), union.cpu().numpy(), target.cpu().numpy()
## update meter
intersectionMeter.update(intersection), unionMeter.update(union), targetMeter.update(target)
# after every epoch, print the log
IoU = intersectionMeter.sum/(unionMeter.sum + 1e-10)
accuracy = intersectionMeter.sum/(targetMeter.sum + 1e-10)
print(f'[{currentepoch}/{globalEpoch}] loss:',lossMeter.avg)
'''
for i in range(numClasses):
print(f'training Class_{i} IoU: {IoU[i]}, acc: {accuracy[i]}')
'''
def subVal(model, criterion, dataLoader, device):
# set to eval mode
model.eval()
intersectionMeter = common.AverageMeter()
unionMeter = common.AverageMeter()
targetMeter = common.AverageMeter()
lossMeter = common.AverageMeter()
for i, (x, y) in enumerate(dataLoader):
x = x.to(device)
y = y.to(device)
out = model(x)
mainLoss = criterion(out[0], y)
# update val loss
lossMeter.update(mainLoss.item(), x.shape[0])
# compute IoU/accuracy
result = out[0].max(1)[1]
intersection, union, target = common.intersectionAndUnionGPU(result, y, numClasses, 255)
intersection, union, target = intersection.cpu().numpy(), union.cpu().numpy(), target.cpu().numpy()
## update meter
intersectionMeter.update(intersection), unionMeter.update(union), targetMeter.update(target)
# show the log
IoU = intersectionMeter.sum/(unionMeter.sum + 1e-10)
accuracy = intersectionMeter.sum/(targetMeter.sum + 1e-10)
print(f'val loss:',lossMeter.avg)
for i in range(numClasses):
print('Class_'+str(i)+' IoU:',IoU[i],' acc:',accuracy[i])
def train():
#device
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
model = fastscnn.FastSCNN(numClasses, True)
numParams = sum(torch.numel(p) for p in model.parameters() )
print(f'Total paramers: {numParams}')
model = model.to(device)
weightsInit(model)
mean,std = getMeanStd()
#criterion = nn.CrossEntropyLoss(ignore_index=255)
criterion = diceloss.DiceLoss()
optimizer = torch.optim.SGD(model.parameters(),lr=baseLr, momentum=0.9, weight_decay=0.0001)
# get dataset
trainDataLoader, valDataLoader = prepareDataset(dataRoot, trainList, valList, mean, std)
# prepare something for learning rate
maxIter = globalEpoch * len(trainDataLoader)
# start training
for epoch in range(1, globalEpoch):
# do train on every epoch
subTrain(model, optimizer, criterion, trainDataLoader, epoch, maxIter, device)
# evaluate
subVal(model, criterion, valDataLoader, device)
# save model
if ( (epoch) % 20) == 0:
filename = 'save/'+'train_'+str(epoch)+'.pth'
torch.save({'epoch':epoch, 'state_dict':model.state_dict(), 'optimizer':optimizer.state_dict()}, filename)
if __name__ == '__main__':
train()