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
width=256
channels=3
learning_rate=0.000001
learning_rate=0.00000001
momentum=0.9
decay=0.0005
seen=0
[crop]
crop_height=256
crop_width=256
flip=0
angle=0
saturation=1
exposure=1
[convolutional]
filters=32
size=3
stride=1
pad=1
activation=leaky
[convolutional]
filters=32
size=3
stride=1
pad=1
activation=ramp
activation=leaky
[convolutional]
filters=32
size=3
stride=1
pad=1
activation=ramp
[convolutional]
filters=32
size=3
stride=1
pad=1
activation=ramp
activation=leaky
[convolutional]
filters=1

View File

@ -106,7 +106,8 @@ void test_captcha(char *cfgfile, char *weightfile, char *filename)
srand(2222222);
int i = 0;
char **names = get_labels("/data/captcha/reimgs.labels.list");
char input[256];
char buff[256];
char *input = buff;
int indexes[26];
while(1){
if(filename){
@ -114,7 +115,8 @@ void test_captcha(char *cfgfile, char *weightfile, char *filename)
}else{
//printf("Enter Image Path: ");
//fflush(stdout);
fgets(input, 256, stdin);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
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);
srand(2222222);
clock_t time;
char input[256];
char buff[256];
char *input = buff;
while(1){
if(filename){
strncpy(input, filename, 256);
} else {
printf("Enter Image Path: ");
fflush(stdout);
fgets(input, 256, stdin);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
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;
}
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)
{
if (!state.truth) return;
@ -83,7 +94,7 @@ void forward_cost_layer_gpu(cost_layer l, network_state state)
if (l.cost_type == MASKED) {
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);
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);
void forward_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
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){
*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){
*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){
*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){
@ -578,14 +578,14 @@ pthread_t load_data_in_thread(load_args args)
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);
char **replace_paths = find_replace_paths(paths, n, ".png", "-label.png");
data d;
d.shallow = 0;
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);
int i;
for(i = 0; i < n; ++i) free(replace_paths[i]);

View File

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

View File

@ -76,7 +76,8 @@ void test_dice(char *cfgfile, char *weightfile, char *filename)
srand(2222222);
int i = 0;
char **names = dice_labels;
char input[256];
char buff[256];
char *input = buff;
int indexes[6];
while(1){
if(filename){
@ -84,7 +85,8 @@ void test_dice(char *cfgfile, char *weightfile, char *filename)
}else{
printf("Enter Image Path: ");
fflush(stdout);
fgets(input, 256, stdin);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
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;
char **names = get_labels("data/shortnames.txt");
clock_t time;
char input[256];
int indexes[10];
char buff[256];
char *input = buff;
while(1){
if(filename){
strncpy(input, filename, 256);
}else{
printf("Enter Image Path: ");
fflush(stdout);
fgets(input, 256, stdin);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
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;
net->w = w;
net->h = h;
int inputs = 0;
//fprintf(stderr, "Resizing to %d x %d...", w, h);
//fflush(stderr);
for (i = 0; i < net->n; ++i){
@ -343,9 +344,12 @@ int resize_network(network *net, int w, int h)
break;
}else if(l.type == NORMALIZATION){
resize_normalization_layer(&l, w, h);
}else if(l.type == COST){
resize_cost_layer(&l, inputs);
}else{
error("Cannot resize this type of layer");
}
inputs = l.outputs;
net->layers[i] = l;
w = l.out_w;
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);
srand(2222222);
clock_t time;
char input[256];
char buff[256];
char *input = buff;
while(1){
if(filename){
strncpy(input, filename, 256);
} else {
printf("Enter Image Path: ");
fflush(stdout);
fgets(input, 256, stdin);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
image im = load_image_color(input,0,0);

View File

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