本文中的总结为本人原创,darkent53 部分的代码来自博主bubbliiiing ,指路b站和github,很优秀的up.在此表示感谢!
https://blog.csdn.net/weixin_44791964/article/details/105310627
1.建立模型需要的基本 函数接口
import torch
import torch.nn as nn
import math
from collections import OrderDict
#进行一次卷积操作,通常都会紧跟bn层和激活函数层
#1.卷积
nn.Conv2d(in_channels,out_channels,kernel_size=3,stride=2,padding=0,bias=False)(x)
#2.batchnormalization
nn.BatchNorm2d(out_channels)(x)
#3.激活函数
nn.LeakyReLU(0.1)(x) # alpha x 中的alpha
2. 在搭建模型时,通常封装类的形式来构建模型,以darknet53 为例
# 先封装小的结构
class BasicBlock(nn.Module): # Module 的 M 一定要大写!!!
def __init__(self, ...):
super(BasicBlock,self).__init__()
# 参数
self.
# 定义一些神经网络层,后续用于forward函数中
# 比如
self.conv1=nn.Conv2d(in_channels,out_channels,kernel_size=3,
stride=2,padding=1,bias=Fasle)
self.bn1=nn.BatchNorm2d(out_channels)
self.relu1=nn.LeakyReLU(0.1)
# 构建前向传播网络
def forward(self,x):
x=self.conv1(x)
x=self.bn1(x)
x=self.relu1(x)
return x
#在使用时
basicblock=BasicBlock(...)
out=basicblock(x) # 继承了nn.Module
#封装大的网络,并对小网络进行使用
class DarkNet(nn.Module):
def __init__(self,...):
super(DarkNet,self)__init__()
self....=...
self.layer=self._make_layers(,)
#权重和偏置初始化
# 搭建网络中用到的具有规律的结构,使forward函数更加简洁
def _make_layers(self,...,...): # 没有x参数,因为 里面用到的其他神经网络常用函数允许在后面用到时再添加
def forward(self,x):
x=self._make_layers(x)
return x
3.关于nn.Module 类函数的使用方法!!!!!!!!!!!
# 1.直接输入x使用
nn.Conv2d(in_channels,out_channels,kernel_size=3,stride=2,padding=1,bias=False)(x)
# 2.现在__init__函数中进行定义,再在forward函数中传入输入
def __init__(self,...):
self.conv1=nn.Conv2d(in_channels,out_channels,kernel_size=3,stride=2,
padding=1,bias=False)
def forward(self,x):
x=self.conv1(x)
return x
4. 关于权重和偏置初始化
5.关于 在模型封装类中可以不传入x
6.关于模型传入预训练参数 模型参数的保存和读取还需要进一步研究!!!!!!!!!!!!!!!!!!!!!!!!
def darknet53(pretrained):
model=DarkNet(...)
if isinstance(pretrained, str):
model.load_state_dict(torch.load(pretrained))
else:
raise Exception("darknet request a pretrained path. got [{}]".format(pretrained))
return model
# 这个部位还得看!!!
7.完整的darkent 53 代码
# 感觉关于inplanes 和 planes 写的不太好。
import torch
import torch.nn as nn
import math
from collections import OrderedDict
# 基本的darknet块
class BasicBlock(nn.Module):
def __init__(self, inplanes, planes):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes[0], kernel_size=1,
stride=1, padding=0, bias=False)
self.bn1 = nn.BatchNorm2d(planes[0])
self.relu1 = nn.LeakyReLU(0.1)
self.conv2 = nn.Conv2d(planes[0], planes[1], kernel_size=3,
stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes[1])
self.relu2 = nn.LeakyReLU(0.1)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu2(out)
out += residual
return out
class DarkNet(nn.Module):
def __init__(self, layers):
super(DarkNet, self).__init__()
self.inplanes = 32
self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(self.inplanes)
self.relu1 = nn.LeakyReLU(0.1)
self.layer1 = self._make_layer([32, 64], layers[0])
self.layer2 = self._make_layer([64, 128], layers[1])
self.layer3 = self._make_layer([128, 256], layers[2])
self.layer4 = self._make_layer([256, 512], layers[3])
self.layer5 = self._make_layer([512, 1024], layers[4])
self.layers_out_filters = [64, 128, 256, 512, 1024]
# 进行权值初始化
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, planes, blocks):
layers = []
# 下采样,步长为2,卷积核大小为3
layers.append(("ds_conv", nn.Conv2d(self.inplanes, planes[1], kernel_size=3,
stride=2, padding=1, bias=False)))
layers.append(("ds_bn", nn.BatchNorm2d(planes[1])))
layers.append(("ds_relu", nn.LeakyReLU(0.1)))
# 加入darknet模块
self.inplanes = planes[1]
for i in range(0, blocks):
layers.append(("residual_{}".format(i), BasicBlock(self.inplanes, planes)))
return nn.Sequential(OrderedDict(layers))
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.layer1(x)
x = self.layer2(x)
out3 = self.layer3(x)
out4 = self.layer4(out3)
out5 = self.layer5(out4)
return out3, out4, out5
def darknet53(pretrained, **kwargs):
model = DarkNet([1, 2, 8, 8, 4])
if pretrained:
if isinstance(pretrained, str):
model.load_state_dict(torch.load(pretrained))
else:
raise Exception("darknet request a pretrained path. got [{}]".format(pretrained))
return model
8. darknet53 结构 3个数分别代表ksp
整体有一个 3,1,1的卷积
然后有5个block (5个block里的resblock数 12884)
每个block,首先 3,2,1卷积,加num个resnet_block
每个resblock有两个卷积 1,1,1(通道数减半)+3,1,1(通道数恢复)
1+(1+2+8+8+4)*2+5+1
9.补充 OrderDict 的使用
‘’‘
1 python 中常用的字典都是无序的,但是collections中的OrderDict是有序的,内容相同,顺序不同也被认为是不一样的两个 排序字典。
’‘’
d1 = collections.OrderedDict()
d1['a'] = 'A'
d1['b'] = 'B'
d1['c'] = 'C'
d1['1'] = '1'
d1['2'] = '2'
for k,v in d1.items():
print k,v
#
a A
b B
c C
1 1
2 2
#例子
from collections import OrderedDict
a=[]
for i in range(3):
print("residual_{}".format(i))
a.append(("residual_{}".format(i),i)) # 两个元素用()括起来作为一个元素append,append只能添加一个元素,
print(a)
m=OrderedDict(a) #有序字典
print(m)
#对字典中的内容遍历, k,v m.items()
for k,v in m.items():
print(k,v)
#结果:
residual_0
residual_1
residual_2
[('residual_0', 0), ('residual_1', 1), ('residual_2', 2)]
OrderedDict([('residual_0', 0), ('residual_1', 1), ('residual_2', 2)])
residual_0 0
residual_1 1
residual_2 2
# 其中,还学习了 “residual_{}”.format(i) 的使用