-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
136 lines (107 loc) · 4.61 KB
/
test.py
File metadata and controls
136 lines (107 loc) · 4.61 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
import os
import cv2
import imageio
import matplotlib.pyplot as plt
import numpy as np
import random
from sklearn.metrics import jaccard_score, accuracy_score,classification_report
from model import *
data_location = ''
test_images_loc = data_location + 'DRIVE/test/images/'
test_label_loc = data_location + 'DRIVE/test/1st_manual/'
test_files = os.listdir(test_images_loc)
test_data = []
test_label = []
desired_size = 592
for i in test_files:
im = cv2.imread(test_images_loc + i)
label = imageio.v2.imread(test_label_loc + i.split('_')[0] + '_manual1.gif')
old_size = im.shape[:2] # old_size is in (height, width) format
delta_w = desired_size - old_size[1]
delta_h = desired_size - old_size[0]
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
left, right = delta_w // 2, delta_w - (delta_w // 2)
color = [0, 0, 0]
color2 = [0]
new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color)
new_label = cv2.copyMakeBorder(label, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color2)
test_data.append(cv2.resize(new_im, (desired_size, desired_size)))
temp = cv2.resize(new_label, (desired_size, desired_size))
_, temp = cv2.threshold(temp, 127, 255, cv2.THRESH_BINARY)
test_label.append(temp)
test_data = np.array(test_data)
test_label = np.array(test_label)
x_test = test_data.astype('float32') / 255.
y_test = test_label.astype('float32') / 255.
x_test = np.reshape(x_test, (len(x_test), desired_size, desired_size, 3)) # adapt this if using `channels_first` image data format
y_test= np.reshape(y_test, (len(y_test), desired_size, desired_size, 1)) # adapt this if using `channels_first` im
if os.path.exists('./save_weights/unet.h5'):
model = UNET.load_weights("./save_weights/unet.h5")
test = model.evaluate(x_test, y_test, batch_size = 4)
#test = model.evaluate(x_test, y_test, batch_size = 4)
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
dice = (2. * intersection + K.epsilon()) / (K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())
return 1 - dice
class UNET_test(object):
def __init__(self):
# create unet
n_classes = 1
self.unet = UNET(n_classes)
# compile
lr = 1e-3
self.unet.compile(optimizer = tf.keras.optimizers.Adam(lr = lr), loss = 'binary_crossentropy', metrics = ['accuracy'])
#self.unet.compile(optimizer = tf.keras.optimizers.Adam(lr = 1e-3), loss = [dice_coef], metrics = ['accuracy'])
def eval(self, x_test, y_test):
if os.path.exists('unet_binary.h5'):
self.unet.build(input_shape=(None, 592, 592, 3))
self.unet.load_weights('unet_binary.h5')
else:
return 0
pred_seg = self.unet(x_test)
plt.figure(figsize=(20, 6))
n = 5
#ini = random.randint(0, 20-n)
ini = 7
for i in range(20):
y_test_flat = (y_test[i]).astype(np.uint8).flatten()
pred = pred_seg[i].numpy()
pred[pred >= 0.5] = 1
pred[pred < 0.5] = 0
pred_flat = (pred).astype(np.uint8).flatten()
j.append(jaccard_score(y_test_flat, pred_flat, pos_label = 1))
print("jaccard: ", sum(j) / len(j))
for i in range(n):
# display image
ax = plt.subplot(3, n, i + 1)
image = cv2.cvtColor(x_test[ini+i], cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.title(str(ini+i) + "original")
plt.savefig('original.png')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display true segmentation
ax = plt.subplot(3, n, i + 1 + n)
image = cv2.cvtColor((y_test[ini+i]*255).astype(np.uint8), cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.title(str(ini+i) + "true segmentation")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display predictions
ax = plt.subplot(3, n, i + 1 + n + n)
pred = pred_seg[ini + i].numpy()
pred[pred > 0.5] = 1
pred[pred <= 0.5] = 0
image = cv2.cvtColor((pred*255).astype(np.uint8), cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.title("prediction")
plt.savefig('prediction.png')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
a = UNET_test()
a.eval(x_test, y_test)