import torch
import torch.nn as nn
import torchvision
def ConvBNReLU(in_channels,out_channels,kernel_size,stride=1,padding=0):
return nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride,padding=padding),
nn.BatchNorm2d(out_channels),
nn.ReLU6(inplace=True),
)
def ConvBNReLUFactorization(in_channels,out_channels,kernel_sizes,paddings):
return nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_sizes, stride=1,padding=paddings),
nn.BatchNorm2d(out_channels),
nn.ReLU6(inplace=True)
)
class InceptionV3ModuleA(nn.Module):
def __init__(self, in_channels,out_channels1,out_channels2reduce, out_channels2, out_channels3reduce, out_channels3, out_channels4):
super(InceptionV3ModuleA, self).__init__()
self.branch1 = ConvBNReLU(in_channels=in_channels,out_channels=out_channels1,kernel_size=1)
self.branch2 = nn.Sequential(
ConvBNReLU(in_channels=in_channels, out_channels=out_channels2reduce, kernel_size=1),
ConvBNReLU(in_channels=out_channels2reduce, out_channels=out_channels2, kernel_size=5, padding=2),
)
self.branch3 = nn.Sequential(
ConvBNReLU(in_channels=in_channels,out_channels=out_channels3reduce,kernel_size=1),
ConvBNReLU(in_channels=out_channels3reduce, out_channels=out_channels3, kernel_size=3, padding=1),
ConvBNReLU(in_channels=out_channels3, out_channels=out_channels3, kernel_size=3, padding=1),
)
self.branch4 = nn.Sequential(
nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
ConvBNReLU(in_channels=in_channels, out_channels=out_channels4, kernel_size=1),
)
def forward(self, x):
out1 = self.branch1(x)
out2 = self.branch2(x)
out3 = self.branch3(x)
out4 = self.branch4(x)
out = torch.cat([out1, out2, out3, out4], dim=1)
return out
class InceptionV3ModuleB(nn.Module):
def __init__(self, in_channels,out_channels1,out_channels2reduce, out_channels2, out_channels3reduce, out_channels3, out_channels4):
super(InceptionV3ModuleB, self).__init__()
self.branch1 = ConvBNReLU(in_channels=in_channels,out_channels=out_channels1,kernel_size=1)
self.branch2 = nn.Sequential(
ConvBNReLU(in_channels=in_channels, out_channels=out_channels2reduce, kernel_size=1),
ConvBNReLUFactorization(in_channels=out_channels2reduce, out_channels=out_channels2reduce, kernel_sizes=[1,7],paddings=[0,3]),
ConvBNReLUFactorization(in_channels=out_channels2reduce, out_channels=out_channels2, kernel_sizes=[7,1],paddings=[3, 0]),
)
self.branch3 = nn.Sequential(
ConvBNReLU(in_channels=in_channels,out_channels=out_channels3reduce,kernel_size=1),
ConvBNReLUFactorization(in_channels=out_channels3reduce, out_channels=out_channels3reduce,kernel_sizes=[1, 7], paddings=[0, 3]),
ConvBNReLUFactorization(in_channels=out_channels3reduce, out_channels=out_channels3reduce,kernel_sizes=[7, 1], paddings=[3, 0]),
ConvBNReLUFactorization(in_channels=out_channels3reduce, out_channels=out_channels3reduce,kernel_sizes=[1, 7], paddings=[0, 3]),
ConvBNReLUFactorization(in_channels=out_channels3reduce, out_channels=out_channels3,kernel_sizes=[7, 1], paddings=[3, 0]),
)
self.branch4 = nn.Sequential(
nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
ConvBNReLU(in_channels=in_channels, out_channels=out_channels4, kernel_size=1),
)
def forward(self, x):
out1 = self.branch1(x)
out2 = self.branch2(x)
out3 = self.branch3(x)
out4 = self.branch4(x)
out = torch.cat([out1, out2, out3, out4], dim=1)
return out
class InceptionV3ModuleC(nn.Module):
def __init__(self, in_channels,out_channels1,out_channels2reduce, out_channels2, out_channels3reduce, out_channels3, out_channels4):
super(InceptionV3ModuleC, self).__init__()
self.branch1 = ConvBNReLU(in_channels=in_channels,out_channels=out_channels1,kernel_size=1)
self.branch2_conv1 = ConvBNReLU(in_channels=in_channels, out_channels=out_channels2reduce, kernel_size=1)
self.branch2_conv2a = ConvBNReLUFactorization(in_channels=out_channels2reduce, out_channels=out_channels2, kernel_sizes=[1,3],paddings=[0,1])
self.branch2_conv2b = ConvBNReLUFactorization(in_channels=out_channels2reduce, out_channels=out_channels2, kernel_sizes=[3,1],paddings=[1, 0])
self.branch3_conv1 = ConvBNReLU(in_channels=in_channels,out_channels=out_channels3reduce,kernel_size=1)
self.branch3_conv2 = ConvBNReLU(in_channels=out_channels3reduce, out_channels=out_channels3, kernel_size=3,stride=1,padding=1)
self.branch3_conv3a = ConvBNReLUFactorization(in_channels=out_channels3, out_channels=out_channels3, kernel_sizes=[3, 1],paddings=[1, 0])
self.branch3_conv3b = ConvBNReLUFactorization(in_channels=out_channels3, out_channels=out_channels3, kernel_sizes=[1, 3],paddings=[0, 1])
self.branch4 = nn.Sequential(
nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
ConvBNReLU(in_channels=in_channels, out_channels=out_channels4, kernel_size=1),
)
def forward(self, x):
out1 = self.branch1(x)
x2 = self.branch2_conv1(x)
out2 = torch.cat([self.branch2_conv2a(x2), self.branch2_conv2b(x2)],dim=1)
x3 = self.branch3_conv2(self.branch3_conv1(x))
out3 = torch.cat([self.branch3_conv3a(x3), self.branch3_conv3b(x3)], dim=1)
out4 = self.branch4(x)
out = torch.cat([out1, out2, out3, out4], dim=1)
return out
class InceptionV3ModuleD(nn.Module):
def __init__(self, in_channels,out_channels1reduce,out_channels1,out_channels2reduce, out_channels2):
super(InceptionV3ModuleD, self).__init__()
self.branch1 = nn.Sequential(
ConvBNReLU(in_channels=in_channels, out_channels=out_channels1reduce, kernel_size=1),
ConvBNReLU(in_channels=out_channels1reduce, out_channels=out_channels1, kernel_size=3,stride=2)
)
self.branch2 = nn.Sequential(
ConvBNReLU(in_channels=in_channels, out_channels=out_channels2reduce, kernel_size=1),
ConvBNReLU(in_channels=out_channels2reduce, out_channels=out_channels2, kernel_size=3, stride=1, padding=1),
ConvBNReLU(in_channels=out_channels2, out_channels=out_channels2, kernel_size=3, stride=2),
)
self.branch3 = nn.MaxPool2d(kernel_size=3,stride=2)
def forward(self, x):
out1 = self.branch1(x)
out2 = self.branch2(x)
out3 = self.branch3(x)
out = torch.cat([out1, out2, out3], dim=1)
return out
class InceptionV3ModuleE(nn.Module):
def __init__(self, in_channels, out_channels1reduce,out_channels1, out_channels2reduce, out_channels2):
super(InceptionV3ModuleE, self).__init__()
self.branch1 = nn.Sequential(
ConvBNReLU(in_channels=in_channels, out_channels=out_channels1reduce, kernel_size=1),
ConvBNReLU(in_channels=out_channels1reduce, out_channels=out_channels1, kernel_size=3, stride=2),
)
self.branch2 = nn.Sequential(
ConvBNReLU(in_channels=in_channels, out_channels=out_channels2reduce, kernel_size=1),
ConvBNReLUFactorization(in_channels=out_channels2reduce, out_channels=out_channels2reduce,kernel_sizes=[1, 7], paddings=[0, 3]),
ConvBNReLUFactorization(in_channels=out_channels2reduce, out_channels=out_channels2reduce,kernel_sizes=[7, 1], paddings=[3, 0]),
ConvBNReLU(in_channels=out_channels2reduce, out_channels=out_channels2, kernel_size=3, stride=2),
)
self.branch3 = nn.MaxPool2d(kernel_size=3, stride=2)
def forward(self, x):
out1 = self.b
Inceptionv1v2v3v4的pytorch实现
1星 需积分: 50 41 浏览量
2022-08-05
09:35:50
上传
评论
收藏 7KB ZIP 举报
CV小Rookie
- 粉丝: 36
- 资源: 1
评论1