-
Notifications
You must be signed in to change notification settings - Fork 24
/
infer.py
215 lines (178 loc) · 6.28 KB
/
infer.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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
#os.environ["CUDA_VISIBLE_DEVICES"]="0";
import numpy as np
import cv2
from glob import glob
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import CustomObjectScope
from tensorflow.keras.metrics import MeanIoU
import tensorflow.keras.backend as K
from m_resunet import ResUnetPlusPlus
from metrics import *
from sklearn.metrics import confusion_matrix
from sklearn.metrics import recall_score, precision_score
from crf import apply_crf
from tta import tta_model
def read_image(x):
image = cv2.imread(x, cv2.IMREAD_COLOR)
image = np.clip(image - np.median(image)+127, 0, 255)
image = image/255.0
image = image.astype(np.float32)
image = np.expand_dims(image, axis=0)
return image
def read_mask(y):
mask = cv2.imread(y, cv2.IMREAD_GRAYSCALE)
mask = mask.astype(np.float32)
mask = mask/255.0
mask = np.expand_dims(mask, axis=-1)
return mask
def get_dice_coef(y_true, y_pred):
intersection = np.sum(y_true * y_pred)
return (2. * intersection + smooth) / (np.sum(y_true) + np.sum(y_pred) + smooth)
def get_mean_iou(y_true, y_pred):
# y_true = y_true.astype(np.int32)
# current = confusion_matrix(y_true, y_pred, labels=[0, 1])
#
# # compute mean iou
# intersection = np.diag(current)
# ground_truth_set = current.sum(axis=1)
# predicted_set = current.sum(axis=0)
# union = ground_truth_set + predicted_set - intersection
# IoU = intersection / union.astype(np.float32)
# return np.mean(IoU)
y_pred = y_pred > 0.5
y_pred = y_pred.astype(np.int32)
y_true = y_true.astype(np.int32)
m = tf.keras.metrics.MeanIoU(num_classes=2)
m.update_state(y_true, y_pred)
r = m.result().numpy()
m.reset_states()
return r
def get_recall(y_true, y_pred):
# smooth = 1
# y_true = y_true.astype(np.int32)
# TN, FP, FN, TP = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
# recall_score = TP + smooth / (TP + FN + smooth)
# return recall_score
y_pred = y_pred > 0.5
y_pred = y_pred.astype(np.int32)
m = tf.keras.metrics.Recall()
m.update_state(y_true, y_pred)
r = m.result().numpy()
m.reset_states()
return r
def get_precision(y_true, y_pred):
# smooth = 1
# y_true = y_true.astype(np.int32)
# TN, FP, FN, TP = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
# precision_score = TP + smooth / (TP + FP + smooth)
# return precision_score
y_pred = y_pred > 0.5
y_pred = y_pred.astype(np.int32)
m = tf.keras.metrics.Precision()
m.update_state(y_true, y_pred)
r = m.result().numpy()
m.reset_states()
return r
def confusion(y_true, y_pred):
y_true = tf.convert_to_tensor(y_true)
y_pred = tf.convert_to_tensor(y_pred)
smooth=1
y_pred_pos = K.clip(y_pred, 0, 1)
y_pred_neg = 1 - y_pred_pos
y_pos = K.clip(y_true, 0, 1)
y_neg = 1 - y_pos
tp = K.sum(y_pos * y_pred_pos)
fp = K.sum(y_neg * y_pred_pos)
fn = K.sum(y_pos * y_pred_neg)
prec = (tp + smooth)/(tp+fp+smooth)
recall = (tp+smooth)/(tp+fn+smooth)
return prec, recall
def get_metrics(y_true, y_pred):
y_pred = y_pred.flatten()
y_true = y_true.flatten()
dice_coef_val = get_dice_coef(y_true, y_pred)
mean_iou_val = get_mean_iou(y_true, y_pred)
y_true = y_true.astype(np.int32)
# recall_value = recall_score(y_pred, y_true, average='micro')
# precision_value = precision_score(y_pred, y_true, average='micro')
recall_value = get_recall(y_true, y_pred)
precision_value = get_precision(y_true, y_pred)
return [dice_coef_val, mean_iou_val, recall_value, precision_value]
def evaluate_normal(model, x_data, y_data):
total = []
for x, y in tqdm(zip(x_data, y_data), total=len(x_data)):
x = read_image(x)
y = read_mask(y)
y_pred = model.predict(x)[0] > 0.5
y_pred = y_pred.astype(np.float32)
value = get_metrics(y, y_pred)
total.append(value)
mean_value = np.mean(total, axis=0)
print(mean_value)
def evaluate_crf(model, x_data, y_data):
total = []
for x, y in tqdm(zip(x_data, y_data), total=len(x_data)):
x = read_image(x)
y = read_mask(y)
y_pred = model.predict(x)[0] > 0.5
y_pred = y_pred.astype(np.float32)
y_pred = apply_crf(x[0]*255, y_pred)
value = get_metrics(y, y_pred)
total.append(value)
mean_value = np.mean(total, axis=0)
print(mean_value)
def evaluate_tta(model, x_data, y_data):
total = []
for x, y in tqdm(zip(x_data, y_data), total=len(x_data)):
x = read_image(x)
y = read_mask(y)
y_pred = tta_model(model, x[0])
y_pred = y_pred > 0.5
y_pred = y_pred.astype(np.float32)
value = get_metrics(y, y_pred)
total.append(value)
mean_value = np.mean(total, axis=0)
print(mean_value)
def evaluate_crf_tta(model, x_data, y_data):
total = []
for x, y in tqdm(zip(x_data, y_data), total=len(x_data)):
x = read_image(x)
y = read_mask(y)
y_pred = tta_model(model, x[0])
y_pred = y_pred > 0.5
y_pred = y_pred.astype(np.float32)
y_pred = apply_crf(x[0]*255, y_pred)
value = get_metrics(y, y_pred)
total.append(value)
mean_value = np.mean(total, axis=0)
print(mean_value)
if __name__ == "__main__":
tf.random.set_seed(42)
np.random.seed(42)
model_path = "files/resunetplusplus.h5"
## Parameters
image_size = 256
batch_size = 32
lr = 1e-4
epochs = 100
## Validation
valid_path = "cs_data/CVC-12k"
valid_image_paths = sorted(glob(os.path.join(valid_path, "image", "*.jpg")))
valid_mask_paths = sorted(glob(os.path.join(valid_path, "mask", "*.jpg")))
with CustomObjectScope({
'dice_loss': dice_loss,
'dice_coef': dice_coef,
'bce_dice_loss': bce_dice_loss,
'focal_loss': focal_loss,
'tversky_loss': tversky_loss,
'focal_tversky': focal_tversky
}):
model = load_model(model_path)
evaluate_normal(model, valid_image_paths, valid_mask_paths)
evaluate_crf(model, valid_image_paths, valid_mask_paths)
evaluate_tta(model, valid_image_paths, valid_mask_paths)
evaluate_crf_tta(model, valid_image_paths, valid_mask_paths)