Python keras.backend.binary_crossentropy() Examples
The following are 30
code examples of keras.backend.binary_crossentropy().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.backend
, or try the search function
.
Example #1
Source File: vae.py From KATE with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #2
Source File: losses.py From kaggle-carvana-2017 with MIT License | 5 votes |
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)
Example #3
Source File: loss_func.py From Keras-FCN with MIT License | 5 votes |
def fcn_xent_nobg(y_true, y_pred): y_true = y_true[:,:,:,1:] y_pred = y_pred[:,:,:,1:] y_true_reshaped = K.flatten(y_true) y_pred_reshaped = K.flatten(y_pred) return K.binary_crossentropy(y_pred_reshaped, y_true_reshaped)
Example #4
Source File: loss_func.py From Keras-FCN with MIT License | 5 votes |
def fcn_xent(y_true, y_pred): y_true_reshaped = K.flatten(y_true) y_pred_reshaped = K.flatten(y_pred) return K.binary_crossentropy(y_pred_reshaped, y_true_reshaped)
Example #5
Source File: losses.py From ssbm_fox_detector with MIT License | 5 votes |
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
Example #6
Source File: losses.py From deephar with MIT License | 5 votes |
def pose_regression_loss(pose_loss, visibility_weight): def _pose_regression_loss(y_true, y_pred): video_clip = K.ndim(y_true) == 4 if video_clip: """The model was time-distributed, so there is one additional dimension. """ p_true = y_true[:, :, :, 0:-1] p_pred = y_pred[:, :, :, 0:-1] v_true = y_true[:, :, :, -1] v_pred = y_pred[:, :, :, -1] else: p_true = y_true[:, :, 0:-1] p_pred = y_pred[:, :, 0:-1] v_true = y_true[:, :, -1] v_pred = y_pred[:, :, -1] if pose_loss == 'l1l2': ploss = elasticnet_loss_on_valid_joints(p_true, p_pred) elif pose_loss == 'l1': ploss = l1_loss_on_valid_joints(p_true, p_pred) elif pose_loss == 'l2': ploss = l2_loss_on_valid_joints(p_true, p_pred) elif pose_loss == 'l1l2bincross': ploss = elasticnet_bincross_loss_on_valid_joints(p_true, p_pred) else: raise Exception('Invalid pose_loss option ({})'.format(pose_loss)) vloss = binary_crossentropy(v_true, v_pred) if video_clip: """If time-distributed, average the error on video frames.""" vloss = K.mean(vloss, axis=-1) ploss = K.mean(ploss, axis=-1) return ploss + visibility_weight*vloss return _pose_regression_loss
Example #7
Source File: losses.py From deephar with MIT License | 5 votes |
def elasticnet_bincross_loss_on_valid_joints(y_true, y_pred): idx = K.cast(K.greater(y_true, 0.), 'float32') num_joints = K.clip(K.sum(idx, axis=(-1, -2)), 1, None) l1 = K.abs(y_pred - y_true) l2 = K.square(y_pred - y_true) bc = 0.01*K.binary_crossentropy(y_true, y_pred) dummy = 0. * y_pred return K.sum(tf.where(K.cast(idx, 'bool'), l1 + l2 + bc, dummy), axis=(-1, -2)) / num_joints
Example #8
Source File: stage2.py From Generative-Adversarial-Networks-Projects with MIT License | 5 votes |
def custom_generator_loss(y_true, y_pred): # Calculate binary cross entropy loss return K.binary_crossentropy(y_true, y_pred)
Example #9
Source File: stage1.py From Generative-Adversarial-Networks-Projects with MIT License | 5 votes |
def custom_generator_loss(y_true, y_pred): # Calculate binary cross entropy loss return K.binary_crossentropy(y_true, y_pred)
Example #10
Source File: losses.py From keras-frcnn with Apache License 2.0 | 5 votes |
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
Example #11
Source File: utils.py From rpg_public_dronet with MIT License | 5 votes |
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
Example #12
Source File: loss.py From maskrcnn with MIT License | 5 votes |
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
Example #13
Source File: losses.py From keras-faster-rcnn with Apache License 2.0 | 5 votes |
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
Example #14
Source File: losses.py From image-segmentation with MIT License | 5 votes |
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
Example #15
Source File: losses.py From Keras_object_detection with Apache License 2.0 | 5 votes |
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
Example #16
Source File: keras_utils.py From KATE with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #17
Source File: losses.py From keras-frcnn with Apache License 2.0 | 5 votes |
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
Example #18
Source File: model.py From dataiku-contrib with Apache License 2.0 | 5 votes |
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 ############################################################
Example #19
Source File: losses.py From FasterRCNN_KERAS with Apache License 2.0 | 5 votes |
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
Example #20
Source File: losses.py From Keras-FasterRCNN with MIT License | 5 votes |
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
Example #21
Source File: losses.py From kaggle-carvana-2017 with MIT License | 5 votes |
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.")
Example #22
Source File: FCD.py From FCD with GNU Lesser General Public License v3.0 | 5 votes |
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
Example #23
Source File: losses.py From kaggle-carvana-2017 with MIT License | 5 votes |
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))
Example #24
Source File: keras_utils.py From KATE with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #25
Source File: model.py From PyTorch-Luna16 with Apache License 2.0 | 4 votes |
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) loss = K.reshape(loss, [1, 1]) return loss ############################################################ # Data Generator ############################################################
Example #26
Source File: MaskRCNN.py From PyTorch-Luna16 with Apache License 2.0 | 4 votes |
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) loss = K.reshape(loss, [1, 1]) return loss ############################################################ # Data Generator ############################################################
Example #27
Source File: model_inceptionresnet.py From cvpr-2018-autonomous-driving-autopilot-solution with MIT License | 4 votes |
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 ############################################################
Example #28
Source File: model.py From ocrd_anybaseocr with Apache License 2.0 | 4 votes |
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 ############################################################
Example #29
Source File: model.py From latte with Apache License 2.0 | 4 votes |
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) loss = K.reshape(loss, [1, 1]) return loss ############################################################ # Data Generator ############################################################
Example #30
Source File: model.py From PanopticSegmentation with MIT License | 4 votes |
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 ############################################################