trial and error with the min_overlap 10% to 2%
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
print("de juiste init file")
|
||||
from .batchgenerator import *
|
||||
from .callbacks import *
|
||||
from .helpers import *
|
||||
|
@@ -105,7 +105,6 @@ def preprocess_softmax_dynamic(softmax: np.ndarray,
|
||||
|
||||
# set dynamic threshold to half the max
|
||||
threshold = max_prob / dynamic_threshold_factor
|
||||
|
||||
# extract blobs for dynamix threshold
|
||||
all_hard_blobs, _, _ = preprocess_softmax_static(working_softmax, threshold=threshold,
|
||||
min_voxels_detection=min_voxels_detection,
|
||||
@@ -188,14 +187,13 @@ def preprocess_softmax(softmax: np.ndarray,
|
||||
def evaluate(
|
||||
y_true: np.ndarray,
|
||||
y_pred: np.ndarray,
|
||||
min_overlap=0.10,
|
||||
min_overlap=0.02,
|
||||
overlap_func: str = 'DSC',
|
||||
case_confidence: str = 'max',
|
||||
multiple_lesion_candidates_selection_criteria='overlap',
|
||||
allow_unmatched_candidates_with_minimal_overlap=True,
|
||||
flat: Optional[bool] = None
|
||||
) -> Dict[str, Any]:
|
||||
|
||||
# Make list out of numpy array so that it can be mapped in parallel with multiple CPUs.
|
||||
y_true_list = y_true #[y_true[mri_idx] for mri_idx in range(y_true.shape[0])]
|
||||
y_pred_list = y_pred #[y_pred[mri_idx] for mri_idx in range(y_pred.shape[0])]
|
||||
@@ -431,7 +429,6 @@ def evaluate_case(
|
||||
confidences, indexed_pred = parse_detection_map(y_pred)
|
||||
|
||||
lesion_candidates_best_overlap: Dict[str, float] = {}
|
||||
|
||||
if y_true.any():
|
||||
# for each malignant scan
|
||||
labeled_gt, num_gt_lesions = ndimage.label(y_true, np.ones((3, 3, 3)))
|
||||
@@ -464,7 +461,7 @@ def evaluate_case(
|
||||
'overlap': overlap_score,
|
||||
})
|
||||
print(lesion_candidates_for_target_gt)
|
||||
|
||||
print("min benodigde overlap:", min_overlap)
|
||||
if len(lesion_candidates_for_target_gt) == 0:
|
||||
# no lesion candidate matched with GT mask. Add FN.
|
||||
y_list.append((1, 0., 0.))
|
||||
|
@@ -30,10 +30,10 @@ try:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
from image_utils import (
|
||||
from sfransen.FROC.image_utils import (
|
||||
resize_image_with_crop_or_pad, read_label, read_prediction
|
||||
)
|
||||
from analysis_utils import (
|
||||
from sfransen.FROC.analysis_utils import (
|
||||
parse_detection_map, calculate_iou, calculate_dsc
|
||||
)
|
||||
|
||||
|
2
src/sfransen/Saliency/__init__.py
Executable file
2
src/sfransen/Saliency/__init__.py
Executable file
@@ -0,0 +1,2 @@
|
||||
from .base import *
|
||||
from .integrated_gradients import *
|
@@ -1,12 +1,11 @@
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
from tensorflow.keras.applications import densenet
|
||||
|
||||
from base import SaliencyMap
|
||||
from sfransen.Saliency.base import SaliencyMap
|
||||
|
||||
class IntegratedGradients(SaliencyMap):
|
||||
|
||||
def get_mask(self, image, baseline=None, num_steps=2):
|
||||
def get_mask(self, image, baseline=None, num_steps=4):
|
||||
"""Computes Integrated Gradients for a predicted label.
|
||||
|
||||
Args:
|
||||
@@ -28,7 +27,6 @@ class IntegratedGradients(SaliencyMap):
|
||||
baseline = np.zeros(img_size).astype(np.float32)
|
||||
else:
|
||||
baseline = baseline.astype(np.float32)
|
||||
print(">>>> step ONE completed")
|
||||
|
||||
img_input = image
|
||||
top_pred_idx = self.get_top_predicted_idx(image)
|
||||
@@ -37,27 +35,20 @@ class IntegratedGradients(SaliencyMap):
|
||||
for i in range(num_steps + 1)
|
||||
]
|
||||
interpolated_image = np.vstack(interpolated_image).astype(np.float32)
|
||||
print(">>>> step TWO completed")
|
||||
|
||||
grads = []
|
||||
for i, img in enumerate(interpolated_image):
|
||||
print("number of image:",i)
|
||||
print("size of image:",np.shape(img))
|
||||
print(f"interpolation step:",i," out of {num_steps}")
|
||||
img = tf.expand_dims(img, axis=0)
|
||||
grad = self.get_gradients(img)
|
||||
print("size of grad is:",np.shape(grad))
|
||||
grads.append(grad[0])
|
||||
grads = tf.convert_to_tensor(grads, dtype=tf.float32)
|
||||
print(">>>> step THREE completed")
|
||||
|
||||
# 4. Approximate the integral using the trapezoidal rule
|
||||
grads = (grads[:-1] + grads[1:]) / 2.0
|
||||
avg_grads = tf.reduce_mean(grads, axis=0)
|
||||
# tf.reduce_mean(grads, axis=(0, 1, 2, 3))
|
||||
print(">>>> step FOUR completed")
|
||||
|
||||
# 5. Calculate integrated gradients and return
|
||||
integrated_grads = (img_input - baseline) * avg_grads
|
||||
print(">>>> step FIVE completed")
|
||||
|
||||
return integrated_grads
|
||||
|
30
src/sfransen/load_images.py
Executable file
30
src/sfransen/load_images.py
Executable file
@@ -0,0 +1,30 @@
|
||||
from typing import List
|
||||
import SimpleITK as sitk
|
||||
from sfransen.DWI_exp.helpers import *
|
||||
|
||||
def load_images_parrallel(
|
||||
image_paths: str,
|
||||
seq: str,
|
||||
target_shape: List[int],
|
||||
target_space = List[float]):
|
||||
|
||||
img_s = sitk.ReadImage(image_paths, sitk.sitkFloat32)
|
||||
|
||||
#resample
|
||||
mri_tra_s = resample(img_s,
|
||||
min_shape=target_shape,
|
||||
method=sitk.sitkNearestNeighbor,
|
||||
new_spacing=target_space)
|
||||
|
||||
#center crop
|
||||
mri_tra_s = center_crop(mri_tra_s, shape=target_shape)
|
||||
#normalize
|
||||
if seq != 'seg':
|
||||
filter = sitk.NormalizeImageFilter()
|
||||
mri_tra_s = filter.Execute(mri_tra_s)
|
||||
else:
|
||||
filter = sitk.BinaryThresholdImageFilter()
|
||||
filter.SetLowerThreshold(1.0)
|
||||
mri_tra_s = filter.Execute(mri_tra_s)
|
||||
|
||||
return sitk.GetArrayFromImage(mri_tra_s).T
|
Reference in New Issue
Block a user