darknet/include/darknet.h

787 lines
18 KiB
C
Raw Normal View History

2017-06-02 06:31:13 +03:00
#ifndef DARKNET_API
#define DARKNET_API
#include <stdlib.h>
2017-06-08 23:47:31 +03:00
#include <stdio.h>
#include <string.h>
#include <pthread.h>
2017-06-02 06:31:13 +03:00
2017-06-08 23:47:31 +03:00
#define SECRET_NUM -1234
2017-06-02 06:31:13 +03:00
extern int gpu_index;
#ifdef GPU
#define BLOCK 512
#include "cuda_runtime.h"
#include "curand.h"
#include "cublas_v2.h"
#ifdef CUDNN
#include "cudnn.h"
#endif
#endif
#ifndef __cplusplus
#ifdef OPENCV
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/version.hpp"
#if CV_MAJOR_VERSION == 3
#include "opencv2/videoio/videoio_c.h"
2017-09-20 01:02:39 +03:00
#include "opencv2/imgcodecs/imgcodecs_c.h"
2017-06-02 06:31:13 +03:00
#endif
#endif
#endif
2017-06-08 23:47:31 +03:00
typedef struct{
int classes;
char **names;
} metadata;
metadata get_metadata(char *file);
2017-06-02 06:31:13 +03:00
typedef struct{
int *leaf;
int n;
int *parent;
int *child;
int *group;
char **name;
int groups;
int *group_size;
int *group_offset;
} tree;
typedef enum{
LOGISTIC, RELU, RELIE, LINEAR, RAMP, TANH, PLSE, LEAKY, ELU, LOGGY, STAIR, HARDTAN, LHTAN
2017-06-08 23:47:31 +03:00
} ACTIVATION;
2017-06-02 06:31:13 +03:00
typedef enum{
MULT, ADD, SUB, DIV
} BINARY_ACTIVATION;
2017-06-02 06:31:13 +03:00
typedef enum {
CONVOLUTIONAL,
DECONVOLUTIONAL,
CONNECTED,
MAXPOOL,
SOFTMAX,
DETECTION,
DROPOUT,
CROP,
ROUTE,
COST,
NORMALIZATION,
AVGPOOL,
LOCAL,
SHORTCUT,
ACTIVE,
RNN,
GRU,
2017-06-07 03:16:13 +03:00
LSTM,
2017-06-02 06:31:13 +03:00
CRNN,
BATCHNORM,
NETWORK,
XNOR,
REGION,
REORG,
UPSAMPLE,
LOGXENT,
2017-06-02 06:31:13 +03:00
BLANK
} LAYER_TYPE;
typedef enum{
2017-06-18 23:05:37 +03:00
SSE, MASKED, L1, SEG, SMOOTH
2017-06-02 06:31:13 +03:00
} COST_TYPE;
2017-06-13 02:19:08 +03:00
typedef struct{
int batch;
float learning_rate;
float momentum;
float decay;
int adam;
float B1;
float B2;
float eps;
int t;
} update_args;
2017-06-02 06:31:13 +03:00
struct network;
typedef struct network network;
struct layer;
typedef struct layer layer;
struct layer{
LAYER_TYPE type;
ACTIVATION activation;
COST_TYPE cost_type;
void (*forward) (struct layer, struct network);
void (*backward) (struct layer, struct network);
2017-06-13 02:19:08 +03:00
void (*update) (struct layer, update_args);
2017-06-02 06:31:13 +03:00
void (*forward_gpu) (struct layer, struct network);
void (*backward_gpu) (struct layer, struct network);
2017-06-13 02:19:08 +03:00
void (*update_gpu) (struct layer, update_args);
2017-06-02 06:31:13 +03:00
int batch_normalize;
int shortcut;
int batch;
int forced;
int flipped;
int inputs;
int outputs;
int nweights;
int nbiases;
int extra;
int truths;
int h,w,c;
int out_h, out_w, out_c;
int n;
int max_boxes;
int groups;
int size;
int side;
int stride;
int reverse;
int flatten;
int spatial;
int pad;
int sqrt;
int flip;
int index;
int binary;
int xnor;
int steps;
int hidden;
int truth;
float smooth;
float dot;
float angle;
float jitter;
float saturation;
float exposure;
float shift;
float ratio;
float learning_rate_scale;
int softmax;
int classes;
int coords;
int background;
int rescore;
int objectness;
int joint;
int noadjust;
int reorg;
int log;
2017-06-10 02:41:00 +03:00
int tanh;
int *mask;
int total;
2017-06-02 06:31:13 +03:00
float alpha;
float beta;
float kappa;
float coord_scale;
float object_scale;
float noobject_scale;
2017-07-12 02:44:09 +03:00
float mask_scale;
2017-06-02 06:31:13 +03:00
float class_scale;
int bias_match;
int random;
float thresh;
int classfix;
int absolute;
int onlyforward;
int stopbackward;
int dontload;
int dontloadscales;
float temperature;
float probability;
float scale;
char * cweights;
int * indexes;
int * input_layers;
int * input_sizes;
int * map;
float * rand;
float * cost;
float * state;
float * prev_state;
float * forgot_state;
float * forgot_delta;
float * state_delta;
2017-06-18 23:05:37 +03:00
float * combine_cpu;
float * combine_delta_cpu;
2017-06-02 06:31:13 +03:00
float * concat;
float * concat_delta;
float * binary_weights;
float * biases;
float * bias_updates;
float * scales;
float * scale_updates;
float * weights;
float * weight_updates;
float * delta;
float * output;
float * loss;
2017-06-02 06:31:13 +03:00
float * squared;
float * norms;
float * spatial_mean;
float * mean;
float * variance;
float * mean_delta;
float * variance_delta;
float * rolling_mean;
float * rolling_variance;
float * x;
float * x_norm;
float * m;
float * v;
float * bias_m;
float * bias_v;
float * scale_m;
float * scale_v;
2017-06-10 02:41:00 +03:00
float *z_cpu;
float *r_cpu;
float *h_cpu;
float * prev_state_cpu;
float *temp_cpu;
float *temp2_cpu;
float *temp3_cpu;
float *dh_cpu;
float *hh_cpu;
float *prev_cell_cpu;
float *cell_cpu;
float *f_cpu;
float *i_cpu;
float *g_cpu;
float *o_cpu;
float *c_cpu;
float *dc_cpu;
2017-06-02 06:31:13 +03:00
float * binary_input;
struct layer *input_layer;
struct layer *self_layer;
struct layer *output_layer;
2017-06-18 23:05:37 +03:00
struct layer *reset_layer;
struct layer *update_layer;
struct layer *state_layer;
2017-06-02 06:31:13 +03:00
struct layer *input_gate_layer;
struct layer *state_gate_layer;
struct layer *input_save_layer;
struct layer *state_save_layer;
struct layer *input_state_layer;
struct layer *state_state_layer;
struct layer *input_z_layer;
struct layer *state_z_layer;
struct layer *input_r_layer;
struct layer *state_r_layer;
struct layer *input_h_layer;
struct layer *state_h_layer;
2017-06-07 02:50:19 +03:00
2017-06-07 03:16:13 +03:00
struct layer *wz;
struct layer *uz;
struct layer *wr;
struct layer *ur;
struct layer *wh;
struct layer *uh;
struct layer *uo;
struct layer *wo;
struct layer *uf;
struct layer *wf;
struct layer *ui;
struct layer *wi;
struct layer *ug;
struct layer *wg;
2017-06-02 06:31:13 +03:00
tree *softmax_tree;
size_t workspace_size;
2017-06-08 23:47:31 +03:00
#ifdef GPU
2017-06-02 06:31:13 +03:00
int *indexes_gpu;
float *z_gpu;
float *r_gpu;
float *h_gpu;
2017-06-07 03:16:13 +03:00
float *temp_gpu;
float *temp2_gpu;
float *temp3_gpu;
float *dh_gpu;
float *hh_gpu;
float *prev_cell_gpu;
float *cell_gpu;
float *f_gpu;
float *i_gpu;
float *g_gpu;
float *o_gpu;
float *c_gpu;
float *dc_gpu;
2017-06-07 02:50:19 +03:00
2017-06-02 06:31:13 +03:00
float *m_gpu;
float *v_gpu;
float *bias_m_gpu;
float *scale_m_gpu;
float *bias_v_gpu;
float *scale_v_gpu;
2017-06-18 23:05:37 +03:00
float * combine_gpu;
float * combine_delta_gpu;
2017-06-02 06:31:13 +03:00
float * prev_state_gpu;
float * forgot_state_gpu;
float * forgot_delta_gpu;
float * state_gpu;
float * state_delta_gpu;
float * gate_gpu;
float * gate_delta_gpu;
float * save_gpu;
float * save_delta_gpu;
float * concat_gpu;
float * concat_delta_gpu;
2017-06-08 23:47:31 +03:00
float * binary_input_gpu;
float * binary_weights_gpu;
2017-06-02 06:31:13 +03:00
float * mean_gpu;
float * variance_gpu;
float * rolling_mean_gpu;
float * rolling_variance_gpu;
float * variance_delta_gpu;
float * mean_delta_gpu;
float * x_gpu;
float * x_norm_gpu;
float * weights_gpu;
float * weight_updates_gpu;
2017-06-08 23:47:31 +03:00
float * weight_change_gpu;
2017-06-02 06:31:13 +03:00
float * biases_gpu;
float * bias_updates_gpu;
2017-06-08 23:47:31 +03:00
float * bias_change_gpu;
2017-06-02 06:31:13 +03:00
float * scales_gpu;
float * scale_updates_gpu;
2017-06-08 23:47:31 +03:00
float * scale_change_gpu;
2017-06-02 06:31:13 +03:00
float * output_gpu;
float * loss_gpu;
2017-06-02 06:31:13 +03:00
float * delta_gpu;
float * rand_gpu;
float * squared_gpu;
float * norms_gpu;
2017-06-08 23:47:31 +03:00
#ifdef CUDNN
2017-06-02 06:31:13 +03:00
cudnnTensorDescriptor_t srcTensorDesc, dstTensorDesc;
cudnnTensorDescriptor_t dsrcTensorDesc, ddstTensorDesc;
cudnnTensorDescriptor_t normTensorDesc;
cudnnFilterDescriptor_t weightDesc;
cudnnFilterDescriptor_t dweightDesc;
cudnnConvolutionDescriptor_t convDesc;
cudnnConvolutionFwdAlgo_t fw_algo;
cudnnConvolutionBwdDataAlgo_t bd_algo;
cudnnConvolutionBwdFilterAlgo_t bf_algo;
2017-06-08 23:47:31 +03:00
#endif
#endif
2017-06-02 06:31:13 +03:00
};
void free_layer(layer);
typedef enum {
CONSTANT, STEP, EXP, POLY, STEPS, SIG, RANDOM
} learning_rate_policy;
typedef struct network{
int n;
int batch;
2017-06-13 02:19:08 +03:00
size_t *seen;
int *t;
2017-06-02 06:31:13 +03:00
float epoch;
int subdivisions;
layer *layers;
float *output;
learning_rate_policy policy;
float learning_rate;
2017-06-13 02:19:08 +03:00
float momentum;
float decay;
2017-06-02 06:31:13 +03:00
float gamma;
float scale;
float power;
int time_steps;
int step;
int max_batches;
float *scales;
int *steps;
int num_steps;
int burn_in;
int adam;
float B1;
float B2;
float eps;
int inputs;
int outputs;
int truths;
int notruth;
int h, w, c;
int max_crop;
int min_crop;
float max_ratio;
float min_ratio;
2017-06-02 06:31:13 +03:00
int center;
float angle;
float aspect;
float exposure;
float saturation;
float hue;
int random;
2017-06-02 06:31:13 +03:00
int gpu_index;
tree *hierarchy;
float *input;
float *truth;
float *delta;
float *workspace;
int train;
int index;
float *cost;
2017-06-08 23:47:31 +03:00
#ifdef GPU
2017-06-02 06:31:13 +03:00
float *input_gpu;
float *truth_gpu;
float *delta_gpu;
float *output_gpu;
2017-06-08 23:47:31 +03:00
#endif
2017-06-02 06:31:13 +03:00
} network;
typedef struct {
int w;
int h;
float scale;
float rad;
float dx;
float dy;
float aspect;
} augment_args;
typedef struct {
int w;
2017-06-08 23:47:31 +03:00
int h;
2017-06-02 06:31:13 +03:00
int c;
float *data;
} image;
typedef struct{
float x, y, w, h;
} box;
typedef struct detection{
box bbox;
int classes;
float *prob;
float *mask;
float objectness;
int sort_class;
} detection;
2017-06-02 06:31:13 +03:00
typedef struct matrix{
int rows, cols;
float **vals;
} matrix;
typedef struct{
int w, h;
matrix X;
matrix y;
int shallow;
int *num_boxes;
box **boxes;
} data;
typedef enum {
2017-07-12 02:44:09 +03:00
CLASSIFICATION_DATA, DETECTION_DATA, CAPTCHA_DATA, REGION_DATA, IMAGE_DATA, COMPARE_DATA, WRITING_DATA, SWAG_DATA, TAG_DATA, OLD_CLASSIFICATION_DATA, STUDY_DATA, DET_DATA, SUPER_DATA, LETTERBOX_DATA, REGRESSION_DATA, SEGMENTATION_DATA, INSTANCE_DATA
2017-06-02 06:31:13 +03:00
} data_type;
typedef struct load_args{
int threads;
char **paths;
char *path;
int n;
int m;
char **labels;
int h;
int w;
int out_w;
int out_h;
int nh;
int nw;
int num_boxes;
int min, max, size;
int classes;
int background;
int scale;
int center;
2017-07-12 02:44:09 +03:00
int coords;
2017-06-02 06:31:13 +03:00
float jitter;
float angle;
float aspect;
float saturation;
float exposure;
float hue;
data *d;
image *im;
image *resized;
data_type type;
tree *hierarchy;
} load_args;
typedef struct{
int id;
float x,y,w,h;
float left, right, top, bottom;
} box_label;
network *load_network(char *cfg, char *weights, int clear);
load_args get_base_args(network *net);
2017-06-02 06:31:13 +03:00
void free_data(data d);
typedef struct node{
void *val;
struct node *next;
struct node *prev;
} node;
typedef struct list{
int size;
node *front;
node *back;
} list;
pthread_t load_data(load_args args);
list *read_data_cfg(char *filename);
list *read_cfg(char *filename);
2017-10-10 23:35:28 +03:00
unsigned char *read_file(char *filename);
data resize_data(data orig, int w, int h);
data *tile_data(data orig, int divs, int size);
data select_data(data *orig, int *inds);
2017-06-02 06:31:13 +03:00
void forward_network(network *net);
void backward_network(network *net);
void update_network(network *net);
2017-06-08 23:47:31 +03:00
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY);
void copy_cpu(int N, float *X, int INCX, float *Y, int INCY);
void scal_cpu(int N, float ALPHA, float *X, int INCX);
void fill_cpu(int N, float ALPHA, float * X, int INCX);
2017-06-08 23:47:31 +03:00
void normalize_cpu(float *x, float *mean, float *variance, int batch, int filters, int spatial);
void softmax(float *input, int n, float temp, int stride, float *output);
2017-06-08 23:47:31 +03:00
int best_3d_shift_r(image a, image b, int min, int max);
#ifdef GPU
2017-06-18 23:05:37 +03:00
void axpy_gpu(int N, float ALPHA, float * X, int INCX, float * Y, int INCY);
void fill_gpu(int N, float ALPHA, float * X, int INCX);
void scal_gpu(int N, float ALPHA, float * X, int INCX);
void copy_gpu(int N, float * X, int INCX, float * Y, int INCY);
2017-06-08 23:47:31 +03:00
void cuda_set_device(int n);
void cuda_free(float *x_gpu);
float *cuda_make_array(float *x, size_t n);
void cuda_pull_array(float *x_gpu, float *x, size_t n);
float cuda_mag_array(float *x_gpu, size_t n);
void cuda_push_array(float *x_gpu, float *x, size_t n);
void forward_network_gpu(network *net);
void backward_network_gpu(network *net);
void update_network_gpu(network *net);
2017-06-08 23:47:31 +03:00
float train_networks(network **nets, int n, data d, int interval);
void sync_nets(network **nets, int n, int interval);
void harmless_update_network_gpu(network *net);
2017-06-08 23:47:31 +03:00
#endif
void save_image_png(image im, const char *name);
void get_next_batch(data d, int n, int offset, float *X, float *y);
void grayscale_image_3c(image im);
void normalize_image(image p);
void matrix_to_csv(matrix m);
float train_network_sgd(network *net, data d, int n);
2017-06-08 23:47:31 +03:00
void rgbgr_image(image im);
data copy_data(data d);
data concat_data(data d1, data d2);
data load_cifar10_data(char *filename);
float matrix_topk_accuracy(matrix truth, matrix guess, int k);
void matrix_add_matrix(matrix from, matrix to);
void scale_matrix(matrix m, float scale);
matrix csv_to_matrix(char *filename);
float *network_accuracies(network *net, data d, int n);
float train_network_datum(network *net);
2017-06-08 23:47:31 +03:00
image make_random_image(int w, int h, int c);
void denormalize_connected_layer(layer l);
void denormalize_convolutional_layer(layer l);
void statistics_connected_layer(layer l);
void rescale_weights(layer l, float scale, float trans);
void rgbgr_weights(layer l);
image *get_weights(layer l);
void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix, int avg, float hier_thresh, int w, int h, int fps, int fullscreen);
void get_detection_detections(layer l, int w, int h, float thresh, detection *dets);
2017-06-08 23:47:31 +03:00
char *option_find_str(list *l, char *key, char *def);
int option_find_int(list *l, char *key, int def);
network *parse_network_cfg(char *filename);
void save_weights(network *net, char *filename);
2017-06-08 23:47:31 +03:00
void load_weights(network *net, char *filename);
void save_weights_upto(network *net, char *filename, int cutoff);
2017-06-08 23:47:31 +03:00
void load_weights_upto(network *net, char *filename, int start, int cutoff);
void zero_objectness(layer l);
void get_region_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, float tree_thresh, int relative, detection *dets);
void free_network(network *net);
2017-06-08 23:47:31 +03:00
void set_batch_network(network *net, int b);
void set_temp_network(network *net, float t);
2017-06-08 23:47:31 +03:00
image load_image(char *filename, int w, int h, int c);
image load_image_color(char *filename, int w, int h);
image make_image(int w, int h, int c);
image resize_image(image im, int w, int h);
image letterbox_image(image im, int w, int h);
image crop_image(image im, int dx, int dy, int w, int h);
image resize_min(image im, int min);
image resize_max(image im, int max);
2017-06-08 23:47:31 +03:00
image threshold_image(image im, float thresh);
image mask_to_rgb(image mask);
int resize_network(network *net, int w, int h);
void free_matrix(matrix m);
void test_resize(char *filename);
void save_image(image p, const char *name);
void show_image(image p, const char *name);
image copy_image(image p);
void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b);
float get_current_rate(network *net);
2017-06-08 23:47:31 +03:00
void composite_3d(char *f1, char *f2, char *out, int delta);
data load_data_old(char **paths, int n, int m, char **labels, int k, int w, int h);
size_t get_current_batch(network *net);
2017-06-08 23:47:31 +03:00
void constrain_image(image im);
image get_network_image_layer(network *net, int i);
layer get_network_output_layer(network *net);
void top_predictions(network *net, int n, int *index);
2017-06-08 23:47:31 +03:00
void flip_image(image a);
image float_to_image(int w, int h, int c, float *data);
void ghost_image(image source, image dest, int dx, int dy);
float network_accuracy(network *net, data d);
2017-06-08 23:47:31 +03:00
void random_distort_image(image im, float hue, float saturation, float exposure);
void fill_image(image m, float s);
image grayscale_image(image im);
void rotate_image_cw(image im, int times);
2017-07-12 02:44:09 +03:00
double what_time_is_it_now();
2017-06-08 23:47:31 +03:00
image rotate_image(image m, float rad);
void visualize_network(network *net);
2017-06-08 23:47:31 +03:00
float box_iou(box a, box b);
data load_all_cifar10();
box_label *read_boxes(char *filename, int *n);
2017-07-12 02:44:09 +03:00
box float_to_box(float *f, int stride);
void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes);
2017-06-08 23:47:31 +03:00
matrix network_predict_data(network *net, data test);
2017-06-08 23:47:31 +03:00
image **load_alphabet();
image get_network_image(network *net);
float *network_predict(network *net, float *input);
2017-06-08 23:47:31 +03:00
int network_width(network *net);
int network_height(network *net);
float *network_predict_image(network *net, image im);
void network_detect(network *net, image im, float thresh, float hier_thresh, float nms, detection *dets);
int num_boxes(network *net);
detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative);
void fill_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, detection *dets);
detection *make_network_boxes(network *net);
void free_detections(detection *dets, int n);
2017-06-08 23:47:31 +03:00
void reset_network_state(network *net, int b);
2017-07-27 11:28:57 +03:00
2017-06-08 23:47:31 +03:00
char **get_labels(char *filename);
void do_nms_obj(detection *dets, int total, int classes, float thresh);
void do_nms_sort(detection *dets, int total, int classes, float thresh);
2017-06-08 23:47:31 +03:00
matrix make_matrix(int rows, int cols);
#ifndef __cplusplus
#ifdef OPENCV
image get_image_from_stream(CvCapture *cap);
#endif
#endif
void free_image(image m);
float train_network(network *net, data d);
2017-06-08 23:47:31 +03:00
pthread_t load_data_in_thread(load_args args);
2017-07-12 02:44:09 +03:00
void load_data_blocking(load_args args);
2017-06-08 23:47:31 +03:00
list *get_paths(char *filename);
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride);
void change_leaves(tree *t, char *leaf_list);
int find_int_arg(int argc, char **argv, char *arg, int def);
float find_float_arg(int argc, char **argv, char *arg, float def);
int find_arg(int argc, char* argv[], char *arg);
char *find_char_arg(int argc, char **argv, char *arg, char *def);
char *basecfg(char *cfgfile);
void find_replace(char *str, char *orig, char *rep, char *output);
void free_ptrs(void **ptrs, int n);
char *fgetl(FILE *fp);
void strip(char *s);
float sec(clock_t clocks);
void **list_to_array(list *l);
void top_k(float *a, int n, int k, int *index);
int *read_map(char *filename);
void error(const char *s);
int max_index(float *a, int n);
int max_int_index(int *a, int n);
2017-06-08 23:47:31 +03:00
int sample_array(float *a, int n);
int *random_index_order(int min, int max);
2017-06-08 23:47:31 +03:00
void free_list(list *l);
float mse_array(float *a, int n);
float variance_array(float *a, int n);
float mag_array(float *a, int n);
void scale_array(float *a, int n, float s);
2017-06-08 23:47:31 +03:00
float mean_array(float *a, int n);
float sum_array(float *a, int n);
2017-06-08 23:47:31 +03:00
void normalize_array(float *a, int n);
int *read_intlist(char *s, int *n, int d);
size_t rand_size_t();
float rand_normal();
2017-06-02 06:31:13 +03:00
#endif