Pytorch深度学习:卷积神经网络(基础篇)

基本概念:

        全连接网络

                整个网络都使用全连接网络进行连接,则成为全连接网络

        卷积神经网络:

                直接对图像进行操作,保留了更多的图像特征,前面的卷积和采样部分称为特征提取,           后面的全连接部分称为分类

Pytorch深度学习:卷积神经网络(基础篇)_第1张图片

卷积层:

        使用RGB图像,输入通道是3,输出通道是卷积核的数量

Pytorch深度学习:卷积神经网络(基础篇)_第2张图片

单通道计算方式:

        对应相乘相加

Pytorch深度学习:卷积神经网络(基础篇)_第3张图片

 多通道运算方式:

        再次累加

Pytorch深度学习:卷积神经网络(基础篇)_第4张图片

 一个拥有三个通道的输入图像经过卷积处理后变成了单通道的输出图像

Pytorch深度学习:卷积神经网络(基础篇)_第5张图片

单个卷积核得到单通道:

Pytorch深度学习:卷积神经网络(基础篇)_第6张图片 

多个卷积核得到多通道:

Pytorch深度学习:卷积神经网络(基础篇)_第7张图片 

padding操作:

在输入层外围加自己需要多少层的0

Pytorch深度学习:卷积神经网络(基础篇)_第8张图片 

stride操作:

stride为2,每次移动两个单位,有效解决长度和宽度问题

Pytorch深度学习:卷积神经网络(基础篇)_第9张图片

 maxpooling操作:

2x2默认stride为2 

Pytorch深度学习:卷积神经网络(基础篇)_第10张图片

一个简单的例子:

Pytorch深度学习:卷积神经网络(基础篇)_第11张图片

 代码实现:

import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt


batch_size = 64

transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.1307,),(0.3081,))])

train_dataset = datasets.MNIST(root = '../dataset/mnist',
                              train = True,
                              download = True,
                              transform = transform)

train_loader = DataLoader(train_dataset,
                          shuffle = True,
                          batch_size = batch_size)

test_dataset = datasets.MNIST(root = '../dataset/mnist',
                              train = False,
                              download = True,
                              transform = transform)

test_loader = DataLoader(test_dataset,
                         shuffle = True,
                         batch_size = batch_size)



class Net (torch.nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = torch.nn.Conv2d(1,10,kernel_size=5)
        self.conv2 = torch.nn.Conv2d(10,20,kernel_size=5)
        self.pooling = torch.nn.MaxPool2d(2)
        self.fc = torch.nn.Linear(320,10)

    def forward (self,x):
        batch_size = x.size(0)
        x = self.pooling(F.relu(self.conv1(x)))
        x = self.pooling(F.relu(self.conv2(x)))
        x = x.view(batch_size,-1)
        x = self.fc(x)

        return x


model = Net()

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)


criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(),lr = 0.01,momentum = 0.5)


def train(epoch):
    running_loss = 0.0
    for batch_idx, data in enumerate(train_loader, 0):
        inputs, target = data
        inputs, target = inputs.to(device), target.to(device)
        optimizer.zero_grad()
 
        outputs = model(inputs)
        loss = criterion(outputs, target)
        loss.backward()
        optimizer.step()
 
        running_loss += loss.item()
        if batch_idx % 300 == 299:
            print('[%d, %5d] loss: %.3f' % (epoch+1, batch_idx+1, running_loss/300))
            running_loss = 0.0
 
 
def test():
    correct = 0
    total = 0
    with torch.no_grad():
        for data in test_loader:
            images, labels = data
            images, labels = images.to(device), labels.to(device)
            outputs = model(images)
            _, predicted = torch.max(outputs.data, dim=1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    print('accuracy on test set: %d %% ' % (100*correct/total))
    return correct/total
 
 
if __name__ == '__main__':
    epoch_list = []
    acc_list = []
    
    for epoch in range(10):
        train(epoch)
        acc = test()
        epoch_list.append(epoch)
        acc_list.append(acc)
    
    plt.plot(epoch_list,acc_list)
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.show()
 

打印图表:

Pytorch深度学习:卷积神经网络(基础篇)_第12张图片

 

 

 

你可能感兴趣的:(大数据,python,pytorch,深度学习,计算机视觉)