def forward(self, x1, x2):
B, N, C = x1.shape
x = torch.cat((x1, x2), dim=-1) # Concatenate on the feature dimension
x = x.view(B * N, -1) # Reshape to (B*N, 2C) for the linear layer
spatial_weights = self.mlp(x).view(B, N, 2, 1).permute(2, 0, 1, 3) # 2 B N 1
return spatial_weights
def _init_weights(self, m):
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x1, x2):
print('it is alive!')
print('x1:', x1.shape)
print('x2:', x2.shape)
channel_weights = self.channel_weights(x1, x2)
print('channel_weights:', channel_weights.shape)
spatial_weights = self.spatial_weights(x1, x2)
print('spatial_weights:', spatial_weights.shape)
# Broadcasting dimensions for channel_weights to match x2 dimensions
channel_weights_c = channel_weights[1].unsqueeze(1).permute(1, 2, 0, 3) # [B, C, N, 1]
# Broadcasting dimensions for spatial_weights to match x2 dimensions
spatial_weights_c = spatial_weights[1].permute(1, 2, 0, 3) # [B, N, 1, C]
# Match dimensions across non-singleton axes for broadcasting
channel_weights_c = channel_weights_c.expand_as(x2) # Align dimensions to match x2
spatial_weights_c = spatial_weights_c.expand_as(x2)
out_x1 = x1 + self.lambda_c * (channel_weights_c * x2) + self.lambda_s * (spatial_weights_c * x2)
channel_weights_e = channel_weights[0].unsqueeze(1).permute(1, 2, 0, 3)
spatial_weights_e = spatial_weights[0].permute(1, 2, 0, 3)
# Ensure dimensions conform when broadcasting
channel_weights_e = channel_weights_e.expand_as(x1)
spatial_weights_e = spatial_weights_e.expand_as(x1)
out_x2 = x2 + self.lambda_c * (channel_weights_e * x1) + self.lambda_s * (spatial_weights_e * x1)
return out_x1, out_x2报错it is alive!
x1: torch.Size([20, 320, 384])
x2: torch.Size([20, 320, 384])
channel_weights: torch.Size([2, 20, 384, 1])
spatial_weights: torch.Size([2, 20, 320, 1])
Training crashed at epoch 1
Traceback for the error!
Traceback (most recent call last):
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/base_trainer.py", line 85, in train
self.train_epoch()
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/ltr_trainer.py", line 133, in train_epoch
self.cycle_dataset(loader)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/ltr_trainer.py", line 86, in cycle_dataset
loss, stats = self.actor(data)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/actors/mamba_fetrack.py", line 31, in call
out_dict = self.forward_pass(data)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/actors/mamba_fetrack.py", line 74, in forward_pass
out_dict = self.net(template=template_list,
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/models/mamba_fetrack/mamba_fetrack.py", line 64, in forward
rgb_feature, event_feature = self.frm(rgb_feature, event_feature)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/models/mamba_fetrack/FRM.py", line 101, in forward
spatial_weights_c = spatial_weights[1].permute(1, 2, 0, 3) # [B, N, 1, C]
RuntimeError: permute(sparse_coo): number of dimensions in the tensor input does not match the length of the desired ordering of dimensions i.e. input.dim() = 3 is not equal to len(dims) = 4
Restarting training from last epoch ...
Finished training!
Assistant
The error you're encountering stems from an attempt to permute a tensor in the FeatureRectifyModule that doesn't have the appropriate number of dimensions. Specifically, the shape of spatial_weights[1] is [20, 320, 1] as indicated by your code, but the permute call expects four dimensions due to the order [1, 2, 0, 3].
Fixing the Code
To rectify this issue, you need to ensure that the tensor being permuted has four dimensions. Here’s how you can adjust your code:
Expand the Tensor with One Additional Dimension: Before permuting spatial_weights[1], add an additional dimension to make it four-dimensional.
Here's how you can modify the permute operation:
python
# Fix: Add unsqueeze to add a new dimension before permuting
spatial_weights_c = spatial_weights[1].unsqueeze(-1).permute(1, 2, 0, 3) # [B, N, 1, C]
Explanation
unsqueeze(-1): This function call adds an additional dimension at the last position, transforming a [B, N, 1] shape into [B, N, 1, 1].
permute(1, 2, 0, 3): With the additional dimension, this now correctly addresses each dimension and results in the expected four-dimensional tensor.
After implementing these changes, the tensor should correctly permute without errors, and your training should proceed without this particular issue. If you run into other issues or exceptions during training, consider checking the exact tensor dimensions at each step to verify alignment with the expected dimensions.
User
中文回答,现在import torch
import torch.nn as nn
from timm.models.layers import trunc_normal_
import math
def forward(self, x1, x2):
B, N, C = x1.shape
x = torch.cat((x1, x2), dim=-1) # Concatenate on the feature dimension
x = x.view(B * N, -1) # Reshape to (B*N, 2C) for the linear layer
spatial_weights = self.mlp(x).view(B, N, 2, 1).permute(2, 0, 1, 3) # 2 B N 1
return spatial_weights
现在在 def forward(self, x1, x2):
print('it is alive!')
print('x1:',x1.shape)
print('x2:',x2.shape)
channel_weights = self.channel_weights(x1, x2)
out_x1 = x1 + self.lambda_c * channel_weights[1].unsqueeze(1) * x2
out_x2 = x2 + self.lambda_c * channel_weights[0].unsqueeze(1) * x1
return out_x1, out_x2中,报错:x1: torch.Size([20, 320, 384])
x2: torch.Size([20, 320, 384])
Training crashed at epoch 1
Traceback for the error!
Traceback (most recent call last):
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/base_trainer.py", line 85, in train
self.train_epoch()
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/ltr_trainer.py", line 133, in train_epoch
self.cycle_dataset(loader)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/ltr_trainer.py", line 86, in cycle_dataset
loss, stats = self.actor(data)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/actors/mamba_fetrack.py", line 31, in call
out_dict = self.forward_pass(data)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/actors/mamba_fetrack.py", line 74, in forward_pass
out_dict = self.net(template=template_list,
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/models/mamba_fetrack/mamba_fetrack.py", line 64, in forward
rgb_feature, event_feature = self.frm(rgb_feature, event_feature)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/models/mamba_fetrack/FRM.py", line 78, in forward
out_x1 = x1 + self.lambda_c * channel_weights[1].unsqueeze(1) * x2
RuntimeError: The size of tensor a (384) must match the size of tensor b (320) at non-singleton dimension 2
Restarting training from last epoch ...
Finished training!
def _init_weights(self, m):
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x1, x2):
print('it is alive!')
print('x1:', x1.shape)
print('x2:', x2.shape)
channel_weights = self.channel_weights(x1, x2)
print('channel_weights:', channel_weights.shape)
spatial_weights = self.spatial_weights(x1, x2)
print('spatial_weights:', spatial_weights.shape)
# Broadcasting dimensions for channel_weights to match x2 dimensions
channel_weights_c = channel_weights[1].unsqueeze(1).permute(1, 2, 0, 3) # [B, C, N, 1]
# Broadcasting dimensions for spatial_weights to match x2 dimensions
# Fix: Add unsqueeze to add a new dimension before permuting
spatial_weights_c = spatial_weights[1].unsqueeze(-1).permute(1, 2, 0, 3) # [B, N, 1, C]
# spatial_weights_c = spatial_weights[1].permute(1, 2, 0, 3) # [B, N, 1, C]
# Match dimensions across non-singleton axes for broadcasting
channel_weights_c = channel_weights_c.expand_as(x2) # Align dimensions to match x2
spatial_weights_c = spatial_weights_c.expand_as(x2)
out_x1 = x1 + self.lambda_c * (channel_weights_c * x2) + self.lambda_s * (spatial_weights_c * x2)
channel_weights_e = channel_weights[0].unsqueeze(1).permute(1, 2, 0, 3)
spatial_weights_e = spatial_weights[0].permute(1, 2, 0, 3)
# Ensure dimensions conform when broadcasting
channel_weights_e = channel_weights_e.expand_as(x1)
spatial_weights_e = spatial_weights_e.expand_as(x1)
out_x2 = x2 + self.lambda_c * (channel_weights_e * x1) + self.lambda_s * (spatial_weights_e * x1)
return out_x1, out_x2中报错x1: torch.Size([20, 320, 384])
x2: torch.Size([20, 320, 384])
channel_weights: torch.Size([2, 20, 384, 1])
spatial_weights: torch.Size([2, 20, 320, 1])
Training crashed at epoch 1
Traceback for the error!
Traceback (most recent call last):
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/base_trainer.py", line 85, in train
self.train_epoch()
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/ltr_trainer.py", line 133, in train_epoch
self.cycle_dataset(loader)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/trainers/ltr_trainer.py", line 86, in cycle_dataset
loss, stats = self.actor(data)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/actors/mamba_fetrack.py", line 31, in call
out_dict = self.forward_pass(data)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/train/actors/mamba_fetrack.py", line 74, in forward_pass
out_dict = self.net(template=template_list,
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/models/mamba_fetrack/mamba_fetrack.py", line 64, in forward
rgb_feature, event_feature = self.frm(rgb_feature, event_feature)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/heyuting/anaconda3/envs/mamba/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/data2/local_userdata/heyuting/Mamba/MyFETrack/lib/train/../../lib/models/mamba_fetrack/FRM.py", line 81, in forward
channel_weights_c = channel_weights_c.expand_as(x2) # Align dimensions to match x2
RuntimeError: expand(torch.cuda.FloatTensor{[1, 384, 20, 1]}, size=[20, 320, 384]): the number of sizes provided (3) must be greater or equal to the number of dimensions in the tensor (4)
Restarting training from last epoch ...
Finished training!,主要问题是 384和320对不上。