使用pytorch深度學(xué)習(xí)框架實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像分類(lèi)

此文章是使用pytorch實(shí)現(xiàn)mnist手寫(xiě)字體的圖像分類(lèi)。
利用pytorch內(nèi)置函數(shù)mnist下載數(shù)據(jù),同時(shí)利用torchvision對(duì)數(shù)據(jù)進(jìn)行預(yù)處理,調(diào)用torch.utils建立一個(gè)數(shù)據(jù)迭代器,利用pytorch的nn工具箱構(gòu)建神經(jīng)網(wǎng)絡(luò)模型。具體實(shí)現(xiàn)代碼如下:

import numpy as np
import torch
import torchvision.transforms as transforms
from torchvision.datasets import mnist
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
from torch import nn

#構(gòu)建網(wǎng)絡(luò)
class Net(nn.Module):
    def __init__(self,in_dim,n_hidden_1,n_hidden_2,out_dim):
        super(Net,self).__init__()
        self.layer1=nn.Sequential(nn.Linear(in_dim,n_hidden_1),nn.BatchNorm1d(n_hidden_1))
        self.layer2=nn.Sequential(nn.Linear(n_hidden_1,n_hidden_2),nn.BatchNorm1d(n_hidden_2))
        self.layer3=nn.Sequential(nn.Linear(n_hidden_2,out_dim))
    def forward(self,x):
        x=F.relu(self.layer1(x))
        x=F.relu(self.layer2(x))
        x=self.layer3(x)
        return x
#加載數(shù)據(jù)集    
train_batch_size =64
test_batch_size=128
num_epoches=20
lr=0.01
momentum=0.5
transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize([0.5],[0.5])])
train_dataset=mnist.MNIST('./data',train=True,transform=transform,download=True)
test_dataset=mnist.MNIST('./data',train=False,transform=transform)
train_loader=DataLoader(train_dataset,batch_size=train_batch_size,shuffle=True)
test_loader=DataLoader(test_dataset,batch_size=test_batch_size,shuffle=False)
#實(shí)例化網(wǎng)絡(luò)
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model=Net(28*28,300,100,10)
model.to(device)
criterion=nn.CrossEntropyLoss()
optimizer=optim.SGD(model.parameters(),lr=lr,momentum=momentum)
losses=[]
acces=[]
eval_losses=[]
eval_acces=[]
for epoch in range(num_epoches):
    train_loss=0
    train_acc=0
    model.train()
    if epoch%5==0:
        optimizer.param_groups[0]['lr']*=0.1
    for img,label in train_loader:
        img=img.to(device)
        label=label.to(device)
        img=img.view(img.size(0),-1)
        out=model(img)
        loss=criterion(out,label)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        train_loss +=loss.item()
        _,pred=out.max(1)
        num_correct=(pred==label).sum().item()
        acc=num_correct/img.shape[0]
        train_acc+=acc
        
    losses.append(train_loss/len(train_loader))
    acces.append(train_acc/len(train_loader))
    eval_loss=0
    eval_acc=0
    model.eval()
    for img,label in test_loader:
        img=img.to(device)
        label=label.to(device)
        img=img.view(img.size(0),-1)
        out=model(img)
        loss=criterion(out,label)
        eval_loss+=loss.item()
        _,pred=out.max(1)
        num_correct=(pred==label).sum().item()
        acc=num_correct/img.shape[0]
        eval_acc+=acc
    eval_losses.append(eval_loss/len(test_loader))
    eval_acces.append(eval_acc/len(test_loader))
    print('epoch:{},Train Loss: {:.4f},Train Acc: {:.4f},Test Loss: {:.4f},Test Acc: {:.4f}'
          .format(epoch,train_loss/len(train_loader),train_acc/len(train_loader),
                 eval_loss/len(test_loader),eval_acc/len(test_loader)))

plt.title('trainloss')
plt.plot(np.arange(len(losses)),losses)
plt.legend(['Train Loss'],loc='upper right')

結(jié)果如圖所示:


代碼結(jié)果

可視化原始數(shù)據(jù),代碼如下:

import matplotlib.pyplot as plt
%matplotlib inline
examples =enumerate(test_loader)
batch_idx,(example_data,example_targets)=next(examples)
fig=plt.figure()
for i in range(6):
    plt.subplot(2,3,i+1)
    plt.tight_layout()
    plt.imshow(example_data[i][0],cmap='gray',interpolation='none')
    plt.title("Ground Truth: {}".format(example_targets[i]))
    plt.xticks([])
    plt.yticks([])

結(jié)果如圖所示:


可視化結(jié)果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容