Python keras.backend.switch() Examples
The following are 30
code examples of keras.backend.switch().
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: model.py From i.ann.maskrcnn with GNU General Public License v2.0 | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for BG/FG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Cross entropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #2
Source File: model.py From dataiku-contrib with Apache License 2.0 | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Cross entropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #3
Source File: model.py From PanopticSegmentation with MIT License | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Cross entropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #4
Source File: model.py From EasyPR-python with Apache License 2.0 | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Crossentropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #5
Source File: keras_extensions.py From visual_turing_test-tutorial with MIT License | 6 votes |
def time_distributed_nonzero_max_pooling(x): """ Computes maximum along the first (time) dimension. It ignores the mask m. In: x - input; a 3D tensor mask_value - value to mask out, if None then no masking; by default 0.0, """ import theano.tensor as T mask_value=0.0 x = T.switch(T.eq(x, mask_value), -numpy.inf, x) masked_max_x = x.max(axis=1) # replace infinities with mask_value masked_max_x = T.switch(T.eq(masked_max_x, -numpy.inf), 0, masked_max_x) return masked_max_x
Example #6
Source File: core.py From text-detection-ocr with Apache License 2.0 | 6 votes |
def _rpn_loss_regr(y_true, y_pred): """ smooth L1 loss y_ture [1][HXWX10][3] (class,regr) y_pred [1][HXWX10][2] (reger) """ sigma = 9.0 cls = y_true[0, :, 0] regr = y_true[0, :, 1:3] regr_keep = tf.where(K.equal(cls, 1))[:, 0] regr_true = tf.gather(regr, regr_keep) regr_pred = tf.gather(y_pred[0], regr_keep) diff = tf.abs(regr_true - regr_pred) less_one = tf.cast(tf.less(diff, 1.0 / sigma), 'float32') loss = less_one * 0.5 * diff ** 2 * sigma + tf.abs(1 - less_one) * (diff - 0.5 / sigma) loss = K.sum(loss, axis=1) return K.switch(tf.size(loss) > 0, K.mean(loss), K.constant(0.0))
Example #7
Source File: model.py From segmentation-unet-maskrcnn with MIT License | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Crossentropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #8
Source File: keras_patches.py From keras-image-captioning with MIT License | 6 votes |
def clip_norm(g, c, n): if c > 0: if K.backend() == 'tensorflow': import tensorflow as tf import copy condition = n >= c then_expression = tf.scalar_mul(c / n, g) else_expression = g if hasattr(then_expression, 'get_shape'): g_shape = copy.copy(then_expression.get_shape()) elif hasattr(then_expression, 'dense_shape'): g_shape = copy.copy(then_expression.dense_shape) if condition.dtype != tf.bool: condition = tf.cast(condition, 'bool') g = K.tensorflow_backend.control_flow_ops.cond( condition, lambda: then_expression, lambda: else_expression) if hasattr(then_expression, 'get_shape'): g.set_shape(g_shape) elif hasattr(then_expression, 'dense_shape'): g._dense_shape = g_shape else: g = K.switch(n >= c, g * c / n, g) return g
Example #9
Source File: model.py From Mask-RCNN-Pedestrian-Detection with MIT License | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Crossentropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #10
Source File: model.py From DeepTL-Lane-Change-Classification with MIT License | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Crossentropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #11
Source File: evaluation_metrics_theano.py From deep-mlsa with Apache License 2.0 | 6 votes |
def f1_score_keras(y_true, y_pred): #convert probas to 0,1 y_ppred = K.zeros_like(y_true) y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), K.argmax(y_pred, axis=-1)], 1) #where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true*y_pred_ones, axis=0) #for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) #for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) #precision for each class precision = K.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred/pred_cnt) #recall for each class recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred/gold_cnt) #f1 for each class f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall)) #return average f1 score over all classes return K.mean(f1_class)
Example #12
Source File: evaluation_metrics_theano.py From deep-mlsa with Apache License 2.0 | 6 votes |
def f1_score_taskB(y_true, y_pred): #convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 #where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true*y_pred_ones, axis=0) #for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) #for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) #precision for each class precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt) #recall for each class recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred/gold_cnt) #f1 for each class f1_class = K.switch(K.equal(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall)) #return average f1 score over all classes return f1_class
Example #13
Source File: evaluation_metrics_theano.py From deep-mlsa with Apache License 2.0 | 6 votes |
def f1_score_semeval(y_true, y_pred): # convert probas to 0,1 y_ppred = K.zeros_like(y_true) y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), K.argmax(y_pred, axis=-1)], 1) # where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true * y_pred_ones, axis=0) # for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) # for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) # precision for each class precision = K.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred / pred_cnt) # recall for each class recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred / gold_cnt) # f1 for each class f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall)) #return average f1 score over all classes return (f1_class[0] + f1_class[2])/2.0
Example #14
Source File: evaluation_metrics_tf.py From deep-mlsa with Apache License 2.0 | 6 votes |
def f1_score_taskB(y_true, y_pred): # convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 # where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true * y_pred_ones, axis=0) # for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) # for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) # precision for each class precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt) # recall for each class recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred / gold_cnt) # f1 for each class f1_class = K.switch(K.equal(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall)) # return average f1 score over all classes return f1_class
Example #15
Source File: keras_extensions.py From visual_turing_test-tutorial with MIT License | 6 votes |
def time_distributed_masked_max(x, m): """ Computes max along the first (time) dimension. In: x - input; a 3D tensor m - mask m_value - value for masking """ # place infinities where mask is off m_value = 0.0 tmp = K.switch(K.equal(m, 0.0), -numpy.inf, 0.0) x_with_inf = x + K.expand_dims(tmp) x_max = K.max(x_with_inf, axis=1) r = K.switch(K.equal(x_max, -numpy.inf), m_value, x_max) return r ## classes ## # Transforms existing layers to masked layers
Example #16
Source File: loss.py From maskrcnn with MIT License | 6 votes |
def labels_loss(gt, pred): """ラベル分類の損失関数 gt: 正解 [N, R] 2軸目はラベルを示すID pred: 予測値(softmax済み) [N, R, labels]. """ # 交差エントロピー誤差 # バッチ毎の計算ではなく、全体の平均値でOK。 # 論文に以下の記載がある。 # In our current implementation (as in the released code), # the cls term in Eqn.(1) is normalized by the mini-batch size # (i.e., Ncls = 256) and the reg term is normalized by the number of # anchor locations (i.e., Nreg ∼ 2, 400). gt = K.cast(gt, 'int32') loss = K.switch(tf.size(gt) > 0, sparse_categorical_crossentropy(gt, pred), K.constant(0.0)) loss = K.mean(loss) return loss
Example #17
Source File: loss.py From maskrcnn with MIT License | 6 votes |
def offsets_loss(gt_offsets, pred_offsets, dump=False): """オフセット回帰の損失関数 positive(gt_fg > 0)データのみ評価対象とする Args: gt_offsets: 正解オフセット [R, 4] 3軸目は領域提案とアンカーのオフセット(中心、幅、高さ)。 (tx, ty, th, tw) pred_offsets: 予測値 [R, 4]. Note: この関数の呼び出し元はrpn_offsets_lossとhead_offsets_loss。 RPNでのRoI予測が外れると全てNegativeなBBoxとなり、結果的にhead_offsets_lossへ渡される正解データのラベルが全てNegativeとなる。 その場合、head_offsets_lossで得られる損失は0となるが、rpn_offsets_lossで得られる損失は大きくなるはずなので、 損失全体(rpn_offsets_loss + head_offsets_loss)で評価すれば適切な損失になるはず。 """ loss = K.switch(tf.size(gt_offsets) > 0, smooth_l1(gt_offsets, pred_offsets), tf.constant(0.0)) loss = K.mean(loss) return loss
Example #18
Source File: model.py From bird_species_classification with MIT License | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Cross entropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #19
Source File: losses.py From image-segmentation with MIT License | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): '''RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. ''' # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Cross entropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #20
Source File: model.py From deep-learning-explorer with Apache License 2.0 | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Crossentropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #21
Source File: model.py From raster-deep-learning with Apache License 2.0 | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Cross entropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #22
Source File: evaluation_metrics.py From deep-mlsa with Apache License 2.0 | 6 votes |
def precision_keras(y_true, y_pred): #convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 #where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true*y_pred_ones, axis=0) #for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) #precision for each class precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt) #return average f1 score over all classes return K.mean(precision)
Example #23
Source File: evaluation_metrics.py From deep-mlsa with Apache License 2.0 | 6 votes |
def f1_score_taskB(y_true, y_pred): #convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 #where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true*y_pred_ones, axis=0) #for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) #for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) #precision for each class precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt) #recall for each class recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred/gold_cnt) #f1 for each class f1_class = K.switch(K.equal(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall)) #return average f1 score over all classes return f1_class
Example #24
Source File: evaluation_metrics_tf.py From deep-mlsa with Apache License 2.0 | 6 votes |
def precision_keras(y_true, y_pred): # convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 # where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true * y_pred_ones, axis=0) # for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) # precision for each class precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt) # return average f1 score over all classes return K.mean(precision)
Example #25
Source File: evaluation_metrics_theano.py From deep-mlsa with Apache License 2.0 | 6 votes |
def precision_keras(y_true, y_pred): #convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 #where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true*y_pred_ones, axis=0) #for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) #precision for each class precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt) #return average f1 score over all classes return K.mean(precision)
Example #26
Source File: losses.py From keras-ctpn with Apache License 2.0 | 5 votes |
def side_regress_loss(predict_deltas, deltas, indices): """ 侧边改善回归目标 :param predict_deltas: 预测的x周偏移回归目标,(batch_num, anchors_num, 1) :param deltas: 真实的回归目标,(batch_num, ctpn_train_anchors, 3+1), 最后一位为tag, tag=0 为padding :param indices: 正负样本索引,(batch_num, ctpn_train_anchors, (idx,tag)), idx:指定anchor索引位置,最后一位为tag, tag=0 为padding; 1为正样本,-1为负样本 :return: """ # 去除padding和负样本 positive_indices = tf.where(tf.equal(indices[:, :, -1], 1)) deltas = tf.gather_nd(deltas[..., 2:3], positive_indices) # (n,(dy,dh,dx,tag)) 取 dx true_positive_indices = tf.gather_nd(indices[..., 0], positive_indices) # 一维,正anchor索引 # batch索引 batch_indices = positive_indices[:, 0] # 正样本anchor的2维索引 train_indices_2d = tf.stack([batch_indices, tf.cast(true_positive_indices, dtype=tf.int64)], axis=1) # 正样本anchor预测的回归类型 predict_deltas = tf.gather_nd(predict_deltas, train_indices_2d, name='ctpn_regress_loss_predict_side_deltas') # Smooth-L1 # 非常重要,不然报NAN loss = K.switch(tf.size(deltas) > 0, smooth_l1_loss(deltas, predict_deltas), tf.constant(0.0)) loss = K.mean(loss) return loss
Example #27
Source File: model.py From raster-deep-learning with Apache License 2.0 | 5 votes |
def mrcnn_bbox_loss_graph(target_bbox, target_class_ids, pred_bbox): """Loss for Mask R-CNN bounding box refinement. target_bbox: [batch, num_rois, (dy, dx, log(dh), log(dw))] target_class_ids: [batch, num_rois]. Integer class IDs. pred_bbox: [batch, num_rois, num_classes, (dy, dx, log(dh), log(dw))] """ # Reshape to merge batch and roi dimensions for simplicity. target_class_ids = K.reshape(target_class_ids, (-1,)) target_bbox = K.reshape(target_bbox, (-1, 4)) pred_bbox = K.reshape(pred_bbox, (-1, K.int_shape(pred_bbox)[2], 4)) # Only positive ROIs contribute to the loss. And only # the right class_id of each ROI. Get their indices. positive_roi_ix = tf.where(target_class_ids > 0)[:, 0] positive_roi_class_ids = tf.cast( tf.gather(target_class_ids, positive_roi_ix), tf.int64) indices = tf.stack([positive_roi_ix, positive_roi_class_ids], axis=1) # Gather the deltas (predicted and true) that contribute to loss target_bbox = tf.gather(target_bbox, positive_roi_ix) pred_bbox = tf.gather_nd(pred_bbox, indices) # Smooth-L1 Loss loss = K.switch(tf.size(target_bbox) > 0, smooth_l1_loss(y_true=target_bbox, y_pred=pred_bbox), tf.constant(0.0)) loss = K.mean(loss) return loss
Example #28
Source File: attention_decoder.py From keras-monotonic-attention with GNU Affero General Public License v3.0 | 5 votes |
def get_initial_state(self, inputs): if isinstance(inputs, list): assert len(inputs) == 2 # inputs == [encoder_outputs, y_true] encoder_outputs = inputs[0] else: encoder_outputs = inputs memory_shape = K.shape(encoder_outputs) # apply the matrix on the first time step to get the initial s0. s0 = activations.tanh(K.dot(encoder_outputs[:, 0], self.W_s)) y0 = K.zeros((memory_shape[0],), dtype='int64') + self.start_token t0 = K.zeros((memory_shape[0],), dtype='int64') initial_states = [y0, s0, t0] if self.is_monotonic: # initial attention has form: [1, 0, 0, ..., 0] for each sample in batch alpha0 = K.ones((memory_shape[0], 1)) alpha0 = K.switch(K.greater(memory_shape[1], 1), lambda: K.concatenate([alpha0, K.zeros((memory_shape[0], memory_shape[1] - 1))], axis=-1), alpha0) # like energy, attention is stored in shape (samples, time, 1) alpha0 = K.expand_dims(alpha0, -1) initial_states.append(alpha0) return initial_states
Example #29
Source File: rationale_CNN.py From robotreviewer with GNU General Public License v3.0 | 5 votes |
def metric_func_maker(metric_name="f", beta=1): return_recall=False return_precision=False func_name = metric_name if metric_name == "recall": return_recall = True elif metric_name == "precision": return_precision = True else: func_name = "f_%s" % beta def f_beta_score(y, y_pred): ''' for convienence ''' y_pred_binary = K.round(y_pred) num_true = K.sum(y) num_pred = K.sum(y_pred_binary) tp = K.sum(y * y_pred_binary) recall = K.switch(num_true>0, tp / num_true, 0.0) if return_recall: return recall precision = K.switch(num_pred>0, tp / num_pred, 0.0) if return_precision: return precision precision_recall_sum = recall + (beta*precision) return K.switch(precision_recall_sum>0, (beta+1)*((precision*recall)/(precision_recall_sum)), 0.0) f_beta_score.__name__ = func_name return f_beta_score
Example #30
Source File: model.py From raster-deep-learning with Apache License 2.0 | 5 votes |
def rpn_bbox_loss_graph(config, target_bbox, rpn_match, rpn_bbox): """Return the RPN bounding box loss graph. config: the model config object. target_bbox: [batch, max positive anchors, (dy, dx, log(dh), log(dw))]. Uses 0 padding to fill in unsed bbox deltas. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_bbox: [batch, anchors, (dy, dx, log(dh), log(dw))] """ # Positive anchors contribute to the loss, but negative and # neutral anchors (match value of 0 or -1) don't. rpn_match = K.squeeze(rpn_match, -1) indices = tf.where(K.equal(rpn_match, 1)) # Pick bbox deltas that contribute to the loss rpn_bbox = tf.gather_nd(rpn_bbox, indices) # Trim target bounding box deltas to the same length as rpn_bbox. batch_counts = K.sum(K.cast(K.equal(rpn_match, 1), tf.int32), axis=1) target_bbox = batch_pack_graph(target_bbox, batch_counts, config.IMAGES_PER_GPU) # TODO: use smooth_l1_loss() rather than reimplementing here # to reduce code duplication diff = K.abs(target_bbox - rpn_bbox) less_than_one = K.cast(K.less(diff, 1.0), "float32") loss = (less_than_one * 0.5 * diff**2) + (1 - less_than_one) * (diff - 0.5) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss