Skip to content
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

Update forward & backward for rendered alpha image #70

Merged
merged 9 commits into from
Jan 16, 2024

Conversation

Zhuoyang-Pan
Copy link
Collaborator

I added the forward and backward for rendered alpha image. This could be helpful if we want to train on datasets with masks/RGBA images.

Specifically, for each call of RasterizeGaussians.apply or NDRasterizeGaussians.apply now we would have two outputs out_img (H * W * 3) and out_alpha(H * W). For $\alpha$ gradient for each gaussian, I calculate it as

$$ \begin{aligned} \frac{\partial{\mathcal{L}}}{ \partial{\alpha_n}} &= \sum_{k=1}^{K} \frac{\partial{\mathcal{L}}}{ \partial{C_i(k)}} \frac{\partial{C_i(k)}}{\partial{\alpha_n}} + \frac{\partial{\mathcal{L}}}{ \partial{A_i}} \frac{\partial{A_i}}{\partial{\alpha_n}} \\ &= \sum_{k=1}^{K} \frac{\partial{\mathcal{L}}}{ \partial{C_i(k)}} (c_n(k) \cdot T_n - \frac{\sum_{m > n} c_m \alpha_m T_m}{1-\alpha_n}) + \frac{\partial{\mathcal{L}}}{ \partial{A_i}} (T_n - \frac{\sum_{m > n} \alpha_m T_m}{1-\alpha_n}) \\ &= \sum_{k=1}^{K} \frac{\partial{\mathcal{L}}}{ \partial{C_i(k)}} (c_n(k) \cdot T_n - \frac{\sum_{m > n} c_m \alpha_m T_m}{1-\alpha_n}) + \frac{\partial{\mathcal{L}}}{ \partial{A_i}} \frac{T_{final}}{1-\alpha_n} \\ \end{aligned} $$

where $C_i$, $A_i$ are output colors, alpha respectively, $K$ is the number of channels of colors. The documents are also updated for reference of function calls.

I've also tested on following image where the left part has alpha=0.5 and the right part has alpha=1.
NDtraining

background.contiguous().cuda(),
final_Ts.contiguous().cuda(),
final_idx.contiguous().cuda(),
v_out_img.contiguous().cuda(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ruilong removed the cuda() for these, could you take them out too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed but should .cuda()s still be in NDRasterize backwards?

@nikmo33 nikmo33 mentioned this pull request Jan 5, 2024
Copy link
Collaborator

@kerrj kerrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review! this looks great, can we add an optional flag to the rasterize forward function called return_alpha that defaults to False for backward compatibility though? After that change it looks good to merge

@maturk
Copy link
Collaborator

maturk commented Jan 12, 2024

@Zhuoyang-Pan any ideas how this could be used with an input dataset that does contain masks to automatically segment the masked regions out?

@Zhuoyang-Pan
Copy link
Collaborator Author

@Zhuoyang-Pan any ideas how this could be used with an input dataset that does contain masks to automatically segment the masked regions out?

I am thinking of adding a L1 mask loss if the input dataset contains mask, but let me finish this PR first :)

@Zhuoyang-Pan Zhuoyang-Pan force-pushed the zhuoyang/alpha-backward branch from ec8b484 to c9e4b98 Compare January 14, 2024 21:37
Copy link
Collaborator

@kerrj kerrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thanks!

@Zhuoyang-Pan Zhuoyang-Pan merged commit 0c305ab into main Jan 16, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants