網(wǎng)站首頁 編程語言 正文
Step 1. 獲取混淆矩陣
#首先定義一個 分類數(shù)*分類數(shù) 的空混淆矩陣 conf_matrix = torch.zeros(Emotion_kinds, Emotion_kinds) # 使用torch.no_grad()可以顯著降低測試用例的GPU占用 with torch.no_grad(): for step, (imgs, targets) in enumerate(test_loader): # imgs: torch.Size([50, 3, 200, 200]) torch.FloatTensor # targets: torch.Size([50, 1]), torch.LongTensor 多了一維,所以我們要把其去掉 targets = targets.squeeze() # [50,1] -----> [50] # 將變量轉(zhuǎn)為gpu targets = targets.cuda() imgs = imgs.cuda() # print(step,imgs.shape,imgs.type(),targets.shape,targets.type()) out = model(imgs) #記錄混淆矩陣參數(shù) conf_matrix = confusion_matrix(out, targets, conf_matrix) conf_matrix=conf_matrix.cpu()
混淆矩陣的求取用到了confusion_matrix函數(shù),其定義如下:
def confusion_matrix(preds, labels, conf_matrix): preds = torch.argmax(preds, 1) for p, t in zip(preds, labels): conf_matrix[p, t] += 1 return conf_matrix
在當(dāng)我們的程序執(zhí)行結(jié)束 test_loader 后,我們可以得到本次數(shù)據(jù)的 混淆矩陣,接下來就要計(jì)算其 識別正確的個數(shù)以及混淆矩陣可視化:
conf_matrix=np.array(conf_matrix.cpu())# 將混淆矩陣從gpu轉(zhuǎn)到cpu再轉(zhuǎn)到np corrects=conf_matrix.diagonal(offset=0)#抽取對角線的每種分類的識別正確個數(shù) per_kinds=conf_matrix.sum(axis=1)#抽取每個分類數(shù)據(jù)總的測試條數(shù) print("混淆矩陣總元素個數(shù):{0},測試集總個數(shù):{1}".format(int(np.sum(conf_matrix)),test_num)) print(conf_matrix) # 獲取每種Emotion的識別準(zhǔn)確率 print("每種情感總個數(shù):",per_kinds) print("每種情感預(yù)測正確的個數(shù):",corrects) print("每種情感的識別準(zhǔn)確率為:{0}".format([rate*100 for rate in corrects/per_kinds]))
執(zhí)行此步的輸出結(jié)果如下所示:
Step 2. 混淆矩陣可視化
對上邊求得的混淆矩陣可視化
# 繪制混淆矩陣 Emotion=8#這個數(shù)值是具體的分類數(shù),大家可以自行修改 labels = ['neutral', 'calm', 'happy', 'sad', 'angry', 'fearful', 'disgust', 'surprised']#每種類別的標(biāo)簽 # 顯示數(shù)據(jù) plt.imshow(conf_matrix, cmap=plt.cm.Blues) # 在圖中標(biāo)注數(shù)量/概率信息 thresh = conf_matrix.max() / 2 #數(shù)值顏色閾值,如果數(shù)值超過這個,就顏色加深。 for x in range(Emotion_kinds): for y in range(Emotion_kinds): # 注意這里的matrix[y, x]不是matrix[x, y] info = int(conf_matrix[y, x]) plt.text(x, y, info, verticalalignment='center', horizontalalignment='center', color="white" if info > thresh else "black") plt.tight_layout()#保證圖不重疊 plt.yticks(range(Emotion_kinds), labels) plt.xticks(range(Emotion_kinds), labels,rotation=45)#X軸字體傾斜45° plt.show() plt.close()
好了,以下就是最終的可視化的混淆矩陣?yán)玻?/p>
其它分類指標(biāo)的獲取
例如 F1分?jǐn)?shù)、TP、TN、FP、FN、精確率、召回率 等指標(biāo), 待補(bǔ)充哈(因?yàn)闀簳r(shí)還沒用到)~
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_38468077/article/details/121671139
相關(guān)推薦
- 2022-10-05 淺談Qt信號槽與事件循環(huán)的關(guān)系_C 語言
- 2022-05-23 Python學(xué)習(xí)之sys模塊使用教程詳解_python
- 2022-09-23 Golang分布式應(yīng)用定時(shí)任務(wù)示例詳解_Golang
- 2022-05-15 Python?matplotlib?seaborn繪圖教程詳解_python
- 2021-10-01 Linux里L(fēng)VM磁盤擴(kuò)容詳細(xì)步驟_Linux
- 2022-07-15 Python中五種實(shí)現(xiàn)字符串反轉(zhuǎn)的方法_python
- 2022-06-27 精簡高效的C#網(wǎng)站優(yōu)化經(jīng)驗(yàn)技巧總結(jié)_C#教程
- 2022-04-11 css左側(cè) div給固定寬 右側(cè)div自適應(yīng)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支