设计具有2个卷积层,2个残差单元和一个全连接层,然后是分类层的CNN模型

时间:2019-03-26 01:12:06

标签: convolution resnet deep-residual-networks

在这里,我正在尝试设计一个具有2个卷积层,2个残差单元和一个完全连接的层,然后是一个分类层的CNN模型。我们使用

-3x3卷积内核(resnet单元的步幅为1,卷积层的步幅为2) -ReLU用于激活功能 -max仅在卷积层之后才使用内核2x2和跨度2的池。

我想在隐藏层中定义要素地图的数量为:16、16、16、32、32、32、64(第1层,...,第7层)。

输入->卷积1-> ResNetBlock1->卷积2-> ResNetBlock2-> FC->输出

我在这里写了我的代码。注意:我经常参考本教程。

http://www.pabloruizruiz10.com/resources/CNNs/ResNet-PyTorch.html

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision

#3x3 kernel=3
def conv3x3(in_channels, out_channels, stride=1):
    return nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        #pooling 2
        self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2, padding=1)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2, padding=1)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
            residual = x

            out = self.conv1(x)
            out = self.maxpool(out)
            out = self.relu(out)

            out = self.conv2(out)
            out = self.maxpool(out)

            if self.downsample is not None:
                residual = self.downsample(x)

            out += residual
            out = self.relu(out)

            return out

class Convnet_Resnet_Layer(nn.Module):

    def __init__(self, block, layers, num_classes=2):
        self.inplanes = 1
        super(Convnet_Resnet_Layer, self).__init__()
        self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=3,
                               bias=False)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 16, layers[0])
        self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=3,
                               bias=False)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        #connect 64 on the last layer, with the 2 blocks
        self.fc = nn.Linear(64 * block.expansion, num_classes)

        # make sure that dimensions check out. 
    def _make_layer(self, block, planes, blocks, stride=1):
            downsample = None
            if stride != 1 or self.inplanes != planes * block.expansion:
                downsample = nn.Sequential(
                    nn.Conv2d(self.inplanes, planes * block.expansion,
                              kernel_size=1, stride=stride, bias=False),
                )

            layers = []
            layers.append(block(self.inplanes, planes, stride, downsample))
            self.inplanes = planes * block.expansion
            for i in range(1, blocks):
                layers.append(block(self.inplanes, planes))

            return nn.Sequential(*layers)

    def forward(self, x):
            x = self.conv1(x)    # 224x224   
            x = self.relu(x)
            x = self.maxpool(x)  # 112x112
            x = self.layer1(x)   # 56x56

            x = self.conv2(x)        
            x = self.relu(x)
            x = self.maxpool(x)  
            x = self.layer2(x)   # 28x28

            # I did average pooling here. Can just omit if you want, but parameters will change
            x = self.avgpool(x)  # 1x1

            x = x.view(x.size(0), -1)
            x = self.out(x)

            return x
# call the model and pass the list with the information of the blocks for each layer
model1 = Convnet_Resnet_Layer(BasicBlock, [2, 3,4 ])
print(model1)

结果如下:

Convnet_Resnet_Layer(   (conv1):Conv2d(1,16,kernel_size =(3,3),步幅=(2,2),padding =(3,3),bias = False)   (relu):ReLU(inplace)   (maxpool):MaxPool2d(kernel_size = 3,stride = 2,padding = 1,dilation = 1,ceil_mode = False)   (第1层):顺序(     (0):基本块(       (conv1):Conv2d(1,16,kernel_size =(3,3),步幅=(1,1),padding =(1,1),bias = False)       (maxpool):MaxPool2d(kernel_size = 2,stride = 2,padding = 1,dilation = 1,ceil_mode = False)       (relu):ReLU(inplace)       (conv2):Conv2d(16,16,kernel_size =(3,3),步幅=(1,1),填充=(1,1),bias = False)       (降采样):顺序(         (0):Conv2d(1、16,kernel_size =(1、1),步幅=(1、1),bias = False)       )     )     (1):BasicBlock(       (conv1):Conv2d(16,16,kernel_size =(3,3),步幅=(1,1),填充=(1,1),bias = False)       (maxpool):MaxPool2d(kernel_size = 2,stride = 2,padding = 1,dilation = 1,ceil_mode = False)       (relu):ReLU(inplace)       (conv2):Conv2d(16,16,kernel_size =(3,3),步幅=(1,1),填充=(1,1),bias = False)     )   )   (conv2):Conv2d(32,32,kernel_size =(3,3),步幅=(2,2),padding =(3,3),bias = False)   [fc):线性(in_features = 64,out_features = 2,bias = True) )

在(conv2)之后,我没有最大池或没有显示在模型中。

我将不胜感激。

0 个答案:

没有答案