forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate-disease-dlv3plus.py
189 lines (146 loc) · 6.42 KB
/
evaluate-disease-dlv3plus.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
import os
import numpy as np
import tensorflow as tf
import keras_cv
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix, jaccard_score
import seaborn as sns
DATASET_DIR = os.path.join(os.getcwd(), "data")
TEST_DIR = os.path.join(DATASET_DIR, "test")
MODEL_SAVE_DIR = os.path.join(os.getcwd(), "dlv3plus", "models")
BEST_MODEL_PATH = os.path.join(MODEL_SAVE_DIR, 'best_model.weights.h5')
VISUALIZATION_DIR = os.path.join(os.getcwd(), "dlv3plus", "visualizations")
os.makedirs(VISUALIZATION_DIR, exist_ok=True)
test_images_dir = os.path.join(TEST_DIR, "images")
test_masks_dir = os.path.join(TEST_DIR, "masks")
test_batch_size = 1
def load_image(image_path, mask=False):
img = tf.keras.preprocessing.image.load_img(image_path.numpy().decode("utf-8"), color_mode="grayscale" if mask else "rgb")
img = tf.keras.preprocessing.image.img_to_array(img)
return img
def parse_function(image_path, mask_path):
image = tf.py_function(load_image, [image_path], tf.float32)
mask = tf.py_function(load_image, [mask_path, True], tf.float32)
image.set_shape([None, None, 3])
mask.set_shape([None, None, 1])
IMAGE_MAX_DIM = 1024
image = tf.image.resize(image, [IMAGE_MAX_DIM, IMAGE_MAX_DIM], method='bilinear')
mask = tf.image.resize(mask, [IMAGE_MAX_DIM, IMAGE_MAX_DIM], method='nearest')
image = image / 255.0
mask = mask / 255.0
return image, mask
def create_dataset(image_paths, mask_paths, batch_size):
dataset = tf.data.Dataset.from_tensor_slices((image_paths, mask_paths))
dataset = dataset.map(parse_function, num_parallel_calls=tf.data.AUTOTUNE)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(buffer_size=tf.data.AUTOTUNE)
return dataset
test_image_paths = sorted([os.path.join(test_images_dir, fname) for fname in os.listdir(test_images_dir) if fname.endswith(".jpg") or fname.endswith(".png")])
test_mask_paths = sorted([os.path.join(test_masks_dir, fname) for fname in os.listdir(test_masks_dir) if fname.endswith(".jpg") or fname.endswith(".png")])
test_dataset = create_dataset(test_image_paths, test_mask_paths, test_batch_size)
backbone = keras_cv.models.ResNet50Backbone(
include_rescaling=False,
)
model = keras_cv.models.segmentation.DeepLabV3Plus(
backbone=backbone,
num_classes=2,
)
if os.path.exists(BEST_MODEL_PATH):
print(f"Loading best model from {BEST_MODEL_PATH}")
model.load_weights(BEST_MODEL_PATH)
else:
raise FileNotFoundError(f"No best model found at {BEST_MODEL_PATH}")
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["accuracy"]
)
print("Evaluating model...")
results = model.evaluate(test_dataset, return_dict=True)
print("\nEvaluation Results:")
for metric_name, metric_value in results.items():
print(f"{metric_name}: {metric_value:.4f}")
def visualize_and_save_results(test_images, test_masks, preds, idx):
fig, ax = plt.subplots(1, 4, figsize=(20, 5))
ax[0].imshow(test_images[0])
ax[0].set_title("Original Image")
ax[0].axis("off")
ax[1].imshow(np.squeeze(test_masks[0]), cmap="gray")
ax[1].set_title("True Mask")
ax[1].axis("off")
ax[2].imshow(np.squeeze(preds[0]), cmap="gray")
ax[2].set_title("Predicted Mask")
ax[2].axis("off")
ax[3].imshow(test_images[0])
ax[3].imshow(np.squeeze(preds[0]), cmap="gray", alpha=0.5, interpolation='none')
ax[3].set_title("Overlay of Predicted Mask")
ax[3].axis("off")
plt.tight_layout()
plt.subplots_adjust(top=0.85)
save_path = os.path.join(VISUALIZATION_DIR, f"result_{idx:04d}.png")
plt.savefig(save_path)
plt.close()
print(f"Saved visualization to {save_path}")
print("Generating predictions and saving visualizations...")
for idx, (test_images, test_masks) in enumerate(test_dataset):
preds = model.predict(test_images)
preds = np.argmax(preds, axis=-1)
preds = np.expand_dims(preds, axis=-1)
visualize_and_save_results(test_images.numpy(), test_masks.numpy(), preds, idx)
print("All visualizations saved.")
all_preds = []
all_trues = []
for test_images, test_masks in test_dataset:
preds = model.predict(test_images)
preds = np.argmax(preds, axis=-1)
preds = np.expand_dims(preds, axis=-1)
all_preds.append(preds)
all_trues.append(test_masks.numpy())
all_preds = np.concatenate(all_preds, axis=0).flatten()
all_trues = np.concatenate(all_trues, axis=0).flatten()
print("\nConfusion Matrix:")
conf_matrix = confusion_matrix(all_trues, all_preds)
print(conf_matrix)
conf_matrix_percentage = conf_matrix.astype('float') / conf_matrix.sum(axis=1)[:, np.newaxis] * 100
TN, FP, FN, TP = conf_matrix.ravel()
print(f"True Positives (TP): {TP}")
print(f"False Positives (FP): {FP}")
print(f"False Negatives (FN): {FN}")
print(f"True Negatives (TN): {TN}")
iou = jaccard_score(all_trues, all_preds, average='binary')
print(f"\nIoU (Jaccard Index): {iou:.4f}")
labels = np.array([
[f"TN\n{conf_matrix[0, 0]} ({conf_matrix_percentage[0, 0]:.1f}%)", f"FP\n{conf_matrix[0, 1]} ({conf_matrix_percentage[0, 1]:.1f}%)"],
[f"FN\n{conf_matrix[1, 0]} ({conf_matrix_percentage[1, 0]:.1f}%)", f"TP\n{conf_matrix[1, 1]} ({conf_matrix_percentage[1, 1]:.1f}%)"]
])
plt.figure(figsize=(8, 6))
sns.heatmap(
conf_matrix,
annot=labels,
fmt='',
cmap='Blues',
xticklabels=["Background", "Disease"],
yticklabels=["Background", "Disease"]
)
plt.xlabel('Predicted')
plt.ylabel('Ground Truth')
plt.title('Confusion Matrix')
conf_matrix_path = os.path.join(VISUALIZATION_DIR, 'confusion_matrix.png')
plt.savefig(conf_matrix_path)
plt.close()
print(f"Saved confusion matrix with counts and percentages to {conf_matrix_path}")
results_file_path = os.path.join(MODEL_SAVE_DIR, 'evaluation_results.txt')
with open(results_file_path, 'w') as f:
for metric_name, metric_value in results.items():
f.write(f"{metric_name}: {metric_value:.4f}\n")
with open(results_file_path, 'a') as f:
f.write("\nConfusion Matrix:\n")
f.write(np.array2string(conf_matrix))
f.write(f"\n\nTrue Positives (TP): {TP}\n")
f.write(f"False Positives (FP): {FP}\n")
f.write(f"False Negatives (FN): {FN}\n")
f.write(f"True Negatives (TN): {TN}\n")
f.write(f"IoU (Jaccard Index): {iou:.4f}\n")
f.write("\n\nClassification Report:\n")
f.write(classification_report(all_trues, all_preds, target_names=["Background", "Disease"]))
print("All evaluation metrics saved.")