pytorch加载darknet权重文件

  • 以yolov3-tiny为例,加载函数是ting.pyload_darknet
  • base.py
import torch
import torch.nn as nn

class BaseConv(nn.Module):
	def __init__(self, in_channel, out_channel, k_size, stride, pad, bn, act):
		super().__init__()
		self.conv = nn.Conv2d(in_channel, out_channel, k_size, stride, pad, bias=not bn)
		if bn == True:
			self.bn = nn.BatchNorm2d(out_channel)
		else:
			self.bn = nn.Identity()
		if act == 'leaky': self.act = nn.LeakyReLU(0.1)
		elif act == 'linear': self.act = nn.Identity()
	def forward(self, x):
		x = self.conv(x)
		x = self.bn(x)
		return self.act(x)
		
if __name__ == '__main__':
	data=torch.zeros(2,3, 320,320)
	bc=BaseConv(3, 16, 3, 1, 1, False, 'leaky')
	x = bc(data)
	print(x.shape)
  • tiny.py(网络结构)
import torch
import torch.nn as nn
from base import BaseConv
import numpy as np

class Backbone(nn.Module):
	def __init__(self):
		super().__init__()
		self.conv0 = BaseConv(3, 16, 3, 1, 1, True, 'leaky')
		self.maxpool1 = nn.MaxPool2d(2)
		self.conv2 = BaseConv(16, 32, 3, 1, 1, True, 'leaky')
		self.maxpool3 = nn.MaxPool2d(2)
		self.conv4 = BaseConv(32, 64, 3, 1, 1, True, 'leaky') 
		self.maxpool5 = nn.MaxPool2d(2)
		self.conv6 = BaseConv(64, 128, 3, 1, 1, True, 'leaky')
		self.maxpool7 = nn.MaxPool2d(2)
		self.conv8 = BaseConv(128, 256, 3, 1, 1, True, 'leaky')
		self.maxpool9 = nn.MaxPool2d(2)
		self.conv10 = BaseConv(256, 512, 3, 1, 1, True, 'leaky')
		self.pad = nn.ZeroPad2d((0,1,0,1))
		self.maxpool11 = nn.MaxPool2d(2, 1)
		self.conv12 = BaseConv(512, 1024, 3, 1, 1, True, 'leaky')
		self.conv13 = BaseConv(1024, 256, 1, 1, 0, True, 'leaky')
		self.conv14 = BaseConv(256, 512, 3, 1, 1, True, 'leaky')
	def forward(self, x):
		x0=self.conv0(x)
		x1=self.maxpool1(x0)  # 1    16*208*208
		x2=self.conv2(x1)
		x3=self.maxpool3(x2)  # 3    32*104*104
		x4=self.conv4(x3)
		x5=self.maxpool5(x4)  # 5    64*52*52
		x6=self.conv6(x5
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值