当前位置:网站首页>【网络结构】VGG
【网络结构】VGG
2022-08-03 14:56:00 【可乐大牛】
概述
VGG的一个核心思想就是使用多个较小的卷积代替较大的卷积并且能够保证感受野相同(如2个连续的3×3卷积核能够替代一个5×5卷积核,三个连续的3×3能够代替一个7×7卷积核)。
这样的操作可以取得一个比较好结果的原因如下:
1、相同感受野的情况下,多个小卷积的叠加,加深了网络,当然核心是引入了更多的非线性,模型的表征能力增强。
2、参数更少。为什么2个连续的3×3卷积核与一个5×5卷积核感受野相同?
5x5卷积,步长取1,padding取0,尺寸变化: ( x − 5 ) / 1 + 1 = x − 4 (x-5)/1+1=x-4 (x−5)/1+1=x−4
2个3x3卷积,步长取1,padding取0,尺寸变化: ( x − 3 ) / 1 + 1 = x − 2 , ( x − 2 − 3 ) / 1 + 1 = x − 4 (x-3)/1+1=x-2,(x-2-3)/1+1=x-4 (x−3)/1+1=x−2,(x−2−3)/1+1=x−4
为什么参数更少?
一个5x5的卷积的参数是 5 ∗ 5 ∗ C 5*5*C 5∗5∗C,而两个3x3卷积的参数是 2 ∗ 3 ∗ 3 ∗ C 2*3*3*C 2∗3∗3∗C
网络结构

最常用的就是VGG16,也就是上图的D,下图是VGG16直观一点的结构图。
实现
import paddle
import paddle.nn as nn
paddle.set_device("cpu")
class VGG(nn.Layer):
# arch表述每一个stage的卷积个数
def __init__(self, arch,num_classes=1000):
super().__init__()
self.in_channels=3
self.conv3_64=self.make_layer(64,arch[0])
self.conv3_128=self.make_layer(128,arch[1])
self.conv3_256=self.make_layer(256,arch[2])
self.conv3_512a=self.make_layer(512,arch[3])
self.conv3_512b=self.make_layer(512,arch[4])
self.pool=nn.MaxPool2D(2)
self.fc1=nn.Linear(512*7*7, 4096)
self.bn1=nn.BatchNorm1D(4096)
self.fc2=nn.Linear(4096, 4096)
self.bn2=nn.BatchNorm1D(4096)
self.fc3=nn.Linear(4096, num_classes)
def make_layer(self,channels,nums):
layers=[]
for i in range(nums):
layers.append(nn.Conv2D(self.in_channels,channels,3,1,1))
layers.append(nn.BatchNorm2D(channels))
layers.append(nn.ReLU())
self.in_channels=channels
return nn.Sequential(*layers)
def forward(self,x):
# x:[n,3,224,224]
print(x.shape)
x=self.conv3_64(x) # x:[n,3,224,224]->[n,64,224,224]
x=self.conv3_128(self.pool(x)) # x:[n,64,224,224]->[n,128,112,112]
x=self.conv3_256(self.pool(x)) # x:[n,128,112,112]->[n,256,56,56]
x=self.conv3_512a(self.pool(x)) # x:[n,256,56,56]->[n,512,28,28]
x=self.conv3_512b(self.pool(x)) # x:[n,512,28,28]->[n,512,14,14]
x=self.pool(x).flatten(1) # x:[n,512,14,14]->[n,512,7,7]->[n,512*7*7]
x=self.fc1(x) # [n,512*7*7]->[n,4096]
x=self.bn1(x)
x=self.fc2(x) # [n,4096]->[n,4096]
x=self.bn2(x)
x=self.fc3(x) # [n,4096]->[n,1000]
return x
def VGG_11():
return VGG([1, 1, 2, 2, 2])
def VGG_13():
return VGG([1, 1, 2, 2, 2])
def VGG_16():
return VGG([2, 2, 3, 3, 3])
def VGG_19():
return VGG([2, 2, 4, 4, 4])
def main():
vgg=VGG_16()
paddle.summary(vgg, (1,3,224,224))
if __name__ == '__main__':
main()
边栏推荐
猜你喜欢
随机推荐
你把 浏览器滚动事件 玩明白
JS每晚24:00更新某方法
Day2:面试必考题目
一个在浏览器中看到的透视Cell实现
理解string类
gocron定时任务管理系统的安装与运行
一文搞懂$_POST和php://input的区别
Leetcode 448. Find All Numbers Disappeared in an Array to Find All Disappeared in an Array of Numbers (simple)
Clickhouse填坑记3:Left Join改Right Join导致统计结果错误
问题4:什么是缺陷?你们公司缺陷的优先级是怎样划分的?
2022-随便学学
HDU 1027 Ignatius and the Princess II(求由1-n组成按字典序排序的第m个序列)
【问题】torch和torchvision对应版本
基于ModelArts的动漫头像自动生成丨【华为云至简致远】
输出一个整数的二进制形式
HDU 1029 Ignatius and the Princess IV
UE4 C disk cache solution
今日睡眠质量记录75分
A high-performance creation book, ASUS Dreadnought Pro15 2022 is completely enough for daily photo editing and editing!
问题7:功能测试花瓶用例









