model.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:few_shot_mAP_public 作者: eleniTriantafillou 项目源码 文件源码
def perform_query_inference(self, q_feats, q_pos_feats, q_neg_feats,
                              q_num_pos, q_num_neg, q_Y_aug):
    """
    Inference for a specific query.

    :param q_feats: the features for the query
    :param q_pos_feats: the features of the query's positive points
    :param q_neg_feats: the features of the query's negative points
    :param q_num_pos: the number of positive points for the query
    :param q_num_neg: the number of negative points for the query
    :param q_Y_aug: the solution of loss-augmented inference for this query

    :return: phi_pos: the similarity between the query and each positive point
    :return: phi_neg: the similarity between the query and each negative point
    :return: AP_score_std: the score of the standard inference solution for AP of this query
    :return: AP_score_aug: the score of the loss-augmented inference solution for AP of this query
    :return: AP_score_GT: the score of the ground truth solution for AP of this query
    """

    S_pos = tf.matmul(q_feats, q_pos_feats, transpose_b=True)  # (1, num_pos)
    S_neg = tf.matmul(q_feats, q_neg_feats, transpose_b=True)  # (1, num_neg)
    phi_pos, sorted_inds_pos = tf.nn.top_k(S_pos, k=q_num_pos)
    phi_neg, sorted_inds_neg = tf.nn.top_k(S_neg, k=q_num_neg)

    phi_pos = tf.transpose(phi_pos)
    phi_neg = tf.transpose(phi_neg)

    # Score of standard inference
    phi_pos_expanded = tf.tile(phi_pos, [1, q_num_neg])  # (num_pos, num_neg)
    phi_neg_expanded = tf.tile(tf.transpose(phi_neg), [q_num_pos,
                                                       1])  # (num_pos, num_neg)
    temp1_Y = tf.greater(phi_pos_expanded,
                         phi_neg_expanded)  # (num_pos, num_neg) of True/False's
    temp2_Y = 2. * tf.to_float(temp1_Y)  # (num_pos, num_neg) of 2/0's
    Y_std = temp2_Y - tf.ones_like(temp2_Y)  # (num_pos, num_neg) of 1/-1's
    F_std = Y_std * (phi_pos_expanded - phi_neg_expanded)  # (num_pos, num_neg)
    AP_score_std = tf.truediv(
        tf.reduce_sum(F_std), tf.to_float(q_num_pos * q_num_neg))

    # Score of loss-augmented inferred ranking
    F_aug = q_Y_aug * (phi_pos_expanded - phi_neg_expanded)
    AP_score_aug = tf.truediv(
        tf.reduce_sum(F_aug), tf.to_float(q_num_pos * q_num_neg))

    # Score of the groundtruth
    q_Y_GT = tf.ones_like(Y_std)
    F_GT = q_Y_GT * (phi_pos_expanded - phi_neg_expanded)
    AP_score_GT = tf.truediv(
        tf.reduce_sum(F_GT), tf.to_float(q_num_pos * q_num_neg))

    AP_score_std = tf.reshape(AP_score_std, [1, 1])
    AP_score_aug = tf.reshape(AP_score_aug, [1, 1])
    AP_score_GT = tf.reshape(AP_score_GT, [1, 1])
    return phi_pos, phi_neg, AP_score_std, AP_score_aug, AP_score_GT
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号