Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,799 Bytes
475d332 |
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
from .transformer_utils import BaseTemperalPointModel
from copy import deepcopy
import torch
import einops
import torch.nn as nn
import torch.nn.functional as F
from torch import nn
from einops import rearrange
import pointops
from pointcept.models.utils import offset2batch, batch2offset
class PointBatchNorm(nn.Module):
"""
Batch Normalization for Point Clouds data in shape of [B*N, C], [B*N, L, C]
"""
def __init__(self, embed_channels):
super().__init__()
self.norm = nn.BatchNorm1d(embed_channels)
def forward(self, input: torch.Tensor) -> torch.Tensor:
if input.dim() == 3:
return (
self.norm(input.transpose(1, 2).contiguous())
.transpose(1, 2)
.contiguous()
)
elif input.dim() == 2:
return self.norm(input)
else:
raise NotImplementedError
#https://github.com/Pointcept/Pointcept/blob/main/pointcept/models/point_transformer_v2/point_transformer_v2m2_base.py
class GroupedVectorAttention(nn.Module):
def __init__(
self,
embed_channels,
groups,
attn_drop_rate=0.0,
qkv_bias=True,
pe_multiplier=False,
pe_bias=True,
):
super(GroupedVectorAttention, self).__init__()
self.embed_channels = embed_channels
self.groups = groups
assert embed_channels % groups == 0
self.attn_drop_rate = attn_drop_rate
self.qkv_bias = qkv_bias
self.pe_multiplier = pe_multiplier
self.pe_bias = pe_bias
self.linear_q = nn.Sequential(
nn.Linear(embed_channels, embed_channels, bias=qkv_bias),
PointBatchNorm(embed_channels),
nn.ReLU(inplace=True),
)
self.linear_k = nn.Sequential(
nn.Linear(embed_channels, embed_channels, bias=qkv_bias),
PointBatchNorm(embed_channels),
nn.ReLU(inplace=True),
)
self.linear_v = nn.Linear(embed_channels, embed_channels, bias=qkv_bias)
if self.pe_multiplier:
self.linear_p_multiplier = nn.Sequential(
nn.Linear(3, embed_channels),
PointBatchNorm(embed_channels),
nn.ReLU(inplace=True),
nn.Linear(embed_channels, embed_channels),
)
if self.pe_bias:
self.linear_p_bias = nn.Sequential(
nn.Linear(3, embed_channels),
PointBatchNorm(embed_channels),
nn.ReLU(inplace=True),
nn.Linear(embed_channels, embed_channels),
)
self.weight_encoding = nn.Sequential(
nn.Linear(embed_channels, groups),
PointBatchNorm(groups),
nn.ReLU(inplace=True),
nn.Linear(groups, groups),
)
self.softmax = nn.Softmax(dim=1)
self.attn_drop = nn.Dropout(attn_drop_rate)
def forward(self, feat, coord, reference_index):
query, key, value = (
self.linear_q(feat),
self.linear_k(feat),
self.linear_v(feat),
)
key = pointops.grouping(reference_index, key, coord, with_xyz=True)
value = pointops.grouping(reference_index, value, coord, with_xyz=False)
pos, key = key[:, :, 0:3], key[:, :, 3:]
relation_qk = key - query.unsqueeze(1)
if self.pe_multiplier:
pem = self.linear_p_multiplier(pos)
relation_qk = relation_qk * pem
if self.pe_bias:
peb = self.linear_p_bias(pos)
relation_qk = relation_qk + peb
value = value + peb
weight = self.weight_encoding(relation_qk)
weight = self.attn_drop(self.softmax(weight))
mask = torch.sign(reference_index + 1)
weight = torch.einsum("n s g, n s -> n s g", weight, mask)
value = einops.rearrange(value, "n ns (g i) -> n ns g i", g=self.groups)
feat = torch.einsum("n s g i, n s g -> n g i", value, weight)
feat = einops.rearrange(feat, "n g i -> n (g i)")
return feat
class BlockSequence(nn.Module):
def __init__(
self,
depth,
embed_channels,
groups,
neighbours=16,
qkv_bias=True,
pe_multiplier=False,
pe_bias=True,
attn_drop_rate=0.0,
drop_path_rate=0.0,
enable_checkpoint=False,
):
super(BlockSequence, self).__init__()
if isinstance(drop_path_rate, list):
drop_path_rates = drop_path_rate
assert len(drop_path_rates) == depth
elif isinstance(drop_path_rate, float):
drop_path_rates = [deepcopy(drop_path_rate) for _ in range(depth)]
else:
drop_path_rates = [0.0 for _ in range(depth)]
self.neighbours = neighbours
self.blocks = nn.ModuleList()
for i in range(depth):
block = Block(
embed_channels=embed_channels,
groups=groups,
qkv_bias=qkv_bias,
pe_multiplier=pe_multiplier,
pe_bias=pe_bias,
attn_drop_rate=attn_drop_rate,
drop_path_rate=drop_path_rates[i],
enable_checkpoint=enable_checkpoint,
)
self.blocks.append(block)
def forward(self, points):
coord, feat, offset = points
# reference index query of neighbourhood attention
# for windows attention, modify reference index query method
reference_index, _ = pointops.knn_query(self.neighbours, coord, offset)
for block in self.blocks:
points = block(points, reference_index)
return points
class GVAPatchEmbed(nn.Module):
def __init__(
self,
depth,
in_channels,
embed_channels,
groups,
neighbours=16,
qkv_bias=True,
pe_multiplier=False,
pe_bias=True,
attn_drop_rate=0.0,
drop_path_rate=0.0,
enable_checkpoint=False,
):
super(GVAPatchEmbed, self).__init__()
self.in_channels = in_channels
self.embed_channels = embed_channels
self.proj = nn.Sequential(
nn.Linear(in_channels, embed_channels, bias=False),
PointBatchNorm(embed_channels),
nn.ReLU(inplace=True),
)
self.blocks = BlockSequence(
depth=depth,
embed_channels=embed_channels,
groups=groups,
neighbours=neighbours,
qkv_bias=qkv_bias,
pe_multiplier=pe_multiplier,
pe_bias=pe_bias,
attn_drop_rate=attn_drop_rate,
drop_path_rate=drop_path_rate,
enable_checkpoint=enable_checkpoint,
)
def forward(self, points):
coord, feat, offset = points
feat = self.proj(feat)
return self.blocks([coord, feat, offset])
class Block(nn.Module):
def __init__(
self,
embed_channels,
groups,
qkv_bias=True,
pe_multiplier=False,
pe_bias=True,
attn_drop_rate=0.0,
drop_path_rate=0.0,
enable_checkpoint=False,
):
super(Block, self).__init__()
self.attn = GroupedVectorAttention(
embed_channels=embed_channels,
groups=groups,
qkv_bias=qkv_bias,
attn_drop_rate=attn_drop_rate,
pe_multiplier=pe_multiplier,
pe_bias=pe_bias,
)
self.fc1 = nn.Linear(embed_channels, embed_channels, bias=False)
self.fc3 = nn.Linear(embed_channels, embed_channels, bias=False)
self.norm1 = PointBatchNorm(embed_channels)
self.norm2 = PointBatchNorm(embed_channels)
self.norm3 = PointBatchNorm(embed_channels)
self.act = nn.ReLU(inplace=True)
self.enable_checkpoint = enable_checkpoint
self.drop_path = (
DropPath(drop_path_rate) if drop_path_rate > 0.0 else nn.Identity()
)
def forward(self, points, reference_index):
coord, feat, offset = points
identity = feat
feat = self.act(self.norm1(self.fc1(feat)))
feat = (
self.attn(feat, coord, reference_index)
if not self.enable_checkpoint
else checkpoint(self.attn, feat, coord, reference_index)
)
feat = self.act(self.norm2(feat))
feat = self.norm3(self.fc3(feat))
feat = identity + self.drop_path(feat)
feat = self.act(feat)
return [coord, feat, offset] |