examples/nerf.py
def show_full_render(
        neural_radiance_field, camera,
        target_image, target_silhouette,
        loss_history_color, loss_history_sil,
        renderer_grid, num_forward=1
):
    """
    This is a helper function for visualizing the
    intermediate results of the learning.

    Since the `NeuralRadianceField` suffers from
    a large memory footprint, which does not allow to
    render the full image grid in a single forward pass,
    we utilize the `NeuralRadianceField.batched_forward`
    function in combination with disabling the gradient caching.
    This chunks the set of emitted rays to batches and
    evaluates the implicit function on one-batch at a time
    to prevent GPU memory overflow.
    """

    rendered_image_list, rendered_silhouette_list = [], []
    # Prevent gradient caching.
    with torch.no_grad():
        for _ in range(num_forward):
            # Render using the grid renderer and the
            # batched_forward function of neural_radiance_field.
            rendered_image_silhouette, _ = renderer_grid(
                cameras=camera,
                volumetric_function=partial(batched_forward, net=neural_radiance_field)
            )
            # Split the rendering result to a silhouette render
            # and the image render.
            rendered_image_, rendered_silhouette_ = (
                rendered_image_silhouette[0].split([3, 1], dim=-1)
            )
            rendered_image_list.append(rendered_image_)
            rendered_silhouette_list.append(rendered_silhouette_)

        rendered_images = torch.stack(rendered_image_list)
        rendered_image = rendered_images.mean(0)

        rendered_silhouettes = torch.stack(rendered_silhouette_list)
        rendered_silhouette = rendered_silhouettes.mean(0)

        if num_forward > 1:
            rendered_image_std = rendered_images.var(0).sum(-1).sqrt()
            rendered_silhouette_std = rendered_silhouettes.std(0)
        else:
            rendered_image_std = torch.zeros_like(rendered_image[..., 0])
            rendered_silhouette_std = torch.zeros_like(rendered_silhouette)

    print(f"Max image std: {rendered_image_std.max().item():.4f}; "
          f"max image: {rendered_image.max().item():.4f}; "
          f"max silhouette std: {rendered_silhouette_std.max().item():.4f}; "
          f"max silhouette: {rendered_silhouette.max().item():.4f}")
    # Generate plots.
    fig, ax = plt.subplots(2, 4, figsize=(20, 10))
    ax = ax.ravel()
    clamp_and_detach = lambda x: x.clamp(0.0, 1.0).cpu().detach().numpy()
    ax[0].plot(list(range(len(loss_history_color))), loss_history_color, linewidth=1)
    ax[1].imshow(clamp_and_detach(rendered_image))
    ax[2].imshow(clamp_and_detach(rendered_silhouette[..., 0]))
    ax[3].imshow(clamp_and_detach(rendered_image_std), cmap="hot", vmax=0.75 ** 0.5)
    ax[4].plot(list(range(len(loss_history_sil))), loss_history_sil, linewidth=1)
    ax[5].imshow(clamp_and_detach(target_image))
    ax[6].imshow(clamp_and_detach(target_silhouette))
    ax[7].imshow(clamp_and_detach(rendered_silhouette_std), cmap="hot", vmax=0.5)
    for ax_, title_ in zip(
            ax,
            (
                    "loss color", "rendered image", "rendered silhouette", "image uncertainty",
                    "loss silhouette", "target image", "target silhouette", "silhouette uncertainty"
            )
    ):
        if not title_.startswith('loss'):
            ax_.grid("off")
            ax_.axis("off")
        ax_.set_title(title_)
    fig.canvas.draw()
    fig.show()
    return fig

    This is a helper function for visualizing the
    intermediate results of the learning.

    Since the `NeuralRadianceField` suffers from
    a large memory footprint, which does not allow to
    render the full image grid in a single forward pass,
    we utilize the `NeuralRadianceField.batched_forward`
    function in combination with disabling the gradient caching.
    This chunks the set of emitted rays to batches and
    evaluates the implicit function on one-batch at a time
    to prevent GPU memory overflow.
    
This is a helper function for visualizing the
intermediate results of the learning.

Since the `NeuralRadianceField` suffers from
a large memory footprint, which does not allow to
render the full image grid in a single forward pass,
we utilize the `NeuralRadianceField.batched_forward`
function in combination with disabling the gradient caching.
This chunks the set of emitted rays to batches and
evaluates the implicit function on one-batch at a time
to prevent GPU memory overflow.
This is a helper function for visualizing the intermediate results of the learning . Since the ` NeuralRadianceField ` suffers from a large memory footprint which does not allow to render the full image grid in a single forward pass we utilize the ` NeuralRadianceField . batched_forward ` function in combination with disabling the gradient caching . This chunks the set of emitted rays to batches and evaluates the implicit function on one - batch at a time to prevent GPU memory overflow .
def show_full_render(
        neural_radiance_field, camera,
        target_image, target_silhouette,
        loss_history_color, loss_history_sil,
        renderer_grid, num_forward=1
):
    rendered_image_list, rendered_silhouette_list = [], []
    with torch.no_grad():
        for _ in range(num_forward):
            rendered_image_silhouette, _ = renderer_grid(
                cameras=camera,
                volumetric_function=partial(batched_forward, net=neural_radiance_field)
            )
            rendered_image_, rendered_silhouette_ = (
                rendered_image_silhouette[0].split([3, 1], dim=-1)
            )
            rendered_image_list.append(rendered_image_)
            rendered_silhouette_list.append(rendered_silhouette_)
        rendered_images = torch.stack(rendered_image_list)
        rendered_image = rendered_images.mean(0)
        rendered_silhouettes = torch.stack(rendered_silhouette_list)
        rendered_silhouette = rendered_silhouettes.mean(0)
        if num_forward > 1:
            rendered_image_std = rendered_images.var(0).sum(-1).sqrt()
            rendered_silhouette_std = rendered_silhouettes.std(0)
        else:
            rendered_image_std = torch.zeros_like(rendered_image[..., 0])
            rendered_silhouette_std = torch.zeros_like(rendered_silhouette)
    print(f"Max image std: {rendered_image_std.max().item():.4f}; "
          f"max image: {rendered_image.max().item():.4f}; "
          f"max silhouette std: {rendered_silhouette_std.max().item():.4f}; "
          f"max silhouette: {rendered_silhouette.max().item():.4f}")
    fig, ax = plt.subplots(2, 4, figsize=(20, 10))
    ax = ax.ravel()
    clamp_and_detach = lambda x: x.clamp(0.0, 1.0).cpu().detach().numpy()
    ax[0].plot(list(range(len(loss_history_color))), loss_history_color, linewidth=1)
    ax[1].imshow(clamp_and_detach(rendered_image))
    ax[2].imshow(clamp_and_detach(rendered_silhouette[..., 0]))
    ax[3].imshow(clamp_and_detach(rendered_image_std), cmap="hot", vmax=0.75 ** 0.5)
    ax[4].plot(list(range(len(loss_history_sil))), loss_history_sil, linewidth=1)
    ax[5].imshow(clamp_and_detach(target_image))
    ax[6].imshow(clamp_and_detach(target_silhouette))
    ax[7].imshow(clamp_and_detach(rendered_silhouette_std), cmap="hot", vmax=0.5)
    for ax_, title_ in zip(
            ax,
            (
                    "loss color", "rendered image", "rendered silhouette", "image uncertainty",
                    "loss silhouette", "target image", "target silhouette", "silhouette uncertainty"
            )
    ):
        if not title_.startswith('loss'):
            ax_.grid("off")
            ax_.axis("off")
        ax_.set_title(title_)
    fig.canvas.draw()
    fig.show()
    return fig
def show_full_render ( neural_radiance_field , camera , target_image , target_silhouette , loss_history_color , loss_history_sil , renderer_grid , num_forward = 1 ) : rendered_image_list , rendered_silhouette_list = [ ] , [ ] with torch . no_grad ( ) : for _ in range ( num_forward ) : rendered_image_silhouette , _ = renderer_grid ( cameras = camera , volumetric_function = partial ( batched_forward , net = neural_radiance_field ) ) rendered_image_ , rendered_silhouette_ = ( rendered_image_silhouette [ 0 ] . split ( [ 3 , 1 ] , dim = - 1 ) ) rendered_image_list . append ( rendered_image_ ) rendered_silhouette_list . append ( rendered_silhouette_ ) rendered_images = torch . stack ( rendered_image_list ) rendered_image = rendered_images . mean ( 0 ) rendered_silhouettes = torch . stack ( rendered_silhouette_list ) rendered_silhouette = rendered_silhouettes . mean ( 0 ) if num_forward > 1 : rendered_image_std = rendered_images . var ( 0 ) . sum ( - 1 ) . sqrt ( ) rendered_silhouette_std = rendered_silhouettes . std ( 0 ) else : rendered_image_std = torch . zeros_like ( rendered_image [ ... , 0 ] ) rendered_silhouette_std = torch . zeros_like ( rendered_silhouette ) print ( f"Max image std: {rendered_image_std.max().item():.4f}; " f"max image: {rendered_image.max().item():.4f}; " f"max silhouette std: {rendered_silhouette_std.max().item():.4f}; " f"max silhouette: {rendered_silhouette.max().item():.4f}" ) fig , ax = plt . subplots ( 2 , 4 , figsize = ( 20 , 10 ) ) ax = ax . ravel ( ) clamp_and_detach = lambda x : x . clamp ( 0.0 , 1.0 ) . cpu ( ) . detach ( ) . numpy ( ) ax [ 0 ] . plot ( list ( range ( len ( loss_history_color ) ) ) , loss_history_color , linewidth = 1 ) ax [ 1 ] . imshow ( clamp_and_detach ( rendered_image ) ) ax [ 2 ] . imshow ( clamp_and_detach ( rendered_silhouette [ ... , 0 ] ) ) ax [ 3 ] . imshow ( clamp_and_detach ( rendered_image_std ) , cmap = "hot" , vmax = 0.75 ** 0.5 ) ax [ 4 ] . plot ( list ( range ( len ( loss_history_sil ) ) ) , loss_history_sil , linewidth = 1 ) ax [ 5 ] . imshow ( clamp_and_detach ( target_image ) ) ax [ 6 ] . imshow ( clamp_and_detach ( target_silhouette ) ) ax [ 7 ] . imshow ( clamp_and_detach ( rendered_silhouette_std ) , cmap = "hot" , vmax = 0.5 ) for ax_ , title_ in zip ( ax , ( "loss color" , "rendered image" , "rendered silhouette" , "image uncertainty" , "loss silhouette" , "target image" , "target silhouette" , "silhouette uncertainty" ) ) : if not title_ . startswith ( 'loss' ) : ax_ . grid ( "off" ) ax_ . axis ( "off" ) ax_ . set_title ( title_ ) fig . canvas . draw ( ) fig . show ( ) return fig
This is a helper function for visualizing the
intermediate results of the learning.
This is a helper function for visualizing the intermediate results of the learning .
"""
    This is a helper function for visualizing the
    intermediate results of the learning.

    Since the `NeuralRadianceField` suffers from
    a large memory footprint, which does not allow to
    render the full image grid in a single forward pass,
    we utilize the `NeuralRadianceField.batched_forward`
    function in combination with disabling the gradient caching.
    This chunks the set of emitted rays to batches and
    evaluates the implicit function on one-batch at a time
    to prevent GPU memory overflow.
    """ # Prevent gradient caching. # Render using the grid renderer and the # batched_forward function of neural_radiance_field. # Split the rendering result to a silhouette render # and the image render. # Generate plots.
neural_radiance_field
camera
target_image
target_silhouette
loss_history_color
loss_history_sil
renderer_grid
num_forward