writing.c
Go to the documentation of this file.
00001 #include "network.h"
00002 #include "utils.h"
00003 #include "parser.h"
00004 
00005 #ifdef OPENCV
00006 #include "opencv2/highgui/highgui_c.h"
00007 #endif
00008 
00009 void train_writing(char *cfgfile, char *weightfile)
00010 {
00011     char *backup_directory = "/home/pjreddie/backup/";
00012     srand(time(0));
00013     float avg_loss = -1;
00014     char *base = basecfg(cfgfile);
00015     printf("%s\n", base);
00016     network net = parse_network_cfg(cfgfile);
00017     if(weightfile){
00018         load_weights(&net, weightfile);
00019     }
00020     printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00021     int imgs = net.batch*net.subdivisions;
00022     list *plist = get_paths("figures.list");
00023     char **paths = (char **)list_to_array(plist);
00024     clock_t time;
00025     int N = plist->size;
00026     printf("N: %d\n", N);
00027     image out = get_network_image(net);
00028 
00029     data train, buffer;
00030 
00031     load_args args = {0};
00032     args.w = net.w;
00033     args.h = net.h;
00034     args.out_w = out.w;
00035     args.out_h = out.h;
00036     args.paths = paths;
00037     args.n = imgs;
00038     args.m = N;
00039     args.d = &buffer;
00040     args.type = WRITING_DATA;
00041 
00042     pthread_t load_thread = load_data_in_thread(args);
00043     int epoch = (*net.seen)/N;
00044     while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
00045         time=clock();
00046         pthread_join(load_thread, 0);
00047         train = buffer;
00048         load_thread = load_data_in_thread(args);
00049         printf("Loaded %lf seconds\n",sec(clock()-time));
00050 
00051         time=clock();
00052         float loss = train_network(net, train);
00053 
00054         /*
00055            image pred = float_to_image(64, 64, 1, out);
00056            print_image(pred);
00057          */
00058 
00059         /*
00060            image im = float_to_image(256, 256, 3, train.X.vals[0]);
00061            image lab = float_to_image(64, 64, 1, train.y.vals[0]);
00062            image pred = float_to_image(64, 64, 1, out);
00063            show_image(im, "image");
00064            show_image(lab, "label");
00065            print_image(lab);
00066            show_image(pred, "pred");
00067            cvWaitKey(0);
00068          */
00069 
00070         if(avg_loss == -1) avg_loss = loss;
00071         avg_loss = avg_loss*.9 + loss*.1;
00072         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);
00073         free_data(train);
00074         if(get_current_batch(net)%100 == 0){
00075             char buff[256];
00076             sprintf(buff, "%s/%s_batch_%d.weights", backup_directory, base, get_current_batch(net));
00077             save_weights(net, buff);
00078         }
00079         if(*net.seen/N > epoch){
00080             epoch = *net.seen/N;
00081             char buff[256];
00082             sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch);
00083             save_weights(net, buff);
00084         }
00085     }
00086 }
00087 
00088 void test_writing(char *cfgfile, char *weightfile, char *filename)
00089 {
00090     network net = parse_network_cfg(cfgfile);
00091     if(weightfile){
00092         load_weights(&net, weightfile);
00093     }
00094     set_batch_network(&net, 1);
00095     srand(2222222);
00096     clock_t time;
00097     char buff[256];
00098     char *input = buff;
00099     while(1){
00100         if(filename){
00101             strncpy(input, filename, 256);
00102         }else{
00103             printf("Enter Image Path: ");
00104             fflush(stdout);
00105             input = fgets(input, 256, stdin);
00106             if(!input) return;
00107             strtok(input, "\n");
00108         }
00109 
00110         image im = load_image_color(input, 0, 0);
00111         resize_network(&net, im.w, im.h);
00112         printf("%d %d %d\n", im.h, im.w, im.c);
00113         float *X = im.data;
00114         time=clock();
00115         network_predict(net, X);
00116         printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
00117         image pred = get_network_image(net);
00118 
00119         image upsampled = resize_image(pred, im.w, im.h);
00120         image thresh = threshold_image(upsampled, .5);
00121         pred = thresh;
00122 
00123         show_image(pred, "prediction");
00124         show_image(im, "orig");
00125 #ifdef OPENCV
00126         cvWaitKey(0);
00127         cvDestroyAllWindows();
00128 #endif
00129 
00130         free_image(upsampled);
00131         free_image(thresh);
00132         free_image(im);
00133         if (filename) break;
00134     }
00135 }
00136 
00137 void run_writing(int argc, char **argv)
00138 {
00139     if(argc < 4){
00140         fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
00141         return;
00142     }
00143 
00144     char *cfg = argv[3];
00145     char *weights = (argc > 4) ? argv[4] : 0;
00146     char *filename = (argc > 5) ? argv[5] : 0;
00147     if(0==strcmp(argv[2], "train")) train_writing(cfg, weights);
00148     else if(0==strcmp(argv[2], "test")) test_writing(cfg, weights, filename);
00149 }
00150 


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:31