예제 #1
0
def yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold=.6):
    """Filter YOLO boxes based on object and class confidence."""
    box_scores = box_confidence * box_class_probs
    box_classes = K.argmax(box_scores, axis=-1)
    box_class_scores = K.max(box_scores, axis=-1)
    prediction_mask = box_class_scores >= threshold

    # TODO: Expose tf.boolean_mask to Keras backend?
    boxes = tf.boolean_mask(boxes, prediction_mask)
    scores = tf.boolean_mask(box_class_scores, prediction_mask)
    classes = tf.boolean_mask(box_classes, prediction_mask)
    return boxes, scores, classes
예제 #2
0
def viterbi_decode(x, U, b_start=None, b_end=None, mask=None):
    """Computes the best tag sequence y for a given input x, i.e. the one that
    maximizes the value of path_energy."""
    x = add_boundary_energy(x, b_start, b_end, mask)

    alpha_0 = x[:, 0, :]
    gamma_0 = K.zeros_like(alpha_0)
    initial_states = [gamma_0, alpha_0]
    _, gamma = _forward(
        x,
        lambda B: [K.cast(K.argmax(B, axis=1), K.floatx()),
                   K.max(B, axis=1)], initial_states, U, mask)
    y = _backward(gamma, mask)
    return y
예제 #3
0
def __center_loss(y_true, y_pred, centers):
    y_true_value = K.argmax(y_true)

    loss = K.variable(0.0)
    for label in range(CONFIG["num_classes"]):
        center = centers[label]

        indices = tf.where(tf.equal(y_true_value, label))
        pred_per_class = tf.gather_nd(y_pred, indices=indices)
        diff = tf.subtract(pred_per_class, center)
        square_diff = K.pow(diff, 2)
        sum = K.sum(K.sum(square_diff, axis=-1), axis=-1)
        loss = tf.add(loss, sum)

    return loss
예제 #4
0
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold=.6):
    # get the p(class = x given object = true) = p(class = x) * p(object = true)
    box_scores = box_confidence * box_class_probs  # 19x19x80

    # box_classes indeces of highest probability
    box_classes = K.argmax(box_scores, axis=-1)  # 19x19x5x1  (1 class index)
    # box class scores of highest probabilites
    box_class_scores = K.max(box_scores, axis=-1)  # 19x19x5x1 (1 class score)
    # make filter  of boxes with have scores more than threshold
    filtering_mask = box_class_scores >= threshold
    # choice from box_classes that is exist in our filter
    scores = tf.boolean_mask(box_class_scores, filtering_mask)
    boxes = tf.boolean_mask(boxes, filtering_mask)
    classes = tf.boolean_mask(box_classes, filtering_mask)

    return scores, boxes, classes
예제 #5
0
def __softmax_cosine_loss(y_true, y_pred, centers):
    y_true_value = K.argmax(y_true)

    base_loss = K.variable(0.0)
    for label in range(CONFIG["num_classes"]):
        center = centers[label]
        indices = tf.where(tf.equal(y_true_value, label))
        pred_per_class = tf.gather_nd(y_pred, indices=indices)
        distances_per_class = __cosine(pred_per_class, center)
        sum = K.sum(distances_per_class, axis=-1)
        base_loss = tf.add(base_loss, sum)

    base_distances = K.zeros_like(y_true_value, dtype='float32')
    for label in range(CONFIG["num_classes"]):
        center = centers[label]
        distances_with_all_classes = __cosine(y_pred, center)
        exp_distances = K.exp(-distances_with_all_classes)
        base_distances = tf.add(base_distances, exp_distances)

    log_distances = K.log(base_distances)
    sum_on_batch = K.sum(log_distances)

    return base_loss + sum_on_batch
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold=0.3):
    """
    通过阈值来过滤对象和分类的置信度。
    
    参数:
        box_confidence  - tensor类型,维度为(19,19,5,1),包含19x19单元格中每个单元格预测的5个锚框中的所有的锚框的pc (一些对象的置信概率)。
        boxes - tensor类型,维度为(19,19,5,4),包含了所有的锚框的(px,py,ph,pw )。
        box_class_probs - tensor类型,维度为(19,19,5,80),包含了所有单元格中所有锚框的所有对象( c1,c2,c3,···,c80 )检测的概率。
        threshold - 实数,阈值,如果分类预测的概率高于它,那么这个分类预测的概率就会被保留。
    
    返回:
        scores - tensor 类型,维度为(None,),包含了保留了的锚框的分类概率。
        boxes - tensor 类型,维度为(None,4),包含了保留了的锚框的(b_x, b_y, b_h, b_w)
        classess - tensor 类型,维度为(None,),包含了保留了的锚框的索引
        
    注意:"None"是因为你不知道所选框的确切数量,因为它取决于阈值。
          比如:如果有10个锚框,scores的实际输出大小将是(10,)
    """

    #第一步:计算锚框的得分
    box_scores = box_confidence * box_class_probs

    #第二步:找到最大值的锚框的索引以及对应的最大值的锚框的分数
    box_classes = K.argmax(box_scores, axis=-1)  #(19*19*5*1)
    box_class_scores = K.max(
        box_scores, axis=-1)  #找到最可能的类,是将最后一个维度进行展开(19*19*5*80)得到(19*19*5*1)

    #第三步:根据阈值创建掩码
    filtering_mask = (box_class_scores >= threshold)

    #对scores, boxes 以及 classes使用掩码
    scores = tf.boolean_mask(box_class_scores, filtering_mask)
    boxes = tf.boolean_mask(boxes, filtering_mask)
    classes = tf.boolean_mask(box_classes, filtering_mask)

    return scores, boxes, classes
예제 #7
0
def chain_crf_loss(y, x, U, b_start=None, b_end=None, mask=None):
    """Variant of sparse_chain_crf_loss but with one-hot encoded tags y."""
    y_sparse = K.argmax(y, -1)
    y_sparse = K.cast(y_sparse, 'int32')
    return sparse_chain_crf_loss(y_sparse, x, U, b_start, b_end, mask)