Attention with both self and cross Implements the decoder in DETR transformer. Args: return_intermediate (bool): Whether to return intermediate outputs. coder_norm_cfg (dict): Config of last normalization layer. Default: `LN`.
| 27 | |
| 28 | """ |
| 29 | Attention with both self and cross |
| 30 | Implements the decoder in DETR transformer. |
| 31 | Args: |
| 32 | return_intermediate (bool): Whether to return intermediate outputs. |
| 33 | coder_norm_cfg (dict): Config of last normalization layer. Default: |
| 34 | `LN`. |
| 35 | """ |
| 36 | |
| 37 | def __init__(self, *args, pc_range=None, num_points_in_pillar=4, return_intermediate=False, dataset_type='nuscenes', |
| 38 | **kwargs): |
| 39 | |
| 40 | super(BEVFormerEncoder, self).__init__(*args, **kwargs) |
| 41 | self.return_intermediate = return_intermediate |
| 42 | |
| 43 | self.num_points_in_pillar = num_points_in_pillar |
| 44 | self.pc_range = pc_range |
| 45 | self.fp16_enabled = False |
| 46 | |
| 47 | @staticmethod |
| 48 | def get_reference_points(H, W, Z=8, num_points_in_pillar=4, dim='3d', bs=1, device='cuda', dtype=torch.float): |
| 49 | """Get the reference points used in SCA and TSA. |
| 50 | Args: |
| 51 | H, W: spatial shape of bev. |
| 52 | Z: hight of pillar. |
| 53 | D: sample D points uniformly from each pillar. |
| 54 | device (obj:`device`): The device where |
| 55 | reference_points should be. |
| 56 | Returns: |
| 57 | Tensor: reference points used in decoder, has \ |
| 58 | shape (bs, num_keys, num_levels, 2). |
| 59 | """ |
| 60 | |
| 61 | # reference points in 3D space, used in spatial cross-attention (SCA) |
| 62 | if dim == '3d': |
| 63 | zs = torch.linspace(0.5, Z - 0.5, num_points_in_pillar, dtype=dtype, |
| 64 | device=device).view(-1, 1, 1).expand(num_points_in_pillar, H, W) / Z |
| 65 | xs = torch.linspace(0.5, W - 0.5, W, dtype=dtype, |
| 66 | device=device).view(1, 1, W).expand(num_points_in_pillar, H, W) / W |
| 67 | ys = torch.linspace(0.5, H - 0.5, H, dtype=dtype, |
| 68 | device=device).view(1, H, 1).expand(num_points_in_pillar, H, W) / H |
| 69 | ref_3d = torch.stack((xs, ys, zs), -1) # (num_points_in_pillar, H, W, 3) |
| 70 | ref_3d = ref_3d.permute(0, 3, 1, 2).flatten(2).permute(0, 2, 1) # (num_points_in_pillar, H*W, 3) |
| 71 | ref_3d = ref_3d[None].repeat(bs, 1, 1, 1) # (bs, num_points_in_pillar, H*W, 3) |
| 72 | return ref_3d |
| 73 | |
| 74 | # reference points on 2D bev plane, used in temporal self-attention (TSA). |
| 75 | elif dim == '2d': |
| 76 | ref_y, ref_x = torch.meshgrid( |
| 77 | torch.linspace( |
| 78 | 0.5, H - 0.5, H, dtype=dtype, device=device), |
| 79 | torch.linspace( |
| 80 | 0.5, W - 0.5, W, dtype=dtype, device=device) |
| 81 | ) |
| 82 | ref_y = ref_y.reshape(-1)[None] / H |
| 83 | ref_x = ref_x.reshape(-1)[None] / W |
| 84 | ref_2d = torch.stack((ref_x, ref_y), -1) |
| 85 | ref_2d = ref_2d.repeat(bs, 1, 1).unsqueeze(2) |
| 86 | return ref_2d |
nothing calls this directly
no outgoing calls
no test coverage detected