pytorch深度学习实践11——卷积神经网络(高级篇)

在这里插入图片描述

  • 一个块:Inception
    在这里插入图片描述

  • 1x1卷积最主要的工作:改变通道数,减小复杂度

  • Inception的实现

class InceptionA(nn.Module):
  def __init__(self.in_channels):
    super(InceptionA, self).__init__()
    self.branch1x1 = nn.Conv2d(in_channels, 16, kernel_size=1)

    self.branch5X5_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
    self.branch5X5_2 = nn.Conv2d(16, 24, kernel_size=5,padding=2)

    self.branch3x3_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
    self.branch3x3_1 = nn.Conv2d(16, 24, kernel_size=3,padding=1)
    self.branch3x3_1 = nn.Conv2d(24, 24, kernel_size=3,padding=1)

    self.branch_pool = nn.Conv2d(in_channels, 24, kernel_size=1)

  def forward(self, x):
    branch1x1 = self.branch1x1(x)

    branch5x5 = self.branch5x5_1(x)
    branch5x5 = self.branch5x5_2(branch5x5)

    branch3x3 = self.branch3x3_1(x)
    branch3x3 = self.branch3x3_2(branch3x3)
    branch3x3 = self.branch3x3_3(branch3x3)

    branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
    branch_pool = self.branch_pool(branch_pool)
    outputs = [branch1x1, branch5x5, branch3x3, branch_pool]
    return torch.cat(outputs, dim=1)
class Net(nn.Module):
   def __init__(self):
       super(Net, self).__init__()
       self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
       self.conv2 = nn.Conv2d(88, 20, kernel_size =5)

       self.incep1 = InceptionA(in_channels=10)
       self.incep2 = InceptionA(in_channels=20)

       self.mp = nn.MaxPool2d(2)    #写代码的时候为了准确这里最好不要手算
       self.fc = nn.Linear(1408, 10) #同上


  def forward(self, x):
    in_size = x.size(0)
    x = F.relu(self.mp(self.conv1(x)))
    x = self.incep1(x)
    x = F.relu(self.mp(self.conv2(x)))
    x = self.incep2(x)
    x = x.view(1408, 10)    #如上所说   in_size=1408
    x = self.fc(x)
    return x
  • 解决梯度消失问题
    • residual block
      在这里插入图片描述
    • 实现 residual block
      在这里插入图片描述
class ResidualBlock(nn.Module):
  def __init__(self, channels):
    super(ResidualBlock, self).__init__()
    slef.channels = channels
    self.conv1 = nn.Conv2d(channels, channels, kernel_size=3,padding=1)
    self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)

  def forward(self, x):
    y = F.relu(self.conv1(x))
    y = self.conv2(y)
    return F.relu(x+y)
class Net(nn.Module):
   def __init__(self):
       super(Net, self).__init__()
       self.conv1 = nn.Conv2d(1, 16, kernel_size=5)
       self.conv2 = nn.Conv2d(16, 32, kernel_size =5)
       self.mp = nn.MaxPool2d(2)  
        
       self.rblock1 = ResidualBlock(16)
       self.rblock2 = ResidualBlock(32)

       self.fc = nn.Linear(512, 10) 


  def forward(self, x):
    in_size = x.size(0)
    x = self.mp(F.relu(self.conv1(x)))
    x = self.rblock1(x)
    x = self.mp(F.relu(self.conv2(x)))
    x = self.rblock2(x)
    x = x.view(in_size, -1)    #如上所说   in_size=1408
    x = self.fc(x)
    return x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值