YOLOv3使用笔记——darknet分类批处理

    主要实现类似于caffe的classification功能,修改predict_classifier做批处理分类图片,根据top1准确率将一批混合的图片按不同类别分类到不同的文件夹,需要一个比较好的分类器。做图片归类时考虑到保存图片比较慢,在c中调用了终端的mv命令。

 

1.分类批处理

 

char *GetFilename(char *p)
{ 
    static char name[256]={""};
    char *q = strrchr(p,'/') + 1;
    strncpy(name,q,256);
    return name;
}

 

void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top)
{
    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");
    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;
    char outfile[2048];
    char terminal[2048];
    char **filelist = get_labels("classification.txt");
    //char *filename = filelist[]
    int count = 0;
    while(filelist[count] != NULL)
   {
        char *filename = filelist[count];
        if(filename)
       {
            strncpy(input, filename, 256);
            //strncpy(out, filename+9, 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);
        //image r = resize_min(im, 320);
        //printf("%d %d\n", r.w, r.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));

	double max_predictions=0;
	int max_index=0;
        for(i = 0; i < top; ++i)
       {
            int index = indexes[i];
	//printf("%d \n",index);
	//printf("%s \n",names[index]);
	//printf("%f \n",predictions[index]);
	    if(predictions[index]>max_predictions)
	   {
	       max_predictions=predictions[index];
	       max_index=index;
	   }
            //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]);
        }
	//printf("%d \n",max_index);
	//printf("%s \n",names[max_index]);
	//printf("%f \n",predictions[max_index]);

	if (max_index == 3)
       {
	sprintf(outfile,"/SSD2TB/yolov3/darknet/open_person/%s",GetFilename(filename));
    sprintf(terminal,"mv /SSD2TB/yolov3/darknet/%s %s",input,outfile);
    system(terminal);
       }
	if (max_index == 2)
       {
	sprintf(outfile,"/SSD2TB/yolov3/darknet/open_nobody/%s",GetFilename(filename));
    sprintf(terminal,"mv /SSD2TB/yolov3/darknet/%s %s",input,outfile);
    system(terminal);
       }
        if (max_index == 1)
       {
	sprintf(outfile,"/SSD2TB/yolov3/darknet/closed_person/%s",GetFilename(filename));
    sprintf(terminal,"mv /SSD2TB/yolov3/darknet/%s %s",input,outfile);
    system(terminal);
       }
        if (max_index == 0)
       {
	sprintf(outfile,"/SSD2TB/yolov3/darknet/closed_nobody/%s",GetFilename(filename));
    sprintf(terminal,"mv /SSD2TB/yolov3/darknet/%s %s",input,outfile);
    system(terminal);
       }

        if(r.data != im.data) 
	free_image(r);
        free_image(im);
        //if (filename) break;
        count++;
    }
}

 

其中classification.txt为darknet根目录下待处理的图片文件夹列表,写图片的相对路径。例classify/a.jpg

index、names[index]、predictions[index]分别为类别、类别名、分类准确率

 

调用终端命令:

sprintf(terminal,"mv /SSD2TB/yolov3/darknet/%s %s",input,outfile);
system(terminal);

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值