-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain_res50unet_warm.py
226 lines (189 loc) · 8.13 KB
/
train_res50unet_warm.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
'''
2022.01.19
@Yinxia Cao
@function: used for training ZY3LC on all cities, 8 bit images
'''
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
import random
import numpy as np
from tqdm import tqdm
import torch.nn as nn
from torch.utils import data
from tensorboardX import SummaryWriter #change tensorboardX
from ZY3LC_dataset import dataloader
from ZY3LC_loader import myImageFloder_8bit_binary
from metrics import SegmentationMetric, AverageMeter
import segmentation_models_pytorch as smp
import shutil
from myloss import BCE_DICE
import argparse
def get_arguments():
parser = argparse.ArgumentParser(description="Test for binary class")
parser.add_argument("--classname", type=str, default='build',
help="oisa|grass|tree|soil|build|water|road")
args = parser.parse_args()
return args
def adjust_learning_rate(optimizer, epoch):
if epoch <= 20:
lr = 0.001
elif epoch <= 40:
lr = 0.0001
else:
lr = 0.00001
# print(lr)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr #added
def main():
# Setup seeds
torch.manual_seed(1337)
torch.cuda.manual_seed(1337)
np.random.seed(1337)
random.seed(1337)
# Get args
args = get_arguments()
# Setup device
device = 'cuda'
# Setup Dataloader
filepath = 'data' # data path
train_img, train_lab, val_img, val_lab,_,_ = dataloader(filepath, split=(0.9, 0.1,0), issave=True) # 90% for training
# batch_size = 16
epochs = 20
iroot = 'runs'
logdir = os.path.join(iroot, 'res50' + args.classname + '_warm')
writer = SummaryWriter(log_dir=logdir)
# NUM_WORKERS = 4
classes = 2 # 0, 1, 2, 3, 4, 5, 6
nchannels = 4
global best_acc
best_acc = 0
positive = 255 # values of buildings
# train on the randomly cropped images
traindataloader = torch.utils.data.DataLoader(
myImageFloder_8bit_binary(train_img, train_lab, aug=True, channels=nchannels, positive=positive),
batch_size=32, shuffle=True, num_workers=8, pin_memory=True)
# test on the whole images
valdataloader = torch.utils.data.DataLoader(
myImageFloder_8bit_binary(val_img, val_lab, aug=False, channels=nchannels, positive=positive),
batch_size=32, shuffle=False, num_workers=8, pin_memory=True)
model = smp.Unet(encoder_name="resnet50", encoder_weights="imagenet",
in_channels=nchannels, classes=1).to(device)
if torch.cuda.device_count() > 1:
model = torch.nn.DataParallel(model, device_ids=range(torch.cuda.device_count()))
# print the model
start_epoch = 0
resume = os.path.join(logdir, 'finetune_80.tar')
if os.path.isfile(resume):
print("=> loading checkpoint '{}'".format(resume))
checkpoint = torch.load(resume)
model.load_state_dict(checkpoint['state_dict'])
# optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(resume, checkpoint['epoch']))
start_epoch = checkpoint['epoch']
val_miou_init = checkpoint['val_miou_init']
else:
print("=> no checkpoint found at resume")
print("=> Will start from scratch.")
# get all parameters (model parameters + task dependent log variances)
# print('Number of model parameters: {}'.format(sum([p.data.nelement() for p in model.parameters()])))
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999))
#weights = torch.FloatTensor([1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 5.0]).to(device) # defined
# criterion = torch.nn.CrossEntropyLoss()
criterion = BCE_DICE()
# warm-up
for epoch in range(epochs-start_epoch):
epoch = start_epoch + epoch + 1 # current epochs
adjust_learning_rate(optimizer, epoch)
lr = optimizer.param_groups[0]['lr']
print('epoch %d, lr: %.6f'%(epoch, lr))
train_loss, train_f1, train_iou = train_epoch(model, criterion,
traindataloader,
optimizer, device, epoch, classes)
val_loss, val_f1, val_iou = vtest_epoch(model, criterion, valdataloader, device, epoch, classes)
# save every epoch
savefilename = os.path.join(logdir, 'checkpoint.tar')
torch.save({
'epoch': epoch,
'state_dict': model.module.state_dict() if hasattr(model, "module") else model.state_dict(), # multiple GPUs
'val_f1': val_f1,
}, savefilename)
if epoch%10==0:
torch.save({
'epoch': epoch,
'state_dict': model.module.state_dict() if hasattr(model, "module") else model.state_dict(),
# multiple GPUs
'val_f1': val_f1,
}, os.path.join(logdir, 'checkpoint'+str(epoch)+'.tar'))
# write
writer.add_scalar('lr', lr, epoch)
writer.add_scalar('train/1.loss', train_loss,epoch)
writer.add_scalar('train/2.f1', train_f1, epoch)
writer.add_scalar('train/3.iou',train_iou, epoch)
writer.add_scalar('val/1.loss', val_loss, epoch)
writer.add_scalar('val/2.f1',val_f1, epoch)
writer.add_scalar('val/3.iou', val_iou, epoch)
writer.close()
# train
def train_epoch(model, criterion, dataloader, optimizer, device, epoch, classes):
model.train()
acc_total = SegmentationMetric(numClass=classes, device=device)
losses = AverageMeter()
num = len(dataloader)
pbar = tqdm(range(num), disable=False)
for idx, (images, labels) in enumerate(dataloader):
images = images.to(device, non_blocking=True)
labels = labels.to(device, non_blocking=True).unsqueeze(1)
output = model(images)
output = torch.sigmoid(output)
loss = criterion(output, labels.float())
optimizer.zero_grad()
loss.backward()
optimizer.step()
output = (output > 0.5) # N C H W
acc_total.addBatch(output, labels)
losses.update(loss.item(), images.size(0))
f1 = acc_total.F1score()[1]
iou = acc_total.IntersectionOverUnion()[1]
pbar.set_description(
'Train Epoch:{epoch:4}. Iter:{batch:4}|{iter:4}. Loss {loss:.3f}. F1 {f1:.3f}, IOU: {iou:.3f}'.format(
epoch=epoch, batch=idx, iter=num, loss=losses.avg, f1=f1, iou=iou))
pbar.update()
pbar.close()
f1 = acc_total.F1score()[1]
iou = acc_total.IntersectionOverUnion()[1]
print('epoch %d, train f1 %.3f, iou: %.3f' % (epoch, f1, iou))
return losses.avg, f1, iou
# test
def vtest_epoch(model, criterion, dataloader, device, epoch, classes):
model.eval()
acc_total = SegmentationMetric(numClass=classes, device=device)
losses = AverageMeter()
num = len(dataloader)
pbar = tqdm(range(num), disable=False)
with torch.no_grad():
for idx, (x, y_true) in enumerate(dataloader):
x = x.to(device, non_blocking =True)
y_true = y_true.to(device, non_blocking =True).unsqueeze(1) # n c h w
ypred = model.forward(x)
ypred = torch.sigmoid(ypred)
loss = criterion(ypred, y_true.float())
# ypred = ypred.argmax(axis=1)
ypred = (ypred>0.5)
acc_total.addBatch(ypred, y_true)
losses.update(loss.item(), x.size(0))
f1 = acc_total.F1score()[1]
iou = acc_total.IntersectionOverUnion()[1]
pbar.set_description(
'Test Epoch:{epoch:4}. Iter:{batch:4}|{iter:4}. Loss {loss:.3f}. F1 {f1:.3f}, IOU: {iou:.3f}'.format(
epoch=epoch, batch=idx, iter=num, loss=losses.avg, f1=f1, iou=iou))
pbar.update()
pbar.close()
f1 = acc_total.F1score()[1]
iou = acc_total.IntersectionOverUnion()[1]
print('epoch %d, train f1 %.3f, iou: %.3f' % (epoch, f1, iou))
return losses.avg, f1, iou
if __name__=="__main__":
main()