From 2f212a47425b2e1002c7c8a20e139fe0da7489b5 Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Thu, 27 Jul 2017 01:28:57 -0700 Subject: [PATCH] idk man :snake: stuff --- Makefile | 5 ++-- examples/darknet.c | 2 +- examples/rnn.c | 22 ++------------ include/darknet.h | 4 +++ python/darknet.py | 74 ++++++++++++++++++++++++++++------------------ src/network.c | 30 +++++++++++++++++++ src/region_layer.c | 6 ++-- 7 files changed, 89 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index 1a64a0af..b0a9f1ce 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ COMMON= -Iinclude/ -Isrc/ CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC ifeq ($(OPENMP), 1) -COMMON+= -fopenmp +CFLAGS+= -fopenmp endif ifeq ($(DEBUG), 1) @@ -68,7 +68,8 @@ EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) OBJS = $(addprefix $(OBJDIR), $(OBJ)) DEPS = $(wildcard src/*.h) Makefile include/darknet.h -all: obj backup results $(SLIB) $(ALIB) $(EXEC) +#all: obj backup results $(SLIB) $(ALIB) $(EXEC) +all: obj results $(SLIB) $(ALIB) $(EXEC) $(EXEC): $(EXECOBJ) $(ALIB) diff --git a/examples/darknet.c b/examples/darknet.c index a7ce1482..8e44df12 100644 --- a/examples/darknet.c +++ b/examples/darknet.c @@ -146,7 +146,7 @@ void oneoff(char *cfgfile, char *weightfile, char *outfile) int c = net.layers[net.n - 2].c; scal_cpu(oldn*c, .1, net.layers[net.n - 2].weights, 1); scal_cpu(oldn, 0, net.layers[net.n - 2].biases, 1); - net.layers[net.n - 2].n = 9418; + net.layers[net.n - 2].n = 11921; net.layers[net.n - 2].biases += 5; net.layers[net.n - 2].weights += 5*c; if(weightfile){ diff --git a/examples/rnn.c b/examples/rnn.c index faef6a7c..45e8deba 100644 --- a/examples/rnn.c +++ b/examples/rnn.c @@ -108,22 +108,6 @@ float_pair get_rnn_data(unsigned char *text, size_t *offsets, int characters, si return p; } -void reset_rnn_state(network net, int b) -{ - int i; - for (i = 0; i < net.n; ++i) { - #ifdef GPU - layer l = net.layers[i]; - if(l.state_gpu){ - fill_gpu(l.outputs, 0, l.state_gpu + l.outputs*b, 1); - } - if(l.h_gpu){ - fill_gpu(l.outputs, 0, l.h_gpu + l.outputs*b, 1); - } - #endif - } -} - void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear, int tokenized) { srand(time(0)); @@ -194,7 +178,7 @@ void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear, if(rand()%64 == 0){ //fprintf(stderr, "Reset\n"); offsets[j] = rand_size_t()%size; - reset_rnn_state(net, j); + reset_network_state(net, j); } } @@ -304,7 +288,7 @@ void test_tactic_rnn_multi(char *cfgfile, char *weightfile, int num, float temp, float *out = 0; while(1){ - reset_rnn_state(net, 0); + reset_network_state(net, 0); while((c = getc(stdin)) != EOF && c != 0){ input[c] = 1; out = network_predict(net, input); @@ -482,7 +466,7 @@ void vec_char_rnn(char *cfgfile, char *weightfile, char *seed) int i; char *line; while((line=fgetl(stdin)) != 0){ - reset_rnn_state(net, 0); + reset_network_state(net, 0); for(i = 0; i < seed_len; ++i){ c = seed[i]; input[(int)c] = 1; diff --git a/include/darknet.h b/include/darknet.h index 92425e21..4f6bd6d6 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -647,6 +647,7 @@ 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, float **masks, int only_objectness, int *map, float tree_thresh, int relative); void free_network(network net); void set_batch_network(network *net, int b); +void set_temp_network(network net, float t); 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); @@ -699,6 +700,9 @@ int network_width(network *net); int network_height(network *net); float *network_predict_image(network *net, image im); +void reset_network_state(network net, int b); +void reset_network_state(network net, int b); + 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); diff --git a/python/darknet.py b/python/darknet.py index e23e5383..7dff2f06 100644 --- a/python/darknet.py +++ b/python/darknet.py @@ -1,4 +1,19 @@ from ctypes import * +import math +import random + +def sample(probs): + s = sum(probs) + probs = [a/s for a in probs] + r = random.uniform(0, 1) + for i in range(len(probs)): + r = r - probs[i] + if r <= 0: + return i + return len(probs)-1 + +def c_array(ctype, values): + return (ctype * len(values))(*values) class IMAGE(Structure): _fields_ = [("w", c_int), @@ -10,43 +25,42 @@ class METADATA(Structure): _fields_ = [("classes", c_int), ("names", POINTER(c_char_p))] -lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL) +#lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL) +lib = CDLL("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) +predict = lib.network_predict_p +predict.argtypes = [c_void_p, POINTER(c_float)] +predict.restype = POINTER(c_float) -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) +reset_rnn = lib.reset_rnn +reset_rnn.argtypes = [c_void_p] -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) +load_net = lib.load_network_p +load_net.argtypes = [c_char_p, c_char_p, c_int] +load_net.restype = c_void_p -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) +letterbox_image = lib.letterbox_image +letterbox_image.argtypes = [IMAGE, c_int, c_int] +letterbox_image.restype = IMAGE -def predict(net, im): - pred = lib.network_predict_image - pred.argtypes = [c_void_p, IMAGE] - pred.restype = POINTER(c_float) - return pred(net, im) +load_meta = lib.get_metadata +lib.get_metadata.argtypes = [c_char_p] +lib.get_metadata.restype = METADATA + +load_image = lib.load_image_color +load_image.argtypes = [c_char_p, c_int, c_int] +load_image.restype = IMAGE + +predict_image = lib.network_predict_image +predict_image.argtypes = [c_void_p, IMAGE] +predict_image.restype = POINTER(c_float) def classify(net, meta, im): - out = predict(net, im) + out = predict_image(net, im) res = [] for i in range(meta.classes): res.append((meta.names[i], out[i])) @@ -54,17 +68,19 @@ def classify(net, meta, im): return res def detect(net, meta, im): - out = predict(net, im) + out = predict_image(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") + net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0) + im = load_image("data/wolf.jpg", 0, 0) meta = load_meta("cfg/imagenet1k.data") r = classify(net, meta, im) print r[:10] + diff --git a/src/network.c b/src/network.c index c72df25e..38d5229e 100644 --- a/src/network.c +++ b/src/network.c @@ -82,6 +82,27 @@ void reset_momentum(network net) #endif } +void reset_network_state(network net, int b) +{ + int i; + for (i = 0; i < net.n; ++i) { + #ifdef GPU + layer l = net.layers[i]; + if(l.state_gpu){ + fill_gpu(l.outputs, 0, l.state_gpu + l.outputs*b, 1); + } + if(l.h_gpu){ + fill_gpu(l.outputs, 0, l.h_gpu + l.outputs*b, 1); + } + #endif + } +} + +void reset_rnn(network *net) +{ + reset_network_state(*net, 0); +} + float get_current_rate(network net) { size_t batch_num = get_current_batch(net); @@ -302,6 +323,15 @@ float train_network(network net, data d) return (float)sum/(n*batch); } +void set_temp_network(network net, float t) +{ + int i; + for(i = 0; i < net.n; ++i){ + net.layers[i].temperature = t; + } +} + + void set_batch_network(network *net, int b) { net->batch = b; diff --git a/src/region_layer.c b/src/region_layer.c index b0a8facf..ba4cc336 100644 --- a/src/region_layer.c +++ b/src/region_layer.c @@ -203,7 +203,7 @@ void forward_region_layer(const layer l, network net) int class_index = entry_index(l, b, n, l.coords + 1); int obj_index = entry_index(l, b, n, l.coords); float scale = l.output[obj_index]; - //l.delta[obj_index] = l.noobject_scale * (0 - l.output[obj_index]); + l.delta[obj_index] = l.noobject_scale * (0 - l.output[obj_index]); float p = scale*get_hierarchy_probability(l.output + class_index, l.softmax_tree, class, l.w*l.h); if(p > maxp){ maxp = p; @@ -213,8 +213,8 @@ void forward_region_layer(const layer l, network net) int class_index = entry_index(l, b, maxi, l.coords + 1); int obj_index = entry_index(l, b, maxi, l.coords); delta_region_class(l.output, l.delta, class_index, class, l.classes, l.softmax_tree, l.class_scale, l.w*l.h, &avg_cat); - //if(l.output[obj_index] < .3) l.delta[obj_index] = l.object_scale * (.3 - l.output[obj_index]); - //else l.delta[obj_index] = 0; + if(l.output[obj_index] < .3) l.delta[obj_index] = l.object_scale * (.3 - l.output[obj_index]); + else l.delta[obj_index] = 0; l.delta[obj_index] = 0; ++class_count; onlyclass = 1;