欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytorch深度學(xué)習(xí)之實(shí)現(xiàn)病蟲害圖像分類

 更新時(shí)間:2021年12月24日 16:33:28   作者:masterHu_  
PyTorch是一個(gè)開源的Python機(jī)器學(xué)習(xí)庫(kù),基于Torch,用于自然語言處理等應(yīng)用程序。它具有強(qiáng)大的GPU加速的張量計(jì)算和自動(dòng)求導(dǎo)系統(tǒng)的深度神經(jīng)網(wǎng)絡(luò)。本文將介紹如何通過PyTorch實(shí)現(xiàn)病蟲害圖像分類,感興趣的可以學(xué)習(xí)一下

一、pytorch框架

1.1、概念

PyTorch是一個(gè)開源的Python機(jī)器學(xué)習(xí)庫(kù),基于Torch,用于自然語言處理等應(yīng)用程序。

2017年1月,由Facebook人工智能研究院(FAIR)基于Torch推出了PyTorch。它是一個(gè)基于Python的可續(xù)計(jì)算包,提供兩個(gè)高級(jí)功能:

1、具有強(qiáng)大的GPU加速的張量計(jì)算(如NumPy)。

2、包含自動(dòng)求導(dǎo)系統(tǒng)的深度神經(jīng)網(wǎng)絡(luò)。

1.2、機(jī)器學(xué)習(xí)與深度學(xué)習(xí)的區(qū)別

兩者之間區(qū)別很多,在本篇博客中只簡(jiǎn)單描述一部分。以圖片的形式展現(xiàn)。

前者為機(jī)器學(xué)習(xí)的過程。

后者為深度學(xué)習(xí)的過程。

1.3、在python中導(dǎo)入pytorch成功截圖

二、數(shù)據(jù)集

本次實(shí)驗(yàn)使用的是coco數(shù)據(jù)集中的植物病蟲害數(shù)據(jù)集。分為訓(xùn)練文件Traindata和測(cè)試文件TestData.,

TrainData有9種分類,每一種分類有100張圖片。

TestData有9中分類,每一種分類有10張圖片。

在我下一篇博客中將數(shù)據(jù)集開源。

下面是我的數(shù)據(jù)集截圖:

三、代碼復(fù)現(xiàn)

3.1、導(dǎo)入第三方庫(kù)

import torch
from torch.utils.data import Dataset, DataLoader
import numpy as np
import matplotlib
import os
import cv2
from PIL import Image
import torchvision.transforms as transforms
import torch.optim as optim
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
from Test.CNN import Net
import json
from Test.train_data import Mydataset,pad_image

3.2、CNN代碼

# 構(gòu)建神經(jīng)網(wǎng)絡(luò)
class Net(nn.Module):#定義網(wǎng)絡(luò)模塊
    def __init__(self):
        super(Net, self).__init__()
        # 卷積,該圖片有3層,6個(gè)特征,長(zhǎng)寬均為5*5的像素點(diǎn),每隔1步跳一下
        self.conv1 = nn.Conv2d(3, 6, 5)
        #//(conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
        self.pool = nn.MaxPool2d(2, 2)#最大池化
        #//(pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
        self.conv2 = nn.Conv2d(6, 16, 5)#卷積
        #//(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
        self.fc1 = nn.Linear(16*77*77, 120)#全連接層,圖片的維度為16,
        #(fc1): Linear(in_features=94864, out_features=120, bias=True)
        self.fc2 = nn.Linear(120, 84)#全連接層,輸入120個(gè)特征輸出84個(gè)特征
        self.fc3 = nn.Linear(84, 7)#全連接層,輸入84個(gè)特征輸出7個(gè)特征
 
   def forward(self, x):
        print("x.shape1: ", x.shape)
        x = self.pool(F.relu(self.conv1(x)))
        print("x.shape2: ", x.shape)
        x = self.pool(F.relu(self.conv2(x)))
        print("x.shape3: ", x.shape)
        x = x.view(-1, 16*77*77)
        print("x.shape4: ", x.shape)
        x = F.relu(self.fc1(x))
        print("x.shape5: ", x.shape)
        x = F.relu(self.fc2(x))
        print("x.shape6: ", x.shape)
        x = self.fc3(x)
        print("x.shape7: ", x.shape)
        return x


3.3、測(cè)試代碼

img_path = "TestData/test_data/1/Apple2 (1).jpg" #使用相對(duì)路徑
image = Image.open(img_path).convert('RGB')
image_pad = pad_image(image, (320, 320))
input = transform(image_pad).to(device).unsqueeze(0)
output = F.softmax(net(input), 1)
_, predicted = torch.max(output, 1)
score = float(output[0][predicted]*100)
print(class_map[predicted], " ", str(score)+" %")
plt.imshow(image_pad) # 顯示圖片

四、訓(xùn)練結(jié)果

4.1、LOSS損失函數(shù)

4.2、 ACC

4.3、單張圖片識(shí)別準(zhǔn)確率

四、小結(jié)

這次搭建的網(wǎng)絡(luò)是基于深度學(xué)習(xí)框架Lenet,并自己做了一些修改完成。最終的訓(xùn)練的結(jié)果LOSS接近0,ACC接近100%。但是一般的識(shí)別率不會(huì)達(dá)到這么高,該模型可能會(huì)過擬合??刹扇〖糁Φ炔僮鳒p小過擬合。

到此這篇關(guān)于Pytorch深度學(xué)習(xí)之實(shí)現(xiàn)病蟲害圖像分類的文章就介紹到這了,更多相關(guān)Pytorch圖像分類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論