1. 程式人生 > >DarkNet(1)--新增新層教程(slice層為例)

DarkNet(1)--新增新層教程(slice層為例)

slice layer:作用是將bottom按照引數切分成多個top,可參考Caffe原始碼中的實現

1、原始碼src資料夾下:
新建slice_layer.cslice_layer.h
ps:稍後我會更新到我的GitHub上
ps:已更新,https://github.com/lwplw/re-id_mgn

2、makefile檔案中:
OBJ新增slice_layer.o

3、include/darknet.h檔案中:
(1)LAYER_TYPE新增SLICE

typedef enum {
    CONVOLUTIONAL,
    DECONVOLUTIONAL,
    CONNECTED,
    MAXPOOL,
    SOFTMAX,
    DETECTION,
    DROPOUT,
    CROP,
    ROUTE,
    COST,
    NORMALIZATION,
    AVGPOOL,
    LOCAL,
    SHORTCUT,
    SLICE, // 2018.11.22-lwp
    ACTIVE,
    RNN,
    GRU,
    LSTM,
    CRNN,
    BATCHNORM,
    NETWORK,
    XNOR,
    REGION,
    YOLO,
    ISEG,
    REORG,
    UPSAMPLE,
    LOGXENT,
    L2NORM,
    BLANK
} LAYER_TYPE;

(2)因為新層中定義了新的引數,所以新增:

...
int extra;
int slice_axis; // 2018.11.22-lwp
int slice_num;
int slice_pos;
int truths;
...

4、parser.c檔案中:
(1)新增標頭檔案:

#include "slice_layer.h"

(2)string_to_layer_type函式中新增:

if (strcmp(type, "[slice]")==0) return SLICE;

(3)新增parse_slice函式:

layer parse_slice(list *options, size_params params)
{
    int slice_axis = option_find_int(options, "slice_axis", 2);
    int slice_num = option_find_int(options, "slice_num", 1);
    int slice_pos = option_find_int(options, "slice_pos", 0);

    int batch,h,w,c;
    h = params.h;
    w = params.w;
    c = params.c;
    batch=params.batch;

    layer l = make_slice_layer(batch, w, h, c, slice_axis, slice_num, slice_pos);
    return l;
}

(4)parse_network_cfg中加入:

...
}else if(lt == SHORTCUT){
       l = parse_shortcut(options, params, net);
}else if(lt == SLICE){
       l = parse_slice(options, params); // 2018.11.22-lwp
}else if(lt == DROPOUT){
       l = parse_dropout(options, params);
...

5、network.c檔案中:
(1)新增標頭檔案:

#include "slice_layer.h"

(2)get_layer_string函式中新增:

case SLICE:
    return "slice";

(3)resize_network函式中新增:

...
}else if(l.type == SHORTCUT){
    resize_shortcut_layer(&l, w, h);
}else if(l.type == SLICE){
    resize_slice_layer(&l, w, h); // 2018.11.22-lwp
}else if(l.type == UPSAMPLE){
    resize_upsample_layer(&l, w, h);
...

6、重新編譯DarkNet

make clean
make all -j16