Deep Learning中常见图、线的绘制 -- ROC、PR、箱线图、折线图、损失变化图

前言

一篇好的论文中图和表是必不可少的,本人结合自己写论文过程中用到的一些图、线,并对此稍加整合,汇总成本文。本文基于Python语言实现。本人研究方向为图像分割,故案例均与图像有关,望周知!

一、ROC曲线和PR曲线

首先需要介绍的是ROC曲线和PR曲线的绘制,这在深度学习的论文中是很常见的,一个神经网络模型的好坏可以通过ROC曲线和PR曲线呈现出来。
绘制ROC曲线和PR曲线,需要使用混淆矩阵,不了解混淆矩阵的小伙伴可以去混淆矩阵查阅,而建立一个混淆矩阵需要图像的label和预测值,注意的是:预测值必须是模型预测分类的概率,而不是0-1。若使用图像的label和0-1矩阵,则画出的ROC和PR曲线会是一条折线,而不是一条光滑的曲线。

  • 导包
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import roc_curve, auc, precision_recall_curve
from scipy.interpolate import interp1d

1.1 ROC曲线的绘制

# 1.载入.npy文件;pred_images.npy文件是模型预测的结果的汇总;gt_images.npy是ground truth的汇总
pred = np.load('pred_images.npy')
gt = np.load('gt_images.npy')
# 2.定义一个画布
plt.figure(1)
# 3.计算fpr、tpr及roc曲线的面积
fpr, tpr, thresholds = roc_curve((gt), pred)
roc_auc = auc(fpr, tpr)
# 4.绘制roc曲线
plt.plot(fpr, tpr, label='UNet (area = {:.4f})'.format(roc_auc), color='blue')
# 5.格式个性化
font1 = {
    
    
'weight' : 'normal',
'size'   : 14, }
plt.xlabel("FPR (False Positive Rate)", font1)
plt.ylabel("TPR (True Positive Rate)", font1)
plt.legend(loc="lower right", fontsize=12)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
plt.axis([0, 1, 0.70, 1])
plt.title('ROC Curve', font1)
plt.show()
print('Done!')

在这里插入图片描述

1.2 PR曲线的绘制

# 1.载入.npy文件;pred_images.npy文件是UNet模型预测的结果的汇总;gt_images.npy是ground truth的汇总
pred = np.load('pred_images.npy')
gt = np.load('gt_images.npy')
# 2.定义一个画布
plt.figure(1)
precision, recall, thresholds = precision_recall_curve(gt, pred)
precision = np.fliplr([precision])[0]  # so the array is increasing (you won't get negative AUC)
recall = np.fliplr([recall])[0]  # so the array is increasing (you won't get negative AUC)
AUC_prec_rec = np.trapz(precision, recall)
plt.plot(recall, precision, '-', label='UNet (area = %.4f)' % AUC_prec_rec, color='blue')
font1 = {
    
    
'weight' : 'normal',
'size'   : 14, }
plt.title('Precision Recall Curve', font1)
plt.xlabel("Recall", font1)
plt.ylabel("Precision", font1)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.legend(loc="lower left", fontsize=8)
plt.axis([0.2, 0.9, 0.7, 1])
plt.show()

在这里插入图片描述

二、折线图

a = np.random.randn(5)
x_list = ['a', 'b', 'c', 'd', 'e']
fig = plt.figure()
plt.plot(x_list, a, marker='o', markersize=3)
for a, b in zip(x_list, a):
    plt.text(a, b, b, ha='center', va='top', fontsize=8)
plt.legend(['line'])
plt.show()

在这里插入图片描述

三、箱型图

data_list = []
a = np.random.randn(10)
b = np.random.randn(10)
c = np.random.randn(10)
d = np.random.randn(10)
e = np.random.randn(10)
data_list.append(a)
data_list.append(b)
data_list.append(c)
data_list.append(d)
data_list.append(e)
fig = plt.figure()
plt.boxplot(data_list, notch=False, vert=True, meanline=True, showmeans=True, sym=None)
plt.xticks([data+1 for data in range(len(data_list))], ['a', 'b', 'c', 'd', 'e'])
plt.plot()
plt.show()

在这里插入图片描述

四、损失变化图

绘制损失变化图需要使用Visdom,相关visdom库得内容可以参考Visdom

  • 导入Visdom及相关工具包
from visdom import Visdom
import numpy as np
import time
  • 绘制
# 实例化一个窗口
wind = Visdom()
# 初始化窗口信息
wind.line([0.], # Y的第一个点的坐标
		  [0.], # X的第一个点的坐标
		  win = 'train_loss', # 窗口的名称
		  opts = dict(title = 'train_loss') # 图像的标例
)
# 更新数据
for step in range(10):
	# 随机获取loss,这里只是模拟实现
	loss = np.random.randn() * 0.5 + 2
	wind.line([loss],[step],win = 'train_loss',update = 'append')
	time.sleep(0.5)

在这里插入图片描述

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/120854080