def dice(y_true, y_pred):
"""
Computes the Sorensen-Dice metric
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
y_sum = K.sum(y_true * y_pred_decision)
return (2. * y_sum + K.epsilon()) / (K.sum(y_true) + K.sum(y_pred_decision) + K.epsilon())
python类floor()的实例源码
def cyclic_learning_rate(
learning_rate_min,
learning_rate_max,
step_size,
global_step,
mode='triangular',
scope=None):
with tf.variable_scope(scope, 'CyclicLearningRate'):
cycle = tf.floor(1 + tf.to_float(global_step) / (2 * step_size))
if mode == 'triangular':
scale = 1
elif mode == 'triangular2':
scale = 2**(cycle - 1)
else:
raise ValueError('Unrecognized mode: {}'.format(mode))
x = tf.abs(tf.to_float(global_step) / step_size - 2 * cycle + 1)
lr = learning_rate_min + (learning_rate_max - learning_rate_min) * \
tf.maximum(0.0, 1 - x) / scale
return lr
def fixed_dropout(xs, keep_prob, noise_shape, seed=None):
"""
Apply dropout with same mask over all inputs
Args:
xs: list of tensors
keep_prob:
noise_shape:
seed:
Returns:
list of dropped inputs
"""
with tf.name_scope("dropout", values=xs):
noise_shape = noise_shape
# uniform [keep_prob, 1.0 + keep_prob)
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape, seed=seed, dtype=xs[0].dtype)
# 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob)
binary_tensor = tf.floor(random_tensor)
outputs = []
for x in xs:
ret = tf.div(x, keep_prob) * binary_tensor
ret.set_shape(x.get_shape())
outputs.append(ret)
return outputs
def mu_law(x, mu=255, int8=False):
"""A TF implementation of Mu-Law encoding.
Args:
x: The audio samples to encode.
mu: The Mu to use in our Mu-Law.
int8: Use int8 encoding.
Returns:
out: The Mu-Law encoded int8 data.
"""
out = tf.sign(x) * tf.log(1 + mu * tf.abs(x)) / np.log(1 + mu)
out = tf.floor(out * 128)
if int8:
out = tf.cast(out, tf.int8)
return out
def blend_layers(control, shape, feather=1.0, *layers):
layer_count = len(layers)
control = normalize(control)
control *= layer_count
control_floor = tf.cast(control, tf.int32)
x_index = row_index(shape)
y_index = column_index(shape)
layers = tf.stack(list(layers) + [layers[-1]])
layer_count += 1
floor_values = control_floor[:, :, 0]
# I'm not sure why the mod operation is needed, but tensorflow-cpu explodes without it.
combined_layer_0 = tf.gather_nd(layers, tf.stack([floor_values % layer_count, y_index, x_index], 2))
combined_layer_1 = tf.gather_nd(layers, tf.stack([(floor_values + 1) % layer_count, y_index, x_index], 2))
control_floor_fract = control - tf.floor(control)
control_floor_fract = tf.minimum(tf.maximum(control_floor_fract - (1.0 - feather), 0.0) / feather, 1.0)
return blend(combined_layer_0, combined_layer_1, control_floor_fract)
def posterize(tensor, levels):
"""
Reduce the number of color levels per channel.
:param Tensor tensor:
:param int levels:
:return: Tensor
"""
tensor *= levels
tensor = tf.floor(tensor)
tensor /= levels
return tensor
def _quantize(x, params, randomize=True):
"""Quantize x according to params, optionally randomizing the rounding."""
if not params.quantize:
return x
if not randomize:
return tf.bitcast(
tf.cast(x / params.quantization_scale, tf.int16), tf.float16)
abs_x = tf.abs(x)
sign_x = tf.sign(x)
y = abs_x / params.quantization_scale
y = tf.floor(y + tf.random_uniform(tf.shape(x)))
y = tf.minimum(y, tf.int16.max) * sign_x
q = tf.bitcast(tf.cast(y, tf.int16), tf.float16)
return q
def _quantize(x, params, randomize=True):
"""Quantize x according to params, optionally randomizing the rounding."""
if not params.quantize:
return x
if not randomize:
return tf.bitcast(
tf.cast(x / params.quantization_scale, tf.int16), tf.float16)
abs_x = tf.abs(x)
sign_x = tf.sign(x)
y = abs_x / params.quantization_scale
y = tf.floor(y + tf.random_uniform(common_layers.shape_list(x)))
y = tf.minimum(y, tf.int16.max) * sign_x
q = tf.bitcast(tf.cast(y, tf.int16), tf.float16)
return q
def feature_sharing(features):
"""Feature sharing operation.
Args:
features: List of hidden features from models.
"""
nmodel = len(features)
with tf.variable_scope('feature_sharing'):
shape = features[0].get_shape()
output = [0.]*nmodel
for from_idx in range(nmodel):
for to_idx in range(nmodel):
if from_idx == to_idx:
# don't drop hidden features within a model.
mask = 1.
else:
# randomly drop features to share with another model.
mask = tf.floor(0.7 + tf.random_uniform(shape))
output[to_idx] += mask * features[from_idx]
return output
def dropout(x, pkeep, phase=None, mask=None):
mask = tf.floor(pkeep + tf.random_uniform(tf.shape(x))) if mask is None else mask
if phase is None:
return mask * x
else:
return switch(phase, mask*x, pkeep*x)
def dice_1(y):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_true, y_pred = y
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :,1]
mask_pred = y_pred_decision[:, :, :, :, 1]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_2(y):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_true, y_pred = y
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 2]
mask_pred = y_pred_decision[:, :, :, :, 2]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_3(y):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,0
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_true, y_pred = y
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 3]
mask_pred = y_pred_decision[:, :, :, :, 3]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_coef(y_true, y_pred):
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred_decision)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + K.epsilon()) / (K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())
def dice_core(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
mask_true1 = y_true[:, :, :, :, 3:]
mask_true2 = y_true[:, :, :, :, 1:2]
mask_true = K.sum(K.concatenate([mask_true1, mask_true2], axis=4), axis=4)
mask_pred1 = y_pred_decision[:, :, :, :, 3:]
mask_pred2 = y_pred_decision[:, :, :, :, 1:2]
mask_pred = K.sum(K.concatenate([mask_pred1, mask_pred2], axis=4), axis=4)
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_enhance(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 3]
mask_pred = y_pred_decision[:, :, :, :, 3]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
# def accuracy_survival(y_true, y_predicted):
def dice_whole_mod(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,0
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
# mask = K.expand_dims(K.sum(y_true,axis=4),axis=4)
# cmp_mask = K.concatenate([K.ones_like(mask) - mask,K.zeros_like(mask), K.zeros_like(mask)],axis=4)
# y_pred = y_pred + cmp_mask
y_true = y_true[:,:,:,:,:3]
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
mask_true = K.sum(y_true, axis=4)
mask_pred = K.sum(y_pred_decision, axis=4) * K.sum(y_true, axis=4)
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_core_mod(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_true = y_true[:,:,:,:,:3]
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
y_pred_decision = tf.where(tf.is_nan(y_pred_decision), tf.zeros_like(y_pred_decision), y_pred_decision)
mask_true1 = K.expand_dims(y_true[:, :, :, :, 2],axis=4)
mask_true2 = K.expand_dims(y_true[:, :, :, :, 0],axis=4)
mask_true = K.sum(K.concatenate([mask_true1, mask_true2], axis=4), axis=4)
mask_pred1 = K.expand_dims(y_pred_decision[:, :, :, :, 2],axis=4)
mask_pred2 = K.expand_dims(y_pred_decision[:, :, :, :, 0],axis=4)
mask_pred = K.sum(K.concatenate([mask_pred1, mask_pred2], axis=4), axis=4) * K.sum(y_true, axis=4)
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_enhance_mod(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_true = y_true[:,:,:,:,:3]
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
# y_pred_decision = tf.where(tf.is_nan(y_pred_decision), tf.zeros_like(y_pred_decision), y_pred_decision)
mask_true = y_true[:, :, :, :, 2]
mask_pred = y_pred_decision[:, :, :, :, 2] * K.sum(y_true, axis=4)
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_1(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :,1]
mask_pred = y_pred_decision[:, :, :, :, 1]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_1_2D(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor((y_pred + K.epsilon())/ K.max(y_pred, axis=2, keepdims=True))
mask_true = y_true[:, :, 1]
mask_pred = y_pred_decision[:, :, 1]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_2(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 2]
mask_pred = y_pred_decision[:, :, :, :, 2]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_3(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,0
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 3]
mask_pred = y_pred_decision[:, :, :, :, 3]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def dice_4(y_true, y_pred):
"""
Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5
TP
Dice = 2 -------
T + P
Parameters
----------
y_true : keras.placeholder
Placeholder that contains the ground truth labels of the classes
y_pred : keras.placeholder
Placeholder that contains the class prediction
Returns
-------
scalar
Dice metric
"""
y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 4]
mask_pred = y_pred_decision[:, :, :, :, 4]
y_sum = K.sum(mask_true * mask_pred)
return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def precision_1(y_true, y_pred):
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 1]
mask_pred = y_pred_decision[:, :, :, :, 1]
y_sum = K.sum(mask_true * mask_pred)
return (y_sum + K.epsilon()) / (K.sum(mask_pred) + K.epsilon())
def precision_2(y_true, y_pred):
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 2]
mask_pred = y_pred_decision[:, :, :, :, 2]
y_sum = K.sum(mask_true * mask_pred)
return (y_sum + K.epsilon()) / (K.sum(mask_pred) + K.epsilon())
def precision_3(y_true, y_pred):
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 3]
mask_pred = y_pred_decision[:, :, :, :, 3]
y_sum = K.sum(mask_true * mask_pred)
return (y_sum + K.epsilon()) / (K.sum(mask_pred) + K.epsilon())
def precision_4(y_true, y_pred):
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 4]
mask_pred = y_pred_decision[:, :, :, :, 4]
y_sum = K.sum(mask_true * mask_pred)
return (y_sum + K.epsilon()) / (K.sum(mask_pred) + K.epsilon())
def recall_0(y_true, y_pred):
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 0]
mask_pred = y_pred_decision[:, :, :, :, 0]
y_sum = K.sum(mask_true * mask_pred)
return (y_sum + K.epsilon()) / (K.sum(mask_true) + K.epsilon())
def recall_2(y_true, y_pred):
y_pred_decision = tf.floor(y_pred / K.max(y_pred, axis=4, keepdims=True))
mask_true = y_true[:, :, :, :, 2]
mask_pred = y_pred_decision[:, :, :, :, 2]
y_sum = K.sum(mask_true * mask_pred)
return (y_sum + K.epsilon()) / (K.sum(mask_true) + K.epsilon())