-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
67 lines (57 loc) · 2.36 KB
/
evaluate.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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import cv2
from glob import glob
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras.utils import CustomObjectScope
from metrics import dice_loss, dice_coef, iou
from train import load_data, create_dir, tf_dataset
H = 512
W = 512
if __name__ == "__main__":
""" Seeding """
np.random.seed(42)
tf.random.set_seed(42)
""" Directory for storing files """
create_dir("results")
""" Loading model """
with CustomObjectScope({'iou': iou, 'dice_coef': dice_coef, 'dice_loss': dice_loss}):
model = tf.keras.models.load_model("files/model.h5")
""" Dataset """
dataset_path = "C:/Users/Ashwath/Desktop/Capstone/Unet lung segmentation/MontgomerySet"
(train_x, train_y1, train_y2), (valid_x, valid_y1, valid_y2), (test_x, test_y1, test_y2) = load_data(dataset_path)
""" Predicting the mask """
for x, y1, y2 in tqdm(zip(test_x, test_y1, test_y2), total=len(test_x)):
""" Extracing the image name. """
#print(x)
image_name = x.split("/")[-1]
#print(image_name)
print(x)
#print(" " + image_name + "|||" + x)
""" Reading the image """
ori_x = cv2.imread(x, cv2.IMREAD_COLOR)
ori_x = cv2.resize(ori_x, (W, H))
x = ori_x/255.0
x = x.astype(np.float32)
x = np.expand_dims(x, axis=0)
""" Reading the mask """
ori_y1 = cv2.imread(y1, cv2.IMREAD_GRAYSCALE)
ori_y2 = cv2.imread(y2, cv2.IMREAD_GRAYSCALE)
ori_y = ori_y1 + ori_y2
ori_y = cv2.resize(ori_y, (W, H))
ori_y = np.expand_dims(ori_y, axis=-1) ## (512, 512, 1)
ori_y = np.concatenate([ori_y, ori_y, ori_y], axis=-1) ## (512, 512, 3)
""" Predicting the mask. """
y_pred = model.predict(x)[0] > 0.5
y_pred = y_pred.astype(np.int32)
""" Saving the predicted mask along with the image and GT """
save_image_path = f"results\\{image_name}"
print(" " + save_image_path)
y_pred = np.concatenate([y_pred, y_pred, y_pred], axis=-1)
sep_line = np.ones((H, 10, 3)) * 255
#cat_image = np.concatenate([ori_x, sep_line, ori_y, sep_line, y_pred*255], axis=1)
cat_image = np.concatenate([y_pred*255], axis=1)
#cv2.imwrite(save_image_path, ori_y)
#cv2.imwrite(save_image_path, cat_image)