1.概述
VGG網路在2014年的影像分類競賽中獲得第二名,驗證了小卷積核對特征的強大的提取能力,并且把卷積層深度推到了16-19層,使用3*3卷積核
2.網路結構

2.1 模型架構
以vgg16為例,16即13個卷積層+3個全連接層,需要注意的是,vgg輸入大小為固定的224*224的rgb影像,資料格式轉化如下圖

需要注意的是,作者在原文中特意提到bn層并不會增加準確性,反而會增加計算復雜度,因此不采用歸一化

3.代碼
import torch
import torch.nn as nn
import cv2
class Vgg16(nn.Module):
def __init__(self,num_classes):
super().__init__()
self.l1=self._conv2(3,63)
self.l2=self._conv2(64,128)
self.l3=self._conv3(128,256)
self.l4=self._conv3(256,512)
self.l5=self._conv3(512,512)
self.classifier=self._linear(7*7*512,num_classes)
def forward(self,x):
l1=self.l1(x)
l2=self.l2(l1)
l3=self.l3(l2)
l4=self.l4(l3)
l5=self.l5(l4)
c=self.classifier(l5)
y=nn.Softmax(-1)(c)
return y
def _conv2(self,in_channel,out_channel):
return nn.Sequential(nn.Conv2d(in_channel,out_channel,3,1,1),
nn.ReLU(),
nn.Conv2d(out_channel,out_channel,3,1,1),
nn.ReLU(),
nn.MaxPool2d(2,2)
)
def _conv3(self,in_channel,out_channel):
return nn.Sequential(nn.Conv2d(in_channel,out_channel,3,1,1),
nn.ReLU(),
nn.Conv2d(out_channel,out_channel,3,1,1),
nn.ReLU(),
nn.Conv2d(out_channel,out_channel,3,1,1),
nn.ReLU(),
nn.MaxPool2d(2,2))
def _linear(self,in_length,out):
return(nn.Linear(in_length,4096),
nn.ReLU(),
nn.Linear(4096,4096),
nn.ReLU(),
nn.Linear(4096,out))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301859.html
標籤:其他
