当前：class ChannelWeights(nn.Module):
    def __init__(self, dim, reduction=1):
        super(ChannelWeights, self).__init__()
        self.dim = dim
        self.avg_pool = nn.AdaptiveAvgPool1d(1)
        self.max_pool = nn.AdaptiveMaxPool1d(1)
        self.mlp = nn.Sequential(
                    nn.Linear(self.dim * 4, self.dim * 4 // reduction),
                    nn.ReLU(inplace=True),
                    nn.Linear(self.dim * 4 // reduction, self.dim * 2),
                    nn.Sigmoid())

    def forward(self, x1, x2):
        B, N, C = x1.shape
        x = torch.cat((x1, x2), dim=-1)  # Concatenate on the feature dimension
        avg = self.avg_pool(x.permute(0, 2, 1)).view(B, self.dim * 2)
        max = self.max_pool(x.permute(0, 2, 1)).view(B, self.dim * 2)
        y = torch.cat((avg, max), dim=1)  # B 4C
        y = self.mlp(y).view(B, 2, self.dim, 1)
        return y.permute(1, 0, 2, 3)  # 2 B C 1


class SpatialWeights(nn.Module):
    def __init__(self, dim, reduction=1):
        super(SpatialWeights, self).__init__()
        self.dim = dim
        self.mlp = nn.Sequential(
                    nn.Linear(self.dim * 2, self.dim // reduction),
                    nn.ReLU(inplace=True),
                    nn.Linear(self.dim // reduction, 2),
                    nn.Sigmoid())

    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


class FeatureRectifyModule(nn.Module):
    def __init__(self, dim, reduction=1, lambda_c=.5, lambda_s=.5):
        super(FeatureRectifyModule, self).__init__()
        self.lambda_c = lambda_c
        self.lambda_s = lambda_s
        self.channel_weights = ChannelWeights(dim=dim, reduction=reduction)
        self.spatial_weights = SpatialWeights(dim=dim, reduction=reduction)
    
    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!