Skip to content

Commit

Permalink
idk man 🐍 stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreddie committed Jul 27, 2017
1 parent 7a223d8 commit 2f212a4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 58 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/darknet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
22 changes: 3 additions & 19 deletions examples/rnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions include/darknet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
82 changes: 49 additions & 33 deletions python/darknet.py
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -10,61 +25,62 @@ 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)

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)
predict = lib.network_predict_p
predict.argtypes = [c_void_p, POINTER(c_float)]
predict.restype = POINTER(c_float)

reset_rnn = lib.reset_rnn
reset_rnn.argtypes = [c_void_p]

load_net = lib.load_network_p
load_net.argtypes = [c_char_p, c_char_p, c_int]
load_net.restype = c_void_p

letterbox_image = lib.letterbox_image
letterbox_image.argtypes = [IMAGE, c_int, c_int]
letterbox_image.restype = IMAGE

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]))
res = sorted(res, key=lambda x: -x[1])
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]


30 changes: 30 additions & 0 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/region_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 2f212a4

Please sign in to comment.