Fixed some problems with visualizing networks

This commit is contained in:
Joseph Redmon 2015-07-24 15:14:23 -07:00
parent 9dd0cab9dc
commit fdcc096694
6 changed files with 72 additions and 20 deletions

View File

@ -41,13 +41,13 @@ void train_captcha2(char *cfgfile, char *weightfile)
//net.seen=0; //net.seen=0;
int imgs = 1024; int imgs = 1024;
int i = net.seen/imgs; int i = net.seen/imgs;
int solved = 1; int solved = 0;
list *plist; list *plist;
char **labels = get_labels("/data/captcha/reimgs.labels2.list"); char **labels = get_labels("/data/captcha/reimgs.labels2.list");
if (solved){ if (solved){
plist = get_paths("/data/captcha/reimgs.solved.list"); plist = get_paths("/data/captcha/reimgs.solved.list");
}else{ }else{
plist = get_paths("/data/captcha/reimgs.train.list"); plist = get_paths("/data/captcha/reimgs.raw.list");
} }
char **paths = (char **)list_to_array(plist); char **paths = (char **)list_to_array(plist);
printf("%d\n", plist->size); printf("%d\n", plist->size);
@ -96,7 +96,6 @@ void test_captcha2(char *cfgfile, char *weightfile, char *filename)
srand(2222222); srand(2222222);
int i = 0; int i = 0;
char **names = get_labels("/data/captcha/reimgs.labels2.list"); char **names = get_labels("/data/captcha/reimgs.labels2.list");
clock_t time;
char input[256]; char input[256];
int indexes[26]; int indexes[26];
while(1){ while(1){
@ -110,7 +109,6 @@ void test_captcha2(char *cfgfile, char *weightfile, char *filename)
} }
image im = load_image_color(input, net.w, net.h); image im = load_image_color(input, net.w, net.h);
float *X = im.data; float *X = im.data;
time=clock();
float *predictions = network_predict(net, X); float *predictions = network_predict(net, X);
top_predictions(net, 26, indexes); top_predictions(net, 26, indexes);
//printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); //printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
@ -126,6 +124,47 @@ void test_captcha2(char *cfgfile, char *weightfile, char *filename)
} }
} }
void valid_captcha(char *cfgfile, char *weightfile, char *filename)
{
char **labels = get_labels("/data/captcha/reimgs.labels.list");
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
}
list *plist = get_paths("/data/captcha/reimgs.fg.list");
char **paths = (char **)list_to_array(plist);
int N = plist->size;
int outputs = net.outputs;
set_batch_network(&net, 1);
srand(2222222);
int i, j;
for(i = 0; i < N; ++i){
if (i%100 == 0) fprintf(stderr, "%d\n", i);
image im = load_image_color(paths[i], net.w, net.h);
float *X = im.data;
float *predictions = network_predict(net, X);
//printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
int truth = -1;
for(j = 0; j < 13; ++j){
if (strstr(paths[i], labels[j])) truth = j;
}
if (truth == -1){
fprintf(stderr, "bad: %s\n", paths[i]);
return;
}
printf("%d, ", truth);
for(j = 0; j < outputs; ++j){
if (j != 0) printf(", ");
printf("%f", predictions[j]);
}
printf("\n");
fflush(stdout);
free_image(im);
if (filename) break;
}
}
void train_captcha(char *cfgfile, char *weightfile) void train_captcha(char *cfgfile, char *weightfile)
{ {
data_seed = time(0); data_seed = time(0);
@ -189,7 +228,6 @@ void test_captcha(char *cfgfile, char *weightfile, char *filename)
srand(2222222); srand(2222222);
int i = 0; int i = 0;
char **names = get_labels("/data/captcha/reimgs.labels.list"); char **names = get_labels("/data/captcha/reimgs.labels.list");
clock_t time;
char input[256]; char input[256];
int indexes[13]; int indexes[13];
while(1){ while(1){
@ -203,7 +241,6 @@ void test_captcha(char *cfgfile, char *weightfile, char *filename)
} }
image im = load_image_color(input, net.w, net.h); image im = load_image_color(input, net.w, net.h);
float *X = im.data; float *X = im.data;
time=clock();
float *predictions = network_predict(net, X); float *predictions = network_predict(net, X);
top_predictions(net, 13, indexes); top_predictions(net, 13, indexes);
//printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); //printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
@ -400,6 +437,7 @@ void run_captcha(int argc, char **argv)
char *filename = (argc > 5) ? argv[5]: 0; char *filename = (argc > 5) ? argv[5]: 0;
if(0==strcmp(argv[2], "train")) train_captcha2(cfg, weights); if(0==strcmp(argv[2], "train")) train_captcha2(cfg, weights);
else if(0==strcmp(argv[2], "test")) test_captcha2(cfg, weights, filename); else if(0==strcmp(argv[2], "test")) test_captcha2(cfg, weights, filename);
else if(0==strcmp(argv[2], "valid")) valid_captcha(cfg, weights, filename);
//if(0==strcmp(argv[2], "test")) test_captcha(cfg, weights); //if(0==strcmp(argv[2], "test")) test_captcha(cfg, weights);
//else if(0==strcmp(argv[2], "encode")) encode_captcha(cfg, weights); //else if(0==strcmp(argv[2], "encode")) encode_captcha(cfg, weights);
//else if(0==strcmp(argv[2], "decode")) decode_captcha(cfg, weights); //else if(0==strcmp(argv[2], "decode")) decode_captcha(cfg, weights);

View File

@ -248,6 +248,7 @@ image *get_filters(convolutional_layer l)
int i; int i;
for(i = 0; i < l.n; ++i){ for(i = 0; i < l.n; ++i){
filters[i] = copy_image(get_convolutional_filter(l, i)); filters[i] = copy_image(get_convolutional_filter(l, i));
normalize_image(filters[i]);
} }
return filters; return filters;
} }

View File

@ -197,8 +197,6 @@ void fill_truth_detection(char *path, float *truth, int classes, int num_boxes,
h = constrain(0, 1, h); h = constrain(0, 1, h);
if (w < .01 || h < .01) continue; if (w < .01 || h < .01) continue;
if(1){ if(1){
//w = sqrt(w);
//h = sqrt(h);
w = pow(w, 1./2.); w = pow(w, 1./2.);
h = pow(h, 1./2.); h = pow(h, 1./2.);
} }

View File

@ -188,7 +188,7 @@ void show_image_cv(image p, char *name)
int x,y,k; int x,y,k;
image copy = copy_image(p); image copy = copy_image(p);
constrain_image(copy); constrain_image(copy);
rgbgr_image(copy); if(p.c == 3) rgbgr_image(copy);
//normalize_image(copy); //normalize_image(copy);
char buff[256]; char buff[256];
@ -849,8 +849,16 @@ image collapse_images_horz(image *ims, int n)
void show_images(image *ims, int n, char *window) void show_images(image *ims, int n, char *window)
{ {
image m = collapse_images_vert(ims, n); image m = collapse_images_vert(ims, n);
save_image(m, window); int w = 448;
show_image(m, window); int h = ((float)m.h/m.w) * 448;
if(h > 896){
h = 896;
w = ((float)m.w/m.h) * 896;
}
image sized = resize_image(m, w, h);
save_image(sized, window);
show_image(sized, window);
free_image(sized);
free_image(m); free_image(m);
} }

View File

@ -47,7 +47,7 @@ void train_imagenet(char *cfgfile, char *weightfile)
avg_loss = avg_loss*.9 + loss*.1; avg_loss = avg_loss*.9 + loss*.1;
printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), net.seen); printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), net.seen);
free_data(train); free_data(train);
if((i % 20000) == 0) net.learning_rate *= .1; if((i % 30000) == 0) net.learning_rate *= .1;
if(i%1000==0){ if(i%1000==0){
char buff[256]; char buff[256];
sprintf(buff, "/home/pjreddie/imagenet_backup/%s_%d.weights",base, i); sprintf(buff, "/home/pjreddie/imagenet_backup/%s_%d.weights",base, i);

View File

@ -125,13 +125,20 @@ float *get_network_output(network net)
float get_network_cost(network net) float get_network_cost(network net)
{ {
if(net.layers[net.n-1].type == COST){ int i;
return net.layers[net.n-1].output[0]; float sum = 0;
int count = 0;
for(i = 0; i < net.n; ++i){
if(net.layers[net.n-1].type == COST){
sum += net.layers[net.n-1].output[0];
++count;
}
if(net.layers[net.n-1].type == DETECTION){
sum += net.layers[net.n-1].cost[0];
++count;
}
} }
if(net.layers[net.n-1].type == DETECTION){ return sum/count;
return net.layers[net.n-1].cost[0];
}
return 0;
} }
int get_predicted_class_network(network net) int get_predicted_class_network(network net)
@ -184,9 +191,9 @@ void backward_network(network net, network_state state)
float train_network_datum(network net, float *x, float *y) float train_network_datum(network net, float *x, float *y)
{ {
#ifdef GPU #ifdef GPU
if(gpu_index >= 0) return train_network_datum_gpu(net, x, y); if(gpu_index >= 0) return train_network_datum_gpu(net, x, y);
#endif #endif
network_state state; network_state state;
state.input = x; state.input = x;
state.delta = 0; state.delta = 0;