在""" Basic mamdafetrack model. """ import math import os from typing import List import…
- User
在""" Basic mamda_fetrack model. """ import math import os from typing import List
import torch from torch import nn from torch.nn.modules.transformer import _get_clones
from lib.models.layers.head import build_box_head from lib.utils.box_ops import box_xyxy_to_cxcywh from lib.models.mamba_fetrack.models_mamba import create_block from timm.models import create_model import torch.nn.functional as F from lib.models.mamba_fetrack.mamba_cross import CrossMamba from thop import profile
class Mamba_FEtrack(nn.Module): """ This is the base class for mamda_fetrack """
def __init__(self, visionmamba, cross_mamba, box_head, aux_loss=False, head_type="CORNER"): """ Initializes the model. Parameters: transformer: torch module of the transformer architecture. aux_loss: True if auxiliary decoding losses (loss at each decoder layer) are to be used. """ super().__init__() self.backbone = visionmamba self.cross_mamba= cross_mamba self.box_head = box_head self.aux_loss = aux_loss self.head_type = head_type if head_type == "CORNER" or head_type == "CENTER": self.feat_sz_s = int(box_head.feat_sz) self.feat_len_s = int(box_head.feat_sz ** 2) if self.aux_loss: self.box_head = _get_clones(self.box_head, 6) def forward(self, template: torch.Tensor, search: torch.Tensor, event_template: torch.Tensor, event_search: torch.Tensor, ce_template_mask=None, ce_keep_rate=None, return_last_attn=False, ): rgb_feature = self.backbone.forward_features( z=template, x=search, #[B, 320, 384] inference_params=None, if_random_cls_token_position=False, if_random_token_rank=False) event_feature = self.backbone.forward_features(z=event_template, x=event_search, #[B, 320, 384] inference_params=None, if_random_cls_token_position=False, if_random_token_rank=False) residual_event_f = 0 residual_rgb_f = 0 event_f = self.cross_mamba(event_feature,residual_event_f,rgb_feature) + event_feature rgb_f = self.cross_mamba(rgb_feature,residual_rgb_f,event_feature) + rgb_feature event_searh = event_f[:, -self.feat_len_s:] rgb_search = rgb_f[:, -self.feat_len_s:] x = torch.cat((event_searh,rgb_search),dim=-1) # Forward head feat_last = x if isinstance(x, list): feat_last = x[-1] out = self.forward_head(feat_last, None) out['backbone_feat'] = x return out def forward_head(self, cat_feature, gt_score_map=None): """ cat_feature: output embeddings of the backbone, it can be (HW1+HW2, B, C) or (HW2, B, C) """ search_feature = cat_feature opt = (search_feature.unsqueeze(-1)).permute((0, 3, 2, 1)).contiguous() # opt.shape = torch.Size([B, 1, 384, 256]) bs, Nq, C, HW = opt.size() opt_feat = opt.view(-1, C, self.feat_sz_s, self.feat_sz_s) # opt_feat.shape = torch.Size([B, 384, 16, 16]) if self.head_type == "CORNER": # run the corner head pred_box, score_map = self.box_head(opt_feat, True) outputs_coord = box_xyxy_to_cxcywh(pred_box) outputs_coord_new = outputs_coord.view(bs, Nq, 4) out = {'pred_boxes': outputs_coord_new, 'score_map': score_map, } return out elif self.head_type == "CENTER": # run the center head score_map_ctr, bbox, size_map, offset_map = self.box_head(opt_feat, gt_score_map) outputs_coord = bbox outputs_coord_new = outputs_coord.view(bs, Nq, 4) out = {'pred_boxes': outputs_coord_new, 'score_map': score_map_ctr, 'size_map': size_map, 'offset_map': offset_map} return out else: raise NotImplementedError - Assistant
在这段代码中,
self.cross_mamba函数被调用时,传入的residual_rgb_f是 0,其作用可以理解为占位符或调节参数。以下是具体的解释:残差连接 (Residual Connection)
残差连接是一种在深度神经网络,尤其是如 ResNet 这样的模型中广泛使用的技术。它的目的是通过直接将输入添加到输出上,帮助解决梯度消失和梯度爆炸的问题,使得训练深度网络变得更加容易和稳定。
作用分析
在调用
self.cross_mamba时:rgb_f = self.cross_mamba(rgb_feature, residual_rgb_f, event_feature) + rgb_feature参数的具体含义如下:
rgb_feature:这是从主干网络(即backbone)提取的 RGB 特征。residual_rgb_f:传入的残差特征,这里被设为 0。event_feature:这是从主干网络提取的事件特征。
self.cross_mamba函数的目的是进行 RGB 和事件特征的融合。函数的具体实现没有给出,但是通过传递residual_rgb_f可以做以下几种可能的操作:- 用作占位符:方便以后在架构上进行扩展或调整。当前设置为 0,但未来可以传入不同的特征进行更复杂的操作。
- 进行特征增强:虽当前为 0,但在跨模态融合中,可以通过这种参数为网络提供更多信息,比如上一次迭代的残差信息。
- 自适应调整:某些情况下可以通过融合不同层次的特征,进一步提高模型的表现。
加法操作
rgb_f的计算式中,self.cross_mamba(rgb_feature, residual_rgb_f, event_feature) + rgb_feature体现了标准的残差连接: - User
这是class CrossMamba(nn.Module): def init(self, dim): super(CrossMamba, self).init() self.cross_mamba = Mamba(dim,bimamba_type="v3") # 使用v3版本,会使用额外的输入extra_emb进行特殊处理 self.norm1 = LayerNorm(dim,'with_bias') self.norm2 = LayerNorm(dim,'with_bias') self.dwconv = nn.Conv2d(dim, dim, kernel_size=3, padding=1, groups=dim) def forward(self,m1,m1_resi,m2):
# print('m1.shape',m1.shape) # m1.shape torch.Size([4, 320, 384]) # print('m2.shape',m2.shape) # m2.shape torch.Size([4, 320, 384]) # print(m1_resi) # 0 m1_resi = m1+m1_resi
# print('m1_resi.shape',m1_resi.shape) # m2.shape torch.Size([4, 320, 384])global_f = self.cross_mamba(m1, extra_emb=m2) # print('global_f.shape',global_f.shape) # global_f.shape torch.Size([4, 320, 384]) return global_f的定义 - Assistant