Computer vision 如何将带有梯度的Torch张量列表转换为张量
我有一个叫做pts的变量,它的形状是[batch,ch,h,w]。这是一张热图,我想把它转换成第二坐标。目标是,pts_o=热图到pts(pts),其中pts_o将是[batch,ch,2]。到目前为止,我已经编写了这个函数Computer vision 如何将带有梯度的Torch张量列表转换为张量,computer-vision,pytorch,heatmap,tensor,face-alignment,Computer Vision,Pytorch,Heatmap,Tensor,Face Alignment,我有一个叫做pts的变量,它的形状是[batch,ch,h,w]。这是一张热图,我想把它转换成第二坐标。目标是,pts_o=热图到pts(pts),其中pts_o将是[batch,ch,2]。到目前为止,我已经编写了这个函数 def heatmap_to_pts(self, pts): <- pts [batch, 68, 128, 128] pt_num = [] for i in range(len(pts)):
def heatmap_to_pts(self, pts): <- pts [batch, 68, 128, 128]
pt_num = []
for i in range(len(pts)):
pt = pts[i]
if type(pt) == torch.Tensor:
d = torch.tensor(128) * get the
m = pt.view(68, -1).argmax(1) * indices
indices = torch.cat(((m / d).view(-1, 1), (m % d).view(-1, 1)), dim=1) * from heatmaps
pt_num.append(indices.type(torch.DoubleTensor) ) <- store the indices in a list
b = torch.Tensor(68, 2) * trying to convert
c = torch.cat(pt_num, out=b) *error* * a list of tensors with grad
c = c.reshape(68,2) * to a tensor like [batch, 68, 2]
return c
def heatmap\u to_pts(self,pts):错误显示
cat():不带out=。。。参数不支持自动微分,但其中一个参数需要grad
这意味着,torch.cat()
等函数的输出作为out=
kwarg不能用作autograd引擎(执行自动区分)的输入
原因是(Python列表中的)张量对于requires\u grad
属性具有不同的值,即,有些张量具有requires\u grad=True
,而有些张量具有requires\u grad=False
在代码中,以下行(逻辑上)很麻烦:
c = torch.cat(pt_num, out=b)
torch.cat()
的返回值,无论是否使用out=
kwarg,都是沿所述维度的张量串联
因此,张量c
已经是pt_num
中单个张量的串联版本。使用out=b
冗余。因此,您可以简单地去掉out=b
,一切都应该很好
c = torch.cat(pt_num)