AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.8k stars 7.97k forks source link

Train problem with mobilenet #3178

Open ChenCong7375 opened 5 years ago

ChenCong7375 commented 5 years ago

I modified your work ,added mobilenet in it and compiled successfully,then I test it successfully with
with classifier.but when I train ,some problem occurred. image

ChenCong7375 commented 5 years ago

When I test it,the result is below: image

ChenCong7375 commented 5 years ago

1 add depthwise convolutional layer into darknet First, open this file /src/parser.c.

add below into it

include "utils.h"

++ #include "depthwise_convolutional_layer.h" //added by chen find function 'string_to_layer_type' added below into it if (strcmp(type, "[upsample]")==0) return UPSAMPLE; ++ if (strcmp(type, "[depthwise_convolutional]") == 0) return DEPTHWISE_CONVOLUTIONAL; //added by chen return BLANK; find function 'parse_network_cfg' added below into it if(lt == CONVOLUTIONAL){ l = parse_convolutional(options, params); } ++ else if (lt == DEPTHWISE_CONVOLUTIONAL) { ++ l = parse_depthwise_convolutional(options, params); //added by chen ++ } else if(lt == DECONVOLUTIONAL){ l = parse_deconvolutional(options, params); } add this function 'parse_depthwise_convolutional' into it //added by chen depthwise_convolutional_layer parse_depthwise_convolutional(list *options, size_params params) { int size = option_find_int(options, "size", 1); int stride = option_find_int(options, "stride", 1); int pad = option_find_int_quiet(options, "pad", 0); int padding = option_find_int_quiet(options, "padding", 0); if (pad) padding = size / 2;

char *activation_s = option_find_str(options, "activation", "logistic"); 
ACTIVATION activation = get_activation(activation_s); 

int batch, h, w, c; 
h = params.h;
w = params.w; 
c = params.c; 
batch = params.batch; 
if (!(h && w && c)) error("Layer before convolutional layer must output image."); 
int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0); 

depthwise_convolutional_layer layer = make_depthwise_convolutional_layer(batch, h, w, c, size, stride, padding, activation, batch_normalize); 
layer.flipped = option_find_int_quiet(options, "flipped", 0); 
layer.dot = option_find_float_quiet(options, "dot", 0);

return layer;

} find function 'load_weights_upto' added below into it if (l.dontload) continue; ++ if (l.type == DEPTHWISE_CONVOLUTIONAL) { ++ load_depthwise_convolutional_weights(l, fp);//added by chen ++ } add this function 'load_depthwise_convolutional_weights' into it

//added by chen void load_depthwise_convolutional_weights(layer l, FILE fp) { int num = l.nl.sizel.size; fread(l.biases, sizeof(float), l.n, fp); if (l.batch_normalize && (!l.dontloadscales)) { fread(l.scales, sizeof(float), l.n, fp); fread(l.rolling_mean, sizeof(float), l.n, fp); fread(l.rolling_variance, sizeof(float), l.n, fp); if (0) { int i; for (i = 0; i < l.n; ++i) { printf("%g, ", l.rolling_mean[i]); } printf("\n"); for (i = 0; i < l.n; ++i) { printf("%g, ", l.rolling_variance[i]); } printf("\n"); } if (0) { fill_cpu(l.n, 0, l.rolling_mean, 1); fill_cpu(l.n, 0, l.rolling_variance, 1); } } fread(l.weights, sizeof(float), num, fp); if (l.flipped) { //transpose_matrix(l.weights, l.cl.size*l.size, l.n); }

ifdef GPU

if (gpu_index >= 0) { 
    push_depthwise_convolutional_layer(l);
}

endif

} find function 'save_weights_upto' added below into it

if (l.dontsave) continue; ++ if (l.type == DEPTHWISE_CONVOLUTIONAL) { ++    save_depthwise_convolutional_weights(l, fp); //added by chen ++ } add this function 'save_depthwise_convolutional_weights' into it

//added by chen void save_depthwise_convolutional_weights(layer l, FILE *fp) {

ifdef GPU

if (gpu_index >= 0) { 
    pull_depthwise_convolutional_layer(l);
} 

endif

int num = l.n*l.size*l.size;
fwrite(l.biases, sizeof(float), l.n, fp);
if (l.batch_normalize) { 
    fwrite(l.scales, sizeof(float), l.n, fp); 
    fwrite(l.rolling_mean, sizeof(float), l.n, fp); 
    fwrite(l.rolling_variance, sizeof(float), l.n, fp);
} 
fwrite(l.weights, sizeof(float), num, fp);

} Second, open this file /src/network.c .

add below into it

include "data.h"

++ #include "depthwise_convolutional_layer.h" // added by chen add below into it

if(l.type == CONVOLUTIONAL){ resize_convolutional_layer(&l, w, h); } ++ else if(l.type == DEPTHWISE_CONVOLUTIONAL){ ++ resize_depthwise_convolutional_layer(&l, w, h); //added by chen ++ } Third,open this file /include/darknet.h and add below into it.

BLANK, ++ DEPTHWISE_CONVOLUTIONAL //added by chen Final, put these files 'depthwise_convolutional_layer.h depthwise_convolutional_layer.c depthwise_convolutional_kernels.cu' into /src.

sde123 commented 5 years ago

@ChenCong7375 Hi, do you test the trained model in the video and got the any result video?

AlexeyAB commented 5 years ago

@ChenCong7375 Hi,

ChenCong7375 commented 5 years ago

OK I know that, thank u very much. I am going to see the latest version of Darknet that supports depthwise/grouped-convolution.

TaihuLight commented 5 years ago

@AlexeyAB (1) In the version of Darknet https://github.com/AlexeyAB/darknet/commit/f0582446f26ada84144eeea2721a0aafd7950b73, Does it support the depthwise/grouped-convolution with CPU and CUDA? (2) If not, do you have plan to implement the depthwise/grouped-convolution in your repo? Since many developers hope that your Darknet supports depthwise/grouped-convolution with CPU and CUDA, such as https://github.com/AlexeyAB/darknet/issues/2613 https://github.com/AlexeyAB/darknet/issues/1684

AlexeyAB commented 5 years ago

@TaihuLight Yes, it supports depthwise/grouped-convolution with CPU and CUDA (CUDNN=0 and CUDNN=1)

Nico1ee1995 commented 5 years ago

hi @AlexeyAB I use group convolution, it can work well. but group convolution is slow than standard convolution. Can you give me some advice? thank you.

Makefile:

GPU=1
CUDNN=0
CUDNN_HALF=0
OPENCV=1
AVX=0
OPENMP=0
LIBSO=0
AlexeyAB commented 5 years ago

@Nico1ee1995 Use GPU=1 CUDNN=1

Nico1ee1995 commented 5 years ago

hi @AlexeyAB I install cudnn version: 7.1.2 then I set: GPU=1 & CUDNN=1 But I get bad news:

Demo
   layer   filters  size/strd(dil)      input                output
   0 
 cuDNN status Error in: file: ./src/convolutional_layer.c : () : line: 236 : build time: Jun 28 2019 - 13:19:38 
cuDNN Error: CUDNN_STATUS_BAD_PARAM
cuDNN Error: CUDNN_STATUS_BAD_PARAM: File exists
darknet: ./src/utils.c:293: error: Assertion `0' failed.
Aborted (core dumped)
AlexeyAB commented 5 years ago

@Nico1ee1995 What command do you use? What GPU do you use? What cfg-file do you use?

Nico1ee1995 commented 5 years ago

@AlexeyAB

command: /.darknet detector demo cfg/data-train.data cfg/darknet.cfg backup/darknet/darknet_6800.weights video/video.mp4 -out_filename demo.avi

GPU: NVIDIA GeForce GTX 1080Ti

cfg file: darknet.txt

The strange thing is that I can work very well with the previous version(not added group conv's version). Please give me some suggestion, thank you.

AlexeyAB commented 5 years ago

@Nico1ee1995

You get an error at - that can be used only on cuDNN 7.2 and higher: https://github.com/AlexeyAB/darknet/blob/88cccfcad4f9591a429c1e71c88a42e0e81a5e80/src/convolutional_layer.c#L235-L237

But you wrote that you use cuDNN 7.1, so it can't use this line.

I install cudnn version: 7.1.2

Nico1ee1995 commented 5 years ago

I install cuDNN 7.3.0 ,and use the latest Darknet version. Now, it can work well, thank you. However, groups convolution is still slow then standard convolution. (fps: about 7) groups cfg file:mobilenetv2.txt

AlexeyAB commented 5 years ago

@Nico1ee1995 This is how group convolution is implemented in cuDNN.

It seems TensorFlow uses its own grouped convolution implementation: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/depthwise_conv_op_gpu.h

Nico1ee1995 commented 5 years ago

so group convolution slow is normal? Is there any way to solve it? thanks again!

AlexeyAB commented 5 years ago

@Nico1ee1995 Just wait when nVidia will improve it in cuDNN and their GPUs.

Nico1ee1995 commented 5 years ago

@AlexeyAB thanks a lot!

AlexeyAB commented 5 years ago

@Nico1ee1995

You can try to use new state-of-arts models EfficientNets which are faster and more accurate than MobileNets.

Also there is added EfficientNet B0 XNOR.