Go to the documentation of this file.00001 #include "network.h"
00002 #include "detection_layer.h"
00003 #include "cost_layer.h"
00004 #include "utils.h"
00005 #include "parser.h"
00006 #include "box.h"
00007
00008 #ifdef OPENCV
00009 #include "opencv2/highgui/highgui_c.h"
00010 #endif
00011
00012 void train_swag(char *cfgfile, char *weightfile)
00013 {
00014 char *train_images = "data/voc.0712.trainval";
00015 char *backup_directory = "/home/pjreddie/backup/";
00016 srand(time(0));
00017 char *base = basecfg(cfgfile);
00018 printf("%s\n", base);
00019 float avg_loss = -1;
00020 network net = parse_network_cfg(cfgfile);
00021 if(weightfile){
00022 load_weights(&net, weightfile);
00023 }
00024 printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00025 int imgs = net.batch*net.subdivisions;
00026 int i = *net.seen/imgs;
00027 data train, buffer;
00028
00029 layer l = net.layers[net.n - 1];
00030
00031 int side = l.side;
00032 int classes = l.classes;
00033 float jitter = l.jitter;
00034
00035 list *plist = get_paths(train_images);
00036
00037 char **paths = (char **)list_to_array(plist);
00038
00039 load_args args = {0};
00040 args.w = net.w;
00041 args.h = net.h;
00042 args.paths = paths;
00043 args.n = imgs;
00044 args.m = plist->size;
00045 args.classes = classes;
00046 args.jitter = jitter;
00047 args.num_boxes = side;
00048 args.d = &buffer;
00049 args.type = REGION_DATA;
00050
00051 pthread_t load_thread = load_data_in_thread(args);
00052 clock_t time;
00053
00054 while(get_current_batch(net) < net.max_batches){
00055 i += 1;
00056 time=clock();
00057 pthread_join(load_thread, 0);
00058 train = buffer;
00059 load_thread = load_data_in_thread(args);
00060
00061 printf("Loaded: %lf seconds\n", sec(clock()-time));
00062
00063 time=clock();
00064 float loss = train_network(net, train);
00065 if (avg_loss < 0) avg_loss = loss;
00066 avg_loss = avg_loss*.9 + loss*.1;
00067
00068 printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs);
00069 if(i%1000==0 || i == 600){
00070 char buff[256];
00071 sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
00072 save_weights(net, buff);
00073 }
00074 free_data(train);
00075 }
00076 char buff[256];
00077 sprintf(buff, "%s/%s_final.weights", backup_directory, base);
00078 save_weights(net, buff);
00079 }
00080
00081 void run_swag(int argc, char **argv)
00082 {
00083 if(argc < 4){
00084 fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
00085 return;
00086 }
00087
00088 char *cfg = argv[3];
00089 char *weights = (argc > 4) ? argv[4] : 0;
00090 if(0==strcmp(argv[2], "train")) train_swag(cfg, weights);
00091 }