├── Train ├── src │ ├── classifier.h │ ├── layer.h │ ├── demo.h │ ├── parser.h │ ├── list.h │ ├── tree.h │ ├── matrix.h │ ├── col2im.h │ ├── im2col.h │ ├── box.h │ ├── l2norm_layer.h │ ├── cuda.h │ ├── logistic_layer.h │ ├── option_list.h │ ├── upsample_layer.h │ ├── activation_layer.h │ ├── batchnorm_layer.h │ ├── shortcut_layer.h │ ├── region_layer.h │ ├── lstm_layer.h │ ├── crop_layer.h │ ├── reorg_layer.h │ ├── route_layer.h │ ├── dropout_layer.h │ ├── yolo_layer.h │ ├── detection_layer.h │ ├── cost_layer.h │ ├── softmax_layer.h │ ├── gru_layer.h │ ├── avgpool_layer.h │ ├── network.h │ ├── rnn_layer.h │ ├── maxpool_layer.h │ ├── normalization_layer.h │ ├── crnn_layer.h │ ├── connected_layer.h │ ├── deconvolutional_layer.h │ ├── gemm.h │ ├── local_layer.h │ ├── mix_layer.h │ ├── focal_layer.h │ ├── dropout_layer_kernels.cu │ ├── col2im.c │ ├── im2col.c │ ├── list.c │ ├── dropout_layer.c │ ├── utils.h │ ├── avgpool_layer_kernels.cu │ ├── activation_layer.c │ ├── l2norm_layer.c │ ├── avgpool_layer.c │ ├── logistic_layer.c │ ├── convolutional_layer.h │ ├── im2col_kernels.cu │ ├── data.h │ ├── image.h │ ├── col2im_kernels.cu │ ├── activations.h │ ├── crop_layer.c │ ├── shortcut_layer.c │ ├── maxpool_layer_kernels.cu │ ├── upsample_layer.c │ ├── option_list.c │ ├── softmax_layer.c │ └── activations.c ├── cfg │ ├── architecture.png │ ├── valid.cfg │ └── train.cfg ├── script │ ├── valid_model.sh │ └── train_model.sh ├── example │ ├── art.c │ ├── swag.c │ ├── dice.c │ └── super.c ├── data │ ├── dataset │ │ └── divided_dataset │ │ │ └── train_dataset_cls.txt │ └── script │ │ ├── divide_dataset_randomly.py │ │ ├── divide_dataset.py │ │ └── generate_dataset.py └── Makefile └── Inference ├── src ├── classifier.h ├── layer.h ├── demo.h ├── parser.h ├── list.h ├── tree.h ├── matrix.h ├── col2im.h ├── box.h ├── im2col.h ├── l2norm_layer.h ├── cuda.h ├── logistic_layer.h ├── option_list.h ├── activation_layer.h ├── upsample_layer.h ├── batchnorm_layer.h ├── shortcut_layer.h ├── region_layer.h ├── crop_layer.h ├── lstm_layer.h ├── reorg_layer.h ├── route_layer.h ├── dropout_layer.h ├── yolo_layer.h ├── detection_layer.h ├── cost_layer.h ├── softmax_layer.h ├── gru_layer.h ├── avgpool_layer.h ├── network.h ├── rnn_layer.h ├── maxpool_layer.h ├── normalization_layer.h ├── crnn_layer.h ├── connected_layer.h ├── deconvolutional_layer.h ├── gemm.h ├── local_layer.h ├── focal_layer.h ├── dropout_layer_kernels.cu ├── col2im.c ├── im2col.c ├── list.c ├── dropout_layer.c ├── utils.h ├── avgpool_layer_kernels.cu ├── activation_layer.c ├── l2norm_layer.c ├── avgpool_layer.c ├── logistic_layer.c ├── upsample_layer.c ├── convolutional_layer.h ├── im2col_kernels.cu ├── data.h ├── col2im_kernels.cu ├── image.h ├── activations.h ├── crop_layer.c ├── shortcut_layer.c ├── maxpool_layer_kernels.cu ├── option_list.c ├── softmax_layer.c └── activations.c ├── script └── main.py ├── Makefile └── cfg └── valid.cfg /Train/src/classifier.h: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Inference/src/classifier.h: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Train/src/layer.h: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | -------------------------------------------------------------------------------- /Inference/src/layer.h: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | -------------------------------------------------------------------------------- /Train/src/demo.h: -------------------------------------------------------------------------------- 1 | #ifndef DEMO_H 2 | #define DEMO_H 3 | 4 | #include "image.h" 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /Inference/src/demo.h: -------------------------------------------------------------------------------- 1 | #ifndef DEMO_H 2 | #define DEMO_H 3 | 4 | #include "image.h" 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /Train/cfg/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jndeng/DACSDC-DeepZ/HEAD/Train/cfg/architecture.png -------------------------------------------------------------------------------- /Train/src/parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | #include "darknet.h" 4 | #include "network.h" 5 | 6 | void save_network(network net, char *filename); 7 | void save_weights_double(network net, char *filename); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /Inference/src/parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | #include "darknet.h" 4 | #include "network.h" 5 | 6 | void save_network(network net, char *filename); 7 | void save_weights_double(network net, char *filename); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /Train/src/list.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | #include "darknet.h" 4 | 5 | list *make_list(); 6 | int list_find(list *l, void *val); 7 | 8 | void list_insert(list *, void *); 9 | 10 | 11 | void free_list_contents(list *l); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Inference/src/list.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | #include "darknet.h" 4 | 5 | list *make_list(); 6 | int list_find(list *l, void *val); 7 | 8 | void list_insert(list *, void *); 9 | 10 | 11 | void free_list_contents(list *l); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Train/src/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_H 2 | #define TREE_H 3 | #include "darknet.h" 4 | 5 | int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride); 6 | float get_hierarchy_probability(float *x, tree *hier, int c, int stride); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /Inference/src/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_H 2 | #define TREE_H 3 | #include "darknet.h" 4 | 5 | int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride); 6 | float get_hierarchy_probability(float *x, tree *hier, int c, int stride); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /Train/src/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef MATRIX_H 2 | #define MATRIX_H 3 | #include "darknet.h" 4 | 5 | matrix copy_matrix(matrix m); 6 | void print_matrix(matrix m); 7 | 8 | matrix hold_out_matrix(matrix *m, int n); 9 | matrix resize_matrix(matrix m, int size); 10 | 11 | float *pop_column(matrix *m, int c); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Inference/src/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef MATRIX_H 2 | #define MATRIX_H 3 | #include "darknet.h" 4 | 5 | matrix copy_matrix(matrix m); 6 | void print_matrix(matrix m); 7 | 8 | matrix hold_out_matrix(matrix *m, int n); 9 | matrix resize_matrix(matrix m, int size); 10 | 11 | float *pop_column(matrix *m, int c); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Train/src/col2im.h: -------------------------------------------------------------------------------- 1 | #ifndef COL2IM_H 2 | #define COL2IM_H 3 | 4 | void col2im_cpu(float* data_col, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_im); 7 | 8 | #ifdef GPU 9 | void col2im_gpu(float *data_col, 10 | int channels, int height, int width, 11 | int ksize, int stride, int pad, float *data_im); 12 | #endif 13 | #endif 14 | -------------------------------------------------------------------------------- /Inference/src/col2im.h: -------------------------------------------------------------------------------- 1 | #ifndef COL2IM_H 2 | #define COL2IM_H 3 | 4 | void col2im_cpu(float* data_col, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_im); 7 | 8 | #ifdef GPU 9 | void col2im_gpu(float *data_col, 10 | int channels, int height, int width, 11 | int ksize, int stride, int pad, float *data_im); 12 | #endif 13 | #endif 14 | -------------------------------------------------------------------------------- /Train/src/im2col.h: -------------------------------------------------------------------------------- 1 | #ifndef IM2COL_H 2 | #define IM2COL_H 3 | 4 | void im2col_cpu(float* data_im, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_col); 7 | 8 | #ifdef GPU 9 | 10 | void im2col_gpu(float *im, 11 | int channels, int height, int width, 12 | int ksize, int stride, int pad,float *data_col); 13 | 14 | #endif 15 | #endif 16 | -------------------------------------------------------------------------------- /Inference/src/box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include "darknet.h" 4 | 5 | #define MMAX(a,b) (((a) > (b)) ? (a) : (b)) 6 | #define MMIN(a,b) (((a) < (b)) ? (a) : (b)) 7 | 8 | typedef struct{ 9 | float dx, dy, dw, dh; 10 | } dbox; 11 | 12 | float box_rmse(box a, box b); 13 | dbox diou(box a, box b); 14 | box decode_box(box b, box anchor); 15 | box encode_box(box b, box anchor); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /Inference/src/im2col.h: -------------------------------------------------------------------------------- 1 | #ifndef IM2COL_H 2 | #define IM2COL_H 3 | 4 | void im2col_cpu(float* data_im, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_col); 7 | 8 | #ifdef GPU 9 | 10 | void im2col_gpu(float *im, 11 | int channels, int height, int width, 12 | int ksize, int stride, int pad,float *data_col); 13 | 14 | #endif 15 | #endif 16 | -------------------------------------------------------------------------------- /Train/src/box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include "darknet.h" 4 | 5 | #define MMAX(a,b) (((a) > (b)) ? (a) : (b)) 6 | #define MMIN(a,b) (((a) < (b)) ? (a) : (b)) 7 | 8 | typedef struct{ 9 | float dx, dy, dw, dh; 10 | } dbox; 11 | 12 | float box_rmse(box a, box b); 13 | dbox diou(box a, box b); 14 | box decode_box(box b, box anchor); 15 | box encode_box(box b, box anchor); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /Train/src/l2norm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef L2NORM_LAYER_H 2 | #define L2NORM_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_l2norm_layer(int batch, int inputs); 7 | void forward_l2norm_layer(const layer l, network net); 8 | void backward_l2norm_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_l2norm_layer_gpu(const layer l, network net); 12 | void backward_l2norm_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Inference/src/l2norm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef L2NORM_LAYER_H 2 | #define L2NORM_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_l2norm_layer(int batch, int inputs); 7 | void forward_l2norm_layer(const layer l, network net); 8 | void backward_l2norm_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_l2norm_layer_gpu(const layer l, network net); 12 | void backward_l2norm_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Train/src/cuda.h: -------------------------------------------------------------------------------- 1 | #ifndef CUDA_H 2 | #define CUDA_H 3 | 4 | #include "darknet.h" 5 | 6 | #ifdef GPU 7 | 8 | void check_error(cudaError_t status); 9 | cublasHandle_t blas_handle(); 10 | int *cuda_make_int_array(int *x, size_t n); 11 | void cuda_random(float *x_gpu, size_t n); 12 | float cuda_compare(float *x_gpu, float *x, size_t n, char *s); 13 | dim3 cuda_gridsize(size_t n); 14 | 15 | #ifdef CUDNN 16 | cudnnHandle_t cudnn_handle(); 17 | #endif 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /Inference/src/cuda.h: -------------------------------------------------------------------------------- 1 | #ifndef CUDA_H 2 | #define CUDA_H 3 | 4 | #include "darknet.h" 5 | 6 | #ifdef GPU 7 | 8 | void check_error(cudaError_t status); 9 | cublasHandle_t blas_handle(); 10 | int *cuda_make_int_array(int *x, size_t n); 11 | void cuda_random(float *x_gpu, size_t n); 12 | float cuda_compare(float *x_gpu, float *x, size_t n, char *s); 13 | dim3 cuda_gridsize(size_t n); 14 | 15 | #ifdef CUDNN 16 | cudnnHandle_t cudnn_handle(); 17 | #endif 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /Train/src/logistic_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGISTIC_LAYER_H 2 | #define LOGISTIC_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_logistic_layer(int batch, int inputs); 7 | void forward_logistic_layer(const layer l, network net); 8 | void backward_logistic_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_logistic_layer_gpu(const layer l, network net); 12 | void backward_logistic_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Inference/src/logistic_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGISTIC_LAYER_H 2 | #define LOGISTIC_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_logistic_layer(int batch, int inputs); 7 | void forward_logistic_layer(const layer l, network net); 8 | void backward_logistic_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_logistic_layer_gpu(const layer l, network net); 12 | void backward_logistic_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Train/src/option_list.h: -------------------------------------------------------------------------------- 1 | #ifndef OPTION_LIST_H 2 | #define OPTION_LIST_H 3 | #include "list.h" 4 | 5 | typedef struct{ 6 | char *key; 7 | char *val; 8 | int used; 9 | } kvp; 10 | 11 | 12 | int read_option(char *s, list *options); 13 | void option_insert(list *l, char *key, char *val); 14 | char *option_find(list *l, char *key); 15 | float option_find_float(list *l, char *key, float def); 16 | float option_find_float_quiet(list *l, char *key, float def); 17 | void option_unused(list *l); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Inference/src/option_list.h: -------------------------------------------------------------------------------- 1 | #ifndef OPTION_LIST_H 2 | #define OPTION_LIST_H 3 | #include "list.h" 4 | 5 | typedef struct{ 6 | char *key; 7 | char *val; 8 | int used; 9 | } kvp; 10 | 11 | 12 | int read_option(char *s, list *options); 13 | void option_insert(list *l, char *key, char *val); 14 | char *option_find(list *l, char *key); 15 | float option_find_float(list *l, char *key, float def); 16 | float option_find_float_quiet(list *l, char *key, float def); 17 | void option_unused(list *l); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Train/src/upsample_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef UPSAMPLE_LAYER_H 2 | #define UPSAMPLE_LAYER_H 3 | #include "darknet.h" 4 | 5 | layer make_upsample_layer(int batch, int w, int h, int c, int stride); 6 | void forward_upsample_layer(const layer l, network net); 7 | void backward_upsample_layer(const layer l, network net); 8 | void resize_upsample_layer(layer *l, int w, int h); 9 | 10 | #ifdef GPU 11 | void forward_upsample_layer_gpu(const layer l, network net); 12 | void backward_upsample_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Train/src/activation_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATION_LAYER_H 2 | #define ACTIVATION_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation); 9 | 10 | void forward_activation_layer(layer l, network net); 11 | void backward_activation_layer(layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_activation_layer_gpu(layer l, network net); 15 | void backward_activation_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /Inference/src/activation_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATION_LAYER_H 2 | #define ACTIVATION_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation); 9 | 10 | void forward_activation_layer(layer l, network net); 11 | void backward_activation_layer(layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_activation_layer_gpu(layer l, network net); 15 | void backward_activation_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /Inference/src/upsample_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef UPSAMPLE_LAYER_H 2 | #define UPSAMPLE_LAYER_H 3 | #include "darknet.h" 4 | 5 | layer make_upsample_layer(int batch, int w, int h, int c, int out_w, int out_h); 6 | void forward_upsample_layer(const layer l, network net); 7 | void backward_upsample_layer(const layer l, network net); 8 | void resize_upsample_layer(layer *l, int w, int h); 9 | 10 | #ifdef GPU 11 | void forward_upsample_layer_gpu(const layer l, network net); 12 | void backward_upsample_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Train/src/batchnorm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef BATCHNORM_LAYER_H 2 | #define BATCHNORM_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_batchnorm_layer(int batch, int w, int h, int c); 9 | void forward_batchnorm_layer(layer l, network net); 10 | void backward_batchnorm_layer(layer l, network net); 11 | 12 | #ifdef GPU 13 | void forward_batchnorm_layer_gpu(layer l, network net); 14 | void backward_batchnorm_layer_gpu(layer l, network net); 15 | void pull_batchnorm_layer(layer l); 16 | void push_batchnorm_layer(layer l); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Inference/src/batchnorm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef BATCHNORM_LAYER_H 2 | #define BATCHNORM_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_batchnorm_layer(int batch, int w, int h, int c); 9 | void forward_batchnorm_layer(layer l, network net); 10 | void backward_batchnorm_layer(layer l, network net); 11 | 12 | #ifdef GPU 13 | void forward_batchnorm_layer_gpu(layer l, network net); 14 | void backward_batchnorm_layer_gpu(layer l, network net); 15 | void pull_batchnorm_layer(layer l); 16 | void push_batchnorm_layer(layer l); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Train/src/shortcut_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SHORTCUT_LAYER_H 2 | #define SHORTCUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2); 8 | void forward_shortcut_layer(const layer l, network net); 9 | void backward_shortcut_layer(const layer l, network net); 10 | void resize_shortcut_layer(layer *l, int w, int h); 11 | 12 | #ifdef GPU 13 | void forward_shortcut_layer_gpu(const layer l, network net); 14 | void backward_shortcut_layer_gpu(const layer l, network net); 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /Inference/src/shortcut_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SHORTCUT_LAYER_H 2 | #define SHORTCUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2); 8 | void forward_shortcut_layer(const layer l, network net); 9 | void backward_shortcut_layer(const layer l, network net); 10 | void resize_shortcut_layer(layer *l, int w, int h); 11 | 12 | #ifdef GPU 13 | void forward_shortcut_layer_gpu(const layer l, network net); 14 | void backward_shortcut_layer_gpu(const layer l, network net); 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /Train/src/region_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REGION_LAYER_H 2 | #define REGION_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_region_layer(int batch, int w, int h, int n, int classes, int coords, int max_boxes); 9 | void forward_region_layer(const layer l, network net); 10 | void backward_region_layer(const layer l, network net); 11 | void resize_region_layer(layer *l, int w, int h); 12 | 13 | #ifdef GPU 14 | void forward_region_layer_gpu(const layer l, network net); 15 | void backward_region_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /Inference/src/region_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REGION_LAYER_H 2 | #define REGION_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_region_layer(int batch, int w, int h, int n, int classes, int coords, int max_boxes); 9 | void forward_region_layer(const layer l, network net); 10 | void backward_region_layer(const layer l, network net); 11 | void resize_region_layer(layer *l, int w, int h); 12 | 13 | #ifdef GPU 14 | void forward_region_layer_gpu(const layer l, network net); 15 | void backward_region_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /Train/src/lstm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LSTM_LAYER_H 2 | #define LSTM_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | #define USET 8 | 9 | layer make_lstm_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_lstm_layer(layer l, network net); 12 | void update_lstm_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_lstm_layer_gpu(layer l, network net); 16 | void backward_lstm_layer_gpu(layer l, network net); 17 | void update_lstm_layer_gpu(layer l, update_args a); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /Inference/src/crop_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CROP_LAYER_H 2 | #define CROP_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | typedef layer crop_layer; 9 | 10 | image get_crop_image(crop_layer l); 11 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure); 12 | void forward_crop_layer(const crop_layer l, network net); 13 | void resize_crop_layer(layer *l, int w, int h); 14 | 15 | #ifdef GPU 16 | void forward_crop_layer_gpu(crop_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /Inference/src/lstm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LSTM_LAYER_H 2 | #define LSTM_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | #define USET 8 | 9 | layer make_lstm_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_lstm_layer(layer l, network net); 12 | void update_lstm_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_lstm_layer_gpu(layer l, network net); 16 | void backward_lstm_layer_gpu(layer l, network net); 17 | void update_lstm_layer_gpu(layer l, update_args a); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /Train/src/crop_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CROP_LAYER_H 2 | #define CROP_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | typedef layer crop_layer; 9 | 10 | image get_crop_image(crop_layer l); 11 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure); 12 | void forward_crop_layer(const crop_layer l, network net); 13 | void resize_crop_layer(layer *l, int w, int h); 14 | 15 | #ifdef GPU 16 | void forward_crop_layer_gpu(crop_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /Train/src/reorg_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REORG_LAYER_H 2 | #define REORG_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra); 10 | void resize_reorg_layer(layer *l, int w, int h); 11 | void forward_reorg_layer(const layer l, network net); 12 | void backward_reorg_layer(const layer l, network net); 13 | 14 | #ifdef GPU 15 | void forward_reorg_layer_gpu(layer l, network net); 16 | void backward_reorg_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /Train/src/route_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ROUTE_LAYER_H 2 | #define ROUTE_LAYER_H 3 | #include "network.h" 4 | #include "layer.h" 5 | 6 | typedef layer route_layer; 7 | 8 | route_layer make_route_layer(int batch, int n, int *input_layers, int *input_size); 9 | void forward_route_layer(const route_layer l, network net); 10 | void backward_route_layer(const route_layer l, network net); 11 | void resize_route_layer(route_layer *l, network *net); 12 | 13 | #ifdef GPU 14 | void forward_route_layer_gpu(const route_layer l, network net); 15 | void backward_route_layer_gpu(const route_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /Inference/src/reorg_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REORG_LAYER_H 2 | #define REORG_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra); 10 | void resize_reorg_layer(layer *l, int w, int h); 11 | void forward_reorg_layer(const layer l, network net); 12 | void backward_reorg_layer(const layer l, network net); 13 | 14 | #ifdef GPU 15 | void forward_reorg_layer_gpu(layer l, network net); 16 | void backward_reorg_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /Inference/src/route_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ROUTE_LAYER_H 2 | #define ROUTE_LAYER_H 3 | #include "network.h" 4 | #include "layer.h" 5 | 6 | typedef layer route_layer; 7 | 8 | route_layer make_route_layer(int batch, int n, int *input_layers, int *input_size); 9 | void forward_route_layer(const route_layer l, network net); 10 | void backward_route_layer(const route_layer l, network net); 11 | void resize_route_layer(route_layer *l, network *net); 12 | 13 | #ifdef GPU 14 | void forward_route_layer_gpu(const route_layer l, network net); 15 | void backward_route_layer_gpu(const route_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /Inference/src/dropout_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DROPOUT_LAYER_H 2 | #define DROPOUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer dropout_layer; 8 | 9 | dropout_layer make_dropout_layer(int batch, int inputs, float probability); 10 | 11 | void forward_dropout_layer(dropout_layer l, network net); 12 | void backward_dropout_layer(dropout_layer l, network net); 13 | void resize_dropout_layer(dropout_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_dropout_layer_gpu(dropout_layer l, network net); 17 | void backward_dropout_layer_gpu(dropout_layer l, network net); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /Train/src/dropout_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DROPOUT_LAYER_H 2 | #define DROPOUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer dropout_layer; 8 | 9 | dropout_layer make_dropout_layer(int batch, int inputs, float probability); 10 | 11 | void forward_dropout_layer(dropout_layer l, network net); 12 | void backward_dropout_layer(dropout_layer l, network net); 13 | void resize_dropout_layer(dropout_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_dropout_layer_gpu(dropout_layer l, network net); 17 | void backward_dropout_layer_gpu(dropout_layer l, network net); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /Train/src/yolo_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef YOLO_LAYER_H 2 | #define YOLO_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes); 9 | void forward_yolo_layer(const layer l, network net); 10 | void backward_yolo_layer(const layer l, network net); 11 | void resize_yolo_layer(layer *l, int w, int h); 12 | int yolo_num_detections(layer l, float thresh); 13 | 14 | #ifdef GPU 15 | void forward_yolo_layer_gpu(const layer l, network net); 16 | void backward_yolo_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Inference/src/yolo_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef YOLO_LAYER_H 2 | #define YOLO_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes); 9 | void forward_yolo_layer(const layer l, network net); 10 | void backward_yolo_layer(const layer l, network net); 11 | void resize_yolo_layer(layer *l, int w, int h); 12 | int yolo_num_detections(layer l, float thresh); 13 | 14 | #ifdef GPU 15 | void forward_yolo_layer_gpu(const layer l, network net); 16 | void backward_yolo_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Train/src/detection_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DETECTION_LAYER_H 2 | #define DETECTION_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer detection_layer; 8 | 9 | detection_layer make_detection_layer(int batch, int inputs, int n, int size, int classes, int coords, int rescore); 10 | void forward_detection_layer(const detection_layer l, network net); 11 | void backward_detection_layer(const detection_layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_detection_layer_gpu(const detection_layer l, network net); 15 | void backward_detection_layer_gpu(detection_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /Inference/src/detection_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DETECTION_LAYER_H 2 | #define DETECTION_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer detection_layer; 8 | 9 | detection_layer make_detection_layer(int batch, int inputs, int n, int size, int classes, int coords, int rescore); 10 | void forward_detection_layer(const detection_layer l, network net); 11 | void backward_detection_layer(const detection_layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_detection_layer_gpu(const detection_layer l, network net); 15 | void backward_detection_layer_gpu(detection_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /Train/src/cost_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef COST_LAYER_H 2 | #define COST_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer cost_layer; 7 | 8 | COST_TYPE get_cost_type(char *s); 9 | char *get_cost_string(COST_TYPE a); 10 | cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale); 11 | void forward_cost_layer(const cost_layer l, network net); 12 | void backward_cost_layer(const cost_layer l, network net); 13 | void resize_cost_layer(cost_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_cost_layer_gpu(cost_layer l, network net); 17 | void backward_cost_layer_gpu(const cost_layer l, network net); 18 | #endif 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Inference/src/cost_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef COST_LAYER_H 2 | #define COST_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer cost_layer; 7 | 8 | COST_TYPE get_cost_type(char *s); 9 | char *get_cost_string(COST_TYPE a); 10 | cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale); 11 | void forward_cost_layer(const cost_layer l, network net); 12 | void backward_cost_layer(const cost_layer l, network net); 13 | void resize_cost_layer(cost_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_cost_layer_gpu(cost_layer l, network net); 17 | void backward_cost_layer_gpu(const cost_layer l, network net); 18 | #endif 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Train/src/softmax_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SOFTMAX_LAYER_H 2 | #define SOFTMAX_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer softmax_layer; 7 | 8 | void softmax_array(float *input, int n, float temp, float *output); 9 | softmax_layer make_softmax_layer(int batch, int inputs, int groups); 10 | void forward_softmax_layer(const softmax_layer l, network net); 11 | void backward_softmax_layer(const softmax_layer l, network net); 12 | 13 | #ifdef GPU 14 | void pull_softmax_layer_output(const softmax_layer l); 15 | void forward_softmax_layer_gpu(const softmax_layer l, network net); 16 | void backward_softmax_layer_gpu(const softmax_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Inference/src/softmax_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SOFTMAX_LAYER_H 2 | #define SOFTMAX_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer softmax_layer; 7 | 8 | void softmax_array(float *input, int n, float temp, float *output); 9 | softmax_layer make_softmax_layer(int batch, int inputs, int groups); 10 | void forward_softmax_layer(const softmax_layer l, network net); 11 | void backward_softmax_layer(const softmax_layer l, network net); 12 | 13 | #ifdef GPU 14 | void pull_softmax_layer_output(const softmax_layer l); 15 | void forward_softmax_layer_gpu(const softmax_layer l, network net); 16 | void backward_softmax_layer_gpu(const softmax_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Inference/src/gru_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef GRU_LAYER_H 3 | #define GRU_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_gru_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_gru_layer(layer l, network state); 12 | void backward_gru_layer(layer l, network state); 13 | void update_gru_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_gru_layer_gpu(layer l, network state); 17 | void backward_gru_layer_gpu(layer l, network state); 18 | void update_gru_layer_gpu(layer l, update_args a); 19 | void push_gru_layer(layer l); 20 | void pull_gru_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /Train/src/gru_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef GRU_LAYER_H 3 | #define GRU_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_gru_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_gru_layer(layer l, network state); 12 | void backward_gru_layer(layer l, network state); 13 | void update_gru_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_gru_layer_gpu(layer l, network state); 17 | void backward_gru_layer_gpu(layer l, network state); 18 | void update_gru_layer_gpu(layer l, update_args a); 19 | void push_gru_layer(layer l); 20 | void pull_gru_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /Train/src/avgpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef AVGPOOL_LAYER_H 2 | #define AVGPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer avgpool_layer; 10 | 11 | image get_avgpool_image(avgpool_layer l); 12 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c); 13 | void resize_avgpool_layer(avgpool_layer *l, int w, int h); 14 | void forward_avgpool_layer(const avgpool_layer l, network net); 15 | void backward_avgpool_layer(const avgpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_avgpool_layer_gpu(avgpool_layer l, network net); 19 | void backward_avgpool_layer_gpu(avgpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /Inference/src/avgpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef AVGPOOL_LAYER_H 2 | #define AVGPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer avgpool_layer; 10 | 11 | image get_avgpool_image(avgpool_layer l); 12 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c); 13 | void resize_avgpool_layer(avgpool_layer *l, int w, int h); 14 | void forward_avgpool_layer(const avgpool_layer l, network net); 15 | void backward_avgpool_layer(const avgpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_avgpool_layer_gpu(avgpool_layer l, network net); 19 | void backward_avgpool_layer_gpu(avgpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /Train/src/network.h: -------------------------------------------------------------------------------- 1 | // Oh boy, why am I about to do this.... 2 | #ifndef NETWORK_H 3 | #define NETWORK_H 4 | #include "darknet.h" 5 | 6 | #include "image.h" 7 | #include "layer.h" 8 | #include "data.h" 9 | #include "tree.h" 10 | 11 | 12 | #ifdef GPU 13 | void pull_network_output(network *net); 14 | #endif 15 | 16 | void compare_networks(network *n1, network *n2, data d); 17 | char *get_layer_string(LAYER_TYPE a); 18 | 19 | network *make_network(int n); 20 | 21 | 22 | float network_accuracy_multi(network *net, data d, int n); 23 | int get_predicted_class_network(network *net); 24 | void print_network(network *net); 25 | int resize_network(network *net, int w, int h); 26 | void calc_network_cost(network *net); 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /Inference/src/network.h: -------------------------------------------------------------------------------- 1 | // Oh boy, why am I about to do this.... 2 | #ifndef NETWORK_H 3 | #define NETWORK_H 4 | #include "darknet.h" 5 | 6 | #include "image.h" 7 | #include "layer.h" 8 | #include "data.h" 9 | #include "tree.h" 10 | 11 | 12 | #ifdef GPU 13 | void pull_network_output(network *net); 14 | #endif 15 | 16 | void compare_networks(network *n1, network *n2, data d); 17 | char *get_layer_string(LAYER_TYPE a); 18 | 19 | network *make_network(int n); 20 | 21 | 22 | float network_accuracy_multi(network *net, data d, int n); 23 | int get_predicted_class_network(network *net); 24 | void print_network(network *net); 25 | int resize_network(network *net, int w, int h); 26 | void calc_network_cost(network *net); 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /Train/src/rnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef RNN_LAYER_H 3 | #define RNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | #define USET 9 | 10 | layer make_rnn_layer(int batch, int inputs, int outputs, int steps, ACTIVATION activation, int batch_normalize, int adam); 11 | 12 | void forward_rnn_layer(layer l, network net); 13 | void backward_rnn_layer(layer l, network net); 14 | void update_rnn_layer(layer l, update_args a); 15 | 16 | #ifdef GPU 17 | void forward_rnn_layer_gpu(layer l, network net); 18 | void backward_rnn_layer_gpu(layer l, network net); 19 | void update_rnn_layer_gpu(layer l, update_args a); 20 | void push_rnn_layer(layer l); 21 | void pull_rnn_layer(layer l); 22 | #endif 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /Inference/src/rnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef RNN_LAYER_H 3 | #define RNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | #define USET 9 | 10 | layer make_rnn_layer(int batch, int inputs, int outputs, int steps, ACTIVATION activation, int batch_normalize, int adam); 11 | 12 | void forward_rnn_layer(layer l, network net); 13 | void backward_rnn_layer(layer l, network net); 14 | void update_rnn_layer(layer l, update_args a); 15 | 16 | #ifdef GPU 17 | void forward_rnn_layer_gpu(layer l, network net); 18 | void backward_rnn_layer_gpu(layer l, network net); 19 | void update_rnn_layer_gpu(layer l, update_args a); 20 | void push_rnn_layer(layer l); 21 | void pull_rnn_layer(layer l); 22 | #endif 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /Train/src/maxpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef MAXPOOL_LAYER_H 2 | #define MAXPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer maxpool_layer; 10 | 11 | image get_maxpool_image(maxpool_layer l); 12 | maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding); 13 | void resize_maxpool_layer(maxpool_layer *l, int w, int h); 14 | void forward_maxpool_layer(const maxpool_layer l, network net); 15 | void backward_maxpool_layer(const maxpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_maxpool_layer_gpu(maxpool_layer l, network net); 19 | void backward_maxpool_layer_gpu(maxpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /Inference/src/maxpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef MAXPOOL_LAYER_H 2 | #define MAXPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer maxpool_layer; 10 | 11 | image get_maxpool_image(maxpool_layer l); 12 | maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding); 13 | void resize_maxpool_layer(maxpool_layer *l, int w, int h); 14 | void forward_maxpool_layer(const maxpool_layer l, network net); 15 | void backward_maxpool_layer(const maxpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_maxpool_layer_gpu(maxpool_layer l, network net); 19 | void backward_maxpool_layer_gpu(maxpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /Train/src/normalization_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef NORMALIZATION_LAYER_H 2 | #define NORMALIZATION_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa); 9 | void resize_normalization_layer(layer *layer, int h, int w); 10 | void forward_normalization_layer(const layer layer, network net); 11 | void backward_normalization_layer(const layer layer, network net); 12 | void visualize_normalization_layer(layer layer, char *window); 13 | 14 | #ifdef GPU 15 | void forward_normalization_layer_gpu(const layer layer, network net); 16 | void backward_normalization_layer_gpu(const layer layer, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Inference/src/normalization_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef NORMALIZATION_LAYER_H 2 | #define NORMALIZATION_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa); 9 | void resize_normalization_layer(layer *layer, int h, int w); 10 | void forward_normalization_layer(const layer layer, network net); 11 | void backward_normalization_layer(const layer layer, network net); 12 | void visualize_normalization_layer(layer layer, char *window); 13 | 14 | #ifdef GPU 15 | void forward_normalization_layer_gpu(const layer layer, network net); 16 | void backward_normalization_layer_gpu(const layer layer, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /Train/src/crnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRNN_LAYER_H 3 | #define CRNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_crnn_layer(int batch, int h, int w, int c, int hidden_filters, int output_filters, int steps, ACTIVATION activation, int batch_normalize); 10 | 11 | void forward_crnn_layer(layer l, network net); 12 | void backward_crnn_layer(layer l, network net); 13 | void update_crnn_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_crnn_layer_gpu(layer l, network net); 17 | void backward_crnn_layer_gpu(layer l, network net); 18 | void update_crnn_layer_gpu(layer l, update_args a); 19 | void push_crnn_layer(layer l); 20 | void pull_crnn_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /Inference/src/crnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRNN_LAYER_H 3 | #define CRNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_crnn_layer(int batch, int h, int w, int c, int hidden_filters, int output_filters, int steps, ACTIVATION activation, int batch_normalize); 10 | 11 | void forward_crnn_layer(layer l, network net); 12 | void backward_crnn_layer(layer l, network net); 13 | void update_crnn_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_crnn_layer_gpu(layer l, network net); 17 | void backward_crnn_layer_gpu(layer l, network net); 18 | void update_crnn_layer_gpu(layer l, update_args a); 19 | void push_crnn_layer(layer l); 20 | void pull_crnn_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /Inference/src/connected_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTED_LAYER_H 2 | #define CONNECTED_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, int batch_normalize, int adam); 9 | 10 | void forward_connected_layer(layer l, network net); 11 | void backward_connected_layer(layer l, network net); 12 | void update_connected_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_connected_layer_gpu(layer l, network net); 16 | void backward_connected_layer_gpu(layer l, network net); 17 | void update_connected_layer_gpu(layer l, update_args a); 18 | void push_connected_layer(layer l); 19 | void pull_connected_layer(layer l); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /Train/src/connected_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTED_LAYER_H 2 | #define CONNECTED_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, int batch_normalize, int adam); 9 | 10 | void forward_connected_layer(layer l, network net); 11 | void backward_connected_layer(layer l, network net); 12 | void update_connected_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_connected_layer_gpu(layer l, network net); 16 | void backward_connected_layer_gpu(layer l, network net); 17 | void update_connected_layer_gpu(layer l, update_args a); 18 | void push_connected_layer(layer l); 19 | void pull_connected_layer(layer l); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /Train/script/valid_model.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # change it to your $TRAIN_ROOT 5 | train_root=/home/djn/projects/DAC-Contest/DACSDC-DeepZ/Train 6 | 7 | # set the path of validation cfg file 8 | va_cfg_path=${train_root}/cfg/valid.cfg 9 | # set the path of initial weights 10 | weights_path=${train_root}/model/yolo_tiny_dacsdc_best.weights 11 | 12 | # set the root path of datax 13 | data_root=${train_root}/data 14 | # set the path of validation images 15 | valid_path=${data_root}/dataset/valid_dataset.txt 16 | # set the path of validation labels 17 | valid_gt_path=${data_root}/dataset/valid_dataset_label.txt 18 | 19 | 20 | # start training 21 | # use parameter -i to choose the GPU device for validation 22 | ${train_root}/darknet detector valid \ 23 | -i 0 \ 24 | -va_cfg ${va_cfg_path} \ 25 | -weights ${weights_path} \ 26 | -va_dir ${valid_path} \ 27 | -va_gt_dir ${valid_gt_path} 28 | -------------------------------------------------------------------------------- /Train/src/deconvolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DECONVOLUTIONAL_LAYER_H 2 | #define DECONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | #ifdef GPU 11 | void forward_deconvolutional_layer_gpu(layer l, network net); 12 | void backward_deconvolutional_layer_gpu(layer l, network net); 13 | void update_deconvolutional_layer_gpu(layer l, update_args a); 14 | void push_deconvolutional_layer(layer l); 15 | void pull_deconvolutional_layer(layer l); 16 | #endif 17 | 18 | layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int adam); 19 | void resize_deconvolutional_layer(layer *l, int h, int w); 20 | void forward_deconvolutional_layer(const layer l, network net); 21 | void update_deconvolutional_layer(layer l, update_args a); 22 | void backward_deconvolutional_layer(layer l, network net); 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /Inference/src/deconvolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DECONVOLUTIONAL_LAYER_H 2 | #define DECONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | #ifdef GPU 11 | void forward_deconvolutional_layer_gpu(layer l, network net); 12 | void backward_deconvolutional_layer_gpu(layer l, network net); 13 | void update_deconvolutional_layer_gpu(layer l, update_args a); 14 | void push_deconvolutional_layer(layer l); 15 | void pull_deconvolutional_layer(layer l); 16 | #endif 17 | 18 | layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int adam); 19 | void resize_deconvolutional_layer(layer *l, int h, int w); 20 | void forward_deconvolutional_layer(const layer l, network net); 21 | void update_deconvolutional_layer(layer l, update_args a); 22 | void backward_deconvolutional_layer(layer l, network net); 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /Train/src/gemm.h: -------------------------------------------------------------------------------- 1 | #ifndef GEMM_H 2 | #define GEMM_H 3 | 4 | void gemm_bin(int M, int N, int K, float ALPHA, 5 | char *A, int lda, 6 | float *B, int ldb, 7 | float *C, int ldc); 8 | 9 | void gemm(int TA, int TB, int M, int N, int K, float ALPHA, 10 | float *A, int lda, 11 | float *B, int ldb, 12 | float BETA, 13 | float *C, int ldc); 14 | 15 | void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA, 16 | float *A, int lda, 17 | float *B, int ldb, 18 | float BETA, 19 | float *C, int ldc); 20 | 21 | #ifdef GPU 22 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 23 | float *A_gpu, int lda, 24 | float *B_gpu, int ldb, 25 | float BETA, 26 | float *C_gpu, int ldc); 27 | 28 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 29 | float *A, int lda, 30 | float *B, int ldb, 31 | float BETA, 32 | float *C, int ldc); 33 | #endif 34 | #endif 35 | -------------------------------------------------------------------------------- /Inference/src/gemm.h: -------------------------------------------------------------------------------- 1 | #ifndef GEMM_H 2 | #define GEMM_H 3 | 4 | void gemm_bin(int M, int N, int K, float ALPHA, 5 | char *A, int lda, 6 | float *B, int ldb, 7 | float *C, int ldc); 8 | 9 | void gemm(int TA, int TB, int M, int N, int K, float ALPHA, 10 | float *A, int lda, 11 | float *B, int ldb, 12 | float BETA, 13 | float *C, int ldc); 14 | 15 | void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA, 16 | float *A, int lda, 17 | float *B, int ldb, 18 | float BETA, 19 | float *C, int ldc); 20 | 21 | #ifdef GPU 22 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 23 | float *A_gpu, int lda, 24 | float *B_gpu, int ldb, 25 | float BETA, 26 | float *C_gpu, int ldc); 27 | 28 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 29 | float *A, int lda, 30 | float *B, int ldb, 31 | float BETA, 32 | float *C, int ldc); 33 | #endif 34 | #endif 35 | -------------------------------------------------------------------------------- /Train/src/local_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOCAL_LAYER_H 2 | #define LOCAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer local_layer; 11 | 12 | #ifdef GPU 13 | void forward_local_layer_gpu(local_layer layer, network net); 14 | void backward_local_layer_gpu(local_layer layer, network net); 15 | void update_local_layer_gpu(local_layer layer, update_args a); 16 | 17 | void push_local_layer(local_layer layer); 18 | void pull_local_layer(local_layer layer); 19 | #endif 20 | 21 | local_layer make_local_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation); 22 | 23 | void forward_local_layer(const local_layer layer, network net); 24 | void backward_local_layer(local_layer layer, network net); 25 | void update_local_layer(local_layer layer, update_args a); 26 | 27 | void bias_output(float *output, float *biases, int batch, int n, int size); 28 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /Inference/src/local_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOCAL_LAYER_H 2 | #define LOCAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer local_layer; 11 | 12 | #ifdef GPU 13 | void forward_local_layer_gpu(local_layer layer, network net); 14 | void backward_local_layer_gpu(local_layer layer, network net); 15 | void update_local_layer_gpu(local_layer layer, update_args a); 16 | 17 | void push_local_layer(local_layer layer); 18 | void pull_local_layer(local_layer layer); 19 | #endif 20 | 21 | local_layer make_local_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation); 22 | 23 | void forward_local_layer(const local_layer layer, network net); 24 | void backward_local_layer(local_layer layer, network net); 25 | void update_local_layer(local_layer layer, update_args a); 26 | 27 | void bias_output(float *output, float *biases, int batch, int n, int size); 28 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /Train/src/mix_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef MIX_LAYER_H 2 | #define MIX_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_mix_layer(int batch, int w, int h, int n, int classes, int coords, int max_boxes, char *loss_type_s, char *con_loss_type_s, char *strategy_type_s); 9 | void forward_mix_layer(const layer l, network net); 10 | void backward_mix_layer(const layer l, network net); 11 | 12 | box get_mix_box(float *x, float *biases, int n, int index, int i, int j, int w, int h, int stride, STRATEGY_TYPE strategy_type); 13 | int mix_entry_index(layer l, int batch, int location, int entry); 14 | 15 | // added 16 | void delta_mix_con(float *output, int label, int index, float alpha, float gamma, float *loss, float *delta, int scale, LOSS_TYPE loss_type); 17 | void delta_mix_pos(float *output, box truth, float *biases, int n, int index, int i, int j, int w, int h, float *loss, float *delta, float scale, int stride, LOSS_TYPE loss_type, STRATEGY_TYPE strategy_type); 18 | void calc_mix_loss(float *loss, layer l); 19 | 20 | 21 | #ifdef GPU 22 | void forward_mix_layer_gpu(const layer l, network net); 23 | void backward_mix_layer_gpu(layer l, network net); 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /Train/src/focal_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef FOCAL_LAYER_H 2 | #define FOCAL_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_focal_layer(int batch, int w, int h, int n, int total, int *mask, int classes, int coords, int max_boxes, char *loss_type_s, char *con_loss_type_s, char *strategy_type_s); 9 | void forward_focal_layer(const layer l, network net); 10 | void backward_focal_layer(const layer l, network net); 11 | 12 | box get_focal_box(float *x, float *biases, int n, int index, int i, int j, int w, int h, int stride, STRATEGY_TYPE strategy_type); 13 | int focal_entry_index(layer l, int batch, int location, int entry); 14 | 15 | // added 16 | void delta_con(float *output, int label, int index, float alpha, float gamma, float *loss, float *delta, int scale, LOSS_TYPE loss_type); 17 | void delta_pos(float *output, box truth, float *biases, int n, int index, int i, int j, int w, int h, float *loss, float *delta, float scale, int stride, LOSS_TYPE loss_type, STRATEGY_TYPE strategy_type); 18 | void calc_tot_loss(float *loss, layer l); 19 | 20 | 21 | #ifdef GPU 22 | void forward_focal_layer_gpu(const layer l, network net); 23 | void backward_focal_layer_gpu(layer l, network net); 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /Inference/src/focal_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef FOCAL_LAYER_H 2 | #define FOCAL_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_focal_layer(int batch, int w, int h, int n, int total, int *mask, int classes, int coords, int max_boxes, char *loss_type_s, char *con_loss_type_s, char *strategy_type_s); 9 | void forward_focal_layer(const layer l, network net); 10 | void backward_focal_layer(const layer l, network net); 11 | 12 | box get_focal_box(float *x, float *biases, int n, int index, int i, int j, int w, int h, int stride, STRATEGY_TYPE strategy_type); 13 | int focal_entry_index(layer l, int batch, int location, int entry); 14 | 15 | // added 16 | void delta_con(float *output, int label, int index, float alpha, float gamma, float *loss, float *delta, int scale, LOSS_TYPE loss_type); 17 | void delta_pos(float *output, box truth, float *biases, int n, int index, int i, int j, int w, int h, float *loss, float *delta, float scale, int stride, LOSS_TYPE loss_type, STRATEGY_TYPE strategy_type); 18 | void calc_tot_loss(float *loss, layer l); 19 | 20 | 21 | #ifdef GPU 22 | void forward_focal_layer_gpu(const layer l, network net); 23 | void backward_focal_layer_gpu(layer l, network net); 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /Inference/src/dropout_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "dropout_layer.h" 7 | #include "cuda.h" 8 | #include "utils.h" 9 | } 10 | 11 | __global__ void yoloswag420blazeit360noscope(float *input, int size, float *rand, float prob, float scale) 12 | { 13 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 14 | if(id < size) input[id] = (rand[id] < prob) ? 0 : input[id]*scale; 15 | } 16 | 17 | void forward_dropout_layer_gpu(dropout_layer layer, network net) 18 | { 19 | if (!net.train) return; 20 | int size = layer.inputs*layer.batch; 21 | cuda_random(layer.rand_gpu, size); 22 | /* 23 | int i; 24 | for(i = 0; i < size; ++i){ 25 | layer.rand[i] = rand_uniform(); 26 | } 27 | cuda_push_array(layer.rand_gpu, layer.rand, size); 28 | */ 29 | 30 | yoloswag420blazeit360noscope<<>>(net.input_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 31 | check_error(cudaPeekAtLastError()); 32 | } 33 | 34 | void backward_dropout_layer_gpu(dropout_layer layer, network net) 35 | { 36 | if(!net.delta_gpu) return; 37 | int size = layer.inputs*layer.batch; 38 | 39 | yoloswag420blazeit360noscope<<>>(net.delta_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 40 | check_error(cudaPeekAtLastError()); 41 | } 42 | -------------------------------------------------------------------------------- /Train/src/dropout_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "dropout_layer.h" 7 | #include "cuda.h" 8 | #include "utils.h" 9 | } 10 | 11 | __global__ void yoloswag420blazeit360noscope(float *input, int size, float *rand, float prob, float scale) 12 | { 13 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 14 | if(id < size) input[id] = (rand[id] < prob) ? 0 : input[id]*scale; 15 | } 16 | 17 | void forward_dropout_layer_gpu(dropout_layer layer, network net) 18 | { 19 | if (!net.train) return; 20 | int size = layer.inputs*layer.batch; 21 | cuda_random(layer.rand_gpu, size); 22 | /* 23 | int i; 24 | for(i = 0; i < size; ++i){ 25 | layer.rand[i] = rand_uniform(); 26 | } 27 | cuda_push_array(layer.rand_gpu, layer.rand, size); 28 | */ 29 | 30 | yoloswag420blazeit360noscope<<>>(net.input_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 31 | check_error(cudaPeekAtLastError()); 32 | } 33 | 34 | void backward_dropout_layer_gpu(dropout_layer layer, network net) 35 | { 36 | if(!net.delta_gpu) return; 37 | int size = layer.inputs*layer.batch; 38 | 39 | yoloswag420blazeit360noscope<<>>(net.delta_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 40 | check_error(cudaPeekAtLastError()); 41 | } 42 | -------------------------------------------------------------------------------- /Inference/src/col2im.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void col2im_add_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad, float val) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return; 11 | im[col + width*(row + height*channel)] += val; 12 | } 13 | //This one might be too, can't remember. 14 | void col2im_cpu(float* data_col, 15 | int channels, int height, int width, 16 | int ksize, int stride, int pad, float* data_im) 17 | { 18 | int c,h,w; 19 | int height_col = (height + 2*pad - ksize) / stride + 1; 20 | int width_col = (width + 2*pad - ksize) / stride + 1; 21 | 22 | int channels_col = channels * ksize * ksize; 23 | for (c = 0; c < channels_col; ++c) { 24 | int w_offset = c % ksize; 25 | int h_offset = (c / ksize) % ksize; 26 | int c_im = c / ksize / ksize; 27 | for (h = 0; h < height_col; ++h) { 28 | for (w = 0; w < width_col; ++w) { 29 | int im_row = h_offset + h * stride; 30 | int im_col = w_offset + w * stride; 31 | int col_index = (c * height_col + h) * width_col + w; 32 | double val = data_col[col_index]; 33 | col2im_add_pixel(data_im, height, width, channels, 34 | im_row, im_col, c_im, pad, val); 35 | } 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Train/src/col2im.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void col2im_add_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad, float val) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return; 11 | im[col + width*(row + height*channel)] += val; 12 | } 13 | //This one might be too, can't remember. 14 | void col2im_cpu(float* data_col, 15 | int channels, int height, int width, 16 | int ksize, int stride, int pad, float* data_im) 17 | { 18 | int c,h,w; 19 | int height_col = (height + 2*pad - ksize) / stride + 1; 20 | int width_col = (width + 2*pad - ksize) / stride + 1; 21 | 22 | int channels_col = channels * ksize * ksize; 23 | for (c = 0; c < channels_col; ++c) { 24 | int w_offset = c % ksize; 25 | int h_offset = (c / ksize) % ksize; 26 | int c_im = c / ksize / ksize; 27 | for (h = 0; h < height_col; ++h) { 28 | for (w = 0; w < width_col; ++w) { 29 | int im_row = h_offset + h * stride; 30 | int im_col = w_offset + w * stride; 31 | int col_index = (c * height_col + h) * width_col + w; 32 | double val = data_col[col_index]; 33 | col2im_add_pixel(data_im, height, width, channels, 34 | im_row, im_col, c_im, pad, val); 35 | } 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Train/src/im2col.c: -------------------------------------------------------------------------------- 1 | #include "im2col.h" 2 | #include 3 | float im2col_get_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return 0; 11 | return im[col + width*(row + height*channel)]; 12 | } 13 | 14 | //From Berkeley Vision's Caffe! 15 | //https://github.com/BVLC/caffe/blob/master/LICENSE 16 | void im2col_cpu(float* data_im, 17 | int channels, int height, int width, 18 | int ksize, int stride, int pad, float* data_col) 19 | { 20 | int c,h,w; 21 | int height_col = (height + 2*pad - ksize) / stride + 1; 22 | int width_col = (width + 2*pad - ksize) / stride + 1; 23 | 24 | int channels_col = channels * ksize * ksize; 25 | for (c = 0; c < channels_col; ++c) { 26 | int w_offset = c % ksize; 27 | int h_offset = (c / ksize) % ksize; 28 | int c_im = c / ksize / ksize; 29 | for (h = 0; h < height_col; ++h) { 30 | for (w = 0; w < width_col; ++w) { 31 | int im_row = h_offset + h * stride; 32 | int im_col = w_offset + w * stride; 33 | int col_index = (c * height_col + h) * width_col + w; 34 | data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, 35 | im_row, im_col, c_im, pad); 36 | } 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Inference/src/im2col.c: -------------------------------------------------------------------------------- 1 | #include "im2col.h" 2 | #include 3 | float im2col_get_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return 0; 11 | return im[col + width*(row + height*channel)]; 12 | } 13 | 14 | //From Berkeley Vision's Caffe! 15 | //https://github.com/BVLC/caffe/blob/master/LICENSE 16 | void im2col_cpu(float* data_im, 17 | int channels, int height, int width, 18 | int ksize, int stride, int pad, float* data_col) 19 | { 20 | int c,h,w; 21 | int height_col = (height + 2*pad - ksize) / stride + 1; 22 | int width_col = (width + 2*pad - ksize) / stride + 1; 23 | 24 | int channels_col = channels * ksize * ksize; 25 | for (c = 0; c < channels_col; ++c) { 26 | int w_offset = c % ksize; 27 | int h_offset = (c / ksize) % ksize; 28 | int c_im = c / ksize / ksize; 29 | for (h = 0; h < height_col; ++h) { 30 | for (w = 0; w < width_col; ++w) { 31 | int im_row = h_offset + h * stride; 32 | int im_col = w_offset + w * stride; 33 | int col_index = (c * height_col + h) * width_col + w; 34 | data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, 35 | im_row, im_col, c_im, pad); 36 | } 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Train/script/train_model.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # change it to your $TRAIN_ROOT 5 | train_root=/home/djn/projects/DAC-Contest/DACSDC-DeepZ/Train 6 | 7 | # set the path of training cfg file 8 | tr_cfg_path=${train_root}/cfg/train.cfg 9 | # set the path of validation cfg file 10 | va_cfg_path=${train_root}/cfg/valid.cfg 11 | # set the path of initial weights 12 | weights_path=${train_root}/model/yolov3_tiny_COCO.conv.weights 13 | 14 | # set the root path of data 15 | data_root=${train_root}/data 16 | # set the path of training data 17 | train_path=${data_root}/dataset/train_dataset.txt 18 | # set the path of validation images 19 | valid_path=${data_root}/dataset/valid_dataset.txt 20 | # set the path of validation labels 21 | valid_gt_path=${data_root}/dataset/valid_dataset_label.txt 22 | 23 | # set the name of the model 24 | model_name=yolo_tiny_dacsdc 25 | # set the path to store trained models 26 | model_path=${train_root}/model 27 | # set the path of the logging file of validation 28 | log_path=${train_root}/log 29 | # set the path of the logging file of training 30 | log_file=${train_root}/log/${model_name}.out 31 | 32 | 33 | # start training 34 | # use -i parameter to choose the GPU device 35 | # use -avg_loss parameter to set the value of initial loss 36 | nohup \ 37 | ${train_root}/darknet detector train \ 38 | -i 0 \ 39 | -tr_cfg ${tr_cfg_path} \ 40 | -va_cfg ${va_cfg_path} \ 41 | -weights ${weights_path} \ 42 | -tr_dir ${train_path} \ 43 | -va_dir ${valid_path} \ 44 | -va_gt_dir ${valid_gt_path} \ 45 | -model_name ${model_name} \ 46 | -model_dir ${model_path} \ 47 | -log_dir ${log_path} \ 48 | -avg_loss -1 > ${log_file} 2>&1 & 49 | -------------------------------------------------------------------------------- /Inference/script/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2.7 2 | # coding=utf-8 3 | 4 | # standard libraries 5 | from __future__ import print_function 6 | import os 7 | import math 8 | import time 9 | # third-party libraries 10 | import numpy as np 11 | # local packages 12 | import procfunc 13 | 14 | 15 | # set root path of the detector 16 | detector_root = '..' 17 | 18 | 19 | if __name__ == "__main__": 20 | DAC = os.path.join(detector_root, 'data') 21 | [imgDir, resultDir, timeDir, xmlDir, myXmlDir, allTimeFile] = procfunc.setupDir(DAC) 22 | # load all the images paths 23 | [allImageName, imageNum] = procfunc.getImageNames(imgDir) 24 | img_path_lst = [os.path.join(imgDir, imgName) for imgName in allImageName] 25 | 26 | ############################################## 27 | ############### detection part ############### 28 | ############################################## 29 | # initialize 30 | cfg_path = os.path.join(detector_root, 'cfg', 'valid.cfg') 31 | model_path = os.path.join(detector_root, 'model', 'yolo_tiny_dacsdc_best.weights') 32 | res_bboxes = np.zeros((imageNum, 4), dtype=int) 33 | 34 | # inference 35 | time_start = time.time() 36 | net = procfunc.loadModel(cfg_path, model_path) # load trained model 37 | procfunc.detectionAndTrackingInParallel(net, img_path_lst, imageNum, res_bboxes) 38 | #procfunc.detectionAndTrackingSerially(net, img_path_lst, imageNum, res_bboxes) 39 | time_end = time.time() 40 | resultRunTime = time_end - time_start 41 | 42 | # write results (write time to allTimeFile and detection results to xml) 43 | procfunc.storeResultsToXML(res_bboxes, allImageName, myXmlDir) 44 | procfunc.write(imageNum, resultRunTime, allTimeFile) 45 | -------------------------------------------------------------------------------- /Train/example/art.c: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | 3 | #include 4 | 5 | void demo_art(char *cfgfile, char *weightfile, int cam_index) 6 | { 7 | #ifdef OPENCV 8 | network *net = load_network(cfgfile, weightfile, 0); 9 | set_batch_network(net, 1); 10 | 11 | srand(2222222); 12 | CvCapture * cap; 13 | 14 | cap = cvCaptureFromCAM(cam_index); 15 | 16 | char *window = "ArtJudgementBot9000!!!"; 17 | if(!cap) error("Couldn't connect to webcam.\n"); 18 | cvNamedWindow(window, CV_WINDOW_NORMAL); 19 | cvResizeWindow(window, 512, 512); 20 | int i; 21 | int idx[] = {37, 401, 434}; 22 | int n = sizeof(idx)/sizeof(idx[0]); 23 | 24 | while(1){ 25 | image in = get_image_from_stream(cap); 26 | image in_s = resize_image(in, net->w, net->h); 27 | show_image(in, window); 28 | 29 | float *p = network_predict(net, in_s.data); 30 | 31 | printf("\033[2J"); 32 | printf("\033[1;1H"); 33 | 34 | float score = 0; 35 | for(i = 0; i < n; ++i){ 36 | float s = p[idx[i]]; 37 | if (s > score) score = s; 38 | } 39 | score = score; 40 | printf("I APPRECIATE THIS ARTWORK: %10.7f%%\n", score*100); 41 | printf("["); 42 | int upper = 30; 43 | for(i = 0; i < upper; ++i){ 44 | printf("%c", ((i+.5) < score*upper) ? 219 : ' '); 45 | } 46 | printf("]\n"); 47 | 48 | free_image(in_s); 49 | free_image(in); 50 | 51 | cvWaitKey(1); 52 | } 53 | #endif 54 | } 55 | 56 | 57 | void run_art(int argc, char **argv) 58 | { 59 | int cam_index = find_int_arg(argc, argv, "-c", 0); 60 | char *cfg = argv[2]; 61 | char *weights = argv[3]; 62 | demo_art(cfg, weights, cam_index); 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Train/src/list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | 5 | list *make_list() 6 | { 7 | list *l = malloc(sizeof(list)); 8 | l->size = 0; 9 | l->front = 0; 10 | l->back = 0; 11 | return l; 12 | } 13 | 14 | /* 15 | void transfer_node(list *s, list *d, node *n) 16 | { 17 | node *prev, *next; 18 | prev = n->prev; 19 | next = n->next; 20 | if(prev) prev->next = next; 21 | if(next) next->prev = prev; 22 | --s->size; 23 | if(s->front == n) s->front = next; 24 | if(s->back == n) s->back = prev; 25 | } 26 | */ 27 | 28 | void *list_pop(list *l){ 29 | if(!l->back) return 0; 30 | node *b = l->back; 31 | void *val = b->val; 32 | l->back = b->prev; 33 | if(l->back) l->back->next = 0; 34 | free(b); 35 | --l->size; 36 | 37 | return val; 38 | } 39 | 40 | void list_insert(list *l, void *val) 41 | { 42 | node *new = malloc(sizeof(node)); 43 | new->val = val; 44 | new->next = 0; 45 | 46 | if(!l->back){ 47 | l->front = new; 48 | new->prev = 0; 49 | }else{ 50 | l->back->next = new; 51 | new->prev = l->back; 52 | } 53 | l->back = new; 54 | ++l->size; 55 | } 56 | 57 | void free_node(node *n) 58 | { 59 | node *next; 60 | while(n) { 61 | next = n->next; 62 | free(n); 63 | n = next; 64 | } 65 | } 66 | 67 | void free_list(list *l) 68 | { 69 | free_node(l->front); 70 | free(l); 71 | } 72 | 73 | void free_list_contents(list *l) 74 | { 75 | node *n = l->front; 76 | while(n){ 77 | free(n->val); 78 | n = n->next; 79 | } 80 | } 81 | 82 | void **list_to_array(list *l) 83 | { 84 | void **a = calloc(l->size, sizeof(void*)); 85 | int count = 0; 86 | node *n = l->front; 87 | while(n){ 88 | a[count++] = n->val; 89 | n = n->next; 90 | } 91 | return a; 92 | } 93 | -------------------------------------------------------------------------------- /Inference/src/list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | 5 | list *make_list() 6 | { 7 | list *l = malloc(sizeof(list)); 8 | l->size = 0; 9 | l->front = 0; 10 | l->back = 0; 11 | return l; 12 | } 13 | 14 | /* 15 | void transfer_node(list *s, list *d, node *n) 16 | { 17 | node *prev, *next; 18 | prev = n->prev; 19 | next = n->next; 20 | if(prev) prev->next = next; 21 | if(next) next->prev = prev; 22 | --s->size; 23 | if(s->front == n) s->front = next; 24 | if(s->back == n) s->back = prev; 25 | } 26 | */ 27 | 28 | void *list_pop(list *l){ 29 | if(!l->back) return 0; 30 | node *b = l->back; 31 | void *val = b->val; 32 | l->back = b->prev; 33 | if(l->back) l->back->next = 0; 34 | free(b); 35 | --l->size; 36 | 37 | return val; 38 | } 39 | 40 | void list_insert(list *l, void *val) 41 | { 42 | node *new = malloc(sizeof(node)); 43 | new->val = val; 44 | new->next = 0; 45 | 46 | if(!l->back){ 47 | l->front = new; 48 | new->prev = 0; 49 | }else{ 50 | l->back->next = new; 51 | new->prev = l->back; 52 | } 53 | l->back = new; 54 | ++l->size; 55 | } 56 | 57 | void free_node(node *n) 58 | { 59 | node *next; 60 | while(n) { 61 | next = n->next; 62 | free(n); 63 | n = next; 64 | } 65 | } 66 | 67 | void free_list(list *l) 68 | { 69 | free_node(l->front); 70 | free(l); 71 | } 72 | 73 | void free_list_contents(list *l) 74 | { 75 | node *n = l->front; 76 | while(n){ 77 | free(n->val); 78 | n = n->next; 79 | } 80 | } 81 | 82 | void **list_to_array(list *l) 83 | { 84 | void **a = calloc(l->size, sizeof(void*)); 85 | int count = 0; 86 | node *n = l->front; 87 | while(n){ 88 | a[count++] = n->val; 89 | n = n->next; 90 | } 91 | return a; 92 | } 93 | -------------------------------------------------------------------------------- /Train/src/dropout_layer.c: -------------------------------------------------------------------------------- 1 | #include "dropout_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include 5 | #include 6 | 7 | dropout_layer make_dropout_layer(int batch, int inputs, float probability) 8 | { 9 | dropout_layer l = {0}; 10 | l.type = DROPOUT; 11 | l.probability = probability; 12 | l.inputs = inputs; 13 | l.outputs = inputs; 14 | l.batch = batch; 15 | l.rand = calloc(inputs*batch, sizeof(float)); 16 | l.scale = 1./(1.-probability); 17 | l.forward = forward_dropout_layer; 18 | l.backward = backward_dropout_layer; 19 | #ifdef GPU 20 | l.forward_gpu = forward_dropout_layer_gpu; 21 | l.backward_gpu = backward_dropout_layer_gpu; 22 | l.rand_gpu = cuda_make_array(l.rand, inputs*batch); 23 | #endif 24 | fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs); 25 | return l; 26 | } 27 | 28 | void resize_dropout_layer(dropout_layer *l, int inputs) 29 | { 30 | l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float)); 31 | #ifdef GPU 32 | cuda_free(l->rand_gpu); 33 | 34 | l->rand_gpu = cuda_make_array(l->rand, inputs*l->batch); 35 | #endif 36 | } 37 | 38 | void forward_dropout_layer(dropout_layer l, network net) 39 | { 40 | int i; 41 | if (!net.train) return; 42 | for(i = 0; i < l.batch * l.inputs; ++i){ 43 | float r = rand_uniform(0, 1); 44 | l.rand[i] = r; 45 | if(r < l.probability) net.input[i] = 0; 46 | else net.input[i] *= l.scale; 47 | } 48 | } 49 | 50 | void backward_dropout_layer(dropout_layer l, network net) 51 | { 52 | int i; 53 | if(!net.delta) return; 54 | for(i = 0; i < l.batch * l.inputs; ++i){ 55 | float r = l.rand[i]; 56 | if(r < l.probability) net.delta[i] = 0; 57 | else net.delta[i] *= l.scale; 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Inference/src/dropout_layer.c: -------------------------------------------------------------------------------- 1 | #include "dropout_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include 5 | #include 6 | 7 | dropout_layer make_dropout_layer(int batch, int inputs, float probability) 8 | { 9 | dropout_layer l = {0}; 10 | l.type = DROPOUT; 11 | l.probability = probability; 12 | l.inputs = inputs; 13 | l.outputs = inputs; 14 | l.batch = batch; 15 | l.rand = calloc(inputs*batch, sizeof(float)); 16 | l.scale = 1./(1.-probability); 17 | l.forward = forward_dropout_layer; 18 | l.backward = backward_dropout_layer; 19 | #ifdef GPU 20 | l.forward_gpu = forward_dropout_layer_gpu; 21 | l.backward_gpu = backward_dropout_layer_gpu; 22 | l.rand_gpu = cuda_make_array(l.rand, inputs*batch); 23 | #endif 24 | fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs); 25 | return l; 26 | } 27 | 28 | void resize_dropout_layer(dropout_layer *l, int inputs) 29 | { 30 | l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float)); 31 | #ifdef GPU 32 | cuda_free(l->rand_gpu); 33 | 34 | l->rand_gpu = cuda_make_array(l->rand, inputs*l->batch); 35 | #endif 36 | } 37 | 38 | void forward_dropout_layer(dropout_layer l, network net) 39 | { 40 | int i; 41 | if (!net.train) return; 42 | for(i = 0; i < l.batch * l.inputs; ++i){ 43 | float r = rand_uniform(0, 1); 44 | l.rand[i] = r; 45 | if(r < l.probability) net.input[i] = 0; 46 | else net.input[i] *= l.scale; 47 | } 48 | } 49 | 50 | void backward_dropout_layer(dropout_layer l, network net) 51 | { 52 | int i; 53 | if(!net.delta) return; 54 | for(i = 0; i < l.batch * l.inputs; ++i){ 55 | float r = l.rand[i]; 56 | if(r < l.probability) net.delta[i] = 0; 57 | else net.delta[i] *= l.scale; 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Train/src/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | #include 4 | #include 5 | #include "darknet.h" 6 | #include "list.h" 7 | 8 | #define TIME(a) \ 9 | do { \ 10 | double start = what_time_is_it_now(); \ 11 | a; \ 12 | printf("%s took: %f seconds\n", #a, what_time_is_it_now() - start); \ 13 | } while (0) 14 | 15 | #define TWO_PI 6.2831853071795864769252866f 16 | 17 | double what_time_is_it_now(); 18 | void shuffle(void *arr, size_t n, size_t size); 19 | void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections); 20 | void free_ptrs(void **ptrs, int n); 21 | int alphanum_to_int(char c); 22 | char int_to_alphanum(int i); 23 | int read_int(int fd); 24 | void write_int(int fd, int n); 25 | void read_all(int fd, char *buffer, size_t bytes); 26 | void write_all(int fd, char *buffer, size_t bytes); 27 | int read_all_fail(int fd, char *buffer, size_t bytes); 28 | int write_all_fail(int fd, char *buffer, size_t bytes); 29 | void find_replace(char *str, char *orig, char *rep, char *output); 30 | void malloc_error(); 31 | void file_error(char *s); 32 | void strip(char *s); 33 | void strip_char(char *s, char bad); 34 | list *split_str(char *s, char delim); 35 | char *fgetl(FILE *fp); 36 | list *parse_csv_line(char *line); 37 | char *copy_string(char *s); 38 | int count_fields(char *line); 39 | float *parse_fields(char *line, int n); 40 | void translate_array(float *a, int n, float s); 41 | float constrain(float min, float max, float a); 42 | int constrain_int(int a, int min, int max); 43 | float rand_scale(float s); 44 | int rand_int(int min, int max); 45 | void mean_arrays(float **a, int n, int els, float *avg); 46 | float dist_array(float *a, float *b, int n, int sub); 47 | float **one_hot_encode(float *a, int n, int k); 48 | float sec(clock_t clocks); 49 | void print_statistics(float *a, int n); 50 | int int_index(int *a, int val, int n); 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /Inference/src/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | #include 4 | #include 5 | #include "darknet.h" 6 | #include "list.h" 7 | 8 | #define TIME(a) \ 9 | do { \ 10 | double start = what_time_is_it_now(); \ 11 | a; \ 12 | printf("%s took: %f seconds\n", #a, what_time_is_it_now() - start); \ 13 | } while (0) 14 | 15 | #define TWO_PI 6.2831853071795864769252866f 16 | 17 | double what_time_is_it_now(); 18 | void shuffle(void *arr, size_t n, size_t size); 19 | void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections); 20 | void free_ptrs(void **ptrs, int n); 21 | int alphanum_to_int(char c); 22 | char int_to_alphanum(int i); 23 | int read_int(int fd); 24 | void write_int(int fd, int n); 25 | void read_all(int fd, char *buffer, size_t bytes); 26 | void write_all(int fd, char *buffer, size_t bytes); 27 | int read_all_fail(int fd, char *buffer, size_t bytes); 28 | int write_all_fail(int fd, char *buffer, size_t bytes); 29 | void find_replace(char *str, char *orig, char *rep, char *output); 30 | void malloc_error(); 31 | void file_error(char *s); 32 | void strip(char *s); 33 | void strip_char(char *s, char bad); 34 | list *split_str(char *s, char delim); 35 | char *fgetl(FILE *fp); 36 | list *parse_csv_line(char *line); 37 | char *copy_string(char *s); 38 | int count_fields(char *line); 39 | float *parse_fields(char *line, int n); 40 | void translate_array(float *a, int n, float s); 41 | float constrain(float min, float max, float a); 42 | int constrain_int(int a, int min, int max); 43 | float rand_scale(float s); 44 | int rand_int(int min, int max); 45 | void mean_arrays(float **a, int n, int els, float *avg); 46 | float dist_array(float *a, float *b, int n, int sub); 47 | float **one_hot_encode(float *a, int n, int k); 48 | float sec(clock_t clocks); 49 | void print_statistics(float *a, int n); 50 | int int_index(int *a, int val, int n); 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /Train/src/avgpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "avgpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output) 11 | { 12 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 13 | if(id >= n) return; 14 | 15 | int k = id % c; 16 | id /= c; 17 | int b = id; 18 | 19 | int i; 20 | int out_index = (k + c*b); 21 | output[out_index] = 0; 22 | for(i = 0; i < w*h; ++i){ 23 | int in_index = i + h*w*(k + b*c); 24 | output[out_index] += input[in_index]; 25 | } 26 | output[out_index] /= w*h; 27 | } 28 | 29 | __global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta) 30 | { 31 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 32 | if(id >= n) return; 33 | 34 | int k = id % c; 35 | id /= c; 36 | int b = id; 37 | 38 | int i; 39 | int out_index = (k + c*b); 40 | for(i = 0; i < w*h; ++i){ 41 | int in_index = i + h*w*(k + b*c); 42 | in_delta[in_index] += out_delta[out_index] / (w*h); 43 | } 44 | } 45 | 46 | extern "C" void forward_avgpool_layer_gpu(avgpool_layer layer, network net) 47 | { 48 | size_t n = layer.c*layer.batch; 49 | 50 | forward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.input_gpu, layer.output_gpu); 51 | check_error(cudaPeekAtLastError()); 52 | } 53 | 54 | extern "C" void backward_avgpool_layer_gpu(avgpool_layer layer, network net) 55 | { 56 | size_t n = layer.c*layer.batch; 57 | 58 | backward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.delta_gpu, layer.delta_gpu); 59 | check_error(cudaPeekAtLastError()); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Inference/src/avgpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "avgpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output) 11 | { 12 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 13 | if(id >= n) return; 14 | 15 | int k = id % c; 16 | id /= c; 17 | int b = id; 18 | 19 | int i; 20 | int out_index = (k + c*b); 21 | output[out_index] = 0; 22 | for(i = 0; i < w*h; ++i){ 23 | int in_index = i + h*w*(k + b*c); 24 | output[out_index] += input[in_index]; 25 | } 26 | output[out_index] /= w*h; 27 | } 28 | 29 | __global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta) 30 | { 31 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 32 | if(id >= n) return; 33 | 34 | int k = id % c; 35 | id /= c; 36 | int b = id; 37 | 38 | int i; 39 | int out_index = (k + c*b); 40 | for(i = 0; i < w*h; ++i){ 41 | int in_index = i + h*w*(k + b*c); 42 | in_delta[in_index] += out_delta[out_index] / (w*h); 43 | } 44 | } 45 | 46 | extern "C" void forward_avgpool_layer_gpu(avgpool_layer layer, network net) 47 | { 48 | size_t n = layer.c*layer.batch; 49 | 50 | forward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.input_gpu, layer.output_gpu); 51 | check_error(cudaPeekAtLastError()); 52 | } 53 | 54 | extern "C" void backward_avgpool_layer_gpu(avgpool_layer layer, network net) 55 | { 56 | size_t n = layer.c*layer.batch; 57 | 58 | backward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.delta_gpu, layer.delta_gpu); 59 | check_error(cudaPeekAtLastError()); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Train/src/activation_layer.c: -------------------------------------------------------------------------------- 1 | #include "activation_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include "blas.h" 5 | #include "gemm.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation) 13 | { 14 | layer l = {0}; 15 | l.type = ACTIVE; 16 | 17 | l.inputs = inputs; 18 | l.outputs = inputs; 19 | l.batch=batch; 20 | 21 | l.output = calloc(batch*inputs, sizeof(float*)); 22 | l.delta = calloc(batch*inputs, sizeof(float*)); 23 | 24 | l.forward = forward_activation_layer; 25 | l.backward = backward_activation_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_activation_layer_gpu; 28 | l.backward_gpu = backward_activation_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 32 | #endif 33 | l.activation = activation; 34 | fprintf(stderr, "Activation Layer: %d inputs\n", inputs); 35 | return l; 36 | } 37 | 38 | void forward_activation_layer(layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, l.activation); 42 | } 43 | 44 | void backward_activation_layer(layer l, network net) 45 | { 46 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 47 | copy_cpu(l.outputs*l.batch, l.delta, 1, net.delta, 1); 48 | } 49 | 50 | #ifdef GPU 51 | 52 | void forward_activation_layer_gpu(layer l, network net) 53 | { 54 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 55 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 56 | } 57 | 58 | void backward_activation_layer_gpu(layer l, network net) 59 | { 60 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 61 | copy_gpu(l.outputs*l.batch, l.delta_gpu, 1, net.delta_gpu, 1); 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /Inference/src/activation_layer.c: -------------------------------------------------------------------------------- 1 | #include "activation_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include "blas.h" 5 | #include "gemm.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation) 13 | { 14 | layer l = {0}; 15 | l.type = ACTIVE; 16 | 17 | l.inputs = inputs; 18 | l.outputs = inputs; 19 | l.batch=batch; 20 | 21 | l.output = calloc(batch*inputs, sizeof(float*)); 22 | l.delta = calloc(batch*inputs, sizeof(float*)); 23 | 24 | l.forward = forward_activation_layer; 25 | l.backward = backward_activation_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_activation_layer_gpu; 28 | l.backward_gpu = backward_activation_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 32 | #endif 33 | l.activation = activation; 34 | fprintf(stderr, "Activation Layer: %d inputs\n", inputs); 35 | return l; 36 | } 37 | 38 | void forward_activation_layer(layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, l.activation); 42 | } 43 | 44 | void backward_activation_layer(layer l, network net) 45 | { 46 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 47 | copy_cpu(l.outputs*l.batch, l.delta, 1, net.delta, 1); 48 | } 49 | 50 | #ifdef GPU 51 | 52 | void forward_activation_layer_gpu(layer l, network net) 53 | { 54 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 55 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 56 | } 57 | 58 | void backward_activation_layer_gpu(layer l, network net) 59 | { 60 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 61 | copy_gpu(l.outputs*l.batch, l.delta_gpu, 1, net.delta_gpu, 1); 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /Inference/src/l2norm_layer.c: -------------------------------------------------------------------------------- 1 | #include "l2norm_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_l2norm_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "l2norm %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = L2NORM; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.output = calloc(inputs*batch, sizeof(float)); 21 | l.scales = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | 24 | l.forward = forward_l2norm_layer; 25 | l.backward = backward_l2norm_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_l2norm_layer_gpu; 28 | l.backward_gpu = backward_l2norm_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.scales_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 33 | #endif 34 | return l; 35 | } 36 | 37 | void forward_l2norm_layer(const layer l, network net) 38 | { 39 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 40 | l2normalize_cpu(l.output, l.scales, l.batch, l.out_c, l.out_w*l.out_h); 41 | } 42 | 43 | void backward_l2norm_layer(const layer l, network net) 44 | { 45 | //axpy_cpu(l.inputs*l.batch, 1, l.scales, 1, l.delta, 1); 46 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 47 | } 48 | 49 | #ifdef GPU 50 | 51 | void forward_l2norm_layer_gpu(const layer l, network net) 52 | { 53 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 54 | l2normalize_gpu(l.output_gpu, l.scales_gpu, l.batch, l.out_c, l.out_w*l.out_h); 55 | } 56 | 57 | void backward_l2norm_layer_gpu(const layer l, network net) 58 | { 59 | axpy_gpu(l.batch*l.inputs, 1, l.scales_gpu, 1, l.delta_gpu, 1); 60 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /Train/src/l2norm_layer.c: -------------------------------------------------------------------------------- 1 | #include "l2norm_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_l2norm_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "l2norm %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = L2NORM; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.output = calloc(inputs*batch, sizeof(float)); 21 | l.scales = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | 24 | l.forward = forward_l2norm_layer; 25 | l.backward = backward_l2norm_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_l2norm_layer_gpu; 28 | l.backward_gpu = backward_l2norm_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.scales_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 33 | #endif 34 | return l; 35 | } 36 | 37 | void forward_l2norm_layer(const layer l, network net) 38 | { 39 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 40 | l2normalize_cpu(l.output, l.scales, l.batch, l.out_c, l.out_w*l.out_h); 41 | } 42 | 43 | void backward_l2norm_layer(const layer l, network net) 44 | { 45 | //axpy_cpu(l.inputs*l.batch, 1, l.scales, 1, l.delta, 1); 46 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 47 | } 48 | 49 | #ifdef GPU 50 | 51 | void forward_l2norm_layer_gpu(const layer l, network net) 52 | { 53 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 54 | l2normalize_gpu(l.output_gpu, l.scales_gpu, l.batch, l.out_c, l.out_w*l.out_h); 55 | } 56 | 57 | void backward_l2norm_layer_gpu(const layer l, network net) 58 | { 59 | axpy_gpu(l.batch*l.inputs, 1, l.scales_gpu, 1, l.delta_gpu, 1); 60 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /Train/src/avgpool_layer.c: -------------------------------------------------------------------------------- 1 | #include "avgpool_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c) 6 | { 7 | fprintf(stderr, "avg %4d x%4d x%4d -> %4d\n", w, h, c, c); 8 | avgpool_layer l = {0}; 9 | l.type = AVGPOOL; 10 | l.batch = batch; 11 | l.h = h; 12 | l.w = w; 13 | l.c = c; 14 | l.out_w = 1; 15 | l.out_h = 1; 16 | l.out_c = c; 17 | l.outputs = l.out_c; 18 | l.inputs = h*w*c; 19 | int output_size = l.outputs * batch; 20 | l.output = calloc(output_size, sizeof(float)); 21 | l.delta = calloc(output_size, sizeof(float)); 22 | l.forward = forward_avgpool_layer; 23 | l.backward = backward_avgpool_layer; 24 | #ifdef GPU 25 | l.forward_gpu = forward_avgpool_layer_gpu; 26 | l.backward_gpu = backward_avgpool_layer_gpu; 27 | l.output_gpu = cuda_make_array(l.output, output_size); 28 | l.delta_gpu = cuda_make_array(l.delta, output_size); 29 | #endif 30 | return l; 31 | } 32 | 33 | void resize_avgpool_layer(avgpool_layer *l, int w, int h) 34 | { 35 | l->w = w; 36 | l->h = h; 37 | l->inputs = h*w*l->c; 38 | } 39 | 40 | void forward_avgpool_layer(const avgpool_layer l, network net) 41 | { 42 | int b,i,k; 43 | 44 | for(b = 0; b < l.batch; ++b){ 45 | for(k = 0; k < l.c; ++k){ 46 | int out_index = k + b*l.c; 47 | l.output[out_index] = 0; 48 | for(i = 0; i < l.h*l.w; ++i){ 49 | int in_index = i + l.h*l.w*(k + b*l.c); 50 | l.output[out_index] += net.input[in_index]; 51 | } 52 | l.output[out_index] /= l.h*l.w; 53 | } 54 | } 55 | } 56 | 57 | void backward_avgpool_layer(const avgpool_layer l, network net) 58 | { 59 | int b,i,k; 60 | 61 | for(b = 0; b < l.batch; ++b){ 62 | for(k = 0; k < l.c; ++k){ 63 | int out_index = k + b*l.c; 64 | for(i = 0; i < l.h*l.w; ++i){ 65 | int in_index = i + l.h*l.w*(k + b*l.c); 66 | net.delta[in_index] += l.delta[out_index] / (l.h*l.w); 67 | } 68 | } 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /Inference/src/avgpool_layer.c: -------------------------------------------------------------------------------- 1 | #include "avgpool_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c) 6 | { 7 | fprintf(stderr, "avg %4d x%4d x%4d -> %4d\n", w, h, c, c); 8 | avgpool_layer l = {0}; 9 | l.type = AVGPOOL; 10 | l.batch = batch; 11 | l.h = h; 12 | l.w = w; 13 | l.c = c; 14 | l.out_w = 1; 15 | l.out_h = 1; 16 | l.out_c = c; 17 | l.outputs = l.out_c; 18 | l.inputs = h*w*c; 19 | int output_size = l.outputs * batch; 20 | l.output = calloc(output_size, sizeof(float)); 21 | l.delta = calloc(output_size, sizeof(float)); 22 | l.forward = forward_avgpool_layer; 23 | l.backward = backward_avgpool_layer; 24 | #ifdef GPU 25 | l.forward_gpu = forward_avgpool_layer_gpu; 26 | l.backward_gpu = backward_avgpool_layer_gpu; 27 | l.output_gpu = cuda_make_array(l.output, output_size); 28 | l.delta_gpu = cuda_make_array(l.delta, output_size); 29 | #endif 30 | return l; 31 | } 32 | 33 | void resize_avgpool_layer(avgpool_layer *l, int w, int h) 34 | { 35 | l->w = w; 36 | l->h = h; 37 | l->inputs = h*w*l->c; 38 | } 39 | 40 | void forward_avgpool_layer(const avgpool_layer l, network net) 41 | { 42 | int b,i,k; 43 | 44 | for(b = 0; b < l.batch; ++b){ 45 | for(k = 0; k < l.c; ++k){ 46 | int out_index = k + b*l.c; 47 | l.output[out_index] = 0; 48 | for(i = 0; i < l.h*l.w; ++i){ 49 | int in_index = i + l.h*l.w*(k + b*l.c); 50 | l.output[out_index] += net.input[in_index]; 51 | } 52 | l.output[out_index] /= l.h*l.w; 53 | } 54 | } 55 | } 56 | 57 | void backward_avgpool_layer(const avgpool_layer l, network net) 58 | { 59 | int b,i,k; 60 | 61 | for(b = 0; b < l.batch; ++b){ 62 | for(k = 0; k < l.c; ++k){ 63 | int out_index = k + b*l.c; 64 | for(i = 0; i < l.h*l.w; ++i){ 65 | int in_index = i + l.h*l.w*(k + b*l.c); 66 | net.delta[in_index] += l.delta[out_index] / (l.h*l.w); 67 | } 68 | } 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /Train/data/dataset/divided_dataset/train_dataset_cls.txt: -------------------------------------------------------------------------------- 1 | train_bird1.txt 2 | train_boat1.txt 3 | train_boat2.txt 4 | train_boat3.txt 5 | train_boat4.txt 6 | train_boat5.txt 7 | train_boat6.txt 8 | train_boat7.txt 9 | train_boat8.txt 10 | train_building1.txt 11 | train_building2.txt 12 | train_building3.txt 13 | train_car1.txt 14 | train_car10.txt 15 | train_car11.txt 16 | train_car12.txt 17 | train_car13.txt 18 | train_car14.txt 19 | train_car15.txt 20 | train_car16.txt 21 | train_car17.txt 22 | train_car18.txt 23 | train_car19.txt 24 | train_car2.txt 25 | train_car20.txt 26 | train_car21.txt 27 | train_car22.txt 28 | train_car23.txt 29 | train_car24.txt 30 | train_car3.txt 31 | train_car4.txt 32 | train_car5.txt 33 | train_car6.txt 34 | train_car7.txt 35 | train_car8.txt 36 | train_car9.txt 37 | train_drone1.txt 38 | train_drone2.txt 39 | train_drone3.txt 40 | train_drone4.txt 41 | train_group1.txt 42 | train_group2.txt 43 | train_group3.txt 44 | train_horseride1.txt 45 | train_paraglider1.txt 46 | train_person1.txt 47 | train_person10.txt 48 | train_person11.txt 49 | train_person12.txt 50 | train_person13.txt 51 | train_person14.txt 52 | train_person15.txt 53 | train_person16.txt 54 | train_person17.txt 55 | train_person18.txt 56 | train_person19.txt 57 | train_person2.txt 58 | train_person20.txt 59 | train_person21.txt 60 | train_person22.txt 61 | train_person23.txt 62 | train_person24.txt 63 | train_person25.txt 64 | train_person26.txt 65 | train_person27.txt 66 | train_person28.txt 67 | train_person29.txt 68 | train_person3.txt 69 | train_person4.txt 70 | train_person5.txt 71 | train_person6.txt 72 | train_person7.txt 73 | train_person8.txt 74 | train_person9.txt 75 | train_riding1.txt 76 | train_riding10.txt 77 | train_riding11.txt 78 | train_riding12.txt 79 | train_riding13.txt 80 | train_riding14.txt 81 | train_riding15.txt 82 | train_riding16.txt 83 | train_riding17.txt 84 | train_riding2.txt 85 | train_riding3.txt 86 | train_riding4.txt 87 | train_riding5.txt 88 | train_riding6.txt 89 | train_riding7.txt 90 | train_riding8.txt 91 | train_riding9.txt 92 | train_truck1.txt 93 | train_truck2.txt 94 | train_wakeboard1.txt 95 | train_wakeboard2.txt 96 | train_wakeboard3.txt 97 | train_wakeboard4.txt 98 | train_whale1.txt -------------------------------------------------------------------------------- /Inference/Makefile: -------------------------------------------------------------------------------- 1 | GPU=1 2 | CUDNN=1 3 | OPENCV=1 4 | 5 | ARCH= -gencode arch=compute_62,code=[sm_62,compute_62] 6 | VPATH=./src/:./examples 7 | SLIB=libdarknet.so 8 | OBJDIR=./obj/ 9 | 10 | CC=gcc 11 | NVCC=nvcc 12 | OPTS=-Ofast 13 | LDFLAGS= -lm -pthread 14 | COMMON= -Iinclude/ -Isrc/ 15 | CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC 16 | 17 | CFLAGS+=$(OPTS) 18 | 19 | ifeq ($(OPENCV), 1) 20 | COMMON+= -DOPENCV 21 | CFLAGS+= -DOPENCV 22 | LDFLAGS+= `pkg-config --libs opencv` 23 | COMMON+= `pkg-config --cflags opencv` 24 | endif 25 | 26 | ifeq ($(GPU), 1) 27 | COMMON+= -DGPU -I/usr/local/cuda/include/ 28 | CFLAGS+= -DGPU 29 | LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand 30 | endif 31 | 32 | ifeq ($(CUDNN), 1) 33 | COMMON+= -DCUDNN 34 | CFLAGS+= -DCUDNN 35 | LDFLAGS+= -lcudnn 36 | endif 37 | 38 | OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o focal_layer.o dac_detection.o 39 | 40 | ifeq ($(GPU), 1) 41 | LDFLAGS+= -lstdc++ 42 | OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o 43 | endif 44 | 45 | OBJS = $(addprefix $(OBJDIR), $(OBJ)) 46 | DEPS = $(wildcard src/*.h) Makefile include/darknet.h 47 | 48 | all: obj model data $(SLIB) 49 | 50 | $(SLIB): $(OBJS) 51 | $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) 52 | 53 | $(OBJDIR)%.o: %.c $(DEPS) 54 | $(CC) $(COMMON) $(CFLAGS) -c $< -o $@ 55 | 56 | $(OBJDIR)%.o: %.cu $(DEPS) 57 | $(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ 58 | 59 | obj: 60 | mkdir -p obj 61 | 62 | model: 63 | mkdir -p model 64 | 65 | data: 66 | mkdir -p data 67 | 68 | 69 | .PHONY: clean 70 | 71 | clean: 72 | rm -rf $(OBJS) $(SLIB) $(OBJDIR)/* 73 | -------------------------------------------------------------------------------- /Train/src/logistic_layer.c: -------------------------------------------------------------------------------- 1 | #include "logistic_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_logistic_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "logistic x entropy %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = LOGXENT; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.loss = calloc(inputs*batch, sizeof(float)); 21 | l.output = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | l.cost = calloc(1, sizeof(float)); 24 | 25 | l.forward = forward_logistic_layer; 26 | l.backward = backward_logistic_layer; 27 | #ifdef GPU 28 | l.forward_gpu = forward_logistic_layer_gpu; 29 | l.backward_gpu = backward_logistic_layer_gpu; 30 | 31 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.loss_gpu = cuda_make_array(l.loss, inputs*batch); 33 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 34 | #endif 35 | return l; 36 | } 37 | 38 | void forward_logistic_layer(const layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, LOGISTIC); 42 | if(net.truth){ 43 | logistic_x_ent_cpu(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss); 44 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 45 | } 46 | } 47 | 48 | void backward_logistic_layer(const layer l, network net) 49 | { 50 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 51 | } 52 | 53 | #ifdef GPU 54 | 55 | void forward_logistic_layer_gpu(const layer l, network net) 56 | { 57 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 58 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, LOGISTIC); 59 | if(net.truth){ 60 | logistic_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu); 61 | cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs); 62 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 63 | } 64 | } 65 | 66 | void backward_logistic_layer_gpu(const layer l, network net) 67 | { 68 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /Inference/src/logistic_layer.c: -------------------------------------------------------------------------------- 1 | #include "logistic_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_logistic_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "logistic x entropy %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = LOGXENT; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.loss = calloc(inputs*batch, sizeof(float)); 21 | l.output = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | l.cost = calloc(1, sizeof(float)); 24 | 25 | l.forward = forward_logistic_layer; 26 | l.backward = backward_logistic_layer; 27 | #ifdef GPU 28 | l.forward_gpu = forward_logistic_layer_gpu; 29 | l.backward_gpu = backward_logistic_layer_gpu; 30 | 31 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.loss_gpu = cuda_make_array(l.loss, inputs*batch); 33 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 34 | #endif 35 | return l; 36 | } 37 | 38 | void forward_logistic_layer(const layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, LOGISTIC); 42 | if(net.truth){ 43 | logistic_x_ent_cpu(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss); 44 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 45 | } 46 | } 47 | 48 | void backward_logistic_layer(const layer l, network net) 49 | { 50 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 51 | } 52 | 53 | #ifdef GPU 54 | 55 | void forward_logistic_layer_gpu(const layer l, network net) 56 | { 57 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 58 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, LOGISTIC); 59 | if(net.truth){ 60 | logistic_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu); 61 | cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs); 62 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 63 | } 64 | } 65 | 66 | void backward_logistic_layer_gpu(const layer l, network net) 67 | { 68 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /Inference/src/upsample_layer.c: -------------------------------------------------------------------------------- 1 | #include "upsample_layer.h" 2 | #include "cuda.h" 3 | #include "blas.h" 4 | 5 | #include 6 | 7 | /* Only support GPU version */ 8 | 9 | layer make_upsample_layer(int batch, int w, int h, int c, int out_w, int out_h) { 10 | layer l = {0}; 11 | l.type = UPSAMPLE; 12 | l.batch = batch; 13 | l.w = w; 14 | l.h = h; 15 | l.c = c; 16 | l.out_w = out_w; 17 | l.out_h = out_h; 18 | l.out_c = c; 19 | 20 | l.outputs = l.out_w*l.out_h*l.out_c; 21 | l.inputs = l.w*l.h*l.c; 22 | l.delta = calloc(l.outputs*batch, sizeof(float)); 23 | l.output = calloc(l.outputs*batch, sizeof(float));; 24 | 25 | l.forward = forward_upsample_layer; 26 | l.backward = backward_upsample_layer; 27 | #ifdef GPU 28 | l.forward_gpu = forward_upsample_layer_gpu; 29 | l.backward_gpu = backward_upsample_layer_gpu; 30 | 31 | l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch); 32 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 33 | #endif 34 | fprintf(stderr, "upsample %4d x%4d x%4d -> %4d x%4d x%4d\n", w, h, c, l.out_w, l.out_h, l.out_c); 35 | return l; 36 | } 37 | 38 | void resize_upsample_layer(layer *l, int w, int h) {} 39 | 40 | void forward_upsample_layer(const layer l, network net) {} 41 | 42 | void backward_upsample_layer(const layer l, network net) {} 43 | 44 | #ifdef GPU 45 | void forward_upsample_layer_gpu(const layer l, network net) 46 | { 47 | fill_gpu(l.outputs*l.batch, 0, l.output_gpu, 1); 48 | upsample_gpu(l.batch, l.c, 1, net.input_gpu, l.w, l.h, l.output_gpu, l.out_w, l.out_h); 49 | /* 50 | fill_gpu(l.outputs*l.batch, 0, l.output_gpu, 1); 51 | if(l.reverse){ 52 | upsample_gpu(l.output_gpu, l.out_w, l.out_h, l.c, l.batch, l.stride, 0, l.scale, net.input_gpu); 53 | }else{ 54 | upsample_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, l.scale, l.output_gpu); 55 | } 56 | */ 57 | } 58 | 59 | void backward_upsample_layer_gpu(const layer l, network net) 60 | { 61 | upsample_gpu(l.batch, l.c, 0, net.delta_gpu, l.w, l.h, l.delta_gpu, l.out_w, l.out_h); 62 | 63 | /* 64 | if(l.reverse){ 65 | upsample_gpu(l.delta_gpu, l.out_w, l.out_h, l.c, l.batch, l.stride, 1, l.scale, net.delta_gpu); 66 | }else{ 67 | upsample_gpu(net.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, l.scale, l.delta_gpu); 68 | } 69 | */ 70 | } 71 | #endif 72 | -------------------------------------------------------------------------------- /Train/src/convolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONVOLUTIONAL_LAYER_H 2 | #define CONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer convolutional_layer; 11 | 12 | #ifdef GPU 13 | void forward_convolutional_layer_gpu(convolutional_layer layer, network net); 14 | void backward_convolutional_layer_gpu(convolutional_layer layer, network net); 15 | void update_convolutional_layer_gpu(convolutional_layer layer, update_args a); 16 | 17 | void push_convolutional_layer(convolutional_layer layer); 18 | void pull_convolutional_layer(convolutional_layer layer); 19 | 20 | void add_bias_gpu(float *output, float *biases, int batch, int n, int size); 21 | void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size); 22 | void adam_update_gpu(float *w, float *d, float *m, float *v, float B1, float B2, float eps, float decay, float rate, int n, int batch, int t); 23 | #ifdef CUDNN 24 | void cudnn_convolutional_setup(layer *l); 25 | #endif 26 | #endif 27 | 28 | convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam); 29 | void resize_convolutional_layer(convolutional_layer *layer, int w, int h); 30 | void forward_convolutional_layer(const convolutional_layer layer, network net); 31 | void update_convolutional_layer(convolutional_layer layer, update_args a); 32 | image *visualize_convolutional_layer(convolutional_layer layer, char *window, image *prev_weights); 33 | void binarize_weights(float *weights, int n, int size, float *binary); 34 | void swap_binary(convolutional_layer *l); 35 | void binarize_weights2(float *weights, int n, int size, char *binary, float *scales); 36 | 37 | void backward_convolutional_layer(convolutional_layer layer, network net); 38 | 39 | void add_bias(float *output, float *biases, int batch, int n, int size); 40 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 41 | 42 | image get_convolutional_image(convolutional_layer layer); 43 | image get_convolutional_delta(convolutional_layer layer); 44 | image get_convolutional_weight(convolutional_layer layer, int i); 45 | 46 | int convolutional_out_height(convolutional_layer layer); 47 | int convolutional_out_width(convolutional_layer layer); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /Inference/src/convolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONVOLUTIONAL_LAYER_H 2 | #define CONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer convolutional_layer; 11 | 12 | #ifdef GPU 13 | void forward_convolutional_layer_gpu(convolutional_layer layer, network net); 14 | void backward_convolutional_layer_gpu(convolutional_layer layer, network net); 15 | void update_convolutional_layer_gpu(convolutional_layer layer, update_args a); 16 | 17 | void push_convolutional_layer(convolutional_layer layer); 18 | void pull_convolutional_layer(convolutional_layer layer); 19 | 20 | void add_bias_gpu(float *output, float *biases, int batch, int n, int size); 21 | void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size); 22 | void adam_update_gpu(float *w, float *d, float *m, float *v, float B1, float B2, float eps, float decay, float rate, int n, int batch, int t); 23 | #ifdef CUDNN 24 | void cudnn_convolutional_setup(layer *l); 25 | #endif 26 | #endif 27 | 28 | convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam); 29 | void resize_convolutional_layer(convolutional_layer *layer, int w, int h); 30 | void forward_convolutional_layer(const convolutional_layer layer, network net); 31 | void update_convolutional_layer(convolutional_layer layer, update_args a); 32 | image *visualize_convolutional_layer(convolutional_layer layer, char *window, image *prev_weights); 33 | void binarize_weights(float *weights, int n, int size, float *binary); 34 | void swap_binary(convolutional_layer *l); 35 | void binarize_weights2(float *weights, int n, int size, char *binary, float *scales); 36 | 37 | void backward_convolutional_layer(convolutional_layer layer, network net); 38 | 39 | void add_bias(float *output, float *biases, int batch, int n, int size); 40 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 41 | 42 | image get_convolutional_image(convolutional_layer layer); 43 | image get_convolutional_delta(convolutional_layer layer); 44 | image get_convolutional_weight(convolutional_layer layer, int i); 45 | 46 | int convolutional_out_height(convolutional_layer layer); 47 | int convolutional_out_width(convolutional_layer layer); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /Train/cfg/valid.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=16 3 | width=640 4 | height=360 5 | channels=3 6 | 7 | [convolutional] 8 | batch_normalize=1 9 | filters=16 10 | size=3 11 | stride=1 12 | pad=1 13 | activation=leaky 14 | 15 | [maxpool] 16 | size=2 17 | stride=2 18 | 19 | [convolutional] 20 | batch_normalize=1 21 | filters=32 22 | size=3 23 | stride=1 24 | pad=1 25 | activation=leaky 26 | 27 | [maxpool] 28 | size=2 29 | stride=2 30 | 31 | [convolutional] 32 | batch_normalize=1 33 | filters=64 34 | size=3 35 | stride=1 36 | pad=1 37 | activation=leaky 38 | 39 | [maxpool] 40 | size=2 41 | stride=2 42 | 43 | [convolutional] 44 | batch_normalize=1 45 | filters=128 46 | size=3 47 | stride=1 48 | pad=1 49 | activation=leaky 50 | 51 | [maxpool] 52 | size=2 53 | stride=2 54 | 55 | [convolutional] 56 | batch_normalize=1 57 | filters=256 58 | size=3 59 | stride=1 60 | pad=1 61 | activation=leaky 62 | 63 | [maxpool] 64 | size=2 65 | stride=2 66 | 67 | [convolutional] 68 | batch_normalize=1 69 | filters=512 70 | size=3 71 | stride=1 72 | pad=1 73 | activation=leaky 74 | 75 | [maxpool] 76 | size=2 77 | stride=1 78 | 79 | [convolutional] 80 | batch_normalize=1 81 | filters=1024 82 | size=3 83 | stride=1 84 | pad=1 85 | activation=leaky 86 | 87 | ########### 88 | 89 | [convolutional] 90 | batch_normalize=1 91 | filters=256 92 | size=3 93 | stride=1 94 | pad=1 95 | activation=leaky 96 | 97 | [convolutional] 98 | batch_normalize=1 99 | filters=512 100 | size=3 101 | stride=1 102 | pad=1 103 | activation=leaky 104 | 105 | [convolutional] 106 | size=3 107 | stride=1 108 | pad=1 109 | filters=15 110 | activation=linear 111 | 112 | [focal] 113 | mask=3,4,5 114 | anchors=0.481,0.370, 1.106,0.714, 1.779,1.534, 3.894,2.168, 6.490,4.469, 16.538,8.435 115 | num=6 116 | 117 | [route] 118 | layers=-4 119 | 120 | [convolutional] 121 | batch_normalize=1 122 | filters=128 123 | size=3 124 | stride=1 125 | pad=1 126 | activation=leaky 127 | 128 | [upsample] 129 | stride=2 130 | 131 | [route] 132 | layers=-1, 8 133 | 134 | [convolutional] 135 | batch_normalize=1 136 | filters=256 137 | size=3 138 | stride=1 139 | pad=1 140 | activation=leaky 141 | 142 | [convolutional] 143 | size=3 144 | stride=1 145 | pad=1 146 | filters=15 147 | activation=linear 148 | 149 | [focal] 150 | mask=0,1,2 151 | anchors=0.962,0.740, 2.211,1.428, 3.558,3.067, 7.788,4.337, 12.981,8.938, 33.077,16.870 152 | num=6 -------------------------------------------------------------------------------- /Inference/cfg/valid.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=16 3 | width=640 4 | height=360 5 | channels=3 6 | 7 | [convolutional] 8 | batch_normalize=1 9 | filters=16 10 | size=3 11 | stride=1 12 | pad=1 13 | activation=leaky 14 | 15 | [maxpool] 16 | size=2 17 | stride=2 18 | 19 | [convolutional] 20 | batch_normalize=1 21 | filters=32 22 | size=3 23 | stride=1 24 | pad=1 25 | activation=leaky 26 | 27 | [maxpool] 28 | size=2 29 | stride=2 30 | 31 | [convolutional] 32 | batch_normalize=1 33 | filters=64 34 | size=3 35 | stride=1 36 | pad=1 37 | activation=leaky 38 | 39 | [maxpool] 40 | size=2 41 | stride=2 42 | 43 | [convolutional] 44 | batch_normalize=1 45 | filters=128 46 | size=3 47 | stride=1 48 | pad=1 49 | activation=leaky 50 | 51 | [maxpool] 52 | size=2 53 | stride=2 54 | 55 | [convolutional] 56 | batch_normalize=1 57 | filters=256 58 | size=3 59 | stride=1 60 | pad=1 61 | activation=leaky 62 | 63 | [maxpool] 64 | size=2 65 | stride=2 66 | 67 | [convolutional] 68 | batch_normalize=1 69 | filters=512 70 | size=3 71 | stride=1 72 | pad=1 73 | activation=leaky 74 | 75 | [maxpool] 76 | size=2 77 | stride=1 78 | 79 | [convolutional] 80 | batch_normalize=1 81 | filters=1024 82 | size=3 83 | stride=1 84 | pad=1 85 | activation=leaky 86 | 87 | ########### 88 | 89 | [convolutional] 90 | batch_normalize=1 91 | filters=256 92 | size=3 93 | stride=1 94 | pad=1 95 | activation=leaky 96 | 97 | [convolutional] 98 | batch_normalize=1 99 | filters=512 100 | size=3 101 | stride=1 102 | pad=1 103 | activation=leaky 104 | 105 | [convolutional] 106 | size=3 107 | stride=1 108 | pad=1 109 | filters=15 110 | activation=linear 111 | 112 | [focal] 113 | mask=3,4,5 114 | anchors=0.481,0.370, 1.106,0.714, 1.779,1.534, 3.894,2.168, 6.490,4.469, 16.538,8.435 115 | num=6 116 | 117 | [route] 118 | layers=-4 119 | 120 | [convolutional] 121 | batch_normalize=1 122 | filters=128 123 | size=3 124 | stride=1 125 | pad=1 126 | activation=leaky 127 | 128 | [upsample] 129 | out_w=40 130 | out_h=22 131 | 132 | [route] 133 | layers=-1, 8 134 | 135 | [convolutional] 136 | batch_normalize=1 137 | filters=256 138 | size=3 139 | stride=1 140 | pad=1 141 | activation=leaky 142 | 143 | [convolutional] 144 | size=3 145 | stride=1 146 | pad=1 147 | filters=15 148 | activation=linear 149 | 150 | [focal] 151 | mask=0,1,2 152 | anchors=0.962,0.740, 2.211,1.428, 3.558,3.067, 7.788,4.337, 12.981,8.938, 33.077,16.870 153 | num=6 -------------------------------------------------------------------------------- /Train/src/im2col_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "im2col.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void im2col_gpu_kernel(const int n, const float* data_im, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_col) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | int w_out = index % width_col; 22 | int h_index = index / width_col; 23 | int h_out = h_index % height_col; 24 | int channel_in = h_index / height_col; 25 | int channel_out = channel_in * ksize * ksize; 26 | int h_in = h_out * stride - pad; 27 | int w_in = w_out * stride - pad; 28 | float* data_col_ptr = data_col; 29 | data_col_ptr += (channel_out * height_col + h_out) * width_col + w_out; 30 | const float* data_im_ptr = data_im; 31 | data_im_ptr += (channel_in * height + h_in) * width + w_in; 32 | for (int i = 0; i < ksize; ++i) { 33 | for (int j = 0; j < ksize; ++j) { 34 | int h = h_in + i; 35 | int w = w_in + j; 36 | 37 | *data_col_ptr = (h >= 0 && w >= 0 && h < height && w < width) ? 38 | data_im_ptr[i * width + j] : 0; 39 | 40 | //*data_col_ptr = data_im_ptr[ii * width + jj]; 41 | 42 | data_col_ptr += height_col * width_col; 43 | } 44 | } 45 | } 46 | } 47 | 48 | void im2col_gpu(float *im, 49 | int channels, int height, int width, 50 | int ksize, int stride, int pad, float *data_col){ 51 | // We are going to launch channels * height_col * width_col kernels, each 52 | // kernel responsible for copying a single-channel grid. 53 | int height_col = (height + 2 * pad - ksize) / stride + 1; 54 | int width_col = (width + 2 * pad - ksize) / stride + 1; 55 | int num_kernels = channels * height_col * width_col; 56 | im2col_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 57 | BLOCK>>>( 58 | num_kernels, im, height, width, ksize, pad, 59 | stride, height_col, 60 | width_col, data_col); 61 | } 62 | -------------------------------------------------------------------------------- /Inference/src/im2col_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "im2col.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void im2col_gpu_kernel(const int n, const float* data_im, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_col) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | int w_out = index % width_col; 22 | int h_index = index / width_col; 23 | int h_out = h_index % height_col; 24 | int channel_in = h_index / height_col; 25 | int channel_out = channel_in * ksize * ksize; 26 | int h_in = h_out * stride - pad; 27 | int w_in = w_out * stride - pad; 28 | float* data_col_ptr = data_col; 29 | data_col_ptr += (channel_out * height_col + h_out) * width_col + w_out; 30 | const float* data_im_ptr = data_im; 31 | data_im_ptr += (channel_in * height + h_in) * width + w_in; 32 | for (int i = 0; i < ksize; ++i) { 33 | for (int j = 0; j < ksize; ++j) { 34 | int h = h_in + i; 35 | int w = w_in + j; 36 | 37 | *data_col_ptr = (h >= 0 && w >= 0 && h < height && w < width) ? 38 | data_im_ptr[i * width + j] : 0; 39 | 40 | //*data_col_ptr = data_im_ptr[ii * width + jj]; 41 | 42 | data_col_ptr += height_col * width_col; 43 | } 44 | } 45 | } 46 | } 47 | 48 | void im2col_gpu(float *im, 49 | int channels, int height, int width, 50 | int ksize, int stride, int pad, float *data_col){ 51 | // We are going to launch channels * height_col * width_col kernels, each 52 | // kernel responsible for copying a single-channel grid. 53 | int height_col = (height + 2 * pad - ksize) / stride + 1; 54 | int width_col = (width + 2 * pad - ksize) / stride + 1; 55 | int num_kernels = channels * height_col * width_col; 56 | im2col_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 57 | BLOCK>>>( 58 | num_kernels, im, height, width, ksize, pad, 59 | stride, height_col, 60 | width_col, data_col); 61 | } 62 | -------------------------------------------------------------------------------- /Train/src/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | #include 4 | 5 | #include "darknet.h" 6 | #include "matrix.h" 7 | #include "list.h" 8 | #include "image.h" 9 | #include "tree.h" 10 | 11 | static inline float distance_from_edge(int x, int max) 12 | { 13 | int dx = (max/2) - x; 14 | if (dx < 0) dx = -dx; 15 | dx = (max/2) + 1 - dx; 16 | dx *= 2; 17 | float dist = (float)dx/max; 18 | if (dist > 1) dist = 1; 19 | return dist; 20 | } 21 | void load_data_blocking(load_args args); 22 | 23 | 24 | void print_letters(float *pred, int n); 25 | data load_data_captcha(char **paths, int n, int m, int k, int w, int h); 26 | data load_data_captcha_encode(char **paths, int n, int m, int w, int h); 27 | 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); 28 | data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 29 | matrix load_image_augment_paths(char **paths, int n, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 30 | data load_data_super(char **paths, int n, int m, int w, int h, int scale); 31 | data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 32 | data load_data_regression(char **paths, int n, int m, int classes, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 33 | data load_go(char *filename); 34 | 35 | 36 | data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h); 37 | 38 | void get_random_batch(data d, int n, float *X, float *y); 39 | data get_data_part(data d, int part, int total); 40 | data get_random_data(data d, int num); 41 | data load_categorical_data_csv(char *filename, int target, int k); 42 | void normalize_data_rows(data d); 43 | void scale_data_rows(data d, float s); 44 | void translate_data_rows(data d, float s); 45 | void randomize_data(data d); 46 | data *split_data(data d, int part, int total); 47 | data concat_datas(data *d, int n); 48 | void fill_truth(char *path, char **labels, int k, float *truth); 49 | 50 | // added function 51 | data load_data_dac(int n, int nthread, class_paths *cls_paths, int w, int h, int boxes, int classes, float jitter, float hue, float saturation, float exposure); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Inference/src/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | #include 4 | 5 | #include "darknet.h" 6 | #include "matrix.h" 7 | #include "list.h" 8 | #include "image.h" 9 | #include "tree.h" 10 | 11 | static inline float distance_from_edge(int x, int max) 12 | { 13 | int dx = (max/2) - x; 14 | if (dx < 0) dx = -dx; 15 | dx = (max/2) + 1 - dx; 16 | dx *= 2; 17 | float dist = (float)dx/max; 18 | if (dist > 1) dist = 1; 19 | return dist; 20 | } 21 | void load_data_blocking(load_args args); 22 | 23 | 24 | void print_letters(float *pred, int n); 25 | data load_data_captcha(char **paths, int n, int m, int k, int w, int h); 26 | data load_data_captcha_encode(char **paths, int n, int m, int w, int h); 27 | 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); 28 | data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 29 | matrix load_image_augment_paths(char **paths, int n, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 30 | data load_data_super(char **paths, int n, int m, int w, int h, int scale); 31 | data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 32 | data load_data_regression(char **paths, int n, int m, int classes, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 33 | data load_go(char *filename); 34 | 35 | 36 | data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h); 37 | 38 | void get_random_batch(data d, int n, float *X, float *y); 39 | data get_data_part(data d, int part, int total); 40 | data get_random_data(data d, int num); 41 | data load_categorical_data_csv(char *filename, int target, int k); 42 | void normalize_data_rows(data d); 43 | void scale_data_rows(data d, float s); 44 | void translate_data_rows(data d, float s); 45 | void randomize_data(data d); 46 | data *split_data(data d, int part, int total); 47 | data concat_datas(data *d, int n); 48 | void fill_truth(char *path, char **labels, int k, float *truth); 49 | 50 | // added function 51 | data load_data_dac(int n, int nthread, class_paths *cls_paths, int w, int h, int boxes, int classes, float jitter, float hue, float saturation, float exposure); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Train/src/image.h: -------------------------------------------------------------------------------- 1 | #ifndef IMAGE_H 2 | #define IMAGE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "box.h" 10 | #include "darknet.h" 11 | 12 | #ifndef __cplusplus 13 | #ifdef OPENCV 14 | int fill_image_from_stream(CvCapture *cap, image im); 15 | image ipl_to_image(IplImage* src); 16 | void ipl_into_image(IplImage* src, image im); 17 | void flush_stream_buffer(CvCapture *cap, int n); 18 | void show_image_cv(image p, const char *name, IplImage *disp); 19 | #endif 20 | #endif 21 | 22 | float get_color(int c, int x, int max); 23 | void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b); 24 | void draw_bbox(image a, box bbox, int w, float r, float g, float b); 25 | void write_label(image a, int r, int c, image *characters, char *string, float *rgb); 26 | image image_distance(image a, image b); 27 | void scale_image(image m, float s); 28 | image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect); 29 | image random_crop_image(image im, int w, int h); 30 | image random_augment_image(image im, float angle, float aspect, int low, int high, int w, int h); 31 | augment_args random_augment_args(image im, float angle, float aspect, int low, int high, int w, int h); 32 | void letterbox_image_into(image im, int w, int h, image boxed); 33 | image resize_max(image im, int max); 34 | void translate_image(image m, float s); 35 | void embed_image(image source, image dest, int dx, int dy); 36 | void place_image(image im, int w, int h, int dx, int dy, image canvas); 37 | void saturate_image(image im, float sat); 38 | void exposure_image(image im, float sat); 39 | void distort_image(image im, float hue, float sat, float val); 40 | void saturate_exposure_image(image im, float sat, float exposure); 41 | void rgb_to_hsv(image im); 42 | void hsv_to_rgb(image im); 43 | void yuv_to_rgb(image im); 44 | void rgb_to_yuv(image im); 45 | 46 | 47 | image collapse_image_layers(image source, int border); 48 | image collapse_images_horz(image *ims, int n); 49 | image collapse_images_vert(image *ims, int n); 50 | 51 | void show_image_normalized(image im, const char *name); 52 | void show_images(image *ims, int n, char *window); 53 | void show_image_layers(image p, char *name); 54 | void show_image_collapsed(image p, char *name); 55 | 56 | void print_image(image m); 57 | 58 | image make_empty_image(int w, int h, int c); 59 | void copy_image_into(image src, image dest); 60 | 61 | image get_image_layer(image m, int l); 62 | 63 | #endif 64 | 65 | -------------------------------------------------------------------------------- /Inference/src/col2im_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "col2im.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void col2im_gpu_kernel(const int n, const float* data_col, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_im) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | float val = 0; 22 | int w = index % width + pad; 23 | int h = (index / width) % height + pad; 24 | int c = index / (width * height); 25 | // compute the start and end of the output 26 | int w_col_start = (w < ksize) ? 0 : (w - ksize) / stride + 1; 27 | int w_col_end = min(w / stride + 1, width_col); 28 | int h_col_start = (h < ksize) ? 0 : (h - ksize) / stride + 1; 29 | int h_col_end = min(h / stride + 1, height_col); 30 | // equivalent implementation 31 | int offset = 32 | (c * ksize * ksize + h * ksize + w) * height_col * width_col; 33 | int coeff_h_col = (1 - stride * ksize * height_col) * width_col; 34 | int coeff_w_col = (1 - stride * height_col * width_col); 35 | for (int h_col = h_col_start; h_col < h_col_end; ++h_col) { 36 | for (int w_col = w_col_start; w_col < w_col_end; ++w_col) { 37 | val += data_col[offset + h_col * coeff_h_col + w_col * coeff_w_col]; 38 | } 39 | } 40 | data_im[index] += val; 41 | } 42 | } 43 | 44 | void col2im_gpu(float *data_col, 45 | int channels, int height, int width, 46 | int ksize, int stride, int pad, float *data_im){ 47 | // We are going to launch channels * height_col * width_col kernels, each 48 | // kernel responsible for copying a single-channel grid. 49 | int height_col = (height + 2 * pad - ksize) / stride + 1; 50 | int width_col = (width + 2 * pad - ksize) / stride + 1; 51 | int num_kernels = channels * height * width; 52 | col2im_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 53 | BLOCK>>>( 54 | num_kernels, data_col, height, width, ksize, pad, 55 | stride, height_col, 56 | width_col, data_im); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Inference/src/image.h: -------------------------------------------------------------------------------- 1 | #ifndef IMAGE_H 2 | #define IMAGE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "box.h" 10 | #include "darknet.h" 11 | 12 | #ifndef __cplusplus 13 | #ifdef OPENCV 14 | int fill_image_from_stream(CvCapture *cap, image im); 15 | image ipl_to_image(IplImage* src); 16 | void ipl_into_image(IplImage* src, image im); 17 | void flush_stream_buffer(CvCapture *cap, int n); 18 | void show_image_cv(image p, const char *name, IplImage *disp); 19 | #endif 20 | #endif 21 | 22 | float get_color(int c, int x, int max); 23 | void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b); 24 | void draw_bbox(image a, box bbox, int w, float r, float g, float b); 25 | void write_label(image a, int r, int c, image *characters, char *string, float *rgb); 26 | image image_distance(image a, image b); 27 | void scale_image(image m, float s); 28 | image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect); 29 | image random_crop_image(image im, int w, int h); 30 | image random_augment_image(image im, float angle, float aspect, int low, int high, int w, int h); 31 | augment_args random_augment_args(image im, float angle, float aspect, int low, int high, int w, int h); 32 | void letterbox_image_into(image im, int w, int h, image boxed); 33 | image resize_max(image im, int max); 34 | void translate_image(image m, float s); 35 | void embed_image(image source, image dest, int dx, int dy); 36 | void place_image(image im, int w, int h, int dx, int dy, image canvas); 37 | void saturate_image(image im, float sat); 38 | void exposure_image(image im, float sat); 39 | void distort_image(image im, float hue, float sat, float val); 40 | void saturate_exposure_image(image im, float sat, float exposure); 41 | void rgb_to_hsv(image im); 42 | void hsv_to_rgb(image im); 43 | void yuv_to_rgb(image im); 44 | void rgb_to_yuv(image im); 45 | 46 | 47 | image collapse_image_layers(image source, int border); 48 | image collapse_images_horz(image *ims, int n); 49 | image collapse_images_vert(image *ims, int n); 50 | 51 | void show_image_normalized(image im, const char *name); 52 | void show_images(image *ims, int n, char *window); 53 | void show_image_layers(image p, char *name); 54 | void show_image_collapsed(image p, char *name); 55 | 56 | void print_image(image m); 57 | 58 | image make_empty_image(int w, int h, int c); 59 | void copy_image_into(image src, image dest); 60 | 61 | image get_image_layer(image m, int l); 62 | 63 | #endif 64 | 65 | -------------------------------------------------------------------------------- /Train/src/col2im_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "col2im.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void col2im_gpu_kernel(const int n, const float* data_col, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_im) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | float val = 0; 22 | int w = index % width + pad; 23 | int h = (index / width) % height + pad; 24 | int c = index / (width * height); 25 | // compute the start and end of the output 26 | int w_col_start = (w < ksize) ? 0 : (w - ksize) / stride + 1; 27 | int w_col_end = min(w / stride + 1, width_col); 28 | int h_col_start = (h < ksize) ? 0 : (h - ksize) / stride + 1; 29 | int h_col_end = min(h / stride + 1, height_col); 30 | // equivalent implementation 31 | int offset = 32 | (c * ksize * ksize + h * ksize + w) * height_col * width_col; 33 | int coeff_h_col = (1 - stride * ksize * height_col) * width_col; 34 | int coeff_w_col = (1 - stride * height_col * width_col); 35 | for (int h_col = h_col_start; h_col < h_col_end; ++h_col) { 36 | for (int w_col = w_col_start; w_col < w_col_end; ++w_col) { 37 | val += data_col[offset + h_col * coeff_h_col + w_col * coeff_w_col]; 38 | } 39 | } 40 | data_im[index] += val; 41 | } 42 | } 43 | 44 | void col2im_gpu(float *data_col, 45 | int channels, int height, int width, 46 | int ksize, int stride, int pad, float *data_im){ 47 | // We are going to launch channels * height_col * width_col kernels, each 48 | // kernel responsible for copying a single-channel grid. 49 | int height_col = (height + 2 * pad - ksize) / stride + 1; 50 | int width_col = (width + 2 * pad - ksize) / stride + 1; 51 | int num_kernels = channels * height * width; 52 | col2im_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 53 | BLOCK>>>( 54 | num_kernels, data_col, height, width, ksize, pad, 55 | stride, height_col, 56 | width_col, data_im); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Train/example/swag.c: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | #include 3 | 4 | void train_swag(char *cfgfile, char *weightfile) 5 | { 6 | char *train_images = "data/voc.0712.trainval"; 7 | char *backup_directory = "/home/pjreddie/backup/"; 8 | srand(time(0)); 9 | char *base = basecfg(cfgfile); 10 | printf("%s\n", base); 11 | float avg_loss = -1; 12 | network net = parse_network_cfg(cfgfile); 13 | if(weightfile){ 14 | load_weights(&net, weightfile); 15 | } 16 | printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); 17 | int imgs = net.batch*net.subdivisions; 18 | int i = *net.seen/imgs; 19 | data train, buffer; 20 | 21 | layer l = net.layers[net.n - 1]; 22 | 23 | int side = l.side; 24 | int classes = l.classes; 25 | float jitter = l.jitter; 26 | 27 | list *plist = get_paths(train_images); 28 | //int N = plist->size; 29 | char **paths = (char **)list_to_array(plist); 30 | 31 | load_args args = {0}; 32 | args.w = net.w; 33 | args.h = net.h; 34 | args.paths = paths; 35 | args.n = imgs; 36 | args.m = plist->size; 37 | args.classes = classes; 38 | args.jitter = jitter; 39 | args.num_boxes = side; 40 | args.d = &buffer; 41 | args.type = REGION_DATA; 42 | 43 | pthread_t load_thread = load_data_in_thread(args); 44 | clock_t time; 45 | //while(i*imgs < N*120){ 46 | while(get_current_batch(net) < net.max_batches){ 47 | i += 1; 48 | time=clock(); 49 | pthread_join(load_thread, 0); 50 | train = buffer; 51 | load_thread = load_data_in_thread(args); 52 | 53 | printf("Loaded: %lf seconds\n", sec(clock()-time)); 54 | 55 | time=clock(); 56 | float loss = train_network(net, train); 57 | if (avg_loss < 0) avg_loss = loss; 58 | avg_loss = avg_loss*.9 + loss*.1; 59 | 60 | printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); 61 | if(i%1000==0 || i == 600){ 62 | char buff[256]; 63 | sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); 64 | save_weights(net, buff); 65 | } 66 | free_data(train); 67 | } 68 | char buff[256]; 69 | sprintf(buff, "%s/%s_final.weights", backup_directory, base); 70 | save_weights(net, buff); 71 | } 72 | 73 | void run_swag(int argc, char **argv) 74 | { 75 | if(argc < 4){ 76 | fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); 77 | return; 78 | } 79 | 80 | char *cfg = argv[3]; 81 | char *weights = (argc > 4) ? argv[4] : 0; 82 | if(0==strcmp(argv[2], "train")) train_swag(cfg, weights); 83 | } 84 | -------------------------------------------------------------------------------- /Train/cfg/train.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=64 3 | subdivisions=4 4 | random=1 5 | 6 | width=640 7 | height=360 8 | channels=3 9 | momentum=0.9 10 | decay=0.0005 11 | angle=0 12 | saturation=1.5 13 | exposure=1.5 14 | hue=.1 15 | 16 | burn_in=1000 17 | learning_rate=0.005 18 | max_batches=400200 19 | policy=poly 20 | power=4 21 | 22 | 23 | [convolutional] 24 | batch_normalize=1 25 | filters=16 26 | size=3 27 | stride=1 28 | pad=1 29 | activation=leaky 30 | 31 | [maxpool] 32 | size=2 33 | stride=2 34 | 35 | [convolutional] 36 | batch_normalize=1 37 | filters=32 38 | size=3 39 | stride=1 40 | pad=1 41 | activation=leaky 42 | 43 | [maxpool] 44 | size=2 45 | stride=2 46 | 47 | [convolutional] 48 | batch_normalize=1 49 | filters=64 50 | size=3 51 | stride=1 52 | pad=1 53 | activation=leaky 54 | 55 | [maxpool] 56 | size=2 57 | stride=2 58 | 59 | [convolutional] 60 | batch_normalize=1 61 | filters=128 62 | size=3 63 | stride=1 64 | pad=1 65 | activation=leaky 66 | 67 | [maxpool] 68 | size=2 69 | stride=2 70 | 71 | [convolutional] 72 | batch_normalize=1 73 | filters=256 74 | size=3 75 | stride=1 76 | pad=1 77 | activation=leaky 78 | 79 | [maxpool] 80 | size=2 81 | stride=2 82 | 83 | [convolutional] 84 | batch_normalize=1 85 | filters=512 86 | size=3 87 | stride=1 88 | pad=1 89 | activation=leaky 90 | 91 | [maxpool] 92 | size=2 93 | stride=1 94 | 95 | [convolutional] 96 | batch_normalize=1 97 | filters=1024 98 | size=3 99 | stride=1 100 | pad=1 101 | activation=leaky 102 | 103 | ########### 104 | 105 | [convolutional] 106 | batch_normalize=1 107 | filters=256 108 | size=3 109 | stride=1 110 | pad=1 111 | activation=leaky 112 | 113 | [convolutional] 114 | batch_normalize=1 115 | filters=512 116 | size=3 117 | stride=1 118 | pad=1 119 | activation=leaky 120 | 121 | [convolutional] 122 | size=3 123 | stride=1 124 | pad=1 125 | filters=15 126 | activation=linear 127 | 128 | [focal] 129 | mask=3,4,5 130 | anchors=0.481,0.370, 1.106,0.714, 1.779,1.534, 3.894,2.168, 6.490,4.469, 16.538,8.435 131 | num=6 132 | strategy=yolo2 133 | ploss=l2 134 | closs=fl 135 | jitter=0.3 136 | ignore_thresh=0.7 137 | 138 | [route] 139 | layers=-4 140 | 141 | [convolutional] 142 | batch_normalize=1 143 | filters=128 144 | size=3 145 | stride=1 146 | pad=1 147 | activation=leaky 148 | 149 | [upsample] 150 | stride=2 151 | 152 | [route] 153 | layers=-1, 8 154 | 155 | [convolutional] 156 | batch_normalize=1 157 | filters=256 158 | size=3 159 | stride=1 160 | pad=1 161 | activation=leaky 162 | 163 | [convolutional] 164 | size=3 165 | stride=1 166 | pad=1 167 | filters=15 168 | activation=linear 169 | 170 | [focal] 171 | mask=0,1,2 172 | anchors=0.962,0.740, 2.211,1.428, 3.558,3.067, 7.788,4.337, 12.981,8.938, 33.077,16.870 173 | num=6 174 | strategy=yolo2 175 | ploss=l2 176 | closs=fl 177 | jitter=0.3 178 | ignore_thresh=0.7 -------------------------------------------------------------------------------- /Train/src/activations.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATIONS_H 2 | #define ACTIVATIONS_H 3 | #include "darknet.h" 4 | #include "cuda.h" 5 | #include "math.h" 6 | 7 | ACTIVATION get_activation(char *s); 8 | 9 | char *get_activation_string(ACTIVATION a); 10 | float activate(float x, ACTIVATION a); 11 | float gradient(float x, ACTIVATION a); 12 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta); 13 | void activate_array(float *x, const int n, const ACTIVATION a); 14 | #ifdef GPU 15 | void activate_array_gpu(float *x, int n, ACTIVATION a); 16 | void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta); 17 | #endif 18 | 19 | static inline float stair_activate(float x) 20 | { 21 | int n = floor(x); 22 | if (n%2 == 0) return floor(x/2.); 23 | else return (x - n) + floor(x/2.); 24 | } 25 | static inline float hardtan_activate(float x) 26 | { 27 | if (x < -1) return -1; 28 | if (x > 1) return 1; 29 | return x; 30 | } 31 | static inline float linear_activate(float x){return x;} 32 | static inline float logistic_activate(float x){return 1./(1. + exp(-x));} 33 | static inline float loggy_activate(float x){return 2./(1. + exp(-x)) - 1;} 34 | static inline float relu_activate(float x){return x*(x>0);} 35 | static inline float elu_activate(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);} 36 | static inline float relie_activate(float x){return (x>0) ? x : .01*x;} 37 | static inline float ramp_activate(float x){return x*(x>0)+.1*x;} 38 | static inline float leaky_activate(float x){return (x>0) ? x : .1*x;} 39 | static inline float tanh_activate(float x){return (exp(2*x)-1)/(exp(2*x)+1);} 40 | static inline float plse_activate(float x) 41 | { 42 | if(x < -4) return .01 * (x + 4); 43 | if(x > 4) return .01 * (x - 4) + 1; 44 | return .125*x + .5; 45 | } 46 | 47 | static inline float lhtan_activate(float x) 48 | { 49 | if(x < 0) return .001*x; 50 | if(x > 1) return .001*(x-1) + 1; 51 | return x; 52 | } 53 | static inline float lhtan_gradient(float x) 54 | { 55 | if(x > 0 && x < 1) return 1; 56 | return .001; 57 | } 58 | 59 | static inline float hardtan_gradient(float x) 60 | { 61 | if (x > -1 && x < 1) return 1; 62 | return 0; 63 | } 64 | inline float linear_gradient(float x){return 1;} 65 | inline float logistic_gradient(float x){return (1-x)*x;} 66 | static inline float loggy_gradient(float x) 67 | { 68 | float y = (x+1.)/2.; 69 | return 2*(1-y)*y; 70 | } 71 | static inline float stair_gradient(float x) 72 | { 73 | if (floor(x) == x) return 0; 74 | return 1; 75 | } 76 | static inline float relu_gradient(float x){return (x>0);} 77 | static inline float elu_gradient(float x){return (x >= 0) + (x < 0)*(x + 1);} 78 | static inline float relie_gradient(float x){return (x>0) ? 1 : .01;} 79 | static inline float ramp_gradient(float x){return (x>0)+.1;} 80 | static inline float leaky_gradient(float x){return (x>0) ? 1 : .1;} 81 | static inline float tanh_gradient(float x){return 1-x*x;} 82 | static inline float plse_gradient(float x){return (x < 0 || x > 1) ? .01 : .125;} 83 | 84 | #endif 85 | 86 | -------------------------------------------------------------------------------- /Inference/src/activations.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATIONS_H 2 | #define ACTIVATIONS_H 3 | #include "darknet.h" 4 | #include "cuda.h" 5 | #include "math.h" 6 | 7 | ACTIVATION get_activation(char *s); 8 | 9 | char *get_activation_string(ACTIVATION a); 10 | float activate(float x, ACTIVATION a); 11 | float gradient(float x, ACTIVATION a); 12 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta); 13 | void activate_array(float *x, const int n, const ACTIVATION a); 14 | #ifdef GPU 15 | void activate_array_gpu(float *x, int n, ACTIVATION a); 16 | void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta); 17 | #endif 18 | 19 | static inline float stair_activate(float x) 20 | { 21 | int n = floor(x); 22 | if (n%2 == 0) return floor(x/2.); 23 | else return (x - n) + floor(x/2.); 24 | } 25 | static inline float hardtan_activate(float x) 26 | { 27 | if (x < -1) return -1; 28 | if (x > 1) return 1; 29 | return x; 30 | } 31 | static inline float linear_activate(float x){return x;} 32 | static inline float logistic_activate(float x){return 1./(1. + exp(-x));} 33 | static inline float loggy_activate(float x){return 2./(1. + exp(-x)) - 1;} 34 | static inline float relu_activate(float x){return x*(x>0);} 35 | static inline float elu_activate(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);} 36 | static inline float relie_activate(float x){return (x>0) ? x : .01*x;} 37 | static inline float ramp_activate(float x){return x*(x>0)+.1*x;} 38 | static inline float leaky_activate(float x){return (x>0) ? x : .1*x;} 39 | static inline float tanh_activate(float x){return (exp(2*x)-1)/(exp(2*x)+1);} 40 | static inline float plse_activate(float x) 41 | { 42 | if(x < -4) return .01 * (x + 4); 43 | if(x > 4) return .01 * (x - 4) + 1; 44 | return .125*x + .5; 45 | } 46 | 47 | static inline float lhtan_activate(float x) 48 | { 49 | if(x < 0) return .001*x; 50 | if(x > 1) return .001*(x-1) + 1; 51 | return x; 52 | } 53 | static inline float lhtan_gradient(float x) 54 | { 55 | if(x > 0 && x < 1) return 1; 56 | return .001; 57 | } 58 | 59 | static inline float hardtan_gradient(float x) 60 | { 61 | if (x > -1 && x < 1) return 1; 62 | return 0; 63 | } 64 | inline float linear_gradient(float x){return 1;} 65 | inline float logistic_gradient(float x){return (1-x)*x;} 66 | static inline float loggy_gradient(float x) 67 | { 68 | float y = (x+1.)/2.; 69 | return 2*(1-y)*y; 70 | } 71 | static inline float stair_gradient(float x) 72 | { 73 | if (floor(x) == x) return 0; 74 | return 1; 75 | } 76 | static inline float relu_gradient(float x){return (x>0);} 77 | static inline float elu_gradient(float x){return (x >= 0) + (x < 0)*(x + 1);} 78 | static inline float relie_gradient(float x){return (x>0) ? 1 : .01;} 79 | static inline float ramp_gradient(float x){return (x>0)+.1;} 80 | static inline float leaky_gradient(float x){return (x>0) ? 1 : .1;} 81 | static inline float tanh_gradient(float x){return 1-x*x;} 82 | static inline float plse_gradient(float x){return (x < 0 || x > 1) ? .01 : .125;} 83 | 84 | #endif 85 | 86 | -------------------------------------------------------------------------------- /Train/Makefile: -------------------------------------------------------------------------------- 1 | GPU=1 2 | CUDNN=1 3 | OPENCV=1 4 | OPENMP=0 5 | DEBUG=0 6 | 7 | ARCH= -gencode arch=compute_30,code=sm_30 \ 8 | -gencode arch=compute_35,code=sm_35 \ 9 | -gencode arch=compute_50,code=[sm_50,compute_50] \ 10 | -gencode arch=compute_52,code=[sm_52,compute_52] 11 | 12 | VPATH=./src/:./example 13 | SLIB=libdarknet.so 14 | ALIB=libdarknet.a 15 | EXEC=darknet 16 | OBJDIR=./obj/ 17 | 18 | CC=gcc 19 | NVCC=nvcc 20 | AR=ar 21 | ARFLAGS=rcs 22 | OPTS=-Ofast 23 | LDFLAGS= -lm -pthread 24 | COMMON= -Iinclude/ -Isrc/ 25 | CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC 26 | 27 | ifeq ($(OPENMP), 1) 28 | CFLAGS+= -fopenmp 29 | endif 30 | 31 | ifeq ($(DEBUG), 1) 32 | OPTS=-O0 -g 33 | endif 34 | 35 | CFLAGS+=$(OPTS) 36 | 37 | ifeq ($(OPENCV), 1) 38 | COMMON+= -DOPENCV 39 | CFLAGS+= -DOPENCV 40 | LDFLAGS+= `pkg-config --libs opencv` 41 | COMMON+= `pkg-config --cflags opencv` 42 | endif 43 | 44 | ifeq ($(GPU), 1) 45 | COMMON+= -DGPU -I/usr/local/cuda/include/ 46 | CFLAGS+= -DGPU 47 | LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand 48 | endif 49 | 50 | ifeq ($(CUDNN), 1) 51 | COMMON+= -DCUDNN 52 | CFLAGS+= -DCUDNN 53 | LDFLAGS+= -lcudnn 54 | endif 55 | 56 | OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o focal_layer.o mix_layer.o 57 | EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o 58 | ifeq ($(GPU), 1) 59 | LDFLAGS+= -lstdc++ 60 | OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o 61 | endif 62 | 63 | EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) 64 | OBJS = $(addprefix $(OBJDIR), $(OBJ)) 65 | DEPS = $(wildcard src/*.h) Makefile include/darknet.h 66 | 67 | all: obj log model $(SLIB) $(ALIB) $(EXEC) 68 | 69 | 70 | $(EXEC): $(EXECOBJ) $(ALIB) 71 | $(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB) 72 | 73 | $(ALIB): $(OBJS) 74 | $(AR) $(ARFLAGS) $@ $^ 75 | 76 | $(SLIB): $(OBJS) 77 | $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) 78 | 79 | $(OBJDIR)%.o: %.c $(DEPS) 80 | $(CC) $(COMMON) $(CFLAGS) -c $< -o $@ 81 | 82 | $(OBJDIR)%.o: %.cu $(DEPS) 83 | $(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ 84 | 85 | obj: 86 | mkdir -p obj 87 | 88 | log: 89 | mkdir -p log 90 | 91 | model: 92 | mkdir -p model 93 | 94 | .PHONY: clean 95 | 96 | clean: 97 | rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)/* -------------------------------------------------------------------------------- /Train/src/crop_layer.c: -------------------------------------------------------------------------------- 1 | #include "crop_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | image get_crop_image(crop_layer l) 6 | { 7 | int h = l.out_h; 8 | int w = l.out_w; 9 | int c = l.out_c; 10 | return float_to_image(w,h,c,l.output); 11 | } 12 | 13 | void backward_crop_layer(const crop_layer l, network net){} 14 | void backward_crop_layer_gpu(const crop_layer l, network net){} 15 | 16 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure) 17 | { 18 | fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c); 19 | crop_layer l = {0}; 20 | l.type = CROP; 21 | l.batch = batch; 22 | l.h = h; 23 | l.w = w; 24 | l.c = c; 25 | l.scale = (float)crop_height / h; 26 | l.flip = flip; 27 | l.angle = angle; 28 | l.saturation = saturation; 29 | l.exposure = exposure; 30 | l.out_w = crop_width; 31 | l.out_h = crop_height; 32 | l.out_c = c; 33 | l.inputs = l.w * l.h * l.c; 34 | l.outputs = l.out_w * l.out_h * l.out_c; 35 | l.output = calloc(l.outputs*batch, sizeof(float)); 36 | l.forward = forward_crop_layer; 37 | l.backward = backward_crop_layer; 38 | 39 | #ifdef GPU 40 | l.forward_gpu = forward_crop_layer_gpu; 41 | l.backward_gpu = backward_crop_layer_gpu; 42 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 43 | l.rand_gpu = cuda_make_array(0, l.batch*8); 44 | #endif 45 | return l; 46 | } 47 | 48 | void resize_crop_layer(layer *l, int w, int h) 49 | { 50 | l->w = w; 51 | l->h = h; 52 | 53 | l->out_w = l->scale*w; 54 | l->out_h = l->scale*h; 55 | 56 | l->inputs = l->w * l->h * l->c; 57 | l->outputs = l->out_h * l->out_w * l->out_c; 58 | 59 | l->output = realloc(l->output, l->batch*l->outputs*sizeof(float)); 60 | #ifdef GPU 61 | cuda_free(l->output_gpu); 62 | l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch); 63 | #endif 64 | } 65 | 66 | 67 | void forward_crop_layer(const crop_layer l, network net) 68 | { 69 | int i,j,c,b,row,col; 70 | int index; 71 | int count = 0; 72 | int flip = (l.flip && rand()%2); 73 | int dh = rand()%(l.h - l.out_h + 1); 74 | int dw = rand()%(l.w - l.out_w + 1); 75 | float scale = 2; 76 | float trans = -1; 77 | if(l.noadjust){ 78 | scale = 1; 79 | trans = 0; 80 | } 81 | if(!net.train){ 82 | flip = 0; 83 | dh = (l.h - l.out_h)/2; 84 | dw = (l.w - l.out_w)/2; 85 | } 86 | for(b = 0; b < l.batch; ++b){ 87 | for(c = 0; c < l.c; ++c){ 88 | for(i = 0; i < l.out_h; ++i){ 89 | for(j = 0; j < l.out_w; ++j){ 90 | if(flip){ 91 | col = l.w - dw - j - 1; 92 | }else{ 93 | col = j + dw; 94 | } 95 | row = i + dh; 96 | index = col+l.w*(row+l.h*(c + l.c*b)); 97 | l.output[count++] = net.input[index]*scale + trans; 98 | } 99 | } 100 | } 101 | } 102 | } 103 | 104 | -------------------------------------------------------------------------------- /Inference/src/crop_layer.c: -------------------------------------------------------------------------------- 1 | #include "crop_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | image get_crop_image(crop_layer l) 6 | { 7 | int h = l.out_h; 8 | int w = l.out_w; 9 | int c = l.out_c; 10 | return float_to_image(w,h,c,l.output); 11 | } 12 | 13 | void backward_crop_layer(const crop_layer l, network net){} 14 | void backward_crop_layer_gpu(const crop_layer l, network net){} 15 | 16 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure) 17 | { 18 | fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c); 19 | crop_layer l = {0}; 20 | l.type = CROP; 21 | l.batch = batch; 22 | l.h = h; 23 | l.w = w; 24 | l.c = c; 25 | l.scale = (float)crop_height / h; 26 | l.flip = flip; 27 | l.angle = angle; 28 | l.saturation = saturation; 29 | l.exposure = exposure; 30 | l.out_w = crop_width; 31 | l.out_h = crop_height; 32 | l.out_c = c; 33 | l.inputs = l.w * l.h * l.c; 34 | l.outputs = l.out_w * l.out_h * l.out_c; 35 | l.output = calloc(l.outputs*batch, sizeof(float)); 36 | l.forward = forward_crop_layer; 37 | l.backward = backward_crop_layer; 38 | 39 | #ifdef GPU 40 | l.forward_gpu = forward_crop_layer_gpu; 41 | l.backward_gpu = backward_crop_layer_gpu; 42 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 43 | l.rand_gpu = cuda_make_array(0, l.batch*8); 44 | #endif 45 | return l; 46 | } 47 | 48 | void resize_crop_layer(layer *l, int w, int h) 49 | { 50 | l->w = w; 51 | l->h = h; 52 | 53 | l->out_w = l->scale*w; 54 | l->out_h = l->scale*h; 55 | 56 | l->inputs = l->w * l->h * l->c; 57 | l->outputs = l->out_h * l->out_w * l->out_c; 58 | 59 | l->output = realloc(l->output, l->batch*l->outputs*sizeof(float)); 60 | #ifdef GPU 61 | cuda_free(l->output_gpu); 62 | l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch); 63 | #endif 64 | } 65 | 66 | 67 | void forward_crop_layer(const crop_layer l, network net) 68 | { 69 | int i,j,c,b,row,col; 70 | int index; 71 | int count = 0; 72 | int flip = (l.flip && rand()%2); 73 | int dh = rand()%(l.h - l.out_h + 1); 74 | int dw = rand()%(l.w - l.out_w + 1); 75 | float scale = 2; 76 | float trans = -1; 77 | if(l.noadjust){ 78 | scale = 1; 79 | trans = 0; 80 | } 81 | if(!net.train){ 82 | flip = 0; 83 | dh = (l.h - l.out_h)/2; 84 | dw = (l.w - l.out_w)/2; 85 | } 86 | for(b = 0; b < l.batch; ++b){ 87 | for(c = 0; c < l.c; ++c){ 88 | for(i = 0; i < l.out_h; ++i){ 89 | for(j = 0; j < l.out_w; ++j){ 90 | if(flip){ 91 | col = l.w - dw - j - 1; 92 | }else{ 93 | col = j + dw; 94 | } 95 | row = i + dh; 96 | index = col+l.w*(row+l.h*(c + l.c*b)); 97 | l.output[count++] = net.input[index]*scale + trans; 98 | } 99 | } 100 | } 101 | } 102 | } 103 | 104 | -------------------------------------------------------------------------------- /Train/src/shortcut_layer.c: -------------------------------------------------------------------------------- 1 | #include "shortcut_layer.h" 2 | #include "cuda.h" 3 | #include "blas.h" 4 | #include "activations.h" 5 | 6 | #include 7 | #include 8 | 9 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2) 10 | { 11 | fprintf(stderr, "res %3d %4d x%4d x%4d -> %4d x%4d x%4d\n",index, w2,h2,c2, w,h,c); 12 | layer l = {0}; 13 | l.type = SHORTCUT; 14 | l.batch = batch; 15 | l.w = w2; 16 | l.h = h2; 17 | l.c = c2; 18 | l.out_w = w; 19 | l.out_h = h; 20 | l.out_c = c; 21 | l.outputs = w*h*c; 22 | l.inputs = l.outputs; 23 | 24 | l.index = index; 25 | 26 | l.delta = calloc(l.outputs*batch, sizeof(float)); 27 | l.output = calloc(l.outputs*batch, sizeof(float));; 28 | 29 | l.forward = forward_shortcut_layer; 30 | l.backward = backward_shortcut_layer; 31 | #ifdef GPU 32 | l.forward_gpu = forward_shortcut_layer_gpu; 33 | l.backward_gpu = backward_shortcut_layer_gpu; 34 | 35 | l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch); 36 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 37 | #endif 38 | return l; 39 | } 40 | 41 | void resize_shortcut_layer(layer *l, int w, int h) 42 | { 43 | assert(l->w == l->out_w); 44 | assert(l->h == l->out_h); 45 | l->w = l->out_w = w; 46 | l->h = l->out_h = h; 47 | l->outputs = w*h*l->out_c; 48 | l->inputs = l->outputs; 49 | l->delta = realloc(l->delta, l->outputs*l->batch*sizeof(float)); 50 | l->output = realloc(l->output, l->outputs*l->batch*sizeof(float)); 51 | 52 | #ifdef GPU 53 | cuda_free(l->output_gpu); 54 | cuda_free(l->delta_gpu); 55 | l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch); 56 | l->delta_gpu = cuda_make_array(l->delta, l->outputs*l->batch); 57 | #endif 58 | 59 | } 60 | 61 | 62 | void forward_shortcut_layer(const layer l, network net) 63 | { 64 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 65 | shortcut_cpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output, l.out_w, l.out_h, l.out_c, l.alpha, l.beta, l.output); 66 | activate_array(l.output, l.outputs*l.batch, l.activation); 67 | } 68 | 69 | void backward_shortcut_layer(const layer l, network net) 70 | { 71 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 72 | axpy_cpu(l.outputs*l.batch, l.alpha, l.delta, 1, net.delta, 1); 73 | shortcut_cpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta, l.w, l.h, l.c, 1, l.beta, net.layers[l.index].delta); 74 | } 75 | 76 | #ifdef GPU 77 | void forward_shortcut_layer_gpu(const layer l, network net) 78 | { 79 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 80 | shortcut_gpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output_gpu, l.out_w, l.out_h, l.out_c, l.alpha, l.beta, l.output_gpu); 81 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 82 | } 83 | 84 | void backward_shortcut_layer_gpu(const layer l, network net) 85 | { 86 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 87 | axpy_gpu(l.outputs*l.batch, l.alpha, l.delta_gpu, 1, net.delta_gpu, 1); 88 | shortcut_gpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta_gpu, l.w, l.h, l.c, 1, l.beta, net.layers[l.index].delta_gpu); 89 | } 90 | #endif 91 | -------------------------------------------------------------------------------- /Inference/src/shortcut_layer.c: -------------------------------------------------------------------------------- 1 | #include "shortcut_layer.h" 2 | #include "cuda.h" 3 | #include "blas.h" 4 | #include "activations.h" 5 | 6 | #include 7 | #include 8 | 9 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2) 10 | { 11 | fprintf(stderr, "res %3d %4d x%4d x%4d -> %4d x%4d x%4d\n",index, w2,h2,c2, w,h,c); 12 | layer l = {0}; 13 | l.type = SHORTCUT; 14 | l.batch = batch; 15 | l.w = w2; 16 | l.h = h2; 17 | l.c = c2; 18 | l.out_w = w; 19 | l.out_h = h; 20 | l.out_c = c; 21 | l.outputs = w*h*c; 22 | l.inputs = l.outputs; 23 | 24 | l.index = index; 25 | 26 | l.delta = calloc(l.outputs*batch, sizeof(float)); 27 | l.output = calloc(l.outputs*batch, sizeof(float));; 28 | 29 | l.forward = forward_shortcut_layer; 30 | l.backward = backward_shortcut_layer; 31 | #ifdef GPU 32 | l.forward_gpu = forward_shortcut_layer_gpu; 33 | l.backward_gpu = backward_shortcut_layer_gpu; 34 | 35 | l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch); 36 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 37 | #endif 38 | return l; 39 | } 40 | 41 | void resize_shortcut_layer(layer *l, int w, int h) 42 | { 43 | assert(l->w == l->out_w); 44 | assert(l->h == l->out_h); 45 | l->w = l->out_w = w; 46 | l->h = l->out_h = h; 47 | l->outputs = w*h*l->out_c; 48 | l->inputs = l->outputs; 49 | l->delta = realloc(l->delta, l->outputs*l->batch*sizeof(float)); 50 | l->output = realloc(l->output, l->outputs*l->batch*sizeof(float)); 51 | 52 | #ifdef GPU 53 | cuda_free(l->output_gpu); 54 | cuda_free(l->delta_gpu); 55 | l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch); 56 | l->delta_gpu = cuda_make_array(l->delta, l->outputs*l->batch); 57 | #endif 58 | 59 | } 60 | 61 | 62 | void forward_shortcut_layer(const layer l, network net) 63 | { 64 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 65 | shortcut_cpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output, l.out_w, l.out_h, l.out_c, l.alpha, l.beta, l.output); 66 | activate_array(l.output, l.outputs*l.batch, l.activation); 67 | } 68 | 69 | void backward_shortcut_layer(const layer l, network net) 70 | { 71 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 72 | axpy_cpu(l.outputs*l.batch, l.alpha, l.delta, 1, net.delta, 1); 73 | shortcut_cpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta, l.w, l.h, l.c, 1, l.beta, net.layers[l.index].delta); 74 | } 75 | 76 | #ifdef GPU 77 | void forward_shortcut_layer_gpu(const layer l, network net) 78 | { 79 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 80 | shortcut_gpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output_gpu, l.out_w, l.out_h, l.out_c, l.alpha, l.beta, l.output_gpu); 81 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 82 | } 83 | 84 | void backward_shortcut_layer_gpu(const layer l, network net) 85 | { 86 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 87 | axpy_gpu(l.outputs*l.batch, l.alpha, l.delta_gpu, 1, net.delta_gpu, 1); 88 | shortcut_gpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta_gpu, l.w, l.h, l.c, 1, l.beta, net.layers[l.index].delta_gpu); 89 | } 90 | #endif 91 | -------------------------------------------------------------------------------- /Train/data/script/divide_dataset_randomly.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding: utf-8 3 | # python 2.7.12 4 | 5 | from __future__ import print_function 6 | import os 7 | import glob 8 | import random 9 | 10 | 11 | def divide_cls(image_path_lst, train_set_cls, train_set_lst, valid_set_lst): 12 | ratio = 0.8 # train:valid = 8:2 13 | image_path_size = len(image_path_lst) 14 | train_set = image_path_lst[:int(ratio * image_path_size)] 15 | valid_set = image_path_lst[int(ratio * image_path_size):] 16 | 17 | with open(train_set_cls, 'w') as f: 18 | f.write('\n'.join(train_set)) 19 | train_set_lst += train_set 20 | valid_set_lst += valid_set 21 | 22 | # check 23 | for valid_path in valid_set: 24 | assert valid_path not in train_set 25 | 26 | 27 | def shuffle_data(data): 28 | random.shuffle(data) 29 | return data 30 | 31 | 32 | def load_data(data_dir, cls): 33 | return glob.glob(os.path.join(data_dir, cls, '*.jpg')) 34 | 35 | 36 | if __name__ == '__main__': 37 | # set paths 38 | TRAIN_DATA_ROOT = os.path.abspath(os.path.pardir) 39 | train_set_cls_meta = os.path.join(TRAIN_DATA_ROOT, 'dataset', 'train_dataset_cls.txt') 40 | train_set_meta = os.path.join(TRAIN_DATA_ROOT, 'dataset', 'train_dataset.txt') 41 | valid_set_meta = os.path.join(TRAIN_DATA_ROOT, 'dataset', 'valid_dataset.txt') 42 | valid_set_label_meta = os.path.join(TRAIN_DATA_ROOT, 'dataset', 'valid_dataset_label.txt') 43 | if not os.path.exists(os.path.join(TRAIN_DATA_ROOT, 'dataset')): 44 | os.mkdir(os.path.join(TRAIN_DATA_ROOT, 'dataset')) 45 | # load the list of class 46 | data_dir = os.path.join(TRAIN_DATA_ROOT, 'train_dataset') 47 | cls_lst = sorted(os.listdir(data_dir)) 48 | # for each class 49 | train_set_cls_lst, train_set_lst, valid_set_lst = [], [], [] 50 | for cls in cls_lst: 51 | print('processing class %s ...' % cls) 52 | # initialize 53 | train_set_cls = os.path.join(data_dir, cls, 'train_%s.txt' % cls) 54 | # add path 55 | train_set_cls_lst.append(train_set_cls) 56 | # load and shuffle 57 | image_path_lst = shuffle_data(load_data(data_dir, cls)) 58 | # divide 59 | divide_cls(image_path_lst, train_set_cls, train_set_lst, valid_set_lst) 60 | 61 | # write dataset into meta files 62 | with open(train_set_cls_meta, 'w') as f: 63 | f.write('\n'.join(train_set_cls_lst)) 64 | # shuffle 65 | train_set_lst = shuffle_data(train_set_lst) 66 | with open(train_set_meta, 'w') as f: 67 | f.write('\n'.join(train_set_lst)) 68 | with open(valid_set_meta, 'w') as f: 69 | f.write('\n'.join(valid_set_lst)) 70 | 71 | # generate the corresponding label file 72 | label_dict = {} 73 | for cls in cls_lst: 74 | label_file = os.path.join(TRAIN_DATA_ROOT, 'train_dataset', cls, 'label_all.txt') 75 | with open(label_file, 'r') as f: 76 | for line in f: 77 | lst = line.strip().split() 78 | frame, box = lst[0], ' '.join(lst[1:]) 79 | label_dict[frame] = box 80 | label_lst = [] 81 | for frame_path in valid_set_lst: 82 | frame = frame_path.split(os.path.sep)[-1].split('.')[0] 83 | label_lst.append(label_dict[frame]) 84 | with open(valid_set_label_meta, 'w') as f: 85 | f.write('\n'.join(label_lst)) -------------------------------------------------------------------------------- /Train/data/script/divide_dataset.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding: utf-8 3 | # python 2.7.12 4 | 5 | from __future__ import print_function 6 | import os 7 | import shutil 8 | import random 9 | 10 | 11 | """ 12 | Revise the paths based on 4 meta files. 13 | """ 14 | 15 | 16 | if __name__ == '__main__': 17 | # set paths 18 | TRAIN_DATA_ROOT = os.path.abspath(os.path.pardir) 19 | 20 | train_set_cls_meta_divided = os.path.join( 21 | TRAIN_DATA_ROOT, 'dataset', 'divided_dataset', 'train_dataset_cls.txt') 22 | train_set_meta_divided = os.path.join( 23 | TRAIN_DATA_ROOT, 'dataset', 'divided_dataset', 'train_dataset.txt') 24 | valid_set_meta_divided = os.path.join( 25 | TRAIN_DATA_ROOT, 'dataset', 'divided_dataset', 'valid_dataset.txt') 26 | valid_set_label_meta_divided = os.path.join( 27 | TRAIN_DATA_ROOT, 'dataset', 'divided_dataset', 'valid_dataset_label.txt') 28 | 29 | train_set_cls_meta = os.path.join( 30 | TRAIN_DATA_ROOT, 'dataset', 'train_dataset_cls.txt') 31 | train_set_meta = os.path.join( 32 | TRAIN_DATA_ROOT, 'dataset', 'train_dataset.txt') 33 | valid_set_meta = os.path.join( 34 | TRAIN_DATA_ROOT, 'dataset', 'valid_dataset.txt') 35 | valid_set_label_meta = os.path.join( 36 | TRAIN_DATA_ROOT, 'dataset', 'valid_dataset_label.txt') 37 | 38 | # revise train_set_cls 39 | with open(train_set_cls_meta_divided, 'r') as f: 40 | train_set_cls = [line.strip() for line in f if line] 41 | train_set_cls_new = [] 42 | for train_cls in train_set_cls: 43 | cls = train_cls.split('_')[-1].split('.')[0] 44 | train_set_cls_new.append( 45 | os.path.join(TRAIN_DATA_ROOT, 'train_dataset', cls, train_cls) 46 | ) 47 | with open(train_set_cls_meta, 'w') as f: 48 | f.write('\n'.join(train_set_cls_new)) 49 | 50 | # revise train_set 51 | with open(train_set_meta_divided, 'r') as f: 52 | train_set = [line.strip() for line in f if line] 53 | train_set_new = [] 54 | for train_path in train_set: 55 | cls = train_path.split('_')[0] 56 | train_set_new.append( 57 | os.path.join(TRAIN_DATA_ROOT, 'train_dataset', cls, train_path) 58 | ) 59 | with open(train_set_meta, 'w') as f: 60 | f.write('\n'.join(train_set_new)) 61 | 62 | # revise valid_set 63 | with open(valid_set_meta_divided, 'r') as f: 64 | valid_set = [line.strip() for line in f if line] 65 | valid_set_new = [] 66 | for valid_path in valid_set: 67 | cls = valid_path.split('_')[0] 68 | valid_set_new.append( 69 | os.path.join(TRAIN_DATA_ROOT, 'train_dataset', cls, valid_path) 70 | ) 71 | with open(valid_set_meta, 'w') as f: 72 | f.write('\n'.join(valid_set_new)) 73 | 74 | # copy 75 | shutil.copyfile(valid_set_label_meta_divided, valid_set_label_meta) 76 | 77 | # revise train_cls 78 | train_cls_dict = {} 79 | for train_path in train_set_new: 80 | cls = train_path.split(os.path.sep)[-1].split('_')[0] 81 | if cls in train_cls_dict: 82 | train_cls_dict[cls].append(train_path) 83 | else: 84 | train_cls_dict[cls] = [train_path] 85 | 86 | for cls, path_lst in train_cls_dict.items(): 87 | random.shuffle(path_lst) 88 | cls_dir = os.path.join(TRAIN_DATA_ROOT, 'train_dataset', cls) 89 | with open(os.path.join(cls_dir, 'train_%s.txt' % cls), 'w') as f: 90 | f.write('\n'.join(path_lst)) -------------------------------------------------------------------------------- /Train/src/maxpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "maxpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *input, float *output, int *indexes) 11 | { 12 | int h = (in_h + 2*pad)/stride; 13 | int w = (in_w + 2*pad)/stride; 14 | int c = in_c; 15 | 16 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 17 | if(id >= n) return; 18 | 19 | int j = id % w; 20 | id /= w; 21 | int i = id % h; 22 | id /= h; 23 | int k = id % c; 24 | id /= c; 25 | int b = id; 26 | 27 | int w_offset = -pad; 28 | int h_offset = -pad; 29 | 30 | int out_index = j + w*(i + h*(k + c*b)); 31 | float max = -INFINITY; 32 | int max_i = -1; 33 | int l, m; 34 | for(l = 0; l < size; ++l){ 35 | for(m = 0; m < size; ++m){ 36 | int cur_h = h_offset + i*stride + l; 37 | int cur_w = w_offset + j*stride + m; 38 | int index = cur_w + in_w*(cur_h + in_h*(k + b*in_c)); 39 | int valid = (cur_h >= 0 && cur_h < in_h && 40 | cur_w >= 0 && cur_w < in_w); 41 | float val = (valid != 0) ? input[index] : -INFINITY; 42 | max_i = (val > max) ? index : max_i; 43 | max = (val > max) ? val : max; 44 | } 45 | } 46 | output[out_index] = max; 47 | indexes[out_index] = max_i; 48 | } 49 | 50 | __global__ void backward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *delta, float *prev_delta, int *indexes) 51 | { 52 | int h = (in_h + 2*pad)/stride; 53 | int w = (in_w + 2*pad)/stride; 54 | int c = in_c; 55 | int area = (size-1)/stride; 56 | 57 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 58 | if(id >= n) return; 59 | 60 | int index = id; 61 | int j = id % in_w; 62 | id /= in_w; 63 | int i = id % in_h; 64 | id /= in_h; 65 | int k = id % in_c; 66 | id /= in_c; 67 | int b = id; 68 | 69 | int w_offset = -pad; 70 | int h_offset = -pad; 71 | 72 | float d = 0; 73 | int l, m; 74 | for(l = -area; l < area+1; ++l){ 75 | for(m = -area; m < area+1; ++m){ 76 | int out_w = (j-w_offset)/stride + m; 77 | int out_h = (i-h_offset)/stride + l; 78 | int out_index = out_w + w*(out_h + h*(k + c*b)); 79 | int valid = (out_w >= 0 && out_w < w && 80 | out_h >= 0 && out_h < h); 81 | d += (valid && indexes[out_index] == index) ? delta[out_index] : 0; 82 | } 83 | } 84 | prev_delta[index] += d; 85 | } 86 | 87 | extern "C" void forward_maxpool_layer_gpu(maxpool_layer layer, network net) 88 | { 89 | int h = layer.out_h; 90 | int w = layer.out_w; 91 | int c = layer.c; 92 | 93 | size_t n = h*w*c*layer.batch; 94 | 95 | forward_maxpool_layer_kernel<<>>(n, layer.h, layer.w, layer.c, layer.stride, layer.size, layer.pad, net.input_gpu, layer.output_gpu, layer.indexes_gpu); 96 | check_error(cudaPeekAtLastError()); 97 | } 98 | 99 | extern "C" void backward_maxpool_layer_gpu(maxpool_layer layer, network net) 100 | { 101 | size_t n = layer.h*layer.w*layer.c*layer.batch; 102 | 103 | backward_maxpool_layer_kernel<<>>(n, layer.h, layer.w, layer.c, layer.stride, layer.size, layer.pad, layer.delta_gpu, net.delta_gpu, layer.indexes_gpu); 104 | check_error(cudaPeekAtLastError()); 105 | } 106 | 107 | -------------------------------------------------------------------------------- /Inference/src/maxpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "maxpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *input, float *output, int *indexes) 11 | { 12 | int h = (in_h + 2*pad)/stride; 13 | int w = (in_w + 2*pad)/stride; 14 | int c = in_c; 15 | 16 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 17 | if(id >= n) return; 18 | 19 | int j = id % w; 20 | id /= w; 21 | int i = id % h; 22 | id /= h; 23 | int k = id % c; 24 | id /= c; 25 | int b = id; 26 | 27 | int w_offset = -pad; 28 | int h_offset = -pad; 29 | 30 | int out_index = j + w*(i + h*(k + c*b)); 31 | float max = -INFINITY; 32 | int max_i = -1; 33 | int l, m; 34 | for(l = 0; l < size; ++l){ 35 | for(m = 0; m < size; ++m){ 36 | int cur_h = h_offset + i*stride + l; 37 | int cur_w = w_offset + j*stride + m; 38 | int index = cur_w + in_w*(cur_h + in_h*(k + b*in_c)); 39 | int valid = (cur_h >= 0 && cur_h < in_h && 40 | cur_w >= 0 && cur_w < in_w); 41 | float val = (valid != 0) ? input[index] : -INFINITY; 42 | max_i = (val > max) ? index : max_i; 43 | max = (val > max) ? val : max; 44 | } 45 | } 46 | output[out_index] = max; 47 | indexes[out_index] = max_i; 48 | } 49 | 50 | __global__ void backward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *delta, float *prev_delta, int *indexes) 51 | { 52 | int h = (in_h + 2*pad)/stride; 53 | int w = (in_w + 2*pad)/stride; 54 | int c = in_c; 55 | int area = (size-1)/stride; 56 | 57 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 58 | if(id >= n) return; 59 | 60 | int index = id; 61 | int j = id % in_w; 62 | id /= in_w; 63 | int i = id % in_h; 64 | id /= in_h; 65 | int k = id % in_c; 66 | id /= in_c; 67 | int b = id; 68 | 69 | int w_offset = -pad; 70 | int h_offset = -pad; 71 | 72 | float d = 0; 73 | int l, m; 74 | for(l = -area; l < area+1; ++l){ 75 | for(m = -area; m < area+1; ++m){ 76 | int out_w = (j-w_offset)/stride + m; 77 | int out_h = (i-h_offset)/stride + l; 78 | int out_index = out_w + w*(out_h + h*(k + c*b)); 79 | int valid = (out_w >= 0 && out_w < w && 80 | out_h >= 0 && out_h < h); 81 | d += (valid && indexes[out_index] == index) ? delta[out_index] : 0; 82 | } 83 | } 84 | prev_delta[index] += d; 85 | } 86 | 87 | extern "C" void forward_maxpool_layer_gpu(maxpool_layer layer, network net) 88 | { 89 | int h = layer.out_h; 90 | int w = layer.out_w; 91 | int c = layer.c; 92 | 93 | size_t n = h*w*c*layer.batch; 94 | 95 | forward_maxpool_layer_kernel<<>>(n, layer.h, layer.w, layer.c, layer.stride, layer.size, layer.pad, net.input_gpu, layer.output_gpu, layer.indexes_gpu); 96 | check_error(cudaPeekAtLastError()); 97 | } 98 | 99 | extern "C" void backward_maxpool_layer_gpu(maxpool_layer layer, network net) 100 | { 101 | size_t n = layer.h*layer.w*layer.c*layer.batch; 102 | 103 | backward_maxpool_layer_kernel<<>>(n, layer.h, layer.w, layer.c, layer.stride, layer.size, layer.pad, layer.delta_gpu, net.delta_gpu, layer.indexes_gpu); 104 | check_error(cudaPeekAtLastError()); 105 | } 106 | 107 | -------------------------------------------------------------------------------- /Train/src/upsample_layer.c: -------------------------------------------------------------------------------- 1 | #include "upsample_layer.h" 2 | #include "cuda.h" 3 | #include "blas.h" 4 | 5 | #include 6 | 7 | layer make_upsample_layer(int batch, int w, int h, int c, int stride) 8 | { 9 | layer l = {0}; 10 | l.type = UPSAMPLE; 11 | l.batch = batch; 12 | l.w = w; 13 | l.h = h; 14 | l.c = c; 15 | l.out_w = w*stride; 16 | l.out_h = h*stride; 17 | l.out_c = c; 18 | if(stride < 0){ 19 | stride = -stride; 20 | l.reverse=1; 21 | l.out_w = w/stride; 22 | l.out_h = h/stride; 23 | } 24 | l.stride = stride; 25 | l.outputs = l.out_w*l.out_h*l.out_c; 26 | l.inputs = l.w*l.h*l.c; 27 | l.delta = calloc(l.outputs*batch, sizeof(float)); 28 | l.output = calloc(l.outputs*batch, sizeof(float));; 29 | 30 | l.forward = forward_upsample_layer; 31 | l.backward = backward_upsample_layer; 32 | #ifdef GPU 33 | l.forward_gpu = forward_upsample_layer_gpu; 34 | l.backward_gpu = backward_upsample_layer_gpu; 35 | 36 | l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch); 37 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 38 | #endif 39 | if(l.reverse) fprintf(stderr, "downsample %2dx %4d x%4d x%4d -> %4d x%4d x%4d\n", stride, w, h, c, l.out_w, l.out_h, l.out_c); 40 | else fprintf(stderr, "upsample %2dx %4d x%4d x%4d -> %4d x%4d x%4d\n", stride, w, h, c, l.out_w, l.out_h, l.out_c); 41 | return l; 42 | } 43 | 44 | void resize_upsample_layer(layer *l, int w, int h) 45 | { 46 | l->w = w; 47 | l->h = h; 48 | l->out_w = w*l->stride; 49 | l->out_h = h*l->stride; 50 | if(l->reverse){ 51 | l->out_w = w/l->stride; 52 | l->out_h = h/l->stride; 53 | } 54 | l->outputs = l->out_w*l->out_h*l->out_c; 55 | l->inputs = l->h*l->w*l->c; 56 | l->delta = realloc(l->delta, l->outputs*l->batch*sizeof(float)); 57 | l->output = realloc(l->output, l->outputs*l->batch*sizeof(float)); 58 | 59 | #ifdef GPU 60 | cuda_free(l->output_gpu); 61 | cuda_free(l->delta_gpu); 62 | l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch); 63 | l->delta_gpu = cuda_make_array(l->delta, l->outputs*l->batch); 64 | #endif 65 | 66 | } 67 | 68 | void forward_upsample_layer(const layer l, network net) 69 | { 70 | fill_cpu(l.outputs*l.batch, 0, l.output, 1); 71 | if(l.reverse){ 72 | upsample_cpu(l.output, l.out_w, l.out_h, l.c, l.batch, l.stride, 0, l.scale, net.input); 73 | }else{ 74 | upsample_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.scale, l.output); 75 | } 76 | } 77 | 78 | void backward_upsample_layer(const layer l, network net) 79 | { 80 | if(l.reverse){ 81 | upsample_cpu(l.delta, l.out_w, l.out_h, l.c, l.batch, l.stride, 1, l.scale, net.delta); 82 | }else{ 83 | upsample_cpu(net.delta, l.w, l.h, l.c, l.batch, l.stride, 0, l.scale, l.delta); 84 | } 85 | } 86 | 87 | #ifdef GPU 88 | void forward_upsample_layer_gpu(const layer l, network net) 89 | { 90 | fill_gpu(l.outputs*l.batch, 0, l.output_gpu, 1); 91 | if(l.reverse){ 92 | upsample_gpu(l.output_gpu, l.out_w, l.out_h, l.c, l.batch, l.stride, 0, l.scale, net.input_gpu); 93 | }else{ 94 | upsample_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, l.scale, l.output_gpu); 95 | } 96 | } 97 | 98 | void backward_upsample_layer_gpu(const layer l, network net) 99 | { 100 | if(l.reverse){ 101 | upsample_gpu(l.delta_gpu, l.out_w, l.out_h, l.c, l.batch, l.stride, 1, l.scale, net.delta_gpu); 102 | }else{ 103 | upsample_gpu(net.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, l.scale, l.delta_gpu); 104 | } 105 | } 106 | #endif 107 | -------------------------------------------------------------------------------- /Train/src/option_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "option_list.h" 5 | #include "utils.h" 6 | 7 | list *read_data_cfg(char *filename) 8 | { 9 | FILE *file = fopen(filename, "r"); 10 | if(file == 0) file_error(filename); 11 | char *line; 12 | int nu = 0; 13 | list *options = make_list(); 14 | while((line=fgetl(file)) != 0){ 15 | ++ nu; 16 | strip(line); 17 | switch(line[0]){ 18 | case '\0': 19 | case '#': 20 | case ';': 21 | free(line); 22 | break; 23 | default: 24 | if(!read_option(line, options)){ 25 | fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line); 26 | free(line); 27 | } 28 | break; 29 | } 30 | } 31 | fclose(file); 32 | return options; 33 | } 34 | 35 | metadata get_metadata(char *file) 36 | { 37 | metadata m = {0}; 38 | list *options = read_data_cfg(file); 39 | 40 | char *name_list = option_find_str(options, "names", 0); 41 | if(!name_list) name_list = option_find_str(options, "labels", 0); 42 | if(!name_list) { 43 | fprintf(stderr, "No names or labels found\n"); 44 | } else { 45 | m.names = get_labels(name_list); 46 | } 47 | m.classes = option_find_int(options, "classes", 2); 48 | free_list(options); 49 | return m; 50 | } 51 | 52 | int read_option(char *s, list *options) 53 | { 54 | size_t i; 55 | size_t len = strlen(s); 56 | char *val = 0; 57 | for(i = 0; i < len; ++i){ 58 | if(s[i] == '='){ 59 | s[i] = '\0'; 60 | val = s+i+1; 61 | break; 62 | } 63 | } 64 | if(i == len-1) return 0; 65 | char *key = s; 66 | option_insert(options, key, val); 67 | return 1; 68 | } 69 | 70 | void option_insert(list *l, char *key, char *val) 71 | { 72 | kvp *p = malloc(sizeof(kvp)); 73 | p->key = key; 74 | p->val = val; 75 | p->used = 0; 76 | list_insert(l, p); 77 | } 78 | 79 | void option_unused(list *l) 80 | { 81 | node *n = l->front; 82 | while(n){ 83 | kvp *p = (kvp *)n->val; 84 | if(!p->used){ 85 | fprintf(stderr, "Unused field: '%s = %s'\n", p->key, p->val); 86 | } 87 | n = n->next; 88 | } 89 | } 90 | 91 | char *option_find(list *l, char *key) 92 | { 93 | node *n = l->front; 94 | while(n){ 95 | kvp *p = (kvp *)n->val; 96 | if(strcmp(p->key, key) == 0){ 97 | p->used = 1; 98 | return p->val; 99 | } 100 | n = n->next; 101 | } 102 | return 0; 103 | } 104 | char *option_find_str(list *l, char *key, char *def) 105 | { 106 | char *v = option_find(l, key); 107 | if(v) return v; 108 | //if(def) fprintf(stderr, "%s: Using default '%s'\n", key, def); 109 | return def; 110 | } 111 | 112 | int option_find_int(list *l, char *key, int def) 113 | { 114 | char *v = option_find(l, key); 115 | if(v) return atoi(v); 116 | fprintf(stderr, "%s: Using default '%d'\n", key, def); 117 | return def; 118 | } 119 | 120 | int option_find_int_quiet(list *l, char *key, int def) 121 | { 122 | char *v = option_find(l, key); 123 | if(v) return atoi(v); 124 | return def; 125 | } 126 | 127 | float option_find_float_quiet(list *l, char *key, float def) 128 | { 129 | char *v = option_find(l, key); 130 | if(v) return atof(v); 131 | return def; 132 | } 133 | 134 | float option_find_float(list *l, char *key, float def) 135 | { 136 | char *v = option_find(l, key); 137 | if(v) return atof(v); 138 | fprintf(stderr, "%s: Using default '%lf'\n", key, def); 139 | return def; 140 | } 141 | -------------------------------------------------------------------------------- /Inference/src/option_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "option_list.h" 5 | #include "utils.h" 6 | 7 | list *read_data_cfg(char *filename) 8 | { 9 | FILE *file = fopen(filename, "r"); 10 | if(file == 0) file_error(filename); 11 | char *line; 12 | int nu = 0; 13 | list *options = make_list(); 14 | while((line=fgetl(file)) != 0){ 15 | ++ nu; 16 | strip(line); 17 | switch(line[0]){ 18 | case '\0': 19 | case '#': 20 | case ';': 21 | free(line); 22 | break; 23 | default: 24 | if(!read_option(line, options)){ 25 | fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line); 26 | free(line); 27 | } 28 | break; 29 | } 30 | } 31 | fclose(file); 32 | return options; 33 | } 34 | 35 | metadata get_metadata(char *file) 36 | { 37 | metadata m = {0}; 38 | list *options = read_data_cfg(file); 39 | 40 | char *name_list = option_find_str(options, "names", 0); 41 | if(!name_list) name_list = option_find_str(options, "labels", 0); 42 | if(!name_list) { 43 | fprintf(stderr, "No names or labels found\n"); 44 | } else { 45 | m.names = get_labels(name_list); 46 | } 47 | m.classes = option_find_int(options, "classes", 2); 48 | free_list(options); 49 | return m; 50 | } 51 | 52 | int read_option(char *s, list *options) 53 | { 54 | size_t i; 55 | size_t len = strlen(s); 56 | char *val = 0; 57 | for(i = 0; i < len; ++i){ 58 | if(s[i] == '='){ 59 | s[i] = '\0'; 60 | val = s+i+1; 61 | break; 62 | } 63 | } 64 | if(i == len-1) return 0; 65 | char *key = s; 66 | option_insert(options, key, val); 67 | return 1; 68 | } 69 | 70 | void option_insert(list *l, char *key, char *val) 71 | { 72 | kvp *p = malloc(sizeof(kvp)); 73 | p->key = key; 74 | p->val = val; 75 | p->used = 0; 76 | list_insert(l, p); 77 | } 78 | 79 | void option_unused(list *l) 80 | { 81 | node *n = l->front; 82 | while(n){ 83 | kvp *p = (kvp *)n->val; 84 | if(!p->used){ 85 | fprintf(stderr, "Unused field: '%s = %s'\n", p->key, p->val); 86 | } 87 | n = n->next; 88 | } 89 | } 90 | 91 | char *option_find(list *l, char *key) 92 | { 93 | node *n = l->front; 94 | while(n){ 95 | kvp *p = (kvp *)n->val; 96 | if(strcmp(p->key, key) == 0){ 97 | p->used = 1; 98 | return p->val; 99 | } 100 | n = n->next; 101 | } 102 | return 0; 103 | } 104 | char *option_find_str(list *l, char *key, char *def) 105 | { 106 | char *v = option_find(l, key); 107 | if(v) return v; 108 | //if(def) fprintf(stderr, "%s: Using default '%s'\n", key, def); 109 | return def; 110 | } 111 | 112 | int option_find_int(list *l, char *key, int def) 113 | { 114 | char *v = option_find(l, key); 115 | if(v) return atoi(v); 116 | fprintf(stderr, "%s: Using default '%d'\n", key, def); 117 | return def; 118 | } 119 | 120 | int option_find_int_quiet(list *l, char *key, int def) 121 | { 122 | char *v = option_find(l, key); 123 | if(v) return atoi(v); 124 | return def; 125 | } 126 | 127 | float option_find_float_quiet(list *l, char *key, float def) 128 | { 129 | char *v = option_find(l, key); 130 | if(v) return atof(v); 131 | return def; 132 | } 133 | 134 | float option_find_float(list *l, char *key, float def) 135 | { 136 | char *v = option_find(l, key); 137 | if(v) return atof(v); 138 | fprintf(stderr, "%s: Using default '%lf'\n", key, def); 139 | return def; 140 | } 141 | -------------------------------------------------------------------------------- /Train/src/softmax_layer.c: -------------------------------------------------------------------------------- 1 | #include "softmax_layer.h" 2 | #include "blas.h" 3 | #include "cuda.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | softmax_layer make_softmax_layer(int batch, int inputs, int groups) 12 | { 13 | assert(inputs%groups == 0); 14 | fprintf(stderr, "softmax %4d\n", inputs); 15 | softmax_layer l = {0}; 16 | l.type = SOFTMAX; 17 | l.batch = batch; 18 | l.groups = groups; 19 | l.inputs = inputs; 20 | l.outputs = inputs; 21 | l.loss = calloc(inputs*batch, sizeof(float)); 22 | l.output = calloc(inputs*batch, sizeof(float)); 23 | l.delta = calloc(inputs*batch, sizeof(float)); 24 | l.cost = calloc(1, sizeof(float)); 25 | 26 | l.forward = forward_softmax_layer; 27 | l.backward = backward_softmax_layer; 28 | #ifdef GPU 29 | l.forward_gpu = forward_softmax_layer_gpu; 30 | l.backward_gpu = backward_softmax_layer_gpu; 31 | 32 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 33 | l.loss_gpu = cuda_make_array(l.loss, inputs*batch); 34 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 35 | #endif 36 | return l; 37 | } 38 | 39 | void forward_softmax_layer(const softmax_layer l, network net) 40 | { 41 | if(l.softmax_tree){ 42 | int i; 43 | int count = 0; 44 | for (i = 0; i < l.softmax_tree->groups; ++i) { 45 | int group_size = l.softmax_tree->group_size[i]; 46 | softmax_cpu(net.input + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output + count); 47 | count += group_size; 48 | } 49 | } else { 50 | softmax_cpu(net.input, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output); 51 | } 52 | 53 | if(net.truth){ 54 | softmax_x_ent_cpu(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss); 55 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 56 | } 57 | } 58 | 59 | void backward_softmax_layer(const softmax_layer l, network net) 60 | { 61 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 62 | } 63 | 64 | #ifdef GPU 65 | 66 | void pull_softmax_layer_output(const softmax_layer layer) 67 | { 68 | cuda_pull_array(layer.output_gpu, layer.output, layer.inputs*layer.batch); 69 | } 70 | 71 | void forward_softmax_layer_gpu(const softmax_layer l, network net) 72 | { 73 | if(l.softmax_tree){ 74 | softmax_tree(net.input_gpu, 1, l.batch, l.inputs, l.temperature, l.output_gpu, *l.softmax_tree); 75 | /* 76 | int i; 77 | int count = 0; 78 | for (i = 0; i < l.softmax_tree->groups; ++i) { 79 | int group_size = l.softmax_tree->group_size[i]; 80 | softmax_gpu(net.input_gpu + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output_gpu + count); 81 | count += group_size; 82 | } 83 | */ 84 | } else { 85 | if(l.spatial){ 86 | softmax_gpu(net.input_gpu, l.c, l.batch*l.c, l.inputs/l.c, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu); 87 | }else{ 88 | softmax_gpu(net.input_gpu, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output_gpu); 89 | } 90 | } 91 | if(net.truth){ 92 | softmax_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu); 93 | if(l.softmax_tree){ 94 | mask_gpu(l.batch*l.inputs, l.delta_gpu, SECRET_NUM, net.truth_gpu, 0); 95 | mask_gpu(l.batch*l.inputs, l.loss_gpu, SECRET_NUM, net.truth_gpu, 0); 96 | } 97 | cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs); 98 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 99 | } 100 | } 101 | 102 | void backward_softmax_layer_gpu(const softmax_layer layer, network net) 103 | { 104 | axpy_gpu(layer.batch*layer.inputs, 1, layer.delta_gpu, 1, net.delta_gpu, 1); 105 | } 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /Inference/src/softmax_layer.c: -------------------------------------------------------------------------------- 1 | #include "softmax_layer.h" 2 | #include "blas.h" 3 | #include "cuda.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | softmax_layer make_softmax_layer(int batch, int inputs, int groups) 12 | { 13 | assert(inputs%groups == 0); 14 | fprintf(stderr, "softmax %4d\n", inputs); 15 | softmax_layer l = {0}; 16 | l.type = SOFTMAX; 17 | l.batch = batch; 18 | l.groups = groups; 19 | l.inputs = inputs; 20 | l.outputs = inputs; 21 | l.loss = calloc(inputs*batch, sizeof(float)); 22 | l.output = calloc(inputs*batch, sizeof(float)); 23 | l.delta = calloc(inputs*batch, sizeof(float)); 24 | l.cost = calloc(1, sizeof(float)); 25 | 26 | l.forward = forward_softmax_layer; 27 | l.backward = backward_softmax_layer; 28 | #ifdef GPU 29 | l.forward_gpu = forward_softmax_layer_gpu; 30 | l.backward_gpu = backward_softmax_layer_gpu; 31 | 32 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 33 | l.loss_gpu = cuda_make_array(l.loss, inputs*batch); 34 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 35 | #endif 36 | return l; 37 | } 38 | 39 | void forward_softmax_layer(const softmax_layer l, network net) 40 | { 41 | if(l.softmax_tree){ 42 | int i; 43 | int count = 0; 44 | for (i = 0; i < l.softmax_tree->groups; ++i) { 45 | int group_size = l.softmax_tree->group_size[i]; 46 | softmax_cpu(net.input + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output + count); 47 | count += group_size; 48 | } 49 | } else { 50 | softmax_cpu(net.input, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output); 51 | } 52 | 53 | if(net.truth){ 54 | softmax_x_ent_cpu(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss); 55 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 56 | } 57 | } 58 | 59 | void backward_softmax_layer(const softmax_layer l, network net) 60 | { 61 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 62 | } 63 | 64 | #ifdef GPU 65 | 66 | void pull_softmax_layer_output(const softmax_layer layer) 67 | { 68 | cuda_pull_array(layer.output_gpu, layer.output, layer.inputs*layer.batch); 69 | } 70 | 71 | void forward_softmax_layer_gpu(const softmax_layer l, network net) 72 | { 73 | if(l.softmax_tree){ 74 | softmax_tree(net.input_gpu, 1, l.batch, l.inputs, l.temperature, l.output_gpu, *l.softmax_tree); 75 | /* 76 | int i; 77 | int count = 0; 78 | for (i = 0; i < l.softmax_tree->groups; ++i) { 79 | int group_size = l.softmax_tree->group_size[i]; 80 | softmax_gpu(net.input_gpu + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output_gpu + count); 81 | count += group_size; 82 | } 83 | */ 84 | } else { 85 | if(l.spatial){ 86 | softmax_gpu(net.input_gpu, l.c, l.batch*l.c, l.inputs/l.c, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu); 87 | }else{ 88 | softmax_gpu(net.input_gpu, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output_gpu); 89 | } 90 | } 91 | if(net.truth){ 92 | softmax_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu); 93 | if(l.softmax_tree){ 94 | mask_gpu(l.batch*l.inputs, l.delta_gpu, SECRET_NUM, net.truth_gpu, 0); 95 | mask_gpu(l.batch*l.inputs, l.loss_gpu, SECRET_NUM, net.truth_gpu, 0); 96 | } 97 | cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs); 98 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 99 | } 100 | } 101 | 102 | void backward_softmax_layer_gpu(const softmax_layer layer, network net) 103 | { 104 | axpy_gpu(layer.batch*layer.inputs, 1, layer.delta_gpu, 1, net.delta_gpu, 1); 105 | } 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /Train/data/script/generate_dataset.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding: utf-8 3 | # python 2.7.12 4 | 5 | from __future__ import print_function 6 | import os 7 | import glob 8 | import shutil 9 | import xml.dom.minidom 10 | 11 | 12 | def generate_cls(src_path, des_path, cls, idx): 13 | # get name list of all frames of class cls 14 | # the order of images does not matter, so we won't do sorting here 15 | frame_list = [ 16 | frame.split(os.path.sep)[-1].split('.')[0] 17 | for frame in glob.glob(src_path + '/*.xml') 18 | ] 19 | 20 | # this file contains all frame and the corresponding label in class cls 21 | label_all_file = 'label_all.txt' 22 | with open(os.path.join(des_path, label_all_file), 'w') as f: pass 23 | 24 | # create a new label file for each frame 25 | cnt = 0 26 | for frame in frame_list: 27 | print('processing frame %06d of class "%s" ...' % (cnt, cls)) 28 | # get source data 29 | image_src = os.path.join(src_path, frame + '.jpg') 30 | label_src = os.path.join(src_path, frame + '.xml') 31 | # parse xml file to get information of label 32 | tr = xml.dom.minidom.parse(label_src) 33 | width = float(tr.getElementsByTagName('width')[0].firstChild.data) 34 | height = float(tr.getElementsByTagName('height')[0].firstChild.data) 35 | xmin = tr.getElementsByTagName('xmin') 36 | if xmin: 37 | xmin = float(xmin[0].firstChild.data) 38 | else: 39 | continue 40 | ymin = tr.getElementsByTagName('ymin') 41 | if ymin: 42 | ymin = float(ymin[0].firstChild.data) 43 | else: 44 | continue 45 | xmax = tr.getElementsByTagName('xmax') 46 | if xmax: 47 | xmax = float(xmax[0].firstChild.data) 48 | else: 49 | continue 50 | ymax = tr.getElementsByTagName('ymax') 51 | if ymax: 52 | ymax = float(ymax[0].firstChild.data) 53 | else: 54 | continue 55 | # check 56 | assert xmin <= xmax 57 | assert ymin <= ymax 58 | 59 | # 1. save the txt file for training, which is formatted as follow: 60 | # cx cy w h 61 | # Note: cx, w are relative to width, cy, h are relative to height 62 | cx = (xmin + xmax) / 2.0 / width 63 | cy = (ymin + ymax) / 2.0 / height 64 | w = (xmax - xmin) / width 65 | h = (ymax - ymin) / height 66 | frame_label_file = '%s_%06d.txt' % (cls, cnt) # re-ordered 67 | with open(os.path.join(des_path, frame_label_file), 'w') as f: 68 | f.write('%d %f %f %f %f\n' % (idx, cx, cy, w, h)) 69 | 70 | # 2. save the txt file for validation, which is formatted as follow: 71 | # left top right bottom 72 | frame_name = '%s_%06d' % (cls, cnt) 73 | with open(os.path.join(des_path, label_all_file), 'a') as f: 74 | f.write('%s %d %d %d %d\n' % 75 | (frame_name, int(xmin), int(ymin), int(xmax), int(ymax))) 76 | 77 | # 3. copy image 78 | image_des = os.path.join(des_path, '%s_%06d.jpg' % (cls, cnt)) 79 | shutil.copyfile(image_src, image_des) 80 | 81 | # counter +1 82 | cnt += 1 83 | 84 | 85 | if __name__ == '__main__': 86 | TRAIN_DATA_ROOT = os.path.abspath(os.path.pardir) 87 | src_dir = os.path.join(TRAIN_DATA_ROOT, 'raw_dataset') 88 | des_dir = os.path.join(TRAIN_DATA_ROOT, 'train_dataset') 89 | if not os.path.exists(des_dir): 90 | os.mkdir(des_dir) 91 | # retrieve all classes (98 classes totally) 92 | cls_lst = sorted(os.listdir(src_dir)) 93 | #assert 98 == len(cls_lst) 94 | print(len(cls_lst)) 95 | # for each class 96 | for idx, cls in enumerate(cls_lst): 97 | # set paths 98 | src_path = os.path.join(src_dir, cls) 99 | des_path = os.path.join(des_dir, cls) 100 | if not os.path.exists(os.path.join(des_dir, cls)): 101 | os.mkdir(des_path) 102 | # generate 103 | generate_cls(src_path, des_path, cls, idx) -------------------------------------------------------------------------------- /Train/example/dice.c: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | 3 | char *dice_labels[] = {"face1","face2","face3","face4","face5","face6"}; 4 | 5 | void train_dice(char *cfgfile, char *weightfile) 6 | { 7 | srand(time(0)); 8 | float avg_loss = -1; 9 | char *base = basecfg(cfgfile); 10 | char *backup_directory = "/home/pjreddie/backup/"; 11 | printf("%s\n", base); 12 | network net = parse_network_cfg(cfgfile); 13 | if(weightfile){ 14 | load_weights(&net, weightfile); 15 | } 16 | printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); 17 | int imgs = 1024; 18 | int i = *net.seen/imgs; 19 | char **labels = dice_labels; 20 | list *plist = get_paths("data/dice/dice.train.list"); 21 | char **paths = (char **)list_to_array(plist); 22 | printf("%d\n", plist->size); 23 | clock_t time; 24 | while(1){ 25 | ++i; 26 | time=clock(); 27 | data train = load_data_old(paths, imgs, plist->size, labels, 6, net.w, net.h); 28 | printf("Loaded: %lf seconds\n", sec(clock()-time)); 29 | 30 | time=clock(); 31 | float loss = train_network(net, train); 32 | if(avg_loss == -1) avg_loss = loss; 33 | avg_loss = avg_loss*.9 + loss*.1; 34 | printf("%d: %f, %f avg, %lf seconds, %ld images\n", i, loss, avg_loss, sec(clock()-time), *net.seen); 35 | free_data(train); 36 | if((i % 100) == 0) net.learning_rate *= .1; 37 | if(i%100==0){ 38 | char buff[256]; 39 | sprintf(buff, "%s/%s_%d.weights",backup_directory,base, i); 40 | save_weights(net, buff); 41 | } 42 | } 43 | } 44 | 45 | void validate_dice(char *filename, char *weightfile) 46 | { 47 | network net = parse_network_cfg(filename); 48 | if(weightfile){ 49 | load_weights(&net, weightfile); 50 | } 51 | srand(time(0)); 52 | 53 | char **labels = dice_labels; 54 | list *plist = get_paths("data/dice/dice.val.list"); 55 | 56 | char **paths = (char **)list_to_array(plist); 57 | int m = plist->size; 58 | free_list(plist); 59 | 60 | data val = load_data_old(paths, m, 0, labels, 6, net.w, net.h); 61 | float *acc = network_accuracies(net, val, 2); 62 | printf("Validation Accuracy: %f, %d images\n", acc[0], m); 63 | free_data(val); 64 | } 65 | 66 | void test_dice(char *cfgfile, char *weightfile, char *filename) 67 | { 68 | network net = parse_network_cfg(cfgfile); 69 | if(weightfile){ 70 | load_weights(&net, weightfile); 71 | } 72 | set_batch_network(&net, 1); 73 | srand(2222222); 74 | int i = 0; 75 | char **names = dice_labels; 76 | char buff[256]; 77 | char *input = buff; 78 | int indexes[6]; 79 | while(1){ 80 | if(filename){ 81 | strncpy(input, filename, 256); 82 | }else{ 83 | printf("Enter Image Path: "); 84 | fflush(stdout); 85 | input = fgets(input, 256, stdin); 86 | if(!input) return; 87 | strtok(input, "\n"); 88 | } 89 | image im = load_image_color(input, net.w, net.h); 90 | float *X = im.data; 91 | float *predictions = network_predict(net, X); 92 | top_predictions(net, 6, indexes); 93 | for(i = 0; i < 6; ++i){ 94 | int index = indexes[i]; 95 | printf("%s: %f\n", names[index], predictions[index]); 96 | } 97 | free_image(im); 98 | if (filename) break; 99 | } 100 | } 101 | 102 | void run_dice(int argc, char **argv) 103 | { 104 | if(argc < 4){ 105 | fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); 106 | return; 107 | } 108 | 109 | char *cfg = argv[3]; 110 | char *weights = (argc > 4) ? argv[4] : 0; 111 | char *filename = (argc > 5) ? argv[5]: 0; 112 | if(0==strcmp(argv[2], "test")) test_dice(cfg, weights, filename); 113 | else if(0==strcmp(argv[2], "train")) train_dice(cfg, weights); 114 | else if(0==strcmp(argv[2], "valid")) validate_dice(cfg, weights); 115 | } 116 | 117 | -------------------------------------------------------------------------------- /Train/example/super.c: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | 3 | void train_super(char *cfgfile, char *weightfile, int clear) 4 | { 5 | char *train_images = "/data/imagenet/imagenet1k.train.list"; 6 | char *backup_directory = "/home/pjreddie/backup/"; 7 | srand(time(0)); 8 | char *base = basecfg(cfgfile); 9 | printf("%s\n", base); 10 | float avg_loss = -1; 11 | network *net = load_network(cfgfile, weightfile, clear); 12 | printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay); 13 | int imgs = net->batch*net->subdivisions; 14 | int i = *net->seen/imgs; 15 | data train, buffer; 16 | 17 | 18 | list *plist = get_paths(train_images); 19 | //int N = plist->size; 20 | char **paths = (char **)list_to_array(plist); 21 | 22 | load_args args = {0}; 23 | args.w = net->w; 24 | args.h = net->h; 25 | args.scale = 4; 26 | args.paths = paths; 27 | args.n = imgs; 28 | args.m = plist->size; 29 | args.d = &buffer; 30 | args.type = SUPER_DATA; 31 | 32 | pthread_t load_thread = load_data_in_thread(args); 33 | clock_t time; 34 | //while(i*imgs < N*120){ 35 | while(get_current_batch(net) < net->max_batches){ 36 | i += 1; 37 | time=clock(); 38 | pthread_join(load_thread, 0); 39 | train = buffer; 40 | load_thread = load_data_in_thread(args); 41 | 42 | printf("Loaded: %lf seconds\n", sec(clock()-time)); 43 | 44 | time=clock(); 45 | float loss = train_network(net, train); 46 | if (avg_loss < 0) avg_loss = loss; 47 | avg_loss = avg_loss*.9 + loss*.1; 48 | 49 | printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); 50 | if(i%1000==0){ 51 | char buff[256]; 52 | sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); 53 | save_weights(net, buff); 54 | } 55 | if(i%100==0){ 56 | char buff[256]; 57 | sprintf(buff, "%s/%s.backup", backup_directory, base); 58 | save_weights(net, buff); 59 | } 60 | free_data(train); 61 | } 62 | char buff[256]; 63 | sprintf(buff, "%s/%s_final.weights", backup_directory, base); 64 | save_weights(net, buff); 65 | } 66 | 67 | void test_super(char *cfgfile, char *weightfile, char *filename) 68 | { 69 | network *net = load_network(cfgfile, weightfile, 0); 70 | set_batch_network(net, 1); 71 | srand(2222222); 72 | 73 | clock_t time; 74 | char buff[256]; 75 | char *input = buff; 76 | while(1){ 77 | if(filename){ 78 | strncpy(input, filename, 256); 79 | }else{ 80 | printf("Enter Image Path: "); 81 | fflush(stdout); 82 | input = fgets(input, 256, stdin); 83 | if(!input) return; 84 | strtok(input, "\n"); 85 | } 86 | image im = load_image_color(input, 0, 0); 87 | resize_network(net, im.w, im.h); 88 | printf("%d %d\n", im.w, im.h); 89 | 90 | float *X = im.data; 91 | time=clock(); 92 | network_predict(net, X); 93 | image out = get_network_image(net); 94 | printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); 95 | save_image(out, "out"); 96 | show_image(out, "out"); 97 | 98 | free_image(im); 99 | if (filename) break; 100 | } 101 | } 102 | 103 | 104 | void run_super(int argc, char **argv) 105 | { 106 | if(argc < 4){ 107 | fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); 108 | return; 109 | } 110 | 111 | char *cfg = argv[3]; 112 | char *weights = (argc > 4) ? argv[4] : 0; 113 | char *filename = (argc > 5) ? argv[5] : 0; 114 | int clear = find_arg(argc, argv, "-clear"); 115 | if(0==strcmp(argv[2], "train")) train_super(cfg, weights, clear); 116 | else if(0==strcmp(argv[2], "test")) test_super(cfg, weights, filename); 117 | /* 118 | else if(0==strcmp(argv[2], "valid")) validate_super(cfg, weights); 119 | */ 120 | } 121 | -------------------------------------------------------------------------------- /Train/src/activations.c: -------------------------------------------------------------------------------- 1 | #include "activations.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | char *get_activation_string(ACTIVATION a) 9 | { 10 | switch(a){ 11 | case LOGISTIC: 12 | return "logistic"; 13 | case LOGGY: 14 | return "loggy"; 15 | case RELU: 16 | return "relu"; 17 | case ELU: 18 | return "elu"; 19 | case RELIE: 20 | return "relie"; 21 | case RAMP: 22 | return "ramp"; 23 | case LINEAR: 24 | return "linear"; 25 | case TANH: 26 | return "tanh"; 27 | case PLSE: 28 | return "plse"; 29 | case LEAKY: 30 | return "leaky"; 31 | case STAIR: 32 | return "stair"; 33 | case HARDTAN: 34 | return "hardtan"; 35 | case LHTAN: 36 | return "lhtan"; 37 | default: 38 | break; 39 | } 40 | return "relu"; 41 | } 42 | 43 | ACTIVATION get_activation(char *s) 44 | { 45 | if (strcmp(s, "logistic")==0) return LOGISTIC; 46 | if (strcmp(s, "loggy")==0) return LOGGY; 47 | if (strcmp(s, "relu")==0) return RELU; 48 | if (strcmp(s, "elu")==0) return ELU; 49 | if (strcmp(s, "relie")==0) return RELIE; 50 | if (strcmp(s, "plse")==0) return PLSE; 51 | if (strcmp(s, "hardtan")==0) return HARDTAN; 52 | if (strcmp(s, "lhtan")==0) return LHTAN; 53 | if (strcmp(s, "linear")==0) return LINEAR; 54 | if (strcmp(s, "ramp")==0) return RAMP; 55 | if (strcmp(s, "leaky")==0) return LEAKY; 56 | if (strcmp(s, "tanh")==0) return TANH; 57 | if (strcmp(s, "stair")==0) return STAIR; 58 | fprintf(stderr, "Couldn't find activation function %s, going with ReLU\n", s); 59 | return RELU; 60 | } 61 | 62 | float activate(float x, ACTIVATION a) 63 | { 64 | switch(a){ 65 | case LINEAR: 66 | return linear_activate(x); 67 | case LOGISTIC: 68 | return logistic_activate(x); 69 | case LOGGY: 70 | return loggy_activate(x); 71 | case RELU: 72 | return relu_activate(x); 73 | case ELU: 74 | return elu_activate(x); 75 | case RELIE: 76 | return relie_activate(x); 77 | case RAMP: 78 | return ramp_activate(x); 79 | case LEAKY: 80 | return leaky_activate(x); 81 | case TANH: 82 | return tanh_activate(x); 83 | case PLSE: 84 | return plse_activate(x); 85 | case STAIR: 86 | return stair_activate(x); 87 | case HARDTAN: 88 | return hardtan_activate(x); 89 | case LHTAN: 90 | return lhtan_activate(x); 91 | } 92 | return 0; 93 | } 94 | 95 | void activate_array(float *x, const int n, const ACTIVATION a) 96 | { 97 | int i; 98 | for(i = 0; i < n; ++i){ 99 | x[i] = activate(x[i], a); 100 | } 101 | } 102 | 103 | float gradient(float x, ACTIVATION a) 104 | { 105 | switch(a){ 106 | case LINEAR: 107 | return linear_gradient(x); 108 | case LOGISTIC: 109 | return logistic_gradient(x); 110 | case LOGGY: 111 | return loggy_gradient(x); 112 | case RELU: 113 | return relu_gradient(x); 114 | case ELU: 115 | return elu_gradient(x); 116 | case RELIE: 117 | return relie_gradient(x); 118 | case RAMP: 119 | return ramp_gradient(x); 120 | case LEAKY: 121 | return leaky_gradient(x); 122 | case TANH: 123 | return tanh_gradient(x); 124 | case PLSE: 125 | return plse_gradient(x); 126 | case STAIR: 127 | return stair_gradient(x); 128 | case HARDTAN: 129 | return hardtan_gradient(x); 130 | case LHTAN: 131 | return lhtan_gradient(x); 132 | } 133 | return 0; 134 | } 135 | 136 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta) 137 | { 138 | int i; 139 | for(i = 0; i < n; ++i){ 140 | delta[i] *= gradient(x[i], a); 141 | } 142 | } 143 | 144 | -------------------------------------------------------------------------------- /Inference/src/activations.c: -------------------------------------------------------------------------------- 1 | #include "activations.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | char *get_activation_string(ACTIVATION a) 9 | { 10 | switch(a){ 11 | case LOGISTIC: 12 | return "logistic"; 13 | case LOGGY: 14 | return "loggy"; 15 | case RELU: 16 | return "relu"; 17 | case ELU: 18 | return "elu"; 19 | case RELIE: 20 | return "relie"; 21 | case RAMP: 22 | return "ramp"; 23 | case LINEAR: 24 | return "linear"; 25 | case TANH: 26 | return "tanh"; 27 | case PLSE: 28 | return "plse"; 29 | case LEAKY: 30 | return "leaky"; 31 | case STAIR: 32 | return "stair"; 33 | case HARDTAN: 34 | return "hardtan"; 35 | case LHTAN: 36 | return "lhtan"; 37 | default: 38 | break; 39 | } 40 | return "relu"; 41 | } 42 | 43 | ACTIVATION get_activation(char *s) 44 | { 45 | if (strcmp(s, "logistic")==0) return LOGISTIC; 46 | if (strcmp(s, "loggy")==0) return LOGGY; 47 | if (strcmp(s, "relu")==0) return RELU; 48 | if (strcmp(s, "elu")==0) return ELU; 49 | if (strcmp(s, "relie")==0) return RELIE; 50 | if (strcmp(s, "plse")==0) return PLSE; 51 | if (strcmp(s, "hardtan")==0) return HARDTAN; 52 | if (strcmp(s, "lhtan")==0) return LHTAN; 53 | if (strcmp(s, "linear")==0) return LINEAR; 54 | if (strcmp(s, "ramp")==0) return RAMP; 55 | if (strcmp(s, "leaky")==0) return LEAKY; 56 | if (strcmp(s, "tanh")==0) return TANH; 57 | if (strcmp(s, "stair")==0) return STAIR; 58 | fprintf(stderr, "Couldn't find activation function %s, going with ReLU\n", s); 59 | return RELU; 60 | } 61 | 62 | float activate(float x, ACTIVATION a) 63 | { 64 | switch(a){ 65 | case LINEAR: 66 | return linear_activate(x); 67 | case LOGISTIC: 68 | return logistic_activate(x); 69 | case LOGGY: 70 | return loggy_activate(x); 71 | case RELU: 72 | return relu_activate(x); 73 | case ELU: 74 | return elu_activate(x); 75 | case RELIE: 76 | return relie_activate(x); 77 | case RAMP: 78 | return ramp_activate(x); 79 | case LEAKY: 80 | return leaky_activate(x); 81 | case TANH: 82 | return tanh_activate(x); 83 | case PLSE: 84 | return plse_activate(x); 85 | case STAIR: 86 | return stair_activate(x); 87 | case HARDTAN: 88 | return hardtan_activate(x); 89 | case LHTAN: 90 | return lhtan_activate(x); 91 | } 92 | return 0; 93 | } 94 | 95 | void activate_array(float *x, const int n, const ACTIVATION a) 96 | { 97 | int i; 98 | for(i = 0; i < n; ++i){ 99 | x[i] = activate(x[i], a); 100 | } 101 | } 102 | 103 | float gradient(float x, ACTIVATION a) 104 | { 105 | switch(a){ 106 | case LINEAR: 107 | return linear_gradient(x); 108 | case LOGISTIC: 109 | return logistic_gradient(x); 110 | case LOGGY: 111 | return loggy_gradient(x); 112 | case RELU: 113 | return relu_gradient(x); 114 | case ELU: 115 | return elu_gradient(x); 116 | case RELIE: 117 | return relie_gradient(x); 118 | case RAMP: 119 | return ramp_gradient(x); 120 | case LEAKY: 121 | return leaky_gradient(x); 122 | case TANH: 123 | return tanh_gradient(x); 124 | case PLSE: 125 | return plse_gradient(x); 126 | case STAIR: 127 | return stair_gradient(x); 128 | case HARDTAN: 129 | return hardtan_gradient(x); 130 | case LHTAN: 131 | return lhtan_gradient(x); 132 | } 133 | return 0; 134 | } 135 | 136 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta) 137 | { 138 | int i; 139 | for(i = 0; i < n; ++i){ 140 | delta[i] *= gradient(x[i], a); 141 | } 142 | } 143 | 144 | --------------------------------------------------------------------------------