Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 0 additions & 169 deletions README.md

This file was deleted.

123 changes: 123 additions & 0 deletions eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

import tensorflow as tf
import numpy as np
import cv2
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
from train import create_dir, load_data

H = 256
W = 256

def read_image(path):
x = cv2.imread(path, cv2.IMREAD_COLOR) ## (H, W, 3)
x = cv2.resize(x, (W, H))
ori_x = x
x = x/255.0
x = x.astype(np.float32)
x = np.expand_dims(x, axis=0)
return ori_x, x ## (1, 256, 256, 3)


def read_mask(path):
x = cv2.imread(path, cv2.IMREAD_GRAYSCALE) ## (H, W)
x = cv2.resize(x, (W, H))
ori_x = x
x = x/255.0
x = x.astype(np.int32) ## (256, 256)
return ori_x, x

def save_results(ori_x, ori_y, y_pred, save_image_path):
line = np.ones((H, 10, 3)) * 255

ori_y = np.expand_dims(ori_y, axis=-1) ## (256, 256, 1)
ori_y = np.concatenate([ori_y, ori_y, ori_y], axis=-1) ## (256, 256, 3)

y_pred = np.expand_dims(y_pred, axis=-1) ## (256, 256, 1)
y_pred = np.concatenate([y_pred, y_pred, y_pred], axis=-1) ## (256, 256, 3)

cat_images = np.concatenate([ori_x, line, ori_y, line, y_pred*255], axis=1)
cv2.imwrite(save_image_path, cat_images)

if __name__ == "__main__":
""" Seeding """
np.random.seed(42)
tf.random.set_seed(42)

""" Folder for saving results """
create_dir("results_DLV3SA_2value_no_early_stop")

""" Load the model """
with CustomObjectScope({'iou': iou, 'dice_coef': dice_coef, 'dice_loss': dice_loss}):
model = tf.keras.models.load_model("files_DLV3SA/model_DLV3SA_2value_no_early_stop.h5")

""" Load the test data """
dataset_path = "D:/medical_challenge/segmentation/CVC-612/data/"
# dataset_path = "D:/medical_challenge/segmentation/2d/VIP_CUP_5fold/fold_1/train/"
# dataset_path = "D:/medical_challenge/segmentation/CVC-612/CVC_ClinicDB"
# dataset_path = "D:/medical_challenge/segmentation/2d/data_mbrain_5fold/fold_0/train/"
(train_x, train_y), (valid_x, valid_y) = load_data(dataset_path)
# print(len(test_x), len(test_y))
print("=====================================================================================================")
print(len(valid_x), len(valid_y))
print("=====================================================================================================")
print(len(train_x), len(train_y))
SCORE = []
for x, y in tqdm(zip(valid_x, valid_y), total=len(valid_x)):
""" Exctracting the image name """
# name = x.split("/")[-1]
# print(x)
name = x
""" Read the image and mask """
ori_x, x = read_image(x)
ori_y, y = read_mask(y)

""" Predicting the mask """
y_pred = model.predict(x)[0] > 0.5
y_pred = np.squeeze(y_pred, axis=-1)
y_pred = y_pred.astype(np.int32)

""" Saving the predicted mask """
save_image_path = os.path.join(os.getcwd(), 'results_DLV3SA_2value_no_early_stop', os.path.basename(name))
## os.getcwd: lấy path fiel hiện tại
## basename: lấy tên ảnh

# print("*"*50, save_image_path)
save_results(ori_x, ori_y, y_pred, save_image_path)

""" Flatten the array """
y = y.flatten()
y_pred = y_pred.flatten()

""" Calculating metrics values """
# dice_coef_value = 1 - dice_loss(y, y_pred)
acc_value = accuracy_score(y, y_pred)
f1_value = f1_score(y, y_pred, labels=[0, 1], average="binary")
jac_value = jaccard_score(y, y_pred, labels=[0, 1], average="binary")
recall_value = recall_score(y, y_pred, labels=[0, 1], average="binary")
precision_value = precision_score(y, y_pred, labels=[0, 1], average="binary")
SCORE.append([name, acc_value, f1_value, jac_value, recall_value, precision_value])


""" mean metrics values """
score = [s[1:] for s in SCORE]
score = np.mean(score, axis=0)
# print(f"Dice score: {score[0]:0.5f}")
print(f"Accuracy: {score[0]:0.5f}")
print(f"F1: {score[1]:0.5f}")
print(f"Jaccard: {score[2]:0.5f}")
print(f"Recall: {score[3]:0.5f}")
print(f"Precision: {score[4]:0.5f}")

df = pd.DataFrame(SCORE, columns = ["Image Name", "Acc", "F1", "Jaccard", "Recall", "Precision"])
df.to_csv("files_DLV3SA/score_DLV3SA_2value_no_early_stop.csv")

Loading