-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpredict.py
More file actions
57 lines (47 loc) · 1.64 KB
/
predict.py
File metadata and controls
57 lines (47 loc) · 1.64 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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import cv2
import pydicom as dicom
import pandas as pd
from glob import glob
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras.utils import CustomObjectScope
from sklearn.metrics import accuracy_score, f1_score, jaccard_score, precision_score, recall_score
from metrics import dice_loss, dice_coef, iou
""" Creating a directory """
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
if __name__ == "__main__":
""" Seeding """
np.random.seed(42)
tf.random.set_seed(42)
""" Directory for storing files """
create_dir("test")
""" Loading model """
with CustomObjectScope({'iou': iou, 'dice_coef': dice_coef, 'dice_loss': dice_loss}):
model = tf.keras.models.load_model("files/model.h5")
""" Load the dataset """
test_x = glob("data/test/*/*/*.dcm")
print(f"Test: {len(test_x)}")
""" Loop over the data """
for x in tqdm(test_x):
""" Extract the names """
dir_name = x.split("/")[-3]
name = dir_name + "_" + x.split("/")[-1].split(".")[0]
""" Read the image """
image = dicom.dcmread(x).pixel_array
image = np.expand_dims(image, axis=-1)
image = image/np.max(image) * 255.0
x = image/255.0
x = np.concatenate([x, x, x], axis=-1)
x = np.expand_dims(x, axis=0)
""" Prediction """
mask = model.predict(x)[0]
mask = mask > 0.5
mask = mask.astype(np.int32)
mask = mask * 255
cat_images = np.concatenate([image, mask], axis=1)
cv2.imwrite(f"test/{name}.png", cat_images)