week 8月6日( 二 )

<=iou_threshold).reshape(-1)B = B[box_iou_idx+1]return torch.tensor(keep,device=boxes.device)"""使用非极大值抑制来预测边界框"""def multibox_detection(cls_probs,offset_preds,anchors,nms_threshold=0.5,pos_threshold=0.009999999):#@savedevice,batch_size = cls_probs.device,cls_probs.shape[0]anchors = anchors.squeeze(0)num_classes,num_anchors = cls_probs.shape[1],cls_probs.shape[2]#存储批量样本中预测的一些信息out = []for i in range(batch_size):cls_prob = cls_probs[i]offset_pred = offset_preds[i].reshape(-1,4)conf,class_id = torch.max(cls_prob[1:],dim=0)#根据anchors和锚框偏移量得到预测真实边界框的值predict_bb = offset_inverse(anchors,offset_pred)#根据nms去除一些重合比较大的预测真实边界框,保留重合率不大的预测真实边界框keep = nms(predict_bb,conf,nms_threshold)# 找到所有的non_keep索引,并将类设置为背景-1anchors_idx = torch.arange(num_anchors,device=device,dtype=torch.long)idx = torch.cat((keep,anchors_idx))uniques,counts = idx.unique(return_counts=True)non_keep = uniques[counts==1]class_id[non_keep] = -1anchors_all_id = torch.cat((keep,non_keep))# pos_threshold是一个用于非背景预测的阈值,将低于pos_threshold阈值的预测真实边界框的类别设置为背景类-1below_min_idx = (conf
1、生成多个锚框
2、根据交并比,将真实框分配给锚框
3、对分配好的,标注类别和偏移量
4、根据偏移量,留下最准确的边界框,并显示出来