Skip to content

Commit

Permalink
⚡️ Improve GMSD run-time
Browse files Browse the repository at this point in the history
  • Loading branch information
francois-rozet committed Jan 10, 2021
1 parent 5768a33 commit ba07335
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion piqa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
specific image quality assessement metric.
"""

__version__ = '1.0.8'
__version__ = '1.0.9'
9 changes: 6 additions & 3 deletions piqa/gmsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ def _gmsd(

# Gradient magnitude similarity
gms_num = (2. - alpha) * gm_xy + c
gms_den = gm_x ** 2 + gm_y ** 2 - alpha * gm_xy + c
gms_den = gm_x ** 2 + gm_y ** 2 + c

if alpha > 0.:
gms_den = gms_den - alpha * gm_xy

gms = gms_num / gms_den

# Gradient magnitude similarity deviation
gmsd = (gms - gms.mean((-1, -2), keepdim=True)) ** 2
gmsd = torch.sqrt(gmsd.mean((-1, -2)))
gmsd = torch.std(gms, dim=(-1, -2))

return gmsd

Expand Down
12 changes: 6 additions & 6 deletions piqa/ssim.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ def ssim_per_channel(
mu_x = filter2d(x, window)
mu_y = filter2d(y, window)

mu_x_sq = mu_x ** 2
mu_y_sq = mu_y ** 2
mu_xx = mu_x ** 2
mu_yy = mu_y ** 2
mu_xy = mu_x * mu_y

# Variance (sigma)
sigma_x_sq = filter2d(x ** 2, window) - mu_x_sq
sigma_y_sq = filter2d(y ** 2, window) - mu_y_sq
sigma_xx = filter2d(x ** 2, window) - mu_xx
sigma_yy = filter2d(y ** 2, window) - mu_yy
sigma_xy = filter2d(x * y, window) - mu_xy

# Contrast sensitivity
cs = (2. * sigma_xy + c2) / (sigma_x_sq + sigma_y_sq + c2)
cs = (2. * sigma_xy + c2) / (sigma_xx + sigma_yy + c2)

# Structural similarity
ss = (2. * mu_x * mu_y + c1) / (mu_x_sq + mu_y_sq + c1) * cs
ss = (2. * mu_xy + c1) / (mu_xx + mu_yy + c1) * cs

return ss.mean((-1, -2)), cs.mean((-1, -2))

Expand Down

0 comments on commit ba07335

Please sign in to comment.