|
a |
|
b/eval.py |
|
|
1 |
import os |
|
|
2 |
import cv2 |
|
|
3 |
import numpy as np |
|
|
4 |
import pandas as pd |
|
|
5 |
from glob import glob |
|
|
6 |
from tqdm import tqdm |
|
|
7 |
|
|
|
8 |
import tensorflow as tf |
|
|
9 |
from tensorflow.keras.utils import CustomObjectScope |
|
|
10 |
from sklearn.metrics import ( |
|
|
11 |
accuracy_score, |
|
|
12 |
f1_score, |
|
|
13 |
jaccard_score, |
|
|
14 |
precision_score, |
|
|
15 |
recall_score, |
|
|
16 |
) |
|
|
17 |
from metrics import dice_loss, dice_coef, iou |
|
|
18 |
from train import load_data |
|
|
19 |
|
|
|
20 |
IMG_HEIGHT = 512 |
|
|
21 |
IMG_WIDTH = 512 |
|
|
22 |
|
|
|
23 |
""" Creating a directory """ |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
def create_dir(path): |
|
|
27 |
if not os.path.exists(path): |
|
|
28 |
os.makedirs(path) |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
def save_results(image, mask, y_pred, save_image_path): |
|
|
32 |
## i - m - y |
|
|
33 |
line = np.ones((IMG_HEIGHT, 10, 3)) * 128 |
|
|
34 |
|
|
|
35 |
""" Mask """ |
|
|
36 |
mask = np.expand_dims(mask, axis=-1) ## (512, 512, 1) |
|
|
37 |
mask = np.concatenate([mask, mask, mask], axis=-1) ## (512, 512, 3) |
|
|
38 |
|
|
|
39 |
""" Predicted Mask """ |
|
|
40 |
y_pred = np.expand_dims(y_pred, axis=-1) ## (512, 512, 1) |
|
|
41 |
y_pred = np.concatenate([y_pred, y_pred, y_pred], axis=-1) ## (512, 512, 3) |
|
|
42 |
y_pred = y_pred * 255 |
|
|
43 |
|
|
|
44 |
cat_images = np.concatenate([image, line, mask, line, y_pred], axis=1) |
|
|
45 |
cv2.imwrite(save_image_path, cat_images) |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
if __name__ == "__main__": |
|
|
49 |
"""Seeding""" |
|
|
50 |
SEEDS = 42 |
|
|
51 |
np.random.seed(SEEDS) |
|
|
52 |
tf.random.set_seed(SEEDS) |
|
|
53 |
|
|
|
54 |
""" Directory for storing files """ |
|
|
55 |
create_dir("results") |
|
|
56 |
|
|
|
57 |
""" Loading model """ |
|
|
58 |
with CustomObjectScope( |
|
|
59 |
{"iou": iou, "dice_coef": dice_coef, "dice_loss": dice_loss} |
|
|
60 |
): |
|
|
61 |
model = tf.keras.models.load_model("files/model.h5") |
|
|
62 |
|
|
|
63 |
""" Load the dataset """ |
|
|
64 |
test_x = sorted(glob(os.path.join("new_data", "valid", "image", "*"))) |
|
|
65 |
test_y = sorted(glob(os.path.join("new_data", "valid", "mask", "*"))) |
|
|
66 |
print(f"Test: {len(test_x)} - {len(test_y)}") |
|
|
67 |
|
|
|
68 |
""" Evaluation and Prediction """ |
|
|
69 |
SCORE = [] |
|
|
70 |
for x, y in tqdm(zip(test_x, test_y), total=len(test_x)): |
|
|
71 |
"""Extract the name""" |
|
|
72 |
name = x.split("/")[-1].split(".")[0] |
|
|
73 |
|
|
|
74 |
""" Reading the image """ |
|
|
75 |
image = cv2.imread(x, cv2.IMREAD_COLOR) |
|
|
76 |
x = image / 255.0 |
|
|
77 |
x = np.expand_dims(x, axis=0) |
|
|
78 |
|
|
|
79 |
""" Reading the mask """ |
|
|
80 |
mask = cv2.imread(y, cv2.IMREAD_GRAYSCALE) |
|
|
81 |
y = mask / 255.0 |
|
|
82 |
y = y > 0.5 |
|
|
83 |
y = y.astype(np.int32) |
|
|
84 |
|
|
|
85 |
""" Prediction """ |
|
|
86 |
y_pred = model.predict(x)[0] |
|
|
87 |
y_pred = np.squeeze(y_pred, axis=-1) |
|
|
88 |
y_pred = y_pred > 0.5 |
|
|
89 |
y_pred = y_pred.astype(np.int32) |
|
|
90 |
|
|
|
91 |
""" Saving the prediction """ |
|
|
92 |
save_image_path = f"results/{name}.png" |
|
|
93 |
save_results(image, mask, y_pred, save_image_path) |
|
|
94 |
|
|
|
95 |
""" Flatten the array """ |
|
|
96 |
y = y.flatten() |
|
|
97 |
y_pred = y_pred.flatten() |
|
|
98 |
|
|
|
99 |
""" Calculating the metrics values """ |
|
|
100 |
acc_value = accuracy_score(y, y_pred) |
|
|
101 |
f1_value = f1_score(y, y_pred, labels=[0, 1], average="binary", zero_division=1) |
|
|
102 |
jac_value = jaccard_score( |
|
|
103 |
y, y_pred, labels=[0, 1], average="binary", zero_division=1 |
|
|
104 |
) |
|
|
105 |
recall_value = recall_score( |
|
|
106 |
y, y_pred, labels=[0, 1], average="binary", zero_division=1 |
|
|
107 |
) |
|
|
108 |
precision_value = precision_score( |
|
|
109 |
y, y_pred, labels=[0, 1], average="binary", zero_division=1 |
|
|
110 |
) |
|
|
111 |
SCORE.append( |
|
|
112 |
[name, acc_value, f1_value, jac_value, recall_value, precision_value] |
|
|
113 |
) |
|
|
114 |
|
|
|
115 |
""" Metrics values """ |
|
|
116 |
score = [s[1:] for s in SCORE] |
|
|
117 |
score = np.mean(score, axis=0) |
|
|
118 |
print(f"Accuracy: {score[0]:0.5f}") |
|
|
119 |
print(f"F1: {score[1]:0.5f}") |
|
|
120 |
print(f"Jaccard: {score[2]:0.5f}") |
|
|
121 |
print(f"Recall: {score[3]:0.5f}") |
|
|
122 |
print(f"Precision: {score[4]:0.5f}") |
|
|
123 |
|
|
|
124 |
df = pd.DataFrame( |
|
|
125 |
SCORE, columns=["Image", "Accuracy", "F1", "Jaccard", "Recall", "Precision"] |
|
|
126 |
) |
|
|
127 |
df.to_csv("files/score.csv") |