yolov3 map 計算

1.代碼 作者原版

git clone https://github.com/pjreddie/darknet.git

 本文的代碼包:

鏈接: https://pan.baidu.com/s/1O6YCyJLKXH9MrqnLAYmlug 提取碼: dj4g

2.生成測試結果

2.1 修改data文件   

我這裏把所有數據都訓練了,爲了統計訓練的map,所以valid 的路徑也放的是train路徑

train.txt 內容

2.2 測試命令及結果文件

命令

# 對應調整 data cfg weights 文件

./darknet detector valid kitti_data/kitti.data cfg/yolov3-kitti.cfg backupp/yolov3-kitti_2000.weights

我這個電腦測試7481張文件共計用時624s,每張平均需要0.083411309s.

結果文件

存放在 results 中。每個類別自成一個txt文件。每個類別文件(唯一)包含所有測試圖片。有幾個類別就有幾txt。

以 Car.txt 舉例

每行含義:

圖片命名,分數,框的位置(左上右下)

2.3 結果文件--->mAP 計算需要的形式

結果文件形式如上圖;

mAP需要的形式:

每個圖片都形成一個標籤文件,保存在 detection-results 文件夾下。

其中一個標籤文件格式展示:

轉換代碼:

參考修改自 https://blog.csdn.net/qq_32761549/article/details/90054023#4__54

# detection_transfer.py
import os


def creat_mapping_dic(result_txt, threshold=0.0):  # 設置一個閾值,用來刪掉置信度低的預測框信息

    mapping_dic = {}  # 創建一個字典,用來存放信息
    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,probably]

            if photo_name not in mapping_dic:  # mapping_dic的每個元素的key值爲圖片名稱,value爲一個二維list,其中存放當前圖片的若干個預測框的位置
                mapping_dic[photo_name] = []

            mapping_dic[photo_name].append(position)

    return mapping_dic


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的路徑
        map_dic = creat_mapping_dic(each_file_path, threshold=threshold)  # 對當前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(class_name)  # 對目標txt寫入類名
                txt_write.write(' '+str(info[4])+' '+str(info[0]) + ' ' + str(info[1]) +
                                ' ' + str(info[2]) + ' ' + str(info[3]) + ' ')  # 寫入預測框信息
                txt_write.write('\n')  # 換行


creat_result_txt('/home/studieren/lunwen/darknet/results',
                 '/home/studieren/lunwen/darknet/result_23',
                 threshold=0.1)
# 第一個文件路徑,結果文件。   輸入路徑
# 第二個文件路徑,生成單獨標籤的保存路徑。   輸出路徑
# 閾值,篩選閾值以上的保存。

2.4 gt 標籤 轉換成 mAP 計算需要的形式

gt標籤的形式:

label_02

源kitti labels 經過合併的標籤。只有 car pedestrain and cyclist。

mAP需要的形式:

將文件保存在 ground-truth 文件夾下

轉換代碼:

# gt_transfer.py
import glob
import os
import string

txt_list = glob.glob('/home/studieren/lunwen/darknet/label_2/*.txt')

for item in txt_list:
    new_txt = []
    try:
        with open(item, 'r') as r_tdf:
            for each_line in r_tdf:
                label = each_line.strip().split(' ')
                info1=int(float(label[4]))
                info2=int(float(label[5]))
                info3=int(float(label[6]))
                info4=int(float(label[7]))
                simple_label=str(label[0])+' '+str(info1)+' '+str(info2)+' '+str(info3)+' '+str(info4)+'\n'

                new_txt.append(simple_label)  # 重新寫入新的txt文件

            with open(item, 'w+') as w_tdf:  # w+是打開原文件將內容刪除,另寫新內容進去
                for temp in new_txt:
                    w_tdf.write(temp)

    except IOError as ioerr:
        print('File error:' + str(ioerr))

2.5 mAP計算

代碼鏈接:https://github.com/Cartucho/mAP

參考自:https://blog.csdn.net/weixin_44791964/article/details/104695264#mAP_96

使用方法:

1.將生成好的detection-results, 放入input 中 ,注意文件名需要 是 detection-results。 

2.將生成好的ground-truth, 放入input 中 ,注意文件名需要 是 ground-truth

3.在 mAP-master 文件夾下,運行 python main.py

生成的結果:

看出來模型效果不是很好,因爲只迭代了2000步。

3. 步驟

1. 在darknet文件夾下,使用命令
./darknet detector valid kitti_data/kitti.data cfg/yolov3-kitti.cfg backupp/yolov3-kitti_2000.weights
生成結果默認保存在results 文件中
2.更改輸入地址和輸出地址
運行detection_transfer.py,並將結果保存在 input/detection-results 文件夾下。
3.更改gt_label地址
運行gt_transfer.py,並將結果保存在 input/ground-truth 文件夾下。
4.查看結果文件,保存在outputs文件夾下。

可以配合 darknet yolov3 訓練 kitti數據集 https://blog.csdn.net/qq_40297851/article/details/104937740  食用。

還在碼具體訓練過程,包括log文件生成loss的圖。不定期更新也可能草稿箱喫灰了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章