@@ -45,7 +45,7 @@ def sigmoid_alpha_blend(colors, fragments, blend_params) -> torch.Tensor:
45
45
"""
46
46
Silhouette blending to return an RGBA image
47
47
- **RGB** - choose color of the closest point.
48
- - **A** - blend based on the 2D distance based probability map [0 ].
48
+ - **A** - blend based on the 2D distance based probability map [1 ].
49
49
50
50
Args:
51
51
colors: (N, H, W, K, 3) RGB color for each of the top K faces per pixel.
@@ -60,7 +60,7 @@ def sigmoid_alpha_blend(colors, fragments, blend_params) -> torch.Tensor:
60
60
Returns:
61
61
RGBA pixel_colors: (N, H, W, 4)
62
62
63
- [0 ] Liu et al, 'Soft Rasterizer: A Differentiable Renderer for Image-based
63
+ [1 ] Liu et al, 'Soft Rasterizer: A Differentiable Renderer for Image-based
64
64
3D Reasoning', ICCV 2019
65
65
"""
66
66
N , H , W , K = fragments .pix_to_face .shape
@@ -73,20 +73,13 @@ def sigmoid_alpha_blend(colors, fragments, blend_params) -> torch.Tensor:
73
73
# the face. Therefore use -1.0 * fragments.dists to get the correct sign.
74
74
prob = torch .sigmoid (- fragments .dists / blend_params .sigma ) * mask
75
75
76
- # The cumulative product ensures that alpha will be 1 if at least 1 face
77
- # fully covers the pixel as for that face prob will be 1.0
78
- # TODO: investigate why torch.cumprod backwards is very slow for large
79
- # values of K.
80
- # Temporarily replace this with exp(sum(log))) using the fact that
81
- # a*b = exp(log(a*b)) = exp(log(a) + log(b))
82
- # alpha = 1.0 - torch.cumprod((1.0 - prob), dim=-1)[..., -1]
83
-
84
- alpha = 1.0 - torch .exp (torch .log ((1.0 - prob )).sum (dim = - 1 ))
85
-
76
+ # The cumulative product ensures that alpha will be 0.0 if at least 1
77
+ # face fully covers the pixel as for that face, prob will be 1.0.
78
+ # This results in a multiplication by 0.0 because of the (1.0 - prob)
79
+ # term. Therefore 1.0 - alpha will be 1.0.
80
+ alpha = torch .prod ((1.0 - prob ), dim = - 1 )
86
81
pixel_colors [..., :3 ] = colors [..., 0 , :] # Hard assign for RGB
87
- pixel_colors [..., 3 ] = alpha
88
-
89
- pixel_colors = torch .clamp (pixel_colors , min = 0 , max = 1.0 )
82
+ pixel_colors [..., 3 ] = 1.0 - alpha
90
83
return torch .flip (pixel_colors , [1 ])
91
84
92
85
@@ -95,7 +88,7 @@ def softmax_rgb_blend(
95
88
) -> torch .Tensor :
96
89
"""
97
90
RGB and alpha channel blending to return an RGBA image based on the method
98
- proposed in [0 ]
91
+ proposed in [1 ]
99
92
- **RGB** - blend the colors based on the 2D distance based probability map and
100
93
relative z distances.
101
94
- **A** - blend based on the 2D distance based probability map.
@@ -151,15 +144,11 @@ def softmax_rgb_blend(
151
144
# Sigmoid probability map based on the distance of the pixel to the face.
152
145
prob_map = torch .sigmoid (- fragments .dists / blend_params .sigma ) * mask
153
146
154
- # The cumulative product ensures that alpha will be 1 if at least 1 face
155
- # fully covers the pixel as for that face prob will be 1.0
156
- # TODO: investigate why torch.cumprod backwards is very slow for large
157
- # values of K.
158
- # Temporarily replace this with exp(sum(log))) using the fact that
159
- # a*b = exp(log(a*b)) = exp(log(a) + log(b))
160
- # alpha = 1.0 - torch.cumprod((1.0 - prob), dim=-1)[..., -1]
161
-
162
- alpha = 1.0 - torch .exp (torch .log ((1.0 - prob_map )).sum (dim = - 1 ))
147
+ # The cumulative product ensures that alpha will be 0.0 if at least 1
148
+ # face fully covers the pixel as for that face, prob will be 1.0.
149
+ # This results in a multiplication by 0.0 because of the (1.0 - prob)
150
+ # term. Therefore 1.0 - alpha will be 1.0.
151
+ alpha = torch .prod ((1.0 - prob_map ), dim = - 1 )
163
152
164
153
# Weights for each face. Adjust the exponential by the max z to prevent
165
154
# overflow. zbuf shape (N, H, W, K), find max over K.
@@ -178,8 +167,6 @@ def softmax_rgb_blend(
178
167
weighted_colors = (weights [..., None ] * colors ).sum (dim = - 2 )
179
168
weighted_background = (delta / denom ) * background
180
169
pix_colors [..., :3 ] = weighted_colors + weighted_background
181
- pix_colors [..., 3 ] = alpha
170
+ pix_colors [..., 3 ] = 1.0 - alpha
182
171
183
- # Clamp colors to the range 0-1 and flip y axis.
184
- pix_colors = torch .clamp (pix_colors , min = 0 , max = 1.0 )
185
172
return torch .flip (pix_colors , [1 ])
0 commit comments