mirror of
https://github.com/pjreddie/darknet.git
synced 2023-08-10 21:13:14 +03:00
🐍 🐍 🐍 🐍
This commit is contained in:
parent
56d69e73ab
commit
fc069593f2
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,6 +4,7 @@
|
||||
*.out
|
||||
*.png
|
||||
*.jpg
|
||||
*.pyc
|
||||
old/
|
||||
mnist/
|
||||
data/
|
||||
|
10
Makefile
10
Makefile
@ -10,21 +10,21 @@ ARCH= -gencode arch=compute_20,code=[sm_20,sm_21] \
|
||||
-gencode arch=compute_52,code=[sm_52,compute_52]
|
||||
|
||||
# This is what I use, uncomment if you know your arch and want to specify
|
||||
# ARCH= -gencode arch=compute_52,code=compute_52
|
||||
ARCH= -gencode arch=compute_52,code=compute_52
|
||||
|
||||
VPATH=./src/:./examples
|
||||
LIB=libdarknet.a
|
||||
LIB=libdarknet.so
|
||||
EXEC=darknet
|
||||
OBJDIR=./obj/
|
||||
|
||||
CC=gcc
|
||||
NVCC=nvcc
|
||||
NVCC=nvcc --compiler-options '-fPIC'
|
||||
AR=ar
|
||||
ARFLAGS=-rv
|
||||
OPTS=-Ofast
|
||||
LDFLAGS= -lm -pthread
|
||||
COMMON= -Iinclude/ -Isrc/
|
||||
CFLAGS=-Wall -Wfatal-errors
|
||||
CFLAGS=-Wall -Wfatal-errors -fPIC
|
||||
|
||||
ifeq ($(DEBUG), 1)
|
||||
OPTS=-O0 -g
|
||||
@ -69,7 +69,7 @@ $(EXEC): $(EXECOBJ) $(LIB)
|
||||
$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(LIB)
|
||||
|
||||
$(LIB): $(OBJS)
|
||||
$(AR) $(ARFLAGS) $@ $^
|
||||
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)
|
||||
|
||||
$(OBJDIR)%.o: %.c $(DEPS)
|
||||
$(CC) $(COMMON) $(CFLAGS) -c $< -o $@
|
||||
|
@ -1,25 +1,23 @@
|
||||
[net]
|
||||
batch=128
|
||||
subdivisions=1
|
||||
height=32
|
||||
width=32
|
||||
height=28
|
||||
width=28
|
||||
channels=3
|
||||
momentum=0.9
|
||||
decay=0.0005
|
||||
max_crop=32
|
||||
min_crop=32
|
||||
|
||||
hue=.1
|
||||
saturation=.75
|
||||
exposure=.75
|
||||
|
||||
learning_rate=0.4
|
||||
policy=poly
|
||||
power=4
|
||||
max_batches = 50000
|
||||
max_batches = 5000
|
||||
momentum=0.9
|
||||
decay=0.0005
|
||||
|
||||
[crop]
|
||||
crop_width=28
|
||||
crop_height=28
|
||||
flip=1
|
||||
angle=0
|
||||
saturation = 1
|
||||
exposure = 1
|
||||
noadjust=1
|
||||
|
||||
[convolutional]
|
||||
batch_normalize=1
|
||||
|
@ -33,7 +33,6 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
|
||||
cuda_set_device(gpus[i]);
|
||||
#endif
|
||||
nets[i] = load_network(cfgfile, weightfile, clear);
|
||||
nets[i].learning_rate *= ngpus;
|
||||
}
|
||||
srand(time(0));
|
||||
network net = nets[0];
|
||||
|
@ -313,7 +313,7 @@ void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
|
||||
if(weightfile){
|
||||
load_weights(&net, weightfile);
|
||||
}
|
||||
detection_layer l = net.layers[net.n-1];
|
||||
layer l = net.layers[net.n-1];
|
||||
set_batch_network(&net, 1);
|
||||
srand(2222222);
|
||||
float nms = .4;
|
||||
|
@ -22,12 +22,7 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
|
||||
#ifdef GPU
|
||||
cuda_set_device(gpus[i]);
|
||||
#endif
|
||||
nets[i] = parse_network_cfg(cfgfile);
|
||||
if(weightfile){
|
||||
load_weights(&nets[i], weightfile);
|
||||
}
|
||||
if(clear) *nets[i].seen = 0;
|
||||
nets[i].learning_rate *= ngpus;
|
||||
nets[i] = load_network(cfgfile, weightfile, clear);
|
||||
}
|
||||
srand(time(0));
|
||||
network net = nets[0];
|
||||
|
@ -451,7 +451,7 @@ void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear,
|
||||
printf("%s\n", base);
|
||||
network gnet = load_network(cfg, weight, clear);
|
||||
network anet = load_network(acfg, aweight, clear);
|
||||
float orig_rate = anet.learning_rate;
|
||||
//float orig_rate = anet.learning_rate;
|
||||
|
||||
int start = 0;
|
||||
int i, j, k;
|
||||
@ -494,7 +494,7 @@ void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear,
|
||||
int y_size = gnet.truths*gnet.batch;
|
||||
float *imerror = cuda_make_array(0, y_size);
|
||||
|
||||
int ay_size = anet.truths*anet.batch;
|
||||
//int ay_size = anet.truths*anet.batch;
|
||||
|
||||
float aloss_avg = -1;
|
||||
|
||||
@ -664,14 +664,14 @@ void train_colorizer(char *cfg, char *weight, char *acfg, char *aweight, int cle
|
||||
clock_t time;
|
||||
|
||||
int x_size = net.inputs*net.batch;
|
||||
int y_size = x_size;
|
||||
//int y_size = x_size;
|
||||
net.delta = 0;
|
||||
net.train = 1;
|
||||
float *pixs = calloc(x_size, sizeof(float));
|
||||
float *graypixs = calloc(x_size, sizeof(float));
|
||||
float *y = calloc(y_size, sizeof(float));
|
||||
//float *y = calloc(y_size, sizeof(float));
|
||||
|
||||
int ay_size = anet.outputs*anet.batch;
|
||||
//int ay_size = anet.outputs*anet.batch;
|
||||
anet.delta = 0;
|
||||
anet.train = 1;
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include "darknet.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
// ./darknet nightmare cfg/extractor.recon.cfg ~/trained/yolo-coco.conv frame6.png -reconstruct -iters 500 -i 3 -lambda .1 -rate .01 -smooth 2
|
||||
|
||||
float abs_mean(float *x, int n)
|
||||
@ -128,11 +130,11 @@ void smooth(image recon, image update, float lambda, int num)
|
||||
void reconstruct_picture(network net, float *features, image recon, image update, float rate, float momentum, float lambda, int smooth_size, int iters)
|
||||
{
|
||||
int iter = 0;
|
||||
layer l = get_network_output_layer(net);
|
||||
for (iter = 0; iter < iters; ++iter) {
|
||||
image delta = make_image(recon.w, recon.h, recon.c);
|
||||
|
||||
#ifdef GPU
|
||||
layer l = get_network_output_layer(net);
|
||||
cuda_push_array(net.input_gpu, recon.data, recon.w*recon.h*recon.c);
|
||||
//cuda_push_array(net.truth_gpu, features, net.truths);
|
||||
net.delta_gpu = cuda_make_array(delta.data, delta.w*delta.h*delta.c);
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include "darknet.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
typedef struct {
|
||||
float *x;
|
||||
float *y;
|
||||
|
@ -24,7 +24,6 @@ void train_segmenter(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
|
||||
load_weights(&nets[i], weightfile);
|
||||
}
|
||||
if(clear) *nets[i].seen = 0;
|
||||
nets[i].learning_rate *= ngpus;
|
||||
}
|
||||
srand(time(0));
|
||||
network net = nets[0];
|
||||
@ -76,6 +75,15 @@ void train_segmenter(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
|
||||
pthread_join(load_thread, 0);
|
||||
train = buffer;
|
||||
load_thread = load_data(args);
|
||||
image tr = float_to_image(net.w, net.h, 81, train.y.vals[0]);
|
||||
image im = float_to_image(net.w, net.h, net.c, train.X.vals[0]);
|
||||
image mask = mask_to_rgb(tr);
|
||||
show_image(im, "input");
|
||||
show_image(mask, "truth");
|
||||
#ifdef OPENCV
|
||||
cvWaitKey(100);
|
||||
#endif
|
||||
free_image(mask);
|
||||
|
||||
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
||||
time=clock();
|
||||
|
@ -279,7 +279,7 @@ void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh)
|
||||
if(weightfile){
|
||||
load_weights(&net, weightfile);
|
||||
}
|
||||
detection_layer l = net.layers[net.n-1];
|
||||
layer l = net.layers[net.n-1];
|
||||
set_batch_network(&net, 1);
|
||||
srand(2222222);
|
||||
clock_t time;
|
||||
|
@ -1,7 +1,11 @@
|
||||
#ifndef DARKNET_API
|
||||
#define DARKNET_API
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#define SECRET_NUM -1234
|
||||
extern int gpu_index;
|
||||
|
||||
#ifdef GPU
|
||||
@ -27,6 +31,13 @@ extern int gpu_index;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef struct{
|
||||
int classes;
|
||||
char **names;
|
||||
} metadata;
|
||||
|
||||
metadata get_metadata(char *file);
|
||||
|
||||
typedef struct{
|
||||
int *leaf;
|
||||
int n;
|
||||
@ -42,7 +53,7 @@ typedef struct{
|
||||
|
||||
typedef enum{
|
||||
LOGISTIC, RELU, RELIE, LINEAR, RAMP, TANH, PLSE, LEAKY, ELU, LOGGY, STAIR, HARDTAN, LHTAN
|
||||
}ACTIVATION;
|
||||
} ACTIVATION;
|
||||
|
||||
typedef enum {
|
||||
CONVOLUTIONAL,
|
||||
@ -255,7 +266,7 @@ struct layer{
|
||||
|
||||
size_t workspace_size;
|
||||
|
||||
#ifdef GPU
|
||||
#ifdef GPU
|
||||
int *indexes_gpu;
|
||||
|
||||
float *z_gpu;
|
||||
@ -281,8 +292,8 @@ struct layer{
|
||||
float * concat_gpu;
|
||||
float * concat_delta_gpu;
|
||||
|
||||
float *binary_input_gpu;
|
||||
float *binary_weights_gpu;
|
||||
float * binary_input_gpu;
|
||||
float * binary_weights_gpu;
|
||||
|
||||
float * mean_gpu;
|
||||
float * variance_gpu;
|
||||
@ -297,19 +308,22 @@ struct layer{
|
||||
float * x_norm_gpu;
|
||||
float * weights_gpu;
|
||||
float * weight_updates_gpu;
|
||||
float * weight_change_gpu;
|
||||
|
||||
float * biases_gpu;
|
||||
float * bias_updates_gpu;
|
||||
float * bias_change_gpu;
|
||||
|
||||
float * scales_gpu;
|
||||
float * scale_updates_gpu;
|
||||
float * scale_change_gpu;
|
||||
|
||||
float * output_gpu;
|
||||
float * delta_gpu;
|
||||
float * rand_gpu;
|
||||
float * squared_gpu;
|
||||
float * norms_gpu;
|
||||
#ifdef CUDNN
|
||||
#ifdef CUDNN
|
||||
cudnnTensorDescriptor_t srcTensorDesc, dstTensorDesc;
|
||||
cudnnTensorDescriptor_t dsrcTensorDesc, ddstTensorDesc;
|
||||
cudnnTensorDescriptor_t normTensorDesc;
|
||||
@ -319,8 +333,8 @@ struct layer{
|
||||
cudnnConvolutionFwdAlgo_t fw_algo;
|
||||
cudnnConvolutionBwdDataAlgo_t bd_algo;
|
||||
cudnnConvolutionBwdFilterAlgo_t bf_algo;
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
};
|
||||
|
||||
void free_layer(layer);
|
||||
@ -383,12 +397,12 @@ typedef struct network{
|
||||
int index;
|
||||
float *cost;
|
||||
|
||||
#ifdef GPU
|
||||
#ifdef GPU
|
||||
float *input_gpu;
|
||||
float *truth_gpu;
|
||||
float *delta_gpu;
|
||||
float *output_gpu;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} network;
|
||||
|
||||
@ -403,8 +417,8 @@ typedef struct {
|
||||
} augment_args;
|
||||
|
||||
typedef struct {
|
||||
int h;
|
||||
int w;
|
||||
int h;
|
||||
int c;
|
||||
float *data;
|
||||
} image;
|
||||
@ -472,6 +486,7 @@ typedef struct{
|
||||
|
||||
|
||||
network load_network(char *cfg, char *weights, int clear);
|
||||
network *load_network_p(char *cfg, char *weights, int clear);
|
||||
load_args get_base_args(network net);
|
||||
|
||||
void free_data(data d);
|
||||
@ -492,47 +507,171 @@ pthread_t load_data(load_args args);
|
||||
list *read_data_cfg(char *filename);
|
||||
list *read_cfg(char *filename);
|
||||
|
||||
void forward_network(network net);
|
||||
void backward_network(network net);
|
||||
void update_network(network net);
|
||||
|
||||
|
||||
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 normalize_cpu(float *x, float *mean, float *variance, int batch, int filters, int spatial);
|
||||
|
||||
int best_3d_shift_r(image a, image b, int min, int max);
|
||||
#ifdef GPU
|
||||
void axpy_ongpu(int N, float ALPHA, float * X, int INCX, float * Y, int INCY);
|
||||
void fill_ongpu(int N, float ALPHA, float * X, int INCX);
|
||||
void scal_ongpu(int N, float ALPHA, float * X, int INCX);
|
||||
void copy_ongpu(int N, float * X, int INCX, float * Y, int INCY);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
#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);
|
||||
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);
|
||||
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_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness);
|
||||
|
||||
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);
|
||||
void load_weights(network *net, char *filename);
|
||||
void save_weights_upto(network net, char *filename, int cutoff);
|
||||
void load_weights_upto(network *net, char *filename, int start, int cutoff);
|
||||
|
||||
void zero_objectness(layer l);
|
||||
void get_region_boxes(layer l, int w, int h, int netw, int neth, float thresh, float **probs, box *boxes, int only_objectness, int *map, float tree_thresh, int relative);
|
||||
void free_network(network net);
|
||||
void set_batch_network(network *net, int b);
|
||||
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 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);
|
||||
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);
|
||||
int get_current_batch(network net);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
image rotate_image(image m, float rad);
|
||||
void visualize_network(network net);
|
||||
float box_iou(box a, box b);
|
||||
void do_nms(box *boxes, float **probs, int total, int classes, float thresh);
|
||||
data load_all_cifar10();
|
||||
box_label *read_boxes(char *filename, int *n);
|
||||
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **labels, int classes);
|
||||
|
||||
matrix network_predict_data(network net, data test);
|
||||
image **load_alphabet();
|
||||
image get_network_image(network net);
|
||||
float *network_predict(network net, float *input);
|
||||
float *network_predict_p(network *net, float *input);
|
||||
|
||||
int network_width(network *net);
|
||||
int network_height(network *net);
|
||||
float *network_predict_image(network *net, image im);
|
||||
|
||||
char **get_labels(char *filename);
|
||||
void do_nms_sort(box *boxes, float **probs, int total, int classes, float thresh);
|
||||
void do_nms_obj(box *boxes, float **probs, int total, int classes, float thresh);
|
||||
|
||||
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);
|
||||
pthread_t load_data_in_thread(load_args args);
|
||||
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 sample_array(float *a, int n);
|
||||
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);
|
||||
float mean_array(float *a, int n);
|
||||
void normalize_array(float *a, int n);
|
||||
int *read_intlist(char *s, int *n, int d);
|
||||
size_t rand_size_t();
|
||||
float rand_normal();
|
||||
|
||||
#include "activation_layer.h"
|
||||
#include "activations.h"
|
||||
#include "avgpool_layer.h"
|
||||
#include "batchnorm_layer.h"
|
||||
#include "blas.h"
|
||||
#include "box.h"
|
||||
#include "classifier.h"
|
||||
#include "col2im.h"
|
||||
#include "connected_layer.h"
|
||||
#include "convolutional_layer.h"
|
||||
#include "cost_layer.h"
|
||||
#include "crnn_layer.h"
|
||||
#include "crop_layer.h"
|
||||
#include "cuda.h"
|
||||
#include "data.h"
|
||||
#include "deconvolutional_layer.h"
|
||||
#include "demo.h"
|
||||
#include "detection_layer.h"
|
||||
#include "dropout_layer.h"
|
||||
#include "gemm.h"
|
||||
#include "gru_layer.h"
|
||||
#include "im2col.h"
|
||||
#include "image.h"
|
||||
#include "layer.h"
|
||||
#include "list.h"
|
||||
#include "local_layer.h"
|
||||
#include "matrix.h"
|
||||
#include "maxpool_layer.h"
|
||||
#include "network.h"
|
||||
#include "normalization_layer.h"
|
||||
#include "option_list.h"
|
||||
#include "parser.h"
|
||||
#include "region_layer.h"
|
||||
#include "reorg_layer.h"
|
||||
#include "rnn_layer.h"
|
||||
#include "route_layer.h"
|
||||
#include "shortcut_layer.h"
|
||||
#include "softmax_layer.h"
|
||||
#include "stb_image.h"
|
||||
#include "stb_image_write.h"
|
||||
#include "tree.h"
|
||||
#include "utils.h"
|
||||
#endif
|
||||
|
70
python/darknet.py
Normal file
70
python/darknet.py
Normal file
@ -0,0 +1,70 @@
|
||||
from ctypes import *
|
||||
|
||||
class IMAGE(Structure):
|
||||
_fields_ = [("w", c_int),
|
||||
("h", c_int),
|
||||
("c", c_int),
|
||||
("data", POINTER(c_float))]
|
||||
|
||||
class METADATA(Structure):
|
||||
_fields_ = [("classes", c_int),
|
||||
("names", POINTER(c_char_p))]
|
||||
|
||||
lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
|
||||
lib.network_width.argtypes = [c_void_p]
|
||||
lib.network_width.restype = c_int
|
||||
lib.network_height.argtypes = [c_void_p]
|
||||
lib.network_height.restype = c_int
|
||||
|
||||
def load_meta(f):
|
||||
lib.get_metadata.argtypes = [c_char_p]
|
||||
lib.get_metadata.restype = METADATA
|
||||
return lib.get_metadata(f)
|
||||
|
||||
def load_net(cfg, weights):
|
||||
load_network = lib.load_network_p
|
||||
load_network.argtypes = [c_char_p, c_char_p, c_int]
|
||||
load_network.restype = c_void_p
|
||||
return load_network(cfg, weights, 0)
|
||||
|
||||
def load_img(f):
|
||||
load_image = lib.load_image_color
|
||||
load_image.argtypes = [c_char_p, c_int, c_int]
|
||||
load_image.restype = IMAGE
|
||||
return load_image(f, 0, 0)
|
||||
|
||||
def letterbox_img(im, w, h):
|
||||
letterbox_image = lib.letterbox_image
|
||||
letterbox_image.argtypes = [IMAGE, c_int, c_int]
|
||||
letterbox_image.restype = IMAGE
|
||||
return letterbox_image(im, w, h)
|
||||
|
||||
def predict(net, im):
|
||||
pred = lib.network_predict_image
|
||||
pred.argtypes = [c_void_p, IMAGE]
|
||||
pred.restype = POINTER(c_float)
|
||||
return pred(net, im)
|
||||
|
||||
def classify(net, meta, im):
|
||||
out = predict(net, im)
|
||||
res = []
|
||||
for i in range(meta.classes):
|
||||
res.append((meta.names[i], out[i]))
|
||||
res = sorted(res, key=lambda x: -x[1])
|
||||
return res
|
||||
|
||||
def detect(net, meta, im):
|
||||
out = predict(net, im)
|
||||
res = []
|
||||
for i in range(meta.classes):
|
||||
res.append((meta.names[i], out[i]))
|
||||
res = sorted(res, key=lambda x: -x[1])
|
||||
return res
|
||||
|
||||
if __name__ == "__main__":
|
||||
net = load_net("cfg/densenet.cfg", "/home/pjreddie/trained/densenet201.weights")
|
||||
im = load_img("data/wolf.jpg")
|
||||
meta = load_meta("cfg/imagenet1k.data")
|
||||
r = classify(net, meta, im)
|
||||
print r[:10]
|
||||
|
@ -1,5 +1,7 @@
|
||||
#ifndef BLAS_H
|
||||
#define BLAS_H
|
||||
#include "darknet.h"
|
||||
|
||||
void flatten(float *x, int size, int layers, int batch, int forward);
|
||||
void pm(int M, int N, float *A);
|
||||
float *random_matrix(int rows, int cols);
|
||||
@ -13,9 +15,6 @@ void constrain_ongpu(int N, float ALPHA, float * X, int INCX);
|
||||
void pow_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY);
|
||||
void mul_cpu(int N, float *X, int INCX, float *Y, int INCY);
|
||||
|
||||
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);
|
||||
float dot_cpu(int N, float *X, int INCX, float *Y, int INCY);
|
||||
int test_gpu_blas();
|
||||
@ -23,7 +22,6 @@ void shortcut_cpu(int batch, int w1, int h1, int c1, float *add, int w2, int h2,
|
||||
|
||||
void mean_cpu(float *x, int batch, int filters, int spatial, float *mean);
|
||||
void variance_cpu(float *x, float *mean, int batch, int filters, int spatial, float *variance);
|
||||
void normalize_cpu(float *x, float *mean, float *variance, int batch, int filters, int spatial);
|
||||
|
||||
void scale_bias(float *output, float *scales, int batch, int n, int size);
|
||||
void backward_scale_cpu(float *x_norm, float *delta, int batch, int n, int size, float *scale_updates);
|
||||
@ -47,14 +45,12 @@ void axpy_ongpu(int N, float ALPHA, float * X, int INCX, float * Y, int INCY);
|
||||
void axpy_ongpu_offset(int N, float ALPHA, float * X, int OFFX, int INCX, float * Y, int OFFY, int INCY);
|
||||
void copy_ongpu(int N, float * X, int INCX, float * Y, int INCY);
|
||||
void copy_ongpu_offset(int N, float * X, int OFFX, int INCX, float * Y, int OFFY, int INCY);
|
||||
void scal_ongpu(int N, float ALPHA, float * X, int INCX);
|
||||
void add_ongpu(int N, float ALPHA, float * X, int INCX);
|
||||
void supp_ongpu(int N, float ALPHA, float * X, int INCX);
|
||||
void mask_ongpu(int N, float * X, float mask_num, float * mask);
|
||||
void const_ongpu(int N, float ALPHA, float *X, int INCX);
|
||||
void pow_ongpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY);
|
||||
void mul_ongpu(int N, float *X, int INCX, float *Y, int INCY);
|
||||
void fill_ongpu(int N, float ALPHA, float * X, int INCX);
|
||||
|
||||
void mean_gpu(float *x, int batch, int filters, int spatial, float *mean);
|
||||
void variance_gpu(float *x, float *mean, int batch, int filters, int spatial, float *variance);
|
||||
|
@ -7,12 +7,8 @@ typedef struct{
|
||||
} dbox;
|
||||
|
||||
box float_to_box(float *f, int stride);
|
||||
float box_iou(box a, box b);
|
||||
float box_rmse(box a, box b);
|
||||
dbox diou(box a, box b);
|
||||
void do_nms(box *boxes, float **probs, int total, int classes, float thresh);
|
||||
void do_nms_sort(box *boxes, float **probs, int total, int classes, float thresh);
|
||||
void do_nms_obj(box *boxes, float **probs, int total, int classes, float thresh);
|
||||
box decode_box(box b, box anchor);
|
||||
box encode_box(box b, box anchor);
|
||||
|
||||
|
@ -12,8 +12,6 @@ connected_layer make_connected_layer(int batch, int inputs, int outputs, ACTIVAT
|
||||
void forward_connected_layer(connected_layer layer, network net);
|
||||
void backward_connected_layer(connected_layer layer, network net);
|
||||
void update_connected_layer(connected_layer layer, int batch, float learning_rate, float momentum, float decay);
|
||||
void denormalize_connected_layer(layer l);
|
||||
void statistics_connected_layer(layer l);
|
||||
|
||||
#ifdef GPU
|
||||
void forward_connected_layer_gpu(connected_layer layer, network net);
|
||||
|
@ -26,7 +26,6 @@ void cudnn_convolutional_setup(layer *l);
|
||||
#endif
|
||||
|
||||
convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam);
|
||||
void denormalize_convolutional_layer(convolutional_layer l);
|
||||
void resize_convolutional_layer(convolutional_layer *layer, int w, int h);
|
||||
void forward_convolutional_layer(const convolutional_layer layer, network net);
|
||||
void update_convolutional_layer(convolutional_layer layer, int batch, float learning_rate, float momentum, float decay);
|
||||
@ -40,15 +39,12 @@ void backward_convolutional_layer(convolutional_layer layer, network net);
|
||||
void add_bias(float *output, float *biases, int batch, int n, int size);
|
||||
void backward_bias(float *bias_updates, float *delta, int batch, int n, int size);
|
||||
|
||||
image *get_weights(convolutional_layer l);
|
||||
image get_convolutional_image(convolutional_layer layer);
|
||||
image get_convolutional_delta(convolutional_layer layer);
|
||||
image get_convolutional_weight(convolutional_layer layer, int i);
|
||||
|
||||
int convolutional_out_height(convolutional_layer layer);
|
||||
int convolutional_out_width(convolutional_layer layer);
|
||||
void rescale_weights(convolutional_layer l, float scale, float trans);
|
||||
void rgbgr_weights(convolutional_layer l);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -96,6 +96,8 @@ float *cuda_make_array(float *x, size_t n)
|
||||
if(x){
|
||||
status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
|
||||
check_error(status);
|
||||
} else {
|
||||
fill_ongpu(n, 0, x_gpu, 1);
|
||||
}
|
||||
if(!x_gpu) error("Cuda malloc failed\n");
|
||||
return x_gpu;
|
||||
|
@ -7,16 +7,10 @@
|
||||
|
||||
void check_error(cudaError_t status);
|
||||
cublasHandle_t blas_handle();
|
||||
float *cuda_make_array(float *x, size_t n);
|
||||
int *cuda_make_int_array(int *x, size_t n);
|
||||
void cuda_push_array(float *x_gpu, float *x, size_t n);
|
||||
void cuda_pull_array(float *x_gpu, float *x, size_t n);
|
||||
void cuda_set_device(int n);
|
||||
void cuda_free(float *x_gpu);
|
||||
void cuda_random(float *x_gpu, size_t n);
|
||||
float cuda_compare(float *x_gpu, float *x, size_t n, char *s);
|
||||
dim3 cuda_gridsize(size_t n);
|
||||
float cuda_mag_array(float *x_gpu, size_t n);
|
||||
|
||||
#ifdef CUDNN
|
||||
cudnnHandle_t cudnn_handle();
|
||||
|
28
src/data.c
28
src/data.c
@ -535,16 +535,17 @@ void or_image(image src, image dest, int c)
|
||||
}
|
||||
}
|
||||
|
||||
void fill_bg_mask(image m)
|
||||
void exclusive_image(image src)
|
||||
{
|
||||
int i,k;
|
||||
int index = m.w*m.h*(m.c-1);
|
||||
for(i = 0; i < m.w*m.h; ++i){
|
||||
m.data[index + i] = 1;
|
||||
}
|
||||
for(k = 0; k < m.c-1; ++k){
|
||||
for(i = 0; i < m.w*m.h; ++i){
|
||||
if(m.data[index + i] && m.data[k*m.w*m.h + i]) m.data[index + i] = 0;
|
||||
int k, j, i;
|
||||
int s = src.w*src.h;
|
||||
for(k = 0; k < src.c-1; ++k){
|
||||
for(i = 0; i < s; ++i){
|
||||
if (src.data[k*s + i]){
|
||||
for(j = k+1; j < src.c; ++j){
|
||||
src.data[j*s + i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -558,6 +559,10 @@ image get_segmentation_image(char *path, int w, int h, int classes)
|
||||
find_replace(labelpath, ".JPG", ".txt", labelpath);
|
||||
find_replace(labelpath, ".JPEG", ".txt", labelpath);
|
||||
image mask = make_image(w, h, classes+1);
|
||||
int i;
|
||||
for(i = 0; i < w*h; ++i){
|
||||
mask.data[w*h*classes + i] = 1;
|
||||
}
|
||||
FILE *file = fopen(labelpath, "r");
|
||||
if(!file) file_error(labelpath);
|
||||
char buff[32788];
|
||||
@ -568,9 +573,12 @@ image get_segmentation_image(char *path, int w, int h, int classes)
|
||||
int *rle = read_intlist(buff, &n, 0);
|
||||
load_rle(part, rle, n);
|
||||
or_image(part, mask, id);
|
||||
for(i = 0; i < w*h; ++i){
|
||||
if(part.data[i]) mask.data[w*h*classes + i] = 0;
|
||||
}
|
||||
free(rle);
|
||||
}
|
||||
fill_bg_mask(mask);
|
||||
//exclusive_image(mask);
|
||||
fclose(file);
|
||||
free_image(part);
|
||||
return mask;
|
||||
|
10
src/data.h
10
src/data.h
@ -20,12 +20,10 @@ static inline float distance_from_edge(int x, int max)
|
||||
}
|
||||
void load_data_blocking(load_args args);
|
||||
|
||||
pthread_t load_data_in_thread(load_args args);
|
||||
|
||||
void print_letters(float *pred, int n);
|
||||
data load_data_captcha(char **paths, int n, int m, int k, int w, int h);
|
||||
data load_data_captcha_encode(char **paths, int n, int m, int w, int h);
|
||||
data load_data_old(char **paths, int n, int m, char **labels, int k, int w, int h);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
@ -34,27 +32,19 @@ data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *h
|
||||
data load_data_regression(char **paths, int n, int m, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure);
|
||||
data load_go(char *filename);
|
||||
|
||||
box_label *read_boxes(char *filename, int *n);
|
||||
data load_cifar10_data(char *filename);
|
||||
data load_all_cifar10();
|
||||
|
||||
data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h);
|
||||
|
||||
list *get_paths(char *filename);
|
||||
char **get_labels(char *filename);
|
||||
void get_random_batch(data d, int n, float *X, float *y);
|
||||
data get_data_part(data d, int part, int total);
|
||||
data get_random_data(data d, int num);
|
||||
void get_next_batch(data d, int n, int offset, float *X, float *y);
|
||||
data load_categorical_data_csv(char *filename, int target, int k);
|
||||
void normalize_data_rows(data d);
|
||||
void scale_data_rows(data d, float s);
|
||||
void translate_data_rows(data d, float s);
|
||||
void randomize_data(data d);
|
||||
data *split_data(data d, int part, int total);
|
||||
data concat_data(data d1, data d2);
|
||||
data concat_datas(data *d, int n);
|
||||
void fill_truth(char *path, char **labels, int k, float *truth);
|
||||
data copy_data(data d);
|
||||
|
||||
#endif
|
||||
|
@ -2,6 +2,5 @@
|
||||
#define DEMO_H
|
||||
|
||||
#include "image.h"
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
@ -9,7 +9,6 @@ typedef layer detection_layer;
|
||||
detection_layer make_detection_layer(int batch, int inputs, int n, int size, int classes, int coords, int rescore);
|
||||
void forward_detection_layer(const detection_layer l, network net);
|
||||
void backward_detection_layer(const detection_layer l, network net);
|
||||
void get_detection_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness);
|
||||
|
||||
#ifdef GPU
|
||||
void forward_detection_layer_gpu(const detection_layer l, network net);
|
||||
|
@ -36,9 +36,9 @@ image mask_to_rgb(image mask)
|
||||
float green = get_color(1,offset,n);
|
||||
float blue = get_color(0,offset,n);
|
||||
for(i = 0; i < im.w*im.h; ++i){
|
||||
im.data[i + 0*im.w*im.h] = mask.data[j*im.h*im.w + i]*red;
|
||||
im.data[i + 1*im.w*im.h] = mask.data[j*im.h*im.w + i]*green;
|
||||
im.data[i + 2*im.w*im.h] = mask.data[j*im.h*im.w + i]*blue;
|
||||
im.data[i + 0*im.w*im.h] += mask.data[j*im.h*im.w + i]*red;
|
||||
im.data[i + 1*im.w*im.h] += mask.data[j*im.h*im.w + i]*green;
|
||||
im.data[i + 2*im.w*im.h] += mask.data[j*im.h*im.w + i]*blue;
|
||||
}
|
||||
}
|
||||
return im;
|
||||
|
34
src/image.h
34
src/image.h
@ -11,7 +11,6 @@
|
||||
|
||||
#ifndef __cplusplus
|
||||
#ifdef OPENCV
|
||||
image get_image_from_stream(CvCapture *cap);
|
||||
int fill_image_from_stream(CvCapture *cap, image im);
|
||||
image ipl_to_image(IplImage* src);
|
||||
void ipl_into_image(IplImage* src, image im);
|
||||
@ -20,36 +19,22 @@ void show_image_cv(image p, const char *name, IplImage *disp);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
image mask_to_rgb(image mask);
|
||||
float get_color(int c, int x, int max);
|
||||
void flip_image(image a);
|
||||
void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b);
|
||||
void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b);
|
||||
void draw_bbox(image a, box bbox, int w, float r, float g, float b);
|
||||
void draw_label(image a, int r, int c, image label, const float *rgb);
|
||||
void write_label(image a, int r, int c, image *characters, char *string, float *rgb);
|
||||
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **labels, int classes);
|
||||
image image_distance(image a, image b);
|
||||
void scale_image(image m, float s);
|
||||
image crop_image(image im, int dx, int dy, int w, int h);
|
||||
image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect);
|
||||
image center_crop_image(image im, int w, int h);
|
||||
image random_crop_image(image im, int w, int h);
|
||||
image random_augment_image(image im, float angle, float aspect, int low, int high, int w, int h);
|
||||
augment_args random_augment_args(image im, float angle, float aspect, int low, int high, int w, int h);
|
||||
void random_distort_image(image im, float hue, float saturation, float exposure);
|
||||
image letterbox_image(image im, int w, int h);
|
||||
void letterbox_image_into(image im, int w, int h, image boxed);
|
||||
image resize_image(image im, int w, int h);
|
||||
image resize_min(image im, int min);
|
||||
image resize_max(image im, int max);
|
||||
void fill_image(image m, float s);
|
||||
void translate_image(image m, float s);
|
||||
void normalize_image(image p);
|
||||
image rotate_image(image m, float rad);
|
||||
void rotate_image_cw(image im, int times);
|
||||
void embed_image(image source, image dest, int dx, int dy);
|
||||
void ghost_image(image source, image dest, int dx, int dy);
|
||||
void place_image(image im, int w, int h, int dx, int dy, image canvas);
|
||||
void saturate_image(image im, float sat);
|
||||
void exposure_image(image im, float sat);
|
||||
@ -59,38 +44,21 @@ void rgb_to_hsv(image im);
|
||||
void hsv_to_rgb(image im);
|
||||
void yuv_to_rgb(image im);
|
||||
void rgb_to_yuv(image im);
|
||||
void rgbgr_image(image im);
|
||||
void constrain_image(image im);
|
||||
void composite_3d(char *f1, char *f2, char *out, int delta);
|
||||
int best_3d_shift_r(image a, image b, int min, int max);
|
||||
|
||||
image grayscale_image(image im);
|
||||
void grayscale_image_3c(image im);
|
||||
image threshold_image(image im, float thresh);
|
||||
|
||||
image collapse_image_layers(image source, int border);
|
||||
image collapse_images_horz(image *ims, int n);
|
||||
image collapse_images_vert(image *ims, int n);
|
||||
|
||||
void show_image(image p, const char *name);
|
||||
void show_image_normalized(image im, const char *name);
|
||||
void save_image_png(image im, const char *name);
|
||||
void save_image(image p, const char *name);
|
||||
void show_images(image *ims, int n, char *window);
|
||||
void show_image_layers(image p, char *name);
|
||||
void show_image_collapsed(image p, char *name);
|
||||
|
||||
void print_image(image m);
|
||||
|
||||
image make_image(int w, int h, int c);
|
||||
image make_random_image(int w, int h, int c);
|
||||
image make_empty_image(int w, int h, int c);
|
||||
image float_to_image(int w, int h, int c, float *data);
|
||||
image copy_image(image p);
|
||||
void copy_image_into(image src, image dest);
|
||||
image load_image(char *filename, int w, int h, int c);
|
||||
image load_image_color(char *filename, int w, int h);
|
||||
image **load_alphabet();
|
||||
|
||||
float get_pixel(image m, int x, int y, int c);
|
||||
float get_pixel_extend(image m, int x, int y, int c);
|
||||
@ -100,7 +68,5 @@ float bilinear_interpolate(image im, float x, float y, int c);
|
||||
|
||||
image get_image_layer(image m, int l);
|
||||
|
||||
void free_image(image m);
|
||||
void test_resize(char *filename);
|
||||
#endif
|
||||
|
||||
|
@ -7,9 +7,7 @@ int list_find(list *l, void *val);
|
||||
|
||||
void list_insert(list *, void *);
|
||||
|
||||
void **list_to_array(list *l);
|
||||
|
||||
void free_list(list *l);
|
||||
void free_list_contents(list *l);
|
||||
|
||||
#endif
|
||||
|
@ -2,17 +2,10 @@
|
||||
#define MATRIX_H
|
||||
#include "darknet.h"
|
||||
|
||||
matrix make_matrix(int rows, int cols);
|
||||
matrix copy_matrix(matrix m);
|
||||
void free_matrix(matrix m);
|
||||
void print_matrix(matrix m);
|
||||
|
||||
matrix csv_to_matrix(char *filename);
|
||||
void matrix_to_csv(matrix m);
|
||||
matrix hold_out_matrix(matrix *m, int n);
|
||||
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 resize_matrix(matrix m, int size);
|
||||
|
||||
float *pop_column(matrix *m, int c);
|
||||
|
@ -58,6 +58,13 @@ network load_network(char *cfg, char *weights, int clear)
|
||||
return net;
|
||||
}
|
||||
|
||||
network *load_network_p(char *cfg, char *weights, int clear)
|
||||
{
|
||||
network *net = calloc(1, sizeof(network));
|
||||
*net = load_network(cfg, weights, clear);
|
||||
return net;
|
||||
}
|
||||
|
||||
int get_current_batch(network net)
|
||||
{
|
||||
int batch_num = (*net.seen)/(net.batch*net.subdivisions);
|
||||
@ -439,6 +446,23 @@ float *network_predict(network net, float *input)
|
||||
return net.output;
|
||||
}
|
||||
|
||||
float *network_predict_p(network *net, float *input)
|
||||
{
|
||||
return network_predict(*net, input);
|
||||
}
|
||||
|
||||
float *network_predict_image(network *net, image im)
|
||||
{
|
||||
image imr = letterbox_image(im, net->w, net->h);
|
||||
set_batch_network(net, 1);
|
||||
float *p = network_predict(*net, imr.data);
|
||||
free_image(imr);
|
||||
return p;
|
||||
}
|
||||
|
||||
int network_width(network *net){return net->w;}
|
||||
int network_height(network *net){return net->h;}
|
||||
|
||||
matrix network_predict_data_multi(network net, data test, int n)
|
||||
{
|
||||
int i,j,b,m;
|
||||
|
@ -10,46 +10,21 @@
|
||||
|
||||
|
||||
#ifdef GPU
|
||||
float train_networks(network *nets, int n, data d, int interval);
|
||||
void sync_nets(network *nets, int n, int interval);
|
||||
float train_network_datum_gpu(network net);
|
||||
float *network_predict_gpu(network net, float *input);
|
||||
void pull_network_output(network net);
|
||||
void forward_network_gpu(network net);
|
||||
void backward_network_gpu(network net);
|
||||
void update_network_gpu(network net);
|
||||
void harmless_update_network_gpu(network net);
|
||||
#endif
|
||||
|
||||
float get_current_rate(network net);
|
||||
int get_current_batch(network net);
|
||||
void free_network(network net);
|
||||
void compare_networks(network n1, network n2, data d);
|
||||
char *get_layer_string(LAYER_TYPE a);
|
||||
|
||||
network make_network(int n);
|
||||
void forward_network(network net);
|
||||
void backward_network(network net);
|
||||
void update_network(network net);
|
||||
|
||||
float train_network(network net, data d);
|
||||
float train_network_sgd(network net, data d, int n);
|
||||
float train_network_datum(network net);
|
||||
|
||||
matrix network_predict_data(network net, data test);
|
||||
float *network_predict(network net, float *input);
|
||||
float network_accuracy(network net, data d);
|
||||
float *network_accuracies(network net, data d, int n);
|
||||
float network_accuracy_multi(network net, data d, int n);
|
||||
void top_predictions(network net, int n, int *index);
|
||||
image get_network_image(network net);
|
||||
image get_network_image_layer(network net, int i);
|
||||
layer get_network_output_layer(network net);
|
||||
int get_predicted_class_network(network net);
|
||||
void print_network(network net);
|
||||
void visualize_network(network net);
|
||||
int resize_network(network *net, int w, int h);
|
||||
void set_batch_network(network *net, int b);
|
||||
void calc_network_cost(network net);
|
||||
|
||||
#endif
|
||||
|
@ -149,6 +149,74 @@ pthread_t train_network_in_thread(network net, data d, float *err)
|
||||
return thread;
|
||||
}
|
||||
|
||||
void merge_weights(layer l, layer base)
|
||||
{
|
||||
if (l.type == CONVOLUTIONAL) {
|
||||
axpy_cpu(l.n, 1, l.bias_updates, 1, base.biases, 1);
|
||||
axpy_cpu(l.n*l.size*l.size*l.c, 1, l.weight_updates, 1, base.weights, 1);
|
||||
if (l.scales) {
|
||||
axpy_cpu(l.n, 1, l.scale_updates, 1, base.scales, 1);
|
||||
}
|
||||
} else if(l.type == CONNECTED) {
|
||||
axpy_cpu(l.outputs, 1, l.bias_updates, 1, base.biases, 1);
|
||||
axpy_cpu(l.outputs*l.inputs, 1, l.weight_updates, 1, base.weights, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void scale_weights(layer l, float s)
|
||||
{
|
||||
if (l.type == CONVOLUTIONAL) {
|
||||
scal_cpu(l.n, s, l.biases, 1);
|
||||
scal_cpu(l.n*l.size*l.size*l.c, s, l.weights, 1);
|
||||
if (l.scales) {
|
||||
scal_cpu(l.n, s, l.scales, 1);
|
||||
}
|
||||
} else if(l.type == CONNECTED) {
|
||||
scal_cpu(l.outputs, s, l.biases, 1);
|
||||
scal_cpu(l.outputs*l.inputs, s, l.weights, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void pull_weights(layer l)
|
||||
{
|
||||
if(l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
|
||||
cuda_pull_array(l.biases_gpu, l.bias_updates, l.n);
|
||||
cuda_pull_array(l.weights_gpu, l.weight_updates, l.n*l.size*l.size*l.c);
|
||||
if(l.scales) cuda_pull_array(l.scales_gpu, l.scale_updates, l.n);
|
||||
} else if(l.type == CONNECTED){
|
||||
cuda_pull_array(l.biases_gpu, l.bias_updates, l.outputs);
|
||||
cuda_pull_array(l.weights_gpu, l.weight_updates, l.outputs*l.inputs);
|
||||
}
|
||||
}
|
||||
|
||||
void push_weights(layer l)
|
||||
{
|
||||
if(l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
|
||||
cuda_push_array(l.biases_gpu, l.biases, l.n);
|
||||
cuda_push_array(l.weights_gpu, l.weights, l.n*l.size*l.size*l.c);
|
||||
if(l.scales) cuda_push_array(l.scales_gpu, l.scales, l.n);
|
||||
} else if(l.type == CONNECTED){
|
||||
cuda_push_array(l.biases_gpu, l.biases, l.outputs);
|
||||
cuda_push_array(l.weights_gpu, l.weights, l.outputs*l.inputs);
|
||||
}
|
||||
}
|
||||
|
||||
void distribute_weights(layer l, layer base)
|
||||
{
|
||||
if (l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL) {
|
||||
cuda_push_array(l.biases_gpu, base.biases, l.n);
|
||||
cuda_push_array(l.weights_gpu, base.weights, l.n*l.size*l.size*l.c);
|
||||
if (base.scales) cuda_push_array(l.scales_gpu, base.scales, l.n);
|
||||
} else if (l.type == CONNECTED) {
|
||||
cuda_push_array(l.biases_gpu, base.biases, l.outputs);
|
||||
cuda_push_array(l.weights_gpu, base.weights, l.outputs*l.inputs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
void pull_updates(layer l)
|
||||
{
|
||||
if(l.type == CONVOLUTIONAL){
|
||||
@ -182,73 +250,6 @@ void update_layer(layer l, network net)
|
||||
l.update_gpu(l, update_batch, rate*l.learning_rate_scale, net.momentum, net.decay);
|
||||
}
|
||||
}
|
||||
|
||||
void merge_weights(layer l, layer base)
|
||||
{
|
||||
if (l.type == CONVOLUTIONAL) {
|
||||
axpy_cpu(l.n, 1, l.biases, 1, base.biases, 1);
|
||||
axpy_cpu(l.n*l.size*l.size*l.c, 1, l.weights, 1, base.weights, 1);
|
||||
if (l.scales) {
|
||||
axpy_cpu(l.n, 1, l.scales, 1, base.scales, 1);
|
||||
}
|
||||
} else if(l.type == CONNECTED) {
|
||||
axpy_cpu(l.outputs, 1, l.biases, 1, base.biases, 1);
|
||||
axpy_cpu(l.outputs*l.inputs, 1, l.weights, 1, base.weights, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void scale_weights(layer l, float s)
|
||||
{
|
||||
if (l.type == CONVOLUTIONAL) {
|
||||
scal_cpu(l.n, s, l.biases, 1);
|
||||
scal_cpu(l.n*l.size*l.size*l.c, s, l.weights, 1);
|
||||
if (l.scales) {
|
||||
scal_cpu(l.n, s, l.scales, 1);
|
||||
}
|
||||
} else if(l.type == CONNECTED) {
|
||||
scal_cpu(l.outputs, s, l.biases, 1);
|
||||
scal_cpu(l.outputs*l.inputs, s, l.weights, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void pull_weights(layer l)
|
||||
{
|
||||
if(l.type == CONVOLUTIONAL){
|
||||
cuda_pull_array(l.biases_gpu, l.biases, l.n);
|
||||
cuda_pull_array(l.weights_gpu, l.weights, l.n*l.size*l.size*l.c);
|
||||
if(l.scales) cuda_pull_array(l.scales_gpu, l.scales, l.n);
|
||||
} else if(l.type == CONNECTED){
|
||||
cuda_pull_array(l.biases_gpu, l.biases, l.outputs);
|
||||
cuda_pull_array(l.weights_gpu, l.weights, l.outputs*l.inputs);
|
||||
}
|
||||
}
|
||||
|
||||
void push_weights(layer l)
|
||||
{
|
||||
if(l.type == CONVOLUTIONAL){
|
||||
cuda_push_array(l.biases_gpu, l.biases, l.n);
|
||||
cuda_push_array(l.weights_gpu, l.weights, l.n*l.size*l.size*l.c);
|
||||
if(l.scales) cuda_push_array(l.scales_gpu, l.scales, l.n);
|
||||
} else if(l.type == CONNECTED){
|
||||
cuda_push_array(l.biases_gpu, l.biases, l.outputs);
|
||||
cuda_push_array(l.weights_gpu, l.weights, l.outputs*l.inputs);
|
||||
}
|
||||
}
|
||||
|
||||
void distribute_weights(layer l, layer base)
|
||||
{
|
||||
if(l.type == CONVOLUTIONAL){
|
||||
cuda_push_array(l.biases_gpu, base.biases, l.n);
|
||||
cuda_push_array(l.weights_gpu, base.weights, l.n*l.size*l.size*l.c);
|
||||
if(base.scales) cuda_push_array(l.scales_gpu, base.scales, l.n);
|
||||
} else if(l.type == CONNECTED){
|
||||
cuda_push_array(l.biases_gpu, base.biases, l.outputs);
|
||||
cuda_push_array(l.weights_gpu, base.weights, l.outputs*l.inputs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void merge_updates(layer l, layer base)
|
||||
{
|
||||
if (l.type == CONVOLUTIONAL) {
|
||||
@ -265,7 +266,7 @@ void merge_updates(layer l, layer base)
|
||||
|
||||
void distribute_updates(layer l, layer base)
|
||||
{
|
||||
if(l.type == CONVOLUTIONAL){
|
||||
if(l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
|
||||
cuda_push_array(l.bias_updates_gpu, base.bias_updates, l.n);
|
||||
cuda_push_array(l.weight_updates_gpu, base.weight_updates, l.n*l.size*l.size*l.c);
|
||||
if(base.scale_updates) cuda_push_array(l.scale_updates_gpu, base.scale_updates, l.n);
|
||||
@ -274,16 +275,37 @@ void distribute_updates(layer l, layer base)
|
||||
cuda_push_array(l.weight_updates_gpu, base.weight_updates, l.outputs*l.inputs);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
void sync_layer(network *nets, int n, int j)
|
||||
{
|
||||
//printf("Syncing layer %d\n", j);
|
||||
int i;
|
||||
network net = nets[0];
|
||||
layer base = net.layers[j];
|
||||
cuda_set_device(net.gpu_index);
|
||||
pull_weights(base);
|
||||
for (i = 1; i < n; ++i) {
|
||||
scale_weights(base, 0);
|
||||
for (i = 0; i < n; ++i) {
|
||||
cuda_set_device(nets[i].gpu_index);
|
||||
layer l = nets[i].layers[j];
|
||||
pull_weights(l);
|
||||
merge_weights(l, base);
|
||||
}
|
||||
scale_weights(base, 1./n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
cuda_set_device(nets[i].gpu_index);
|
||||
layer l = nets[i].layers[j];
|
||||
distribute_weights(l, base);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void sync_layer(network *nets, int n, int j)
|
||||
{
|
||||
int i;
|
||||
network net = nets[0];
|
||||
layer base = net.layers[j];
|
||||
scale_weights(base, 0);
|
||||
for (i = 0; i < n; ++i) {
|
||||
cuda_set_device(nets[i].gpu_index);
|
||||
layer l = nets[i].layers[j];
|
||||
pull_weights(l);
|
||||
@ -295,7 +317,6 @@ void sync_layer(network *nets, int n, int j)
|
||||
layer l = nets[i].layers[j];
|
||||
distribute_weights(l, base);
|
||||
}
|
||||
//printf("Done syncing layer %d\n", j);
|
||||
}
|
||||
|
||||
typedef struct{
|
||||
|
@ -32,6 +32,23 @@ list *read_data_cfg(char *filename)
|
||||
return options;
|
||||
}
|
||||
|
||||
metadata get_metadata(char *file)
|
||||
{
|
||||
metadata m = {0};
|
||||
list *options = read_data_cfg(file);
|
||||
|
||||
char *name_list = option_find_str(options, "names", 0);
|
||||
if(!name_list) name_list = option_find_str(options, "labels", 0);
|
||||
if(!name_list) {
|
||||
fprintf(stderr, "No names or labels found\n");
|
||||
} else {
|
||||
m.names = get_labels(name_list);
|
||||
}
|
||||
m.classes = option_find_int(options, "classes", 2);
|
||||
free_list(options);
|
||||
return m;
|
||||
}
|
||||
|
||||
int read_option(char *s, list *options)
|
||||
{
|
||||
size_t i;
|
||||
|
@ -12,8 +12,6 @@ typedef struct{
|
||||
int read_option(char *s, list *options);
|
||||
void option_insert(list *l, char *key, char *val);
|
||||
char *option_find(list *l, char *key);
|
||||
char *option_find_str(list *l, char *key, char *def);
|
||||
int option_find_int(list *l, char *key, int def);
|
||||
int option_find_int_quiet(list *l, char *key, int def);
|
||||
float option_find_float(list *l, char *key, float def);
|
||||
float option_find_float_quiet(list *l, char *key, float def);
|
||||
|
@ -1,13 +1,9 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
#include "darknet.h"
|
||||
#include "network.h"
|
||||
|
||||
network parse_network_cfg(char *filename);
|
||||
void save_network(network net, char *filename);
|
||||
void save_weights(network net, char *filename);
|
||||
void save_weights_upto(network net, char *filename, int cutoff);
|
||||
void save_weights_double(network net, char *filename);
|
||||
void load_weights(network *net, char *filename);
|
||||
void load_weights_upto(network *net, char *filename, int start, int cutoff);
|
||||
|
||||
#endif
|
||||
|
@ -445,6 +445,15 @@ void forward_region_layer_gpu(const layer l, network net)
|
||||
}
|
||||
}
|
||||
if (l.softmax_tree){
|
||||
int mmin = 9000;
|
||||
int mmax = 0;
|
||||
int i;
|
||||
for(i = 0; i < l.softmax_tree->groups; ++i){
|
||||
int group_size = l.softmax_tree->group_size[i];
|
||||
if (group_size < mmin) mmin = group_size;
|
||||
if (group_size > mmax) mmax = group_size;
|
||||
}
|
||||
printf("%d %d %d \n", l.softmax_tree->groups, mmin, mmax);
|
||||
int index = entry_index(l, 0, 0, 5);
|
||||
softmax_tree(net.input_gpu + index, l.w*l.h, l.batch*l.n, l.inputs/l.n, 1, l.output_gpu + index, *l.softmax_tree);
|
||||
/*
|
||||
|
@ -1,15 +1,14 @@
|
||||
#ifndef REGION_LAYER_H
|
||||
#define REGION_LAYER_H
|
||||
|
||||
#include "darknet.h"
|
||||
#include "layer.h"
|
||||
#include "network.h"
|
||||
|
||||
layer make_region_layer(int batch, int h, int w, int n, int classes, int coords);
|
||||
void forward_region_layer(const layer l, network net);
|
||||
void backward_region_layer(const layer l, network net);
|
||||
void get_region_boxes(layer l, int w, int h, int netw, int neth, float thresh, float **probs, box *boxes, int only_objectness, int *map, float tree_thresh, int relative);
|
||||
void resize_region_layer(layer *l, int w, int h);
|
||||
void zero_objectness(layer l);
|
||||
|
||||
#ifdef GPU
|
||||
void forward_region_layer_gpu(const layer l, network net);
|
||||
|
@ -3,8 +3,6 @@
|
||||
#include "darknet.h"
|
||||
|
||||
tree *read_tree(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 hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride);
|
||||
float get_hierarchy_probability(float *x, tree *hier, int c, int stride);
|
||||
|
||||
|
20
src/utils.h
20
src/utils.h
@ -2,18 +2,15 @@
|
||||
#define UTILS_H
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include "darknet.h"
|
||||
#include "list.h"
|
||||
|
||||
#define SECRET_NUM -1234
|
||||
#define TWO_PI 6.2831853071795864769252866
|
||||
|
||||
double what_time_is_it_now();
|
||||
int *read_intlist(char *s, int *n, int d);
|
||||
int *read_map(char *filename);
|
||||
void shuffle(void *arr, size_t n, size_t size);
|
||||
void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections);
|
||||
void free_ptrs(void **ptrs, int n);
|
||||
char *basecfg(char *cfgfile);
|
||||
int alphanum_to_int(char c);
|
||||
char int_to_alphanum(int i);
|
||||
int read_int(int fd);
|
||||
@ -23,43 +20,28 @@ void write_all(int fd, char *buffer, size_t bytes);
|
||||
int read_all_fail(int fd, char *buffer, size_t bytes);
|
||||
int write_all_fail(int fd, char *buffer, size_t bytes);
|
||||
void find_replace(char *str, char *orig, char *rep, char *output);
|
||||
void error(const char *s);
|
||||
void malloc_error();
|
||||
void file_error(char *s);
|
||||
void strip(char *s);
|
||||
void strip_char(char *s, char bad);
|
||||
void top_k(float *a, int n, int k, int *index);
|
||||
list *split_str(char *s, char delim);
|
||||
char *fgetl(FILE *fp);
|
||||
list *parse_csv_line(char *line);
|
||||
char *copy_string(char *s);
|
||||
int count_fields(char *line);
|
||||
float *parse_fields(char *line, int n);
|
||||
void normalize_array(float *a, int n);
|
||||
void scale_array(float *a, int n, float s);
|
||||
void translate_array(float *a, int n, float s);
|
||||
int max_index(float *a, int n);
|
||||
float constrain(float min, float max, float a);
|
||||
int constrain_int(int a, int min, int max);
|
||||
float mse_array(float *a, int n);
|
||||
float rand_normal();
|
||||
size_t rand_size_t();
|
||||
float rand_uniform(float min, float max);
|
||||
float rand_scale(float s);
|
||||
int rand_int(int min, int max);
|
||||
float sum_array(float *a, int n);
|
||||
float mean_array(float *a, int n);
|
||||
void mean_arrays(float **a, int n, int els, float *avg);
|
||||
float variance_array(float *a, int n);
|
||||
float mag_array(float *a, int n);
|
||||
float dist_array(float *a, float *b, int n, int sub);
|
||||
float **one_hot_encode(float *a, int n, int k);
|
||||
float sec(clock_t clocks);
|
||||
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);
|
||||
int sample_array(float *a, int n);
|
||||
void print_statistics(float *a, int n);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user