DarkNet(1)--添加新层教程(slice层为例)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lwplwf/article/details/84339881

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

猜你喜欢

转载自blog.csdn.net/lwplwf/article/details/84339881