more writing fixes

This commit is contained in:
Joseph Redmon 2015-09-23 14:13:43 -07:00
parent 55fbdd1007
commit f996bd59a6
13 changed files with 115 additions and 86 deletions

View File

@ -4,39 +4,31 @@ subdivisions=2
height=256 height=256
width=256 width=256
channels=3 channels=3
learning_rate=0.000001 learning_rate=0.00000001
momentum=0.9 momentum=0.9
decay=0.0005 decay=0.0005
seen=0 seen=0
[crop] [convolutional]
crop_height=256 filters=32
crop_width=256 size=3
flip=0 stride=1
angle=0 pad=1
saturation=1 activation=leaky
exposure=1
[convolutional] [convolutional]
filters=32 filters=32
size=3 size=3
stride=1 stride=1
pad=1 pad=1
activation=ramp activation=leaky
[convolutional] [convolutional]
filters=32 filters=32
size=3 size=3
stride=1 stride=1
pad=1 pad=1
activation=ramp activation=leaky
[convolutional]
filters=32
size=3
stride=1
pad=1
activation=ramp
[convolutional] [convolutional]
filters=1 filters=1

View File

@ -106,7 +106,8 @@ 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");
char input[256]; char buff[256];
char *input = buff;
int indexes[26]; int indexes[26];
while(1){ while(1){
if(filename){ if(filename){
@ -114,7 +115,8 @@ void test_captcha(char *cfgfile, char *weightfile, char *filename)
}else{ }else{
//printf("Enter Image Path: "); //printf("Enter Image Path: ");
//fflush(stdout); //fflush(stdout);
fgets(input, 256, stdin); input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n"); strtok(input, "\n");
} }
image im = load_image_color(input, net.w, net.h); image im = load_image_color(input, net.w, net.h);

View File

@ -495,14 +495,16 @@ void test_coco(char *cfgfile, char *weightfile, char *filename)
set_batch_network(&net, 1); set_batch_network(&net, 1);
srand(2222222); srand(2222222);
clock_t time; clock_t time;
char input[256]; char buff[256];
char *input = buff;
while(1){ while(1){
if(filename){ if(filename){
strncpy(input, filename, 256); strncpy(input, filename, 256);
} else { } else {
printf("Enter Image Path: "); printf("Enter Image Path: ");
fflush(stdout); fflush(stdout);
fgets(input, 256, stdin); input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n"); strtok(input, "\n");
} }
image im = load_image_color(input,0,0); image im = load_image_color(input,0,0);

View File

@ -45,6 +45,17 @@ cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float sca
return l; return l;
} }
void resize_cost_layer(cost_layer *l, int inputs)
{
l->inputs = inputs;
l->outputs = inputs;
l->delta = realloc(l->delta, inputs*l->batch*sizeof(float));
#ifdef GPU
cuda_free(l->delta_gpu);
l->delta_gpu = cuda_make_array(l->delta, inputs*l->batch);
#endif
}
void forward_cost_layer(cost_layer l, network_state state) void forward_cost_layer(cost_layer l, network_state state)
{ {
if (!state.truth) return; if (!state.truth) return;
@ -83,7 +94,7 @@ void forward_cost_layer_gpu(cost_layer l, network_state state)
if (l.cost_type == MASKED) { if (l.cost_type == MASKED) {
mask_ongpu(l.batch*l.inputs, state.input, SECRET_NUM, state.truth); mask_ongpu(l.batch*l.inputs, state.input, SECRET_NUM, state.truth);
} }
copy_ongpu(l.batch*l.inputs, state.truth, 1, l.delta_gpu, 1); copy_ongpu(l.batch*l.inputs, state.truth, 1, l.delta_gpu, 1);
axpy_ongpu(l.batch*l.inputs, -1, state.input, 1, l.delta_gpu, 1); axpy_ongpu(l.batch*l.inputs, -1, state.input, 1, l.delta_gpu, 1);

View File

@ -10,6 +10,7 @@ char *get_cost_string(COST_TYPE a);
cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale); cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale);
void forward_cost_layer(const cost_layer l, network_state state); void forward_cost_layer(const cost_layer l, network_state state);
void backward_cost_layer(const cost_layer l, network_state state); void backward_cost_layer(const cost_layer l, network_state state);
void resize_cost_layer(cost_layer *l, int inputs);
#ifdef GPU #ifdef GPU
void forward_cost_layer_gpu(cost_layer l, network_state state); void forward_cost_layer_gpu(cost_layer l, network_state state);

View File

@ -554,7 +554,7 @@ void *load_thread(void *ptr)
} else if (a.type == DETECTION_DATA){ } else if (a.type == DETECTION_DATA){
*a.d = load_data_detection(a.n, a.paths, a.m, a.classes, a.w, a.h, a.num_boxes, a.background); *a.d = load_data_detection(a.n, a.paths, a.m, a.classes, a.w, a.h, a.num_boxes, a.background);
} else if (a.type == WRITING_DATA){ } else if (a.type == WRITING_DATA){
*a.d = load_data_writing(a.paths, a.n, a.m, a.w, a.h, a.downsample); *a.d = load_data_writing(a.paths, a.n, a.m, a.w, a.h, a.out_w, a.out_h);
} else if (a.type == REGION_DATA){ } else if (a.type == REGION_DATA){
*a.d = load_data_region(a.n, a.paths, a.m, a.w, a.h, a.num_boxes, a.classes); *a.d = load_data_region(a.n, a.paths, a.m, a.w, a.h, a.num_boxes, a.classes);
} else if (a.type == COMPARE_DATA){ } else if (a.type == COMPARE_DATA){
@ -578,14 +578,14 @@ pthread_t load_data_in_thread(load_args args)
return thread; return thread;
} }
data load_data_writing(char **paths, int n, int m, int w, int h, int downsample) data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h)
{ {
if(m) paths = get_random_paths(paths, n, m); if(m) paths = get_random_paths(paths, n, m);
char **replace_paths = find_replace_paths(paths, n, ".png", "-label.png"); char **replace_paths = find_replace_paths(paths, n, ".png", "-label.png");
data d; data d;
d.shallow = 0; d.shallow = 0;
d.X = load_image_paths(paths, n, w, h); d.X = load_image_paths(paths, n, w, h);
d.y = load_image_paths_gray(replace_paths, n, w/downsample, h/downsample); d.y = load_image_paths_gray(replace_paths, n, out_w, out_h);
if(m) free(paths); if(m) free(paths);
int i; int i;
for(i = 0; i < n; ++i) free(replace_paths[i]); for(i = 0; i < n; ++i) free(replace_paths[i]);

View File

@ -37,7 +37,8 @@ typedef struct load_args{
char **labels; char **labels;
int h; int h;
int w; int w;
int downsample; int out_w;
int out_h;
int nh; int nh;
int nw; int nw;
int num_boxes; int num_boxes;
@ -69,7 +70,7 @@ box_label *read_boxes(char *filename, int *n);
data load_cifar10_data(char *filename); data load_cifar10_data(char *filename);
data load_all_cifar10(); data load_all_cifar10();
data load_data_writing(char **paths, int n, int m, int w, int h, int downsample); 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); list *get_paths(char *filename);
char **get_labels(char *filename); char **get_labels(char *filename);

View File

@ -76,7 +76,8 @@ void test_dice(char *cfgfile, char *weightfile, char *filename)
srand(2222222); srand(2222222);
int i = 0; int i = 0;
char **names = dice_labels; char **names = dice_labels;
char input[256]; char buff[256];
char *input = buff;
int indexes[6]; int indexes[6];
while(1){ while(1){
if(filename){ if(filename){
@ -84,7 +85,8 @@ void test_dice(char *cfgfile, char *weightfile, char *filename)
}else{ }else{
printf("Enter Image Path: "); printf("Enter Image Path: ");
fflush(stdout); fflush(stdout);
fgets(input, 256, stdin); input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n"); strtok(input, "\n");
} }
image im = load_image_color(input, net.w, net.h); image im = load_image_color(input, net.w, net.h);

View File

@ -152,15 +152,17 @@ void test_imagenet(char *cfgfile, char *weightfile, char *filename)
int i = 0; int i = 0;
char **names = get_labels("data/shortnames.txt"); char **names = get_labels("data/shortnames.txt");
clock_t time; clock_t time;
char input[256];
int indexes[10]; int indexes[10];
char buff[256];
char *input = buff;
while(1){ while(1){
if(filename){ if(filename){
strncpy(input, filename, 256); strncpy(input, filename, 256);
}else{ }else{
printf("Enter Image Path: "); printf("Enter Image Path: ");
fflush(stdout); fflush(stdout);
fgets(input, 256, stdin); input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n"); strtok(input, "\n");
} }
image im = load_image_color(input, 256, 256); image im = load_image_color(input, 256, 256);

View File

@ -330,6 +330,7 @@ int resize_network(network *net, int w, int h)
//if(w == net->w && h == net->h) return 0; //if(w == net->w && h == net->h) return 0;
net->w = w; net->w = w;
net->h = h; net->h = h;
int inputs = 0;
//fprintf(stderr, "Resizing to %d x %d...", w, h); //fprintf(stderr, "Resizing to %d x %d...", w, h);
//fflush(stderr); //fflush(stderr);
for (i = 0; i < net->n; ++i){ for (i = 0; i < net->n; ++i){
@ -343,9 +344,12 @@ int resize_network(network *net, int w, int h)
break; break;
}else if(l.type == NORMALIZATION){ }else if(l.type == NORMALIZATION){
resize_normalization_layer(&l, w, h); resize_normalization_layer(&l, w, h);
}else if(l.type == COST){
resize_cost_layer(&l, inputs);
}else{ }else{
error("Cannot resize this type of layer"); error("Cannot resize this type of layer");
} }
inputs = l.outputs;
net->layers[i] = l; net->layers[i] = l;
w = l.out_w; w = l.out_w;
h = l.out_h; h = l.out_h;

View File

@ -274,14 +274,16 @@ void test_swag(char *cfgfile, char *weightfile, char *filename, float thresh)
set_batch_network(&net, 1); set_batch_network(&net, 1);
srand(2222222); srand(2222222);
clock_t time; clock_t time;
char input[256]; char buff[256];
char *input = buff;
while(1){ while(1){
if(filename){ if(filename){
strncpy(input, filename, 256); strncpy(input, filename, 256);
} else { } else {
printf("Enter Image Path: "); printf("Enter Image Path: ");
fflush(stdout); fflush(stdout);
fgets(input, 256, stdin); input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n"); strtok(input, "\n");
} }
image im = load_image_color(input,0,0); image im = load_image_color(input,0,0);

View File

@ -25,16 +25,18 @@ void train_writing(char *cfgfile, char *weightfile)
clock_t time; clock_t time;
int N = plist->size; int N = plist->size;
printf("N: %d\n", N); printf("N: %d\n", N);
image out = get_network_image(net);
data train, buffer; data train, buffer;
load_args args = {0}; load_args args = {0};
args.w = net.w; args.w = net.w;
args.h = net.h; args.h = net.h;
args.out_w = out.w;
args.out_h = out.h;
args.paths = paths; args.paths = paths;
args.n = imgs; args.n = imgs;
args.m = N; args.m = N;
args.downsample = 1;
args.d = &buffer; args.d = &buffer;
args.type = WRITING_DATA; args.type = WRITING_DATA;
@ -51,9 +53,9 @@ void train_writing(char *cfgfile, char *weightfile)
float loss = train_network(net, train); float loss = train_network(net, train);
/* /*
image pred = float_to_image(64, 64, 1, out); image pred = float_to_image(64, 64, 1, out);
print_image(pred); print_image(pred);
*/ */
/* /*
image im = float_to_image(256, 256, 3, train.X.vals[0]); image im = float_to_image(256, 256, 3, train.X.vals[0]);
@ -69,22 +71,22 @@ void train_writing(char *cfgfile, char *weightfile)
if(avg_loss == -1) avg_loss = loss; if(avg_loss == -1) avg_loss = loss;
avg_loss = avg_loss*.9 + loss*.1; avg_loss = avg_loss*.9 + loss*.1;
printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen);
free_data(train); free_data(train);
if(get_current_batch(net)%100 == 0){ if(get_current_batch(net)%100 == 0){
char buff[256]; char buff[256];
sprintf(buff, "%s/%s_batch_%d.weights", backup_directory, base, get_current_batch(net)); sprintf(buff, "%s/%s_batch_%d.weights", backup_directory, base, get_current_batch(net));
save_weights(net, buff); save_weights(net, buff);
} }
if(*net.seen/N > epoch){ if(*net.seen/N > epoch){
epoch = *net.seen/N; epoch = *net.seen/N;
char buff[256]; char buff[256];
sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch); sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch);
save_weights(net, buff); save_weights(net, buff);
} }
} }
} }
void test_writing(char *cfgfile, char *weightfile, char *outfile) void test_writing(char *cfgfile, char *weightfile, char *filename)
{ {
network net = parse_network_cfg(cfgfile); network net = parse_network_cfg(cfgfile);
if(weightfile){ if(weightfile){
@ -93,51 +95,57 @@ void test_writing(char *cfgfile, char *weightfile, char *outfile)
set_batch_network(&net, 1); set_batch_network(&net, 1);
srand(2222222); srand(2222222);
clock_t time; clock_t time;
char filename[256]; char buff[256];
char *input = buff;
while(1){
if(filename){
strncpy(input, filename, 256);
}else{
printf("Enter Image Path: ");
fflush(stdout);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
fgets(filename, 256, stdin); image im = load_image_color(input, 0, 0);
strtok(filename, "\n"); resize_network(&net, im.w, im.h);
image im = load_image_color(filename, 0, 0); printf("%d %d %d\n", im.h, im.w, im.c);
//image im = load_image_color("/home/pjreddie/darknet/data/figs/C02-1001-Figure-1.png", 0, 0); float *X = im.data;
image sized = resize_image(im, net.w, net.h); time=clock();
printf("%d %d %d\n", im.h, im.w, im.c); network_predict(net, X);
float *X = sized.data; printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
time=clock(); image pred = get_network_image(net);
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
image pred = get_network_image(net);
image t = threshold_image(pred, .5); image upsampled = resize_image(pred, im.w, im.h);
free_image(pred); image thresh = threshold_image(upsampled, .5);
pred = t; pred = thresh;
if (outfile) {
printf("Save image as %s.png (shape: %d %d)\n", outfile, pred.w, pred.h);
save_image(pred, outfile);
} else {
show_image(sized, "orig");
show_image(pred, "prediction"); show_image(pred, "prediction");
show_image(im, "orig");
#ifdef OPENCV #ifdef OPENCV
cvWaitKey(0); cvWaitKey(0);
cvDestroyAllWindows(); cvDestroyAllWindows();
#endif #endif
}
free_image(im); free_image(upsampled);
free_image(sized); free_image(thresh);
free_image(im);
if (filename) break;
}
} }
void run_writing(int argc, char **argv) void run_writing(int argc, char **argv)
{ {
if(argc < 4){ if(argc < 4){
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
return; return;
} }
char *cfg = argv[3]; char *cfg = argv[3];
char *weights = (argc > 4) ? argv[4] : 0; char *weights = (argc > 4) ? argv[4] : 0;
char *outfile = (argc > 5) ? argv[5] : 0; char *filename = (argc > 5) ? argv[5] : 0;
if(0==strcmp(argv[2], "train")) train_writing(cfg, weights); if(0==strcmp(argv[2], "train")) train_writing(cfg, weights);
else if(0==strcmp(argv[2], "test")) test_writing(cfg, weights, outfile); else if(0==strcmp(argv[2], "test")) test_writing(cfg, weights, filename);
} }

View File

@ -290,14 +290,16 @@ void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh)
set_batch_network(&net, 1); set_batch_network(&net, 1);
srand(2222222); srand(2222222);
clock_t time; clock_t time;
char input[256]; char buff[256];
char *input = buff;
while(1){ while(1){
if(filename){ if(filename){
strncpy(input, filename, 256); strncpy(input, filename, 256);
} else { } else {
printf("Enter Image Path: "); printf("Enter Image Path: ");
fflush(stdout); fflush(stdout);
fgets(input, 256, stdin); input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n"); strtok(input, "\n");
} }
image im = load_image_color(input,0,0); image im = load_image_color(input,0,0);