yolov7代码梳理,读取json格式数据

Posted 东东就是我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yolov7代码梳理,读取json格式数据相关的知识,希望对你有一定的参考价值。

1.整体结构

说实话,看配置文件的方式真的不适合阅读代码,自己改写了一下
https://blog.csdn.net/u012863603/article/details/126118799 ,对着这个网络图

# coding=utf-8
import math

import torch
import torch.nn as nn

'''
Author:Don
date:2022/10/17 15:51
desc:
'''
from baseblock import *
from utils import *
class Model(nn.Module):
	def __init__(self,classes=2,anchors=None,ch=3):
		super(Model, self).__init__()
		# self.train=False
		na=3*(classes+5)

		self.layer0=Conv(ch,32,3,1)  # 0
		self.layer1=Conv(32,64,3,2)  # 1-p1/2
		self.layer2=Conv(64,64,3,1)
		self.layer3=Conv(64,128,3,2) #3-p2/4

		self.layer11=ELAN(128)
		self.layer16=MP_1(256)
		self.layer24=ELAN(256)
		self.layer29=MP_1(512)
		self.layer37=ELAN(512)
		self.layer42=MP_1(1024)
		self.layer50=ELAN_E(1024)

		self.layer51=SPPCSPC(1024,512)

		self.layer52=Conv(512,256,1,1)
		self.upsample=nn.Upsample(None,2,'nearest')
		self.concat = Concat(1)
		self.layer54=Conv(1024,256,1,1)


		self.layer63=ELAN_W(512)

		self.layer64=Conv(256,128,1,1)
		self.layer66=Conv(512,128,1,1)

		self.layer75=ELAN_W(256)
		self.layer80=MP_2(128)

		self.layer88=ELAN_W(512)
		self.layer93=MP_2(256)
		self.layer101=ELAN_W(1024)

		self.layer102=RepConv(128,256,3,1)
		self.layer103=RepConv(256,512,3,1)
		self.layer104=RepConv(512,1024,3,1)

		self.idetect=IDetect(classes,anchors=anchors,ch=[256,512,1024])
		s = 256  # 2x min stride  把256带入求得缩放的倍数
		self.idetect.stride=torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))])
		self.idetect.anchors /= self.idetect.stride.view(-1, 1, 1)
		# check_anchor_order(self.idetect)
		self.stride = self.idetect.stride
		self._initialize_biases()  # only run once

		# Init weights, biases
		initialize_weights(self)



	def forward(self,x):
		x3=self.layer3(self.layer2(self.layer1(self.layer0(x))))
		x24=self.layer24(self.layer16(self.layer11(x3)))
		x37=self.layer37(self.layer29(x24))
		x51=self.layer51(self.layer50(self.layer42(x37)))
		x63=self.layer63(self.concat([self.layer54(x37),self.upsample(self.layer52(x51))]))
		x75=self.layer75(self.concat([self.layer66(x24),self.upsample(self.layer64(x63))]))
		x88=self.layer88(self.concat([self.layer80(x75),x63]))
		x101=self.layer101(self.concat([self.layer93(x88),x51]))

		x102=self.layer102(x75)
		x103=self.layer103(x88)
		x104=self.layer104(x101)

		out=self.idetect([x102,x103,x104])


		return out

	def _initialize_biases(self):
		for mi,s in zip(self.idetect.m,self.idetect.stride):
			b=mi.bias.view(self.idetect.na,-1) # conv.bias(255) to (3,85)
			b.data[:,4]+= math.log(8/(640/s)**2)
			b.data[:, 5:] += math.log(0.6 / (self.idetect.nc - 0.99))   # cls
			mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

# device=torch.device('cuda:0')
# anchors=[[12,16, 19,36, 40,28],[36,75, 76,55, 72,146],[142,110, 192,243, 459,401]]
#
# yolov7 = Model(classes=1,anchors=anchors).to(device)
# img = torch.rand( 1, 3, 640, 640).to(device)
# y = yolov7(img)
# print(y[0].shape)

2.各个模块

还是对照网格图
链接: link

# coding=utf-8

import torch
import torch.nn as nn
'''
Author:Don
date:2022/10/17 16:24
desc:
'''

def autopad(k,p=None):
	if p is None:
		p=k//2 if isinstance(k,int) else [x//2 for x in k]
	return p

class Conv(nn.Module):
	# ch_in, ch_out, kernel, stride, padding, groups
	def __init__(self,c1,c2,k=1,s=1,p=None,g=1,act=True):
		super(Conv, self).__init__()
		self.conv=nn.Conv2d(c1,c2,k,s,autopad(k,p),groups=g,bias=False)
		self.bn=nn.BatchNorm2d(c2)
		self.act=nn.SiLU() if act is True else (act if isinstance(act,nn.Module) else nn.Identity())

	def forward(self,x):
		return  self.act(self.bn(self.conv(x)))

	def fuseforward(self,x):
		return self.act(self.conv(x))



class Concat(nn.Module):
	def __init__(self,dimension=1):
		super(Concat, self).__init__()
		self.d=dimension

	def forward(self,x):
		return torch.cat(x,self.d)


class MP(nn.Module):
	def __init__(self,k=2):
		super(MP, self).__init__()
		self.m=nn.MaxPool2d(kernel_size=k,stride=k)

	def forward(self,x):
		return self.m(x)


class MP_2(nn.Module):
	def __init__(self,c1):
		super(MP_2, self).__init__()
		self.conv1=Conv(c1,c1,1,1)
		self.conv2=Conv(c1,c1,3,2)
		self.m=MP()
		self.concat=Concat(1)

	def forward(self,x):
		y1=self.conv1(self.m(x))
		y2=self.conv2(self.conv1(x))
		return self.concat([y2,y1])


class MP_1(nn.Module):
	def __init__(self,c1):
		super(MP_1, self).__init__()
		c2=c1//2
		self.m=MP()
		self.conv1=Conv(c1,c2,1,1)
		self.conv2=Conv(c2,c2,3,2)
		self.concat=Concat(1)

	def forward(self,x):
		y1=self.conv1(self.m(x))
		y2=self.conv2(self.conv1(x))
		return self.concat([y1,y2])

class ELAN(nn.Module):
	def __init__(self, c1):
		super(ELAN, self).__init__()
		c2=c1//2
		c3=c1*2
		self.conv1 = Conv(c1, c2, 1, 1)
		self.conv2 = Conv(c2, c2, 3, 1)
		self.conv3 = Conv(c3, c3, 1, 1)
		self.concat=Concat(1)

	def forward(self,x):
		y1=self.conv1(x)
		y2=self.conv1(x)
		y3=self.conv2(self.conv2(y2))
		y4=self.conv2(self.conv2(y3))
		return self.conv3(self.concat([y4,y3,y2,y1]))


class ELAN_E(nn.Module):
	def __init__(self, c1):
		super(ELAN_E, self).__init__()
		c2=c1//4
		c3=c1
		self.conv1 = Conv(c1, c2, 1, 1)
		self.conv2 = Conv(c2, c2, 3, 1)
		self.conv3 = Conv(c3, c3, 1, 1)
		self.concat=Concat(1)

	def forward(self,x):
		y1=self.conv1(x)
		y2=self.conv1(x)
		y3=self.conv2(self.conv2(y2))
		y4=self.conv2(self.conv2(y3))
		return self.conv3(self.concat([y4,y3,y2,y1]))


class ELAN_W(nn.Module):
	def __init__(self, c1):
		super(ELAN_W, self).__init__()
		c2=c1//2
		c3=c2//2
		c4=2*c2+4*c3
		self.conv1 = Conv(c1, c2, 1, 1)
		self.conv2 = Conv(c2, c3, 3, 1)
		self.conv3 = Conv(c3, c3, 3, 1)
		self.conv4 = Conv(c4, c4//4, 1, 1)
		self.concat=Concat(1)

	def forward(self,x):
		y1=self.conv1(x)
		y2=self.conv1(x)
		y3=self.conv2(y2)
		y4=self.conv3(y3)
		y5=self.conv3(y4)
		y6=self.conv3(y5)
		return self.conv4(self.concat([y6,y5,y4,y3,y2,y1]))

class SPPCSPC(nn.Module):
	def __init__(self,c1,c2,e=0.5,k=(5,9,13)):
		super(SPPCSPC, self).__init__()
		c_=int(2*c2*e) #hidden channels
		self.cv1=Conv(c1,c_,1,1)
		self.cv2=Conv(c1,c_,1,1)
		self.cv3=Conv(c_,c_,3,1)
		self.cv4=Conv(c_,c_,1,1)
		self.m=nn.ModuleList([nn.MaxPool2d(kernel_size=x,stride=1,padding=x//2) for x in k])
		self.cv5=Conv(4*c_,c_,1,1)
		self.cv6=Conv(c_,c_,3,1)
		self.cv7=Conv(2*c_,c2,1,1)

	def forward(self,x):
		x1=self.cv4(self.cv3(self.cv1(x)))
		y1=self.cv6(self.cv5(torch.cat([x1]+[m(x1) for m in self.m],1)))
		y2=self.cv2(x)
		return self.cv7(torch.cat((y1,y2),dim=1))

class RepConv(nn.Module):
	def __init__(self,c1,c2,k=3,s=1,p=None,g=1,act=True,deploy=False):
		super(RepConv, self).__init__()
		self.deploy=deploy
		self.groups=g
		self.in_channels=c1
		self.out_channels=c2
		padding_11=autopad(k,p)-k//2

		self.act=nn.SiLU() if act is True else (act if isinstance(act,nn.Module) else nn.Identity())

		if deploy:
			self.rbr_reparam=nn.Conv2d(c1,c2,k,s,autopad(k,p),groups=g,bias=True)

		else:
			self.rbr_dense = nn.Sequential(
				nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False),
				nn.以上是关于yolov7代码梳理,读取json格式数据的主要内容,如果未能解决你的问题,请参考以下文章

YOLOv7 pytorch

Android studio解析含有GPS地理坐标的Json格式数据

如何循环 JSON 数据以获取所有对象的打印百分比值,使用 for each 或 other [重复]

物联网7.物联网通信 - 数据格式(XML ,JSON,MessagePack)

物联网7.物联网通信 - 数据格式(XML ,JSON,MessagePack)

怎么用jsp生成json格式数据,然后用js又怎样读取其数据?