slothfulxtx / diff-gaussian-rasterization

Differentiable gaussian rasterization with depth, alpha, normal map and extra per-Gaussian attributes, also support camera pose gradient
Other
153 stars 10 forks source link

Segmentaion fault #18

Closed ProKaroly closed 1 week ago

ProKaroly commented 1 month ago

Hi!

Is there anybody who also get segmentation fault error in rasterizer_impl.cu at line:

ImageState imgState = ImageState::fromChunk(img_chunkptr,width * height);

I get the segmentation fault error before the function "fromChunk" starts to process, because even the first line inside the function does not executed.

The original rasterizer implementation works fine, but I need to optimize the image pose, so I need pose gradients.

Can someone provide me an example usage of the code? (A render.py code would be the best :) )

Thank you, in advance!

slothfulxtx commented 3 weeks ago

Hi, can you provide more details about which branch you got the segment error on? If you're using the pose branch, here's the render code I implement for testing.


import torch
import math
from diff_gauss_pose import GaussianRasterizationSettings, GaussianRasterizer
from utils.sh import eval_sh

def pose_render(viewpoint_camera, gaussians, cfg, bg_color, viewmatrix, scaling_modifier = 1.0, override_color = None, extra_attrs=None, debug=False):
    """
    Render the scene. 

    Background tensor (bg_color) must be on GPU!
    """

    # Create zero tensor. We will use it to make pytorch return gradients of the 2D (screen-space) means
    screenspace_points = torch.zeros_like(gaussians.get_xyz, dtype=gaussians.get_xyz.dtype, requires_grad=True, device="cuda") + 0
    try:
        screenspace_points.retain_grad()
    except:
        pass

    # Set up rasterization configuration
    tanfovx = math.tan(viewpoint_camera.FoVx * 0.5)
    tanfovy = math.tan(viewpoint_camera.FoVy * 0.5)

    raster_settings = GaussianRasterizationSettings(
        image_height=int(viewpoint_camera.image_height),
        image_width=int(viewpoint_camera.image_width),
        tanfovx=tanfovx,
        tanfovy=tanfovy,
        bg=bg_color,
        scale_modifier=scaling_modifier,
        projmatrix=viewpoint_camera.projection_matrix,
        sh_degree=gaussians.active_sh_degree if hasattr(gaussians, 'active_sh_degree') else 0,
        prefiltered=False,
        enable_cov_grad=True,
        enable_sh_grad=False,
        debug=debug
    )

    rasterizer = GaussianRasterizer(raster_settings=raster_settings)

    means3D = gaussians.get_xyz
    means2D = screenspace_points
    opacity = gaussians.get_opacity

    # If precomputed 3d covariance is provided, use it. If not, then it will be computed from
    # scaling / rotation by the rasterizer.
    scales = None
    rotations = None
    cov3D_precomp = None
    if cfg.compute_cov3D_python:
        cov3D_precomp = gaussians.get_covariance(scaling_modifier)
    else:
        scales = gaussians.get_scaling
        rotations = gaussians.get_rotation

    # If precomputed colors are provided, use them. Otherwise, if it is desired to precompute colors
    # from SHs in Python, do it. If not, then SH -> RGB conversion will be done by rasterizer.
    shs = None
    colors_precomp = None
    if override_color is None:
        if cfg.convert_SHs_python:
            shs_view = gaussians.get_features.transpose(1, 2).view(-1, 3, (gaussians.max_sh_degree+1)**2)
            dir_pp = (gaussians.get_xyz - viewpoint_camera.camera_center.repeat(gaussians.get_features.shape[0], 1))
            dir_pp_normalized = dir_pp/dir_pp.norm(dim=1, keepdim=True)
            sh2rgb = eval_sh(gaussians.active_sh_degree, shs_view, dir_pp_normalized)
            colors_precomp = torch.clamp_min(sh2rgb + 0.5, 0.0)
        else:
            shs = gaussians.get_features
    else:
        colors_precomp = override_color

    # Rasterize visible Gaussians to image, obtain their radii (on screen). 
    rendered_image, rendered_depth, rendered_norm, rendered_alpha, radii, extra = rasterizer(
        means3D = means3D,
        means2D = means2D,
        shs = shs,
        colors_precomp = colors_precomp,
        opacities = opacity,
        scales = scales,
        rotations = rotations,
        cov3Ds_precomp = cov3D_precomp,
        extra_attrs=extra_attrs,
        viewmatrix=viewmatrix)

    # Those Gaussians that were frustum culled or had a radius of 0 were not visible.
    # They will be excluded from value updates used in the splitting criteria.
    return {"render": rendered_image,
            "depth": rendered_depth,
            "norm": rendered_norm,
            "alpha": rendered_alpha,
            "viewspace_points": screenspace_points,
            "visibility_filter" : radii > 0,
            "extra": extra,
            "radii": radii}