Pytorch:VGG16

import torch
import torch.nn as nn
import torch.nn.functional as F
 
 
class VGG16(nn.Module):
    
    
    def __init__(self):
        super(VGG16, self).__init__()
        
        #输入图片大小为:3 * 224 * 224    
        self.conv1_1 = nn.Conv2d(3, 64, 3) # 64 * 222 * 222                               (224 - 3 + 2*0)/1 + 1 = 222      
        self.conv1_2 = nn.Conv2d(64, 64, 3, padding=(1, 1)) # 64 * 222* 222               (222 - 3 + 2*1)/1 + 1 = 222
        self.maxpool1 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 64 * 112 * 112     (222 - 2 + 2*1)/2 + 1 = 112
        
        self.conv2_1 = nn.Conv2d(64, 128, 3) # 128 * 110 * 110                            (112 - 3 + 2*0)/1 + 1 =110
        self.conv2_2 = nn.Conv2d(128, 128, 3, padding=(1, 1)) # 128 * 110 * 110           (110 - 3 + 2*1)/1 + 1 =110
        self.maxpool2 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 128 * 56 * 56      (110 - 2 + 2*1)/2 + 1 = 56
        
        self.conv3_1 = nn.Conv2d(128, 256, 3) # 256 * 54 * 54                             (56 - 3 + 2*0)/1 + 1 = 54
        self.conv3_2 = nn.Conv2d(256, 256, 3, padding=(1, 1)) # 256 * 54 * 54             (54 - 3 + 2*1)/1 + 1 = 54
        self.conv3_3 = nn.Conv2d(256, 256, 3, padding=(1, 1)) # 256 * 54 * 54             (54 - 3 + 2*1)/1 + 1 = 54
        self.maxpool3 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 256 * 28 * 28      (54 - 2 + 2*1)/2 + 1 = 28
        
        self.conv4_1 = nn.Conv2d(256, 512, 3) # 512 * 26 * 26                             (28 - 3 + 2*0)/1 + 1 = 26
        self.conv4_2 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 26 * 26             (26 - 3 + 2*1)/1 + 1 = 26
        self.conv4_3 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 26 * 26             (26 - 3 + 2*1)/1 + 1 = 26
        self.maxpool4 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 512 * 14 * 14      (26 - 2 + 2*1)/2 + 1 = 14
        
        self.conv5_1 = nn.Conv2d(512, 512, 3) # 512 * 12 * 12                             (14 - 3 + 2*0)/1 + 1 = 12
        self.conv5_2 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 12 * 12             (12 - 3 + 2*1)/1 + 1 = 12 
        self.conv5_3 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 12 * 12             (12 - 3 + 2*1)/1 + 1 = 12
        self.maxpool5 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 512 * 7 * 7        (12 - 2 + 2*1)/2 + 1 =7
        
        # view
        
        self.fc1 = nn.Linear(512 * 7 * 7, 4096)    #512 * 7 * 7 = 25088 ————> 4096
        self.fc2 = nn.Linear(4096, 4096)           #4096 ————> 4096
        self.fc3 = nn.Linear(4096, 1000)           #4096 ————> 1000
        # softmax 1 * 1 * 1000
        
    def forward(self, x):
        
        # x.size(0)即为batch_size
        in_size = x.size(0)
        
        out = self.conv1_1(x) # 222
        out = F.relu(out)
        out = self.conv1_2(out) # 222
        out = F.relu(out)
        out = self.maxpool1(out) # 112
        
        out = self.conv2_1(out) # 110
        out = F.relu(out)
        out = self.conv2_2(out) # 110
        out = F.relu(out)
        out = self.maxpool2(out) # 56
        
        out = self.conv3_1(out) # 54
        out = F.relu(out)
        out = self.conv3_2(out) # 54
        out = F.relu(out)
        out = self.conv3_3(out) # 54
        out = F.relu(out)
        out = self.maxpool3(out) # 28
        
        out = self.conv4_1(out) # 26
        out = F.relu(out)
        out = self.conv4_2(out) # 26
        out = F.relu(out)
        out = self.conv4_3(out) # 26
        out = F.relu(out)
        out = self.maxpool4(out) # 14
        
        out = self.conv5_1(out) # 12
        out = F.relu(out)
        out = self.conv5_2(out) # 12
        out = F.relu(out)
        out = self.conv5_3(out) # 12
        out = F.relu(out)
        out = self.maxpool5(out) # 7
        
        # 展平
        out = out.view(in_size, -1)
        
        out = self.fc1(out)
        out = F.relu(out)
        out = self.fc2(out)
        out = F.relu(out)
        out = self.fc3(out)
        
        out = F.log_softmax(out, dim=1)
        
        
        return out
  1. 输入图像尺寸为224x224x3,经64个通道为3的3x3的卷积核,步长为1,padding=same填充,卷积两次,再经ReLU激活,输出的尺寸大小为224x224x64
  2. 经max pooling(最大化池化),滤波器为2x2,步长为2,图像尺寸减半,池化后的尺寸变为112x112x64
  3. 经128个3x3的卷积核,两次卷积,ReLU激活,尺寸变为112x112x128
  4. max pooling池化,尺寸变为56x56x128
  5. 经256个3x3的卷积核,三次卷积,ReLU激活,尺寸变为56x56x256
  6. max pooling池化,尺寸变为28x28x256
  7. 经512个3x3的卷积核,三次卷积,ReLU激活,尺寸变为28x28x512
  8. max pooling池化,尺寸变为14x14x512
  9. 经512个3x3的卷积核,三次卷积,ReLU,尺寸变为14x14x512
  10. max pooling池化,尺寸变为7x7x512
  11. 然后view(),将数据拉平成向量,变成一维51277=25088。
  12. 再经过两层1x1x4096,一层1x1x1000的全连接层(共三层),经ReLU激活
  13. 最后通过softmax输出1000个预测结果
    在这里插入图片描述
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁天牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值