# -*- coding: utf-8 -*-# Created by WIN10 on 2020/8/5# Copyright (c) 2020 WIN10. All rights reserved.import torch
from torch import nn
import torch.nn.functional as F
classBasicConv2d(nn.Module):def__init__(self,in_dim,out_dim,kernel_size,padding=0):super(BasicConv2d,self).__init__()
self.conv=nn.Conv2d(in_dim,out_dim,kernel_size,padding=padding)
self.bn=nn.BatchNorm2d(out_dim,eps=0.001)defforward(self, x):
x=self.conv(x)
x=self.bn(x)return F.relu(x,inplace=True)classInceptionv2(nn.Module):def__init__(self):super(Inceptionv2,self).__init__()
self.branch1=BasicConv2d(192,96,1,0)
self.branch2=nn.Sequential(
BasicConv2d(192,48,1,0),
BasicConv2d(48,64,3,1))
self.branch3=nn.Sequential(
BasicConv2d(192,64,1,0),
BasicConv2d(64,96,3,1),
BasicConv2d(96,96,3,1))
self.branch4=nn.Sequential(
nn.AvgPool2d(3,stride=1,padding=1,count_include_pad=False),
BasicConv2d(192,64,1,0))defforward(self,x):
x0=self.branch1(x)
x1=self.branch2(x)
x2=self.branch3(x)
x3=self.branch4(x)
out=torch.cat((x0,x1,x2,x3),1)return out
net_inceptionv2=Inceptionv2()input=torch.randn(1,192,32,32)print(net_inceptionv2(input).shape)
2.Resnet
# -*- coding: utf-8 -*-# Created by WIN10 on 2020/8/5# Copyright (c) 2020 WIN10. All rights reserved.import torch.nn as nn
import torch
classBottleneck(nn.Module):def__init__(self,in_dim,out_dim,stride=1):super(Bottleneck,self).__init__()
self.bottleneck=nn.Sequential(
nn.Conv2d(in_dim,in_dim,1,bias=False),
nn.BatchNorm2d(in_dim),
nn.ReLU(inplace=True),
nn.Conv2d(in_dim,in_dim,3,stride,1,bias=False),
nn.BatchNorm2d(in_dim),
nn.ReLU(inplace=True),
nn.Conv2d(in_dim,out_dim,1,bias=False),
nn.BatchNorm2d(out_dim))
self.relu=nn.ReLU(inplace=True)
self.downsample=nn.Sequential(
nn.Conv2d(in_dim,out_dim,1,1),
nn.BatchNorm2d(out_dim))defforward(self,x):
identity=x
out=self.bottleneck(x)
identity=self.downsample(x)
out+=identity
out=self.relu(out)return out
bottleneck_1_1=Bottleneck(64,256)input=torch.randn(1,64,56,56)print(bottleneck_1_1(input).shape)
3.DenseNet
# -*- coding: utf-8 -*-# Created by WIN10 on 2020/8/5# Copyright (c) 2020 WIN10. All rights reserved.import torch
from torch import nn
import torch.nn.functional as F
classBottleneck(nn.Module):def__init__(self,nChannels,growthRate):super(Bottleneck,self).__init__()
interChannels=4*growthRate
self.bn1=nn.BatchNorm2d(nChannels)
self.conv1=nn.Conv2d(nChannels,interChannels,kernel_size=1,bias=False)
self.bn2=nn.BatchNorm2d(interChannels)
self.conv2=nn.Conv2d(interChannels,growthRate,kernel_size=3,padding=1,bias=False)defforward(self,x):
out=self.conv1(F.relu(self.bn1(x)))
out=self.conv2(F.relu(self.bn2(out)))
out=torch.cat((x,out),1)return out
classDenseblock(nn.Module):def__init__(self,nChannels,growthRate,nDenseBlocks):super(Denseblock,self).__init__()
layers=[]for i inrange(int(nDenseBlocks)):
layers.append(Bottleneck(nChannels,growthRate))
nChannels+=growthRate
self.denseblock=nn.Sequential(*layers)defforward(self,x):return self.denseblock(x)# denseblock=Denseblock(64,32,6)# input=torch.randn(1,64,256,256)# print(denseblock(input).shape)
bottleneck=Bottleneck(64,32)input=torch.randn(1,64,256,256)print(bottleneck(input).shape)
4.FPN
# -*- coding: utf-8 -*-# Created by WIN10 on 2020/8/5# Copyright (c) 2020 WIN10. All rights reserved.import torch.nn as nn
import torch.nn.functional as F
import math
import torch
#resnet基本bottleneck类classBottleneck(nn.Module):
expansion =4def__init__(self,in_planes,planes,stride=1,downsample=None):super(Bottleneck,self).__init__()
self.bottleneck=nn.Sequential(
nn.Conv2d(in_planes,planes,1,bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(inplace=True),
nn.Conv2d(planes,planes,3,stride,1,bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(inplace=True),
nn.Conv2d(planes,self.expansion*planes,1,bias=False),
nn.BatchNorm2d(self.expansion*planes),)
self.relu=nn.ReLU(inplace=True)
self.downsample=downsample
defforward(self,x):
identity=x
out=self.bottleneck(x)if self.downsample isnotNone:
identity=self.downsample(x)
out+=identity
out=self.relu(out)return out
classFPN(nn.Module):def__init__(self,layers):super(FPN,self).__init__()
self.inplanes=64#c1
self.conv1=nn.Conv2d(3,64,7,2,3,bias=False)
self.bn1=nn.BatchNorm2d(64)
self.relu=nn.ReLU(inplace=True)
self.maxpool=nn.MaxPool2d(3,2,1)#c2,c3,c4,c5
self.layer1=self.__make_layer(64,layers[0])
self.layer2=self.__make_layer(128,layers[1],2)
self.layer3=self.__make_layer(256,layers[2],2)
self.layer4=self.__make_layer(512,layers[3],2)#p5
self.toplayer=nn.Conv2d(2048,256,1,1,0)#3x3卷积融合特征
self.smooth1=nn.Conv2d(256,256,3,1,1)#横向连接
self.latlayer1=nn.Conv2d(1024,256,1,1,0)
self.latlayer2=nn.Conv2d(512,256,1,1,0)
self.latlayer3=nn.Conv2d(256,256,1,1,0)def__make_layer(self,planes,blocks,stride=1):
downsample=nn.Sequential(
nn.Conv2d(self.inplanes,Bottleneck.expansion*planes,1,stride,bias=False),
nn.BatchNorm2d(Bottleneck.expansion*planes))
layers=[]
layers.append(Bottleneck(self.inplanes,planes,stride,downsample))
self.inplanes=planes*Bottleneck.expansion
for i inrange(1,blocks):
layers.append(Bottleneck(self.inplanes,planes))return nn.Sequential(*layers)def_upsample_add(self,x,y):
_,_,H,W=y.shape
return F.interpolate(x, size=(H, W), mode='bilinear', align_corners=True)+y
defforward(self,x):
c1=self.maxpool(self.relu(self.bn1(self.conv1(x))))
c2=self.layer1(c1)
c3=self.layer2(c2)
c4=self.layer3(c3)
c5=self.layer4(c4)
p5=self.toplayer(c5)
p4=self._upsample_add(p5,self.latlayer1(c4))
p3=self._upsample_add(p4,self.latlayer2(c3))
p2=self._upsample_add(p3,self.latlayer3(c2))
p4=self.smooth1(p4)
p3=self.smooth1(p3)
p2=self.smooth1(p2)return p2,p3,p4,p5
input=torch.randn(1,3,224,224)
net_fpn=FPN([3,4,6,3])print(net_fpn(input)[3].shape)
5.DetNet
# -*- coding: utf-8 -*-# Created by WIN10 on 2020/8/5# Copyright (c) 2020 WIN10. All rights reserved.from torch import nn
import torch
classDetBottleneck(nn.Module):def__init__(self, inplanes, planes, stride=1, extra=False):super(DetBottleneck, self).__init__()
self.bottleneck = nn.Sequential(
nn.Conv2d(inplanes, planes,1, bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(inplace=True),
nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=2, dilation=2, bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(inplace=True),
nn.Conv2d(planes, planes,1, bias=False),
nn.BatchNorm2d(planes))
self.relu = nn.ReLU(inplace=True)
self.extra = extra # false 为A true 为Bif self.extra:
self.extra_conv = nn.Sequential(
nn.Conv2d(inplanes, planes,1, bias=False),
nn.BatchNorm2d(planes))defforward(self, x):if self.extra:
identity = self.extra_conv(x)else:
identity = x
out = self.bottleneck(x)
out += identity
out = self.relu(out)return out
bottleneck_b = DetBottleneck(1024,256,1,True)print(bottleneck_b)
bottleneck_a1 = DetBottleneck(256,256)
bottleneck_a2 = DetBottleneck(256,256)input= torch.randn(1,1024,14,14)
output1 = bottleneck_b(input)
output2 = bottleneck_a1(output1)
output3 = bottleneck_a2(output2)print(output1.shape)