python yolo-v2 设计批处理程序对训练生成的权重文件进行自动化批量测试,并输出结果到指定txt文件
生活随笔
收集整理的這篇文章主要介紹了
python yolo-v2 设计批处理程序对训练生成的权重文件进行自动化批量测试,并输出结果到指定txt文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 項目背景
在yolo-v2生成D:\dahuangfeng\darknet-master\build\darknet\x64\backup路徑下的權重文件
后,我們先需要先修改D:\dahuangfeng\darknet-master\build\darknet\x64路徑下的test_obj.cmd文件,將操作對象修改成我們需要測試的權重文件,
然后運行test_obj.cmd程序,程序加載權重文件并開始運行,
運行完畢后,取輸出最后一行作為我們測試需要的結果,將其取樣并寫入指定txt文件并保存。
然后按照上述步驟循環測試后面的權重文件。太多太累太麻煩,于是便想寫一個批處理程序。 - 預設步驟
1、建立列表,將權重文件序號填寫進去。
2、讀取列表,打開test_obj.cmd文件,運用正則化參數修改保存序號。
3、運行test_obj.cmd文件,取最后行輸出,用正則化參數提取數據并寫入輸出文件。
在visual studio中,打開detector.c文件,直接在validate_detector_recall()函數中加入“創建Test_Weight_Files.txt文件并將輸出結果寫入”的代碼:
void validate_detector_recall(char *datacfg, char *cfgfile, char *weightfile) {network net = parse_network_cfg_custom(cfgfile, 1);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 *options = read_data_cfg(datacfg);char *valid_images = option_find_str(options, "valid", "data/train.txt");list *plist = get_paths(valid_images);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 = .2;// .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);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);find_replace(labelpath, ".png", ".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);}//創建Test_Weight_Files.txt文件并將輸出結果寫入FILE *fp = fopen("./Test_Weight_Files.txt", "a");char* token = strtok(weightfile, "/");token = strtok(NULL, "/");fprintf(fp, "%s\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", token, (float)proposals / (i + 1), avg_iou * 100 / total, 100.*correct / total);fclose(fp); }然后修改我們的test_obj.cmd文件的信息:
20200309:生成可用以下python代碼(其實可用代碼提取生成的權重文件中數字,沒做,莎嗶了。。。)
# -*- coding: utf-8 -*- """ @File : 20200308_dontla_auto_evaluate.py @Time : 2020/3/8 23:43 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import osepoch = ['100', '200', '300', '400', '500', '600', '700', '800', '900','1000', '2000', '3000', '4000', '5000', '6000', '7000', '8000', '9000','10000', '11000', '12000', '13000', '14000', '15000', '16000', '17000', '18000', '19000','20000', '21000', '22000', '23000', '24000', '25000', '26000', '27000', '28000', '29000','30000', '31000', '32000', '33000', '34000', '35000', '36000', '37000', '38000', '39000','40000', '41000', '42000', '43000', '44000', '45000', 'final'] file = open('./dontla_result.txt', 'w', encoding='utf-8') for i in epoch:command = '.\darknet.exe detector recall data/obj.data yolo-obj.cfg backup/yolo-obj_{}.weights'.format(i)print(command)# print(command)# ..\darknet.exe detector recall data/obj.data yolo-obj.cfg backup/yolo-obj_100.weights# ..\darknet.exe detector recall data/obj.data yolo-obj.cfg backup/yolo-obj_200.weights# ...# result = os.popen(command)# res = result.read()# for line in res.splitlines():# print(line)# file.write(''.join([i, '\t', result_list])) file.close()
運行test_obj.cmd便可批量生成測試結果到Test_Weight_Files.txt文件中,全部輸出完畢后,會自行停止,
- 結果:雖然不如設想那樣運用python去直接執行我們的操作,而是直接在c語言程序的函數中去增加處理代碼,但還是順利地實現了我們需要的功能。
參考文章1:c語言中的制表符\t與空格
參考文章2:用c語言創建一個txt文件,并且寫入數據
參考文章3:C語言字符串處理的一些函數strok,strstr, strchr,strsub
參考文章4:C\C++編程中:相對路徑+絕對路徑
總結
以上是生活随笔為你收集整理的python yolo-v2 设计批处理程序对训练生成的权重文件进行自动化批量测试,并输出结果到指定txt文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 程序开发以及推荐机制设计
- 下一篇: 深度学习 点积的概念