pytorch 卷积神经网络

卷积神经网络的原理和结构

  1. 局部性:特征识别通常情况下由局部的区域确定
  2. 相同性:如果具有相同的特征,这些特征处在不同的位置不影响特征检测。
  3. 不变性:下采样,图片的性质基本保持不变。

 

卷积层:是卷积神经网咯的核心,大多数计算都是在卷积层中进行的。

第一个卷积层的宽度和高度可以自定义,但是深度需和图片的通道一致。

 

感受野(receptive field):与神经元链接的空间大小,即滤波器的宽和高。

 

Pytorch卷积层:

torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

shape:
input: (N,C_in,H_in,W_in) 
output: (N,C_out,H_out,W_out)

常用参数:

in_channels,:      输入数据体的深度

out_channels:       输出数据体的深度

kernel_size:           卷积核的大小,例如:kernel_size= 3或者kernel_size=(3, 2)

stride:                   滑动的歩长

padding:               padding=value,value即多少个像素点进行填充

bias:                     是否使用偏置,默认为bias=True

groups:                 从输入通道到输出通道的阻塞连接数

dilation                 卷积核元素之间的间距,这里的间距默认为1,有点奇怪。具体见:

https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md

重点理解下dilation:

# -*- coding: utf-8 -*-
'''
Created on 2018年10月2日

@author: plus
'''
import torch
from torch.autograd import Variable
import torch.nn as nn



m = nn.Conv2d(3, 10, 3, stride=2, dilation = 1)
input = Variable(torch.randn(1, 3, 27, 27))
output = m(input)
print(output.shape)

torch.Size([1, 10, 13, 13])

与公式计算一致,在dilation=1的情况下。

pytorch池化层:

torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)

常用参数:

部分同上

return_indices:      是否返回输出最大值的序号,对于上采样会有帮助。

ceil_mode:            True,计算输出信号大小的时候,会使用向上取整,默认为False,向下取整。

还有个接口是均值池化:torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)

以下是范例:

# -*- coding: utf-8 -*-
'''
Created on 2018年10月2日

@author: plus
'''
import torch

class SimpleCNN(torch.nn.Module):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        super(SimpleCNN, self).__init__() #b, 3, 32, 32
        layer1 = torch.nn.Sequential()
        layer1.add_module('conv1', torch.nn.Conv2d(3, 32, 3, 1, padding=1))
        #b, 32, 32, 32
        
        layer1.add_module('relu1', torch.nn.ReLU(True))
        layer1.add_module('pool1', torch.nn.MaxPool2d(2, 2))  #b, 32, 16, 16
        self.layer1 = layer1
        
        
        layer2 = torch.nn.Sequential()
        layer2.add_module('conv2', torch.nn.Conv2d(32, 64, 3, 1, padding=1))
        #b, 64, 16, 16
        
        layer2.add_module('relu2', torch.nn.ReLU(True))
        layer2.add_module('pool2', torch.nn.MaxPool2d(2, 2))  #b, 64, 8, 8
        self.layer2 = layer2
        
        
        layer3 = torch.nn.Sequential()
        layer3.add_module('conv3', torch.nn.Conv2d(64, 128, 3, 1, padding=1))
        #b, 128, 8, 8
        
        
        layer3.add_module('relu3', torch.nn.ReLU(True))
        layer3.add_module('pool3', torch.nn.MaxPool2d(2, 2))  #b, 128, 4, 4
        self.layer3 = layer3
        
        
        layer4 = torch.nn.Sequential()
        layer4.add_module('fc1', torch.nn.Linear(2048, 512))
        layer4.add_module('fc_relu1', torch.nn.ReLU(True))
        layer4.add_module('fc2', torch.nn.Linear(512, 64))
        layer4.add_module('fc_relu2', torch.nn.ReLU(True))
        layer4.add_module('fc3', torch.nn.Linear(64, 10))
        self.layer4 = layer4
        
        
        
    def forward(self, x):
        conv1 = self.layer1(x)
        conv2 = self.layer1(conv1)
        conv3 = self.layer1(conv2)
        fc_input = conv3.view(conv3.size(0), -1)
        fc_out = self.layer4(fc_input)
        return fc_out
    
model = SimpleCNN()
print(model)

SimpleCNN(
  (layer1): Sequential(
    (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (relu1): ReLU(inplace)
    (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (layer2): Sequential(
    (conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (relu2): ReLU(inplace)
    (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (layer3): Sequential(
    (conv3): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (relu3): ReLU(inplace)
    (pool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (layer4): Sequential(
    (fc1): Linear(in_features=2048, out_features=512, bias=True)
    (fc_relu1): ReLU(inplace)
    (fc2): Linear(in_features=512, out_features=64, bias=True)
    (fc_relu2): ReLU(inplace)
    (fc3): Linear(in_features=64, out_features=10, bias=True)
  )
)
参考:

https://pytorch-cn.readthedocs.io/zh/latest/

深度学习入门之pytorch

cs231n

猜你喜欢

转载自blog.csdn.net/zzb5233/article/details/82929243