提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
def match
代码如下():
理解须知:
《1》torch.max(a,dim=0,keepdim=True) dim=1,表示 the dimension to reduce(这个维度上减少消去)
《2》index_fill_(dim,index,val)按照指定的维度轴dim 根据index去对应位置,将原tensor用参数val值填充,这里强调一下,index必须是1D tensor,index去指定轴上索引数据时候会广播
特别感谢下这位同志的笔记:豆豆小朋友的笔记:添加链接描述
《3》下图表示的为真实框(ground_truth)和先验框(priors)相互匹配得到的交并比值(overlap)和地址(idx),其中数据可能会影响结果的确切性
问题版:
正确版:
def match(threshold, truths, priors, variances, labels, loc_t, conf_t, idx):
#----------------------------------------------#
# 计算所有的先验框和真实框的重合程度
#----------------------------------------------#
overlaps = jaccard(
truths,
point_form(priors)
)
#----------------------------------------------#
# 所有真实框和先验框的最好重合程度 # 拿先验框来去匹配真实框,与每个真实框的最好匹配结果
# best_prior_overlap [truth_box,1]
# best_prior_idx [truth_box,0]
#----------------------------------------------#
# https: // blog.popkx.com/detail-analysis-of-retinaface-multibox-loss-priorbox-and-object-bbox-matches-analysis/
# torch.max(a,dim=0,keepdim=True) dim=1,表示 the dimension to reduce(这个维度上减少消去)
best_prior_overlap, best_prior_idx = overlaps.max(1, keepdim=True)
best_prior_idx.squeeze_(1) # 变为tensor
best_prior_overlap.squeeze_(1)
#----------------------------------------------#
# 所有先验框和真实框的最好重合程度 # 拿真实框来去匹配先验框,与每个先验框的最好匹配结果
# best_truth_overlap [1,prior]
# best_truth_idx [1,prior]
#----------------------------------------------#
# torch.max(a,dim=0,keepdim=True) dim=0,表示 the dimension to reduce(这个维度上减少消去)
best_truth_overlap, best_truth_idx = overlaps.max(0, keepdim=True)
best_truth_idx.squeeze_(0)
best_truth_overlap.squeeze_(0)
#----------------------------------------------#
# 用于保证每个真实框都至少有对应的一个先验框
#----------------------------------------------#
for j in range(best_prior_idx.size(0)):
best_truth_idx[best_prior_idx[j]] = j
best_truth_overlap.index_fill_(0, best_prior_idx, 2)
#----------------------------------------------#
# 获取每一个先验框对应的真实框[num_priors,4]
#----------------------------------------------#
matches = truths[best_truth_idx]
# Shape: [num_priors]
conf = labels[best_truth_idx] + 1
#----------------------------------------------#
# 如果重合程度小于threhold则认为是背景
#----------------------------------------------#
conf[best_truth_overlap < threshold] = 0
#----------------------------------------------#
# 利用真实框和先验框进行编码
# 编码后的结果就是网络应该有的预测结果
#----------------------------------------------#
loc = encode(matches, priors, variances)
# [num_priors,4]
loc_t[idx] = loc
# [num_priors]
conf_t[idx] = conf