Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Computer vision 如何将带有梯度的Torch张量列表转换为张量_Computer Vision_Pytorch_Heatmap_Tensor_Face Alignment - Fatal编程技术网

Computer vision 如何将带有梯度的Torch张量列表转换为张量

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)):

我有一个叫做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)):
        
        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)