From 55fbdd1007488b2e98dbe4b07cc798954b5e165f Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Tue, 22 Sep 2015 23:29:03 -0700 Subject: [PATCH] fixes to image --- cfg/writing.cfg | 2 +- src/image.c | 4 ++-- src/writing.c | 50 +++++++++++++++++++++++++++---------------------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/cfg/writing.cfg b/cfg/writing.cfg index 030deeb6..ebee3edd 100644 --- a/cfg/writing.cfg +++ b/cfg/writing.cfg @@ -4,7 +4,7 @@ subdivisions=2 height=256 width=256 channels=3 -learning_rate=0.0000001 +learning_rate=0.000001 momentum=0.9 decay=0.0005 seen=0 diff --git a/src/image.c b/src/image.c index 2811cb37..861d8a2a 100644 --- a/src/image.c +++ b/src/image.c @@ -499,7 +499,7 @@ image threshold_image(image im, float thresh) int i; image t = make_image(im.w, im.h, im.c); for(i = 0; i < im.w*im.h*im.c; ++i){ - t.data[i] = im.data[i]>0 ? 1 : 0; + t.data[i] = im.data[i]>thresh ? 1 : 0; } return t; } @@ -622,7 +622,7 @@ image resize_image(image im, int w, int h) float val = (1-dy) * get_pixel(part, c, iy, k); set_pixel(resized, c, r, k, val); } - if(r == h-1) continue; + if(r == h-1 || im.h == 1) continue; for(c = 0; c < w; ++c){ float val = dy * get_pixel(part, c, iy+1, k); add_pixel(resized, c, r, k, val); diff --git a/src/writing.c b/src/writing.c index 02d3fcce..71dd53b8 100644 --- a/src/writing.c +++ b/src/writing.c @@ -69,13 +69,18 @@ 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(*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); + } } } @@ -102,7 +107,7 @@ void test_writing(char *cfgfile, char *weightfile, char *outfile) printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time)); image pred = get_network_image(net); - image t = threshold_image(pred, .2); + image t = threshold_image(pred, .5); free_image(pred); pred = t; @@ -110,28 +115,29 @@ void test_writing(char *cfgfile, char *weightfile, char *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"); #ifdef OPENCV - cvWaitKey(0); - cvDestroyAllWindows(); + cvWaitKey(0); + cvDestroyAllWindows(); #endif - } + } - free_image(im); - free_image(sized); + free_image(im); + free_image(sized); } 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 *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); }