| 78 | |
| 79 | |
| 80 | class VNLinearLeakyReLU(nn.Module): |
| 81 | def __init__(self, in_channels, out_channels, dim=5, share_nonlinearity=False, negative_slope=0.2): |
| 82 | super(VNLinearLeakyReLU, self).__init__() |
| 83 | self.dim = dim |
| 84 | self.negative_slope = negative_slope |
| 85 | |
| 86 | self.map_to_feat = nn.Linear(in_channels, out_channels, bias=False) |
| 87 | self.batchnorm = VNBatchNorm(out_channels, dim=dim) |
| 88 | |
| 89 | if share_nonlinearity == True: |
| 90 | self.map_to_dir = nn.Linear(in_channels, 1, bias=False) |
| 91 | else: |
| 92 | self.map_to_dir = nn.Linear(in_channels, out_channels, bias=False) |
| 93 | |
| 94 | def forward(self, x): |
| 95 | ''' |
| 96 | x: point features of shape [B, N_feat, 3, N_samples, ...] |
| 97 | ''' |
| 98 | # Linear |
| 99 | p = self.map_to_feat(x.transpose(1, -1)).transpose(1, -1) |
| 100 | # BatchNorm |
| 101 | p = self.batchnorm(p) |
| 102 | # LeakyReLU |
| 103 | d = self.map_to_dir(x.transpose(1, -1)).transpose(1, -1) |
| 104 | dotprod = (p * d).sum(2, keepdims=True) |
| 105 | mask = (dotprod >= 0).float() |
| 106 | d_norm_sq = (d * d).sum(2, keepdims=True) |
| 107 | x_out = self.negative_slope * p + (1 - self.negative_slope) * ( |
| 108 | mask * p + (1 - mask) * (p - (dotprod / (d_norm_sq + EPS)) * d)) |
| 109 | return x_out |
| 110 | |
| 111 | |
| 112 | class VNLinearAndLeakyReLU(nn.Module): |