darknet-yolov3模型预测框size不正确的原因

2023-05-11,,

问题描述:预测框的中心位置正常,但是预测的框的width和height不正常。

解决方法:使得训练的配置cfg和测试中cfg的输入width, height, anchorbox保持一致!

问题是我在修改anchorbox时遇到的,当时训练和测试不在同一环境下,测试端没有及时更新cfg文件造成的,如下图所示:

mAP也是极低的。

修改后,检测框正常,如下图所示:

下面做下boundingbox来源分析:

首先是yolo(you only look once)中是这样定义的:

算法中明确告诉我们:boundingbox只和cell(featuremap中的)位置(Cx,Cy)以及anchorbox的(Pw,Ph)直接相关。所以我们可以重点关注这两个量。

对应到源代码中的实现主要是以下两个函数:

yolo_layer.c
...
box get_yolo_box(float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, int stride)
/*
输入参数解析:(*x 预测数据),(*biases 存放anchor数据),
(i、j 对应在feature map上的坐标),
(n 表示anchor数组的mask,为了让三个yolo_layer能取到自己对应的三组anchor, 小尺寸feature map对应大size anchor,比较好理解小尺寸特征图负责检查大尺寸目标),
(index,当前bbox对应的数据的起始下标),
(lw lh,特征图的w h),
(w h, 网络输入的w h),
(同一个bbox数据之间的stride lw*lh)
*/
{
box b; // 网络为了每一个bbox都给出了4个坐标预测值: tx ty tw ty
/*
其中tx 和 ty是相对于当前feature map坐标的偏移
除以lw&&lh 是计算出bbox坐标在图像中的比例
*/
b.x = (i + x[index + 0*stride]) / lw;
b.y = (j + x[index + 1*stride]) / lh;
/*
e^tw * biases[2*n] 表示学习到的w回归值和对应prior bbox(anchor) w的乘积得到
bbox在网络输入size基础上的w size, 除以 net_w得到相对于网络输入图像的比例
h的计算同理, 这部分的内容涉及到yolov3论文中的图二
*/
b.w = exp(x[index + 2*stride]) * biases[2*n] / w;
b.h = exp(x[index + 3*stride]) * biases[2*n+1] / h;
return b;
/*补充一下,这里算出的x,y,w,h都是相对于net input size的比例*/
}
此不分为转载:
https://blog.csdn.net/wwwhp/article/details/84718089
...
int get_yolo_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, int relative, detection *dets)
{
int i,j,n;
float *predictions = l.output;
if (l.batch == 2) avg_flipped_yolo(l);
int count = 0;
for (i = 0; i < l.w*l.h; ++i){
int row = i / l.w;
int col = i % l.w;
//printf("get_yolo_detections:i =%d,row = i / l.w=%d, col = i % l.w;\n",i, row, col);
for(n = 0; n < l.n; ++n){
int obj_index = entry_index(l, 0, n*l.w*l.h + i, 4);
float objectness = predictions[obj_index];//objectness:有框的
//printf("objectness:%f\n",objectness);
if(objectness <= thresh) continue;
printf("obj_index = %d,objectness:%f, thresh:%f\n",obj_index,objectness,thresh);
int box_index = entry_index(l, 0, n*l.w*l.h + i, 0);
dets[count].bbox = get_yolo_box(predictions, l.biases, l.mask[n], box_index, col, row, l.w, l.h, netw, neth, l.w*l.h);//模型推理出偏移量
dets[count].objectness = objectness;
dets[count].classes = l.classes;
for(j = 0; j < l.classes; ++j){
int class_index = entry_index(l, 0, n*l.w*l.h + i, 4 + 1 + j);
float prob = objectness*predictions[class_index];//predictions[class_index]:框中物体是class的概率,prob:置信度
printf("get_yolo_detections1:prob=objectness*predictions[class_index] = %f * predictions[%d] = %f * %f = %f;\n",objectness,class_index,objectness,predictions[class_index],prob);
dets[count].prob[j] = (prob > thresh) ? prob : 0;
printf("get_yolo_detections2:[dets[count].prob[j] = (prob > thresh) ? prob : 0] = [ %f = (%f > %f) ? %f : 0];\n",dets[count].prob[j],prob,thresh,prob);
}
++count;
}
}
correct_yolo_boxes(dets, count, w, h, netw, neth, relative);
return count;
}
...

如有疑问可以留言。

希望可以帮到困惑的你!

<!--
p { margin-bottom: 0.25cm; line-height: 120%; }
-->

darknet-yolov3模型预测框size不正确的原因的相关教程结束。

《darknet-yolov3模型预测框size不正确的原因.doc》

下载本文的Word格式文档,以方便收藏与打印。