u版本yolov3中一些pytorch语法学习
torch.meshgrid meshgrid翻译:网格)
x1 ,y1 = torch.meshgrid(x,y) (
参数是两个,第一个参数我们假设是x,第二个参数假设就是y
输出的是两个tensor,size就是x.size * y.size(行数是x的个数,列数是y的个数)
具体输出看下面
注意:两个参数的数据类型要相同,要么都是float,要么都是int,否则会报错。
其中第一个输出张量填充第一个输入张量中的元素,各行元素相同;第二个输出张量填充第二个输入张量中的元素各列元素相同。
import torch
a1,a2 = torch.arange(4), torch.arange(4)
yv, xv = torch.meshgrid([a1,a2])
a1:
tensor([0, 1, 2, 3])
a2:
tensor([0, 1, 2, 3])
yv:
tensor([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]])
xv:
tensor([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
为了方便看出变化,我把a2变成负数
import torch
a1,a2 = torch.arange(4), (torch.arange(2)+1)*-1
yv, xv = torch.meshgrid([a1,a2])
a1:
tensor([0, 1, 2, 3])
a2:
tensor([-1, -2])
yv:
tensor([[0, 0],
[1, 1],
[2, 2],
[3, 3]])
xv:
tensor([[-1, -2],
[-1, -2],
[-1, -2],
[-1, -2]])
这里更加清楚的阐释了:
输出的是两个tensor,size就是x.size * y.size(行数是x的个数,列数是y的个数)
第一个输出张量填充第一个输入张量中的元素,各行元素相同;第二个输出张量填充第二个输入张量中的元素各列元素相同。
torch.stack() (stack翻译:堆栈)
outputs = torch.stack(inputs, dim=0) → Tensor
inputs : 待连接的张量序列。
注:python的序列数据只有list和tuple。
dim : 新的维度, 必须在0到len(outputs)之间。
注:len(outputs)是生成数据的维度大小,也就是outputs的维度值。
沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。
浅显说法:把多个2维的张量凑成一个3维的张量;多个3维的凑成一个4维的张量…以此类推,也就是在增加新的维度进行堆叠。
torch.stack()对tensors沿指定维度拼接,但返回的Tensor维数会变
import torch
a1 = torch.arange(6).view(2,3)
a2 = (-1)*(torch.arange(6)+1).view(2,3)
b0 = torch.stack((a1,a2),dim=0)
b1 = torch.stack((a1,a2),dim=1)
b2 = torch.stack((a1,a2),dim=2)
a1:
tensor([[0, 1, 2],
[3, 4, 5]])
a2:
tensor([[-1, -2, -3],
[-4, -5, -6]])
b0: torch.Size([2, 2, 3])
tensor([[[ 0, 1, 2],
[ 3, 4, 5]],
[[-1, -2, -3],
[-4, -5, -6]]])
b1: torch.Size([2, 2, 3])
tensor([[[ 0, 1, 2],
[-1, -2, -3]],
[[ 3, 4, 5],
[-4, -5, -6]]])
b2: torch.Size([2, 3, 2])
tensor([[[ 0, -1],
[ 1, -2],
[ 2, -3]],
[[ 3, -4],
[ 4, -5],
[ 5, -6]]])
b3:
b3 = torch.stack((a1,a2),dim=3)
IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)
例子2:
import torch
a1 = torch.arange(12).view(4,3)
a2 = (-1)*(torch.arange(12)+1).view(4,3)
b0 = torch.stack((a1,a2),dim=0)
print(b0.shape)
b1 = torch.stack((a1,a2),dim=1)
print(b1.shape)
b2 = torch.stack((a1,a2),dim=2)
print(b2.shape)
b3 = torch.stack((a1,a2),dim=3)
a1:
tensor([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
a2:
tensor([[ -1, -2, -3],
[ -4, -5, -6],
[ -7, -8, -9],
[-10, -11, -12]])
b0: torch.Size([2, 4, 3])
tensor([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]],
[[ -1, -2, -3],
[ -4, -5, -6],
[ -7, -8, -9],
[-10, -11, -12]]])
b1: torch.Size([4, 2, 3])
tensor([[[ 0, 1, 2],
[ -1, -2, -3]],
[[ 3, 4, 5],
[ -4, -5, -6]],
[[ 6, 7, 8],
[ -7, -8, -9]],
[[ 9, 10, 11],
[-10, -11, -12]]])
b2: torch.Size([4, 3, 2])
tensor([[[ 0, -1],
[ 1, -2],
[ 2, -3]],
[[ 3, -4],
[ 4, -5],
[ 5, -6]],
[[ 6, -7],
[ 7, -8],
[ 8, -9]],
[[ 9, -10],
[ 10, -11],
[ 11, -12]]])
所以看上面,dim=0的时候还好理解,直接拼接,再增加一个维度,但是dim=1的时候,[4,3] [4,3]就变成了[4,2,3],把两个维度各取一行拼接而来。
yolov3 里面结合meshgrid,stack生成网格点例子
yv, xv = torch.meshgrid([torch.arange(self.ny, device=device), torch.arange(self.nx, device=device)])
tmp =torch.stack((xv, yv), 2)
self.grid = torch.stack((xv, yv), 2).view((1, 1, self.ny, self.nx, 2)).float()
yv:
tensor([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
[ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11],
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
[13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13],
[14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14],
[15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15]], device='cuda:0')
xv:
tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]], device='cuda:0')
tmp: [16,16,2]
tensor([[[ 0, 0],
[ 1, 0],
[ 2, 0],
[ 3, 0],
[ 4, 0],
[ 5, 0],
[ 6, 0],
[ 7,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 2, 8],
[ 3, 8],
[ 4, 8],
[ 5, 8],
[ 6, 8],
[ 7, 8],
[ 8, 8],
[ 9, 8],
[10, 8],
[11, 8],
[12, 8],
[13, 8],
[14, 8],
[15, 8]],
[[ 0, 9],
[ 1, 9],
[ 2, 9],
[ 3, 9],
[ 4, 9],
[ 5, 9],
[ 6, 9],
[ 7, 9],
[ 8, 9],
[ 9, 9],
[10, 9],
[11, 9],
[12, 9],
[13, 9],
[14, 9],
[15, 9]],
[[ 0, 10],
[ 1, 10],
[ 2, 10],
[ 3, 10],
[ 4, 10],
[ 5, 10],
[ 6, 10],
[ 7, 10],
[ 8, 10],
[ 9, 10],
[10, 10],
[11, 10],
[12, 10],
[13, 10],
[14, 10],
[15, 10]],
[[ 0, 11],
[ 1, 11],
[ 2, 11],
[ 3, 11],
[ 4, 11],
[ 5, 11],
[ 6, 11],
[ 7, 11],
[ 8, 11],
[ 9, 11],
[10, 11],
[11, 11],
[12, 11],
[13, 11],
[14, 11],
[15, 11]],
[[ 0, 12],
[ 1, 12],
[ 2, 12],
[ 3, 12],
[ 4, 12],
[ 5, 12],
[ 6, 12],
[ 7, 12],
[ 8, 12],
[ 9, 12],
[10, 12],
[11, 12],
[12, 12],
[13, 12],
[14, 12],
[15, 12]],
[[ 0, 13],
[ 1, 13],
[ 2, 13],
[ 3, 13],
[ 4, 13],
[ 5, 13],
[ 6, 13],
[ 7, 13],
[ 8, 13],
[ 9, 13],
[10, 13],
[11, 13],
[12, 13],
[13, 13],
[14, 13],
[15, 13]],
[[ 0, 14],
[ 1, 14],
[ 2, 14],
[ 3, 14],
[ 4, 14],
[ 5, 14],
[ 6, 14],
[ 7, 14],
[ 8, 14],
[ 9, 14],
[10, 14],
[11, 14],
[12, 14],
[13, 14],
[14, 14],
[15, 14]],
[[ 0, 15],
[ 1, 15],
[ 2, 15],
[ 3, 15],
[ 4, 15],
[ 5, 15],
[ 6, 15],
[ 7, 15],
[ 8, 15],
[ 9, 15],
[10, 15],
[11, 15],
[12, 15],
[13, 15],
[14, 15],
[15, 15]]], device='cuda:0')
grid: [1,1,16,16,2]
tensor([[[[[ 0., 0.],
[ 1., 0.],
[ 2., 0.],
[ 3., 0.],
[ 4., 0.],
[ 5., 0.],
[ 6., 0.],
[ 7., 0.],
[ 8., 0.],
[ 9., 0.],
[10., 0.],
[11., 0.],
[12., 0.],
[13., 0.],
[14., 0.],
[15., 0.]],
[[ 0., 1.],
[ 1., 1.],
[ 2., 1.],
[ 3., 1.],
[ 4., 1.],
[ 5., 1.],
[ 6., 1.],
[ 7., 1.],
[ 8., 1.],
[ 9., 1.],
[10., 1.],
[11., 1.],
[12., 1.],
[13., 1.],
[14., 1.],
[15., 1.]],
[[ 0., 2.],
[ 1., 2.],
[ 2., 2.],
[ 3., 2.],
[ 4., 2.],
[ 5., 2.],
[ 6., 2.],
[ 7., 2.],
[ 8., 2.],
[ 9., 2.],
[10., 2.],
[11., 2.],
[12., 2.],
[13., 2.],
[14., 2.],
[15., 2.]],
[[ 0., 3.],
[ 1., 3.],
[ 2., 3.],
[ 3., 3.],
[ 4., 3.],
[ 5., 3.],
[ 6., 3.],
[ 7., 3.],
[ 8., 3.],
[ 9., 3.],
[10., 3.],
[11., 3.],
[12., 3.],
[13., 3.],
[14., 3.],
[15., 3.]],
[[ 0., 4.],
[ 1., 4.],
[ 2., 4.],
[ 3., 4.],
[ 4., 4.],
[ 5., 4.],
[ 6., 4.],
[ 7., 4.],
[ 8., 4.],
[ 9., 4.],
[10., 4.],
[11., 4.],
[12., 4.],
[13., 4.],
[14., 4.],
[15., 4.]],
[[ 0., 5.],
[ 1., 5.],
[ 2., 5.],
[ 3., 5.],
[ 4., 5.],
[ 5., 5.],
[ 6., 5.],
[ 7., 5.],
[ 8., 5.],
[ 9., 5.],
[10., 5.],
[11., 5.],
[12., 5.],
[13., 5.],
[14., 5.],
[15., 5.]],
[[ 0., 6.],
[ 1., 6.],
[ 2., 6.],
[ 3., 6.],
[ 4., 6.],
[ 5., 6.],
[ 6., 6.],
[ 7., 6.],
[ 8., 6.],
[ 9., 6.],
[10., 6.],
[11., 6.],
[12., 6.],
[13., 6.],
[14., 6.],
[15., 6.]],
[[ 0., 7.],
[ 1., 7.],
[ 2., 7.],
[ 3., 7.],
[ 4., 7.],
[ 5., 7.],
[ 6., 7.],
[ 7., 7.],
[ 8., 7.],
[ 9., 7.],
[10., 7.],
[11., 7.],
[12., 7.],
[13., 7.],
[14., 7.],
[15., 7.]],
[[ 0., 8.],
[ 1., 8.],
[ 2., 8.],
[ 3., 8.],
[ 4., 8.],
[ 5., 8.],
[ 6., 8.],
[ 7., 8.],
[ 8., 8.],
[ 9., 8.],
[10., 8.],
[11., 8.],
[12., 8.],
[13., 8.],
[14., 8.],
[15., 8.]],
[[ 0., 9.],
[ 1., 9.],
[ 2., 9.],
[ 3., 9.],
[ 4., 9.],
[ 5., 9.],
[ 6., 9.],
[ 7., 9.],
[ 8., 9.],
[ 9., 9.],
[10., 9.],
[11., 9.],
[12., 9.],
[13., 9.],
[14., 9.],
[15., 9.]],
[[ 0., 10.],
[ 1., 10.],
[ 2., 10.],
[ 3., 10.],
[ 4., 10.],
[ 5., 10.],
[ 6., 10.],
[ 7., 10.],
[ 8., 10.],self.grid = torch.stack((xv, yv), 2).view((1, 1, self.ny, self.nx, 2)).float()
[ 9., 10.],
[10., 10.],
[11., 10.],
[12., 10.],
[13., 10.],
[14., 10.],
[15., 10.]],
[[ 0., 11.],
[ 1., 11.],
[ 2., 11.],
[ 3., 11.],
[ 4., 11.],
[ 5., 11.],
[ 6., 11.],
[ 7., 11.],
[ 8., 11.],
[ 9., 11.],
[10., 11.],
[11., 11.],
[12., 11.],
[13., 11.],
[14., 11.],
[15., 11.]],
[[ 0., 12.],
[ 1., 12.],
[ 2., 12.],
[ 3., 12.],
[ 4., 12.],
[ 5., 12.],
[ 6., 12.],
[ 7., 12.],
[ 8., 12.],
[ 9., 12.],
[10., 12.],
[11., 12.],
[12., 12.],
[13., 12.],
[14., 12.],
[15., 12.]],
[[ 0., 13.],
[ 1., 13.],
[ 2., 13.],
[ 3., 13.],
[ 4., 13.],
[ 5., 13.],
[ 6., 13.],
[ 7., 13.],
[ 8., 13.],
[ 9., 13.],
[10., 13.],
[11., 13.],
[12., 13.],
[13., 13.],
[14., 13.],
[15., 13.]],
[[ 0., 14.],
[ 1., 14.],
[ 2., 14.],
[ 3., 14.],
[ 4., 14.],
[ 5., 14.],
[ 6., 14.],
[ 7., 14.],
[ 8., 14.],
[ 9., 14.],
[10., 14.],
[11., 14.],
[12., 14.],
[13., 14.],
[14., 14.],
[15., 14.]],
[[ 0., 15.],
[ 1., 15.],
[ 2., 15.],
[ 3., 15.],
[ 4., 15.],
[ 5., 15.],
[ 6., 15.],
[ 7., 15.],
[ 8., 15.],
[ 9., 15.],
[10., 15.],
[11., 15.],
[12., 15.],
[13., 15.],
[14., 15.],
[15., 15.]]]]], device='cuda:0')
self.grid = torch.stack((xv, yv), 2).view((1, 1, self.ny, self.nx, 2)).float()
yolov3里面之所以要整成[1,1,16,16,2]这种形状,原因在于需要与之前一一对应,之前定义的shape是[bs,na,x,y,no] ##na:number of anchor no:number of output
.all
import torch
a = torch.tensor([[True,False],
[True,True],
[False,True]])
b = torch.tensor([[False,False],
[True,True],
[False,True]])
c = a & b
d0 = c.all(0)
d1 = c.all(1)
c:
tensor([[False, False],
[ True, True],
[False, True]])
d0:
tensor([False, False])
d1:
tensor([False, True, False])
对应的yolov3里面:
# x[12096,25] tmp_a:[12096]
tmp_a = x[:, 4] > conf_thres
# Apply constraints
x = x[x[:, 4] > conf_thres] # confidence ###################################################
##x[13,25]
tmp_b = x[:, 2:4] > min_wh ## [13,2]
tmp_c = x[:, 2:4] < max_wh ## [13,2]
tmp_d = tmp_b & tmp_c ## [13,2]
tmp_e = tmp_d.all(1) ## [13]
x = x[((x[:, 2:4] > min_wh) & (x[:, 2:4] < max_wh)).all(1)] # width-height ###################################################
##x:[13,25] 由于tmp_e全是True
opencv画图并显示label,这是我见过画图显示最好的代码
# Plotting functions ---------------------------------------------------------------------------------------------------
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
# Plots one bounding box on image img
tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
if label:
tf = max(tl - 1, 1) # font thickness
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
不同label自动用不同的颜色,文字大小与图像分辨率自适应调整,文字底色填充颜色。