本文整理汇总了Python中keras.backend.binary_crossentropy方法的典型用法代码示例。如果您正苦于以下问题:Python backend.binary_crossentropy方法的具体用法?Python backend.binary_crossentropy怎么用?Python backend.binary_crossentropy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.binary_crossentropy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: vae_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def vae_loss(self, x, x_decoded_mean):
xent_loss = K.sum(K.binary_crossentropy(x_decoded_mean, x), axis=-1)
kl_loss = - 0.5 * K.sum(1 + self.z_log_var - K.square(self.z_mean) - K.exp(self.z_log_var), axis=-1)
return xent_loss + kl_loss
# def weighted_vae_loss(self, feature_weights):
# def loss(y_true, y_pred):
# try:
# x = K.binary_crossentropy(y_pred, y_true)
# y = tf.Variable(feature_weights.astype('float32'))
# # y2 = y_true / K.sum(y_true)
# # import pdb;pdb.set_trace()
# xent_loss = K.dot(x, y)
# kl_loss = - 0.5 * K.sum(1 + self.z_log_var - K.square(self.z_mean) - K.exp(self.z_log_var), axis=-1)
# except Exception as e:
# print e
# import pdb;pdb.set_trace()
# return xent_loss + kl_loss
# return loss
示例2: online_bootstrapping
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def online_bootstrapping(y_true, y_pred, pixels=512, threshold=0.5):
""" Implements nline Bootstrapping crossentropy loss, to train only on hard pixels,
see https://arxiv.org/abs/1605.06885 Bridging Category-level and Instance-level Semantic Image Segmentation
The implementation is a bit different as we use binary crossentropy instead of softmax
SUPPORTS ONLY MINIBATCH WITH 1 ELEMENT!
# Arguments
y_true: A tensor with labels.
y_pred: A tensor with predicted probabilites.
pixels: number of hard pixels to keep
threshold: confidence to use, i.e. if threshold is 0.7, y_true=1, prediction=0.65 then we consider that pixel as hard
# Returns
Mean loss value
"""
y_true = K.flatten(y_true)
y_pred = K.flatten(y_pred)
difference = K.abs(y_true - y_pred)
values, indices = K.tf.nn.top_k(difference, sorted=True, k=pixels)
min_difference = (1 - threshold)
y_true = K.tf.gather(K.gather(y_true, indices), K.tf.where(values > min_difference))
y_pred = K.tf.gather(K.gather(y_pred, indices), K.tf.where(values > min_difference))
return K.mean(K.binary_crossentropy(y_true, y_pred))
示例3: bce_border
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def bce_border(y_true, y_pred):
border = get_border_mask((21, 21), y_true)
border = K.flatten(border)
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
y_true_f = K.tf.gather(y_true_f, K.tf.where(border > 0.5))
y_pred_f = K.tf.gather(y_pred_f, K.tf.where(border > 0.5))
return binary_crossentropy(y_true_f, y_pred_f)
示例4: make_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def make_loss(loss_name):
if loss_name == 'crossentropy':
return K.binary_crossentropy
elif loss_name == 'crossentropy_boot':
def loss(y, p):
return bootstrapped_crossentropy(y, p, 'hard', 0.9)
return loss
elif loss_name == 'dice':
return dice_coef_loss
elif loss_name == 'bce_dice':
def loss(y, p):
return dice_coef_loss_bce(y, p, dice=0.8, bce=0.2, bootstrapping='soft', alpha=1)
return loss
elif loss_name == 'boot_soft':
def loss(y, p):
return dice_coef_loss_bce(y, p, dice=0.8, bce=0.2, bootstrapping='soft', alpha=0.95)
return loss
elif loss_name == 'boot_hard':
def loss(y, p):
return dice_coef_loss_bce(y, p, dice=0.8, bce=0.2, bootstrapping='hard', alpha=0.95)
return loss
elif loss_name == 'online_bootstrapping':
def loss(y, p):
return online_bootstrapping(y, p, pixels=512 * 64, threshold=0.7)
return loss
elif loss_name == 'dice_coef_loss_border':
return dice_coef_loss_border
elif loss_name == 'bce_dice_loss_border':
return bce_dice_loss_border
else:
ValueError("Unknown loss.")
示例5: rpn_loss_cls
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def rpn_loss_cls(num_anchors):
def rpn_loss_cls_fixed_num(y_true, y_pred):
if K.image_dim_ordering() == 'tf':
return lambda_rpn_class * K.sum(y_true[:, :, :, :num_anchors] * K.binary_crossentropy(y_pred[:, :, :, :], y_true[:, :, :, num_anchors:])) / K.sum(epsilon + y_true[:, :, :, :num_anchors])
else:
return lambda_rpn_class * K.sum(y_true[:, :num_anchors, :, :] * K.binary_crossentropy(y_pred[:, :, :, :], y_true[:, num_anchors:, :, :])) / K.sum(epsilon + y_true[:, :num_anchors, :, :])
return rpn_loss_cls_fixed_num
示例6: mrcnn_mask_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def mrcnn_mask_loss_graph(target_masks, target_class_ids, pred_masks):
"""Mask binary cross-entropy loss for the masks head.
target_masks: [batch, num_rois, height, width].
A float32 tensor of values 0 or 1. Uses zero padding to fill array.
target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded.
pred_masks: [batch, proposals, height, width, num_classes] float32 tensor
with values from 0 to 1.
"""
# Reshape for simplicity. Merge first two dimensions into one.
target_class_ids = K.reshape(target_class_ids, (-1,))
mask_shape = tf.shape(target_masks)
target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3]))
pred_shape = tf.shape(pred_masks)
pred_masks = K.reshape(pred_masks,
(-1, pred_shape[2], pred_shape[3], pred_shape[4]))
# Permute predicted masks to [N, num_classes, height, width]
pred_masks = tf.transpose(pred_masks, [0, 3, 1, 2])
# Only positive ROIs contribute to the loss. And only
# the class specific mask of each ROI.
positive_ix = tf.where(target_class_ids > 0)[:, 0]
positive_class_ids = tf.cast(
tf.gather(target_class_ids, positive_ix), tf.int64)
indices = tf.stack([positive_ix, positive_class_ids], axis=1)
# Gather the masks (predicted and true) that contribute to loss
y_true = tf.gather(target_masks, positive_ix)
y_pred = tf.gather_nd(pred_masks, indices)
# Compute binary cross entropy. If no positive ROIs, then return 0.
# shape: [batch, roi, num_classes]
loss = K.switch(tf.size(y_true) > 0,
K.binary_crossentropy(target=y_true, output=y_pred),
tf.constant(0.0))
loss = K.mean(loss)
return loss
############################################################
# Data Generator
############################################################
示例7: rpn_loss_cls
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def rpn_loss_cls(num_anchors):
def rpn_loss_cls_fixed_num(y_true, y_pred):
if K.image_dim_ordering() == 'tf':
return lambda_rpn_class * K.sum(y_true[:, :, :, :num_anchors] * K.binary_crossentropy(y_pred[:, :, :, :], y_true[:, :, :, num_anchors:])) / K.sum(epsilon + y_true[:, :, :, :num_anchors])
else:
return lambda_rpn_class * K.sum(y_true[:, :num_anchors, :, :] * K.binary_crossentropy(y_pred[:, :, :, :], y_true[:, num_anchors:, :, :])) / K.sum(epsilon + y_true[:, :num_anchors, :, :])
return rpn_loss_cls_fixed_num
示例8: load_ref_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def load_ref_model(model_file=None):
"""Loads the Chemnet model. If called without arguments it will use the
model in the package. In case you want to use a different one provide the path
Args:
model_file: Path to model. (default=None)
"""
if model_file is None:
chemnet_model_filename = 'ChemNet_v0.13_pretrained.h5'
model_bytes = pkgutil.get_data('fcd', chemnet_model_filename)
tmpdir = tempfile.TemporaryDirectory()
model_file = os.path.join(tmpdir.name, chemnet_model_filename)
with open(model_file, 'wb') as f:
f.write(model_bytes)
masked_loss_function = build_masked_loss(K.binary_crossentropy, 0.5)
model = load_model(
model_file,
custom_objects={
'masked_loss_function': masked_loss_function,
'masked_accuracy': masked_accuracy})
model.pop()
model.pop()
return model
示例9: contractive_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def contractive_loss(model, lam=1e-4):
def loss(y_true, y_pred):
ent_loss = K.mean(K.binary_crossentropy(y_pred, y_true), axis=-1)
W = K.variable(value=model.encoder.get_weights()[0]) # N x N_hidden
W = K.transpose(W) # N_hidden x N
h = model.encoder.output
dh = h * (1 - h) # N_batch x N_hidden
# N_batch x N_hidden * N_hidden x 1 = N_batch x 1
contractive = lam * K.sum(dh**2 * K.sum(W**2, axis=1), axis=1)
return ent_loss + contractive
return loss
示例10: weighted_binary_crossentropy
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def weighted_binary_crossentropy(feature_weights):
def loss(y_true, y_pred):
# try:
# x = K.binary_crossentropy(y_pred, y_true)
# # y = tf.Variable(feature_weights.astype('float32'))
# # z = K.dot(x, y)
# y_true = tf.pow(y_true + 1e-5, .75)
# y2 = tf.div(y_true, tf.reshape(K.sum(y_true, 1), [-1, 1]))
# z = K.sum(tf.mul(x, y2), 1)
# except Exception as e:
# print e
# import pdb;pdb.set_trace()
# return z
return K.dot(K.binary_crossentropy(y_pred, y_true), K.variable(feature_weights.astype('float32')))
return loss
示例11: rpn_loss_cls
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def rpn_loss_cls(num_anchors):
def rpn_loss_cls_fixed_num(y_true, y_pred):
if K.image_dim_ordering() == 'tf':
return lambda_rpn_class * K.sum(y_true[:, :, :, :num_anchors] * K.binary_crossentropy(y_true[:, :, :, num_anchors:],y_pred[:, :, :, :])) / K.sum(epsilon + y_true[:, :, :, :num_anchors])
else:
return lambda_rpn_class * K.sum(y_true[:, :num_anchors, :, :] * K.binary_crossentropy(y_true[:, num_anchors:, :, :],y_pred[:, :, :, :])) / K.sum(epsilon + y_true[:, :num_anchors, :, :])
return rpn_loss_cls_fixed_num
示例12: mrcnn_mask_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def mrcnn_mask_loss_graph(target_masks, target_class_ids, pred_masks):
'''Mask binary cross-entropy loss for the masks head.
target_masks: [batch, num_rois, height, width].
A float32 tensor of values 0 or 1. Uses zero padding to fill array.
target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded.
pred_masks: [batch, proposals, height, width, num_classes] float32 tensor
with values from 0 to 1.
'''
# Reshape for simplicity. Merge first two dimensions into one.
target_class_ids = K.reshape(target_class_ids, (-1,))
mask_shape = tf.shape(target_masks)
target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3]))
pred_shape = tf.shape(pred_masks)
pred_masks = K.reshape(pred_masks,
(-1, pred_shape[2], pred_shape[3], pred_shape[4]))
# Permute predicted masks to [N, num_classes, height, width]
pred_masks = tf.transpose(pred_masks, [0, 3, 1, 2])
# Only positive ROIs contribute to the loss. And only
# the class specific mask of each ROI.
positive_ix = tf.where(target_class_ids > 0)[:, 0]
positive_class_ids = tf.cast(
tf.gather(target_class_ids, positive_ix), tf.int64)
indices = tf.stack([positive_ix, positive_class_ids], axis=1)
# Gather the masks (predicted and true) that contribute to loss
y_true = tf.gather(target_masks, positive_ix)
y_pred = tf.gather_nd(pred_masks, indices)
# Compute binary cross entropy. If no positive ROIs, then return 0.
# shape: [batch, roi, num_classes]
loss = K.switch(tf.size(y_true) > 0,
K.binary_crossentropy(target=y_true, output=y_pred),
tf.constant(0.0))
loss = K.mean(loss)
return loss
示例13: head_mask_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def head_mask_loss(gt_masks, gt_labels, pred_masks):
"""マスクの損失関数
gt_masks: 正解データ。
マスクデータをbboxの領域のみ切り抜いてconfig.mask_out_shapeにリサイズしたデータ。
[N, R, h, w]
バイナリマスク
gt_labels: 正解データのラベルID
[N, R]
pred_masks: 予測値
バイナリマスク
[N, R, n_labels h, w]
※h, w は config.mask_out_shape になる。
"""
# Positiveなラベルが付与されているRoIのみ評価対象とする
pos_idx = tf.where(gt_labels > 0)
i = K.cast(pos_idx[:, 0], tf.int32)
j = K.cast(pos_idx[:, 1], tf.int32)
k = K.cast(tf.gather_nd(gt_labels, pos_idx), tf.int32)
# i = log.tfprint(i, "i:head_mask_loss")
# j = log.tfprint(j, "j:head_mask_loss")
# k = log.tfprint(k, "k:head_mask_loss")
pos_pred_idx = K.stack((i, j, k), axis=1)
# pos_pred_idx = log.tfprint(pos_pred_idx, "pos_pred_idx:head_mask_loss")
pred_masks = tf.gather_nd(pred_masks, pos_pred_idx)
gt_masks = tf.gather_nd(gt_masks, pos_idx)
loss = K.switch(tf.size(gt_masks) > 0,
K.binary_crossentropy(gt_masks, pred_masks),
tf.constant(0.0))
loss = K.mean(loss)
loss = log.tfprint(loss, "head_mask_loss")
return loss
示例14: hard_mining_entropy
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def hard_mining_entropy(k):
"""
Compute binary cross-entropy for collision evaluation and hard-mining.
# Arguments
k: Number of samples for hard-mining.
# Returns
custom_bin_crossentropy: average binary cross-entropy for the current batch.
"""
def custom_bin_crossentropy(y_true, y_pred):
# Parameter t indicates the type of experiment
t = y_true[:,0]
# Number of collision samples
samples_coll = tf.cast(tf.equal(t,0), tf.int32)
n_samples_coll = tf.reduce_sum(samples_coll)
if n_samples_coll == 0:
return 0.0
else:
# Predicted and real labels
pred_coll = tf.squeeze(y_pred, squeeze_dims=-1)
true_coll = y_true[:,1]
# Collision loss
l_coll = tf.multiply((1-t), K.binary_crossentropy(true_coll, pred_coll))
# Hard mining
k_min = tf.minimum(k, n_samples_coll)
_, indices = tf.nn.top_k(l_coll, k=k_min)
max_l_coll = tf.gather(l_coll, indices)
hard_l_coll = tf.divide(tf.reduce_sum(max_l_coll), tf.cast(k, tf.float32))
return hard_l_coll
return custom_bin_crossentropy
示例15: rpn_loss_cls
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import binary_crossentropy [as 别名]
def rpn_loss_cls(num_anchors):
def rpn_loss_cls_fixed_num(y_true, y_pred):
return lambda_rpn_class * K.sum(y_true[:, :num_anchors, :, :] * K.binary_crossentropy(y_pred[:, :, :, :], y_true[:, num_anchors:, :, :])) / 256.0
return rpn_loss_cls_fixed_num