void test_dice(char *cfgfile, char *weightfile, char *filename) { network * net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(net, weightfile); } set_batch_network(net, 1); srand(2222222); int i = 0; char **names = dice_labels; char buff[256]; char *input = buff; int indexes[6]; 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, net->w, net->h); float *X = im.data; float *predictions = network_predict(net, X); top_predictions(net, 6, indexes); for(i = 0; i < 6; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } free_image(im); if (filename) break; } }
void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename) { 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"); int 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; int size = net.w; 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 = resize_min(im, size); 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); top_predictions(net, top, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < top; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } if(r.data != im.data) free_image(r); free_image(im); if (filename) break; } }
void test_tag(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); int i = 0; char **names = get_labels("data/tags.txt"); clock_t time; int indexes[10]; char buff[256]; char *input = buff; int size = net.w; 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 = resize_min(im, size); 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); top_predictions(net, 10, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < 10; ++i){ int index = indexes[i]; printf("%.1f%%: %s\n", predictions[index]*100, names[index]); } if(r.data != im.data) free_image(r); free_image(im); if (filename) break; } }
void test_captcha(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); int i = 0; char **names = get_labels("/data/captcha/reimgs.labels.list"); char buff[256]; char *input = buff; int indexes[26]; 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, net.w, net.h); float *X = im.data; float *predictions = network_predict(net, X); top_predictions(net, 26, indexes); //printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < 26; ++i){ int index = indexes[i]; if(i != 0) printf(", "); printf("%s %f", names[index], predictions[index]); } printf("\n"); fflush(stdout); free_image(im); if (filename) break; } }
void test_imagenet(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile, 1); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); int i = 0; char **names = get_labels("data/shortnames.txt"); clock_t time; int indexes[10]; 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, 256, 256); float *X = im.data; time=clock(); float *predictions = network_predict(net, X); top_predictions(net, 10, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < 10; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } free_image(im); if (filename) break; } }
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 gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename) { #ifdef OPENCV int bad_cats[] = {218, 539, 540, 1213, 1501, 1742, 1911, 2415, 4348, 19223, 368, 369, 370, 1133, 1200, 1306, 2122, 2301, 2537, 2823, 3179, 3596, 3639, 4489, 5107, 5140, 5289, 6240, 6631, 6762, 7048, 7171, 7969, 7984, 7989, 8824, 8927, 9915, 10270, 10448, 13401, 15205, 18358, 18894, 18895, 19249, 19697}; 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("Threat Detection", CV_WINDOW_NORMAL); cvResizeWindow("Threat Detection", 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, "Threat Detection"); float *predictions = network_predict(net, in_s.data); top_predictions(net, top, indexes); printf("\033[2J"); printf("\033[1;1H"); int threat = 0; for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){ int index = bad_cats[i]; if(predictions[index] > .01){ printf("Threat Detected!\n"); threat = 1; break; } } if(!threat) printf("Scanning...\n"); for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){ int index = bad_cats[i]; if(predictions[index] > .01){ printf("%s\n", 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 threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename) { #ifdef OPENCV float threat = 0; float roll = .2; 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("Threat", CV_WINDOW_NORMAL); //cvResizeWindow("Threat", 512, 512); float fps = 0; int i; int count = 0; while(1){ ++count; struct timeval tval_before, tval_after, tval_result; gettimeofday(&tval_before, NULL); image in = get_image_from_stream(cap); if(!in.data) break; image in_s = resize_image(in, net->w, net->h); image out = in; int x1 = out.w / 20; int y1 = out.h / 20; int x2 = 2*x1; int y2 = out.h - out.h/20; int border = .01*out.h; int h = y2 - y1 - 2*border; int w = x2 - x1 - 2*border; float *predictions = network_predict(net, in_s.data); float curr_threat = 0; if(1){ curr_threat = predictions[0] * 0 + predictions[1] * .6 + predictions[2]; } else { curr_threat = predictions[218] + predictions[539] + predictions[540] + predictions[368] + predictions[369] + predictions[370]; } threat = roll * curr_threat + (1-roll) * threat; draw_box_width(out, x2 + border, y1 + .02*h, x2 + .5 * w, y1 + .02*h + border, border, 0,0,0); if(threat > .97) { draw_box_width(out, x2 + .5 * w + border, y1 + .02*h - 2*border, x2 + .5 * w + 6*border, y1 + .02*h + 3*border, 3*border, 1,0,0); } draw_box_width(out, x2 + .5 * w + border, y1 + .02*h - 2*border, x2 + .5 * w + 6*border, y1 + .02*h + 3*border, .5*border, 0,0,0); draw_box_width(out, x2 + border, y1 + .42*h, x2 + .5 * w, y1 + .42*h + border, border, 0,0,0); if(threat > .57) { draw_box_width(out, x2 + .5 * w + border, y1 + .42*h - 2*border, x2 + .5 * w + 6*border, y1 + .42*h + 3*border, 3*border, 1,1,0); } draw_box_width(out, x2 + .5 * w + border, y1 + .42*h - 2*border, x2 + .5 * w + 6*border, y1 + .42*h + 3*border, .5*border, 0,0,0); draw_box_width(out, x1, y1, x2, y2, border, 0,0,0); for(i = 0; i < threat * h ; ++i){ float ratio = (float) i / h; float r = (ratio < .5) ? (2*(ratio)) : 1; float g = (ratio < .5) ? 1 : 1 - 2*(ratio - .5); draw_box_width(out, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0); } top_predictions(net, top, indexes); char buff[256]; sprintf(buff, "/home/pjreddie/tmp/threat_%06d", count); //save_image(out, buff); 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]); } if(1){ show_image(out, "Threat"); cvWaitKey(10); } free_image(in_s); free_image(in); 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 try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int layer_num) { network *net = load_network(cfgfile, weightfile, 0); 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"); int 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 orig = load_image_color(input, 0, 0); image r = resize_min(orig, 256); image im = crop_image(r, (r.w - 224 - 1)/2 + 1, (r.h - 224 - 1)/2 + 1, 224, 224); float mean[] = {0.48263312050943, 0.45230225481413, 0.40099074308742}; float std[] = {0.22590347483426, 0.22120921437787, 0.22103996251583}; float var[3]; var[0] = std[0]*std[0]; var[1] = std[1]*std[1]; var[2] = std[2]*std[2]; normalize_cpu(im.data, mean, var, 1, 3, im.w*im.h); float *X = im.data; time=clock(); float *predictions = network_predict(net, X); layer l = net->layers[layer_num]; for(i = 0; i < l.c; ++i){ if(l.rolling_mean) printf("%f %f %f\n", l.rolling_mean[i], l.rolling_variance[i], l.scales[i]); } #ifdef GPU cuda_pull_array(l.output_gpu, l.output, l.outputs); #endif for(i = 0; i < l.outputs; ++i){ printf("%f\n", l.output[i]); } /* printf("\n\nWeights\n"); for(i = 0; i < l.n*l.size*l.size*l.c; ++i){ printf("%f\n", l.filters[i]); } printf("\n\nBiases\n"); for(i = 0; i < l.n; ++i){ printf("%f\n", l.biases[i]); } */ top_predictions(net, top, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < top; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } free_image(im); if (filename) break; } }