Skip to content

Allow training and rasterizing on a full image background in addition to flat colors #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cuda_rasterizer/backward.cu
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ renderCUDA(
// the background color is added if nothing left to blend
float bg_dot_dpixel = 0;
for (int i = 0; i < C; i++)
bg_dot_dpixel += bg_color[i] * dL_dpixel[i];
bg_dot_dpixel += bg_color[i + pix_id * C] * dL_dpixel[i];
dL_dalpha += (-T_final / (1.f - alpha)) * bg_dot_dpixel;


Expand Down
2 changes: 1 addition & 1 deletion cuda_rasterizer/forward.cu
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ renderCUDA(
final_T[pix_id] = T;
n_contrib[pix_id] = last_contributor;
for (int ch = 0; ch < CHANNELS; ch++)
out_color[ch * H * W + pix_id] = C[ch] + T * bg_color[ch];
out_color[ch * H * W + pix_id] = C[ch] + T * bg_color[ch + pix_id * CHANNELS];
}
}

Expand Down
13 changes: 11 additions & 2 deletions diff_gaussian_rasterization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ def forward(
cov3Ds_precomp,
raster_settings,
):
# Flatten from CxHxW
bg = raster_settings.bg if len(raster_settings.bg.shape) == 1 else raster_settings.bg.permute(1,2,0).flatten()
pixel_count = raster_settings.image_height * raster_settings.image_width
bg = bg if len(bg) > 3 else bg.repeat(pixel_count)

# Restructure arguments the way that the C++ lib expects them
args = (
raster_settings.bg,
bg,
means3D,
colors_precomp,
opacities,
Expand Down Expand Up @@ -105,8 +109,13 @@ def backward(ctx, grad_out_color, _):
raster_settings = ctx.raster_settings
colors_precomp, means3D, scales, rotations, cov3Ds_precomp, radii, sh, geomBuffer, binningBuffer, imgBuffer = ctx.saved_tensors

# Flatten from CxHxW
bg = raster_settings.bg if len(raster_settings.bg.shape) == 1 else raster_settings.bg.permute(1,2,0).flatten()
pixel_count = raster_settings.image_height * raster_settings.image_width
bg = bg if len(bg) > 3 else bg.repeat(pixel_count)

# Restructure args as C++ method expects them
args = (raster_settings.bg,
args = (bg,
means3D,
radii,
colors_precomp,
Expand Down