Skip to content

Commit 0513122

Browse files
committed
Updates SSIM for 1D, 2D and 3D data, and MS-SSIM for 2D and 3D data.
1 parent 06e8b7d commit 0513122

File tree

7 files changed

+1024
-50
lines changed

7 files changed

+1024
-50
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$$
44
\begin{align}
5-
l(\mathbf{x}, \mathbf{y}) & = \frac{2\mu_x\mu_y+C_1}{\mu_x^2+\mu_y^2+C_1}, C_1=(K_1L)^2, K_1=0.01, \\
5+
l(\mathbf{x}, \mathbf{y}) & = \frac{2\mu_x\mu_y+C_1}{\mu_x^2+\mu_y^2+C_1}, C_1=(K_1L)^2, K_1=0.01, \\
66
c(\mathbf{x}, \mathbf{y}) & = \frac{2\sigma_{x}\sigma_{y}+C_2}{\sigma_x^2+\sigma_y^2+C_2}, C_2=(K_2L)^2, K_2=0.02, \\
77
s(\mathbf{x}, \mathbf{y}) & = \frac{\sigma_{xy}+C_3}{\sigma_x\sigma_y+C_3}, C_3=C_2/2, \\
88
\text{SSIM}(\mathbf{x}, \mathbf{y}) & = [l(\mathbf{x}, \mathbf{y})]^\alpha \cdot [c(\mathbf{x}, \mathbf{y})]^\beta \cdot [s(\mathbf{x}, \mathbf{y})]^\gamma \\
@@ -23,9 +23,25 @@ Compared to this widely used implementation: <https://github.com/Po-Hsun-Su/pyto
2323

2424
At the same time, in this implementation, I have dealt with the problem that the calculation with the fp16 mode cannot be consistent with the calculation with the fp32 mode. Typecasting is used here to ensure that the computation is done in fp32 mode. This might also avoid unexpected results when using it as a loss.
2525

26+
> [!note]
27+
> 2024-12-04: SSIM for 1D, 2D and 3D data, and MS-SSIM calculation for 2D and 3D data are now supported simultaneously.
28+
29+
| Setting | SSIM1d | SSIM2d | SSIM3d | MS-SSIM2d | MS-SSIM3d (**only pooling in the spatial domain**) |
30+
| --------------- | -------------- | --------------------- | ---------------------------- | --------------------- | -------------------------------------------------- |
31+
| data_dim | 1 | 2 (Default) | 3 | 2 | 3 |
32+
| return_msssim | `False` | `False` | `False` | `True` | `True` |
33+
| window_size | int, [int] | int, [int, int] | int, [int, int, int] | int, [int, int] | int, [int, int, int] |
34+
| padding | int, [int] | int, [int, int] | int, [int, int, int] | int, [int, int] | int, [int, int, int] |
35+
| sigma | float, [float] | float, [float, float] | float, [float, float, float] | float, [float, float] | float, [float, float, float] |
36+
| in_channels | int | int | int | int | int |
37+
| L | 1, 255 | 1, 255 | 1, 255 | 1, 255 | 1, 255 |
38+
| keep_batch_dim ||||||
39+
| return_log ||||||
40+
| ensemble_kernel ||||||
41+
2642
## Structural similarity index
2743

28-
> When comparing images, the mean squared error (MSE)–while simple to implement–is not highly indicative of perceived similarity. Structural similarity aims to address this shortcoming by taking texture into account. More details can be seen at https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html?highlight=structure+similarity
44+
> When comparing images, the mean squared error (MSE)–while simple to implement–is not highly indicative of perceived similarity. Structural similarity aims to address this shortcoming by taking texture into account. More details can be seen at <https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html?highlight=structure+similarity>
2945
3046
![results](https://user-images.githubusercontent.com/26847524/175031400-92426661-4536-43c7-8f6e-5c470fb9ccb5.png)
3147

@@ -286,10 +302,10 @@ plt.savefig("prediction.png")
286302

287303
## Reference
288304

289-
- https://github.com/Po-Hsun-Su/pytorch-ssim
290-
- https://github.com/VainF/pytorch-msssim
291-
- https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html?highlight=structure+similarity
292-
- Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli, “Image quality assessment: From error visibility to structural similarity,” IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
305+
* <https://github.com/Po-Hsun-Su/pytorch-ssim>
306+
* <https://github.com/VainF/pytorch-msssim>
307+
* <https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html?highlight=structure+similarity>
308+
* Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli, “Image quality assessment: From error visibility to structural similarity,” IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
293309

294310
## Cite
295311

pyproject.toml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[tool.ruff]
2+
# Exclude a variety of commonly ignored directories.
3+
exclude = [
4+
".bzr",
5+
".direnv",
6+
".eggs",
7+
".git",
8+
".git-rewrite",
9+
".hg",
10+
".ipynb_checkpoints",
11+
".mypy_cache",
12+
".nox",
13+
".pants.d",
14+
".pyenv",
15+
".pytest_cache",
16+
".pytype",
17+
".ruff_cache",
18+
".svn",
19+
".tox",
20+
".venv",
21+
".vscode",
22+
"__pypackages__",
23+
"_build",
24+
"buck-out",
25+
"build",
26+
"dist",
27+
"node_modules",
28+
"site-packages",
29+
"venv",
30+
]
31+
32+
# Same as Black.
33+
line-length = 118
34+
indent-width = 4
35+
36+
[tool.ruff.lint]
37+
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
38+
select = ["E4", "E7", "E9", "F"]
39+
ignore = []
40+
41+
# Allow fix for all enabled rules (when `--fix`) is provided.
42+
fixable = ["ALL"]
43+
unfixable = []
44+
45+
# Allow unused variables when underscore-prefixed.
46+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
47+
48+
[tool.ruff.format]
49+
# Like Black, use double quotes for strings.
50+
quote-style = "double"
51+
52+
# Like Black, indent with spaces, rather than tabs.
53+
indent-style = "space"
54+
55+
# Like Black, respect magic trailing commas.
56+
skip-magic-trailing-comma = false
57+
58+
# Like Black, automatically detect the appropriate line ending.
59+
line-ending = "auto"

0 commit comments

Comments
 (0)