天天看點

網絡結構總結1-Lenet:Lenet-5網絡結構說明及pytorch實作

1.網絡結構

1.1論文結構

網絡結構總結1-Lenet:Lenet-5網絡結構說明及pytorch實作

1.2簡化說明

說明:共7層(3Conv+2pool+2FC)

網絡結構總結1-Lenet:Lenet-5網絡結構說明及pytorch實作

2.作用

2.1各個部分的作用

卷積:特征提取。

(說明:同一卷積層中不同通道學習的是同一尺度下的多種特征,淺層卷積和深層卷積學習到的是多種尺度的特征,且深層次的卷積學習到的特征更抽象。)

激活函數:引入非線性,提高模型的表達能力

池化(降采樣):

特征壓縮:降低模型複雜度防止過拟合+提取主要特征。

不變性:translation(平移:不同位置),rotation(旋轉:不同角度),scale(尺度:放縮)。

全連接配接:特征整合(減少特征位置對分類帶來的影響)。

2.2特點

局部感覺(連接配接):每個輸出僅與輸入的一塊區域連接配接(了解感受野的概念),這個概念相對于全連接配接網絡。

權值共享:卷積過程中卷積核的權重是不會改變,提取的是不同位置的同樣特征。

作用:減少參數數量,加快學習速率,一定程度上減少過拟合。

3.pytorch實作

3.1網絡結構:激活函數改用Relu

import torch.nn as nn
import torch
from torchsummary import summary
from torch.utils.data import DataLoader
from torchvision import transforms,datasets

class LenetNet(nn.Module):
    def __init__(self, num_classes=10):
        super(LenetNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 6, 5),
            nn.ReLU(inplace=True),
            nn.AvgPool2d(kernel_size=2, stride=2),
            nn.Conv2d(6, 16, 5),
            nn.ReLU(inplace=True),
            nn.AvgPool2d(kernel_size=2, stride=2),
            nn.Conv2d(16, 120, 5),
            nn.ReLU(inplace=True),
        )
        self.classifier = nn.Sequential(
            nn.Linear(120, 84),
            nn.ReLU(inplace=True),
            nn.Linear(84, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
#        print(x.shape)
        x = x.view(x.size(0), 120 * 1 * 1)
        x = self.classifier(x)
        return x
           

3.2列印網絡結構

if __name__ == "__main__": 
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model=LenetNet().to(device)
    summary(model, (1,32, 32)) #列印網絡結構
           

結果顯示

網絡結構總結1-Lenet:Lenet-5網絡結構說明及pytorch實作

3.3 mnist資料集測試

if __name__ == "__main__": 
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    learning_rate=0.001
    num_epochs = 1               # train the training data n times, to save time, we just train 1 epoch
    batch_size = 32
    LR = 0.001              # learning rate
    DOWNLOAD_MNIST = False
    
    data_tf = transforms.Compose(
        [transforms.Resize([32, 32]),
         transforms.ToTensor(),
         transforms.Normalize([0.5], [0.5])])

    train_dataset = datasets.MNIST(
        root='mnist', train=True, transform=data_tf)
    test_dataset = datasets.MNIST(root='mnist', train=False, transform=data_tf)
    train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
    test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
    
    model=LenetNet().to(device)
    #summary(model, (1,32, 32)) #列印網絡結構
     
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
     
    total_step = len(train_loader)
    for epoch in range(num_epochs):
        for i, (images, labels) in enumerate(train_loader):

            images = images.to(device)
            labels = labels.to(device)
     
            # Forward pass
            outputs = model(images)
            loss = criterion(outputs, labels)
     
            # Backward and optimize
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
     
            if (i + 1) % 100 == 0:
                print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
                      .format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))
            
    # Test the model
    model.eval()  #
    with torch.no_grad():
        correct = 0
        total = 0
        for images, labels in test_loader:
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

        print('Test Accuracy  {} %'.format(100 * correct / total))
           

結果

網絡結構總結1-Lenet:Lenet-5網絡結構說明及pytorch實作

繼續閱讀