"third_party/libxsmm/scripts/libxsmm_config.py" did not exist on "3359c1f1af3d67082d590a17a1663169e5fe29b3"
generic_model.py 36.8 KB
Newer Older
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
1
2
3
4
5
6
7
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.


Jeremy Reizenstein's avatar
logging  
Jeremy Reizenstein committed
8
import logging
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
9
10
11
12
13
14
15
16
import math
import warnings
from dataclasses import field
from typing import Any, Dict, List, Optional, Tuple

import torch
import tqdm
from pytorch3d.implicitron.tools import image_utils, vis_utils
17
18
19
20
from pytorch3d.implicitron.tools.config import (
    registry,
    run_auto_creation,
)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
21
from pytorch3d.implicitron.tools.rasterize_mc import rasterize_mc_samples
David Novotny's avatar
David Novotny committed
22
from pytorch3d.implicitron.tools.utils import cat_dataclass, setattr_if_hasattr
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
23
24
25
26
27
from pytorch3d.renderer import RayBundle, utils as rend_utils
from pytorch3d.renderer.cameras import CamerasBase
from visdom import Visdom

from .autodecoder import Autodecoder
28
from .base_model import ImplicitronModelBase, ImplicitronRender
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from .implicit_function.base import ImplicitFunctionBase
from .implicit_function.idr_feature_field import IdrFeatureField  # noqa
from .implicit_function.neural_radiance_field import (  # noqa
    NeRFormerImplicitFunction,
    NeuralRadianceFieldImplicitFunction,
)
from .implicit_function.scene_representation_networks import (  # noqa
    SRNHyperNetImplicitFunction,
    SRNImplicitFunction,
)
from .metrics import ViewMetrics
from .renderer.base import (
    BaseRenderer,
    EvaluationMode,
    ImplicitFunctionWrapper,
    RendererOutput,
    RenderSamplingMode,
)
from .renderer.lstm_renderer import LSTMRenderer  # noqa
from .renderer.multipass_ea import MultiPassEmissionAbsorptionRenderer  # noqa
David Novotny's avatar
David Novotny committed
49
from .renderer.ray_sampler import RaySamplerBase
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
50
51
from .renderer.sdf_renderer import SignedDistanceFunctionRenderer  # noqa
from .resnet_feature_extractor import ResNetFeatureExtractor
David Novotny's avatar
David Novotny committed
52
from .view_pooler.view_pooler import ViewPooler
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
53
54
55


STD_LOG_VARS = ["objective", "epoch", "sec/it"]
Jeremy Reizenstein's avatar
logging  
Jeremy Reizenstein committed
56
logger = logging.getLogger(__name__)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
57
58


59
60
@registry.register
class GenericModel(ImplicitronModelBase, torch.nn.Module):  # pyre-ignore: 13
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
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
    """
    GenericModel is a wrapper for the neural implicit
    rendering and reconstruction pipeline which consists
    of the following sequence of 7 steps (steps 2–4 are normally
    skipped in overfitting scenario, since conditioning on source views
    does not add much information; otherwise they should be present altogether):


        (1) Ray Sampling
        ------------------
        Rays are sampled from an image grid based on the target view(s).
                │_____________
                │             │
                │             ▼
                │    (2) Feature Extraction (optional)
                │    -----------------------
                │    A feature extractor (e.g. a convolutional
                │    neural net) is used to extract image features
                │    from the source view(s).
                │            │
                │            ▼
                │    (3) View Sampling  (optional)
                │    ------------------
                │    Image features are sampled at the 2D projections
                │    of a set of 3D points along each of the sampled
                │    target rays from (1).
                │            │
                │            ▼
                │    (4) Feature Aggregation  (optional)
                │    ------------------
                │    Aggregate features and masks sampled from
                │    image view(s) in (3).
                │            │
                │____________▼


        (5) Implicit Function Evaluation
        ------------------
        Evaluate the implicit function(s) at the sampled ray points
        (optionally pass in the aggregated image features from (4)).


        (6) Rendering
        ------------------
        Render the image into the target cameras by raymarching along
        the sampled rays and aggregating the colors and densities
        output by the implicit function in (5).


        (7) Loss Computation
        ------------------
        Compute losses based on the predicted target image(s).


    The `forward` function of GenericModel executes
    this sequence of steps. Currently, steps 1, 3, 4, 5, 6
    can be customized by intializing a subclass of the appropriate
    baseclass and adding the newly created module to the registry.
    Please see https://github.com/fairinternal/pytorch3d/blob/co3d/projects/implicitron_trainer/README.md#custom-plugins
    for more details on how to create and register a custom component.

    In the config .yaml files for experiments, the parameters below are
    contained in the `generic_model_args` node. As GenericModel
    derives from Configurable, the input arguments are
    parsed by the run_auto_creation function to initialize the
    necessary member modules. Please see implicitron_trainer/README.md
    for more details on this process.

    Args:
        mask_images: Whether or not to mask the RGB image background given the
            foreground mask (the `fg_probability` argument of `GenericModel.forward`)
        mask_depths: Whether or not to mask the depth image background given the
            foreground mask (the `fg_probability` argument of `GenericModel.forward`)
        render_image_width: Width of the output image to render
        render_image_height: Height of the output image to render
        mask_threshold: If greater than 0.0, the foreground mask is
            thresholded by this value before being applied to the RGB/Depth images
        output_rasterized_mc: If True, visualize the Monte-Carlo pixel renders by
            splatting onto an image grid. Default: False.
        bg_color: RGB values for the background color. Default (0.0, 0.0, 0.0)
        view_pool: If True, features are sampled from the source image(s)
            at the projected 2d locations of the sampled 3d ray points from the target
            view(s), i.e. this activates step (3) above.
        num_passes: The specified implicit_function is initialized num_passes
            times and run sequentially.
        chunk_size_grid: The total number of points which can be rendered
            per chunk. This is used to compute the number of rays used
            per chunk when the chunked version of the renderer is used (in order
            to fit rendering on all rays in memory)
        render_features_dimensions: The number of output features to render.
            Defaults to 3, corresponding to RGB images.
        n_train_target_views: The number of cameras to render into at training
            time; first `n_train_target_views` in the batch are considered targets,
            the rest are sources.
        sampling_mode_training: The sampling method to use during training. Must be
            a value from the RenderSamplingMode Enum.
        sampling_mode_evaluation: Same as above but for evaluation.
        sequence_autodecoder: An instance of `Autodecoder`. This is used to generate an encoding
            of the image (referred to as the global_code) that can be used to model aspects of
            the scene such as multiple objects or morphing objects. It is up to the implicit
            function definition how to use it, but the most typical way is to broadcast and
            concatenate to the other inputs for the implicit function.
David Novotny's avatar
David Novotny committed
163
164
        raysampler_class_type: The name of the raysampler class which is available
            in the global registry.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
165
166
167
168
169
170
        raysampler: An instance of RaySampler which is used to emit
            rays from the target view(s).
        renderer_class_type: The name of the renderer class which is available in the global
            registry.
        renderer: A renderer class which inherits from BaseRenderer. This is used to
            generate the images from the target view(s).
David Novotny's avatar
David Novotny committed
171
172
        image_feature_extractor_enabled: If `True`, constructs and enables
            the `image_feature_extractor` object.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
173
        image_feature_extractor: A module for extrating features from an input image.
David Novotny's avatar
David Novotny committed
174
175
        view_pooler_enabled: If `True`, constructs and enables the `view_pooler` object.
        view_pooler: An instance of ViewPooler which is used for sampling of
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
176
            image-based features at the 2D projections of a set
David Novotny's avatar
David Novotny committed
177
            of 3D points and aggregating the sampled features.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
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
        implicit_function_class_type: The type of implicit function to use which
            is available in the global registry.
        implicit_function: An instance of ImplicitFunctionBase. The actual implicit functions
            are initialised to be in self._implicit_functions.
        loss_weights: A dictionary with a {loss_name: weight} mapping; see documentation
            for `ViewMetrics` class for available loss functions.
        log_vars: A list of variable names which should be logged.
            The names should correspond to a subset of the keys of the
            dict `preds` output by the `forward` function.
    """

    mask_images: bool = True
    mask_depths: bool = True
    render_image_width: int = 400
    render_image_height: int = 400
    mask_threshold: float = 0.5
    output_rasterized_mc: bool = False
    bg_color: Tuple[float, float, float] = (0.0, 0.0, 0.0)
    num_passes: int = 1
    chunk_size_grid: int = 4096
    render_features_dimensions: int = 3
    tqdm_trigger_threshold: int = 16

    n_train_target_views: int = 1
    sampling_mode_training: str = "mask_sample"
    sampling_mode_evaluation: str = "full_grid"

    # ---- autodecoder settings
    sequence_autodecoder: Autodecoder

    # ---- raysampler
David Novotny's avatar
David Novotny committed
209
210
    raysampler_class_type: str = "AdaptiveRaySampler"
    raysampler: RaySamplerBase
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
211
212
213
214
215

    # ---- renderer configs
    renderer_class_type: str = "MultiPassEmissionAbsorptionRenderer"
    renderer: BaseRenderer

David Novotny's avatar
David Novotny committed
216
217
218
219
220
221
    # ---- image feature extractor settings
    image_feature_extractor_enabled: bool = False
    image_feature_extractor: Optional[ResNetFeatureExtractor]
    # ---- view pooler settings
    view_pooler_enabled: bool = False
    view_pooler: Optional[ViewPooler]
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
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

    # ---- implicit function settings
    implicit_function_class_type: str = "NeuralRadianceFieldImplicitFunction"
    # This is just a model, never constructed.
    # The actual implicit functions live in self._implicit_functions
    implicit_function: ImplicitFunctionBase

    # ---- loss weights
    loss_weights: Dict[str, float] = field(
        default_factory=lambda: {
            "loss_rgb_mse": 1.0,
            "loss_prev_stage_rgb_mse": 1.0,
            "loss_mask_bce": 0.0,
            "loss_prev_stage_mask_bce": 0.0,
        }
    )

    # ---- variables to be logged (logger automatically ignores if not computed)
    log_vars: List[str] = field(
        default_factory=lambda: [
            "loss_rgb_psnr_fg",
            "loss_rgb_psnr",
            "loss_rgb_mse",
            "loss_rgb_huber",
            "loss_depth_abs",
            "loss_depth_abs_fg",
            "loss_mask_neg_iou",
            "loss_mask_bce",
            "loss_mask_beta_prior",
            "loss_eikonal",
            "loss_density_tv",
            "loss_depth_neg_penalty",
            "loss_autodecoder_norm",
            # metrics that are only logged in 2+stage renderes
            "loss_prev_stage_rgb_mse",
            "loss_prev_stage_rgb_psnr_fg",
            "loss_prev_stage_rgb_psnr",
            "loss_prev_stage_mask_bce",
            *STD_LOG_VARS,
        ]
    )

    def __post_init__(self):
        super().__init__()
        self.view_metrics = ViewMetrics()

        run_auto_creation(self)

        self._implicit_functions = self._construct_implicit_functions()

Jeremy Reizenstein's avatar
logging  
Jeremy Reizenstein committed
272
        self.log_loss_weights()
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
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

    def forward(
        self,
        *,  # force keyword-only arguments
        image_rgb: Optional[torch.Tensor],
        camera: CamerasBase,
        fg_probability: Optional[torch.Tensor],
        mask_crop: Optional[torch.Tensor],
        depth_map: Optional[torch.Tensor],
        sequence_name: Optional[List[str]],
        evaluation_mode: EvaluationMode = EvaluationMode.EVALUATION,
        **kwargs,
    ) -> Dict[str, Any]:
        """
        Args:
            image_rgb: A tensor of shape `(B, 3, H, W)` containing a batch of rgb images;
                the first `min(B, n_train_target_views)` images are considered targets and
                are used to supervise the renders; the rest corresponding to the source
                viewpoints from which features will be extracted.
            camera: An instance of CamerasBase containing a batch of `B` cameras corresponding
                to the viewpoints of target images, from which the rays will be sampled,
                and source images, which will be used for intersecting with target rays.
            fg_probability: A tensor of shape `(B, 1, H, W)` containing a batch of
                foreground masks.
            mask_crop: A binary tensor of shape `(B, 1, H, W)` deonting valid
                regions in the input images (i.e. regions that do not correspond
                to, e.g., zero-padding). When the `RaySampler`'s sampling mode is set to
                "mask_sample", rays  will be sampled in the non zero regions.
            depth_map: A tensor of shape `(B, 1, H, W)` containing a batch of depth maps.
            sequence_name: A list of `B` strings corresponding to the sequence names
                from which images `image_rgb` were extracted. They are used to match
                target frames with relevant source frames.
            evaluation_mode: one of EvaluationMode.TRAINING or
                EvaluationMode.EVALUATION which determines the settings used for
                rendering.

        Returns:
            preds: A dictionary containing all outputs of the forward pass including the
                rendered images, depths, masks, losses and other metrics.
        """
        image_rgb, fg_probability, depth_map = self._preprocess_input(
            image_rgb, fg_probability, depth_map
        )

        # Obtain the batch size from the camera as this is the only required input.
        batch_size = camera.R.shape[0]

        # Determine the number of target views, i.e. cameras we render into.
        n_targets = (
            1
            if evaluation_mode == EvaluationMode.EVALUATION
            else batch_size
            if self.n_train_target_views <= 0
            else min(self.n_train_target_views, batch_size)
        )

        # Select the target cameras.
        target_cameras = camera[list(range(n_targets))]

        # Determine the used ray sampling mode.
        sampling_mode = RenderSamplingMode(
            self.sampling_mode_training
            if evaluation_mode == EvaluationMode.TRAINING
            else self.sampling_mode_evaluation
        )

        # (1) Sample rendering rays with the ray sampler.
David Novotny's avatar
David Novotny committed
340
        ray_bundle: RayBundle = self.raysampler(  # pyre-fixme[29]
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
341
342
343
344
345
346
347
348
349
350
            target_cameras,
            evaluation_mode,
            mask=mask_crop[:n_targets]
            if mask_crop is not None and sampling_mode == RenderSamplingMode.MASK_SAMPLE
            else None,
        )

        # custom_args hold additional arguments to the implicit function.
        custom_args = {}

David Novotny's avatar
David Novotny committed
351
352
353
354
355
356
357
        if self.image_feature_extractor_enabled:
            # (2) Extract features for the image
            img_feats = self.image_feature_extractor(  # pyre-fixme[29]
                image_rgb, fg_probability
            )

        if self.view_pooler_enabled:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
358
359
            if sequence_name is None:
                raise ValueError("sequence_name must be provided for view pooling")
David Novotny's avatar
David Novotny committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
            if not self.image_feature_extractor_enabled:
                raise ValueError(
                    "image_feature_extractor has to be enabled for for view pooling"
                    + " (I.e. set self.image_feature_extractor_enabled=True)."
                )

            # (3-4) Sample features and masks at the ray points.
            #       Aggregate features from multiple views.
            def curried_viewpooler(pts):
                return self.view_pooler(
                    pts=pts,
                    seq_id_pts=sequence_name[:n_targets],
                    camera=camera,
                    seq_id_camera=sequence_name,
                    feats=img_feats,
                    masks=mask_crop,
                )

            custom_args["fun_viewpool"] = curried_viewpooler
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

        global_code = None
        if self.sequence_autodecoder.n_instances > 0:
            if sequence_name is None:
                raise ValueError("sequence_name must be provided for autodecoder.")
            global_code = self.sequence_autodecoder(sequence_name[:n_targets])
        custom_args["global_code"] = global_code

        # pyre-fixme[29]:
        #  `Union[BoundMethod[typing.Callable(torch.Tensor.__iter__)[[Named(self,
        #  torch.Tensor)], typing.Iterator[typing.Any]], torch.Tensor], torch.Tensor,
        #  torch.nn.Module]` is not a function.
        for func in self._implicit_functions:
            func.bind_args(**custom_args)

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
394
        chunked_renderer_inputs = {}
395
        if fg_probability is not None and self.renderer.requires_object_mask():
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
396
397
398
            sampled_fb_prob = rend_utils.ndc_grid_sample(
                fg_probability[:n_targets], ray_bundle.xys, mode="nearest"
            )
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
399
            chunked_renderer_inputs["object_mask"] = sampled_fb_prob > 0.5
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
400
401
402
403
404
405
406

        # (5)-(6) Implicit function evaluation and Rendering
        rendered = self._render(
            ray_bundle=ray_bundle,
            sampling_mode=sampling_mode,
            evaluation_mode=evaluation_mode,
            implicit_functions=self._implicit_functions,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
407
            chunked_inputs=chunked_renderer_inputs,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        )

        # Unbind the custom arguments to prevent pytorch from storing
        # large buffers of intermediate results due to points in the
        # bound arguments.
        # pyre-fixme[29]:
        #  `Union[BoundMethod[typing.Callable(torch.Tensor.__iter__)[[Named(self,
        #  torch.Tensor)], typing.Iterator[typing.Any]], torch.Tensor], torch.Tensor,
        #  torch.nn.Module]` is not a function.
        for func in self._implicit_functions:
            func.unbind_args()

        preds = self._get_view_metrics(
            raymarched=rendered,
            xys=ray_bundle.xys,
            image_rgb=None if image_rgb is None else image_rgb[:n_targets],
            depth_map=None if depth_map is None else depth_map[:n_targets],
            fg_probability=None
            if fg_probability is None
            else fg_probability[:n_targets],
            mask_crop=None if mask_crop is None else mask_crop[:n_targets],
        )

        if sampling_mode == RenderSamplingMode.MASK_SAMPLE:
            if self.output_rasterized_mc:
                # Visualize the monte-carlo pixel renders by splatting onto
                # an image grid.
                (
                    preds["images_render"],
                    preds["depths_render"],
                    preds["masks_render"],
                ) = self._rasterize_mc_samples(
                    ray_bundle.xys,
                    rendered.features,
                    rendered.depths,
                    masks=rendered.masks,
                )
        elif sampling_mode == RenderSamplingMode.FULL_GRID:
            preds["images_render"] = rendered.features.permute(0, 3, 1, 2)
            preds["depths_render"] = rendered.depths.permute(0, 3, 1, 2)
            preds["masks_render"] = rendered.masks.permute(0, 3, 1, 2)

450
            preds["implicitron_render"] = ImplicitronRender(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
                image_render=preds["images_render"],
                depth_render=preds["depths_render"],
                mask_render=preds["masks_render"],
            )
        else:
            raise AssertionError("Unreachable state")

        # calc the AD penalty, returns None if autodecoder is not active
        ad_penalty = self.sequence_autodecoder.calc_squared_encoding_norm()
        if ad_penalty is not None:
            preds["loss_autodecoder_norm"] = ad_penalty

        # (7) Compute losses
        # finally get the optimization objective using self.loss_weights
        objective = self._get_objective(preds)
        if objective is not None:
            preds["objective"] = objective

        return preds

    def _get_objective(self, preds) -> Optional[torch.Tensor]:
        """
        A helper function to compute the overall loss as the dot product
        of individual loss functions with the corresponding weights.
        """
        losses_weighted = [
            preds[k] * float(w)
            for k, w in self.loss_weights.items()
            if (k in preds and w != 0.0)
        ]
        if len(losses_weighted) == 0:
            warnings.warn("No main objective found.")
            return None
        loss = sum(losses_weighted)
        assert torch.is_tensor(loss)
        return loss

    def visualize(
        self,
        viz: Visdom,
        visdom_env_imgs: str,
        preds: Dict[str, Any],
        prefix: str,
    ) -> None:
        """
        Helper function to visualize the predictions generated
        in the forward pass.

        Args:
            viz: Visdom connection object
            visdom_env_imgs: name of visdom environment for the images.
            preds: predictions dict like returned by forward()
            prefix: prepended to the names of images
        """
        if not viz.check_connection():
Jeremy Reizenstein's avatar
logging  
Jeremy Reizenstein committed
506
            logger.info("no visdom server! -> skipping batch vis")
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
507
508
509
510
511
512
513
514
515
516
517
            return

        idx_image = 0
        title = f"{prefix}_im{idx_image}"

        vis_utils.visualize_basics(viz, preds, visdom_env_imgs, title=title)

    def _render(
        self,
        *,
        ray_bundle: RayBundle,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
518
        chunked_inputs: Dict[str, torch.Tensor],
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
519
520
521
522
523
524
525
        sampling_mode: RenderSamplingMode,
        **kwargs,
    ) -> RendererOutput:
        """
        Args:
            ray_bundle: A `RayBundle` object containing the parametrizations of the
                sampled rendering rays.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
526
527
528
529
530
            chunked_inputs: A collection of tensor of shape `(B, _, H, W)`. E.g.
                SignedDistanceFunctionRenderer requires "object_mask", shape
                (B, 1, H, W), the silhouette of the object in the image. When
                chunking, they are passed to the renderer as shape
                `(B, _, chunksize)`.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
531
532
            sampling_mode: The sampling method to use. Must be a value from the
                RenderSamplingMode Enum.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
533

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
534
535
536
537
538
539
540
541
542
        Returns:
            An instance of RendererOutput
        """
        if sampling_mode == RenderSamplingMode.FULL_GRID and self.chunk_size_grid > 0:
            return _apply_chunked(
                self.renderer,
                _chunk_generator(
                    self.chunk_size_grid,
                    ray_bundle,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
543
                    chunked_inputs,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
544
545
546
547
548
549
550
551
552
                    self.tqdm_trigger_threshold,
                    **kwargs,
                ),
                lambda batch: _tensor_collator(batch, ray_bundle.lengths.shape[:-1]),
            )
        else:
            # pyre-fixme[29]: `BaseRenderer` is not a function.
            return self.renderer(
                ray_bundle=ray_bundle,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
553
                **chunked_inputs,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
554
555
556
557
558
                **kwargs,
            )

    def _get_viewpooled_feature_dim(self):
        return (
David Novotny's avatar
David Novotny committed
559
            self.view_pooler.get_aggregated_feature_dim(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
560
561
                self.image_feature_extractor.get_feat_dims()
            )
David Novotny's avatar
David Novotny committed
562
            if self.view_pooler_enabled
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
563
564
565
            else 0
        )

David Novotny's avatar
David Novotny committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
    def create_raysampler(self):
        raysampler_args = getattr(
            self, "raysampler_" + self.raysampler_class_type + "_args"
        )
        setattr_if_hasattr(
            raysampler_args, "sampling_mode_training", self.sampling_mode_training
        )
        setattr_if_hasattr(
            raysampler_args, "sampling_mode_evaluation", self.sampling_mode_evaluation
        )
        setattr_if_hasattr(raysampler_args, "image_width", self.render_image_width)
        setattr_if_hasattr(raysampler_args, "image_height", self.render_image_height)
        self.raysampler = registry.get(RaySamplerBase, self.raysampler_class_type)(
            **raysampler_args
        )

    def create_renderer(self):
        raysampler_args = getattr(
            self, "raysampler_" + self.raysampler_class_type + "_args"
        )
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
586
587
        self.renderer_MultiPassEmissionAbsorptionRenderer_args[
            "stratified_sampling_coarse_training"
David Novotny's avatar
David Novotny committed
588
        ] = raysampler_args["stratified_point_sampling_training"]
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
589
590
        self.renderer_MultiPassEmissionAbsorptionRenderer_args[
            "stratified_sampling_coarse_evaluation"
David Novotny's avatar
David Novotny committed
591
        ] = raysampler_args["stratified_point_sampling_evaluation"]
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
592
593
594
        self.renderer_SignedDistanceFunctionRenderer_args[
            "render_features_dimensions"
        ] = self.render_features_dimensions
David Novotny's avatar
David Novotny committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

        if self.renderer_class_type == "SignedDistanceFunctionRenderer":
            if "scene_extent" not in raysampler_args:
                raise ValueError(
                    "SignedDistanceFunctionRenderer requires"
                    + " a raysampler that defines the 'scene_extent' field"
                    + " (this field is supported by, e.g., the adaptive raysampler - "
                    + " self.raysampler_class_type='AdaptiveRaySampler')."
                )
            self.renderer_SignedDistanceFunctionRenderer_args.ray_tracer_args[
                "object_bounding_sphere"
            ] = self.raysampler_AdaptiveRaySampler_args["scene_extent"]

        renderer_args = getattr(self, "renderer_" + self.renderer_class_type + "_args")
        self.renderer = registry.get(BaseRenderer, self.renderer_class_type)(
            **renderer_args
        )
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
612

David Novotny's avatar
David Novotny committed
613
    def create_view_pooler(self):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
614
        """
David Novotny's avatar
David Novotny committed
615
616
        Custom creation function called by run_auto_creation checking
        that image_feature_extractor is enabled when view_pooler is enabled.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
617
        """
David Novotny's avatar
David Novotny committed
618
619
620
621
622
623
624
625
626
        if self.view_pooler_enabled:
            if not self.image_feature_extractor_enabled:
                raise ValueError(
                    "image_feature_extractor has to be enabled for view pooling"
                    + " (I.e. set self.image_feature_extractor_enabled=True)."
                )
            self.view_pooler = ViewPooler(**self.view_pooler_args)
        else:
            self.view_pooler = None
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686

    def create_implicit_function(self) -> None:
        """
        No-op called by run_auto_creation so that self.implicit_function
        does not get created. __post_init__ creates the implicit function(s)
        in wrappers explicitly in self._implicit_functions.
        """
        pass

    def _construct_implicit_functions(self):
        """
        After run_auto_creation has been called, the arguments
        for each of the possible implicit function methods are
        available. `GenericModel` arguments are first validated
        based on the custom requirements for each specific
        implicit function method. Then the required implicit
        function(s) are initialized.
        """
        # nerf preprocessing
        nerf_args = self.implicit_function_NeuralRadianceFieldImplicitFunction_args
        nerformer_args = self.implicit_function_NeRFormerImplicitFunction_args
        nerf_args["latent_dim"] = nerformer_args["latent_dim"] = (
            self._get_viewpooled_feature_dim()
            + self.sequence_autodecoder.get_encoding_dim()
        )
        nerf_args["color_dim"] = nerformer_args[
            "color_dim"
        ] = self.render_features_dimensions

        # idr preprocessing
        idr = self.implicit_function_IdrFeatureField_args
        idr["feature_vector_size"] = self.render_features_dimensions
        idr["encoding_dim"] = self.sequence_autodecoder.get_encoding_dim()

        # srn preprocessing
        srn = self.implicit_function_SRNImplicitFunction_args
        srn.raymarch_function_args.latent_dim = (
            self._get_viewpooled_feature_dim()
            + self.sequence_autodecoder.get_encoding_dim()
        )

        # srn_hypernet preprocessing
        srn_hypernet = self.implicit_function_SRNHyperNetImplicitFunction_args
        srn_hypernet_args = srn_hypernet.hypernet_args
        srn_hypernet_args.latent_dim_hypernet = (
            self.sequence_autodecoder.get_encoding_dim()
        )
        srn_hypernet_args.latent_dim = self._get_viewpooled_feature_dim()

        # check that for srn, srn_hypernet, idr we have self.num_passes=1
        implicit_function_type = registry.get(
            ImplicitFunctionBase, self.implicit_function_class_type
        )
        if self.num_passes != 1 and not implicit_function_type.allows_multiple_passes():
            raise ValueError(
                self.implicit_function_class_type
                + f"requires num_passes=1 not {self.num_passes}"
            )

        if implicit_function_type.requires_pooling_without_aggregation():
David Novotny's avatar
David Novotny committed
687
            if self.view_pooler_enabled and self.view_pooler.has_aggregation():
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
688
                raise ValueError(
David Novotny's avatar
David Novotny committed
689
                    "The chosen implicit function requires view pooling without aggregation."
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
690
691
692
693
694
695
696
697
698
699
700
                )
        config_name = f"implicit_function_{self.implicit_function_class_type}_args"
        config = getattr(self, config_name, None)
        if config is None:
            raise ValueError(f"{config_name} not present")
        implicit_functions_list = [
            ImplicitFunctionWrapper(implicit_function_type(**config))
            for _ in range(self.num_passes)
        ]
        return torch.nn.ModuleList(implicit_functions_list)

Jeremy Reizenstein's avatar
logging  
Jeremy Reizenstein committed
701
    def log_loss_weights(self) -> None:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
702
703
704
        """
        Print a table of the loss weights.
        """
Jeremy Reizenstein's avatar
logging  
Jeremy Reizenstein committed
705
706
707
708
709
710
        loss_weights_message = (
            "-------\nloss_weights:\n"
            + "\n".join(f"{k:40s}: {w:1.2e}" for k, w in self.loss_weights.items())
            + "-------"
        )
        logger.info(loss_weights_message)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873

    def _preprocess_input(
        self,
        image_rgb: Optional[torch.Tensor],
        fg_probability: Optional[torch.Tensor],
        depth_map: Optional[torch.Tensor],
    ) -> Tuple[Optional[torch.Tensor], Optional[torch.Tensor], Optional[torch.Tensor]]:
        """
        Helper function to preprocess the input images and optional depth maps
        to apply masking if required.

        Args:
            image_rgb: A tensor of shape `(B, 3, H, W)` containing a batch of rgb images
                corresponding to the source viewpoints from which features will be extracted
            fg_probability: A tensor of shape `(B, 1, H, W)` containing a batch
                of foreground masks with values in [0, 1].
            depth_map: A tensor of shape `(B, 1, H, W)` containing a batch of depth maps.

        Returns:
            Modified image_rgb, fg_mask, depth_map
        """
        fg_mask = fg_probability
        if fg_mask is not None and self.mask_threshold > 0.0:
            # threshold masks
            warnings.warn("Thresholding masks!")
            fg_mask = (fg_mask >= self.mask_threshold).type_as(fg_mask)

        if self.mask_images and fg_mask is not None and image_rgb is not None:
            # mask the image
            warnings.warn("Masking images!")
            image_rgb = image_utils.mask_background(
                image_rgb, fg_mask, dim_color=1, bg_color=torch.tensor(self.bg_color)
            )

        if self.mask_depths and fg_mask is not None and depth_map is not None:
            # mask the depths
            assert (
                self.mask_threshold > 0.0
            ), "Depths should be masked only with thresholded masks"
            warnings.warn("Masking depths!")
            depth_map = depth_map * fg_mask

        return image_rgb, fg_mask, depth_map

    def _get_view_metrics(
        self,
        raymarched: RendererOutput,
        xys: torch.Tensor,
        image_rgb: Optional[torch.Tensor] = None,
        depth_map: Optional[torch.Tensor] = None,
        fg_probability: Optional[torch.Tensor] = None,
        mask_crop: Optional[torch.Tensor] = None,
        keys_prefix: str = "loss_",
    ):
        # pyre-fixme[29]: `Union[torch.Tensor, torch.nn.Module]` is not a function.
        metrics = self.view_metrics(
            image_sampling_grid=xys,
            images_pred=raymarched.features,
            images=image_rgb,
            depths_pred=raymarched.depths,
            depths=depth_map,
            masks_pred=raymarched.masks,
            masks=fg_probability,
            masks_crop=mask_crop,
            keys_prefix=keys_prefix,
            **raymarched.aux,
        )

        if raymarched.prev_stage:
            metrics.update(
                self._get_view_metrics(
                    raymarched.prev_stage,
                    xys,
                    image_rgb,
                    depth_map,
                    fg_probability,
                    mask_crop,
                    keys_prefix=(keys_prefix + "prev_stage_"),
                )
            )

        return metrics

    @torch.no_grad()
    def _rasterize_mc_samples(
        self,
        xys: torch.Tensor,
        features: torch.Tensor,
        depth: torch.Tensor,
        masks: Optional[torch.Tensor] = None,
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        """
        Rasterizes Monte-Carlo features back onto the image.

        Args:
            xys: B x ... x 2 2D point locations in PyTorch3D NDC convention
            features: B x ... x C tensor containing per-point rendered features.
            depth: B x ... x 1 tensor containing per-point rendered depth.
        """
        ba = xys.shape[0]

        # Flatten the features and xy locations.
        features_depth_ras = torch.cat(
            (
                features.reshape(ba, -1, features.shape[-1]),
                depth.reshape(ba, -1, 1),
            ),
            dim=-1,
        )
        xys_ras = xys.reshape(ba, -1, 2)
        if masks is not None:
            masks_ras = masks.reshape(ba, -1, 1)
        else:
            masks_ras = None

        if min(self.render_image_height, self.render_image_width) <= 0:
            raise ValueError(
                "Need to specify a positive"
                " self.render_image_height and self.render_image_width"
                " for MC rasterisation."
            )

        # Estimate the rasterization point radius so that we approximately fill
        # the whole image given the number of rasterized points.
        pt_radius = 2.0 * math.sqrt(xys.shape[1])

        # Rasterize the samples.
        features_depth_render, masks_render = rasterize_mc_samples(
            xys_ras,
            features_depth_ras,
            (self.render_image_height, self.render_image_width),
            radius=pt_radius,
            masks=masks_ras,
        )
        images_render = features_depth_render[:, :-1]
        depths_render = features_depth_render[:, -1:]
        return images_render, depths_render, masks_render


def _apply_chunked(func, chunk_generator, tensor_collator):
    """
    Helper function to apply a function on a sequence of
    chunked inputs yielded by a generator and collate
    the result.
    """
    processed_chunks = [
        func(*chunk_args, **chunk_kwargs)
        for chunk_args, chunk_kwargs in chunk_generator
    ]

    return cat_dataclass(processed_chunks, tensor_collator)


def _tensor_collator(batch, new_dims) -> torch.Tensor:
    """
    Helper function to reshape the batch to the desired shape
    """
    return torch.cat(batch, dim=1).reshape(*new_dims, -1)


def _chunk_generator(
    chunk_size: int,
    ray_bundle: RayBundle,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
874
    chunked_inputs: Dict[str, torch.Tensor],
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
    tqdm_trigger_threshold: int,
    *args,
    **kwargs,
):
    """
    Helper function which yields chunks of rays from the
    input ray_bundle, to be used when the number of rays is
    large and will not fit in memory for rendering.
    """
    (
        batch_size,
        *spatial_dim,
        n_pts_per_ray,
    ) = ray_bundle.lengths.shape  # B x ... x n_pts_per_ray
    if n_pts_per_ray > 0 and chunk_size % n_pts_per_ray != 0:
        raise ValueError(
            f"chunk_size_grid ({chunk_size}) should be divisible "
            f"by n_pts_per_ray ({n_pts_per_ray})"
        )

    n_rays = math.prod(spatial_dim)
    # special handling for raytracing-based methods
    n_chunks = -(-n_rays * max(n_pts_per_ray, 1) // chunk_size)
    chunk_size_in_rays = -(-n_rays // n_chunks)

    iter = range(0, n_rays, chunk_size_in_rays)
    if len(iter) >= tqdm_trigger_threshold:
        iter = tqdm.tqdm(iter)

    for start_idx in iter:
        end_idx = min(start_idx + chunk_size_in_rays, n_rays)
        ray_bundle_chunk = RayBundle(
            origins=ray_bundle.origins.reshape(batch_size, -1, 3)[:, start_idx:end_idx],
            directions=ray_bundle.directions.reshape(batch_size, -1, 3)[
                :, start_idx:end_idx
            ],
            lengths=ray_bundle.lengths.reshape(
                batch_size, math.prod(spatial_dim), n_pts_per_ray
            )[:, start_idx:end_idx],
            xys=ray_bundle.xys.reshape(batch_size, -1, 2)[:, start_idx:end_idx],
        )
        extra_args = kwargs.copy()
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
917
918
        for k, v in chunked_inputs.items():
            extra_args[k] = v.flatten(2)[:, :, start_idx:end_idx]
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
919
        yield [ray_bundle_chunk, *args], extra_args