作者 |?Fengwen、BBuf
(相關(guān)資料圖)
本文主要介紹在One-YOLOv5項(xiàng)目中計(jì)算mAP用到的一些numpy操作,這些numpy操作使用在utils/metrics.py中。本文是《YOLOv5全面解析教程④:目標(biāo)檢測(cè)模型精確度評(píng)估》的補(bǔ)充,希望能幫助到小伙伴們。
歡迎Star、試用One-YOLOv5:
https://github.com/Oneflow-Inc/one-yolov5
用到的numpy操作比如:np.cumsum()、np.interp()、np.maximum.accumulate()、np.trapz()等。接下來將在下面逐一介紹。
import?numpy?as?np
np.cumsum()
返回元素沿給定軸的累積和。
numpy.cumsum(a, axis=None, dtype=None, out=None)源碼(https://github.com/numpy/numpy/blob/v1.23.0/numpy/core/fromnumeric.py#L2497-L2571)
參數(shù)
a:數(shù)組
axis: 軸索引,整型,若a為n維數(shù)組,則axis的取值范圍為[0,n-1]
dtype:?返回結(jié)果的數(shù)據(jù)類型,若不指定,則默認(rèn)與a一致n
out: 數(shù)據(jù)類型為數(shù)組。用來放置結(jié)果的替代輸出數(shù)組,它必須具有與輸出結(jié)果具有相同的形狀和數(shù)據(jù)緩沖區(qū)長度
返回
沿著指定軸的元素累加和所組成的數(shù)組,其形狀應(yīng)與輸入數(shù)組a一致
更多信息請(qǐng)參閱讀:
1.API_CN(https://www.osgeo.cn/numpy/reference/generated/numpy.cumsum.html?highlight=cumsum#numpy.cumsum)
2.API_EN(https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html?highlight=cumsum#numpy.cumsum)
np.cumsum(a)?#?計(jì)算累積和的軸。默認(rèn)(無)是在展平的數(shù)組上計(jì)算cumsum。
array([ 1, ?3, ?6, 10, 15, 21])
a?=?np.array([[1,2,3],?[4,5,6]])np.cumsum(a,?dtype=float)?????#?指定輸出的特定的類型
array([ 1., ?3., ?6., 10., 15., 21.])
np.cumsum(a,axis=0)??????#?3列中每一列的行總和
array([[1, 2, 3], ? ? ? [5, 7, 9]])
x?=?np.ones((3,4),dtype=int)?np.cumsum(?x?,axis=0)
array([[1, 1, 1, 1], ? ? ? [2, 2, 2, 2], ? ? ? [3, 3, 3, 3]])
np.cumsum(a,axis=1)??????#?2行中每行的列總和
array([[ 1, ?3, ?6], ? ? ? [ 4, ?9, 15]])
np.interp()
參數(shù)
x: 數(shù)組待插入數(shù)據(jù)的橫坐標(biāo)
xp: 一維浮點(diǎn)數(shù)序列原始數(shù)據(jù)點(diǎn)的橫坐標(biāo),如果period參數(shù)沒有指定那么就必須是遞增的 否則,在使用xp = xp % period正則化之后,xp在內(nèi)部進(jìn)行排序
fp: 一維浮點(diǎn)數(shù)或復(fù)數(shù)序列 原始數(shù)據(jù)點(diǎn)的縱坐標(biāo),和xp序列等長.
left: 可選參數(shù),類型為浮點(diǎn)數(shù)或復(fù)數(shù)(對(duì)應(yīng)于fp值) 當(dāng)x < xp[0]時(shí)的插值返回值,默認(rèn)為fp[0].
right: 可選參數(shù),類型為浮點(diǎn)數(shù)或復(fù)數(shù)(對(duì)應(yīng)于fp值),當(dāng)x > xp[-1]時(shí)的插值返回值,默認(rèn)為fp[-1].
period: None或者浮點(diǎn)數(shù),可選參數(shù)橫坐標(biāo)的周期 此參數(shù)使得可以正確插入angular x-coordinates. 如果該參數(shù)被設(shè)定,那么忽略left參數(shù)和right參數(shù)
返回
浮點(diǎn)數(shù)或復(fù)數(shù)(對(duì)應(yīng)于fp值)或ndarray. 插入數(shù)據(jù)的縱坐標(biāo),和x形狀相同
注意!
在沒有設(shè)置period參數(shù)時(shí),默認(rèn)要求xp參數(shù)是遞增序列
#?插入一個(gè)值import?numpy?as?npimport?matplotlib.pyplot?as?pltx?=?2.5xp?=?[1,?2,?3]fp?=?[3,?2,?0]y?=?np.interp(x,?xp,?fp)??#?1.0plt.plot(xp,?fp,?"-o")?plt.plot(x,?y,?"x")?#?畫插值plt.show()
#?插入一個(gè)序列import?numpy?as?npimport?matplotlib.pyplot?as?pltx?=?[0,?1,?1.5,?2.72,?3.14]xp?=?[1,?2,?3]fp?=?[3,?2,?0]y?=?np.interp(x,?xp,?fp)??#?array([?3.?,??3.?,??2.5?,??0.56,??0.?])plt.plot(xp,?fp,?"-o")plt.plot(x,?y,?"x")plt.show()
計(jì)算數(shù)組(或數(shù)組的特定軸)的累積最大值
import?numpy?as?npd?=?np.random.randint(low?=?1,?high?=?10,?size=(2,3))print("d:\n",d)c?=?np.maximum.accumulate(d,?axis=1)print("c:\n",c)
d:?
[[1 9 5]
[2 6 1]]
c:?
[[1 9 9]?
[2 6 6]]
numpy.trapz(y, x=None, dx=1.0, axis=- 1) 使用復(fù)合梯形規(guī)則沿給定軸積分。
import?matplotlib.pyplot?as?pltimport?numpy?as?npy?=?[1,?2,?3]?;?x?=?[i+1?for?i?in?range(len(y))]print(np.trapz(x))plt.fill_between(x,?y)plt.show()?#?(1?+?3)*(3?-?1)/2?=?4
4.0
import?matplotlib.pyplot?as?pltimport?numpy?as?npy?=?[1,?2,?3]?x?=?[4,?6,?8]print(np.trapz(y,x))plt.fill_between(x,?y)plt.show()?#?(3?+?1)*(8?-?4)?/?2?=?8
8.0
1. numpy API文檔 CN:https://www.osgeo.cn/numpy/dev/index.html
2. numpy API文檔 EN:https://numpy.org/doc/stable/reference/index.html
3. axis的基本使用:https://www.jb51.net/article/242067.htm
其他人都在看
OneFlow v0.9.0正式發(fā)布
從0到1,OpenAI的創(chuàng)立之路
一塊GPU搞定ChatGPT;ML系統(tǒng)入坑指南
YOLOv5解析教程:目標(biāo)檢測(cè)模型精確度評(píng)估
比快更快,開源Stable Diffusion刷新作圖速度
OneEmbedding:單卡訓(xùn)練TB級(jí)推薦模型不是夢(mèng)
GLM訓(xùn)練加速:性能最高提升3倍,顯存節(jié)省1/3
歡迎Star、試用OneFlow最新版本:GitHub - Oneflow-Inc/oneflow: OneFlow is a deep learning framework designed to be user-friendly, scalable and efficient.OneFlow is a deep learning framework designed to be user-friendly, scalable and efficient. - GitHub - Oneflow-Inc/oneflow: OneFlow is a deep learning framework designed to be user-friendly, scalable and efficient.https://github.com/Oneflow-Inc/oneflow/
關(guān)鍵詞: