几个卷积神经网络Backbone网络结构
2021-06-10 本文已影响0人
zeolite
做个笔记
VGG16
import torch
from torch import nn
import numpy as np
class VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, 3, 1, 1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 128, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, 3, 1, 1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Conv2d(128, 256, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, 3, 1, 1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Conv2d(256, 512, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, 1, 1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Conv2d(512, 512, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, 1, 1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2)
)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(7, 7))
self.classifier = nn.Sequential(
nn.Linear(25088, 4096, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(4096, 4096, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(4096, 1000, bias=True)
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
if __name__ == '__main__':
image = np.random.randn(1, 3, 224, 224)
image = torch.tensor(image, dtype=torch.float32)
model = VGG16()
print(model)
output = model(image)
print(output.shape)
这么堆叠写是因为我想看结构呀,所以没有抽出公共代码。
AlexNet
import torch
from torch import nn
import numpy as np
class AlexNet(nn.Module):
def __init__(self):
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, 11, 4, 2),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2),
nn.Conv2d(64, 192, 5, 1, 2),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2),
nn.Conv2d(192, 384, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, 3, 1, 1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, 3, 1, 1),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2)
)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(6, 6))
self.classifier = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(9216, 4096, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(4096, 4096, bias=True),
nn.ReLU(inplace=True),
nn.Linear(4096, 1000, bias=True)
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
if __name__ == '__main__':
image = np.random.randn(1, 3, 224, 224)
image = torch.tensor(image, dtype=torch.float32)
model = AlexNet()
output = model(image)
print(output.shape)
ResNet18
import torch
from torch import nn
import numpy as np
class BasicBlock(nn.Module):
def __init__(self, in_channel, out_channel, stride=1, isDownsample=False):
super(BasicBlock, self).__init__()
self.isDownsample = isDownsample
self.conv1 = nn.Conv2d(in_channel, out_channel, 3, stride, 1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channel)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channel, out_channel, 3, 1, 1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channel)
self.downsample = None
if isDownsample:
self.downsample = nn.Sequential(
nn.Conv2d(in_channel, out_channel, 1, 2, bias=False),
nn.BatchNorm2d(out_channel)
)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.isDownsample:
out += self.downsample(x)
return out
class ResNet(nn.Module):
def __init__(self):
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(3, 2, padding=1)
self.layer1 = nn.Sequential(
BasicBlock(64, 64),
BasicBlock(64, 64)
)
self.layer2 = nn.Sequential(
BasicBlock(64, 128, stride=2, isDownsample=True),
BasicBlock(128, 128)
)
self.layer3 = nn.Sequential(
BasicBlock(128, 256, stride=2, isDownsample=True),
BasicBlock(256, 256)
)
self.layer4 = nn.Sequential(
BasicBlock(256, 512, stride=2, isDownsample=True),
BasicBlock(512, 512)
)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.fc = nn.Linear(512, 1000, bias=True)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
if __name__ == '__main__':
model = ResNet()
image = np.random.randn(1, 3, 224, 224)
image = torch.tensor(image, dtype=torch.float32)
output = model(image)
print(model)
print(output.shape)
GoogleLeNet
output是主干输出, output_1是第一个辅助输出,output_2是第二个辅助输出
import torch
from torch import nn
import numpy as np
class BasicConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, stride, padding=0):
super(BasicConv2d, self).__init__()
self.conv = nn.Conv2d(in_channel, out_channel, kernel_size, stride, padding, bias=False)
self.bn = nn.BatchNorm2d(out_channel)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return x
class InceptionAux(nn.Module):
def __init__(self, in_channel, out_channel):
super(InceptionAux, self).__init__()
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(4, 4))
self.conv = BasicConv2d(in_channel, out_channel, 1, 1)
self.fc1 = nn.Linear(2048, 1024, bias=True)
self.fc2 = nn.Linear(1024, 1000, bias=True)
def forward(self, x):
x = self.avgpool(x)
x = self.conv(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = self.fc2(x)
return x
class Inception(nn.Module):
def __init__(self, in_channel, out_branch1, out_branch2_1, out_branch2_2, out_branch3_1, out_branch3_2,
out_branch4):
super(Inception, self).__init__()
self.branch1 = BasicConv2d(in_channel, out_branch1, 1, 1)
self.branch2 = nn.Sequential(
BasicConv2d(in_channel, out_branch2_1, 1, 1),
BasicConv2d(out_branch2_1, out_branch2_2, 3, 1, 1)
)
self.branch3 = nn.Sequential(
BasicConv2d(in_channel, out_branch3_1, 1, 1),
BasicConv2d(out_branch3_1, out_branch3_2, 3, 1, 1)
)
self.branch4 = nn.Sequential(
nn.MaxPool2d(3, 1, padding=1),
BasicConv2d(in_channel, out_branch4, 1, 1)
)
def forward(self, x):
# print('0:', x.shape)
branch1 = self.branch1(x)
# print('1:', branch1.shape)
branch2 = self.branch2(x)
# print('2:', branch2.shape)
branch3 = self.branch3(x)
# print('3:', branch3.shape)
branch4 = self.branch4(x)
# print('4:', branch4.shape)
out = torch.cat((branch1, branch2, branch3, branch4), dim=1)
return out
class GoogLeNet(nn.Module):
def __init__(self):
super(GoogLeNet, self).__init__()
self.conv1 = BasicConv2d(3, 64, 7, 2, 3)
self.maxpool1 = nn.MaxPool2d(3, 2)
self.conv2 = BasicConv2d(64, 64, 1, 1)
self.conv3 = BasicConv2d(64, 192, 3, 1, 1)
self.maxpool2 = nn.MaxPool2d(3, 2)
self.inception3a = Inception(in_channel=192, out_branch1=64, out_branch2_1=96, out_branch2_2=128,
out_branch3_1=16, out_branch3_2=32, out_branch4=32)
self.inception3b = Inception(in_channel=256, out_branch1=128, out_branch2_1=128, out_branch2_2=192,
out_branch3_1=32, out_branch3_2=96, out_branch4=64)
self.maxpool3 = nn.MaxPool2d(3, 2)
self.inception4a = Inception(in_channel=480, out_branch1=192, out_branch2_1=96, out_branch2_2=208,
out_branch3_1=16, out_branch3_2=48, out_branch4=64)
self.inception4b = Inception(in_channel=512, out_branch1=160, out_branch2_1=112, out_branch2_2=224,
out_branch3_1=24, out_branch3_2=64, out_branch4=64)
self.inception4c = Inception(in_channel=512, out_branch1=128, out_branch2_1=128, out_branch2_2=256,
out_branch3_1=24, out_branch3_2=64, out_branch4=64)
self.inception4d = Inception(in_channel=512, out_branch1=112, out_branch2_1=144, out_branch2_2=288,
out_branch3_1=32, out_branch3_2=64, out_branch4=64)
self.inception4e = Inception(in_channel=528, out_branch1=256, out_branch2_1=160, out_branch2_2=320,
out_branch3_1=32, out_branch3_2=128, out_branch4=128)
self.maxpool4 = nn.MaxPool2d(2, 2)
self.inception5a = Inception(in_channel=832, out_branch1=256, out_branch2_1=160, out_branch2_2=320,
out_branch3_1=32, out_branch3_2=128, out_branch4=128)
self.inception5b = Inception(in_channel=832, out_branch1=384, out_branch2_1=192, out_branch2_2=384,
out_branch3_1=48, out_branch3_2=128, out_branch4=128)
self.aux1 = InceptionAux(512, 128)
self.aux2 = InceptionAux(528, 128)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.dropout = nn.Dropout(0.2, inplace=False)
self.fc = nn.Linear(1024, 1000, bias=True)
def forward(self, x):
output = self.conv1(x)
output = self.maxpool1(output)
output = self.conv2(output)
output = self.conv3(output)
output = self.maxpool2(output)
output = self.inception3a(output)
output = self.inception3b(output)
output = self.maxpool3(output)
output = self.inception4a(output)
output_1 = self.aux1(output)
softmax = nn.Softmax(dim=1)
output_1 = softmax(output_1)
output = self.inception4b(output)
output = self.inception4c(output)
output = self.inception4d(output)
output_2 = self.aux2(output)
output_2 = softmax(output_2)
output = self.inception4e(output)
output = self.inception5a(output)
output = self.inception5b(output)
output = self.avgpool(output)
output = torch.flatten(output, 1)
output = self.fc(output)
output = softmax(output)
return [output, output_1, output_2]
if __name__ == '__main__':
image = np.random.randn(1, 3, 224, 224)
image = torch.tensor(image, dtype=torch.float32)
model = GoogLeNet()
outputs = model(image)
for out in outputs:
out = out.detach().numpy()
max = np.argmax(out, axis=1)
print(out[0, max])