-
Notifications
You must be signed in to change notification settings - Fork 12
/
train.py
154 lines (126 loc) · 4.84 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
from __future__ import print_function
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import time
import torch.nn.functional as F
from numpy import *
from measure import SegmentationMetric
from dataset import train_dataset
from MACUNet import MACUNet
from early_stopping import EarlyStopping
from tqdm import tqdm, trange
batch_size = 16
niter = 100
class_num = 6
learning_rate = 0.0001 * 3
beta1 = 0.5
cuda = True
num_workers = 1
size_h = 256
size_w = 256
flip = 0
band = 3
net = MACUNet(band, class_num)
train_path = './dataset/train/'
val_path = './dataset/val/'
test_path = './dataset/test/'
out_file = './checkpoint/' + net.name
num_GPU = 1
index = 100
torch.cuda.set_device(0)
try:
import os
os.makedirs(out_file)
except OSError:
pass
manual_seed = random.randint(1, 10000)
random.seed(manual_seed)
torch.manual_seed(manual_seed)
cudnn.benchmark = True
train_datatset_ = train_dataset(train_path, size_w, size_h, flip, band, batch_size)
val_datatset_ = train_dataset(val_path, size_w, size_h, 0, band)
def weights_init(m):
class_name = m.__class__.__name__
if class_name.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
m.bias.data.fill_(0)
elif class_name.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
try:
os.makedirs(out_file)
os.makedirs(out_file + '/')
except OSError:
pass
if cuda:
net.cuda()
if num_GPU > 1:
net = nn.DataParallel(net)
########### LOSS & OPTIMIZER ##########
criterion = nn.CrossEntropyLoss(ignore_index=255)
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
metric = SegmentationMetric(class_num)
early_stopping = EarlyStopping(patience=10, verbose=True)
if __name__ == '__main__':
start = time.time()
net.train()
lr_adjust = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, 10, eta_min=learning_rate * 0.01, last_epoch=-1)
for epoch in range(1, niter + 1):
for iter_num in trange(2000 // index, desc='train, epoch:%s' % epoch):
train_iter = train_datatset_.data_iter_index(index=index)
for initial_image, semantic_image in train_iter:
# print(initial_image.shape)
initial_image = initial_image.cuda()
semantic_image = semantic_image.cuda()
semantic_image_pred = net(initial_image)
loss = criterion(semantic_image_pred, semantic_image.long())
# print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()
lr_adjust.step()
with torch.no_grad():
net.eval()
val_iter = val_datatset_.data_iter()
for initial_image, semantic_image in tqdm(val_iter, desc='val'):
# print(initial_image.shape)
initial_image = initial_image.cuda()
semantic_image = semantic_image.cuda()
semantic_image_pred = net(initial_image).detach()
semantic_image_pred = F.softmax(semantic_image_pred.squeeze(), dim=0)
semantic_image_pred = semantic_image_pred.argmax(dim=0)
semantic_image = torch.squeeze(semantic_image.cpu(), 0)
semantic_image_pred = torch.squeeze(semantic_image_pred.cpu(), 0)
metric.addBatch(semantic_image_pred, semantic_image)
mIoU = metric.meanIntersectionOverUnion()
print('mIoU: ', mIoU)
metric.reset()
net.train()
early_stopping(1 - mIoU, net, '%s/' % out_file + 'netG.pth')
if early_stopping.early_stop:
break
end = time.time()
print('Program processed ', end - start, 's, ', (end - start)/60, 'min, ', (end - start)/3600, 'h')
test_datatset_ = train_dataset(test_path, time_series=band)
start = time.time()
test_iter = test_datatset_.data_iter()
if os.path.exists('%s/' % out_file + 'netG.pth'):
net.load_state_dict(torch.load('%s/' % out_file + 'netG.pth'))
net.eval()
for initial_image, semantic_image in tqdm(test_iter, desc='test'):
# print(initial_image.shape)
initial_image = initial_image.cuda()
semantic_image = semantic_image.cuda()
# semantic_image_pred = model(initial_image)
semantic_image_pred = net(initial_image).detach()
semantic_image_pred = F.softmax(semantic_image_pred.squeeze(), dim=0)
semantic_image_pred = semantic_image_pred.argmax(dim=0)
semantic_image = torch.squeeze(semantic_image.cpu(), 0)
semantic_image_pred = torch.squeeze(semantic_image_pred.cpu(), 0)
metric.addBatch(semantic_image_pred, semantic_image)
image = semantic_image_pred
end = time.time()
print('Program processed ', end - start, 's, ', (end - start)/60, 'min, ', (end - start)/3600, 'h')
mIoU = metric.meanIntersectionOverUnion()
print('mIoU: ', mIoU)