|
import numpy as np |
|
import tensorflow as tf |
|
|
|
|
|
def min_max_norm(img: np.ndarray, out_max_val=1.): |
|
out_img = img |
|
max_val = np.amax(img) |
|
min_val = np.amin(img) |
|
if (max_val - min_val) != 0: |
|
out_img = (img - min_val) / (max_val - min_val) |
|
return out_img * out_max_val |
|
|
|
|
|
def soft_threshold(x, threshold, name='soft_threshold'): |
|
|
|
|
|
|
|
|
|
with tf.name_scope(name): |
|
x = tf.convert_to_tensor(x, name='x') |
|
threshold = tf.convert_to_tensor(threshold, dtype=x.dtype, name='threshold') |
|
return tf.sign(x) * tf.maximum(tf.abs(x) - threshold, 0.) |
|
|
|
|
|
def hard_threshold(x, threshold, name='hard_threshold'): |
|
with tf.name_scope(name): |
|
threshold = tf.convert_to_tensor(threshold, dtype=x.dtype, name='threshold') |
|
return tf.sign(tf.maximum(tf.abs(x) - threshold, 0.)) |
|
|
|
|
|
def binary_activation(x): |
|
|
|
cond = tf.less(x, tf.zeros(tf.shape(x))) |
|
out = tf.where(cond, tf.zeros(tf.shape(x)), tf.ones(tf.shape(x))) |
|
|
|
return out |
|
|
|
|
|
def gaussian_kernel(kernel_size, sigma, in_ch, out_ch, dim, dtype=tf.float32): |
|
|
|
x = tf.range(-kernel_size // 2 + 1, kernel_size // 2 + 1, dtype=dtype) |
|
g = tf.math.exp(-(tf.pow(x, 2) / (2 * tf.pow(tf.cast(sigma, dtype), 2)))) |
|
|
|
g_kernel = tf.identity(g) |
|
g_kernel = tf.tensordot(g_kernel, g, 0) |
|
g_kernel = tf.tensordot(g_kernel, g, 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g_kernel = g_kernel / tf.reduce_sum(g_kernel) |
|
g_kernel = tf.expand_dims(tf.expand_dims(g_kernel, axis=-1), axis=-1) |
|
return tf.tile(g_kernel, (*(1,)*dim, in_ch, out_ch)) |
|
|
|
|
|
def sample_unique(population, samples, tout=tf.int32): |
|
|
|
z = -tf.log(-tf.log(tf.random_uniform((tf.shape(population)[0],), 0, 1))) |
|
_, indices = tf.nn.top_k(z, samples) |
|
ret_val = tf.gather(population, indices) |
|
return tf.cast(ret_val, tout) |
|
|
|
|
|
def safe_medpy_metric(prediction, reference, nb_labels, metric_fnc, fnc_args: dict={}): |
|
vals = list() |
|
if 'voxelspacing' in fnc_args.keys(): |
|
diag = np.power(reference.shape[:-1] * fnc_args['voxelspacing'], 2) |
|
else: |
|
diag = np.power(reference.shape[:-1], 2) |
|
diag = np.sqrt(np.sum(diag)) |
|
for l in range(nb_labels): |
|
try: |
|
vals.append(metric_fnc(prediction[..., l], reference[..., l], **fnc_args)) |
|
except RuntimeError: |
|
vals.append(diag) |
|
return vals |