MCPcopy Create free account
hub / github.com/aigc3d/LHM / SSIM

Function SSIM

engine/BiRefNet/loss.py:223–241  ·  view source on GitHub ↗
(x, y)

Source from the content-addressed store, hash-verified

221
222
223def SSIM(x, y):
224 C1 = 0.01 ** 2
225 C2 = 0.03 ** 2
226
227 mu_x = nn.AvgPool2d(3, 1, 1)(x)
228 mu_y = nn.AvgPool2d(3, 1, 1)(y)
229 mu_x_mu_y = mu_x * mu_y
230 mu_x_sq = mu_x.pow(2)
231 mu_y_sq = mu_y.pow(2)
232
233 sigma_x = nn.AvgPool2d(3, 1, 1)(x * x) - mu_x_sq
234 sigma_y = nn.AvgPool2d(3, 1, 1)(y * y) - mu_y_sq
235 sigma_xy = nn.AvgPool2d(3, 1, 1)(x * y) - mu_x_mu_y
236
237 SSIM_n = (2 * mu_x_mu_y + C1) * (2 * sigma_xy + C2)
238 SSIM_d = (mu_x_sq + mu_y_sq + C1) * (sigma_x + sigma_y + C2)
239 SSIM = SSIM_n / SSIM_d
240
241 return torch.clamp((1 - SSIM) / 2, 0, 1)
242
243
244def saliency_structure_consistency(x, y):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected