{"id":15029307,"url":"https://github.com/vainf/pytorch-msssim","last_synced_at":"2025-05-14T19:07:10.450Z","repository":{"id":39861951,"uuid":"182106341","full_name":"VainF/pytorch-msssim","owner":"VainF","description":"Fast and differentiable MS-SSIM and SSIM for pytorch.","archived":false,"fork":false,"pushed_at":"2024-03-12T11:37:10.000Z","size":12260,"stargazers_count":1156,"open_issues_count":28,"forks_count":126,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-13T13:57:51.806Z","etag":null,"topics":["image-quality","ms-ssim","pytorch","ssim","structure-similarity"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VainF.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-18T14:49:02.000Z","updated_at":"2025-04-11T01:20:41.000Z","dependencies_parsed_at":"2023-02-19T16:35:45.271Z","dependency_job_id":"6a2a8c18-db67-44c5-b913-6cafeee074be","html_url":"https://github.com/VainF/pytorch-msssim","commit_stats":{"total_commits":131,"total_committers":10,"mean_commits":13.1,"dds":0.3893129770992366,"last_synced_commit":"d23a69e869df30fefde0773869be90745a078711"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VainF%2Fpytorch-msssim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VainF%2Fpytorch-msssim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VainF%2Fpytorch-msssim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VainF%2Fpytorch-msssim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VainF","download_url":"https://codeload.github.com/VainF/pytorch-msssim/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254209859,"owners_count":22032897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["image-quality","ms-ssim","pytorch","ssim","structure-similarity"],"created_at":"2024-09-24T20:10:16.185Z","updated_at":"2025-05-14T19:07:08.795Z","avatar_url":"https://github.com/VainF.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyTorch MS-SSIM\n\n\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n[![Downloads](https://static.pepy.tech/badge/pytorch_msssim)](https://pepy.tech/project/pytorch_msssim)\n[![Latest Version](https://img.shields.io/badge/Latest%20Version-1.0.0-blue.svg)](https://github.com/VainF/pytorch_msssim/releases/latest)\n\nFast and differentiable MS-SSIM and SSIM for pytorch.\n\n\u003cdiv\u003e\n\u003cimg src=\"https://github.com/VainF/Images/blob/master/pytorch_msssim/lcs.png\" width=\"25%\"\u003e\n\n#### Structural Similarity (SSIM):   \n\u003cimg src=\"https://github.com/VainF/Images/blob/master/pytorch_msssim/ssim.png\" width=\"50%\"\u003e\n\n#### Multi-Scale Structural Similarity (MS-SSIM):  \n\u003cimg src=\"https://github.com/VainF/Images/blob/master/pytorch_msssim/ms-ssim.png\" width=\"55%\"\u003e\n\u003c/div\u003e\n\n#### Why it is faster than other versions?\n\nGaussian kernels used in SSIM \u0026 MS-SSIM are seperable. A [separable filter](https://en.wikipedia.org/wiki/Separable_filter) in image processing can be written as product of two more simple filters. Typically a 2-dimensional convolution operation is separated into two 1-dimensional filters. This reduces the computational costs on an $N\\times M$ image with a $m\\times n$ filter from $\\mathcal{O}(M\\cdot N \\cdot m \\cdot n)$ down to $\\mathcal{O}(M\\cdot N \\cdot (m+n))$. More importantly, seperated kernels are more contiguous and thus cache-friendly than 2-D kernels, which effectively accelerates the computing of SSIM/MS-SSIM. \n\n# Update\n\n#### _2023.05.26_ (v1.0.0)\n\nJust a release. Type hints by [@iyume](https://github.com/iyume)\n\n#### _2020.08.21_ (v0.2.1)\n\n3D image support from [@FynnBe](https://github.com/FynnBe)!  \n\n#### _2020.04.30_ (v0.2)\n\nNow (v0.2), **ssim \u0026 ms-ssim can produce consistent results as tensorflow and skimage**. A benchmark (pytorch-msssim, tensorflow and skimage) can be found in the Tests section.\n\n# Installation\n\n```bash\npip install pytorch-msssim\n```\n\n# Usage\n\n### 1. Basic Usage \n\n```python\nfrom pytorch_msssim import ssim, ms_ssim, SSIM, MS_SSIM\n# X: (N,3,H,W) a batch of non-negative RGB images (0~255)\n# Y: (N,3,H,W)  \n\n# calculate ssim \u0026 ms-ssim for each image\nssim_val = ssim( X, Y, data_range=255, size_average=False) # return (N,)\nms_ssim_val = ms_ssim( X, Y, data_range=255, size_average=False ) #(N,)\n\n# set 'size_average=True' to get a scalar value as loss. see tests/tests_loss.py for more details\nssim_loss = 1 - ssim( X, Y, data_range=255, size_average=True) # return a scalar\nms_ssim_loss = 1 - ms_ssim( X, Y, data_range=255, size_average=True )\n\n# reuse the gaussian kernel with SSIM \u0026 MS_SSIM. \nssim_module = SSIM(data_range=255, size_average=True, channel=3) # channel=1 for grayscale images\nms_ssim_module = MS_SSIM(data_range=255, size_average=True, channel=3)\n\nssim_loss = 1 - ssim_module(X, Y)\nms_ssim_loss = 1 - ms_ssim_module(X, Y)\n```\n### 2. Normalized input\nIf you need to calculate MS-SSIM/SSIM on normalized images, please denormalize them to the range of [0, 1] or [0, 255] first.\n\n```python\n# X: (N,3,H,W) a batch of normalized images (-1 ~ 1)\n# Y: (N,3,H,W)  \nX = (X + 1) / 2  # [-1, 1] =\u003e [0, 1]\nY = (Y + 1) / 2  \nms_ssim_val = ms_ssim( X, Y, data_range=1, size_average=False ) #(N,)\n```\n\n### 3. Enable nonnegative_ssim\n\nFor ssim, it is recommended to set `nonnegative_ssim=True` to avoid negative results. However, this option is set to `False` by default to keep it consistent with tensorflow and skimage.\n\nFor ms-ssim, there is no nonnegative_ssim option and the ssim reponses is forced to be non-negative to avoid NaN results.\n\n\n# Tests and Examples\n\n```bash\ncd tests\n```\n### 1. Benchmark\n\n```bash\n# requires tf2\npython tests_comparisons_tf_skimage.py \n\n# or skimage only\n# python tests_comparisons_skimage.py \n```\n\nOutputs:\n\n```\nDownloading test image...\n===================================\n             Test SSIM\n===================================\n====\u003e Single Image\nRepeat 100 times\nsigma=0.0 ssim_skimage=1.000000 (147.2605 ms), ssim_tf=1.000000 (343.4146 ms), ssim_torch=1.000000 (92.9151 ms)\nsigma=10.0 ssim_skimage=0.932423 (147.5198 ms), ssim_tf=0.932661 (343.5191 ms), ssim_torch=0.932421 (95.6283 ms)\nsigma=20.0 ssim_skimage=0.785744 (152.6441 ms), ssim_tf=0.785733 (343.4085 ms), ssim_torch=0.785738 (87.5639 ms)\nsigma=30.0 ssim_skimage=0.636902 (145.5763 ms), ssim_tf=0.636902 (343.5312 ms), ssim_torch=0.636895 (90.4084 ms)\nsigma=40.0 ssim_skimage=0.515798 (147.3798 ms), ssim_tf=0.515801 (344.8978 ms), ssim_torch=0.515791 (96.4440 ms)\nsigma=50.0 ssim_skimage=0.422011 (148.2900 ms), ssim_tf=0.422007 (345.4076 ms), ssim_torch=0.422005 (86.3799 ms)\nsigma=60.0 ssim_skimage=0.351139 (146.2039 ms), ssim_tf=0.351139 (343.4428 ms), ssim_torch=0.351133 (93.3445 ms)\nsigma=70.0 ssim_skimage=0.296336 (145.5341 ms), ssim_tf=0.296337 (345.2255 ms), ssim_torch=0.296331 (92.6771 ms)\nsigma=80.0 ssim_skimage=0.253328 (147.6655 ms), ssim_tf=0.253328 (343.1386 ms), ssim_torch=0.253324 (82.5985 ms)\nsigma=90.0 ssim_skimage=0.219404 (142.6025 ms), ssim_tf=0.219405 (345.8275 ms), ssim_torch=0.219400 (100.9946 ms)\nsigma=100.0 ssim_skimage=0.192681 (144.5597 ms), ssim_tf=0.192682 (346.5489 ms), ssim_torch=0.192678 (85.0229 ms)\nPass!\n====\u003e Batch\nPass!\n\n\n===================================\n             Test MS-SSIM\n===================================\n====\u003e Single Image\nRepeat 100 times\nsigma=0.0 msssim_tf=1.000000 (671.5363 ms), msssim_torch=1.000000 (125.1403 ms)\nsigma=10.0 msssim_tf=0.991137 (669.0296 ms), msssim_torch=0.991086 (113.4078 ms)\nsigma=20.0 msssim_tf=0.967292 (670.5530 ms), msssim_torch=0.967281 (107.6428 ms)\nsigma=30.0 msssim_tf=0.934875 (668.7717 ms), msssim_torch=0.934875 (111.3334 ms)\nsigma=40.0 msssim_tf=0.897660 (669.0801 ms), msssim_torch=0.897658 (107.3700 ms)\nsigma=50.0 msssim_tf=0.858956 (671.4629 ms), msssim_torch=0.858954 (100.9959 ms)\nsigma=60.0 msssim_tf=0.820477 (670.5424 ms), msssim_torch=0.820475 (103.4489 ms)\nsigma=70.0 msssim_tf=0.783511 (671.9357 ms), msssim_torch=0.783507 (113.9048 ms)\nsigma=80.0 msssim_tf=0.749522 (672.3925 ms), msssim_torch=0.749518 (120.3891 ms)\nsigma=90.0 msssim_tf=0.716221 (672.9066 ms), msssim_torch=0.716217 (118.3788 ms)\nsigma=100.0 msssim_tf=0.684958 (675.2075 ms), msssim_torch=0.684953 (117.9481 ms)\nPass\n====\u003e Batch\nPass\n```\n\n\u003cdiv\u003e\n\u003cimg src=\"https://github.com/VainF/Images/blob/master/pytorch_msssim/simga_0_ssim_1.0000.png\"   width=\"20%\"\u003e\n\u003cfigcaption\u003essim=1.0000\u003c/figcaption\u003e\n\u003cimg src=\"https://github.com/VainF/Images/blob/master/pytorch_msssim/simga_50_ssim_0.4225.png\"  width=\"20%\"\u003e\n\u003cfigcaption\u003essim=0.4225\u003c/figcaption\u003e\n\u003cimg src=\"https://github.com/VainF/Images/blob/master/pytorch_msssim/simga_100_ssim_0.1924.png\"    width=\"20%\"\u003e\n\u003cfigcaption\u003essim=0.1924\u003c/figcaption\u003e\n\u003c/div\u003e\n\n### 2. MS_SSIM as loss function\n\nSee ['tests/tests_loss.py'](https://github.com/VainF/pytorch-msssim/tree/master/tests/tests_loss.py) for more details about how to use ssim or ms_ssim as loss functions\n\n### 3. AutoEncoder\n\nSee ['tests/ae_example'](https://github.com/VainF/pytorch-msssim/tree/master/tests/ae_example)\n\n![results](https://github.com/VainF/Images/blob/master/pytorch_msssim/ae_ms_ssim.jpg)\n*left: the original image, right: the reconstructed image*\n\n# References\n\n[https://github.com/jorge-pessoa/pytorch-msssim](https://github.com/jorge-pessoa/pytorch-msssim)  \n[https://ece.uwaterloo.ca/~z70wang/research/ssim/](https://ece.uwaterloo.ca/~z70wang/research/ssim/)  \n[https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf](https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf)  \n[Matlab Code](https://ece.uwaterloo.ca/~z70wang/research/iwssim/)   \n[ssim \u0026 ms-ssim from tensorflow](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/ops/image_ops_impl.py#L3314-L3438) \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvainf%2Fpytorch-msssim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvainf%2Fpytorch-msssim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvainf%2Fpytorch-msssim/lists"}