pytorch yolov3 darknet53

本文中的總結爲本人原創,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) 的使用

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章