|
a |
|
b/CaraNet/eval_blood.py |
|
|
1 |
import os |
|
|
2 |
import torch |
|
|
3 |
import torch.nn as nn |
|
|
4 |
import torch.nn.functional as F |
|
|
5 |
import torchvision.transforms as transforms |
|
|
6 |
from PIL import Image |
|
|
7 |
import numpy as np |
|
|
8 |
import pandas |
|
|
9 |
import cv2 |
|
|
10 |
|
|
|
11 |
class test_dataset: |
|
|
12 |
def __init__(self, image_root, gt_root): |
|
|
13 |
self.images = [image_root + f for f in os.listdir(image_root) if f.endswith('.jpg') or f.endswith('.png')] |
|
|
14 |
self.gts = [gt_root + f for f in os.listdir(gt_root) if f.endswith('.jpg') or f.endswith('.png')] |
|
|
15 |
self.images = sorted(self.images) |
|
|
16 |
self.gts = sorted(self.gts) |
|
|
17 |
self.transform = transforms.ToTensor() |
|
|
18 |
self.gt_transform = transforms.ToTensor() |
|
|
19 |
self.size = len(self.images) |
|
|
20 |
self.index = 0 |
|
|
21 |
|
|
|
22 |
def load_data(self): |
|
|
23 |
image = self.rgb_loader(self.images[self.index]) |
|
|
24 |
image = self.transform(image).unsqueeze(0) |
|
|
25 |
gt = self.binary_loader(self.gts[self.index]) |
|
|
26 |
name = self.images[self.index].split('/')[-1] |
|
|
27 |
if name.endswith('.jpg'): |
|
|
28 |
name = name.split('.jpg')[0] + '.png' |
|
|
29 |
self.index += 1 |
|
|
30 |
return image, gt, name |
|
|
31 |
|
|
|
32 |
def rgb_loader(self, path): |
|
|
33 |
with open(path, 'rb') as f: |
|
|
34 |
img = Image.open(f) |
|
|
35 |
return img.convert('RGB') |
|
|
36 |
|
|
|
37 |
def binary_loader(self, path): |
|
|
38 |
with open(path, 'rb') as f: |
|
|
39 |
img = Image.open(f) |
|
|
40 |
return img.convert('L') |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
if __name__ == '__main__': |
|
|
44 |
for _data_name in ['test']: |
|
|
45 |
########################################################### |
|
|
46 |
##### image_root : your model inference results' path ### |
|
|
47 |
##### gt_root : gt files' path ### |
|
|
48 |
########################################################### |
|
|
49 |
|
|
|
50 |
# image_root = 'D:/HarDNet-MSEG-master/results/HarDMSEG/Kvasir_SEG_Validation_120/' |
|
|
51 |
# gt_root = 'D:/HarDNet-MSEG-master/Kvasir_SEG_Validation_120/mask/' |
|
|
52 |
|
|
|
53 |
image_root = '/home/data/spleen_blood/CaraNet/results/CaraNet/test/' |
|
|
54 |
gt_root = '/home/data/spleen_blood/CaraNet/TestDataset/test/masks/' |
|
|
55 |
|
|
|
56 |
test_loader = test_dataset(image_root, gt_root) |
|
|
57 |
b=0.0 |
|
|
58 |
|
|
|
59 |
for i in range(test_loader.size): |
|
|
60 |
image, gt, name = test_loader.load_data() |
|
|
61 |
gt = np.asarray(gt, np.float32) |
|
|
62 |
gt /= (gt.max() + 1e-8) |
|
|
63 |
image = image |
|
|
64 |
input = image[0,1,:,:] |
|
|
65 |
input = np.array(input) |
|
|
66 |
|
|
|
67 |
target = np.array(gt) |
|
|
68 |
N = gt.shape |
|
|
69 |
smooth = 1 |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
input_flat = np.reshape(input,(-1)) |
|
|
73 |
target_flat = np.reshape(target,(-1)) |
|
|
74 |
|
|
|
75 |
intersection = (input_flat*target_flat) |
|
|
76 |
#intersection = (iflat * tflat).sum() |
|
|
77 |
#A_sum = input_flat.sum() |
|
|
78 |
#B_sum = target_flat.sum() |
|
|
79 |
#intersection = (input_flat * target_flat).sum() |
|
|
80 |
#a= ((2. * intersection + smooth) / (A_sum + B_sum + smooth) ) |
|
|
81 |
#loss = 2 * (intersection.sum(1) + smooth) / (input_flat.sum(1) + target_flat.sum(1) + smooth) |
|
|
82 |
loss = (2 * intersection.sum() + smooth) / (input_flat.sum() + target_flat.sum() + smooth) |
|
|
83 |
#loss = loss.sum() / N[1] |
|
|
84 |
a = '{:.4f}'.format(loss) |
|
|
85 |
a = float(a) |
|
|
86 |
|
|
|
87 |
b = b + a |
|
|
88 |
print( i, a) |
|
|
89 |
|
|
|
90 |
print(b/test_loader.size) |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
|