yolo v3模型測試及測試結果轉化

2021-09-22 02:30:59 字數 4703 閱讀 9031

訓練完成生成模型後,進行模型測試。對測試集資料進行檢測,得到檢測結果

像製作訓練集時生產2019_train.txt(檔案內容為包含所有訓練的路徑和檔名)一樣,製作2019_test.txt檔案(檔案內容為包含所有測試的路徑和檔名)。

採用以下程式來生成測試集的 test.txt 檔案,其中包含每個測試樣本的路徑和檔名,注意此處包含字尾

# coding=utf-8

import os

from os import listdir, getcwd

from os.path import join

if __name__ ==

'__main__'

:# 只有在檔案作為指令碼檔案直接執行時才執行下面**

source_folder=

'your path'

#儲存的路徑

dest=

'your path/test.txt'

#寫有的名字的路徑

file_list=os.listdir(source_folder)

#獲取各的名稱

test_file=

open

(dest,

'a')

#追加寫開啟

count =

0for file_obj in file_list:

count +=

1

file_path=os.path.join(source_folder,file_obj)

#路徑拼接 指向 檔案的路徑

# file_name,file_extend=os.path.splitext(file_path) #分離檔名與副檔名 file_name為去掉副檔名的名稱

test_file.write(file_path+

'\n'

)#寫入去掉副檔名的檔名名稱

test_file.close(

)#關閉檔案

然後在該.py檔案的路徑下,執行如下命令:

python create_test_txt.py
修改./cfg/voc.data檔案中的valid部分,將其設定為第1步中生成的test.txt路徑。

classes=

# 不用改

train =

# 不用改

valid = /your path/test.txt

names =

# 不用改

backup =

# 不用改`在這裡插入**片`

將測試模式開啟,將訓練模式關閉,如下:

[net]

# testing

batch=1

subdivisions=1

# training

# batch=64

# subdivisions=16

在命令視窗的./darknet路徑下,執行以下語句:

./darknet detector valid cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights
執行完畢之後會在./darknet/result/ 路徑下生成若干個txt檔案(個數和類別數一致),名稱為comp4_det_test_[class_name].txt,這便是**結果檔案。

通常我們希望**結果儲存在乙個個以名稱為名字的txt檔案中,例如,我所期望的檔案格式如下:

那麼我們可以使用如下程式進行格式轉換:

注意:1)需要將第4步生成的若干個txt檔案重新命名為:類名.txt

2)將這些類名.txt統一放到乙個空資料夾下,即下面程式中的 『your path\raw_txt』

3)建立乙個用來存放轉換後txt檔案的資料夾,即下面程式中的 『your path\converted_txt』

import os

def(result_txt, threshold=

0.0)

:# 設定乙個閾值,用來刪掉置信度低的**框資訊

# 建立乙個字典,用來存放資訊

txt =

open

(result_txt,

'r')

.readlines(

)# 按行讀取txt檔案

for info in txt:

# 提取每一行

info = info.split(

)# 將每一行(每個**框)的資訊切分開

photo_name = info[0]

# 名稱

probably =

float

(info[1]

)# 當前**框的置信度

if probably < threshold:

continue

else

: xmin =

int(

float

(info[2]

))ymin =

int(

float

(info[3]

))xmax =

int(

float

(info[4]

))ymax =

int(

float

(info[5]

))position =

[xmin, ymin, xmax, ymax]

if photo_name not=[

] def

creat_result_txt

(raw_txt_path, target_path, threshold=

0.0)

:# raw_txt_path為yolo按類輸出的txt的路徑 target_path 為轉換後的txt存放路徑

all_files = os.listdir(raw_txt_path)

# 獲取所以的原始txt

for each_file in all_files:

# 遍歷所有的原始txt檔案,each_file為乙個檔名,例如『car.txt』

each_file_path = os.path.join(raw_txt_path, each_file)

# 獲取當前txt的路徑

# 對當前txt生成map_dic

for each_map in map_dic:

# 遍歷當前存放資訊的字典

target_txt = each_map +

'.txt'

# 生成目標txt檔名

target_txt_path = os.path.join(target_path, target_txt)

# 生成目標txt路徑

if target_txt not

in os.listdir(target_path)

: txt_write =

open

(target_txt_path,

'w')

# 如果目標路徑下沒有這個目標txt檔案,則建立它,即模式設定為「覆蓋」

else

: txt_write =

open

(target_txt_path,

'a')

# 如果目標路徑下有這個目標txt檔案,則將模式設定為「追加」

class_name = each_file[:-

4]# 獲取當前原始txt的類名

txt_write.write(class_name)

# 對目標txt寫入類名

txt_write.write(

'\n'

)# 換行

for info in map_dic[each_map]

:# 遍歷某張的所有**框資訊

txt_write.write(

str(info[0]

)+' '+

str(info[1]

)+' '+

str(info[2]

)+' '+

str(info[3]

)+' ')

# 寫入**框資訊

txt_write.write(

'\n'

)# 換行

creat_result_txt(

'your path\\raw_txt'

,'your path\converted_txt'

, threshold=

0.1)

執行完畢後,在your path\converted_txt資料夾中會產生每個對應得檢測結果

至此完成了 基於yolo-v3 針對自己資料集得檢測與測試 結果轉化 。

yolo v3使用測試

yolov3 測試影象新增置信度與訓練bmp影象格式 2.yolo cfg檔案中各個引數的含義 3 批量測試並新增到自己的資料夾 參考部落格 其中第乙個存在的問題是,儲存的資料夾中影象沒有標記 問題是沒有新增畫框函式 void test detector char datacfg,char cfgf...

YOLOv3測試時python介面分析

呼叫darknet python介面需使用darknet python darknet.py,而其中核心為detect 函式,該引數主要引數為 def detect net,meta,image,thresh 5,hier thresh 5,nms 45 而detect 函式中的核心為dets ge...

在python指令碼中呼叫YOLOv3測試命令

這個其實就是在python指令碼裡呼叫shell命令。python檔案中對yolov3的全部呼叫如下 os.system darknet detector demo cfg voc.data cfg yolov3 voc.cfg backup yolov3 voc 25000.weights dat...