濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋)

Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋)

熱門標(biāo)簽:騰訊外呼線路 激戰(zhàn)2地圖標(biāo)注 哈爾濱ai外呼系統(tǒng)定制 陜西金融外呼系統(tǒng) 公司電話機(jī)器人 白銀外呼系統(tǒng) 海南400電話如何申請(qǐng) 廣告地圖標(biāo)注app 唐山智能外呼系統(tǒng)一般多少錢

使用了兩個(gè)卷積層加上兩個(gè)全連接層實(shí)現(xiàn)
本來(lái)打算從頭手撕的,但是調(diào)試太耗時(shí)間了,改天有時(shí)間在從頭寫一份
詳細(xì)過(guò)程看代碼注釋,參考了下一個(gè)博主的文章,但是鏈接沒注意關(guān)了找不到了,博主看到了聯(lián)系下我,我加上
代碼相關(guān)的問(wèn)題可以評(píng)論私聊,也可以翻看博客里的文章,部分有詳細(xì)解釋

Python實(shí)現(xiàn)代碼:

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
import torchvision
from torch.autograd import Variable
from torch.utils.data import DataLoader
import cv2

# 下載訓(xùn)練集
train_dataset = datasets.MNIST(root='E:\mnist',
                               train=True,
                               transform=transforms.ToTensor(),
                               download=True)
# 下載測(cè)試集
test_dataset = datasets.MNIST(root='E:\mnist',
                              train=False,
                              transform=transforms.ToTensor(),
                              download=True)

# dataset 參數(shù)用于指定我們載入的數(shù)據(jù)集名稱
# batch_size參數(shù)設(shè)置了每個(gè)包中的圖片數(shù)據(jù)個(gè)數(shù)
# 在裝載的過(guò)程會(huì)將數(shù)據(jù)隨機(jī)打亂順序并進(jìn)打包
batch_size = 64
# 建立一個(gè)數(shù)據(jù)迭代器
# 裝載訓(xùn)練集
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)
# 裝載測(cè)試集
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=True)


# 卷積層使用 torch.nn.Conv2d
# 激活層使用 torch.nn.ReLU
# 池化層使用 torch.nn.MaxPool2d
# 全連接層使用 torch.nn.Linear
class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Sequential(nn.Conv2d(1, 6, 3, 1, 2),
                                   nn.ReLU(), nn.MaxPool2d(2, 2))

        self.conv2 = nn.Sequential(nn.Conv2d(6, 16, 5), nn.ReLU(),
                                   nn.MaxPool2d(2, 2))

        self.fc1 = nn.Sequential(nn.Linear(16 * 5 * 5, 120),
                                 nn.BatchNorm1d(120), nn.ReLU())

        self.fc2 = nn.Sequential(
            nn.Linear(120, 84),
            nn.BatchNorm1d(84),
            nn.ReLU(),
            nn.Linear(84, 10))
        # 最后的結(jié)果一定要變?yōu)?10,因?yàn)閿?shù)字的選項(xiàng)是 0 ~ 9

    def forward(self, x):
        x = self.conv1(x)
        # print("1:", x.shape)
        # 1: torch.Size([64, 6, 30, 30])
        # max pooling
        # 1: torch.Size([64, 6, 15, 15])
        x = self.conv2(x)
        # print("2:", x.shape)
        # 2: torch.Size([64, 16, 5, 5])
        # 對(duì)參數(shù)實(shí)現(xiàn)扁平化
        x = x.view(x.size()[0], -1)
        x = self.fc1(x)
        x = self.fc2(x)
        return x


def test_image_data(images, labels):
    # 初始輸出為一段數(shù)字圖像序列
    # 將一段圖像序列整合到一張圖片上 (make_grid會(huì)默認(rèn)將圖片變成三通道,默認(rèn)值為0)
    # images: torch.Size([64, 1, 28, 28])
    img = torchvision.utils.make_grid(images)
    # img: torch.Size([3, 242, 242])
    # 將通道維度置在第三個(gè)維度
    img = img.numpy().transpose(1, 2, 0)
    # img: torch.Size([242, 242, 3])
    # 減小圖像對(duì)比度
    std = [0.5, 0.5, 0.5]
    mean = [0.5, 0.5, 0.5]
    img = img * std + mean
    # print(labels)
    cv2.imshow('win2', img)
    key_pressed = cv2.waitKey(0)


# 初始化設(shè)備信息
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 學(xué)習(xí)速率
LR = 0.001
# 初始化網(wǎng)絡(luò)
net = LeNet().to(device)
# 損失函數(shù)使用交叉熵
criterion = nn.CrossEntropyLoss()
# 優(yōu)化函數(shù)使用 Adam 自適應(yīng)優(yōu)化算法
optimizer = optim.Adam(net.parameters(), lr=LR, )
epoch = 1
if __name__ == '__main__':
    for epoch in range(epoch):
        print("GPU:", torch.cuda.is_available())
        sum_loss = 0.0
        for i, data in enumerate(train_loader):
            inputs, labels = data
            # print(inputs.shape)
            # torch.Size([64, 1, 28, 28])
            # 將內(nèi)存中的數(shù)據(jù)復(fù)制到gpu顯存中去
            inputs, labels = Variable(inputs).cuda(), Variable(labels).cuda()
            # 將梯度歸零
            optimizer.zero_grad()
            # 將數(shù)據(jù)傳入網(wǎng)絡(luò)進(jìn)行前向運(yùn)算
            outputs = net(inputs)
            # 得到損失函數(shù)
            loss = criterion(outputs, labels)
            # 反向傳播
            loss.backward()
            # 通過(guò)梯度做一步參數(shù)更新
            optimizer.step()
            # print(loss)
            sum_loss += loss.item()
            if i % 100 == 99:
                print('[%d,%d] loss:%.03f' % (epoch + 1, i + 1, sum_loss / 100))
                sum_loss = 0.0
                # 將模型變換為測(cè)試模式
        net.eval()
        correct = 0
        total = 0
        for data_test in test_loader:
            _images, _labels = data_test
            # 將內(nèi)存中的數(shù)據(jù)復(fù)制到gpu顯存中去
            images, labels = Variable(_images).cuda(), Variable(_labels).cuda()
            # 圖像預(yù)測(cè)結(jié)果
            output_test = net(images)
            # torch.Size([64, 10])
            # 從每行中找到最大預(yù)測(cè)索引
            _, predicted = torch.max(output_test, 1)
            # 圖像可視化
            # print("predicted:", predicted)
            # test_image_data(_images, _labels)
            # 預(yù)測(cè)數(shù)據(jù)的數(shù)量
            total += labels.size(0)
            # 預(yù)測(cè)正確的數(shù)量
            correct += (predicted == labels).sum()
        print("correct1: ", correct)
        print("Test acc: {0}".format(correct.item() / total))

測(cè)試結(jié)果:

可以通過(guò)調(diào)用test_image_data函數(shù)查看測(cè)試圖片


可以看到最后預(yù)測(cè)的準(zhǔn)確度可以達(dá)到98%

到此這篇關(guān)于Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋)的文章就介紹到這了,更多相關(guān)Pytorch 數(shù)字識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 使用pytorch完成kaggle貓狗圖像識(shí)別方式
  • PyTorch一小時(shí)掌握之圖像識(shí)別實(shí)戰(zhàn)篇

標(biāo)簽:黑龍江 鷹潭 益陽(yáng) 上海 四川 黔西 常德 惠州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋)》,本文關(guān)鍵詞  Pytorch,實(shí)現(xiàn),圖像,識(shí)別,之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    乐安县| 东乡| 临夏县| 铜鼓县| 兴宁市| 重庆市| 镇坪县| 蒙山县| 郧西县| 正安县| 广汉市| 信宜市| 东港市| 唐山市| 马关县| 大姚县| 龙海市| 张家川| 遂昌县| 惠安县| 金沙县| 汉阴县| 滦平县| 通海县| 东乡族自治县| 南丹县| 吉木萨尔县| 潼南县| 隆回县| 隆安县| 凤凰县| 富川| 广州市| 章丘市| 寻甸| 临洮县| 石首市| 沭阳县| 依兰县| 平湖市| 开平市|