在訓練yolo模型中肴熏,本作者采用了Yolov2的預訓練模型來訓練了門蛙吏,車励烦,行人,建筑物屉栓,樹這五個類別,而在我們sence.name中我們門是第一類(0),車是第二類(1),行人是第三類(2)酬土,建筑物是第四類(3),樹是第五類(4)屈呕。但是,由于項目要求岳守,本人只想采用得到只有行人的boundingbox作為網(wǎng)絡的輸出而不是訓練的五類,有什么方法能夠?qū)崿F(xiàn)呢?
首先,第一個方法當然是你可以拿Yolov2的官方提供的預訓練模型進行行人的訓練,這樣模型只會檢測到行人,而在網(wǎng)絡的最后會輸出行人的boundingbox。
其次外傅,COCO數(shù)據(jù)集訓練(包含行人棚辽,行人作為第一類)联逻,可以改動想改配置文件來實現(xiàn)只輸出一類锨推,此方法不做說明锦担。
最后,但是由于本人時間有限磁椒,所以想在原來訓練五類的模型基礎(chǔ)上得到行人(這里請注意堤瘤,行人在我訓練模型的名字列表中是第三類,其序號是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而已茫多,這樣做檢測的精度當然沒有純訓練行人的精度高,所以如果有充分時間就不建議這樣做忽刽,但是如果你想偷懶就可以嘗試一下這種方式天揖。
本篇文章僅代表作者本人的觀點,如有不對的地方請在下方留言跪帝,謝謝今膊!