| | |
| | | #ifdef OPENCV |
| | | #include "opencv2/highgui/highgui_c.h" |
| | | #endif |
| | | static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90}; |
| | | |
| | | void train_detector(char *datacfg, char *cfgfile, char *weightfile, int clear) |
| | | void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear) |
| | | { |
| | | list *options = read_data_cfg(datacfg); |
| | | char *train_images = option_find_str(options, "train", "data/train.list"); |
| | |
| | | char *base = basecfg(cfgfile); |
| | | printf("%s\n", base); |
| | | float avg_loss = -1; |
| | | network net = parse_network_cfg(cfgfile); |
| | | network *nets = calloc(ngpus, sizeof(network)); |
| | | |
| | | srand(time(0)); |
| | | int seed = rand(); |
| | | int i; |
| | | for(i = 0; i < ngpus; ++i){ |
| | | srand(seed); |
| | | #ifdef GPU |
| | | cuda_set_device(gpus[i]); |
| | | #endif |
| | | nets[i] = parse_network_cfg(cfgfile); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | load_weights(&nets[i], weightfile); |
| | | } |
| | | if(clear) *net.seen = 0; |
| | | if(clear) *nets[i].seen = 0; |
| | | nets[i].learning_rate *= ngpus; |
| | | } |
| | | srand(time(0)); |
| | | network net = nets[0]; |
| | | |
| | | int imgs = net.batch * net.subdivisions * ngpus; |
| | | printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); |
| | | int imgs = net.batch*net.subdivisions; |
| | | int i = *net.seen/imgs; |
| | | data train, buffer; |
| | | |
| | | layer l = net.layers[net.n - 1]; |
| | |
| | | clock_t time; |
| | | //while(i*imgs < N*120){ |
| | | while(get_current_batch(net) < net.max_batches){ |
| | | i += 1; |
| | | time=clock(); |
| | | pthread_join(load_thread, 0); |
| | | train = buffer; |
| | |
| | | printf("Loaded: %lf seconds\n", sec(clock()-time)); |
| | | |
| | | time=clock(); |
| | | float loss = train_network(net, train); |
| | | float loss = 0; |
| | | #ifdef GPU |
| | | if(ngpus == 1){ |
| | | loss = train_network(net, train); |
| | | } else { |
| | | loss = train_networks(nets, ngpus, train, 4); |
| | | } |
| | | #else |
| | | loss = train_network(net, train); |
| | | #endif |
| | | if (avg_loss < 0) avg_loss = loss; |
| | | avg_loss = avg_loss*.9 + loss*.1; |
| | | |
| | | 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); |
| | | i = get_current_batch(net); |
| | | printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); |
| | | if(i%1000==0 || (i < 1000 && i%100 == 0)){ |
| | | char buff[256]; |
| | | sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); |
| | |
| | | save_weights(net, buff); |
| | | } |
| | | |
| | | |
| | | static int get_coco_image_id(char *filename) |
| | | { |
| | | char *p = strrchr(filename, '_'); |
| | | return atoi(p+1); |
| | | } |
| | | |
| | | static void print_cocos(FILE *fp, char *image_path, box *boxes, float **probs, int num_boxes, int classes, int w, int h) |
| | | { |
| | | int i, j; |
| | | int image_id = get_coco_image_id(image_path); |
| | | for(i = 0; i < num_boxes; ++i){ |
| | | float xmin = boxes[i].x - boxes[i].w/2.; |
| | | float xmax = boxes[i].x + boxes[i].w/2.; |
| | | float ymin = boxes[i].y - boxes[i].h/2.; |
| | | float ymax = boxes[i].y + boxes[i].h/2.; |
| | | |
| | | if (xmin < 0) xmin = 0; |
| | | if (ymin < 0) ymin = 0; |
| | | if (xmax > w) xmax = w; |
| | | if (ymax > h) ymax = h; |
| | | |
| | | float bx = xmin; |
| | | float by = ymin; |
| | | float bw = xmax - xmin; |
| | | float bh = ymax - ymin; |
| | | |
| | | for(j = 0; j < classes; ++j){ |
| | | if (probs[i][j]) fprintf(fp, "{\"image_id\":%d, \"category_id\":%d, \"bbox\":[%f, %f, %f, %f], \"score\":%f},\n", image_id, coco_ids[j], bx, by, bw, bh, probs[i][j]); |
| | | } |
| | | } |
| | | } |
| | | |
| | | void print_detector_detections(FILE **fps, char *id, box *boxes, float **probs, int total, int classes, int w, int h) |
| | | { |
| | | int i, j; |
| | |
| | | list *options = read_data_cfg(datacfg); |
| | | char *valid_images = option_find_str(options, "valid", "data/train.list"); |
| | | char *name_list = option_find_str(options, "names", "data/names.list"); |
| | | char *prefix = option_find_str(options, "results", "results"); |
| | | char **names = get_labels(name_list); |
| | | |
| | | |
| | | char buff[1024]; |
| | | int coco = option_find_int_quiet(options, "coco", 0); |
| | | FILE *coco_fp = 0; |
| | | if(coco){ |
| | | snprintf(buff, 1024, "%s/coco_results.json", prefix); |
| | | coco_fp = fopen(buff, "w"); |
| | | fprintf(coco_fp, "[\n"); |
| | | } |
| | | |
| | | network net = parse_network_cfg(cfgfile); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | |
| | | fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); |
| | | srand(time(0)); |
| | | |
| | | char *base = "results/comp4_det_test_"; |
| | | char *base = "comp4_det_test_"; |
| | | list *plist = get_paths(valid_images); |
| | | char **paths = (char **)list_to_array(plist); |
| | | |
| | |
| | | int j; |
| | | FILE **fps = calloc(classes, sizeof(FILE *)); |
| | | for(j = 0; j < classes; ++j){ |
| | | char buff[1024]; |
| | | snprintf(buff, 1024, "%s%s.txt", base, names[j]); |
| | | snprintf(buff, 1024, "%s/%s%s.txt", prefix, base, names[j]); |
| | | fps[j] = fopen(buff, "w"); |
| | | } |
| | | box *boxes = calloc(l.w*l.h*l.n, sizeof(box)); |
| | |
| | | int h = val[t].h; |
| | | get_region_boxes(l, w, h, thresh, probs, boxes, 0); |
| | | if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, classes, nms); |
| | | if(coco_fp){ |
| | | print_cocos(coco_fp, path, boxes, probs, l.w*l.h*l.n, classes, w, h); |
| | | }else{ |
| | | print_detector_detections(fps, id, boxes, probs, l.w*l.h*l.n, classes, w, h); |
| | | } |
| | | free(id); |
| | | free_image(val[t]); |
| | | free_image(val_resized[t]); |
| | |
| | | for(j = 0; j < classes; ++j){ |
| | | fclose(fps[j]); |
| | | } |
| | | if(coco_fp){ |
| | | fseek(coco_fp, -2, SEEK_CUR); |
| | | fprintf(coco_fp, "\n]\n"); |
| | | fclose(coco_fp); |
| | | } |
| | | fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start)); |
| | | } |
| | | |
| | |
| | | fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); |
| | | return; |
| | | } |
| | | char *gpu_list = find_char_arg(argc, argv, "-gpus", 0); |
| | | int *gpus = 0; |
| | | int gpu = 0; |
| | | int ngpus = 0; |
| | | if(gpu_list){ |
| | | printf("%s\n", gpu_list); |
| | | int len = strlen(gpu_list); |
| | | ngpus = 1; |
| | | int i; |
| | | for(i = 0; i < len; ++i){ |
| | | if (gpu_list[i] == ',') ++ngpus; |
| | | } |
| | | gpus = calloc(ngpus, sizeof(int)); |
| | | for(i = 0; i < ngpus; ++i){ |
| | | gpus[i] = atoi(gpu_list); |
| | | gpu_list = strchr(gpu_list, ',')+1; |
| | | } |
| | | } else { |
| | | gpu = gpu_index; |
| | | gpus = &gpu; |
| | | ngpus = 1; |
| | | } |
| | | |
| | | int clear = find_arg(argc, argv, "-clear"); |
| | | |
| | | char *datacfg = argv[3]; |
| | |
| | | char *weights = (argc > 5) ? argv[5] : 0; |
| | | char *filename = (argc > 6) ? argv[6]: 0; |
| | | if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh); |
| | | else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, clear); |
| | | else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear); |
| | | else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights); |
| | | else if(0==strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights); |
| | | else if(0==strcmp(argv[2], "demo")) { |