Unverified Commit 86c03747 authored by Pavel Yakubovskiy's avatar Pavel Yakubovskiy Committed by GitHub

Remove reduce_sum from crossentropy

parent ef635038
import keras.backend as K
from keras.losses import binary_crossentropy
from keras.losses import categorical_crossentropy
from keras.utils.generic_utils import get_custom_objects
from .metrics import jaccard_score, f_score
......@@ -13,6 +12,24 @@ __all__ = [
]
def _categorical_crossentropy(target, output, axis=-1):
"""Categorical crossentropy between an output tensor and a target tensor.
# Arguments
target: A tensor of the same shape as `output`.
output: A tensor resulting from a softmax
(unless `from_logits` is True, in which
case `output` is expected to be the logits).
# Returns
Output tensor.
"""
# scale preds so that the class probas of each sample sum to 1
output /= K.sum(output, axis=axis, keepdims=True)
# manual computation of crossentropy
output = K.clip(output, K.epsilon(), 1. - K.epsilon())
return - target * K.log(output)
# ============================== Jaccard Losses ==============================
def jaccard_loss(gt, pr, class_weights=1., smooth=SMOOTH, per_image=True):
......@@ -74,7 +91,7 @@ def cce_jaccard_loss(gt, pr, cce_weight=1., class_weights=1., smooth=SMOOTH, per
loss
"""
cce = categorical_crossentropy(gt, pr) * class_weights
cce = _categorical_crossentropy(gt, pr) * class_weights
cce = K.mean(cce)
return cce_weight * cce + jaccard_loss(gt, pr, smooth=smooth, class_weights=class_weights, per_image=per_image)
......@@ -152,7 +169,7 @@ def cce_dice_loss(gt, pr, cce_weight=1., class_weights=1., smooth=SMOOTH, per_im
loss
"""
cce = categorical_crossentropy(gt, pr) * class_weights
cce = _categorical_crossentropy(gt, pr) * class_weights
cce = K.mean(cce)
return cce_weight * cce + dice_loss(gt, pr, smooth=smooth, class_weights=class_weights, per_image=per_image, beta=beta)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment