File size: 11,276 Bytes
c3ec853
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""
MLP-based decoder for reconstructing point clouds from latent codes.

This module provides flexible MLP architectures for decoding latent representations
into point clouds, commonly used in generative models like VAE and cVAE.
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import List, Optional


class PointCloudDecoder(nn.Module):
    """
    MLP-based decoder for reconstructing point clouds from latent representations.

    This decoder uses a series of fully connected layers with optional dropout
    and normalization to transform a latent code into point cloud coordinates.

    Args:
        latent_dim: Dimensionality of input latent code
        num_points: Number of output points in the point cloud
        point_dim: Dimensionality of each point (default: 3 for XYZ coordinates)
        hidden_dims: List of hidden layer dimensions (default: [1024, 2048])
        dropout_rate: Dropout probability (default: 0.3)
        use_batch_norm: Whether to use batch normalization (default: False)
        activation: Activation function to use (default: 'relu')

    Input:
        Latent code of shape (B, latent_dim)

    Output:
        Point cloud of shape (B, num_points, point_dim)
    """

    def __init__(
        self,
        latent_dim: int,
        num_points: int,
        point_dim: int = 3,
        hidden_dims: Optional[List[int]] = None,
        dropout_rate: float = 0.3,
        use_batch_norm: bool = False,
        activation: str = 'relu'
    ):
        super(PointCloudDecoder, self).__init__()
        self.latent_dim = latent_dim
        self.num_points = num_points
        self.point_dim = point_dim
        self.dropout_rate = dropout_rate
        self.use_batch_norm = use_batch_norm

        # Default hidden dimensions
        if hidden_dims is None:
            hidden_dims = [1024, 2048]
        self.hidden_dims = hidden_dims

        # Select activation function
        if activation == 'relu':
            self.activation = nn.ReLU()
        elif activation == 'leaky_relu':
            self.activation = nn.LeakyReLU(0.2)
        elif activation == 'elu':
            self.activation = nn.ELU()
        elif activation == 'gelu':
            self.activation = nn.GELU()
        else:
            raise ValueError(f"Unsupported activation: {activation}")

        # Build network layers
        self.layers = nn.ModuleList()
        self.batch_norms = nn.ModuleList() if use_batch_norm else None
        self.dropouts = nn.ModuleList()

        # Input layer
        prev_dim = latent_dim
        for hidden_dim in hidden_dims:
            self.layers.append(nn.Linear(prev_dim, hidden_dim))
            if use_batch_norm:
                self.batch_norms.append(nn.BatchNorm1d(hidden_dim))
            self.dropouts.append(nn.Dropout(dropout_rate))
            prev_dim = hidden_dim

        # Output layer
        output_dim = num_points * point_dim
        self.output_layer = nn.Linear(prev_dim, output_dim)

    def forward(self, z: torch.Tensor) -> torch.Tensor:
        """
        Decode latent code into point cloud.

        Args:
            z: Latent code of shape (B, latent_dim)

        Returns:
            Reconstructed point cloud of shape (B, num_points, point_dim)
        """
        x = z

        # Process through hidden layers
        for i, layer in enumerate(self.layers):
            x = layer(x)
            if self.use_batch_norm:
                x = self.batch_norms[i](x)
            x = self.activation(x)
            x = self.dropouts[i](x)

        # Output layer
        x = self.output_layer(x)

        # Reshape to point cloud
        x = x.view(-1, self.num_points, self.point_dim)

        return x

    def get_output_shape(self) -> tuple:
        """
        Get the output point cloud shape (excluding batch dimension).

        Returns:
            Tuple of (num_points, point_dim)
        """
        return (self.num_points, self.point_dim)


class ResidualMLPDecoder(nn.Module):
    """
    MLP decoder with residual connections for improved gradient flow.

    This decoder uses residual blocks to help with training deep networks.

    Args:
        latent_dim: Dimensionality of input latent code
        num_points: Number of output points in the point cloud
        point_dim: Dimensionality of each point (default: 3 for XYZ coordinates)
        hidden_dim: Hidden layer dimension (default: 1024)
        num_blocks: Number of residual blocks (default: 3)
        dropout_rate: Dropout probability (default: 0.3)
        use_batch_norm: Whether to use batch normalization (default: True)

    Input:
        Latent code of shape (B, latent_dim)

    Output:
        Point cloud of shape (B, num_points, point_dim)
    """

    def __init__(
        self,
        latent_dim: int,
        num_points: int,
        point_dim: int = 3,
        hidden_dim: int = 1024,
        num_blocks: int = 3,
        dropout_rate: float = 0.3,
        use_batch_norm: bool = True
    ):
        super(ResidualMLPDecoder, self).__init__()
        self.latent_dim = latent_dim
        self.num_points = num_points
        self.point_dim = point_dim
        self.hidden_dim = hidden_dim

        # Initial projection
        self.input_proj = nn.Linear(latent_dim, hidden_dim)

        # Residual blocks
        self.blocks = nn.ModuleList([
            ResidualBlock(hidden_dim, dropout_rate, use_batch_norm)
            for _ in range(num_blocks)
        ])

        # Output projection
        output_dim = num_points * point_dim
        self.output_proj = nn.Linear(hidden_dim, output_dim)

    def forward(self, z: torch.Tensor) -> torch.Tensor:
        """
        Decode latent code into point cloud.

        Args:
            z: Latent code of shape (B, latent_dim)

        Returns:
            Reconstructed point cloud of shape (B, num_points, point_dim)
        """
        # Initial projection
        x = F.relu(self.input_proj(z))

        # Residual blocks
        for block in self.blocks:
            x = block(x)

        # Output projection
        x = self.output_proj(x)
        x = x.view(-1, self.num_points, self.point_dim)

        return x


class ResidualBlock(nn.Module):
    """
    Residual block with optional batch normalization and dropout.

    Args:
        hidden_dim: Dimension of the hidden layer
        dropout_rate: Dropout probability
        use_batch_norm: Whether to use batch normalization
    """

    def __init__(
        self,
        hidden_dim: int,
        dropout_rate: float = 0.3,
        use_batch_norm: bool = True
    ):
        super(ResidualBlock, self).__init__()
        self.fc1 = nn.Linear(hidden_dim, hidden_dim)
        self.fc2 = nn.Linear(hidden_dim, hidden_dim)
        self.dropout = nn.Dropout(dropout_rate)

        if use_batch_norm:
            self.bn1 = nn.BatchNorm1d(hidden_dim)
            self.bn2 = nn.BatchNorm1d(hidden_dim)
        else:
            self.bn1 = None
            self.bn2 = None

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Forward pass through residual block.

        Args:
            x: Input tensor of shape (B, hidden_dim)

        Returns:
            Output tensor of shape (B, hidden_dim)
        """
        residual = x

        out = self.fc1(x)
        if self.bn1 is not None:
            out = self.bn1(out)
        out = F.relu(out)
        out = self.dropout(out)

        out = self.fc2(out)
        if self.bn2 is not None:
            out = self.bn2(out)

        out = out + residual
        out = F.relu(out)

        return out


def create_pointcloud_decoder(
    latent_dim: int,
    num_points: int = 6890,
    point_dim: int = 3,
    architecture: str = 'mlp',
    **kwargs
) -> nn.Module:
    """
    Factory function to create a point cloud decoder with specified architecture.

    Args:
        latent_dim: Dimensionality of input latent code
        num_points: Number of output points (default: 6890 for SMPL mesh)
        point_dim: Dimensionality of each point (default: 3 for XYZ)
        architecture: Decoder architecture type ('mlp' or 'residual')
        **kwargs: Additional arguments passed to the decoder constructor

    Returns:
        Configured decoder instance

    Examples:
        >>> # Create a simple MLP decoder
        >>> decoder = create_pointcloud_decoder(
        ...     latent_dim=256,
        ...     num_points=6890,
        ...     architecture='mlp',
        ...     hidden_dims=[1024, 2048],
        ...     dropout_rate=0.3
        ... )

        >>> # Create a residual decoder
        >>> decoder = create_pointcloud_decoder(
        ...     latent_dim=256,
        ...     num_points=6890,
        ...     architecture='residual',
        ...     hidden_dim=1024,
        ...     num_blocks=3
        ... )
    """
    if architecture == 'mlp':
        return PointCloudDecoder(
            latent_dim=latent_dim,
            num_points=num_points,
            point_dim=point_dim,
            **kwargs
        )
    elif architecture == 'residual':
        return ResidualMLPDecoder(
            latent_dim=latent_dim,
            num_points=num_points,
            point_dim=point_dim,
            **kwargs
        )
    else:
        raise ValueError(f"Unsupported architecture: {architecture}")


if __name__ == '__main__':
    print("Testing Point Cloud Decoders...\n")

    # Test parameters
    batch_size = 32
    latent_dim = 256
    num_points = 6890  # SMPL mesh vertices
    point_dim = 3

    # Test standard MLP decoder
    print("1. Standard MLP Decoder:")
    mlp_decoder = create_pointcloud_decoder(
        latent_dim=latent_dim,
        num_points=num_points,
        point_dim=point_dim,
        architecture='mlp',
        hidden_dims=[1024, 2048],
        dropout_rate=0.3,
        use_batch_norm=False
    )

    z = torch.randn(batch_size, latent_dim)
    output = mlp_decoder(z)
    print(f"  Input shape: {z.shape}")
    print(f"  Output shape: {output.shape}")
    print(f"  Output expected shape: {mlp_decoder.get_output_shape()}")
    print()

    # Test MLP decoder with batch norm
    print("2. MLP Decoder with Batch Normalization:")
    mlp_bn_decoder = create_pointcloud_decoder(
        latent_dim=latent_dim,
        num_points=num_points,
        architecture='mlp',
        use_batch_norm=True,
        activation='leaky_relu'
    )
    output = mlp_bn_decoder(z)
    print(f"  Output shape: {output.shape}")
    print()

    # Test residual MLP decoder
    print("3. Residual MLP Decoder:")
    residual_decoder = create_pointcloud_decoder(
        latent_dim=latent_dim,
        num_points=num_points,
        architecture='residual',
        hidden_dim=1024,
        num_blocks=3,
        dropout_rate=0.3
    )
    output = residual_decoder(z)
    print(f"  Input shape: {z.shape}")
    print(f"  Output shape: {output.shape}")
    print()

    # Test with different point dimensions
    print("4. Decoder with 6D points (XYZ + RGB):")
    decoder_6d = create_pointcloud_decoder(
        latent_dim=latent_dim,
        num_points=1000,
        point_dim=6,
        architecture='mlp',
        hidden_dims=[512, 1024]
    )
    output = decoder_6d(z)
    print(f"  Output shape: {output.shape}")
    print()