-
Notifications
You must be signed in to change notification settings - Fork 1
/
strt2ftprnt_ResUnet_train.py
471 lines (400 loc) · 16.3 KB
/
strt2ftprnt_ResUnet_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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import torch
import torch.utils.data as data
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import os
import numpy as np
import pandas as pd
import glob
import cv2
from tqdm import tqdm, trange
import matplotlib.pyplot as plt
img_size = 256
X_img_path = "D:/AI in Urban Design/DL UD dir/Filtered_X"
Y_img_path = "D:/AI in Urban Design/DL UD dir/Filtered_Y"
def make_data(X_img_path, Y_img_path, img_size):
X_data = []
Y_data = []
X_img_count = 0
Y_img_count = 0
for i in tqdm(os.listdir(X_img_path), ncols=100, disable=False):
path = os.path.join(X_img_path, i)
X = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
X = cv2.resize(X, (img_size, img_size))
X = np.array(X)
X = X.reshape((1, img_size, img_size))
X = X / 255
X = 1 - X
X_data.append(X)
X_img_count += 1
for i in tqdm(os.listdir(Y_img_path), ncols=100, disable=False):
path = os.path.join(Y_img_path, i)
Y = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
Y = cv2.resize(Y, (img_size, img_size))
Y = np.array(Y)
Y = Y.reshape((1, img_size, img_size))
Y = Y / 255
Y = 1 - Y
Y_data.append(Y)
Y_img_count += 1
print('X Image_count:' + str(X_img_count))
print('Y Image_count:' + str(Y_img_count))
return X_data, Y_data
class segmentationDataSet(data.Dataset):
def __init__(self, X_img_path, Y_img_path, img_size):
self.inputs_path = X_img_path
self.targets_path = Y_img_path
self.img_size = img_size
self.inputs_dtype = torch.float32
self.targets_dtype = torch.float32
self.inputs, self.targets = make_data(self.inputs_path, self.targets_path, self.img_size)
def __len__(self):
return len(self.inputs)
def __getitem__(self, index: int):
# Select the sample
input_ID = self.inputs[index]
target_ID = self.targets[index]
# Load input and target
x, y = input_ID, target_ID
# Typecasting
x, y = torch.from_numpy(x).type(self.inputs_dtype), torch.from_numpy(y).type(self.targets_dtype)
return x, y
batch_S = 2
test_split = 0.1
ts = 0.1 / 0.9
dataset = segmentationDataSet(X_img_path, Y_img_path, img_size)
dataset_size = len(dataset)
test_size = int(test_split * dataset_size)
train_size = dataset_size - test_size
train_dataset, test_dataset = data.random_split(dataset, [train_size, test_size])
trdataset_size = len(train_dataset)
val_size = int(ts * trdataset_size)
training_size = trdataset_size - val_size
training_dataset, val_dataset = data.random_split(train_dataset, [training_size, val_size])
training_dataloader = data.DataLoader(dataset=training_dataset, batch_size = batch_S, shuffle=True)
x, y = next(iter(training_dataloader))
print(f'x = shape: {x.shape}; type: {x.dtype}')
print(f'x = min: {x.min()}; max: {x.max()}')
print(f'y = shape: {y.shape}; type: {y.dtype}')
print(f'y = min: {y.min()}; max: {y.max()}')
val_dataloader = data.DataLoader(dataset=val_dataset, batch_size = batch_S, shuffle=True)
x, y = next(iter(val_dataloader))
print(f'x = shape: {x.shape}; type: {x.dtype}')
print(f'x = min: {x.min()}; max: {x.max()}')
print(f'y = shape: {y.shape}; type: {y.dtype}')
print(f'y = min: {y.min()}; max: {y.max()}')
# Unet architecture
def initial_block(in_c, out_c):
conv = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=3, padding=1, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c),
nn.Conv2d(out_c, out_c, kernel_size=3, padding=1, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c),
)
return conv
def enc_block(in_c, out_c):
conv = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=3, padding=1, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c),
nn.Conv2d(out_c, out_c, kernel_size=3, padding=1, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c)
)
return conv
def mid_block(out_c):
conv = nn.Sequential(
nn.Conv2d(out_c, out_c, kernel_size=7, padding=3, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c),
nn.Conv2d(out_c, out_c, kernel_size=7, padding=3, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c)
)
return conv
def dec_block(out_c):
conv = nn.Sequential(
nn.BatchNorm2d(out_c),
nn.Conv2d(out_c, out_c, kernel_size=7, padding=3, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c)
)
return conv
def end_block(in_c, out_c):
conv = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=7, padding=3, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c),
nn.Conv2d(out_c, out_c, kernel_size=7, padding=3, stride=1, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_c),
)
return conv
def sigmoid_block(out_c):
conv = nn.Sequential(
nn.Conv2d(out_c, 1, kernel_size=1, padding=0, stride=1, bias=True),
nn.Sigmoid()
)
return conv
def conv_res(in_c, out_c):
resconv = nn.Conv2d(in_c, out_c, kernel_size=1, padding=0, stride=1, bias=True)
return resconv
class Sigmoid_Unet(nn.Module):
def __init__(self):
super(Sigmoid_Unet, self).__init__()
self.initial = initial_block(1, 32)
self.encblock1 = enc_block(32, 64)
self.encblock2 = enc_block(64, 128)
self.encblock3 = enc_block(128, 256)
self.mid = mid_block(256)
self.decblock1 = dec_block(128)
self.decblock2 = dec_block(64)
self.decblock3 = dec_block(32)
self.end = end_block(16, 16)
self.sigmoid = sigmoid_block(16)
self.transpose1 = nn.ConvTranspose2d(512, 128, kernel_size=2, stride=2, bias=False)
self.transpose2 = nn.ConvTranspose2d(256, 64, kernel_size=2, stride=2, bias=False)
self.transpose3 = nn.ConvTranspose2d(128, 32, kernel_size=2, stride=2, bias=False)
self.transpose4 = nn.ConvTranspose2d(64, 16, kernel_size=2, stride=2, bias=False)
self.res1 = conv_res(1, 32)
self.res2 = conv_res(32, 64)
self.res3 = conv_res(64, 128)
self.res4 = conv_res(128, 256)
self.res5 = conv_res(256, 256)
self.res6 = conv_res(128, 128)
self.res7 = conv_res(64, 64)
self.res8 = conv_res(32, 32)
self.res9 = conv_res(16, 16)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
def forward(self, image):
x1 = self.initial(image)
x1res = self.relu(x1 + self.res1(image))
x2 = self.maxpool(x1res)
x3 = self.encblock1(x2)
x2res = self.relu(x3 + self.res2(x2))
x4 = self.maxpool(x2res)
x5 = self.encblock2(x4)
x3res = self.relu(x5 + self.res3(x4))
x6 = self.maxpool(x3res)
x7 = self.encblock3(x6)
x4res = self.relu(x7 + self.res4(x6))
x8 = self.maxpool(x4res)
x9 = self.mid(x8)
x5res = self.relu(x9 + self.res5(x8))
x10 = self.transpose1(torch.cat([x5res, x8], 1))
x11 = self.decblock1(x10)
x6res = self.relu(x11 + self.res6(x10))
x12 = self.transpose2(torch.cat([x6res, x6], 1))
x13 = self.decblock2(x12)
x7res = self.relu(x13 + self.res7(x12))
x14 = self.transpose3(torch.cat([x7res, x4], 1))
x15 = self.decblock3(x14)
x8res = self.relu(x15 + self.res8(x14))
x16 = self.transpose4(torch.cat([x8res, x2], 1))
x17 = self.end(x16)
x9res = self.relu(x17 + self.res9(x16))
out = self.sigmoid(x9res)
return out
# hyperparameters
num_epochs = 100
learning_rate = 0.002
lr_decay = 0.1
# Set device
def get_device():
if torch.cuda.is_available():
return torch.device("cuda")
else:
return torch.device("cpu")
device = get_device()
print(device)
def dice_metric(inputs, target):
intersection = 2.0 * (target * inputs).sum()
union = target.sum() + inputs.sum()
if target.sum() == 0 and inputs.sum() == 0:
return 1.0
return intersection / union
def diceCoeff(pred, gt, smooth=1e-5):
N = gt.size(0)
pred_flat = pred.view(N, -1)
gt_flat = gt.view(N, -1)
intersection = (pred_flat * gt_flat).sum(1)
unionset = pred_flat.sum(1) + gt_flat.sum(1)
loss = (2 * intersection + smooth) / (unionset + smooth)
return loss.sum() / N
def diceCoeffv2(pred, gt, eps=1e-5):
N = gt.size(0)
pred_flat = pred.view(N, -1)
gt_flat = gt.view(N, -1)
tp = torch.sum(gt_flat * pred_flat, dim=1)
fp = torch.sum(pred_flat, dim=1) - tp
fn = torch.sum(gt_flat, dim=1) - tp
loss = (2 * tp + eps) / (2 * tp + fp + fn + eps)
return loss.sum() / N
class DiceLoss(nn.Module):
def __init__(self):
super(DiceLoss, self).__init__()
def forward(self, y_pr, y_gt):
return 1 - diceCoeffv2(y_pr, y_gt)
# model
model = Sigmoid_Unet()
model.to(device)
# criterion
criterion = DiceLoss()
# optimizer
optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay = 1e-5)
# Scheduler
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=10,
threshold=0.0001, threshold_mode='rel', cooldown=0,
min_lr=0, eps=1e-08, verbose=False)
# Training
class Trainer:
def __init__(self,
model: torch.nn.Module,
device: torch.device,
criterion: torch.nn.Module,
optimizer: torch.optim.Optimizer,
training_DataLoader: torch.utils.data.DataLoader,
validation_DataLoader: torch.utils.data.DataLoader,
lr_scheduler: torch.optim.lr_scheduler,
epochs: int,
epoch: int = 0,
batch: int = 0
):
self.model = model
self.criterion = criterion
self.optimizer = optimizer
self.lr_scheduler = lr_scheduler
self.training_DataLoader = training_DataLoader
self.validation_DataLoader = validation_DataLoader
self.device = device
self.epochs = epochs
self.epoch = epoch
self.batch = batch
self.training_loss = []
self.validation_loss = []
self.training_acc = []
self.validation_acc = []
self.learning_rate = []
def run_trainer(self):
progressbar = trange(self.epochs, desc='Progress')
for i in progressbar:
"""Epoch counter"""
self.epoch += 1 # epoch counter
"""Training block"""
self._train()
"""Validation block"""
if self.validation_DataLoader is not None:
self._validate()
"""Learning rate scheduler block"""
if self.lr_scheduler is not None:
if self.validation_DataLoader is not None and self.lr_scheduler.__class__.__name__ == 'ReduceLROnPlateau':
self.lr_scheduler.step(self.validation_loss[i]) # learning rate scheduler step with validation loss
else:
self.lr_scheduler.step() # learning rate scheduler step
return self.training_loss, self.validation_loss, self.training_acc, self.validation_acc, self.learning_rate
def _train(self):
self.model.train() # train mode
train_losses = []
train_accuracy = []
batch_iter = tqdm(enumerate(self.training_DataLoader), 'Training', total=len(self.training_DataLoader),
leave=False)
for i, (x, y) in batch_iter:
if i == 0:
self.batch += 1 # batch counter
input, target = x.to(self.device), y.to(self.device) # send to device (GPU or CPU)
self.optimizer.zero_grad() # zerograd the parameters
out = self.model(input) # one forward pass
# Saving training output images
img1 = torch.unsqueeze(out[0, 0, :, :], 2)
img1 = img1 * 255
img1 = img1.cpu()
np_img1 = img1.detach().numpy()
cv2.imwrite('D:/AI in Urban Design/DL UD dir/strt2ftprnt_trainOUT/'
+ 'train' + str(self.epoch) + '-' + str(self.batch) + '-' + str(i+1) + '.jpeg',
np_img1)
acc = dice_metric(out, target) # calculate accuracy
acc_value = acc.item()
train_accuracy.append(acc_value)
loss = self.criterion(out, target) # calculate loss
loss_value = loss.item()
train_losses.append(loss_value)
loss.backward() # one backward pass
self.optimizer.step() # update the parameters
batch_iter.set_description(f'Training: (loss {loss_value:.4f}) (acc {acc_value:.4f})') # update progressbar
self.training_acc.append(np.mean(train_accuracy))
self.training_loss.append(np.mean(train_losses))
self.learning_rate.append(self.optimizer.param_groups[0]['lr'])
batch_iter.close()
def _validate(self):
self.model.eval() # evaluation mode
val_losses = []
val_accuracy = []
batch_iter = tqdm(enumerate(self.validation_DataLoader), 'Validation', total=len(self.validation_DataLoader),
leave=False)
for i, (x, y) in batch_iter:
input, target = x.to(self.device), y.to(self.device) # send to device (GPU or CPU)
with torch.no_grad():
out = self.model(input)
# Saving validation output images
img1 = torch.unsqueeze(out[0, 0, :, :], 2)
img1 = img1 * 255
img1 = img1.cpu()
np_img1 = img1.detach().numpy()
cv2.imwrite('D:/AI in Urban Design/DL UD dir/strt2ftprnt_valOUT/'
+ 'train' + str(self.epoch) + '-' + str(self.batch) + '-' + str(i + 1) + '.jpeg',
np_img1)
acc = dice_metric(out, target) # calculate accuracy
acc_value = acc.item()
val_accuracy.append(acc_value)
loss = self.criterion(out, target)
loss_value = loss.item()
val_losses.append(loss_value)
batch_iter.set_description(f'Validation: (loss {loss_value:.4f}) (acc {acc_value:.4f})')
self.validation_acc.append(np.mean(val_accuracy))
self.validation_loss.append(np.mean(val_losses))
batch_iter.close()
trainer = Trainer(model=model,
device=device,
criterion=criterion,
optimizer=optimizer,
training_DataLoader = training_dataloader,
validation_DataLoader = val_dataloader,
lr_scheduler=scheduler,
epochs=num_epochs,
epoch=0)
# start training
training_losses, validation_losses, training_acc, validation_acc, lr_rates = trainer.run_trainer()
# save trained model
PATH = 'D:/AI in Urban Design/DL UD dir/DL model/strt2ftprnt_ResUnet.pt'
torch.save(model.state_dict(), PATH)
# Plot loss vs epochs
epoch_list = []
for i in range(len(training_losses)):
epoch_list.append(i + 1)
# Dice loss and Accuracy plot
plt.plot(epoch_list, training_losses, color='r', label="Training Loss")
plt.plot(epoch_list, validation_losses, color='b', label="Validation Loss")
plt.title("Dice Loss")
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
# Accuracy plot
plt.plot(epoch_list, training_acc, color='r', linestyle='--', label="Training Accuracy")
plt.plot(epoch_list, validation_acc, color='b', linestyle='--', label="Validation Accuracy")
plt.title("Dice Metric")
plt.xlabel('Epochs')
plt.ylabel('Acc')
plt.legend()
plt.show()
# lr plot
plt.plot(epoch_list, lr_rates, color='g', label="Learning rate")
plt.title("Learning rate during training")
plt.xlabel('Epochs')
plt.ylabel('Lr')
plt.legend()
plt.show()