SSD算法学习及PyTorch代码分析[3]---先验框匹配

SSD 算法在先验框匹配上,采用了两个原则:

  • 对于图像中每一个 ground truth 找到与其 IOU 最大的的先验框, 该先验框为正样本, 若一个先验框没有与任何的 ground truth 匹配,则为负样本。
  • 对于剩下的未匹配的先验框,若与某个 ground truth 的 IOU 大于某个阈值(一般取0.5),则该先验框也与 ground truth 匹配

通过代码可以看出:

 def match(threshold, truths, priors, variances, labels, loc_t, conf_t, idx):
    """Match each prior box with the ground truth box of the highest jaccard overlap, encode the bounding boxes, then return the matched indices corresponding to both confidence and location preds. Args: threshold: (float) The overlap threshold used when mathing boxes. truths: (tensor) Ground truth boxes, Shape: [num_obj, 4]. priors: (tensor) Prior boxes from priorbox layers, Shape: [n_priors,4]. variances: (tensor) Variances corresponding to each prior coord, Shape: [num_priors, 4]. labels: (tensor) All the class labels for the image, Shape: [num_obj]. loc_t: (tensor) Tensor to be filled w/ endcoded location targets. conf_t: (tensor) Tensor to be filled w/ matched indices for conf preds. idx: (int) current batch index Return: The matched indices corresponding to 1)location and 2)confidence preds. """
    # jaccard index 每个真实框和先验框的IOU
    overlaps = jaccard(
        truths, #(x1,y1,w,h)
        point_form(priors)  # priors:(cx,cy,w,h) 转换成(x1,y1,w,h)
    ) # 二维张量,真实box数*先验框数
    
    # (Bipartite Matching)
    # [num_objects,1] best prior for each ground truth 每个真值对应的最好的先验框,依然保持维度不变
    # best_prior_idx存放的是先验框的id
    best_prior_overlap, best_prior_idx = overlaps.max(1, keepdim=True)
    # [1,num_priors] best ground truth for each prior 每一个先验框对应最好的真值
    # best_truth_idx存放的是真值的id
    best_truth_overlap, best_truth_idx = overlaps.max(0, keepdim=True)
    # 往往 len(best_truth_idx) > len(best_prior_idx)

    best_truth_idx.squeeze_(0) # [num_priors]
    best_truth_overlap.squeeze_(0)
    best_prior_idx.squeeze_(1) # [num_objects]
    best_prior_overlap.squeeze_(1)
    best_truth_overlap.index_fill_(0, best_prior_idx, 2)  # ensure best prior

    for j in range(best_prior_idx.size(0)): # 0 -> (num_objects-1)
        best_truth_idx[best_prior_idx[j]] = j
    # 广播,best_truth_idx长度为num_priors,best_truth_idx装着objects序号(truths序号)
    # 表示第i个先验框对应的truths框坐标,总共num_priors个先验框
    matches = truths[best_truth_idx]  # Shape: [num_priors,4]
    
    # conf装着每个先验框对应的label值 +1处理,为了添加背景这一类
    conf = labels[best_truth_idx] + 1         # Shape: [num_priors]
    conf[best_truth_overlap < threshold] = 0  # label as background
    loc = encode(matches, priors, variances)
    loc_t[idx] = loc    # [num_priors,4] encoded offsets to learn
    conf_t[idx] = conf  # [num_priors] top class label for each prior


全部评论

相关推荐

已oc&nbsp;云智断更了好几天,也有一些话想说,继续更新一篇云智timeline&nbsp;4.18&nbsp;一面&nbsp;半个小时后约二面&nbsp;4.21二面&nbsp;当晚&nbsp;约hr面&nbsp;4.23hr面&nbsp;4.30&nbsp;发offer之前美团的二面挂了,进入人才库,后面又被捞起来面试,4.30号&nbsp;美团又一面,现在还没出一面结果感觉也不报什么希望,就算一面过了,还有二面,我经不起深入拷打,唉,真的,好难五一躺平了五天,吃吃玩玩睡睡~还要担心毕业,科研更是难,唉暑期可能就到此为止了,后面没有时间在这个上面了,要抓紧时间做科研,为了后面能出去实习。大厂,秋招再见!!!有一些感慨:4.1是我的第一次面试,美团,面试的时候紧张到浑身发...
daisy9542:我今晚也是美团一面,已经第六次了。我也面了其他的,没拿到 offer。但我想开了,要按照自己的节奏来,找暑期转正然后秋招大杀四方并不是唯一的出路,其实还有很多选择的,有 0 实习最后秋招拿 offer 了,也有不选择互联网去国企的外企的,考编的,创业的。现在的失败不代表以后的路都是黑暗的,只不过可能运气还没降临到头上。所以现在要做的,就是放平心态,提升自己,通过面试了解到自己的优点和不足,争取下次机会来了能好好抓住
点赞 评论 收藏
分享
03-11 21:46
西北大学 Java
河和静子:这只是实习工资,我学长北大通班博一的,他同学被这家天天发邮件让他去实习,一个月10w
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务