Depth hands-on science learning - convolution neural network 2 LeNet

Here Insert Picture Description
White whore Bo Yu platform, or to thank about, playing wave advertising.
Mainly study notes, problem-solving can be ignored in this blog, so as not to waste time and effort
focuses on three areas

lenet model described in
lenet network structures
use an image recognition lenet dataset -fashion-mnist

LeNet model

LeNet layer block convolution two parts and the fully connected layer block. Below we describe these two modules.
The following model structure
Here Insert Picture Description
comprising: an input layer, a convolution of two layers, two hidden layer fully connected, and an output layer fully connected

Layer block convolution in the basic unit cell layer average access layer convolution: Convolution layer serves to identify the image in the spatial patterns, such as lines and local object, the average cell layer after layer is used to reduce the convolution the sensitivity of the location.

Layer block convolution of two such basic units are repeatedly stacked configuration. Layer block convolution, convolution in each layer uses a 5 × 5 window, and using sigmoid activation function on the output. The first convolutional layer output channels is 6, a second convolutional layer is increased to 16 the number of output channels.

Fully connected layer block contains 3 layers fully connected. The number of outputs thereof respectively 120,84 and 10, where 10 is the number of output classes.

Now we come to realize LeNet model by Sequential class.

#import
import sys
sys.path.append("/home/kesci/input")
import d2lzh1981 as d2l
import torch
import torch.nn as nn
import torch.optim as optim
import time
#net
class Flatten(torch.nn.Module):  #展平操作
    def forward(self, x):
        return x.view(x.shape[0], -1)

class Reshape(torch.nn.Module): #将图像大小重定型
    def forward(self, x):
        return x.view(-1,1,28,28)      #(B x C x H x W)
    
net = torch.nn.Sequential(     #Lelet                                                  
    Reshape(),
    nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2), #b*1*28*28  =>b*6*28*28
    nn.Sigmoid(),                                                       
    nn.AvgPool2d(kernel_size=2, stride=2),                              #b*6*28*28  =>b*6*14*14
    nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5),           #b*6*14*14  =>b*16*10*10
    nn.Sigmoid(),
    nn.AvgPool2d(kernel_size=2, stride=2),                              #b*16*10*10  => b*16*5*5
    Flatten(),                                                          #b*16*5*5   => b*400
    nn.Linear(in_features=16*5*5, out_features=120),
    nn.Sigmoid(),
    nn.Linear(120, 84),
    nn.Sigmoid(),
    nn.Linear(84, 10)
)

Next, we construct a single-channel data are the height and width of the sample 28, and the front layer by layer to the computing at the output from the shape of each layer.

print
X = torch.randn(size=(1,1,28,28), dtype = torch.float32)
for layer in net:
    X = layer(X)
    print(layer.__class__.__name__,'output shape: \t',X.shape)

每层的尺寸输出
Reshape output shape: torch.Size([1, 1, 28, 28])
Conv2d output shape: torch.Size([1, 6, 28, 28])
Sigmoid output shape: torch.Size([1, 6, 28, 28])
AvgPool2d output shape: torch.Size([1, 6, 14, 14])
Conv2d output shape: torch.Size([1, 16, 10, 10])
Sigmoid output shape: torch.Size([1, 16, 10, 10])
AvgPool2d output shape: torch.Size([1, 16, 5, 5])
Flatten output shape: torch.Size([1, 400])
Linear output shape: torch.Size([1, 120])
Sigmoid output shape: torch.Size([1, 120])
Linear output shape: torch.Size([1, 84])
Sigmoid output shape: torch.Size([1, 84])
Linear output shape: torch.Size([1, 10])
It can be seen in the input layer block convolution height and the width decreases layer by layer. The use of convolutional layer height and width are 5 convolution kernel, thereby reducing the height and width, respectively 4, while the pool height and width of the layer will be halved, but the number of channels from 1 to 16. Layer is fully connected reduce the number of output-layer until it becomes the image number of categories 10.
Here Insert Picture Description

Access to data and model train

Achieve LeNet model. Fashion-MNIST we still use as a training data set.

# 数据
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(
    batch_size=batch_size, root='/home/kesci/input/FashionMNIST2065')
print(len(train_iter))

Adding an extra part to display the image data

#数据展示
import matplotlib.pyplot as plt
def show_fashion_mnist(images, labels):
    d2l.use_svg_display()
    # 这里的_表示我们忽略(不使用)的变量
    _, figs = plt.subplots(1, len(images), figsize=(12, 12))
    for f, img, lbl in zip(figs, images, labels):
        f.imshow(img.view((28, 28)).numpy())
        f.set_title(lbl)
        f.axes.get_xaxis().set_visible(False)
        f.axes.get_yaxis().set_visible(False)
    plt.show()

for Xdata,ylabel in train_iter:
    break
X, y = [], []
for i in range(10):
    print(Xdata[i].shape,ylabel[i].numpy())
    X.append(Xdata[i]) # 将第i个feature加到X中
    y.append(ylabel[i].numpy()) # 将第i个label加到y中
show_fashion_mnist(X, y)

torch.Size ([. 1, 28, 28]). 3
torch.Size ([. 1, 28, 28]). 8
torch.Size ([. 1, 28, 28]). 1
torch.Size ([. 1, 28, 28] ). 4
torch.Size ([. 1, 28, 28]) 0
torch.Size ([. 1, 28, 28]) 0
torch.Size ([. 1, 28, 28]). 4
torch.Size ([. 1, 28, 28]). 9
torch.Size ([. 1, 28, 28]). 4
torch.Size ([. 1, 28, 28]). 7
Here Insert Picture Description
as the convolution neural network computing confidential complex than multi-layer perceptron, using a GPU acceleration calculated recommended . Check to see whether we can use the GPU, if successful use cuda: 0, otherwise still use cpu.

# This function has been saved in the d2l package for future use
#use GPU
def try_gpu():
    """If GPU is available, return torch.device as cuda:0; else return torch.device as cpu."""
    if torch.cuda.is_available():
        device = torch.device('cuda:0')
    else:
        device = torch.device('cpu')
    return device

device = try_gpu()
device

Achieve evaluate_accuracy function, which is used to calculate net model data set on the accuracy of data_iter.

#计算准确率
'''
(1). net.train()
  启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为True
(2). net.eval()
不启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为False
'''

def evaluate_accuracy(data_iter, net,device=torch.device('cpu')):
    """Evaluate accuracy of a model on the given data set."""
    acc_sum,n = torch.tensor([0],dtype=torch.float32,device=device),0
    for X,y in data_iter:
        # If device is the GPU, copy the data to the GPU.
        X,y = X.to(device),y.to(device)
        net.eval()
        with torch.no_grad():
            y = y.long()
            acc_sum += torch.sum((torch.argmax(net(X), dim=1) == y))  #[[0.2 ,0.4 ,0.5 ,0.6 ,0.8] ,[ 0.1,0.2 ,0.4 ,0.3 ,0.1]] => [ 4 , 2 ]
            n += y.shape[0]
    return acc_sum.item()/n

We define the function train_ch5, used to train the model.

#训练函数
def train_ch5(net, train_iter, test_iter,criterion, num_epochs, batch_size, device,lr=None):
    """Train and evaluate a model with CPU or GPU."""
    print('training on', device)
    net.to(device)
    optimizer = optim.SGD(net.parameters(), lr=lr)
    for epoch in range(num_epochs):
        train_l_sum = torch.tensor([0.0],dtype=torch.float32,device=device)
        train_acc_sum = torch.tensor([0.0],dtype=torch.float32,device=device)
        n, start = 0, time.time()
        for X, y in train_iter:
            net.train()
            
            optimizer.zero_grad()
            X,y = X.to(device),y.to(device) 
            y_hat = net(X)
            loss = criterion(y_hat, y)
            loss.backward()
            optimizer.step()
            
            with torch.no_grad():
                y = y.long()
                train_l_sum += loss.float()
                train_acc_sum += (torch.sum((torch.argmax(y_hat, dim=1) == y))).float()
                n += y.shape[0]
        test_acc = evaluate_accuracy(test_iter, net,device)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, '
              'time %.1f sec'
              % (epoch + 1, train_l_sum/n, train_acc_sum/n, test_acc,
                 time.time() - start))

We will re-initialize the model parameters corresponding to the device device (cpu or cuda: 0) above, and using the random initialization Xavier. Loss functions and training algorithms are still using cross-entropy loss function and small quantities of stochastic gradient descent.

# 训练
lr, num_epochs = 0.9, 10

def init_weights(m):
    if type(m) == nn.Linear or type(m) == nn.Conv2d:
        torch.nn.init.xavier_uniform_(m.weight)

net.apply(init_weights)
net = net.to(device)

criterion = nn.CrossEntropyLoss()   #交叉熵描述了两个概率分布之间的距离,交叉熵越小说明两者之间越接近
train_ch5(net, train_iter, test_iter, criterion,num_epochs, batch_size,device, lr)
# test
for testdata,testlabe in test_iter:
    testdata,testlabe = testdata.to(device),testlabe.to(device)
    break
print(testdata.shape,testlabe.shape)
net.eval()
y_pre = net(testdata)
print(torch.argmax(y_pre,dim=1)[:10])
print(testlabe[:10])

to sum up:

Convolutional neural network is a network containing a convolution layer. LeNet the alternating layers and the maximum pool convolution contact layer fully connected layers for image classification.

Published 12 original articles · won praise 0 · Views 267

Guess you like

Origin blog.csdn.net/inventertom/article/details/104634916