統(tǒng)計數(shù)據集中目標大、中、小個數(shù)
最近看到一篇論文,其中在數(shù)據集介紹部分統(tǒng)計了大、中、小目標信息。因此,為了獲取數(shù)據集的統(tǒng)計信息,我參考了作者寫的代碼基于tensorpack統(tǒng)計coco數(shù)據集中大、中、小目標的數(shù)量文章來源:http://www.zghlxwxcb.cn/news/detail-641956.html
精簡版代碼如下(內有多個參數(shù)需要修改,仔細查看注釋)文章來源地址http://www.zghlxwxcb.cn/news/detail-641956.html
# 1、統(tǒng)計數(shù)據集中小、中、大 GT的個數(shù)
# 2、統(tǒng)計某個類別小、中、大 GT的個數(shù)
# 3、統(tǒng)計數(shù)據集中ss、sm、sl GT的個數(shù)
import os
from pathlib import Path
import matplotlib.pyplot as plt
# 設置中文字體為微軟雅黑
plt.rcParams['font.sans-serif'] = 'SimHei'
def getGtAreaAndRatio(label_dir):
"""
得到不同尺度的gt框個數(shù)
:params label_dir: label文件地址
:return data_dict: {dict: 3} 3 x {'類別':{’area':[...]}, {'ratio':[...]}}
"""
data_dict = {}
assert Path(label_dir).is_dir(), "label_dir is not exist"
txts = os.listdir(label_dir) # 得到label_dir目錄下的所有txt GT文件
for txt in txts: # 遍歷每一個txt文件
with open(os.path.join(label_dir, txt), 'r') as f: # 打開當前txt文件 并讀取所有行的數(shù)據
lines = f.readlines()
for line in lines: # 遍歷當前txt文件中每一行的數(shù)據
temp = line.split() # str to list{5}
coor_list = list(map(lambda x: x, temp[1:])) # [x, y, w, h]
area = float(coor_list[2]) * float(coor_list[3]) # 計算出當前txt文件中每一個gt的面積
# center = (int(coor_list[0] + 0.5*coor_list[2]),
# int(coor_list[1] + 0.5*coor_list[3]))
ratio = round(float(coor_list[2]) / float(coor_list[3]), 2) # 計算出當前txt文件中每一個gt的 w/h
if temp[0] not in data_dict:
data_dict[temp[0]] = {}
data_dict[temp[0]]['area'] = []
data_dict[temp[0]]['ratio'] = []
data_dict[temp[0]]['area'].append(area)
data_dict[temp[0]]['ratio'].append(ratio)
return data_dict
def getSMLGtNumByClass(data_dict, class_num):
"""
計算某個類別的小物體、中物體、大物體的個數(shù)
params data_dict: {dict: 3} 3 x {'類別':{’area':[...]}, {'ratio':[...]}}
params class_num: 類別 0, 1, 2
return s: 該類別小物體的個數(shù) 0 < area <= 0.5%
m: 該類別中物體的個數(shù) 0.5% < area <= 1%
l: 該類別大物體的個數(shù) area > 1%
"""
s, m, l = 0, 0, 0
# 圖片的尺寸大小 注意修改!!!
h = 960
w = 540
for item in data_dict['{}'.format(class_num)]['area']:
if item * h * w <= h * w * 0.005:
s += 1
elif item * h * w <= h * w * 0.010:
m += 1
else:
l += 1
return s, m, l
def getAllSMLGtNum(data_dict, isEachClass=False):
"""
數(shù)據集所有類別小、中、大GT分布情況
isEachClass 控制是否按每個類別輸出結構
"""
S, M, L = 0, 0, 0
# 需要手動初始化下,有多少個類別就需要寫多個
classDict = {'0': {'S': 0, 'M': 0, 'L': 0}, '1': {'S': 0, 'M': 0, 'L': 0}, '2': {'S': 0, 'M': 0, 'L': 0},
'3': {'S': 0, 'M': 0, 'L': 0}}
print(classDict['0']['S'])
# range(class_num)類別數(shù) 注意修改!!!
if isEachClass == False:
for i in range(4):
s, m, l = getSMLGtNumByClass(data_dict, i)
S += s
M += m
L += l
return [S, M, L]
else:
for i in range(4):
S = 0
M = 0
L = 0
s, m, l = getSMLGtNumByClass(data_dict, i)
S += s
M += m
L += l
classDict[str(i)]['S'] = S
classDict[str(i)]['M'] = M
classDict[str(i)]['L'] = L
return classDict
# 畫圖函數(shù)
def plotAllSML(SML):
x = ['S:[0, 32x32]', 'M:[32x32, 96x96]', 'L:[96*96, 640x640]']
fig = plt.figure(figsize=(10, 8)) # 畫布大小和像素密度
plt.bar(x, SML, width=0.5, align="center", color=['skyblue', 'orange', 'green'])
for a, b, i in zip(x, SML, range(len(x))): # zip 函數(shù)
plt.text(a, b + 0.01, "%d" % int(SML[i]), ha='center', fontsize=15, color="r") # plt.text 函數(shù)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.xlabel('gt大小', fontsize=16)
plt.ylabel('數(shù)量', fontsize=16)
plt.title('廣佛手病蟲害訓練集小、中、大GT分布情況(640x640)', fontsize=16)
plt.show()
# 保存到本地
# plt.savefig("")
if __name__ == '__main__':
labeldir = r'E:\project\py-project\network\dataset\UA-DETRAC-G2\labels\val'
data_dict = getGtAreaAndRatio(labeldir)
# 1、數(shù)據集所有類別小、中、大GT分布情況
# 控制是否按每個類別輸出結構
isEachClass = False
SML = getAllSMLGtNum(data_dict, isEachClass)
print(SML)
if not isEachClass:
plotAllSML(SML)
到了這里,關于統(tǒng)計數(shù)據集中目標大、中、小個數(shù)【需要用到y(tǒng)olo的txt標注文件數(shù)據,其他格式數(shù)據不一定適用】的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!