void WadImageCache::cache_image(WadImageDescriptor& desc, int width, int height, SDL_Surface *image) { std::string name = retrieve_name(desc, width, height, true); if (!name.empty()) { autosave_cache(); return; } SDL_Surface *resized_image = NULL; if (image) { resized_image = resize_image(image, width, height); } else { image = image_from_desc(desc); if (image) { resized_image = resize_image(image, width, height); SDL_FreeSurface(image); } } if (!resized_image) return; name = add_to_cache(cache_key_t(desc, width, height), resized_image); SDL_FreeSurface(resized_image); }
void draw_label(image a, int r, int c, image label, image prob_label, const float *rgb) { float ratio = (float) label.w / label.h; int h = label.h; int w = ratio * h; image rl = resize_image(label, w, h); if (r - h >= 0) r = r - h; float ratiop = (float) prob_label.w / prob_label.h; int hp = prob_label.h; int wp = ratiop * hp; image rpl = resize_image(prob_label, wp, hp); int i, j, k; for(j = 0; j < h && j + r < a.h; ++j){ for(i = 0; i < w && i + c < a.w; ++i){ for(k = 0; k < label.c; ++k){ float val = get_pixel(rl, i, j, k); set_pixel(a, i+c+50, j+r, k, rgb[k] * val); } } } for(j = 0; j < hp && j + r < a.h; ++j){ for(i = 0; i < wp && i + c < a.w; ++i){ for(k = 0; k < prob_label.c; ++k){ float val = get_pixel(rpl, i, j, k); set_pixel(a, i+c, j+r, k, rgb[k] * val); } } } free_image(rl); free_image(rpl); }
SDL_Surface *WadImageCache::get_image(WadImageDescriptor& desc, int width, int height, SDL_Surface *image) { SDL_Surface *surface = retrieve_image(desc, width, height); if (surface) return surface; if (image) { surface = resize_image(image, width, height); } else { image = image_from_desc(desc); if (image) { surface = resize_image(image, width, height); SDL_FreeSurface(image); } } if (surface) { add_to_cache(cache_key_t(desc, width, height), surface); } return surface; }
static bool create_ios_launch_images(const char *dir, export_config *conf) { size_t len; stbi_uc *img_data; int width, height; if (conf->launch_image == NULL) { width = 1024; height = 1024; len = width * height * 4; img_data = (stbi_uc*)malloc(len); memset(img_data, 255, len); } else { void *data = am_read_file(conf->launch_image, &len); int components = 4; stbi_set_flip_vertically_on_load(0); img_data = stbi_load_from_memory((stbi_uc const *)data, len, &width, &height, &components, 4); free(data); if (img_data == NULL) return false; } if (!resize_image(img_data, width, height, dir, "Default.png", 320, 480) || !resize_image(img_data, width, height, dir, "*****@*****.**", 640, 960) || !resize_image(img_data, width, height, dir, "*****@*****.**", 640, 1136) || !resize_image(img_data, width, height, dir, "*****@*****.**", 750, 1334) || !resize_image(img_data, width, height, dir, "*****@*****.**", 1242, 2208) || !resize_image(img_data, width, height, dir, "Default-Landscape@2x~ipad.png", 2048, 1536) || !resize_image(img_data, width, height, dir, "Default-Landscape~ipad.png", 1024, 768) || !resize_image(img_data, width, height, dir, "Default-Portrait@2x~ipad.png", 1536, 2048) || !resize_image(img_data, width, height, dir, "Default-Portrait~ipad.png", 768, 1024) ) return false; free(img_data); return true; }
/* * do prediction *@param[in]: yoloctx, context *@param[in]: filename, input picture *@param[in]: thresh, threshold for probability x confidence level *@param[out]: predictions, store detected objects */ void yoloPredict(context_param_yolo_t *yoloctx, char *filename, float thresh, yoloPredictions *predictions) { printf("YOLO predict\n"); int nwidth = yoloctx->_nwidth; int nheight = yoloctx->_nheight; int side = yoloctx->_grid.grids; int classes = yoloctx->_grid.classes; int bbs = yoloctx->_grid.bbs; int sqrt = yoloctx->_sqrt; float nms = yoloctx->_nms; image im = load_image_color(filename, 0, 0); image sized = resize_image(im, nwidth, nheight); resetData(yoloctx); float *x = sized.data; float *fpredictions = network_predict(yoloctx->_net, x); float **probs = yoloctx->_grid.probs; box *boxes = yoloctx->_grid.boxes; convertDetections(fpredictions, classes, bbs, sqrt, side, 1, 1, thresh, probs, boxes, 0); if (nms) do_nms_sort(boxes, probs, side*side*bbs, classes, nms); convertResults(im.w, im.h, side*side*bbs, thresh, boxes, probs, class_names, 20, predictions); //free(predictions); free_image(sized); free_image(im); }
void yolo_net_predict(network *pNet, char *imgfilename, char * resfile, float thresh){ detection_layer l = pNet->layers[pNet->n-1]; clock_t time; char buff[256]; // char *input = buff; int j; float nms=.5; box *boxes = calloc(l.side*l.side*l.n, sizeof(box)); float **probs = calloc(l.side*l.side*l.n, sizeof(float *)); for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(l.classes, sizeof(float *)); image im = load_image_color(imgfilename,0,0); image resized = resize_image(im, pNet->w, pNet->h); float *X = resized.data; time=clock(); float *predictions = network_predict(*pNet, X); free_image(im); printf("%s: Predicted in %f seconds.\n", imgfilename, sec(clock()-time)); FILE *ofp = fopen(resfile, "w"); if (ofp == NULL) {fprintf(stderr, "Can't open output file %s!\n",resfile); exit(1);} //convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, demo_thresh, probs, boxes, 0); convert_print_yolo_detections(ofp, predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); //if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); fclose(ofp); }
void CAntimonyDlg::showImage(IplImage *image, UINT ID) // ID 是Picture Control控件的ID号 { CvSize ImgSize; IplImage *theimg; ImgSize.width = picrect.Width(); ImgSize.height = picrect.Height(); theimg = cvCreateImage(ImgSize, IPL_DEPTH_8U, 3); IplImage *image2 = image; resize_image(image2, theimg); CDC *pDC = GetDlgItem(ID)->GetDC(); HDC hDC = pDC->GetSafeHdc(); int rw = picrect.right - picrect.left; int rh = picrect.bottom - picrect.top; int iw = theimg->width; int ih = theimg->height; int tx = (int)(rw - iw) / 2; int ty = (int)(rh - ih) / 2; SetRect(picrect, tx, ty, tx + iw, ty + ih); CvvImage cimg; cimg.CopyOf(theimg); // 复制图片 cimg.DrawToHDC(hDC, &picrect); // 将图片绘制到显示控件的指定区域内 ReleaseDC(pDC); }
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh) { int show_flag = 1; list *options = read_data_cfg(datacfg); char *name_list = option_find_str(options, "names", "data/names.list"); char **names = get_labels(name_list); image **alphabet = load_alphabet(); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; int j; float nms=.4; 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 sized = resize_image(im, net.w, net.h); layer l = net.layers[net.n-1]; box *boxes = calloc(l.w*l.h*l.n, sizeof(box)); float **probs = calloc(l.w*l.h*l.n, sizeof(float *)); for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(l.classes + 1, sizeof(float *)); float *X = sized.data; time=clock(); network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); get_region_boxes(l, 1, 1, thresh, probs, boxes, 0, 0, hier_thresh); if (l.softmax_tree && nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms); else if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms); draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, names, alphabet, l.classes, show_flag); save_image(im, "predictions"); show_image(im, "predictions"); free_image(im); free_image(sized); free(boxes); free_ptrs((void **)probs, l.w*l.h*l.n); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
void *fetch_in_thread(void *ptr) { in = get_image_from_stream(cap); if(!in.data){ error("Stream closed."); } in_s = resize_image(in, net.w, net.h); return 0; }
float *network_predict_image(network *net, image im) { //image imr = letterbox_image(im, net->w, net->h); image imr = resize_image(im, net->w, net->h); set_batch_network(net, 1); float *p = network_predict(*net, imr.data); free_image(imr); return p; }
void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } detection_layer l = net.layers[net.n-1]; set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; int j; float nms=.5; box *boxes = calloc(l.side*l.side*l.n, sizeof(box)); float **probs = calloc(l.side*l.side*l.n, sizeof(float *)); for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(l.classes, sizeof(float *)); 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 sized = resize_image(im, net.w, net.h); float *X = sized.data; time=clock(); float *predictions = network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); //draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, voc_labels, 20); draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, 0, 20); show_image(im, "predictions"); save_image(im, "predictions"); show_image(sized, "resized"); free_image(im); free_image(sized); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
image load_image(char *filename, int w, int h, int c) { #ifdef OPENCV image out = load_image_cv(filename, c); #else image out = load_image_stb(filename, c); #endif if((h && w) && (h != out.h || w != out.w)){ image resized = resize_image(out, w, h); free_image(out); out = resized; } return out; }
void test_writing(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); clock_t time; 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); resize_network(net, im.w, im.h); printf("%d %d %d\n", im.h, im.w, im.c); float *X = im.data; time=clock(); network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); image pred = get_network_image(net); image upsampled = resize_image(pred, im.w, im.h); image thresh = threshold_image(upsampled, .5); pred = thresh; show_image(pred, "prediction"); show_image(im, "orig"); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif free_image(upsampled); free_image(thresh); free_image(im); if (filename) break; } }
void test_resize(char *filename) { image im = load_image(filename, 0,0, 3); float mag = mag_array(im.data, im.w*im.h*im.c); printf("L2 Norm: %f\n", mag); image gray = grayscale_image(im); image sat2 = copy_image(im); saturate_image(sat2, 2); image sat5 = copy_image(im); saturate_image(sat5, .5); image exp2 = copy_image(im); exposure_image(exp2, 2); image exp5 = copy_image(im); exposure_image(exp5, .5); #ifdef GPU image r = resize_image(im, im.w, im.h); image black = make_image(im.w*2 + 3, im.h*2 + 3, 9); image black2 = make_image(im.w, im.h, 3); float *r_gpu = cuda_make_array(r.data, r.w*r.h*r.c); float *black_gpu = cuda_make_array(black.data, black.w*black.h*black.c); float *black2_gpu = cuda_make_array(black2.data, black2.w*black2.h*black2.c); shortcut_gpu(3, r.w, r.h, 1, r_gpu, black.w, black.h, 3, black_gpu); //flip_image(r); //shortcut_gpu(3, r.w, r.h, 1, r.data, black.w, black.h, 3, black.data); shortcut_gpu(3, black.w, black.h, 3, black_gpu, black2.w, black2.h, 1, black2_gpu); cuda_pull_array(black_gpu, black.data, black.w*black.h*black.c); cuda_pull_array(black2_gpu, black2.data, black2.w*black2.h*black2.c); show_image_layers(black, "Black"); show_image(black2, "Recreate"); #endif show_image(im, "Original"); show_image(gray, "Gray"); show_image(sat2, "Saturation-2"); show_image(sat5, "Saturation-.5"); show_image(exp2, "Exposure-2"); show_image(exp5, "Exposure-.5"); #ifdef OPENCV cvWaitKey(0); #endif }
float *get_image_dct( const IplImage *src, const int nSub, int &featLen ) { IplImage *img = get_gray( src ); IplImage *subImage = resize_image( img, nSub ); IplImage *dctImage = cvCloneImage( subImage ); cvDCT( subImage, dctImage, CV_DXT_FORWARD ); // print_32f_image( dctImage ); // float *data = ( float * )( dctImage->imageData ); // qsort( data + 1, nSub * nSub - 1, sizeof( float ), cmp ); // print_32f_image( dctImage ); int subDct = 6; featLen = subDct * subDct - 1; int index = 0; float *feature = new float[ featLen ]; /*copy only 35 points of dcts*/ for ( int y = 0; y < subDct; y++ ) { for ( int x = 0; x < subDct; x++ ) { if ( x == 0 && y == 0 ) continue; feature[ index++ ] = pixval32f( dctImage, x, y ); } } /*print the feature without sort*/ // print_dct_feature( feature, subDct ); qsort( feature, featLen, sizeof( float ), cmp ); //sort( feature, feature + featLen ); /*print the feature after sort*/ //print_dct_feature( feature, subDct ); for ( int i = 0; i < featLen; i++ ) { printf( "%.3f\t", feature[ i ] ); } std::cout << endl; show_image( img, "gray" ); cvReleaseImage( &img ); cvReleaseImage( &subImage ); cvReleaseImage( &dctImage ); return feature; }
void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh) { image **alphabet = load_alphabet(); network *net = load_network(cfgfile, weightfile, 0); layer l = net->layers[net->n-1]; set_batch_network(net, 1); srand(2222222); float nms = .4; clock_t time; 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 sized = resize_image(im, net->w, net->h); float *X = sized.data; time=clock(); network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); int nboxes = 0; detection *dets = get_network_boxes(net, 1, 1, thresh, 0, 0, 0, &nboxes); if (nms) do_nms_sort(dets, l.side*l.side*l.n, l.classes, nms); draw_detections(im, dets, l.side*l.side*l.n, thresh, coco_classes, alphabet, 80); save_image(im, "prediction"); show_image(im, "predictions"); free_detections(dets, nboxes); free_image(im); free_image(sized); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
void demo_art(char *cfgfile, char *weightfile, int cam_index) { #ifdef OPENCV network *net = load_network(cfgfile, weightfile, 0); set_batch_network(net, 1); srand(2222222); void * cap = open_video_stream(0, cam_index, 0,0,0); char *window = "ArtJudgementBot9000!!!"; if(!cap) error("Couldn't connect to webcam.\n"); int i; int idx[] = {37, 401, 434}; int n = sizeof(idx)/sizeof(idx[0]); while(1){ image in = get_image_from_stream(cap); image in_s = resize_image(in, net->w, net->h); float *p = network_predict(net, in_s.data); printf("\033[2J"); printf("\033[1;1H"); float score = 0; for(i = 0; i < n; ++i){ float s = p[idx[i]]; if (s > score) score = s; } score = score; printf("I APPRECIATE THIS ARTWORK: %10.7f%%\n", score*100); printf("["); int upper = 30; for(i = 0; i < upper; ++i){ printf("%c", ((i+.5) < score*upper) ? 219 : ' '); } printf("]\n"); show_image(in, window, 1); free_image(in_s); free_image(in); } #endif }
void CAntimonyDlg::showVedio() { //VideoCapture capture("E:\\毕设\\锑粗选视频\\1\\20140608115045Sb.avi"); // for (int i = 0; i < 100; i++) // { // capture >> frame; CvSize ImgSize; IplImage *theimg; ImgSize.width = picrect.Width(); ImgSize.height = picrect.Height(); theimg = cvCreateImage(ImgSize, IPL_DEPTH_8U, 3); IplImage image = vedioframe; resize_image(&image, theimg); showImage(theimg, IDC_SHOWAVIS); // waitKey(30); // } }
void show_images(image *ims, int n, char *window) { image m = collapse_images_vert(ims, n); /* int w = 448; int h = ((float)m.h/m.w) * 448; if(h > 896){ h = 896; w = ((float)m.w/m.h) * 896; } image sized = resize_image(m, w, h); */ normalize_image(m); image sized = resize_image(m, m.w, m.h); save_image(sized, window); show_image(sized, window); free_image(sized); free_image(m); }
static uint8_t *frame_resize_image( mlt_frame frame, int owidth, int oheight, int bpp ) { // Get properties mlt_properties properties = MLT_FRAME_PROPERTIES( frame ); // Get the input image, width and height uint8_t *input = mlt_properties_get_data( properties, "image", NULL ); uint8_t *alpha = mlt_frame_get_alpha( frame ); int alpha_size = 0; mlt_properties_get_data( properties, "alpha", &alpha_size ); int iwidth = mlt_properties_get_int( properties, "width" ); int iheight = mlt_properties_get_int( properties, "height" ); // If width and height are correct, don't do anything if ( iwidth < owidth || iheight < oheight ) { uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" ); // Create the output image uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * bpp ); // Call the generic resize resize_image( output, owidth, oheight, input, iwidth, iheight, bpp ); // Now update the frame mlt_frame_set_image( frame, output, owidth * ( oheight + 1 ) * bpp, mlt_pool_release ); // We should resize the alpha too if ( alpha && alpha_size >= iwidth * iheight ) { alpha = resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value ); if ( alpha ) mlt_frame_set_alpha( frame, alpha, owidth * oheight, mlt_pool_release ); } // Return the output return output; } // No change, return input return input; }
void test_coco(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); clock_t time; 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 sized = resize_image(im, net.w, net.h); float *X = sized.data; time=clock(); float *predictions = network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); draw_coco(im, predictions, 7, "predictions"); free_image(im); free_image(sized); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
static void yolo_image2(const char *cfg, const char *weights, const char *filename, float thresh) { int i; for (i = 0; i < 20; ++i){ char buff[256]; sprintf(buff, "/home/amenmd/myfs/source-codes/oss/darknet/data/labels/%s.png", voc_names[i]); voc_labels[i] = load_image_color(buff, 0, 0); } network net = parse_network_cfg((char *)cfg); load_weights(&net, (char *)weights); detection_layer l = net.layers[net.n-1]; set_batch_network(&net, 1); int j; float nms=.5; box *boxes = (box *)calloc(l.side*l.side*l.n, sizeof(box)); float **probs = (float **)calloc(l.side*l.side*l.n, sizeof(float *)); for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = (float *)calloc(l.classes, sizeof(float *)); image im = load_image_color((char *)filename, 0, 0); image sized = resize_image(im, net.w, net.h); float *X = sized.data; float *predictions = network_predict(net, X); convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); //draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, voc_labels, 20); draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, 0, 20); show_image(im, "predictions"); save_image(im, "predictions"); show_image(sized, "resized"); free_image(im); free_image(sized); }
void test_writing(char *cfgfile, char *weightfile, char *outfile) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); clock_t time; char filename[256]; fgets(filename, 256, stdin); strtok(filename, "\n"); image im = load_image_color(filename, 0, 0); //image im = load_image_color("/home/pjreddie/darknet/data/figs/C02-1001-Figure-1.png", 0, 0); image sized = resize_image(im, 256, 256); printf("%d %d %d\n", im.h, im.w, im.c); float *X = sized.data; time=clock(); float *predictions = network_predict(net, X); printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time)); image pred = get_network_image(net); if (outfile) { printf("Save image as %s.png (shape: %d %d)\n", outfile, pred.w, pred.h); save_image(pred, outfile); } else { show_image(pred, "prediction"); #ifdef OPENCV cvWaitKey(0); #endif } free_image(im); free_image(sized); }
void validate_coco_recall(char *cfgfile, char *weightfile) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); 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_"; list *plist = get_paths("/home/pjreddie/data/voc/test/2007_test.txt"); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n-1]; int classes = l.classes; int square = l.sqrt; int side = l.side; int j, k; FILE **fps = calloc(classes, sizeof(FILE *)); for(j = 0; j < classes; ++j){ char buff[1024]; _snprintf(buff, 1024, "%s%s.txt", base, coco_classes[j]); fps[j] = fopen(buff, "w"); } box *boxes = calloc(side*side*l.n, sizeof(box)); float **probs = calloc(side*side*l.n, sizeof(float *)); for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *)); int m = plist->size; int i=0; float thresh = .001; int nms = 0; float iou_thresh = .5; float nms_thresh = .5; int total = 0; int correct = 0; int proposals = 0; float avg_iou = 0; for(i = 0; i < m; ++i){ char *path = paths[i]; image orig = load_image_color(path, 0, 0); image sized = resize_image(orig, net.w, net.h); char *id = basecfg(path); float *predictions = network_predict(net, sized.data); convert_detections(predictions, classes, l.n, square, side, 1, 1, thresh, probs, boxes, 1); if (nms) do_nms(boxes, probs, side*side*l.n, 1, nms_thresh); char *labelpath = find_replace(path, "images", "labels"); labelpath = find_replace(labelpath, "JPEGImages", "labels"); labelpath = find_replace(labelpath, ".jpg", ".txt"); labelpath = find_replace(labelpath, ".JPEG", ".txt"); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); for(k = 0; k < side*side*l.n; ++k){ if(probs[k][0] > thresh){ ++proposals; } } for (j = 0; j < num_labels; ++j) { ++total; box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float best_iou = 0; for(k = 0; k < side*side*l.n; ++k){ float iou = box_iou(boxes[k], t); if(probs[k][0] > thresh && iou > best_iou){ best_iou = iou; } } avg_iou += best_iou; if(best_iou > iou_thresh){ ++correct; } } fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total); free(id); free_image(orig); free_image(sized); } }
void validate_detector_recall(char *cfgfile, char *weightfile) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); srand(time(0)); list *plist = get_paths("data/voc.2007.test"); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n-1]; int classes = l.classes; int j, k; box *boxes = calloc(l.w*l.h*l.n, sizeof(box)); float **probs = calloc(l.w*l.h*l.n, sizeof(float *)); for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(classes, sizeof(float *)); int m = plist->size; int i=0; float thresh = .001; float iou_thresh = .5; float nms = .4; int total = 0; int correct = 0; int proposals = 0; float avg_iou = 0; for(i = 0; i < m; ++i){ char *path = paths[i]; image orig = load_image_color(path, 0, 0); image sized = resize_image(orig, net.w, net.h); char *id = basecfg(path); network_predict(net, sized.data); get_region_boxes(l, 1, 1, thresh, probs, boxes, 1, 0, .5); if (nms) do_nms(boxes, probs, l.w*l.h*l.n, 1, nms); char labelpath[4096]; find_replace(path, "images", "labels", labelpath); find_replace(labelpath, "JPEGImages", "labels", labelpath); find_replace(labelpath, ".jpg", ".txt", labelpath); find_replace(labelpath, ".JPEG", ".txt", labelpath); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); for(k = 0; k < l.w*l.h*l.n; ++k){ if(probs[k][0] > thresh){ ++proposals; } } for (j = 0; j < num_labels; ++j) { ++total; box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float best_iou = 0; for(k = 0; k < l.w*l.h*l.n; ++k){ float iou = box_iou(boxes[k], t); if(probs[k][0] > thresh && iou > best_iou){ best_iou = iou; } } avg_iou += best_iou; if(best_iou > iou_thresh){ ++correct; } } fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total); free(id); free_image(orig); free_image(sized); } }
void *fetch_in_thread(void *ptr) { in = get_image_from_stream(cap); in_s = resize_image(in, net.w, net.h); return 0; }
void validate_yolo_classify(char *datacfg, char *cfgfile, char *weightfile) { list *options = read_data_cfg(datacfg); //char *train_list = option_find_str(options, "train", "data/train_list.txt"); //char *test_list = option_find_str(options, "test", "data/test_list.txt"); char *valid_list = option_find_str(options, "valid", "data/valid_list.txt"); //char *backup_directory = option_find_str(options, "backup", "/backup/"); //char *label_list = option_find_str(options, "labels", "data/labels_list.txt"); //int classes = option_find_int(options, "classes", 2); //char **labels = get_labels(label_list); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); 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_"; //list *plist = get_paths("data/voc.2007.test"); list *plist = get_paths(valid_list); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n-1]; int classes = l.classes; int square = l.sqrt; int side = l.side; int j, k; FILE **fps = calloc(classes, sizeof(FILE *)); for(j = 0; j < classes; ++j){ char buff[1024]; snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]); fps[j] = fopen(buff, "w"); } box *boxes = calloc(side*side*l.n, sizeof(box)); float **probs = calloc(side*side*l.n, sizeof(float *)); for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *)); int m = plist->size; int i=0; //float thresh = .001; float thresh = .2; float iou_thresh = .5; //float nms = 0; float nms = 0.5; int total = 0; int correct = 0; int class_correct = 0; int proposals = 0; float avg_iou = 0; for(i = 0; i < m; ++i){ char *path = paths[i]; image orig = load_image_color(path, 0, 0); image sized = resize_image(orig, net.w, net.h); char *id = basecfg(path); float *predictions = network_predict(net, sized.data); convert_yolo_detections(predictions, classes, l.n, square, side, 1, 1, thresh, probs, boxes, 0); //if (nms) do_nms(boxes, probs, side*side*l.n, 1, nms); if (nms) do_nms(boxes, probs, side*side*l.n, classes, nms); char *labelpath = find_replace(path, "images", "labels"); labelpath = find_replace(labelpath, "JPEGImages", "labels"); labelpath = find_replace(labelpath, ".jpg", ".txt"); labelpath = find_replace(labelpath, ".JPEG", ".txt"); labelpath = find_replace(labelpath, ".bmp", ".txt"); labelpath = find_replace(labelpath, ".dib", ".txt"); labelpath = find_replace(labelpath, ".jpe", ".txt"); labelpath = find_replace(labelpath, ".jp2", ".txt"); labelpath = find_replace(labelpath, ".png", ".txt"); labelpath = find_replace(labelpath, ".pbm", ".txt"); labelpath = find_replace(labelpath, ".pgm", ".txt"); labelpath = find_replace(labelpath, ".ppm", ".txt"); labelpath = find_replace(labelpath, ".sr", ".txt"); labelpath = find_replace(labelpath, ".ras", ".txt"); labelpath = find_replace(labelpath, ".tiff", ".txt"); labelpath = find_replace(labelpath, ".tif", ".txt"); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); for(k = 0; k < side*side*l.n; ++k){ int class = max_index(probs[k], classes); float prob = probs[k][class]; //fprintf(stderr, "path=%s\t\tk=%d\tprob=%f\tclass=%d\n", path, k, prob, class); if(prob > thresh){ ++proposals; } } for (j = 0; j < num_labels; ++j) { ++total; box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float best_iou = 0; int pre_class = -1; for(k = 0; k < side*side*l.n; ++k){ float iou = box_iou(boxes[k], t); int class = max_index(probs[k], classes); float prob = probs[k][class]; //fprintf(stderr, "path=%s\t\tk=%d\tprob=%f\tclass=%d\n", path, k, prob, class); if(prob > thresh && iou > best_iou){ best_iou = iou; pre_class = class; } } avg_iou += best_iou; if(best_iou > iou_thresh){ ++correct; } if(pre_class == truth[j].id){ ++class_correct; } //fprintf(stderr, "true_class=%d\tpre_class=%d\n", truth[j].id, pre_class); } fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\t\tClassify:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total, 100.*class_correct/total); free(id); free_image(orig); free_image(sized); }
void optimize_picture(network *net, image orig, int max_layer, float scale, float rate, float thresh, int norm) { //scale_image(orig, 2); //translate_image(orig, -1); net->n = max_layer + 1; int dx = rand()%16 - 8; int dy = rand()%16 - 8; int flip = rand()%2; image crop = crop_image(orig, dx, dy, orig.w, orig.h); image im = resize_image(crop, (int)(orig.w * scale), (int)(orig.h * scale)); if(flip) flip_image(im); resize_network(net, im.w, im.h); layer_t last = net->layers[net->n-1]; //net->layers[net->n - 1].activation = LINEAR; image delta = make_image(im.w, im.h, im.c); NETWORK_STATE(state); #ifdef GPU state.input = cuda_make_array(im.data, im.w*im.h*im.c); state.delta = cuda_make_array(im.data, im.w*im.h*im.c); forward_network_gpu(*net, state); copy_ongpu(last.outputs, last.output_gpu, 1, last.delta_gpu, 1); cuda_pull_array(last.delta_gpu, last.delta, last.outputs); calculate_loss(last.delta, last.delta, last.outputs, thresh); cuda_push_array(last.delta_gpu, last.delta, last.outputs); backward_network_gpu(*net, state); cuda_pull_array(state.delta, delta.data, im.w*im.h*im.c); cuda_free(state.input); cuda_free(state.delta); #else state.input = im.data; state.delta = delta.data; forward_network(*net, state); fltcpy(last.delta, last.output, last.outputs); calculate_loss(last.output, last.delta, last.outputs, thresh); backward_network(*net, state); #endif if(flip) flip_image(delta); //normalize_array(delta.data, delta.w*delta.h*delta.c); image resized = resize_image(delta, orig.w, orig.h); image out = crop_image(resized, -dx, -dy, orig.w, orig.h); /* image g = grayscale_image(out); free_image(out); out = g; */ //rate = rate / abs_mean(out.data, out.w*out.h*out.c); if(norm) normalize_array(out.data, out.w*out.h*out.c); fltaddmul(orig.data, out.data, orig.w * orig.h * orig.c, rate); /* normalize_array(orig.data, orig.w*orig.h*orig.c); scale_image(orig, sqrt(var)); translate_image(orig, mean); */ //translate_image(orig, 1); //scale_image(orig, .5); //normalize_image(orig); constrain_image(orig); free_image(crop); free_image(im); free_image(delta); free_image(resized); free_image(out); }
void run_nightmare(int argc, char **argv) { srand(0); if(argc < 4){ fprintf(stderr, "usage: %s %s [cfg] [weights] [image] [layer] [options! (optional)]\n", argv[0], argv[1]); return; } char *cfg = argv[2]; char *weights = argv[3]; char *input = argv[4]; int max_layer = atoi(argv[5]); int range = find_int_arg(argc, argv, "-range", 1); int norm = find_int_arg(argc, argv, "-norm", 1); int rounds = find_int_arg(argc, argv, "-rounds", 1); int iters = find_int_arg(argc, argv, "-iters", 10); int octaves = find_int_arg(argc, argv, "-octaves", 4); float zoom = find_float_arg(argc, argv, "-zoom", 1.); float rate = find_float_arg(argc, argv, "-rate", .04); float thresh = find_float_arg(argc, argv, "-thresh", 1.); float rotate = find_float_arg(argc, argv, "-rotate", 0); float momentum = find_float_arg(argc, argv, "-momentum", .9); float lambda = find_float_arg(argc, argv, "-lambda", .01); char *prefix = find_char_arg(argc, argv, "-prefix", 0); int reconstruct = find_arg(argc, argv, "-reconstruct"); int smooth_size = find_int_arg(argc, argv, "-smooth", 1); network net = parse_network_cfg(cfg); load_weights(&net, weights); char *cfgbase = basecfg(cfg); char *imbase = basecfg(input); set_batch_network(&net, 1); image im = load_image_color(input, 0, 0); if(0){ float scale = 1; if(im.w > 512 || im.h > 512){ if(im.w > im.h) scale = 512.0/im.w; else scale = 512.0/im.h; } image resized = resize_image(im, scale*im.w, scale*im.h); free_image(im); im = resized; } float *features = 0; image update; if (reconstruct){ resize_network(&net, im.w, im.h); int zz = 0; network_predict(net, im.data); image out_im = get_network_image(net); image crop = crop_image(out_im, zz, zz, out_im.w-2*zz, out_im.h-2*zz); //flip_image(crop); image f_im = resize_image(crop, out_im.w, out_im.h); free_image(crop); printf("%d features\n", out_im.w*out_im.h*out_im.c); im = resize_image(im, im.w, im.h); f_im = resize_image(f_im, f_im.w, f_im.h); features = f_im.data; int i; for(i = 0; i < 14*14*512; ++i){ features[i] += rand_uniform(-.19, .19); } free_image(im); im = make_random_image(im.w, im.h, im.c); update = make_image(im.w, im.h, im.c); } int e; int n; for(e = 0; e < rounds; ++e){ fprintf(stderr, "Iteration: "); fflush(stderr); for(n = 0; n < iters; ++n){ fprintf(stderr, "%d, ", n); fflush(stderr); if(reconstruct){ reconstruct_picture(net, features, im, update, rate, momentum, lambda, smooth_size, 1); //if ((n+1)%30 == 0) rate *= .5; show_image(im, "reconstruction"); #ifdef OPENCV cvWaitKey(10); #endif }else{ int layer = max_layer + rand()%range - range/2; int octave = rand()%octaves; optimize_picture(&net, im, layer, 1/pow(1.33333333, octave), rate, thresh, norm); } } fprintf(stderr, "done\n"); if(0){ image g = grayscale_image(im); free_image(im); im = g; } char buff[256]; if (prefix){ sprintf(buff, "%s/%s_%s_%d_%06d",prefix, imbase, cfgbase, max_layer, e); }else{ sprintf(buff, "%s_%s_%d_%06d",imbase, cfgbase, max_layer, e); } printf("%d %s\n", e, buff); save_image(im, buff); //show_image(im, buff); //cvWaitKey(0); if(rotate){ image rot = rotate_image(im, rotate); free_image(im); im = rot; } image crop = crop_image(im, im.w * (1. - zoom)/2., im.h * (1.-zoom)/2., im.w*zoom, im.h*zoom); image resized = resize_image(crop, im.w, im.h); free_image(im); free_image(crop); im = resized; } }
void validate_yolo_recall(char *cfg, char *weights) { network *net = load_network(cfg, weights, 0); set_batch_network(net, 1); 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_"; list *plist = get_paths("data/voc.2007.test"); char **paths = (char **)list_to_array(plist); layer l = net->layers[net->n-1]; int classes = l.classes; int side = l.side; int j, k; FILE **fps = calloc(classes, sizeof(FILE *)); for(j = 0; j < classes; ++j){ char buff[1024]; snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]); fps[j] = fopen(buff, "w"); } int m = plist->size; int i=0; float thresh = .001; float iou_thresh = .5; float nms = 0; int total = 0; int correct = 0; int proposals = 0; float avg_iou = 0; for(i = 0; i < m; ++i){ char *path = paths[i]; image orig = load_image_color(path, 0, 0); image sized = resize_image(orig, net->w, net->h); char *id = basecfg(path); network_predict(net, sized.data); int nboxes = 0; detection *dets = get_network_boxes(net, orig.w, orig.h, thresh, 0, 0, 1, &nboxes); if (nms) do_nms_obj(dets, side*side*l.n, 1, nms); char labelpath[4096]; find_replace(path, "images", "labels", labelpath); find_replace(labelpath, "JPEGImages", "labels", labelpath); find_replace(labelpath, ".jpg", ".txt", labelpath); find_replace(labelpath, ".JPEG", ".txt", labelpath); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); for(k = 0; k < side*side*l.n; ++k){ if(dets[k].objectness > thresh){ ++proposals; } } for (j = 0; j < num_labels; ++j) { ++total; box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float best_iou = 0; for(k = 0; k < side*side*l.n; ++k){ float iou = box_iou(dets[k].bbox, t); if(dets[k].objectness > thresh && iou > best_iou){ best_iou = iou; } } avg_iou += best_iou; if(best_iou > iou_thresh){ ++correct; } } fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total); free_detections(dets, nboxes); free(id); free_image(orig); free_image(sized); } free_network( net ); }