darknet源碼學習


darknet是一個較為輕型的完全基於C與CUDA的開源深度學習框架,其主要特點就是容易安裝,沒有任何依賴項(OpenCV都可以不用),移植性非常好,支持CPU與GPU兩種計算方式。
1、test源碼(泛化過程)
   (1)test image
   a(預測):load_network(network.c) ---> network_predict(network.c) ---> forward_network(network.c) ---> forward_yolo_layer(yolo_layer.c) ----> calc_network_cost(network.c)
   b(后處理):get_network_boxes(network.c) ---> make_network_boxes(network.c) ---> fill_network_boxes(network.c)---> get_yolo_detections(yolo_layer.c)
            do_nms_sort(box.c) ---> draw_detections(image.c) ---> save_image(image.c)
   (2)test 過程中thresh作用
    a:get_yolo_detections接口中:

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;
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];
if(objectness <= thresh) continue;
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];
dets[count].prob[j] = (prob > thresh) ? prob : 0;
}
++count;
}
}
correct_yolo_boxes(dets, count, w, h, netw, neth, relative);
return count;
}

    b:draw_detections接口中:
      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;

2、train源碼(訓練過程)
   (1)根據配置文件解析、創建、配置net的各個層(以卷積層為例),同時配置net的其他參數
   load_network(network.c) ---> parse_network_cfg(parser.c)--->parse_convolutional(parser.c) --->make_convolutional_layer(convolutional_layer.c);
   注意:make_convolutional_layer過程中特別需要注意以下幾個函數指針的配置,分別用來確定前向求損失函數,反向求誤差函數,update函數(用來更新參數)
   void (*forward)   (struct layer, struct network); ---> l.forward = forward_convolutional_layer;
   void (*backward)  (struct layer, struct network); ---> l.backward = backward_convolutional_layer;
   void (*update)    (struct layer, update_args); ---> l.update = update_convolutional_layer;

   parse_network_cfg(section list node的概念處理配置文件)
   總結:該過程最后得到的就是一個根據配置文件創建好的一個net框架, 只差灌入數據

   (2)加載數據
   load_thread(data.c)--->load_data_detection(data.c)--->fill_truth_detection(data.c 讀取圖像的標簽數據 其他數據集也可以在這里作修改 然后更改路徑)

   (3)開始訓練
   train_network(network.c) ---> train_network_datum(network.c 網絡訓練\前向求損失\反向求誤差\最后更新網絡參數) --->forward_network (network.c) ---> backward_network (network.c) ---> update_network(network.c) (forward backward update分別使用對應層的函數進行處理)

   
  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM