资源简介
图片分类,图像识别,目标检测的resnet50,python实现。
代码片段和文件信息
import torch.nn as nn
import math
import torch.utils.model_zoo as model_zoo
class Bottleneck(nn.Module): #预定义网络架构并定义前向传播
expansion = 4 #expansion是指残差块输出维度是输入维度的多少倍。在ResNet类中的_make_layer函数中会用到
def __init__(selfin_planesplanesstride=1downsample=None): #初始化并继承nn.Module中的一些属性。in_planes指输入的通道数,planes指输出的通道数,步长默认为1,下采样函数默认为空(即默认不需要下采样)
super(Bottleneckself).__init__() #定义nn.Module的子类Bottleneck。并在下面给出新的属性
self.conv1 = nn.Conv2d(in_planesplaneskernel_size=1bias=False)
self.bn1 = nn.BatchNorm2d(planes) #归一化处理
self.conv2 = nn.Conv2d(in_planesplaneskernel_size=3stride=stridepadding=1bias=False) #stride默认为1,但可更改
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(in_planesplanes*4kernel_size=1bias=False) #第三次卷积输出纬度为上一层的四倍
self.bn3 = nn.BatchNorm(planes*4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride=stride
def forwar(selfx): #前向传播
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None: # 用来处理H(x) = F(x) + x中 F(x)和x维度(高、宽、通道数)不匹配的问题
self.downsample = downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(selfblocklayersnum_classes=1000): #block即为Bottleneck模型,layers可控制传入的Bottleneck
selfinplanes = 64 #初始输入通道数为64
super(ResNetself).__init__() #可见ResNet也是nn.Module的子类
self.conv1 = nn.Conv2d(364kernel_size = 7stride = 2padding = 3bias = False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace = True)
self.maxpool = nn.MaxPool2d(kernel_size = 3stride = 2padding = 1)
self.layer1 = self._make_layer(block64layers[0]) # 四层残差块64为这一层输入的通道数layer[0]表示有几个残差块
self.layer2 = self._make_layer(block128layers[1]stride = 2)
self.layer3 = self._make_layer(block256layers[2]stride = 2)
self.layer4 = self._make_layer(block512layers[3]
评论
共有 条评论