YOLOv2 項目中在檢測多類情況下只輸出其中一類 2018-05-27

在訓練yolo模型中,本作者采用了Yolov2的預訓練模型來訓練了門,車,行人,建筑物,樹這五個類別,而在我們sence.name中我們門是第一類(0),車是第二類(1),行人是第三類(2),建筑物是第四類(3),樹是第五類(4)。但是,由于項目要求,本人只想采用得到只有行人的boundingbox作為網(wǎng)絡的輸出而不是訓練的五類,有什么方法能夠實現(xiàn)呢?

首先,第一個方法當然是你可以拿Yolov2的官方提供的預訓練模型進行行人的訓練,這樣模型只會檢測到行人,而在網(wǎng)絡的最后會輸出行人的boundingbox。

其次,COCO數(shù)據(jù)集訓練(包含行人,行人作為第一類),可以改動想改配置文件來實現(xiàn)只輸出一類,此方法不做說明。

最后,但是由于本人時間有限,所以想在原來訓練五類的模型基礎上得到行人(這里請注意,行人在我訓練模型的名字列表中是第三類,其序號是2)如果用上面COCO數(shù)據(jù)集中的方法是行不通的,但是如果我只想得到行人的boundingbox,那該怎么辦呢?

注意:在Yolov2主文件中找到image.c或detect.c(此處作者可能與其他人的文件命名不一樣)文件,并在該文件下的draw_detectiongs函數(shù)里面做相應的修改,修改如下:

(1)在第一個for循環(huán)中加入以下代碼:

if (strcmp(names[class], "person") !=0 )

{

continue;

}

(2)在第二個for循環(huán)中加入以下代碼:

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而已,這樣做檢測的精度當然沒有純訓練行人的精度高,所以如果有充分時間就不建議這樣做,但是如果你想偷懶就可以嘗試一下這種方式。

本篇文章僅代表作者本人的觀點,如有不對的地方請在下方留言,謝謝!

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 超高速音視頻編碼器用法: ffmpeg [options] [[infile options] -i infile...
    吉兇以情遷閱讀 4,663評論 0 4
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,054評論 0 2
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 13,906評論 0 38
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,537評論 0 17
  • 南方濕濕的天氣。 雨罷,夜市的人多了起來,在攢動的人潮里,明快的節(jié)奏嘲弄著耳朵,牽絆著腳步。 幾把四弦琴,一...
    字幕點點點閱讀 267評論 6 8