void validate_classifier_full(char *datacfg, char *filename, char *weightfile) { int i, j; network net = parse_network_cfg(filename); set_batch_network(&net, 1); if(weightfile){ load_weights(&net, weightfile); } srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "labels", "data/labels.list"); char *valid_list = option_find_str(options, "valid", "data/train.list"); int classes = option_find_int(options, "classes", 2); int topk = option_find_int(options, "top", 1); char **labels = get_labels(label_list); list *plist = get_paths(valid_list); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); float avg_acc = 0; float avg_topk = 0; int *indexes = calloc(topk, sizeof(int)); int size = net.w; for(i = 0; i < m; ++i){ int class = -1; char *path = paths[i]; for(j = 0; j < classes; ++j){ if(strstr(path, labels[j])){ class = j; break; } } image im = load_image_color(paths[i], 0, 0); image resized = resize_min(im, size); resize_network(&net, resized.w, resized.h); //show_image(im, "orig"); //show_image(crop, "cropped"); //cvWaitKey(0); float *pred = network_predict(net, resized.data); if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1, 1); free_image(im); free_image(resized); top_k(pred, classes, topk, indexes); if(indexes[0] == class) avg_acc += 1; for(j = 0; j < topk; ++j){ if(indexes[j] == class) avg_topk += 1; } printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1)); } }
void get_region_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness, int *map) { int i; float *const predictions = l.output; #pragma omp parallel for for (i = 0; i < l.w*l.h; ++i){ int j, n; int row = i / l.w; int col = i % l.w; for(n = 0; n < l.n; ++n){ int index = i*l.n + n; int p_index = index * (l.classes + 5) + 4; float scale = predictions[p_index]; if(l.classfix == -1 && scale < .5) scale = 0; int box_index = index * (l.classes + 5); boxes[index] = get_region_box(predictions, l.biases, n, box_index, col, row, l.w, l.h); boxes[index].x *= w; boxes[index].y *= h; boxes[index].w *= w; boxes[index].h *= h; int class_index = index * (l.classes + 5) + 5; if(l.softmax_tree){ hierarchy_predictions(predictions + class_index, l.classes, l.softmax_tree, 0); int found = 0; if(map){ for(j = 0; j < 200; ++j){ float prob = scale*predictions[class_index+map[j]]; probs[index][j] = (prob > thresh) ? prob : 0; } } else { for(j = l.classes - 1; j >= 0; --j){ if(!found && predictions[class_index + j] > .5){ found = 1; } else { predictions[class_index + j] = 0; } float prob = predictions[class_index+j]; probs[index][j] = (scale > thresh) ? prob : 0; } } } else { for(j = 0; j < l.classes; ++j){ float prob = scale*predictions[class_index+j]; probs[index][j] = (prob > thresh) ? prob : 0; } } if(only_objectness){ probs[index][0] = scale; } } } }
void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); list *options = read_data_cfg(datacfg); char *name_list = option_find_str(options, "names", 0); if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list"); if(top == 0) top = option_find_int(options, "top", 1); int i = 0; char **names = get_labels(name_list); clock_t time; int *indexes = calloc(top, sizeof(int)); 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"); } image im = load_image_color(input, 0, 0); image r = letterbox_image(im, net.w, net.h); //resize_network(&net, r.w, r.h); //printf("%d %d\n", r.w, r.h); float *X = r.data; time=clock(); float *predictions = network_predict(net, X); if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 1, 1); top_k(predictions, net.outputs, top, indexes); fprintf(stderr, "%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < top; ++i){ int index = indexes[i]; //if(net.hierarchy) printf("%d, %s: %f, parent: %s \n",index, names[index], predictions[index], (net.hierarchy->parent[index] >= 0) ? names[net.hierarchy->parent[index]] : "Root"); //else printf("%s: %f\n",names[index], predictions[index]); printf("%5.2f%%: %s\n", predictions[index]*100, names[index]); } if(r.data != im.data) free_image(r); free_image(im); if (filename) break; } }
void get_region_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, float tree_thresh, int relative, detection *dets) { int i, j, n, z; float *predictions = l.output; if (l.batch == 2) { float *flip = l.output + l.outputs; for (j = 0; j < l.h; ++j) { for (i = 0; i < l.w / 2; ++i) { for (n = 0; n < l.n; ++n) { for (z = 0; z < l.classes + l.coords + 1; ++z) { int i1 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + i; int i2 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + (l.w - i - 1); float swap = flip[i1]; flip[i1] = flip[i2]; flip[i2] = swap; if (z == 0) { flip[i1] = -flip[i1]; flip[i2] = -flip[i2]; } } } } } for (i = 0; i < l.outputs; ++i) { l.output[i] = (l.output[i] + flip[i]) / 2.; } } for (i = 0; i < l.w*l.h; ++i) { int row = i / l.w; int col = i % l.w; for (n = 0; n < l.n; ++n) { int index = n*l.w*l.h + i; for (j = 0; j < l.classes; ++j) { dets[index].prob[j] = 0; } int obj_index = entry_index(l, 0, n*l.w*l.h + i, l.coords); int box_index = entry_index(l, 0, n*l.w*l.h + i, 0); int mask_index = entry_index(l, 0, n*l.w*l.h + i, 4); float scale = l.background ? 1 : predictions[obj_index]; dets[index].bbox = get_region_box(predictions, l.biases, n, box_index, col, row, l.w, l.h);// , l.w*l.h); dets[index].objectness = scale > thresh ? scale : 0; if (dets[index].mask) { for (j = 0; j < l.coords - 4; ++j) { dets[index].mask[j] = l.output[mask_index + j*l.w*l.h]; } } int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + !l.background); if (l.softmax_tree) { hierarchy_predictions(predictions + class_index, l.classes, l.softmax_tree, 0);// , l.w*l.h); if (map) { for (j = 0; j < 200; ++j) { int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + 1 + map[j]); float prob = scale*predictions[class_index]; dets[index].prob[j] = (prob > thresh) ? prob : 0; } } else { int j = hierarchy_top_prediction(predictions + class_index, l.softmax_tree, tree_thresh, l.w*l.h); dets[index].prob[j] = (scale > thresh) ? scale : 0; } } else { if (dets[index].objectness) { for (j = 0; j < l.classes; ++j) { int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + 1 + j); float prob = scale*predictions[class_index]; dets[index].prob[j] = (prob > thresh) ? prob : 0; } } } } } correct_region_boxes(dets, l.w*l.h*l.n, w, h, netw, neth, relative); }
void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename) { #ifdef OPENCV printf("Classifier Demo\n"); network *net = load_network(cfgfile, weightfile, 0); set_batch_network(net, 1); list *options = read_data_cfg(datacfg); srand(2222222); CvCapture * cap; if(filename){ cap = cvCaptureFromFile(filename); }else{ cap = cvCaptureFromCAM(cam_index); } int top = option_find_int(options, "top", 1); char *name_list = option_find_str(options, "names", 0); char **names = get_labels(name_list); int *indexes = calloc(top, sizeof(int)); if(!cap) error("Couldn't connect to webcam.\n"); cvNamedWindow("Classifier", CV_WINDOW_NORMAL); cvResizeWindow("Classifier", 512, 512); float fps = 0; int i; while(1){ struct timeval tval_before, tval_after, tval_result; gettimeofday(&tval_before, NULL); image in = get_image_from_stream(cap); image in_s = resize_image(in, net->w, net->h); show_image(in, "Classifier"); float *predictions = network_predict(net, in_s.data); if(net->hierarchy) hierarchy_predictions(predictions, net->outputs, net->hierarchy, 1, 1); top_predictions(net, top, indexes); printf("\033[2J"); printf("\033[1;1H"); printf("\nFPS:%.0f\n",fps); for(i = 0; i < top; ++i){ int index = indexes[i]; printf("%.1f%%: %s\n", predictions[index]*100, names[index]); } free_image(in_s); free_image(in); cvWaitKey(10); gettimeofday(&tval_after, NULL); timersub(&tval_after, &tval_before, &tval_result); float curr = 1000000.f/((long int)tval_result.tv_usec); fps = .9*fps + .1*curr; } #endif }
void validate_classifier_multi(char *datacfg, char *cfg, char *weights) { int i, j; network *net = load_network(cfg, weights, 0); set_batch_network(net, 1); srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "labels", "data/labels.list"); char *valid_list = option_find_str(options, "valid", "data/train.list"); int classes = option_find_int(options, "classes", 2); int topk = option_find_int(options, "top", 1); char **labels = get_labels(label_list); list *plist = get_paths(valid_list); //int scales[] = {224, 288, 320, 352, 384}; int scales[] = {224, 256, 288, 320}; int nscales = sizeof(scales)/sizeof(scales[0]); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); float avg_acc = 0; float avg_topk = 0; int *indexes = calloc(topk, sizeof(int)); for(i = 0; i < m; ++i){ int class = -1; char *path = paths[i]; for(j = 0; j < classes; ++j){ if(strstr(path, labels[j])){ class = j; break; } } float *pred = calloc(classes, sizeof(float)); image im = load_image_color(paths[i], 0, 0); for(j = 0; j < nscales; ++j){ image r = resize_max(im, scales[j]); resize_network(net, r.w, r.h); float *p = network_predict(net, r.data); if(net->hierarchy) hierarchy_predictions(p, net->outputs, net->hierarchy, 1 , 1); axpy_cpu(classes, 1, p, 1, pred, 1); flip_image(r); p = network_predict(net, r.data); axpy_cpu(classes, 1, p, 1, pred, 1); if(r.data != im.data) free_image(r); } free_image(im); top_k(pred, classes, topk, indexes); free(pred); if(indexes[0] == class) avg_acc += 1; for(j = 0; j < topk; ++j){ if(indexes[j] == class) avg_topk += 1; } printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1)); } }
void validate_classifier_10(char *datacfg, char *filename, char *weightfile) { int i, j; network *net = load_network(filename, weightfile, 0); set_batch_network(net, 1); srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "labels", "data/labels.list"); char *valid_list = option_find_str(options, "valid", "data/train.list"); int classes = option_find_int(options, "classes", 2); int topk = option_find_int(options, "top", 1); char **labels = get_labels(label_list); list *plist = get_paths(valid_list); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); float avg_acc = 0; float avg_topk = 0; int *indexes = calloc(topk, sizeof(int)); for(i = 0; i < m; ++i){ int class = -1; char *path = paths[i]; for(j = 0; j < classes; ++j){ if(strstr(path, labels[j])){ class = j; break; } } int w = net->w; int h = net->h; int shift = 32; image im = load_image_color(paths[i], w+shift, h+shift); image images[10]; images[0] = crop_image(im, -shift, -shift, w, h); images[1] = crop_image(im, shift, -shift, w, h); images[2] = crop_image(im, 0, 0, w, h); images[3] = crop_image(im, -shift, shift, w, h); images[4] = crop_image(im, shift, shift, w, h); flip_image(im); images[5] = crop_image(im, -shift, -shift, w, h); images[6] = crop_image(im, shift, -shift, w, h); images[7] = crop_image(im, 0, 0, w, h); images[8] = crop_image(im, -shift, shift, w, h); images[9] = crop_image(im, shift, shift, w, h); float *pred = calloc(classes, sizeof(float)); for(j = 0; j < 10; ++j){ float *p = network_predict(net, images[j].data); if(net->hierarchy) hierarchy_predictions(p, net->outputs, net->hierarchy, 1, 1); axpy_cpu(classes, 1, p, 1, pred, 1); free_image(images[j]); } free_image(im); top_k(pred, classes, topk, indexes); free(pred); if(indexes[0] == class) avg_acc += 1; for(j = 0; j < topk; ++j){ if(indexes[j] == class) avg_topk += 1; } printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1)); } }