idk man 🐍 stuff

This commit is contained in:
Joseph Redmon 2017-07-27 01:28:57 -07:00
parent 7a223d8591
commit 2f212a4742
7 changed files with 89 additions and 54 deletions

View File

@ -29,7 +29,7 @@ COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC
ifeq ($(OPENMP), 1) ifeq ($(OPENMP), 1)
COMMON+= -fopenmp CFLAGS+= -fopenmp
endif endif
ifeq ($(DEBUG), 1) ifeq ($(DEBUG), 1)
@ -68,7 +68,8 @@ EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
OBJS = $(addprefix $(OBJDIR), $(OBJ)) OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h 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) $(EXEC): $(EXECOBJ) $(ALIB)

View File

@ -146,7 +146,7 @@ void oneoff(char *cfgfile, char *weightfile, char *outfile)
int c = net.layers[net.n - 2].c; int c = net.layers[net.n - 2].c;
scal_cpu(oldn*c, .1, net.layers[net.n - 2].weights, 1); scal_cpu(oldn*c, .1, net.layers[net.n - 2].weights, 1);
scal_cpu(oldn, 0, net.layers[net.n - 2].biases, 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].biases += 5;
net.layers[net.n - 2].weights += 5*c; net.layers[net.n - 2].weights += 5*c;
if(weightfile){ if(weightfile){

View File

@ -108,22 +108,6 @@ float_pair get_rnn_data(unsigned char *text, size_t *offsets, int characters, si
return p; 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) void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear, int tokenized)
{ {
srand(time(0)); srand(time(0));
@ -194,7 +178,7 @@ void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear,
if(rand()%64 == 0){ if(rand()%64 == 0){
//fprintf(stderr, "Reset\n"); //fprintf(stderr, "Reset\n");
offsets[j] = rand_size_t()%size; 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; float *out = 0;
while(1){ while(1){
reset_rnn_state(net, 0); reset_network_state(net, 0);
while((c = getc(stdin)) != EOF && c != 0){ while((c = getc(stdin)) != EOF && c != 0){
input[c] = 1; input[c] = 1;
out = network_predict(net, input); out = network_predict(net, input);
@ -482,7 +466,7 @@ void vec_char_rnn(char *cfgfile, char *weightfile, char *seed)
int i; int i;
char *line; char *line;
while((line=fgetl(stdin)) != 0){ while((line=fgetl(stdin)) != 0){
reset_rnn_state(net, 0); reset_network_state(net, 0);
for(i = 0; i < seed_len; ++i){ for(i = 0; i < seed_len; ++i){
c = seed[i]; c = seed[i];
input[(int)c] = 1; input[(int)c] = 1;

View File

@ -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 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 free_network(network net);
void set_batch_network(network *net, int b); 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(char *filename, int w, int h, int c);
image load_image_color(char *filename, int w, int h); image load_image_color(char *filename, int w, int h);
image make_image(int w, int h, int c); image make_image(int w, int h, int c);
@ -699,6 +700,9 @@ int network_width(network *net);
int network_height(network *net); int network_height(network *net);
float *network_predict_image(network *net, image im); 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); char **get_labels(char *filename);
void do_nms_sort(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); void do_nms_obj(box *boxes, float **probs, int total, int classes, float thresh);

View File

@ -1,4 +1,19 @@
from ctypes import * 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): class IMAGE(Structure):
_fields_ = [("w", c_int), _fields_ = [("w", c_int),
@ -10,43 +25,42 @@ class METADATA(Structure):
_fields_ = [("classes", c_int), _fields_ = [("classes", c_int),
("names", POINTER(c_char_p))] ("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.argtypes = [c_void_p]
lib.network_width.restype = c_int lib.network_width.restype = c_int
lib.network_height.argtypes = [c_void_p] lib.network_height.argtypes = [c_void_p]
lib.network_height.restype = c_int lib.network_height.restype = c_int
def load_meta(f): predict = lib.network_predict_p
lib.get_metadata.argtypes = [c_char_p] predict.argtypes = [c_void_p, POINTER(c_float)]
lib.get_metadata.restype = METADATA predict.restype = POINTER(c_float)
return lib.get_metadata(f)
def load_net(cfg, weights): reset_rnn = lib.reset_rnn
load_network = lib.load_network_p reset_rnn.argtypes = [c_void_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_net = lib.load_network_p
load_image = lib.load_image_color load_net.argtypes = [c_char_p, c_char_p, c_int]
load_image.argtypes = [c_char_p, c_int, c_int] load_net.restype = c_void_p
load_image.restype = IMAGE
return load_image(f, 0, 0)
def letterbox_img(im, w, h): letterbox_image = lib.letterbox_image
letterbox_image = lib.letterbox_image letterbox_image.argtypes = [IMAGE, c_int, c_int]
letterbox_image.argtypes = [IMAGE, c_int, c_int] letterbox_image.restype = IMAGE
letterbox_image.restype = IMAGE
return letterbox_image(im, w, h)
def predict(net, im): load_meta = lib.get_metadata
pred = lib.network_predict_image lib.get_metadata.argtypes = [c_char_p]
pred.argtypes = [c_void_p, IMAGE] lib.get_metadata.restype = METADATA
pred.restype = POINTER(c_float)
return pred(net, im) 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): def classify(net, meta, im):
out = predict(net, im) out = predict_image(net, im)
res = [] res = []
for i in range(meta.classes): for i in range(meta.classes):
res.append((meta.names[i], out[i])) res.append((meta.names[i], out[i]))
@ -54,17 +68,19 @@ def classify(net, meta, im):
return res return res
def detect(net, meta, im): def detect(net, meta, im):
out = predict(net, im) out = predict_image(net, im)
res = [] res = []
for i in range(meta.classes): for i in range(meta.classes):
res.append((meta.names[i], out[i])) res.append((meta.names[i], out[i]))
res = sorted(res, key=lambda x: -x[1]) res = sorted(res, key=lambda x: -x[1])
return res return res
if __name__ == "__main__": if __name__ == "__main__":
net = load_net("cfg/densenet.cfg", "/home/pjreddie/trained/densenet201.weights") net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0)
im = load_img("data/wolf.jpg") im = load_image("data/wolf.jpg", 0, 0)
meta = load_meta("cfg/imagenet1k.data") meta = load_meta("cfg/imagenet1k.data")
r = classify(net, meta, im) r = classify(net, meta, im)
print r[:10] print r[:10]

View File

@ -82,6 +82,27 @@ void reset_momentum(network net)
#endif #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) float get_current_rate(network net)
{ {
size_t batch_num = get_current_batch(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); 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) void set_batch_network(network *net, int b)
{ {
net->batch = b; net->batch = b;

View File

@ -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 class_index = entry_index(l, b, n, l.coords + 1);
int obj_index = entry_index(l, b, n, l.coords); int obj_index = entry_index(l, b, n, l.coords);
float scale = l.output[obj_index]; 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); float p = scale*get_hierarchy_probability(l.output + class_index, l.softmax_tree, class, l.w*l.h);
if(p > maxp){ if(p > maxp){
maxp = p; 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 class_index = entry_index(l, b, maxi, l.coords + 1);
int obj_index = entry_index(l, b, maxi, l.coords); 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); 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]); if(l.output[obj_index] < .3) l.delta[obj_index] = l.object_scale * (.3 - l.output[obj_index]);
//else l.delta[obj_index] = 0; else l.delta[obj_index] = 0;
l.delta[obj_index] = 0; l.delta[obj_index] = 0;
++class_count; ++class_count;
onlyclass = 1; onlyclass = 1;