edge_map = edge_map.unsqueeze(1).expand(-1,1,1 -1, -1) RuntimeError: expand(torch.FloatTensor{[1, 1, 1, 254, 254]}, size=[-1, 1, 0, -1]): the number of sizes provided (4) must be greater or equal to the number of dimensions in the tensor (5)
时间: 2023-09-09 19:06:30 浏览: 93
这个错误是由于 `expand()` 中提供的 size 参数有误,导致无法将 tensor 进行扩展。具体来说,你提供了一个 shape 为 4 的 size 参数,但是你的 tensor 的维度为 5。所以,你需要在 `expand()` 中提供正确的 size 参数。
可能的解决方案是检查你的 tensor 的 shape 和你提供的 size 参数,确保它们匹配。你可以使用 `tensor.shape` 来获取 tensor 的 shape,然后手动计算出正确的 size 参数。或者你可以尝试使用其他函数来扩展 tensor,例如 `repeat()` 或者 `tile()`。这些函数的使用方式与 `expand()` 不同,但可以实现相似的功能。
相关问题
q_mask = (q_mask == 0).unsqueeze(1).unsqueeze(1).expand_as(q2v) v_mask = (v_mask == 0).unsqueeze(1).unsqueeze(1).expand_as(v2q)
这段代码是将条件判断的结果应用到两个变量 q_mask 和 v_mask 上。
首先,`(q_mask == 0)` 是一个条件判断表达式,判断 q_mask 是否等于0。结果是一个布尔型的张量。
然后,`.unsqueeze(1).unsqueeze(1)` 是将维度扩展操作,将布尔型的张量维度扩展为与 q2v 相同的维度。这两个 `.unsqueeze(1)` 操作将在第1个维度上增加一个维度。
最后,`.expand_as(q2v)` 是将张量扩展操作,将张量扩展为与 q2v 相同的形状。
这样,q_mask 的形状被扩展为与 q2v 相同,并且根据条件判断的结果进行了相应的填充。
v_mask 的处理方式与 q_mask 类似,只是将条件判断的结果应用到 v_mask 上,并根据 v2q 的形状进行了扩展。
修改下列模块代码,使其能够对三维模型的直线特征进行提取:class FaceKernelCorrelation(nn.Module): def __init__(self, num_kernel=64, sigma=0.2): super(FaceKernelCorrelation, self).__init__() self.num_kernel = num_kernel self.sigma = sigma self.weight_alpha = Parameter(torch.rand(1, num_kernel, 4) * np.pi) self.weight_beta = Parameter(torch.rand(1, num_kernel, 4) * 2 * np.pi) self.bn = nn.BatchNorm1d(num_kernel) self.relu = nn.ReLU() def forward(self, normals, neighbor_index): b, _, n = normals.size() center = normals.unsqueeze(2).expand(-1, -1, self.num_kernel, -1).unsqueeze(4) neighbor = torch.gather(normals.unsqueeze(3).expand(-1, -1, -1, 3), 2, neighbor_index.unsqueeze(1).expand(-1, 3, -1, -1)) neighbor = neighbor.unsqueeze(2).expand(-1, -1, self.num_kernel, -1, -1) fea = torch.cat([center, neighbor], 4) fea = fea.unsqueeze(5).expand(-1, -1, -1, -1, -1, 4) weight = torch.cat([torch.sin(self.weight_alpha) * torch.cos(self.weight_beta), torch.sin(self.weight_alpha) * torch.sin(self.weight_beta), torch.cos(self.weight_alpha)], 0) weight = weight.unsqueeze(0).expand(b, -1, -1, -1) weight = weight.unsqueeze(3).expand(-1, -1, -1, n, -1) weight = weight.unsqueeze(4).expand(-1, -1, -1, -1, 4, -1) dist = torch.sum((fea - weight)**2, 1) fea = torch.sum(torch.sum(np.e**(dist / (-2 * self.sigma**2)), 4), 3) / 16 return self.relu(self.bn(fea))
修改后的代码如下:
```python
class FaceKernelCorrelation(nn.Module):
def __init__(self, num_kernel=64, sigma=0.2):
super(FaceKernelCorrelation, self).__init__()
self.num_kernel = num_kernel
self.sigma = sigma
self.weight_alpha = Parameter(torch.rand(1, num_kernel, 6) * np.pi)
self.weight_beta = Parameter(torch.rand(1, num_kernel, 6) * 2 * np.pi)
self.bn = nn.BatchNorm1d(num_kernel)
self.relu = nn.ReLU()
def forward(self, vertices, neighbor_index):
b, _, n = vertices.size()
center = vertices.unsqueeze(2).expand(-1, -1, self.num_kernel, -1).unsqueeze(4)
neighbor = torch.gather(vertices.unsqueeze(3).expand(-1, -1, -1, 3),
2, neighbor_index.unsqueeze(1).expand(-1, 3, -1, -1))
neighbor = neighbor.unsqueeze(2).expand(-1, -1, self.num_kernel, -1, -1)
# 计算直线特征
line = neighbor - center
length = torch.sqrt(torch.sum(line**2, dim=-1, keepdim=True))
direction = line / (length + 1e-8)
fea = torch.cat([center, direction, length], dim=4)
fea = fea.unsqueeze(5).expand(-1, -1, -1, -1, -1, 6)
weight = torch.cat([torch.sin(self.weight_alpha) * torch.cos(self.weight_beta),
torch.sin(self.weight_alpha) * torch.sin(self.weight_beta),
torch.cos(self.weight_alpha)], 0)
weight = weight.unsqueeze(0).expand(b, -1, -1, -1)
weight = weight.unsqueeze(3).expand(-1, -1, -1, n, -1)
weight = weight.unsqueeze(4).expand(-1, -1, -1, -1, 6, -1)
dist = torch.sum((fea - weight)**2, 1)
fea = torch.sum(torch.sum(np.e**(dist / (-2 * self.sigma**2)), 4), 3) / 16
return self.relu(self.bn(fea))
```
对比原有的代码,主要修改的地方如下:
1. 修改了 weight_alpha 和 weight_beta 的形状,将其从 4 改为 6,以便存储直线特征;
2. 在 forward 函数中,首先计算出所有点的邻居点,然后根据邻居点和中心点计算出直线特征(即方向向量和长度),并将其拼接在一起;
3. 将拼接后的直线特征与权重相减后,进行距离计算和高斯加权求和。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)