| 19 | |
| 20 | |
| 21 | class AutoEncoderGroupV3(nn.Module): |
| 22 | def __init__(self, geo_feat_channels, tex_feat_channels, feat_channel_up, mlp_hidden_channels, mlp_hidden_layers, use_tex=True, tex_channels=3, posenc=0) -> None: |
| 23 | super().__init__() |
| 24 | self.use_tex = use_tex |
| 25 | |
| 26 | self.geo_encoder = nn.Conv3d(1, geo_feat_channels, kernel_size=4, stride=2, padding=1, bias=True) |
| 27 | if use_tex: |
| 28 | self.tex_encoder = nn.Conv3d(tex_channels + 1, tex_feat_channels, kernel_size=4, stride=2, padding=1, bias=True) |
| 29 | out_channels = geo_feat_channels + tex_feat_channels if use_tex else geo_feat_channels |
| 30 | self.norm = nn.InstanceNorm2d(out_channels) |
| 31 | |
| 32 | self.geo_feat_dim = geo_feat_channels |
| 33 | self.tex_feat_dim = tex_feat_channels |
| 34 | |
| 35 | self.geo_convs = TriplaneGroupResnetBlock( |
| 36 | geo_feat_channels, feat_channel_up, ks=5, input_norm=False, input_act=False |
| 37 | ) |
| 38 | self.geo_decoder = DecoderMLP(feat_channel_up, 1, mlp_hidden_channels, mlp_hidden_layers) |
| 39 | |
| 40 | if use_tex: |
| 41 | self.tex_convs = TriplaneGroupResnetBlock( |
| 42 | tex_feat_channels, feat_channel_up, ks=5, input_norm=False, input_act=False |
| 43 | ) |
| 44 | self.tex_decoder = DecoderMLP(feat_channel_up, tex_channels, mlp_hidden_channels, mlp_hidden_layers, posenc=posenc) |
| 45 | |
| 46 | self.register_buffer("aabb", torch.tensor([-1, -1, -1, 1, 1, 1], dtype=torch.float32)) |
| 47 | |
| 48 | def geo_parameters(self): |
| 49 | return list(self.geo_encoder.parameters()) + list(self.geo_convs.parameters()) + list(self.geo_decoder.parameters()) |
| 50 | |
| 51 | def tex_parameters(self): |
| 52 | return list(self.tex_encoder.parameters()) + list(self.tex_convs.parameters()) + list(self.tex_decoder.parameters()) |
| 53 | |
| 54 | def reset_aabb(self, aabb): |
| 55 | print("set net aabb:", aabb) |
| 56 | if not isinstance(aabb, torch.Tensor): |
| 57 | aabb = torch.tensor(aabb, dtype=torch.float32) |
| 58 | # self.register_buffer("aabb", aabb.to(self.encoder.weight.device)) |
| 59 | self.aabb = aabb.to(self.geo_encoder.weight.device) |
| 60 | |
| 61 | def encode(self, vol): |
| 62 | geo_feat = self.geo_encoder(vol[:, :1]) |
| 63 | if self.use_tex: |
| 64 | tex_feat = self.tex_encoder(vol) |
| 65 | vol_feat = torch.cat([geo_feat, tex_feat], dim=1) |
| 66 | else: |
| 67 | vol_feat = geo_feat |
| 68 | |
| 69 | xy_feat = vol_feat.mean(dim=4) |
| 70 | xz_feat = vol_feat.mean(dim=3) |
| 71 | yz_feat = vol_feat.mean(dim=2) |
| 72 | |
| 73 | xy_feat = (self.norm(xy_feat) * 0.5).tanh() |
| 74 | xz_feat = (self.norm(xz_feat) * 0.5).tanh() |
| 75 | yz_feat = (self.norm(yz_feat) * 0.5).tanh() |
| 76 | |
| 77 | return [xy_feat, xz_feat, yz_feat] |
| 78 | |