1. 程式人生 > >Yolov3程式碼分析與訓練自己資料集

Yolov3程式碼分析與訓練自己資料集

   現在要針對我們需求引入檢測模型,只檢測人物,然後是影象能側立,這樣人物在裡面佔比更多,也更清晰,也不需要檢測人佔比小的情況,如下是針對這個需求,用的yolov3-tiny模型訓練後的效果。

  

  Yolov3模型網上也講爛了,但是總感覺不看程式碼,不清楚具體實現看講解總是不清晰,在這分析下darknet的實現,給自己解惑,順便也做個筆記。

  首先檢視開啟yolov3.cfg,我們看下網路,可以用netron檢視圖形介面,可以發現網路主要以卷積層構成,shortcut(殘差連線),route(通道組合)三種構成,首先用步長為2的卷積縮小影象一次,然後開始用shortcut(殘差連線)連線一次再用步長為2的卷積縮小影象縮小二次,後面開始不斷用卷積與殘差組合,到開始分支,分出二個部分,每分出一個分支就把主支影象縮小次,最後加上主支部分一共三個分支,就是一共有3個yolo層,其中主支部分縮小了五次,第一次分支縮小三次,第二次分支縮小四次。

  這裡也解答了以前我的一個疑惑,從ResNet網路開始,開始隔層交流,不管是相加還是整合,我疑惑的是如何在檔案這種列表形式下描述分支結構,原來很簡單,一次描述一個分支,然後用route/shortcut記錄分支層,繼續向下描述。

  回到網路部分,這三次分支可以用這圖表示,網路上不知誰的,確實表達了很多要說的,不過有個問題,應該是版本更新了,我手上配置拿的是長寬是608*608,所以下圖需要改一些,如13*13是19*19,大家明白就行,還有主支是32(2^5)倍那個,16與8是第二分支與第一分支,其中先算主支部分,算完主支然後上層卷積層upsample再與第二分支route一起算,第一分支同這邏輯。  

  

   

  這個yolo層就是主支部分,可以看如上分析,在這層特徵圖長寬只有19*19,對應如上 anchors中是根據k-means演算法拿到對應圖集K=9的分類簇框,其中mask=6,7,8指向最後三個大框,主支部分主要檢測大物體,根據前面分析可以知道中框包含了大框的特徵圖結果,而小框包含中大的特徵圖結果,這應該yolov3相比v2針對小物體的識別提高的原因,而shortcut與route則保證網路能加深到一百多層。

  darknet種各層的主要有三個函式,分別是make_xxx_layer, forward_xxx_layer, backward_xxx_layer, 這三個函式make_xxx_layer是初始各種引數,根據引數自動算一些引數,如卷積層,根據傳入特徵圖的大小與核的引數,就能算出傳出特徵圖的大小,以及申請記憶體或是視訊記憶體,forward_xxx_layer表示根據當前層引數計算預測,而backward_xxx_layer根據上層的delta(如上層是yolo層,delta就表示期望輸出-預測輸出)與輸入計算梯度,並更新下層需要的delta,以及還有個update_xxx_layer用於根據backward_xxx_layer計算的梯度來更新引數。而yolo,region,detection,softmax這幾種檢測層相對卷積層來說,沒有引數weight,主要用來計算delta(期望輸出-預測輸出).而反向傳播就是從這個yolo的delta開始的,在yolo的反向傳播中,先把這個delta給卷積層的delta,然後結合前一層的輸出,求得當前層的梯度,並把delta結合當前層的weights求得新的delta,然後下一層卷積層根據這個delta梯度,迴圈下去更新所有引數。

  下面根據程式碼來分析yolov層,先看下make_yolo_layer的實現。

//在yolov3中,n=3 total=9(mask在yolov3中分三組,分別是[0,1,2/3,4,5/6,7,8],n表示分組裡幾個資料,n*3=total)
//608*608下,第一組是19*19,第二組是38*38,第三組是76*76,每組檢查對應索引裡的anchors
layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes)
{
    //在這假設在主支中,其中縮小5次,608/(2^5)=19,這個分支中,w=h=19
    int i;
    layer l = { 0 };
    l.type = YOLO;
    //檢測幾種型別邊框(這分支對應上面anchors[6,7,8]這個用來檢測大邊框)
    l.n = n;
    //如上,在yolov3中,有大中小分別有三個邊框聚合,一共是3*3=9
    //而在yolov3-tiny中,有大小分別三個邊框聚合,一共是3*2=6
    l.total = total;
    //一般來說,訓練為32,預測為1
    l.batch = batch;
    //主支,608/(2^5)=19
    l.h = h;
    l.w = w;
    //如上在主支中,每張特徵圖有19*19個元素,c表示特徵圖個數,n表示對應的anchors[6,7,8]這三個
    //4表示box座標,1是Po(預測機率與IOU正確率)的概率,classes是預測的類別數
    l.c = n * (classes + 4 + 1);
    l.out_w = l.w;
    l.out_h = l.h;
    l.out_c = l.c;
    //檢測一共有多少個類別
    l.classes = classes;
    //計算代價(資料集整體的誤差描述)
    l.cost = calloc(1, sizeof(float));
    //對應表示anchors
    l.biases = calloc(total * 2, sizeof(float));
    //對應如上anchors中n對應需要用到的索引
    if (mask) l.mask = mask;
    else {
        l.mask = calloc(n, sizeof(int));
        for (i = 0; i < n; ++i) {
            l.mask[i] = i;
        }
    }
    l.bias_updates = calloc(n * 2, sizeof(float));
    //當前層batch為1的所有輸出特徵圖包含的元素個數,每個元素為一個float
    l.outputs = h * w*n*(classes + 4 + 1);
    //當前層batch為1時所有輸入特徵圖包含的元素個數,每個元素為一個float
    l.inputs = l.outputs;
    //標籤(真實)資料,這裡90表示如上w*h(19*19中)每個格子最多有90個label。
    //而每個label前面4個float表示box的四個點,後面1個float表示當前類別
    l.truths = 90 * (4 + 1);
    //計算誤差(資料單個的誤差描述),用來表示 期望輸出-真實輸出
    l.delta = calloc(batch*l.outputs, sizeof(float));
    l.output = calloc(batch*l.outputs, sizeof(float));
    for (i = 0; i < total * 2; ++i) {
        l.biases[i] = .5;
    }

    l.forward = forward_yolo_layer;
    l.backward = backward_yolo_layer;
#ifdef GPU
    l.forward_gpu = forward_yolo_layer_gpu;
    l.backward_gpu = backward_yolo_layer_gpu;
    l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
    l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
#endif

    fprintf(stderr, "yolo\n");
    srand(0);

    return l;
}
make_yolo_layer

  以主支來做分析,每張特徵圖有19*19個元素,c表示特徵圖個數,結合上圖,一共有3*(80+4+1)=255,簡單來說,分別對應116,90, 156,198, 373,326這三個聚類簇,其中前85個就是116,90這框的結果,前4個是邊框座標,1個置信度,80個類別概率,一共三個,還有標籤(真實)資料,這裡90表示如上w*h(19*19中)每個格子最多有90個label,而每個label前面4個float表示box的四個點,後面1個float表示當前類別,搞清楚這二個對應排列,在如下的forward_yolo_layer裡,我們才能明白如何計算的delta,程式碼加上註釋有點多,這段就不貼了,程式碼部分說明下。

  主要有二部分,還是先說下,在這裡delta表示 期望輸出-out(不同框架可能不同,我看caffe裡的yolo實現,就是out-期望輸出)。

  第一部分,前面查詢所有特徵圖(在這1batch是三張)裡的所有元素(19*19)裡的所有confidence,準確來說在第三分支19*19個元素,每個元素有三個大框預測,檢測對應所有框裡真實資料最好的box的iou,如果iou大於設定的ignore_thresh,則設delta為0,否則就是0-out(0表示沒有,我們期望輸出是0)。

  第二部分,在對照所有真實label中,先找到這個label是否是大框,如果不是,這個yolo層不管,如果是,繼續看是上面6,7,8中的那一個,我們假設是8,根據真實框的位置確定在特徵圖的位置(19*19中),其8對應255張特徵圖中間的85-170這85張圖,然後比較真實的BOX與對應特徵圖預測的box,算出對應box的 delta,然後是confidence的delta(可以知道,正確的box位置上的元素會算二次confidence損失),然後是類別的delta。各delta比較簡單,如果認為是真的,delta=1-out,如果是錯的,delta=0-out,簡單來說就是期望輸出-out,最後網路cost就是每層yolo的delta的平方和加起來的均值。

  然後是yolo訓練時輸出的各項引數(這圖用的是yolov3-tiny訓練,所以只有16和23這二個yolo層),對比如上16層檢測大的,23檢測小的。

  

  可以看到,count是表示當前層與真實label正確配對的box數,其中所有引數都是針對這個值的平均值,除no obj外,不過從程式碼上來,這個引數意義並不大,所以當前yolo層如果出現nan這個的列印,也是正常的,只是表示當前batch剛好所有圖片都是大框或是小框,所以提高batch的數目可以降低nan出現的機率,不過相應的是,batch提高,可能視訊記憶體就暴了,我用的2070一次用預設的64張視訊記憶體就不夠,只能改成32張。其中avg iou表示當前層正確配對的box的交併比的平均值,class表示表示當前層正確配對類別的平均機率,obj表示confidence = P(object)* IOU,表示預測box包含物件與IOU好壞的評分,0.5R/0.7R:表示iou在0.5/0.7上與正確配對的box的比率。

  搞明白darknet框架各層後,回到我們需求,引入檢測模型,只檢測人物,然後是影象能側立,這樣人物在裡面佔比更多,也更清晰,也不需要檢測人佔比小的情況。先說明下,用的yolov3-tiny,因為可能要每楨檢查並不需要佔太多資源,故使用簡化模型。

  首先篩選滿足條件的資料集,本來準備用coco資料自帶api分析,發現還麻煩些,資料全有了,邏輯並不複雜,用winform自己寫了就行了。

/// <summary>
/// 資料經過funcFilterLabel過濾,過濾後的資料需要全部滿足discardFilterLabel
/// </summary>
/// <param name="instData"></param>
/// <param name="funcFilterLabel">滿足條件就採用</param>
/// <param name="discardFilterLabel">需要所有標籤滿足的條件</param>
/// <returns></returns>
public List<ImageLabel> CreateYoloLabel(instances instData, 
    Func<annotationOD, image, bool> funcFilterLabel,
    Func<annotationOD, image, bool> discardFilterLabel)
{
    List<ImageLabel> labels = new List<ImageLabel>();
    //foreach (var image in instData.images)
    Parallel.ForEach(instData.images, (image image) =>
     {
         var anns = instData.annotations.FindAll(p => p.image_id == image.id && funcFilterLabel(p, image));
         bool bReserved = anns.TrueForAll((annotationOD ao) => discardFilterLabel(ao, image));
         if (anns.Count > 0 && bReserved)
         {
             ImageLabel iml = new ImageLabel();
             iml.imageId = image.id;
             iml.name = image.file_name;
             float dw = 1.0f / image.width;
             float dh = 1.0f / image.height;
             foreach (var ann in anns)
             {
                 BoxIndex boxIndex = new BoxIndex();
                 boxIndex.box.xcenter = (ann.bbox[0] + ann.bbox[2] / 2.0f) * dw;
                 boxIndex.box.ycenter = (ann.bbox[1] + ann.bbox[3] / 2.0f) * dh;

                 boxIndex.box.width = ann.bbox[2] * dw;
                 boxIndex.box.height = ann.bbox[3] * dh;
                 //註冊
                 boxIndex.catId = findCategoryId(instData.categories, ann.category_id);
                 if (boxIndex.catId >= 0)
                     iml.boxs.Add(boxIndex);
             }
             if (iml.boxs.Count > 0)
             {
                 lock (labels)
                 {
                     labels.Add(iml);
                 }
             }
         }

     });
    return labels;
}

public async void BuildYoloData(DataPath dataPath, string txtListName)
{
    instances instance = new instances();
    if (!File.Exists(dataPath.AnnotationPath))
    {
        setText(dataPath.AnnotationPath + " 路徑不存在.");
        return;
    }
    setText("正在讀取檔案中:" + Environment.NewLine + dataPath.AnnotationPath);
    var jsonTex = await Task.FromResult(File.ReadAllText(dataPath.AnnotationPath));
    setText("正在解析檔案中:" + Environment.NewLine + dataPath.AnnotationPath);
    instance = await Task.FromResult(JsonConvert.DeserializeObject<instances>(jsonTex));
    setText("正在分析檔案包含人物影象:" + instance.images.Count + "個");
    List<ImageLabel> labels = await Task.FromResult(COCODataManager.Instance.CreateYoloLabel(
        instance,
        (annotationOD at, image image) =>
         {
             //是否人類
             return at.category_id == 1;
         },
        (annotationOD at, image image) =>
         {
             //是否滿足所有人類標籤都面積佔比都大於十分之一
             return (at.bbox[2] / image.width) * (at.bbox[3] / image.height) > 0.1f;
         }));
    setText("正在生成label檔案:" + Environment.NewLine + dataPath.LabelPath);
    if (!Directory.Exists(dataPath.LabelPath))
    {
        Directory.CreateDirectory(dataPath.LabelPath);
    }
    await Task.Run(() =>
    {
        Parallel.ForEach(labels, (ImageLabel imageLabel) =>
        {
            string fileName = Path.Combine(dataPath.LabelPath,
                Path.GetFileNameWithoutExtension(imageLabel.name) + ".txt");
            using (var file = new StreamWriter(Path.Combine(dataPath.LabelPath, fileName), false))
            {
                foreach (var label in imageLabel.boxs)
                {
                    file.WriteLine(label.catId + " " + label.box.xcenter + " " + label.box.ycenter +
                        " " + label.box.width + " " + label.box.height + " ");
                }
            }
        });
        string path = Path.Combine(Directory.GetParent(dataPath.LabelPath).FullName, 
            txtListName + ".txt");
        using (var file = new StreamWriter(path, false))
        {
            foreach (var label in labels)
            {
                string lpath = Path.Combine(dataPath.DestImagePath, label.name);
                file.WriteLine(lpath);
            }
        }
    });
    setText("正在複製需要的檔案到指定目錄:" + dataPath.AnnotationPath);
    await Task.Run(() =>
    {
        Parallel.ForEach(labels, (ImageLabel imageLabel) =>
        {
            string spath = Path.Combine(dataPath.SourceImagePath, imageLabel.name);
            string dpsth = Path.Combine(dataPath.DestImagePath, imageLabel.name);
            if (File.Exists(spath))
                File.Copy(spath, dpsth, true);
        });
    });
    setText("全部完成");
}
CreateYoloLabel

  只有一點需要注意,我們只記錄人類box標籤資料,但是這些標籤需要全部大於特定面積的圖,如果你選擇的上面還有小面積人物,又不給box標籤訓練,最後yolo層並沒面板的損失函式,會造成干擾,在這本來也沒有檢查小面積人物的需求,yolov3-tiny層數本也不多,需求泛化後精度很低。

  darknet本身並沒有針對側立做適配,我們需要修改相應邏輯來完成,很簡單,一張圖四個方向旋轉後,同樣修改相應的truth box就行了是,darknet資料載入主要在data.c這部分,找到我們使用的載入邏輯load_data_detection裡,主要針對如下修改。

data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, int classes,
    float jitter, float hue, float saturation, float exposure)
{
....................
        random_distort_image(sized, hue, saturation, exposure);

        int flip = rand() % 2;
        if (flip)
            flip_image(sized);
        int vflip = rand() % 2;
        if (vflip)
            vflip_image(sized);
        int trans = rand() % 2;
        if (trans)
            transpose_image(sized);
        d.X.vals[i] = sized.data;

        fill_truth_detection(random_paths[i], boxes, d.y.vals[i], classes, flip,
            -dx / w, -dy / h, nw / w, nh / h, vflip, trans);

        free_image(orig);
....................
}

void correct_boxes(box_label *boxes, int n, float dx, float dy, float sx, 
    float sy, int flip, int vflip, int trans)
{
    int i;
    for (i = 0; i < n; ++i) {
        if (boxes[i].x == 0 && boxes[i].y == 0) {
            boxes[i].x = 999999;
            boxes[i].y = 999999;
            boxes[i].w = 999999;
            boxes[i].h = 999999;
            continue;
        }
        boxes[i].left = boxes[i].left  * sx - dx;
        boxes[i].right = boxes[i].right * sx - dx;
        boxes[i].top = boxes[i].top   * sy - dy;
        boxes[i].bottom = boxes[i].bottom* sy - dy;

        if (flip)
        {
            float swap = boxes[i].left;
            boxes[i].left = 1. - boxes[i].right;
            boxes[i].right = 1. - swap;
        }
        if (vflip)
        {
            float swap = boxes[i].top;
            boxes[i].top = 1. - boxes[i].bottom;
            boxes[i].bottom = 1. - swap;
        }

        boxes[i].left = constrain(0, 1, boxes[i].left);
        boxes[i].right = constrain(0, 1, boxes[i].right);
        boxes[i].top = constrain(0, 1, boxes[i].top);
        boxes[i].bottom = constrain(0, 1, boxes[i].bottom);

        boxes[i].x = (boxes[i].left + boxes[i].right) / 2;
        boxes[i].y = (boxes[i].top + boxes[i].bottom) / 2;
        boxes[i].w = (boxes[i].right - boxes[i].left);
        boxes[i].h = (boxes[i].bottom - boxes[i].top);

        boxes[i].w = constrain(0, 1, boxes[i].w);
        boxes[i].h = constrain(0, 1, boxes[i].h);

        if (trans)
        {
            float temp = boxes[i].x;
            boxes[i].x = boxes[i].y;
            boxes[i].y = temp;
            temp = boxes[i].w;
            boxes[i].w = boxes[i].h;
            boxes[i].h = temp;
        }
    }
}
load_data_detection

  然後拿到yolov3-tiny.cfg檔案,先把burn_in修改成1,我們沒有預精確資料,最開始就以原始學習率開始訓練,對應二個yolo層裡的classes改成一,記的前面說過,這個層就是分析上面的卷積層,故上面的輸出filters=3*(4+1+1)=18,第一次訓練後發現五W次就沒怎麼收斂了,分析了下,應該是anchors導致的,當影象側立後,這裡的也應該有類似資料,故選擇全大面積人物二種特定框,分別取側立,這裡正確的搞法應該是用k-means再重新取K=2算一下所有現在特定圖的框,分別對應大框與小框,然後側立下,大框就有二個,小框二個,num=4,mask每層設二個索引,或是K=4,然後讓上面來,後面抽出時間完善這步,現暫時用如下資料anchors = 100,100, 119,59, 59,119, 200,200, 326,373, 373,326,這些調整後,現訓練21W次,也還一直收斂中。

  訓練與驗證根據他自身的train_yolo/validata_yolo修改下,自己可以打印出自己想要的資訊,驗證可以結合opencv顯示我們想要的各種圖形比對效果。

  

&n