YOLOv2 项目中在检测多类情况下只输出其中一类

在训练yolo模型中,本作者采用了Yolov2的预训练模型来训练了门,车,行人,建筑物,树这五个类别,而在我们sence.name中我们门是第一类(0),车是第二类(1),行人是第三类(2),建筑物是第四类(3),树是第五类(4)。但是,由于项目要求,本人只想采用得到只有行人的boundingbox作为网络的输出而不是训练的五类,有什么方法能够实现呢?

首先,第一个方法当然是你可以拿Yolov2的官方提供的预训练模型进行行人的训练,这样模型只会检测到行人,而在网络的最后会输出行人的boundingbox。

其次,COCO数据集训练(包含行人,行人作为第一类),可以改动想改配置文件来实现只输出一类,此方法不做说明。

最后,但是由于本人时间有限,所以想在原来训练五类的模型基础上得到行人(这里请注意,行人在我训练模型的名字列表中是第三类,其序号是2)如果用上面COCO数据集中的方法是行不通的,但是如果我只想得到行人的boundingbox,那该怎么办呢?

注意:在Yolov2主文件中找到image.c或detect.c(此处作者可能与其他人的文件命名不一样)文件,并在该文件下的draw_detectiongs函数里面做相应的修改,修改如下:

(1)在第一个for循环中加入以下代码:

if (strcmp(names[class], "person") !=0 )
{
continue;
}

(2)在第二个for循环中加入以下代码:

if(class != 2)
{
continue;
}

image.c:

void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes)
{
    int i;

    for (i = 0; i < num; ++i) {
        int class = max_index(probs[i], classes);
        float prob = probs[i][class];
        
      if (strcmp(names[class], "person") !=0 )
      {
          continue;
      }
        或
    
    if(class != 2)
    {
        continue;
    }
         if (prob > thresh) { int width = im.h * .006; if (0) { width = pow(prob, 1. / 2.) * 10 + 1; alphabet = 0; } int offset = class * 123457 % classes; float red = get_color(2, offset, classes); float green = get_color(1, offset, classes); float blue = get_color(0, offset, classes); float rgb[3]; rgb[0] = red; rgb[1] = green; rgb[2] = blue; box b = boxes[i]; int left = (b.x - b.w / 2.) * im.w; int right = (b.x + b.w / 2.) * im.w; int top = (b.y - b.h / 2.) * im.h; int bot = (b.y + b.h / 2.) * im.h; if (left < 0) left = 0; if (right > im.w - 1) right = im.w - 1; if (top < 0) top = 0; if (bot > im.h - 1) bot = im.h - 1; draw_box_width(im, left, top, right, bot, width, red, green, blue); if (alphabet) { image label = get_label(alphabet, names[class], (im.h * .03) / 10); draw_label(im, top + width, left, label, rgb); free_image(label); } if (masks) { image mask = float_to_image(14, 14, 1, masks[i]); image resized_mask = resize_image(mask, b.w * im.w, b.h * im.h); image tmask = threshold_image(resized_mask, .5); embed_image(tmask, im, left, top); free_image(mask); free_image(resized_mask); free_image(tmask); } } }}


即使此处添加了一个类别的判断其是否为行人,但是模型做检测时是检测出了五类,只不过显示其中的行人的boundingbox而已,这样做检测的精度当然没有纯训练行人的精度高,所以如果有充分时间就不建议这样做,但是如果你想偷懒就可以尝试一下这种方式。

本篇文章仅代表作者本人的观点,如有不对的地方请在下方留言,谢谢!


  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值