-
Notifications
You must be signed in to change notification settings - Fork 1
/
paanet_test.py
370 lines (315 loc) · 12.2 KB
/
paanet_test.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
from tqdm import tqdm
import torch.nn.functional as F
from operator import add
from utils.utils import clip_gradient, adjust_lr, AvgMeter
from model_bio import EncoderBlock
def get_n_params(model):
pp=0
for p in list(model.parameters()):
nn=1
for s in list(p.size()):
nn = nn*s
pp += nn
return pp
import math
import os
import numpy as np
from glob import glob
import torch
from torch.utils.data import Dataset, DataLoader
from PIL import Image
from lib.utils_hrnet import shuffling, make_channel_first, make_channel_last, create_dir, epoch_time, print_and_save
def load_names(path, file_path):
f = open(file_path, "r")
data = f.read().split("\n")[:-1]
images = [os.path.join(path,"images", name) + ".jpg" for name in data]
masks = [os.path.join(path,"masks", name) + ".jpg" for name in data]
return images, masks
def load_data(path):
train_names_path = f"{path}/train.txt"
valid_names_path = f"{path}/val.txt"
train_x, train_y = load_names(path, train_names_path)
valid_x, valid_y = load_names(path, valid_names_path)
return (train_x, train_y), (valid_x, valid_y)
class KvasirDataset(Dataset):
""" Dataset for the Kvasir-SEG dataset. """
def __init__(self, images_path, masks_path, size):
"""
Arguments:
images_path: A list of path of the images.
masks_path: A list of path of the masks.
"""
self.images_path = images_path
self.masks_path = masks_path
self.size = size
self.n_samples = len(images_path)
def __getitem__(self, index):
""" Reading image and mask. """
image = Image.open(self.images_path[index])
mask = Image.open(self.masks_path[index])
image = image.resize((256,256))
mask = mask.resize((256,256))
mask = mask.convert('L')
""" Resizing. """
#image = cv2.resize(image, self.size)
#mask = cv2.resize(mask, self.size)
""" Proper channel formatting. """
image = np.transpose(image, (2, 0, 1))
mask = np.expand_dims(mask, axis=0)
""" Normalization. """
image = image/255.0
mask = mask/255.0
""" Changing datatype to float32. """
image = image.astype(np.float32)
mask = mask.astype(np.float32)
""" Changing numpy to tensor. """
image = torch.from_numpy(image)
mask = torch.from_numpy(mask)
return image, mask
def __len__(self):
return self.n_samples
def structure_loss(pred, mask):
weit = 1 + 5*torch.abs(F.avg_pool2d(mask, kernel_size=31, stride=1, padding=15) - mask)
wbce = F.binary_cross_entropy_with_logits(pred, mask, reduce='none')
wbce = (weit*wbce).sum(dim=(2, 3)) / weit.sum(dim=(2, 3))
pred = torch.sigmoid(pred)
inter = ((pred * mask)*weit).sum(dim=(2, 3))
union = ((pred + mask)*weit).sum(dim=(2, 3))
wiou = 1 - (inter + 1)/(union - inter+1)
return (wbce + wiou).mean()
def train(train_loader, model, optimizer, epoch,device):
model.train()
# ---- multi-scale training ----
size_rates = [0.75, 1, 1.25]
loss_record2, loss_record3, loss_record4, loss_record5 = AvgMeter(), AvgMeter(), AvgMeter(), AvgMeter()
for i, pack in enumerate(train_loader, start=1):
optimizer.zero_grad()
# ---- data prepare ----
images, gts = pack
images = images.to(device)
gts = gts.to(device)
# ---- rescale ----
trainsize = 256
# ---- forward ----
lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2 = model(images)
# ---- loss function ----
loss5 = structure_loss(lateral_map_5, gts)
loss4 = structure_loss(lateral_map_4, gts)
loss3 = structure_loss(lateral_map_3, gts)
loss2 = structure_loss(lateral_map_2, gts)
loss = loss2 + loss3 + loss4 + loss5 # TODO: try different weights for loss
# ---- backward ----
loss.backward()
clip_gradient(optimizer, 0.5)
optimizer.step()
# ---- recording loss ----
# ---- train visualization ----
save_path = 'files/'
os.makedirs(save_path, exist_ok=True)
if (epoch+1) % 10 == 0:
torch.save(model.state_dict(), save_path + 'PraNet-%d.pth' % epoch)
print('[Saving Snapshot:]', save_path + 'PraNet-%d.pth'% epoch)
def evaluate(model, loader, loss_fn, device):
epoch_loss = 0
model.eval()
with torch.no_grad():
for i, (x, y) in enumerate(loader):
x = x.to(device)
y = y.to(device)
y_pred,_,_,_ = model(x)
loss = loss_fn(y_pred, y)
epoch_loss += loss.item()
epoch_loss = epoch_loss/len(loader)
return epoch_loss
import os
import time
import random
import numpy as np
from glob import glob
import torch
from torch.utils.data import DataLoader
import torch.nn as nn
from loss import DiceLoss, DiceBCELoss
model = EncoderBlock()
#train_main(model)
from sklearn.metrics import f1_score, recall_score, precision_score,jaccard_score
import statistics
import time
def single_dice_coef(y_true, y_pred_bin):
# shape of y_true and y_pred_bin: (height, width)
intersection = np.sum(y_true * y_pred_bin)
if (np.sum(y_true)==0) and (np.sum(y_pred_bin)==0):
return 1
return (2*intersection) / (np.sum(y_true) + np.sum(y_pred_bin))
def mean_dice_coef(y_true, y_pred_bin):
# shape of y_true and y_pred_bin: (n_samples, height, width, n_channels)
batch_size = y_true.shape[0]
channel_num = y_true.shape[-1]
mean_dice_channel = 0.
dice_all = []
for i in range(batch_size):
for j in range(channel_num):
channel_dice = single_dice_coef(y_true[i, :, :, j], y_pred_bin[i, :, :, j])
dice_all.append(channel_dice)
mean_dice_channel += channel_dice/(channel_num*batch_size)
std = statistics.stdev(dice_all)
return mean_dice_channel,std,np.asarray(dice_all)
def calculate_metrics(y_true, y_pred):
y_true = y_true.cpu().numpy()
y_pred = y_pred.cpu().numpy()
y_pred = y_pred > 0.5
y_pred = y_pred.astype(np.uint8)
y_true = y_true > 0.5
y_true = y_true.astype(np.uint8)
## Score
score_f1 = f1_score(y_true.reshape(-1), y_pred.reshape(-1), average='binary')
score_recall = recall_score(y_true.reshape(-1), y_pred.reshape(-1), average='binary')
score_precision = precision_score(y_true.reshape(-1), y_pred.reshape(-1), average='binary', zero_division=0)
score_iou = jaccard_score(y_true.reshape(-1),y_pred.reshape(-1))
return [score_f1, score_recall, score_precision,score_iou]
def mask_parse(mask):
mask = np.squeeze(mask)
mask = [mask, mask, mask]
mask = np.transpose(mask, (1, 2, 0))
return mask
def main_test():
""" Seeding """
create_dir("results")
#test_img_list = glob("../../data/test_img/images/*.jpg")
#test_mask_list = glob("../../data/test_img_m/masks/*.jpg")
#test_img_list = glob("../../data/cvc_data/test/image/*.tif")
#test_mask_list = glob("../../data/cvc_data/test/mask/*.tif")
#test_img_list = glob("../../data/kdsb/test/images/*.jpg")
#test_mask_list = glob("../../data/kdsb/test/masks/*.jpg")
test_img_list = glob('data/kins/test/image/*.jpg')
test_mask_list = glob('data/kins/test/mask/*.jpg')
test_x, test_y = test_img_list,test_mask_list
""" Hyperparameters """
size = (256, 256)
checkpoint_path = "files/PAANet-49.pth"
#checkpoint_path = "files/checkpoint.pth"
""" Directories """
create_dir("results")
""" Load the checkpoint """
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = EncoderBlock()
print('no of params:',get_n_params(model))
model = model.to(device)
model.load_state_dict(torch.load(checkpoint_path, map_location=device))
model.eval()
""" Testing """
pred_all = []
mask_all = []
dice_all = []
""" Testing """
metrics_score = [0.0, 0.0, 0.0, 0.0]
dice_all_images,iou_all,recall_all,precision_all = [],[],[],[]
total_time = 0
images_all = []
for i, (x, y) in tqdm(enumerate(zip(test_x, test_y)), total=len(test_x)):
name = y.split("/")[-1].split(".")[0]
## Image
image = Image.open(x)
image = image.resize((256,256))
ori_img = image
image = np.transpose(image, (2, 0, 1))
image = image/255.0
image = np.expand_dims(image, axis=0)
image = image.astype(np.float32)
images_all.append(image)
image = torch.from_numpy(image)
image = image.to(device)
## Mask
mask = Image.open(y)
mask = mask.resize((256,256))
mask = mask.convert('L')
ori_mask = mask
mask = np.expand_dims(mask, axis=0)
mask = mask/255.0
mask = np.expand_dims(mask, axis=0)
mask = mask.astype(np.float32)
mask = torch.from_numpy(mask)
mask = mask.to(device)
#print(mask.size())
with torch.no_grad():
start_time = time.time()
#pred_y = model(image)
#pred_y = torch.sigmoid(pred_y)
#print(pred_y.size())
res5, res4, res3, res2,_,_,_,_,_,_ = model(image)
res = res5
#res = F.upsample(res, size=256, mode='bilinear', align_corners=False)
#res = torch.sigmoid(res)
pred_y = res
ending_time = time.time() - start_time
total_time = total_time - ending_time
score = calculate_metrics(mask, pred_y)
score = calculate_metrics(mask, pred_y)
dice_all.append(score[0])
dice_image = score[0]
if math.isnan(dice_image):
dice_image = 0
iou_image = score[3]
if math.isnan(iou_image):
iou_image = 0
recall_image = score[1]
if math.isnan(recall_image):
recall_image= 0
precision_image = score[2]
if math.isnan(precision_image):
precision_image = 0
dice_all_images.append(dice_image)
iou_all.append(iou_image)
recall_all.append(recall_image)
precision_all.append(precision_image)
metrics_score = list(map(add, metrics_score, score))
## Mask
pred_y = pred_y[0].cpu().numpy()
pred_y = np.squeeze(pred_y, axis=0)
pred_y = pred_y > 0.5
pred_y = pred_y * 255
# pred_y = np.transpose(pred_y, (1, 0))
pred_y = np.array(pred_y, dtype=np.uint8)
pred_all.append(pred_y)
mask_all.append(mask)
ori_img = ori_img
ori_mask = mask_parse(ori_mask)
pred_y = mask_parse(pred_y)
sep_line = np.ones((size[0], 10, 3)) * 255
tmp = [
ori_img, sep_line,
ori_mask, sep_line,
pred_y, sep_line,
]
cat_images = np.concatenate(tmp, axis=1)
#cv2.imwrite(f"results/{name}.png", cat_images)
pred_all = np.asarray(pred_all)
print(len(mask_all),mask_all[0].shape)
dice_all = np.asarray(dice_all)
new_masks = []
for m in mask_all:
print(m.shape)
m = np.asarray(m.cpu())
new_masks.append(m)
new_masks = np.asarray(new_masks)
images_all = np.asarray(images_all)
np.save('kins_images.npy',images_all)
np.save('kins_gt.npy',new_masks)
np.save('kins_pred.npy',pred_all)
pred_all = np.expand_dims(pred_all,axis=-1)
_,std,_ = mean_dice_coef(pred_all,new_masks)
print(dice_all)
np.save('kvasir_paanet_dice.npy',dice_all)
std_iou= statistics.stdev(iou_all)
std_recall = statistics.stdev(recall_all)
std_precision = statistics.stdev(precision_all)
std = statistics.stdev(dice_all_images)
f1 = metrics_score[0]/len(test_x)
recall = metrics_score[1]/len(test_x)
precision = metrics_score[2]/len(test_x)
iou = metrics_score[3]/len(test_x)
FPS = len(test_x) / total_time
print(f"F1: {f1:1.4f} - Recall: {recall:1.4f} - Precision: {precision:1.4f} - IOU: {iou:1.4f} - FPS:{FPS:1.4f} - std:{std:1.4f}")
print(f"F1: {f1:1.4f} - Recall: {recall:1.4f} - Precision: {precision:1.4f} - IOU:{iou:1.4f}")
print(f"std of iou:{std_iou:1.4f} - Std of recall: {std_recall:1.4f} - std of precision: {std_precision:1.4f}")
main_test()