zoukankan      html  css  js  c++  java
  • darknet-yolov3模型预测框size不正确的原因

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

    解决方法:使得训练的配置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;
    ",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
    ",objectness);
                if(objectness <= thresh) continue;
            printf("obj_index = %d,objectness:%f, thresh:%f
    ",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;
    ",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];
    ",dets[count].prob[j],prob,thresh,prob);
                }
                ++count;
            }
        }
        correct_yolo_boxes(dets, count, w, h, netw, neth, relative);
        return count;
    }
    ...

    如有疑问可以留言。

    希望可以帮到困惑的你!

     

     

     

     

     

  • 相关阅读:
    二、js基本语法
    一、js概览
    浅析URL
    css动画总结
    HTML常用标签
    从头学习HTML1
    了解HTTP协议
    常用ES6语法
    集合框架
    让图片铺满整个页面,自适应拉伸;限制文本行数,多余的显示...(省略号)
  • 原文地址:https://www.cnblogs.com/zhibei/p/12088061.html
Copyright © 2011-2022 走看看