File size: 5,284 Bytes
663494c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
## Not used in this project
## Provide an implementation for radar feature encoding

import torch
from mmcv.cnn import build_norm_layer
from mmcv.runner import auto_fp16
from mmcv.utils import Registry
from torch import nn
from torch.nn import functional as F

RADAR_ENCODERS = Registry("radar_encoder")


def build_radar_encoder(cfg):
    """Build backbone."""
    return RADAR_ENCODERS.build(cfg)


class RFELayer(nn.Module):
    """Radar Feature Encoder layer.

    Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        norm_cfg (dict): Config dict of normalization layers
    """

    def __init__(
        self,
        in_channels,
        out_channels,
        norm_cfg=dict(type="BN1d", eps=1e-3, momentum=0.01),
    ):
        super(RFELayer, self).__init__()
        self.fp16_enabled = False
        self.norm = build_norm_layer(norm_cfg, out_channels)[1]
        self.linear = nn.Linear(in_channels, out_channels, bias=False)

    @auto_fp16(apply_to=("inputs"), out_fp32=True)
    def forward(self, inputs):
        """Forward function.

        Args:
            inputs (torch.Tensor): Points features of shape (B, M, C).
                M is the number of points in
                C is the number of channels of point features.

        Returns:
            the same shape
        """

        x = self.linear(inputs)  # [B, M, C]
        # BMC -> BCM -> BMC
        x = self.norm(x.permute(0, 2, 1).contiguous()).permute(0, 2, 1).contiguous()
        out = F.relu(x)

        return out


@RADAR_ENCODERS.register_module()
class RadarPointEncoder(nn.Module):
    def __init__(
        self,
        in_channels,
        out_channels,
        norm_cfg=dict(type="BN1d", eps=1e-3, momentum=0.01),
    ):
        super(RadarPointEncoder, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        in_chn = in_channels

        layers = []
        for out_chn in out_channels:
            layer = RFELayer(in_chn, out_chn)
            layers.append(layer)
            in_chn = out_chn

        self.feat_layers = nn.Sequential(*layers)

    def forward(self, points):
        """
        points: [B, N, C]. N: as max
        masks: [B, N, 1]

        ret:
            out: [B, N, C+1], last channel as 0-1 mask
        """
        masks = points[:, :, [-1]]
        x = points[:, :, :-1]
        for feat_layer in self.feat_layers:
            x = feat_layer(x)

        out = x * masks

        out = torch.cat((x, masks), dim=-1)
        return out


@RADAR_ENCODERS.register_module()
class RadarPointEncoderXYAttn(nn.Module):
    def __init__(
        self,
        in_channels,
        out_channels,
        norm_cfg=dict(type="BN1d", eps=1e-3, momentum=0.01),
    ):
        super(RadarPointEncoderXYAttn, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        in_chn = in_channels

        layers = []
        for out_chn in out_channels:
            layer = RFELayer(in_chn, out_chn)
            layers.append(layer)
            in_chn = out_chn

        trans_encoder_layer = nn.TransformerEncoderLayer(
            in_chn, 8, dim_feedforward=2048, dropout=0.1, activation="relu"
        )

        self.transformer_encoder = torch.nn.TransformerEncoder(
            trans_encoder_layer,
            num_layers=2,
        )

        self.feat_layers = nn.Sequential(*layers)

    def forward(self, points):
        """
        points: [B, N, C]. N: as max
        masks: [B, N, 1]

        ret:
            out: [B, N, C+1], last channel as 0-1 mask
        """
        masks = points[:, :, [-1]]
        x = points[:, :, :-1]
        xy = points[:, :, :2]

        for feat_layer in self.feat_layers:
            x = feat_layer(x)

        x = x.permute(1, 0, 2)
        x = self.transformer_encoder(
            x, src_key_padding_mask=masks.squeeze(dim=-1).type(torch.bool)
        )

        x = x.permute(1, 0, 2)

        out = x * masks

        out = torch.cat((x, masks), dim=-1)

        out = torch.cat((xy, out), dim=-1)
        return out


@RADAR_ENCODERS.register_module()
class RadarPointEncoderXY(nn.Module):
    def __init__(
        self,
        in_channels,
        out_channels,
        norm_cfg=dict(type="BN1d", eps=1e-3, momentum=0.01),
    ):
        super(RadarPointEncoderXY, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        in_chn = in_channels

        layers = []
        for out_chn in out_channels:
            layer = RFELayer(in_chn, out_chn)
            layers.append(layer)
            in_chn = out_chn

        self.feat_layers = nn.Sequential(*layers)

    def forward(self, points):
        """
        points: [B, N, C]. N: as max
        masks: [B, N, 1]

        ret:
            out: [B, N, C+3],
                last channel as 0-1 mask
                first two channes as xy cords
        """
        masks = points[:, :, [-1]]
        x = points[:, :, :-1]
        xy = points[:, :, :2]

        for feat_layer in self.feat_layers:
            x = feat_layer(x)

        out = x * masks

        out = torch.cat((x, masks), dim=-1)

        out = torch.cat((xy, out), dim=-1)
        return out