forked from LeeJunHyun/Image_Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
302 lines (241 loc) · 8.09 KB
/
solver.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import os
import numpy as np
import time
import datetime
import torch
import torchvision
from torch import optim
from torch.autograd import Variable
import torch.nn.functional as F
from evaluation import *
from network import U_Net,R2U_Net,AttU_Net,R2AttU_Net
import csv
class Solver(object):
def __init__(self, config, train_loader, valid_loader, test_loader):
# Data loader
self.train_loader = train_loader
self.valid_loader = valid_loader
self.test_loader = test_loader
# Models
self.unet = None
self.optimizer = None
self.img_ch = config.img_ch
self.output_ch = config.output_ch
self.criterion = torch.nn.BCELoss()
self.augmentation_prob = config.augmentation_prob
# Hyper-parameters
self.lr = config.lr
self.beta1 = config.beta1
self.beta2 = config.beta2
# Training settings
self.num_epochs = config.num_epochs
self.num_epochs_decay = config.num_epochs_decay
self.batch_size = config.batch_size
# Step size
self.log_step = config.log_step
self.val_step = config.val_step
# Path
self.model_path = config.model_path
self.result_path = config.result_path
self.mode = config.mode
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model_type = config.model_type
self.t = config.t
self.build_model()
def build_model(self):
"""Build generator and discriminator."""
if self.model_type =='U_Net':
self.unet = U_Net(img_ch=3,output_ch=1)
elif self.model_type =='R2U_Net':
self.unet = R2U_Net(img_ch=3,output_ch=1,t=self.t)
elif self.model_type =='AttU_Net':
self.unet = AttU_Net(img_ch=3,output_ch=1)
elif self.model_type == 'R2AttU_Net':
self.unet = R2AttU_Net(img_ch=3,output_ch=1,t=self.t)
self.optimizer = optim.Adam(list(self.unet.parameters()),
self.lr, [self.beta1, self.beta2])
self.unet.to(self.device)
# self.print_network(self.unet, self.model_type)
def print_network(self, model, name):
"""Print out the network information."""
num_params = 0
for p in model.parameters():
num_params += p.numel()
print(model)
print(name)
print("The number of parameters: {}".format(num_params))
def to_data(self, x):
"""Convert variable to tensor."""
if torch.cuda.is_available():
x = x.cpu()
return x.data
def update_lr(self, g_lr, d_lr):
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
def reset_grad(self):
"""Zero the gradient buffers."""
self.unet.zero_grad()
def compute_accuracy(self,SR,GT):
SR_flat = SR.view(-1)
GT_flat = GT.view(-1)
acc = GT_flat.data.cpu()==(SR_flat.data.cpu()>0.5)
def tensor2img(self,x):
img = (x[:,0,:,:]>x[:,1,:,:]).float()
img = img*255
return img
def train(self):
"""Train encoder, generator and discriminator."""
#====================================== Training ===========================================#
#===========================================================================================#
unet_path = os.path.join(self.model_path, '%s-%d-%.4f-%d-%.4f.pkl' %(self.model_type,self.num_epochs,self.lr,self.num_epochs_decay,self.augmentation_prob))
# U-Net Train
if os.path.isfile(unet_path):
# Load the pretrained Encoder
self.unet.load_state_dict(torch.load(unet_path))
print('%s is Successfully Loaded from %s'%(self.model_type,unet_path))
else:
# Train for Encoder
lr = self.lr
best_unet_score = 0.
for epoch in range(self.num_epochs):
self.unet.train(True)
epoch_loss = 0
acc = 0. # Accuracy
SE = 0. # Sensitivity (Recall)
SP = 0. # Specificity
PC = 0. # Precision
F1 = 0. # F1 Score
JS = 0. # Jaccard Similarity
DC = 0. # Dice Coefficient
length = 0
for i, (images, GT) in enumerate(self.train_loader):
# GT : Ground Truth
images = images.to(self.device)
GT = GT.to(self.device)
# SR : Segmentation Result
SR = self.unet(images)
SR_probs = F.sigmoid(SR)
SR_flat = SR_probs.view(SR_probs.size(0),-1)
GT_flat = GT.view(GT.size(0),-1)
loss = self.criterion(SR_flat,GT_flat)
epoch_loss += loss.item()
# Backprop + optimize
self.reset_grad()
loss.backward()
self.optimizer.step()
acc += get_accuracy(SR,GT)
SE += get_sensitivity(SR,GT)
SP += get_specificity(SR,GT)
PC += get_precision(SR,GT)
F1 += get_F1(SR,GT)
JS += get_JS(SR,GT)
DC += get_DC(SR,GT)
length += images.size(0)
acc = acc/length
SE = SE/length
SP = SP/length
PC = PC/length
F1 = F1/length
JS = JS/length
DC = DC/length
# Print the log info
print('Epoch [%d/%d], Loss: %.4f, \n[Training] Acc: %.4f, SE: %.4f, SP: %.4f, PC: %.4f, F1: %.4f, JS: %.4f, DC: %.4f' % (
epoch+1, self.num_epochs, \
epoch_loss,\
acc,SE,SP,PC,F1,JS,DC))
# Decay learning rate
if (epoch+1) > (self.num_epochs - self.num_epochs_decay):
lr -= (self.lr / float(self.num_epochs_decay))
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
print ('Decay learning rate to lr: {}.'.format(lr))
#===================================== Validation ====================================#
if epoch % 10 == 0:
self.unet.train(False)
self.unet.eval()
acc = 0. # Accuracy
SE = 0. # Sensitivity (Recall)
SP = 0. # Specificity
PC = 0. # Precision
F1 = 0. # F1 Score
JS = 0. # Jaccard Similarity
DC = 0. # Dice Coefficient
length=0
for i, (images, GT) in enumerate(self.valid_loader):
images = images.to(self.device)
GT = GT.to(self.device)
SR = F.sigmoid(self.unet(images))
acc += get_accuracy(SR,GT)
SE += get_sensitivity(SR,GT)
SP += get_specificity(SR,GT)
PC += get_precision(SR,GT)
F1 += get_F1(SR,GT)
JS += get_JS(SR,GT)
DC += get_DC(SR,GT)
length += images.size(0)
acc = acc/length
SE = SE/length
SP = SP/length
PC = PC/length
F1 = F1/length
JS = JS/length
DC = DC/length
unet_score = JS + DC
print('[Validation] Acc: %.4f, SE: %.4f, SP: %.4f, PC: %.4f, F1: %.4f, JS: %.4f, DC: %.4f'%(acc,SE,SP,PC,F1,JS,DC))
'''
torchvision.utils.save_image(images.data.cpu(),
os.path.join(self.result_path,
'%s_valid_%d_image.png'%(self.model_type,epoch+1)))
torchvision.utils.save_image(SR.data.cpu(),
os.path.join(self.result_path,
'%s_valid_%d_SR.png'%(self.model_type,epoch+1)))
torchvision.utils.save_image(GT.data.cpu(),
os.path.join(self.result_path,
'%s_valid_%d_GT.png'%(self.model_type,epoch+1)))
'''
# Save Best U-Net model
if unet_score > best_unet_score:
best_unet_score = unet_score
best_epoch = epoch
best_unet = self.unet.state_dict()
print('Best %s model score : %.4f'%(self.model_type,best_unet_score))
torch.save(best_unet,unet_path)
#===================================== Test ====================================#
del self.unet
del best_unet
self.build_model()
self.unet.load_state_dict(torch.load(unet_path))
self.unet.train(False)
self.unet.eval()
acc = 0. # Accuracy
SE = 0. # Sensitivity (Recall)
SP = 0. # Specificity
PC = 0. # Precision
F1 = 0. # F1 Score
JS = 0. # Jaccard Similarity
DC = 0. # Dice Coefficient
length=0
for i, (images, GT) in enumerate(self.valid_loader):
images = images.to(self.device)
GT = GT.to(self.device)
SR = F.sigmoid(self.unet(images))
acc += get_accuracy(SR,GT)
SE += get_sensitivity(SR,GT)
SP += get_specificity(SR,GT)
PC += get_precision(SR,GT)
F1 += get_F1(SR,GT)
JS += get_JS(SR,GT)
DC += get_DC(SR,GT)
length += images.size(0)
acc = acc/length
SE = SE/length
SP = SP/length
PC = PC/length
F1 = F1/length
JS = JS/length
DC = DC/length
unet_score = JS + DC
f = open(os.path.join(self.result_path,'result.csv'), 'a', encoding='utf-8', newline='')
wr = csv.writer(f)
wr.writerow([self.model_type,acc,SE,SP,PC,F1,JS,DC,self.lr,best_epoch,self.num_epochs,self.num_epochs_decay,self.augmentation_prob])
f.close()