本文整理汇总了Python中tensorflow.keras.losses.binary_crossentropy方法的典型用法代码示例。如果您正苦于以下问题:Python losses.binary_crossentropy方法的具体用法?Python losses.binary_crossentropy怎么用?Python losses.binary_crossentropy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.losses
的用法示例。
在下文中一共展示了losses.binary_crossentropy方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bce_jaccard_loss
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def bce_jaccard_loss(gt, pr, bce_weight=1., smooth=SMOOTH, per_image=True):
r"""Sum of binary crossentropy and jaccard losses:
.. math:: L(A, B) = bce_weight * binary_crossentropy(A, B) + jaccard_loss(A, B)
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights for jaccard loss, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, jaccard loss is calculated as mean over images in batch (B),
else over whole batch (only for jaccard loss)
Returns:
loss
"""
bce = K.mean(binary_crossentropy(gt, pr))
loss = bce_weight * bce + jaccard_loss(gt, pr, smooth=smooth, per_image=per_image)
return loss
示例2: bce_dice_loss
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def bce_dice_loss(gt, pr, bce_weight=1., smooth=SMOOTH, per_image=True, beta=1.):
r"""Sum of binary crossentropy and dice losses:
.. math:: L(A, B) = bce_weight * binary_crossentropy(A, B) + dice_loss(A, B)
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights for dice loss, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, dice loss is calculated as mean over images in batch (B),
else over whole batch
beta: coefficient for precision recall balance
Returns:
loss
"""
bce = K.mean(binary_crossentropy(gt, pr))
loss = bce_weight * bce + dice_loss(gt, pr, smooth=smooth, per_image=per_image, beta=beta)
return loss
示例3: variational_free_energy_loss
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def variational_free_energy_loss(model, scale_factor, kl_alpha):
"""Defines variational free energy loss.
Sum of KL divergence (supplied by tfp) and binary cross-entropy.
"""
# KL Divergence should be applied once per epoch only, so
# scale_factor should be num_samples / batch_size.
kl = sum(model.losses) / scale_factor
def loss(y_true, y_pred):
bce = binary_crossentropy(y_true, y_pred)
return bce + K.get_value(kl_alpha) * kl
return loss
示例4: loss
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def loss(y_true, y_pred):
def dice_coefficient(y_true, y_pred):
numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=-1)
denominator = tf.reduce_sum(y_true + y_pred, axis=-1)
return numerator / (denominator + epsilon())
return binary_crossentropy(y_true, y_pred) - tf.math.log(dice_coefficient(y_true, y_pred) + epsilon())
示例5: compile
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def compile(self) -> None:
optimizer = Adam(lr=self.learning_rate)
loss = losses.binary_crossentropy
self.model.compile(loss=loss, optimizer=optimizer)
示例6: compile
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def compile(self) -> None:
optimizer = Adam(lr=self.learning_rate)
if self.triplet_mode:
loss = self._triplet_loss
else:
loss = losses.binary_crossentropy
self.model.compile(loss=loss, optimizer=optimizer)
self.score_model = self.create_score_model()
示例7: bce_dice_loss
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def bce_dice_loss(y_true, y_pred):
loss = binary_crossentropy(y_true, y_pred) + dice_loss(y_true, y_pred)
return loss
# Focal Tversky loss, brought to you by: https://github.com/nabsabraham/focal-tversky-unet
示例8: YoloLoss
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def YoloLoss(anchors, num_classes=10, ignore_thresh=0.5):
def yolo_loss(y_true, y_pred):
# 1. transform all pred outputs
# y_pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...cls))
pred_box, pred_obj, pred_class, pred_xywh = yolo_boxes(y_pred, anchors, num_classes)
pred_xy = pred_xywh[..., 0:2]
pred_wh = pred_xywh[..., 2:4]
# 2. transform all true outputs
# y_true: (batch_size, grid, grid, anchors, (x1, y1, x2, y2, obj, cls))
true_box, true_obj, true_class_idx = tf.split(y_true, (4, 1, 1), axis=-1)
true_xy = (true_box[..., 0:2] + true_box[..., 2:4]) / 2.
true_wh = true_box[..., 2:4] - true_box[..., 0:2]
# give higher weights to small boxes
box_loss_scale = 2 - true_wh[..., 0] * true_wh[..., 1]
# 3. inverting the pred box equations
grid_size = tf.shape(y_pred)[1:3][::-1]
grid_y, grid_x = tf.shape(y_pred)[1], tf.shape(y_pred)[2]
grid = tf.meshgrid(tf.range(grid_x), tf.range(grid_y))
grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)
true_xy = true_xy * tf.cast(grid_size, tf.float32) - tf.cast(grid, tf.float32)
true_wh = tf.math.log(true_wh / anchors)
true_wh = tf.where(tf.math.is_inf(true_wh), tf.zeros_like(true_wh), true_wh)
# 4. calculate all masks
obj_mask = tf.squeeze(true_obj, -1)
# ignore false positive when iou is over threshold
true_box_flat = tf.boolean_mask(true_box, tf.cast(obj_mask, tf.bool))
best_iou = tf.reduce_max(broadcast_iou(pred_box, true_box_flat), axis=-1)
ignore_mask = tf.cast(best_iou < ignore_thresh, tf.float32)
# 5. calculate all losses
xy_loss = obj_mask * box_loss_scale * tf.reduce_sum(tf.square(true_xy - pred_xy), axis=-1)
wh_loss = obj_mask * box_loss_scale * tf.reduce_sum(tf.square(true_wh - pred_wh), axis=-1)
obj_loss = binary_crossentropy(true_obj, pred_obj)
obj_loss = obj_mask * obj_loss + (1 - obj_mask) * ignore_mask * obj_loss
class_loss = obj_mask * sparse_categorical_crossentropy(true_class_idx, pred_class)
# 6. sum over (batch, gridx, gridy, anchors) => (batch, 1)
xy_loss = tf.reduce_sum(xy_loss, axis=(1, 2, 3))
wh_loss = tf.reduce_sum(wh_loss, axis=(1, 2, 3))
obj_loss = tf.reduce_sum(obj_loss, axis=(1, 2, 3))
class_loss = tf.reduce_sum(class_loss, axis=(1, 2, 3))
loss = xy_loss + wh_loss + obj_loss + class_loss
return loss
return yolo_loss
示例9: detection_loss
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def detection_loss():
def get_box_highest_percentage(arr):
shape = tf.shape(arr)
reshaped = tf.reshape(arr, (shape[0], tf.reduce_prod(shape[1:-1]), -1))
# returns array containing the index of the highest percentage of each batch
# where 0 <= index <= height * width
max_prob_ind = tf.argmax(reshaped[...,-1], axis=-1, output_type=tf.int32)
# turn indices (batch, y * x) into (batch, y, x)
# returns (3, batch) tensor
unraveled = tf.unravel_index(max_prob_ind, shape[:-1])
# turn tensor into (batch, 3) and keep only (y, x)
unraveled = tf.transpose(unraveled)[:,1:]
y, x = unraveled[...,0], unraveled[...,1]
# stack indices and create (batch, 5) tensor which
# contains height, width, offset_y, offset_x, percentage
indices = tf.stack([tf.range(shape[0]), y, x], axis=-1)
box = tf.gather_nd(arr, indices)
y, x = tf.cast(y, tf.float32), tf.cast(x, tf.float32)
# transform box to (y + offset_y, x + offset_x, 7 * height, 7 * width, obj)
# output is (batch, 5)
out = tf.stack([y + box[...,2], x + box[...,3],
GRID_SIZE * box[...,0], GRID_SIZE * box[...,1],
box[...,-1]], axis=-1)
return out
def loss(y_true, y_pred):
# get the box with the highest percentage in each image
true_box = get_box_highest_percentage(y_true)
pred_box = get_box_highest_percentage(y_pred)
# object loss
obj_loss = binary_crossentropy(y_true[...,4:5], y_pred[...,4:5])
# mse with the boxes that have the highest percentage
box_loss = tf.reduce_sum(tf.math.squared_difference(true_box[...,:-1], pred_box[...,:-1]))
return tf.reduce_sum(obj_loss) + box_loss
return loss
示例10: k_weighted_bce
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def k_weighted_bce(y_true, y_pred, weight):
"""Weighted binary cross-entropy for Keras.
Arguments:
----------
y_true : ``tf.Tensor``
passed silently by Keras during model training.
y_pred : ``tf.Tensor``
passed silently by Keras during model training.
weight : :class:`float` or :class:`int`
Weight to assign to mask foreground pixels. Use values
>1 to over-weight foreground or 0<value<1 to under-weight foreground.
weight=1 is identical to vanilla binary cross-entropy.
Returns:
--------
The binary cross-entropy loss function output multiplied by a weighting
mask.
Usage:
------
Because Keras doesn't make it easy to implement loss functions that
take arguments beyond `y_true` and `y_pred `, this function's arguments
must be partially defined before passing it into your `model.compile`
command. See example below, modified from ternausnet.py::
model = Model(input=inputs, output=output_layer) # defined in ternausnet.py
loss_func = partial(weighted_bce, weight=loss_weight)
loss_func = update_wrapper(loss_func, weighted_bce)
model.compile(optimizer=Adam(), loss=loss_func)
If you wish to save and re-load a model which used this loss function,
you must pass the loss function as a custom object::
model.save('path_to_your_model.hdf5')
wbce_loss = partial(weighted_bce, weight=loss_weight)
wbce_loss = update_wrapper(wbce_loss, weighted_bce)
reloaded_model = keras.models.load_model(
'path_to_your_model.hdf5', custom_objects={'weighted_bce': wbce_loss}
)
"""
if weight == 1: # identical to vanilla bce
return K.binary_crossentropy(y_pred, y_true)
weight_mask = K.ones_like(y_true) # initialize weight mask
class_two = K.equal(y_true, weight_mask) # identify foreground pixels
class_two = K.cast(class_two, 'float32')
if weight < 1:
class_two = class_two*(1-weight)
final_mask = weight_mask - class_two # foreground pixels weighted
elif weight > 1:
class_two = class_two*(weight-1)
final_mask = weight_mask + class_two # foreground pixels weighted
return K.binary_crossentropy(y_pred, y_true) * final_mask
示例11: k_layered_weighted_bce
# 需要导入模块: from tensorflow.keras import losses [as 别名]
# 或者: from tensorflow.keras.losses import binary_crossentropy [as 别名]
def k_layered_weighted_bce(y_true, y_pred, weights):
"""Binary cross-entropy function with different weights for mask channels.
Arguments:
----------
y_true (tensor): passed silently by Keras during model training.
y_pred (tensor): passed silently by Keras during model training.
weights (list-like): Weights to assign to mask foreground pixels for each
channel in the 3rd axis of the mask.
Returns:
--------
The binary cross-entropy loss function output multiplied by a weighting
mask.
Usage:
------
See implementation instructions for `weighted_bce`.
This loss function is intended to allow different weighting of different
segmentation outputs - for example, if a model outputs a 3D image mask,
where the first channel corresponds to foreground objects and the second
channel corresponds to object edges. `weights` must be a list of length
equal to the depth of the output mask. The output mask's "z-axis"
corresponding to the mask channel must be the third axis in the output
array.
"""
weight_mask = K.ones_like(y_true)
submask_list = []
for i in range(len(weights)):
class_two = K.equal(y_true[:, :, :, i], weight_mask[:, :, :, i])
class_two = K.cast(class_two, 'float32')
if weights[i] < 1:
class_two = class_two*(1-weights[i])
layer_mask = weight_mask[:, :, :, i] - class_two
elif weights[i] > 1:
class_two = class_two*(weights[i]-1)
layer_mask = weight_mask[:, :, :, i] + class_two
else:
layer_mask = weight_mask[:, :, :, i]
submask_list.append(layer_mask)
final_mask = K.stack(submask_list, axis=-1)
return K.binary_crossentropy(y_pred, y_true) * final_mask