import torch
from torch import nn
import torch.nn.functional as F
class Redisual_block(nn.Module):
def __init__(self,in_channels,mid_channels,out_channels,use1_by_1=False,stride=1):
super().__init__()
self.conv1=nn.Conv2d(in_channels,mid_channels,kernel_size=1)
self.conv2=nn.Conv2d(mid_channels,mid_channels,kernel_size=3,stride=stride,padding=1)
self.conv3=nn.Conv2d(mid_channels,out_channels,kernel_size=1)
if use1_by_1==True:
self.conv4=nn.Conv2d(in_channels,out_channels,kernel_size=1,stride=stride)
else :
self.conv4=None
self.bn1=nn.BatchNorm2d(mid_channels)
self.bn2=nn.BatchNorm2d(mid_channels)
self.bn3=nn.BatchNorm2d(out_channels)
def forward(self,X):
Y=F.relu(self.bn1(self.conv1(X)))
Y=F.relu(self.bn2(self.conv2(Y)))
Y=F.relu(self.bn3(self.conv3(Y)))
if self.conv4:
X=self.conv4(X)
return F.relu(Y+X)
def resnet_block(in_channels,mid_channels,out_channels,num_redisuals,stride):
blk=[]
for i in range(num_redisuals):
if i==0:
blk.append(Redisual_block(in_channels,mid_channels,out_channels,use1_by_1=True,stride=stride))
else :
blk.append(Redisual_block(out_channels,mid_channels,out_channels))
return nn.Sequential(*blk)
class FlattenLayer(nn.Module):
def __init__(self):
super().__init__()
def forward(self,x):
return x.view(x.shape[0],-1)
class GlobalAvgPool(nn.Module):
def __init__(self):
super().__init__()
def forward(self,x):
return F.avg_pool2d(x,kernel_size=x.size()[2:])
def resnet50(classify=10):
net=nn.Sequential()
net.add_module("conv1",nn.Conv2d(3,64,kernel_size=7,stride=2,padding=1))
net.add_module("maxpool1",nn.MaxPool2d(kernel_size=3,stride=2,padding=1))
net.add_module("resnet_block1",resnet_block(64,64,256,3,1))
net.add_module("resnet_block2",resnet_block(256,128,512,4,2))#stride=2,通道数加倍,宽度减半
net.add_module("resnet_block3",resnet_block(512,256,1024,6,2))
net.add_module("resnet_block4",resnet_block(1024,512,2048,3,2))
net.add_module("GlobalAvgPool",GlobalAvgPool())
net.add_module("FlattenLayer",FlattenLayer())
net.add_module("fc",nn.Linear(2048,classify))
return net
打印结构
if __name__=="__main__":
resnet=resnet50(10)
X=torch.rand(1,3,244,244)
for name,blk in resnet.named_children():
X=blk(X)
print(name,"output shape",X.shape)