首页 > 其他分享 >基于mnist的P-R曲线(准确率,召回率)

基于mnist的P-R曲线(准确率,召回率)

时间:2022-10-24 08:44:39浏览次数:52  
标签:nn self torch 准确率 召回 model out mnist size

一.准确率,召回率

  • TP(True Positive):正确的正例,一个实例是正类并且也被判定成正类
  • FN(False Negative):错误的反例,漏报,本为正类但判定为假类
  • FP(False Positive):错误的正例,误报,本为假类但判定为正类
  • TN(True Negative):正确的反例,一个实例是假类并且也被判定成假类
  • 准确率

    所有的预测正确(正类负类)的占总的比重。 

  • 召回率

    即正确预测为正的占全部实际为正的比例。

  • PR-曲线

    PR曲线是以召回率作为横坐标轴,精确率作为纵坐标轴,遍历所有的阈值,绘制出的曲线。

二. 代码

1.train

import torch
import torch.nn as nn
import torchvision.transforms
import os

device=torch.device('cuda:0')
num_epoch=5
num_classes=2
batch_size=32
learning_rate=0.001
chack_number=8

train_dataset=torchvision.datasets.MNIST(root='../MNIST_data/',
                                         train=True, #train(bool,可选)–如果为True,则从training.pt创建数据集,否则从test.pt创建数据集。
                                         download=True,
                                         transform=torchvision.transforms.ToTensor() #接受PIL图像并返回已转换版本的函数/转换。E、 g,变换。随机裁剪
                                         )
test_dataset=torchvision.datasets.MNIST(root='../MNIST_data/',
                                        train=False,
                                        transform=torchvision.transforms.ToTensor())


#Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=False)

class ConvNet(nn.Module):
    def __init__(self, num_classes=2):
        super(ConvNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.fc = nn.Linear(7 * 7 * 32, num_classes)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1)
        out = self.fc(out)
        return out



model=ConvNet(num_classes).to(device)

checkpoint_save_path='../mnist_checkpoint_two/model.ckpt'
if os.path.exists(checkpoint_save_path):
    print("---------------load the model---------------")
    model.load_state_dict(torch.load(checkpoint_save_path)['model_state_dict'])
else :
    os.makedirs(os.path.dirname(checkpoint_save_path),exist_ok=True)

# Loss and optimizer
criterion=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(model.parameters(),lr=learning_rate)

#Train and model
total_step=len(train_loader)

loss_plt = []
for epoch in range(num_epoch):
    for i,(images,labels) in enumerate(train_loader):
        images=images.to(device)
        labels=labels.to(device)
        labels = torch.tensor([1 if i == chack_number else 0 for i in labels]).to(device)

        #forward
        output=model(images)
        loss=criterion(output,labels)

        #backward
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


        if (i+1) % 100 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
                   .format(epoch+1, num_epoch, i+1, total_step, loss.item()))
    loss_plt.append(loss.sum().mean().item())


torch.save({'model_state_dict':model.state_dict(),
            },
            checkpoint_save_path)

2.predict

 

import torch
import torch.nn as nn
import numpy as np
import torchvision
import matplotlib.pyplot as plt
import os
from sklearn.metrics import precision_recall_curve


device=torch.device('cuda:0')
num_epoch=1
num_classes=10
batch_size=1

train_dataset=torchvision.datasets.MNIST(root='../MNIST_data/',
                                         train=True, #train(bool,可选)–如果为True,则从training.pt创建数据集,否则从test.pt创建数据集。
                                         download=True,
                                         transform=torchvision.transforms.ToTensor() #接受PIL图像并返回已转换版本的函数/转换。E、 g,变换。随机裁剪
                                         )
test_dataset=torchvision.datasets.MNIST(root='../MNIST_data/',
                                        train=False,
                                        transform=torchvision.transforms.ToTensor())

#显示图片
# image=test_dataset[0][0].view(28,28)
# plt.gray()
# plt.axis('off')
# plt.imshow(image)
# plt.show()
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                           batch_size=batch_size,
                                           shuffle=False)


class ConvNet(nn.Module):
    def __init__(self, num_classes=2):
        super(ConvNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.fc = nn.Linear(7 * 7 * 32, num_classes)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1)
        out = self.fc(out)
        return out


checkpoint_save_path='../mnist_checkpoint_two/model.ckpt'
model=ConvNet()
model=model.to(device)
if os.path.exists(checkpoint_save_path):
    print("---------------load the model---------------")
    model.load_state_dict(torch.load(checkpoint_save_path)['model_state_dict'])
#pred
model.eval()
with torch.no_grad():
    check_number=8
    y_pred=[]#预测得分
    y_true=[]
    for i,(images,labels) in enumerate(test_loader):
        images=images.to(device)
        labels=labels.to(device)
        labels = torch.tensor([1 if i == check_number else 0 for i in labels]).to(device) #将多分类转为2分类
        outputs=model(images)
        pred=torch.sigmoid(outputs)[0][1]
        y_true.append(labels.to('cpu')[0])
        y_pred.append(pred.to('cpu'))
        # _, pred = torch.max(outputs.data, 1)
        # if i ==10000:
        #     break

y_pred=np.array(y_pred)
y_true=np.array(y_true)
precision, recall, thresholds = precision_recall_curve(y_true, y_pred)
#plt画图
plt.ylabel('Recall')
plt.xlabel('Precision')
plt.plot(precision,recall)
plt.show()

 

3.P-R曲线

 

 

标签:nn,self,torch,准确率,召回,model,out,mnist,size
From: https://www.cnblogs.com/danchegg/p/16820336.html

相关文章