Python keras.backend.maximum() Examples
The following are 30
code examples of keras.backend.maximum().
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: my_layers.py From Attention-Based-Aspect-Extraction with Apache License 2.0 | 7 votes |
def call(self, input_tensor, mask=None): z_s = input_tensor[0] z_n = input_tensor[1] r_s = input_tensor[2] z_s = K.l2_normalize(z_s, axis=-1) z_n = K.l2_normalize(z_n, axis=-1) r_s = K.l2_normalize(r_s, axis=-1) steps = z_n.shape[1] pos = K.sum(z_s * r_s, axis=-1, keepdims=True) pos = K.repeat_elements(pos, steps, axis=1) r_s = K.expand_dims(r_s, axis=-2) r_s = K.repeat_elements(r_s, steps, axis=1) neg = K.sum(z_n * r_s, axis=-1) loss = K.cast(K.sum(K.maximum(0., (1. - pos + neg)), axis=-1, keepdims=True), K.floatx()) return loss
Example #2
Source File: padam.py From keras-contrib with MIT License | 6 votes |
def __init__(self, lr=1e-1, beta_1=0.9, beta_2=0.999, epsilon=1e-8, decay=0., amsgrad=False, partial=1. / 8., **kwargs): if partial < 0 or partial > 0.5: raise ValueError( "Padam: 'partial' must be a positive float with a maximum " "value of `0.5`, since higher values will cause divergence " "during training." ) super(Padam, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') if epsilon is None: epsilon = K.epsilon() self.epsilon = epsilon self.partial = partial self.initial_decay = decay self.amsgrad = amsgrad
Example #3
Source File: densities.py From DeepIV with MIT License | 6 votes |
def mix_gaussian_loss(x, mu, log_sig, w): ''' Combine the mixture of gaussian distribution and the loss into a single function so that we can do the log sum exp trick for numerical stability... ''' if K.backend() == "tensorflow": x.set_shape([None, 1]) gauss = log_norm_pdf(K.repeat_elements(x=x, rep=mu.shape[1], axis=1), mu, log_sig) # TODO: get rid of clipping. gauss = K.clip(gauss, -40, 40) max_gauss = K.maximum((0.), K.max(gauss)) # log sum exp trick... gauss = gauss - max_gauss out = K.sum(w * K.exp(gauss), axis=1) loss = K.mean(-K.log(out) + max_gauss) return loss
Example #4
Source File: utils.py From squeezedet-keras with MIT License | 6 votes |
def batch_iou(boxes, box): """Compute the Intersection-Over-Union of a batch of boxes with another box. Args: box1: 2D array of [cx, cy, width, height]. box2: a single array of [cx, cy, width, height] Returns: ious: array of a float number in range [0, 1]. """ lr = np.maximum( np.minimum(boxes[:,0]+0.5*boxes[:,2], box[0]+0.5*box[2]) - \ np.maximum(boxes[:,0]-0.5*boxes[:,2], box[0]-0.5*box[2]), 0 ) tb = np.maximum( np.minimum(boxes[:,1]+0.5*boxes[:,3], box[1]+0.5*box[3]) - \ np.maximum(boxes[:,1]-0.5*boxes[:,3], box[1]-0.5*box[3]), 0 ) inter = lr*tb union = boxes[:,2]*boxes[:,3] + box[2]*box[3] - inter return inter/union
Example #5
Source File: losses.py From deep_qa with Apache License 2.0 | 6 votes |
def ranking_loss_with_margin(y_pred, y_true): """ Using this loss trains the model to give scores to all correct elements in y_true that are higher than all scores it gives to incorrect elements in y_true, plus a margin. For example, let ``y_true = [0, 0, 1, 1, 0]``, and let ``y_pred = [-1, 1, 2, 0, -2]``. We will find the lowest score assigned to correct elements in ``y_true`` (``0`` in this case), and the highest score assigned to incorrect elements in ``y_true`` (``1`` in this case). We will then compute a hinge loss given these values: ``K.maximum(0.0, 1 + 1 - 0)``. Note that the way we do this uses ``K.max()`` and ``K.min()`` over the elements in ``y_true``, which means that if you have a lot of values in here, you'll only get gradients backpropping through two of them (the ones on the margin). This could be an inefficient use of your computation time. Think carefully about the data that you're using with this loss function. Because of the way masking works with Keras loss functions, also, you need to be sure that any masked elements in ``y_pred`` have very negative values before they get passed into this loss function. """ correct_elements = y_pred + (1.0 - y_true) * VERY_LARGE_NUMBER lowest_scoring_correct = K.min(correct_elements, axis=-1) incorrect_elements = y_pred + y_true * VERY_NEGATIVE_NUMBER highest_scoring_incorrect = K.max(incorrect_elements, axis=-1) return K.mean(K.maximum(0.0, 1.0 + highest_scoring_incorrect - lowest_scoring_correct))
Example #6
Source File: custom_loss.py From mhcflurry with Apache License 2.0 | 5 votes |
def loss(self, y_true, y_pred): # We always delay import of Keras so that mhcflurry can be imported # initially without tensorflow debug output, etc. from keras import backend as K y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) # Handle (=) inequalities diff1 = y_pred - y_true diff1 *= K.cast(y_true >= 0.0, "float32") diff1 *= K.cast(y_true <= 1.0, "float32") # Handle (>) inequalities diff2 = y_pred - (y_true - 2.0) diff2 *= K.cast(y_true >= 2.0, "float32") diff2 *= K.cast(y_true <= 3.0, "float32") diff2 *= K.cast(diff2 < 0.0, "float32") # Handle (<) inequalities diff3 = y_pred - (y_true - 4.0) diff3 *= K.cast(y_true >= 4.0, "float32") diff3 *= K.cast(diff3 > 0.0, "float32") denominator = K.maximum( K.sum(K.cast(K.not_equal(y_true, 2.0), "float32"), 0), 1.0) result = ( K.sum(K.square(diff1)) + K.sum(K.square(diff2)) + K.sum(K.square(diff3))) / denominator return result
Example #7
Source File: dfcBaseline.py From dfc2019 with MIT License | 5 votes |
def no_nan_mse(self, y_true, y_pred): """ Custom mean squared error loss function for single-view depth prediction used to ignore NaN/invalid depth values :param y_true: ground truth depth :param y_pred: predicted depth :return: mse loss without nan influence """ mask_true = K.cast(K.not_equal(y_true, self.params.IGNORE_VALUE), K.floatx()) masked_squared_error = K.square(mask_true * (y_true - y_pred)) masked_mse = K.sum(masked_squared_error, axis=-1) / K.maximum(K.sum(mask_true, axis=-1), 1) return masked_mse
Example #8
Source File: keras_yolo3.py From chinese_ocr with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #9
Source File: model.py From WorkControl with Apache License 2.0 | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #10
Source File: model.py From WorkControl with Apache License 2.0 | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #11
Source File: model.py From WorkControl with Apache License 2.0 | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #12
Source File: losses.py From cs-ranking with Apache License 2.0 | 5 votes |
def hinged_rank_loss(y_true, y_pred): y_true, y_pred = tensorify(y_true), tensorify(y_pred) mask = K.cast(K.greater(y_true[:, None] - y_true[:, :, None], 0), dtype="float32") diff = y_pred[:, :, None] - y_pred[:, None] hinge = K.maximum(mask * (1 - diff), 0) n = K.sum(mask, axis=(1, 2)) return K.sum(hinge, axis=(1, 2)) / n
Example #13
Source File: model.py From YOLO-V3-IOU with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #14
Source File: custom_loss.py From mhcflurry with Apache License 2.0 | 5 votes |
def loss(self, y_true, y_pred): import tensorflow as tf y_true = tf.reshape(y_true, (-1,)) pos = tf.boolean_mask(y_pred, tf.math.equal(y_true, 1.0)) pos_max = tf.reduce_max(pos, axis=1) neg = tf.boolean_mask(y_pred, tf.math.equal(y_true, 0.0)) term = tf.reshape(neg, (-1, 1)) - pos_max + self.delta result = tf.math.divide_no_nan( tf.reduce_sum(tf.maximum(0.0, term) ** 2), tf.cast(tf.size(term), tf.float32)) * self.multiplier return result
Example #15
Source File: sinerelu.py From keras-contrib with MIT License | 5 votes |
def call(self, Z): m = self.epsilon * (K.sin(Z) - K.cos(Z)) A = K.maximum(m, Z) return A
Example #16
Source File: model.py From keras-yolo3 with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #17
Source File: model.py From human_counter with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #18
Source File: model.py From deep_sort_yolov3 with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #19
Source File: rank_losses.py From NPRF with Apache License 2.0 | 5 votes |
def rank_hinge_loss(y_true, y_pred): margin = 1. y_neg = Lambda(lambda a: a[::2, :], output_shape= (1,))(y_pred) y_pos = Lambda(lambda a: a[1::2, :], output_shape= (1,))(y_pred) loss = K.maximum(0., margin + y_neg - y_pos) return K.mean(loss)
Example #20
Source File: utils.py From pCVR with Apache License 2.0 | 5 votes |
def logloss(act, pred): ''' 官方给的损失函数 :param act: :param pred: :return: ''' epsilon = 1e-15 pred = sp.maximum(epsilon, pred) pred = sp.minimum(1 - epsilon, pred) ll = sum(act * sp.log(pred) + sp.subtract(1, act) * sp.log(sp.subtract(1, pred))) ll = ll * -1.0 / len(act) return ll
Example #21
Source File: utils.py From pCVR with Apache License 2.0 | 5 votes |
def my_logloss(act, pred): epsilon = 1e-15 pred = K.maximum(epsilon, pred) pred = K.minimum(1 - epsilon, pred) ll = K.sum(act * K.log(pred) + (1 - act) * K.log(1 - pred)) ll = ll * -1.0 / K.shape(act)[0] return ll
Example #22
Source File: model.py From yolo3_keras_Flag_Detection with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #23
Source File: rank_losses.py From NeuralResponseRanking with MIT License | 5 votes |
def rank_hinge_loss(kwargs=None): margin = 1. if isinstance(kwargs, dict) and 'margin' in kwargs: margin = kwargs['margin'] def _margin_loss(y_true, y_pred): # 2 * batchsize * 1 #output_shape = K.int_shape(y_pred) y_pos = Lambda(lambda a: a[::2, :], output_shape= (1,))(y_pred) y_neg = Lambda(lambda a: a[1::2, :], output_shape= (1,))(y_pred) loss = K.maximum(0., margin + y_neg - y_pos) return K.mean(loss) return _margin_loss
Example #24
Source File: bbox.py From maskrcnn with MIT License | 5 votes |
def get_iou_K(bbox_base, bbox_target): """2つのBoundingBoxのIoU(Intersection Over Union)を取得する。 https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/ Args: bbox_base (tensor): 基準になるBoudingBox。 Its shape is :math:`(N, 4)`. 2軸目に以下の順でBBoxの座標を保持する。 :math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`. bbox_target (tensor): BoudingBox。 Its shape is :math:`(K, 4)`. 2軸目に以下の順でBBoxの座標を保持する。 :math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`. bbox_baseの各Box毎にbbox_targetを適用し、IoUを求める。 Returns: tensor: IoU(0 <= IoU <= 1) 形状は以下の通り。 :math:`(N, K)`. """ if bbox_base.shape[1] != 4 or bbox_target.shape[1] != 4: raise IndexError # 交差領域の左上の座標 # bbox_base[:, None, :]のより次元を増やすことで、 # bbox_baseとbbox_targetを総当りで評価出来る。 # (N, K, 2)の座標が得られる tl = K.maximum(bbox_base[:, None, :2], bbox_target[:, :2]) # 交差領域の右下の座標 # (N, K, 2)の座標が得られる br = K.minimum(bbox_base[:, None, 2:], bbox_target[:, 2:]) # 右下-左下=交差領域の(h, w)が得られる。 # h*wで交差領域の面積。ただし、交差領域がない(右下 <= 左上)ものは除くため0とする。 area_i = K.prod(br - tl, axis=2) * \ K.cast(K.all(br > tl, axis=2), 'float32') area_base = K.prod(bbox_base[:, 2:] - bbox_base[:, :2], axis=1) area_target = K.prod(bbox_target[:, 2:] - bbox_target[:, :2], axis=1) return area_i / (area_base[:, None] + area_target - area_i)
Example #25
Source File: bbox.py From maskrcnn with MIT License | 5 votes |
def get_iou(bbox_base, bbox_target): """2つのBoundingBoxのIoU(Intersection Over Union)を取得する。 https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/ Args: bbox_base (ndarray): 基準になるBoudingBox。 Its shape is :math:`(N, 4)`. 2軸目に以下の順でBBoxの座標を保持する。 :math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`. bbox_target (ndarray): BoudingBox。 Its shape is :math:`(K, 4)`. 2軸目に以下の順でBBoxの座標を保持する。 :math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`. bbox_baseの各Box毎にbbox_targetを適用し、IoUを求める。 Returns: ndarray: IoU(0 <= IoU <= 1) 形状は以下の通り。 :math:`(N, K)`. """ if bbox_base.shape[1] != 4 or bbox_target.shape[1] != 4: raise IndexError # 交差領域の左上の座標 # bbox_base[:, None, :]のより次元を増やすことで、 # bbox_baseとbbox_targetを総当りで評価出来る。 # (N, K, 2)の座標が得られる tl = np.maximum(bbox_base[:, None, :2], bbox_target[:, :2]) # 交差領域の右下の座標 # (N, K, 2)の座標が得られる br = np.minimum(bbox_base[:, None, 2:], bbox_target[:, 2:]) # 右下-左下=交差領域の(h, w)が得られる。 # h*wで交差領域の面積。ただし、交差領域がない(右下 <= 左上)ものは除くため0とする。 area_i = np.prod(br - tl, axis=2) * \ np.all(br > tl, axis=2).astype('float32') area_base = np.prod(bbox_base[:, 2:] - bbox_base[:, :2], axis=1) area_target = np.prod(bbox_target[:, 2:] - bbox_target[:, :2], axis=1) return area_i / (area_base[:, None] + area_target - area_i)
Example #26
Source File: triplet_loss.py From bootcamp with Apache License 2.0 | 5 votes |
def deep_speaker_loss(y_true, y_pred, alpha=ALPHA): # y_true is not used. we respect this convention: # y_true.shape = (batch_size, embedding_size) [not used] # y_pred.shape = (batch_size, embedding_size) # EXAMPLE: # _____________________________________________________ # ANCHOR 1 (512,) # ANCHOR 2 (512,) # POS EX 1 (512,) # POS EX 2 (512,) # NEG EX 1 (512,) # NEG EX 2 (512,) # _____________________________________________________ split = K.shape(y_pred)[0] // 3 anchor = y_pred[0:split] positive_ex = y_pred[split:2 * split] negative_ex = y_pred[2 * split:] # If the loss does not decrease below ALPHA then the model does not learn anything. # If all anchor = positive = negative (model outputs the same vector always). # Then sap = san = 1. and loss = max(alpha,0) = alpha. # On the contrary if anchor = positive = [1] and negative = [-1]. # Then sap = 1 and san = -1. loss = max(-1-1+0.1,0) = max(-1.9, 0) = 0. sap = batch_cosine_similarity(anchor, positive_ex) san = batch_cosine_similarity(anchor, negative_ex) loss = K.maximum(san - sap + alpha, 0.0) total_loss = K.mean(loss) return total_loss
Example #27
Source File: model_vgg16.py From keras-YOLOv3-mobilenet with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #28
Source File: model_Mobilenet.py From keras-YOLOv3-mobilenet with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #29
Source File: model.py From keras-YOLOv3-mobilenet with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #30
Source File: capsule.py From CapsNet with MIT License | 5 votes |
def margin_loss(y, pred): """ For the first part of loss(classification loss) """ return K.mean(K.sum(y * K.square(K.maximum(0.9 - pred, 0)) + \ 0.5 * K.square((1 - y) * K.maximum(pred - 0.1, 0)), axis=1))