forked from liu3xing3long/CSNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
executable file
·191 lines (157 loc) · 5.94 KB
/
predict.py
File metadata and controls
executable file
·191 lines (157 loc) · 5.94 KB
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
import torch
from torchvision import transforms
from PIL import Image, ImageOps
import numpy as np
import scipy.misc as misc
import os
import glob
from utils.misc import thresh_OTSU, ReScaleSize, Crop
from utils.model_eval import eval
DATABASE = './DRIVE/'
#
args = {
'root' : './dataset/' + DATABASE,
'test_path': './dataset/' + DATABASE + 'test/',
'pred_path': 'assets/' + 'DRIVE/',
'img_size' : 512
}
if not os.path.exists(args['pred_path']):
os.makedirs(args['pred_path'])
def rescale(img):
w, h = img.size
min_len = min(w, h)
new_w, new_h = min_len, min_len
scale_w = (w - new_w) // 2
scale_h = (h - new_h) // 2
box = (scale_w, scale_h, scale_w + new_w, scale_h + new_h)
img = img.crop(box)
return img
def ReScaleSize_DRIVE(image, re_size=512):
w, h = image.size
min_len = min(w, h)
new_w, new_h = min_len, min_len
scale_w = (w - new_w) // 2
scale_h = (h - new_h) // 2
box = (scale_w, scale_h, scale_w + new_w, scale_h + new_h)
image = image.crop(box)
image = image.resize((re_size, re_size))
return image # , origin_w, origin_h
def ReScaleSize_STARE(image, re_size=512):
w, h = image.size
max_len = max(w, h)
new_w, new_h = max_len, max_len
delta_w = new_w - w
delta_h = new_h - h
padding = (delta_w // 2, delta_h // 2, delta_w - (delta_w // 2), delta_h - (delta_h // 2))
image = ImageOps.expand(image, padding, fill=0)
# origin_w, origin_h = w, h
image = image.resize((re_size, re_size))
return image # , origin_w, origin_h
def load_nerve():
test_images = []
test_labels = []
for file in glob.glob(os.path.join(args['test_path'], 'orig', '*.tif')):
basename = os.path.basename(file)
file_name = basename[:-4]
image_name = os.path.join(args['test_path'], 'orig', basename)
label_name = os.path.join(args['test_path'], 'mask2', file_name + '_centerline_overlay.tif')
test_images.append(image_name)
test_labels.append(label_name)
return test_images, test_labels
def load_drive():
test_images = []
test_labels = []
for file in glob.glob(os.path.join(args['test_path'], 'images', '*.tif')):
basename = os.path.basename(file)
file_name = basename[:3]
image_name = os.path.join(args['test_path'], 'images', basename)
label_name = os.path.join(args['test_path'], '1st_manual', file_name + 'manual1.gif')
test_images.append(image_name)
test_labels.append(label_name)
return test_images, test_labels
def load_stare():
test_images = []
test_labels = []
for file in glob.glob(os.path.join(args['test_path'], 'images', '*.ppm')):
basename = os.path.basename(file)
file_name = basename[:-4]
image_name = os.path.join(args['test_path'], 'images', basename)
label_name = os.path.join(args['test_path'], 'labels-ah', file_name + '.ah.ppm')
test_images.append(image_name)
test_labels.append(label_name)
return test_images, test_labels
def load_padova1():
test_images = []
test_labels = []
for file in glob.glob(os.path.join(args['test_path'], 'images', '*.tif')):
basename = os.path.basename(file)
file_name = basename[:-4]
image_name = os.path.join(args['test_path'], 'images', basename)
label_name = os.path.join(args['test_path'], 'label2', file_name + '_centerline_overlay.tif')
test_images.append(image_name)
test_labels.append(label_name)
return test_images, test_labels
def load_octa():
test_images = []
test_labels = []
for file in glob.glob(os.path.join(args['test_path'], 'images', '*.png')):
basename = os.path.basename(file)
file_name = basename[:-4]
image_name = os.path.join(args['test_path'], 'images', basename)
label_name = os.path.join(args['test_path'], 'label', file_name + '_nerve_ann.tif')
test_images.append(image_name)
test_labels.append(label_name)
return test_images, test_labels
def load_net():
net = torch.load('./checkpoint/xxxx.pkl')
return net
def save_prediction(pred, filename=''):
save_path = args['pred_path'] + 'pred/'
if not os.path.exists(save_path):
os.makedirs(save_path)
print("Make dirs success!")
mask = pred.data.cpu().numpy() * 255
mask = np.transpose(np.squeeze(mask, axis=0), [1, 2, 0])
mask = np.squeeze(mask, axis=-1)
misc.imsave(save_path + filename + '.png', mask)
def predict():
net = load_net()
# images, labels = load_nerve()
images, labels = load_drive()
# images, labels = load_stare()
# images, labels = load_padova1()
# images, labels = load_octa()
transform = transforms.Compose([
transforms.ToTensor()
])
with torch.no_grad():
net.eval()
for i in range(len(images)):
print(images[i])
name_list = images[i].split('/')
index = name_list[-1][:-4]
image = Image.open(images[i])
# image=image.convert("RGB")
label = Image.open(labels[i])
image, label = center_crop(image, label)
# for other retinal vessel
# image = rescale(image)
# label = rescale(label)
# image = ReScaleSize_STARE(image, re_size=args['img_size'])
# label = ReScaleSize_DRIVE(label, re_size=args['img_size'])
# for OCTA
# image = Crop(image)
# image = ReScaleSize(image)
# label = Crop(label)
# label = ReScaleSize(label)
# label = label.resize((args['img_size'], args['img_size']))
# if cuda
image = transform(image).cuda()
# image = transform(image)
image = image.unsqueeze(0)
output = net(image)
save_prediction(output, filename=index + '_pred')
print("output saving successfully")
if __name__ == '__main__':
predict()
thresh_OTSU(args['pred_path'] + 'pred/')