frame-analytics 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. frame_analytics-0.2.0/LICENSE +202 -0
  2. frame_analytics-0.2.0/MANIFEST.in +8 -0
  3. frame_analytics-0.2.0/PKG-INFO +420 -0
  4. frame_analytics-0.2.0/README.md +384 -0
  5. frame_analytics-0.2.0/bench/__init__.py +0 -0
  6. frame_analytics-0.2.0/bench/baselines.py +81 -0
  7. frame_analytics-0.2.0/bench/bench.py +381 -0
  8. frame_analytics-0.2.0/bench/bench_fused_ssim.py +162 -0
  9. frame_analytics-0.2.0/bench/bench_memory.py +542 -0
  10. frame_analytics-0.2.0/bench/bench_training.py +327 -0
  11. frame_analytics-0.2.0/frame_analytics/__init__.py +80 -0
  12. frame_analytics-0.2.0/frame_analytics/_cabi.py +530 -0
  13. frame_analytics-0.2.0/frame_analytics/backend.py +666 -0
  14. frame_analytics-0.2.0/frame_analytics/csrc/fa_abi.h +206 -0
  15. frame_analytics-0.2.0/frame_analytics/csrc/fa_cpu.cpp +943 -0
  16. frame_analytics-0.2.0/frame_analytics/csrc/fa_cuda.cu +1459 -0
  17. frame_analytics-0.2.0/frame_analytics/functional.py +1207 -0
  18. frame_analytics-0.2.0/frame_analytics/modules.py +370 -0
  19. frame_analytics-0.2.0/frame_analytics/reference.py +318 -0
  20. frame_analytics-0.2.0/frame_analytics.egg-info/PKG-INFO +420 -0
  21. frame_analytics-0.2.0/frame_analytics.egg-info/SOURCES.txt +28 -0
  22. frame_analytics-0.2.0/frame_analytics.egg-info/dependency_links.txt +1 -0
  23. frame_analytics-0.2.0/frame_analytics.egg-info/requires.txt +14 -0
  24. frame_analytics-0.2.0/frame_analytics.egg-info/top_level.txt +1 -0
  25. frame_analytics-0.2.0/pyproject.toml +59 -0
  26. frame_analytics-0.2.0/setup.cfg +4 -0
  27. frame_analytics-0.2.0/setup.py +161 -0
  28. frame_analytics-0.2.0/tests/test_metrics.py +241 -0
  29. frame_analytics-0.2.0/tests/test_training_metrics.py +538 -0
  30. frame_analytics-0.2.0/tests/validate.py +210 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Nilas
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ include LICENSE README.md
2
+ recursive-include frame_analytics/csrc *.cu *.cpp *.h
3
+ recursive-include tests *.py
4
+ recursive-include bench *.py
5
+ # sdists carry sources only -- the binaries are built per platform in CI and
6
+ # only ever appear in wheels
7
+ prune frame_analytics/lib
8
+ global-exclude __pycache__/* *.py[cod] *.so *.pyd *.dll *.dylib
@@ -0,0 +1,420 @@
1
+ Metadata-Version: 2.4
2
+ Name: frame-analytics
3
+ Version: 0.2.0
4
+ Summary: Fast MSE, PSNR and SSIM for PyTorch (CPU + CUDA) at float64-reference accuracy
5
+ Author: Nilas
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/NevermindNilas/frame_analytics
8
+ Project-URL: Source, https://github.com/NevermindNilas/frame_analytics
9
+ Project-URL: Issues, https://github.com/NevermindNilas/frame_analytics/issues
10
+ Keywords: ssim,psnr,mse,image-quality,video-quality,metrics,pytorch,cuda,perceptual-loss
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: C++
17
+ Classifier: Environment :: GPU :: NVIDIA CUDA
18
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
19
+ Classifier: Topic :: Multimedia :: Video
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: torch>=2.0
24
+ Requires-Dist: numpy>=1.20
25
+ Provides-Extra: bench
26
+ Requires-Dist: scikit-image; extra == "bench"
27
+ Requires-Dist: opencv-python; extra == "bench"
28
+ Requires-Dist: pytorch-msssim; extra == "bench"
29
+ Requires-Dist: piq; extra == "bench"
30
+ Requires-Dist: torchmetrics; extra == "bench"
31
+ Requires-Dist: kornia; extra == "bench"
32
+ Provides-Extra: test
33
+ Requires-Dist: pytest; extra == "test"
34
+ Requires-Dist: scikit-image; extra == "test"
35
+ Dynamic: license-file
36
+
37
+ # frame_analytics
38
+
39
+ Image and video quality metrics for PyTorch — CPU and CUDA — at float64-reference
40
+ accuracy. Fused kernels for forward and backward, so the same code works as an
41
+ evaluation metric and as a training loss.
42
+
43
+ ```bash
44
+ pip install frame-analytics
45
+ ```
46
+
47
+ ```python
48
+ import torch, frame_analytics as fa
49
+
50
+ a = torch.randint(0, 256, (8, 3, 1080, 1920), dtype=torch.uint8, device="cuda")
51
+ b = torch.randint(0, 256, (8, 3, 1080, 1920), dtype=torch.uint8, device="cuda")
52
+
53
+ fa.mse(a, b)
54
+ fa.psnr(a, b, data_range=255.0)
55
+ fa.ssim(a, b, data_range=255.0) # Wang et al. 2004, exactly
56
+ fa.ms_ssim(a, b, data_range=255.0) # Wang et al. 2003
57
+ fa.gmsd(a, b, data_range=255.0) # Xue et al. 2014
58
+ fa.l1(a, b); fa.charbonnier(a, b); fa.huber(a, b)
59
+
60
+ fa.ssim(a, b, reduction="none") # per-image, (8,)
61
+ fa.ssim(a, b, return_map=True) # (8, 3, 1070, 1910)
62
+
63
+ # the convention the super-resolution literature reports
64
+ fa.psnr(a, b, data_range=255.0, luma="matlab", crop_border=4)
65
+ ```
66
+
67
+ Accepts `(H,W)` / `(C,H,W)` / `(N,C,H,W)`, uint8 through float64. uint8 stays
68
+ uint8 into the kernel — 2 bytes/pixel of bandwidth instead of 8.
69
+
70
+ ## Speed
71
+
72
+ Speedup over each library, across 512²→4K and batch 1→8, RTX 3090 / 16-thread CPU:
73
+
74
+ | | SSIM | MS-SSIM | GMSD | PSNR / MSE |
75
+ |---|---|---|---|---|
76
+ | pytorch-msssim | 16–22× | 7.5–14× | — | — |
77
+ | kornia | 18–32× | — | — | 2.0–8.5× |
78
+ | torchmetrics | 24–28× | — | — | 4.9–14× |
79
+ | piq | 25–30× | 6.7–20× | 40–50× | 12–37× |
80
+ | scikit-image (CPU) | 21–29× | — | — | 170–920× |
81
+ | OpenCV recipe (CPU) | 6.0–7.6× | — | — | 0.7–7.3× (`cv2.PSNR`) |
82
+ | [fused-ssim](https://github.com/rahul-goel/fused-ssim) (CUDA) | 1.2–1.9× | — | — | — |
83
+ | torch built-ins (L1 / Huber) | — | — | — | 2.4–100× |
84
+
85
+ Selected absolute numbers, RTX 3090, ms/call (lower is better):
86
+
87
+ | CUDA, uint8 in | 512² | 1080p | 1080p ×8 | 4K ×8 |
88
+ |---|---:|---:|---:|---:|
89
+ | SSIM | 0.024 | 0.085 | 0.651 | 2.642 |
90
+ | PSNR | 0.020 | 0.028 | 0.045 | 0.151 |
91
+ | MS-SSIM | 0.331 | 0.512 | 3.088 | — |
92
+ | GMSD | 0.023 | 0.055 | 0.313 | — |
93
+ | L1 | 0.021 | 0.026 | 0.115 | — |
94
+
95
+ SSIM sustains 25.6 Gpixel/s (~11 700 fps at 1080p). PSNR at 4K ×8 hits 880 GB/s
96
+ — 94% of the card's theoretical bandwidth, the ceiling for anything that must
97
+ read both frames.
98
+
99
+ | CPU, uint8 in, ms/call | 512² | 1080p | 1080p ×8 | 4K ×8 |
100
+ |---|---:|---:|---:|---:|
101
+ | **PSNR**, 1 channel | **0.008** | **0.015** | **0.102** | **1.41** |
102
+ | `cv2.PSNR`, per frame | 0.006 | 0.049 | 0.740 | 4.24 |
103
+ | kornia | 0.046 | 0.114 | 3.29 | 14.4 |
104
+ | torchmetrics | 0.081 | 0.195 | 5.52 | 22.7 |
105
+ | scikit-image | 1.43 | 11.7 | 93.5 | 381 |
106
+ | | | | | |
107
+ | **L1**, RGB | **0.011** | **0.039** | **1.03** | — |
108
+ | `F.l1_loss` | 0.193 | 3.05 | 30.1 | — |
109
+
110
+ Streaming 1080p RGB via CUDA graphs, host uint8 in, python float out: **931 fps**
111
+ (2 941 fps for resident tensors).
112
+
113
+ Full per-size CPU and CUDA tables: `python bench/bench.py`.
114
+
115
+ ### Where it doesn't win
116
+
117
+ - `cv2.PSNR` on one sub-megapixel single-channel frame: 0.006 ms against our
118
+ 0.008. Not the kernel — that runs the same 262 144 pixels in 3.8 µs, less than
119
+ `cv2.PSNR` takes for the whole call — but the ~4 µs of Python in front of it,
120
+ which is a fixed cost and so only visible when there is nothing else to pay
121
+ for. One frame bigger, or one channel wider, and it inverts: 3.3× at 1080p,
122
+ 7.3× at 1080p ×8.
123
+ - **Without the compiled extension** the portable PyTorch fallback is only ~1.1×
124
+ faster than `pytorch-msssim` and uses *more* memory. The speed claims are
125
+ claims about the kernels; the fallback exists to be correct, not to win.
126
+
127
+ ## Memory
128
+
129
+ Peak allocation *above the two input frames*, RTX 3090, 3-channel, MiB per
130
+ call. Nothing here is the frames themselves — those you already have:
131
+
132
+ | CUDA, forward | 1080p | 1080p ×8 | 4K ×8 |
133
+ |---|---:|---:|---:|
134
+ | **SSIM** | **0.02** | **0.19** | **0.75** |
135
+ | pytorch-msssim | 288 | 2253 | 9048 |
136
+ | piq | 336 | 2633 | 10568 |
137
+ | kornia | 360 | 2850 | 11397 |
138
+ | torchmetrics | 550 | 4388 | 17509 |
139
+ | | | | |
140
+ | **MS-SSIM** | **15.5** | **120** | **476** |
141
+ | pytorch-msssim | 288 | 2253 | 9048 |
142
+ | piq | 336 | 2633 | 10568 |
143
+ | | | | |
144
+ | **GMSD** | **0.02** | **0.19** | **0.75** |
145
+ | piq | 72 | 760 | 3040 |
146
+ | | | | |
147
+ | **PSNR / MSE / L1 / Charbonnier** | **0.007** | **0.007** | **0.007** |
148
+ | torchmetrics, kornia, `F.l1_loss` | 48 | 380 | 1520 |
149
+
150
+ A fused kernel has nowhere to put an intermediate. What is left for SSIM and
151
+ GMSD is the per-block partial sums — sized by the tile grid, so 4K ×8 costs
152
+ 0.75 MiB where the alternatives cost 9–17 GiB — and for the pixel metrics, the
153
+ output scalar. The libraries above are not doing anything wrong; five or six
154
+ frame-shaped temporaries is what the textbook formulation asks for, and 2253
155
+ MiB at 1080p ×8 is almost exactly 6× the 380 MiB input.
156
+
157
+ MS-SSIM is the one metric that must materialise something: the four
158
+ downsampled copies of both frames. That geometric series sums to a third of a
159
+ frame each, and nothing full-resolution survives — 120 MiB at 1080p ×8, where
160
+ the frame pair itself is 380.
161
+
162
+ The CPU story is the same one. There is no `max_memory_allocated` for host
163
+ memory, so these are peak process footprints, each measured in its own
164
+ interpreter — how much more memory the OS had to hand over, which is the
165
+ number that decides whether the box swaps:
166
+
167
+ | CPU, forward | 1080p | 1080p ×8 |
168
+ |---|---:|---:|
169
+ | **SSIM** | **2.0** | **2.4** |
170
+ | pytorch-msssim | 433 | 2796 |
171
+ | piq | 446 | 3256 |
172
+ | kornia | 457 | 3305 |
173
+ | torchmetrics | 915 | 7026 |
174
+ | | | |
175
+ | **MS-SSIM**, uint8 in | **8.9** | **10.6** |
176
+ | **MS-SSIM**, fp32 in | 16.8 | 127 |
177
+ | pytorch-msssim | 462 | 2898 |
178
+ | piq | 471 | 3256 |
179
+ | | | |
180
+ | **GMSD** | **0.11** | **0.69** |
181
+ | piq | 84 | 793 |
182
+ | | | |
183
+ | **PSNR / MSE / L1** | **<0.01** | **<0.01** |
184
+ | kornia (PSNR) | 3.0 | 190 |
185
+ | torchmetrics, `F.l1_loss` | 27–28 | 380 |
186
+
187
+ The pixel metrics reduce two buffers to a scalar and allocate nothing at all.
188
+ SSIM's 2 MiB does not move when the pixel count goes up 8×, so it is fixed
189
+ working set rather than anything per-pixel. MS-SSIM is cheaper from uint8 than
190
+ from float because the cast is fused into the first pooling step: fed uint8 it
191
+ never materialises a full-resolution float copy of either frame, and only the
192
+ pyramid survives.
193
+
194
+ | CUDA, loss step (fwd + bwd, fp32) | 1080p | 1080p ×8 | 4K ×8 |
195
+ |---|---:|---:|---:|
196
+ | **SSIM** | **94** | **752** | **3022** |
197
+ | pytorch-msssim | 480 | 3763 | 15098 |
198
+ | piq | 456 | 3579 | 14350 |
199
+ | | | | |
200
+ | **MS-SSIM** | **118** | **942** | **3782** |
201
+ | pytorch-msssim | 480 | 3763 | 15098 |
202
+ | piq | 456 | 3579 | 14350 |
203
+ | | | | |
204
+ | **GMSD** | 78 | **616** | **2465** |
205
+ | piq | 72 | 760 | 3040 |
206
+ | | | | |
207
+ | **L1** | **24** | **190** | **760** |
208
+ | `F.l1_loss` | 96 | 760 | 3040 |
209
+
210
+ Every row includes the gradient with respect to the prediction — 24 MiB at
211
+ 1080p, 760 at 4K ×8 — which is unavoidable and which every implementation
212
+ pays. For `fa.l1` it is the *entire* figure: the backward writes the gradient
213
+ and allocates nothing else. SSIM costs three more planes on top of it and
214
+ MS-SSIM four, at every size — the fused backward recomputes the local moments
215
+ instead of saving them, while the autograd implementations keep the forward's
216
+ activations, which is where the 15 GiB comes from. GMSD on a single 1080p
217
+ frame is the one row that loses, by 6 MiB.
218
+
219
+ `StreamingMetrics` at 1080p RGB holds **18 MiB** — the two device frames plus
220
+ the CUDA graph's private pool — and allocates **nothing** per frame, whether
221
+ it is scoring one metric or nine.
222
+
223
+ Without the compiled extension the portable path allocates like the rest of
224
+ the field, and slightly worse than the best of it: 2830 MiB for SSIM at
225
+ 1080p ×8, and 4.5–4.7 GiB as a loss step depending on what `torch.compile`
226
+ managed to fuse.
227
+
228
+ Full tables: `python bench/bench_memory.py`.
229
+
230
+ ## Accuracy
231
+
232
+ Defaults reproduce `ssim_index.m`: 11×11 Gaussian, σ=1.5, K=(0.01, 0.03),
233
+ `valid` support. Every kernel is gated against a float64 transcription of the
234
+ paper (`python tests/validate.py`).
235
+
236
+ | implementation | SSIM abs. error |
237
+ |---|---:|
238
+ | **frame_analytics** (CUDA / CPU / portable) | **2.4e-09 / 3.1e-09 / 6.9e-09** |
239
+ | pytorch-msssim | 1.0e-07 |
240
+ | fused-ssim (`padding="valid"`) | 3.3e-06 |
241
+ | kornia | 2.1e-05 |
242
+ | torchmetrics | 2.2e-05 |
243
+ | scikit-image (defaults) | 1.1e-03 |
244
+ | fused-ssim (`padding="same"`, its default) | 3.0e-03 |
245
+ | piq (defaults) | 8.8e-03 |
246
+
247
+ The bottom entries aren't bugs — different windows, MATLAB-style downsampling,
248
+ zero padding. Defensible defaults, different metric.
249
+
250
+ MSE and PSNR match float64 numpy exactly; the accumulator is float64 even when
251
+ the elementwise work is float32 (a 4K frame has 8.3M residuals, and summing
252
+ those in float32 loses ~4 significant digits straight into the dB figure).
253
+
254
+ Worst abs. error vs float64 reference, over CPU and CUDA, native and portable:
255
+
256
+ | metric | error |
257
+ |---|---:|
258
+ | MS-SSIM | 3.7e-08 |
259
+ | GMSD | 2.8e-09 |
260
+ | L1, Huber (uint8) | 8.9e-16 — exact, integer accumulator |
261
+ | Charbonnier | 1.3e-07 |
262
+
263
+ GMSD's similarity map is float32, so deviations below ~1e-7 measure rounding,
264
+ not the images (`gmsd(x, x)` ≈ 3e-08, not 0). A typical GMSD is ~0.03.
265
+
266
+ ## Training
267
+
268
+ Every metric is differentiable, and the SSIM family has a fused CUDA backward
269
+ rather than autograd over the portable path — which is where the memory goes:
270
+
271
+ ```python
272
+ crit_ms = fa.MSSSIM(data_range=1.0)
273
+ crit_l1 = fa.L1()
274
+ loss = 0.84 * crit_ms.loss(pred, target) + 0.16 * crit_l1(pred, target)
275
+ loss.backward()
276
+ ```
277
+
278
+ That mix is Zhao et al., *Loss Functions for Image Restoration with Neural
279
+ Networks* (2016) — the reason MS-SSIM is here at all.
280
+
281
+ MS-SSIM loss step, 1080p ×8, float32 RGB: **12.7 ms / 942 MiB** against
282
+ pytorch-msssim's 81.3 ms / 3763 MiB — **6.4× faster on 4.0× less memory**. The
283
+ fused backward recomputes the local moments instead of storing them, so nothing
284
+ full-resolution survives the forward pass at any of the five scales. Both
285
+ figures include the 190 MiB gradient neither implementation can avoid; see
286
+ [Memory](#memory).
287
+
288
+ Gradients are verified two independent ways — against autograd over the portable
289
+ path, and against central differences on the float64 reference forward — both to
290
+ ~1e-6 relative. The scalar-reduction backward issues no device→host sync, so it
291
+ does not stall the training pipeline.
292
+
293
+ Caveats:
294
+
295
+ - `GMSD.loss()` returns `1 − mean(GMS)`, not the deviation. `d√var/dvar` is
296
+ unbounded as the variance goes to zero, which is exactly where a converging
297
+ model lives; the mean is the well-behaved objective from the same map.
298
+ `fa.gmsd()` still gives you the published metric.
299
+ - MS-SSIM's per-scale factors are clamped at zero, so on anti-correlated content
300
+ the value **and its gradient** are exactly zero. Standard formulation, every
301
+ implementation clamps — but MS-SSIM alone cannot pull a diverged model back.
302
+ The L1 term above removes the dead zone.
303
+ - The native backward is CUDA + float32 only; CPU, float64, uint8 and
304
+ `downsample=True` fall back to autograd over the portable path.
305
+ - The fused backward is a kernel, not a graph, so `create_graph=True` (gradient
306
+ penalties, HVPs, `torch.func.hessian`) raises rather than silently returning
307
+ zeros. For a second derivative use `backend_hint="torch"` with
308
+ `fa.set_compile_enabled(False)`.
309
+
310
+ ## Reporting conventions
311
+
312
+ `luma=` and `crop_border=` are on every metric. Nearly every super-resolution
313
+ and restoration paper reports PSNR/SSIM on the luma plane of a border-cropped
314
+ frame, so a library without them produces right-looking numbers that quietly
315
+ disagree with the literature.
316
+
317
+ ```python
318
+ fa.psnr(a, b, data_range=255.0, luma="matlab", crop_border=scale) # Y-PSNR
319
+ fa.ssim(a, b, data_range=255.0, luma="matlab", crop_border=scale) # Y-SSIM
320
+ ```
321
+
322
+ `"matlab"` is the studio-range Y′ of MATLAB's `rgb2ycbcr`, i.e. what BasicSR's
323
+ `test_y_channel=True` computes. `"bt601"` and `"bt709"` are the full-range
324
+ definitions. The crop is a view, so it costs nothing.
325
+
326
+ ## Install
327
+
328
+ ```bash
329
+ pip install frame-analytics
330
+ ```
331
+
332
+ torch ≥ 2.0, numpy. The kernels arrive **precompiled**, one wheel per platform
333
+ and no version matrix — the same wheel works on every Python and every torch
334
+ build.
335
+
336
+ That is not the usual arrangement, because the usual arrangement cannot be
337
+ published. A torch C++ extension is ABI-locked to the exact {python} × {torch} ×
338
+ {CUDA} it was compiled against, and a wheel filename has nowhere to record
339
+ "torch 2.6": pip would match on Python and platform alone and hand you an
340
+ `undefined symbol` ImportError. So the kernels sit behind a plain C ABI instead
341
+ — raw pointers, an int dtype code, a stream handle, int return codes — and the
342
+ binaries link neither libtorch nor libpython. On Windows they import exactly one
343
+ library, `KERNEL32.dll`; on Linux, glibc. An NVIDIA driver at runtime is the
344
+ only external requirement, and only for the CUDA half.
345
+
346
+ Install on a platform with no wheel and the sources — which ship too — compile
347
+ on first call (~1 min, cached thereafter). No compiler at all and every native
348
+ kernel still has a portable PyTorch fallback returning the same numbers.
349
+ `fa.backend_status()` reports which tier is live and where its binary came from;
350
+ `backend_hint="torch"` / `"native"` forces either. On Windows the MSVC build
351
+ environment is located automatically.
352
+
353
+ To compile from source at install time instead:
354
+
355
+ ```bash
356
+ FA_BUILD_EXT=1 FA_CUDA_ARCHS="8.6 9.0+PTX" pip install frame-analytics
357
+ ```
358
+
359
+ From a checkout:
360
+
361
+ ```bash
362
+ pip install -e .
363
+ python tests/validate.py # accuracy gate
364
+ python bench/bench.py # speed + accuracy tables
365
+ python bench/bench_training.py # MS-SSIM / GMSD / pixel losses
366
+ python bench/bench_memory.py # peak memory, forward and loss step
367
+ python bench/bench_fused_ssim.py # head-to-head vs fused-ssim
368
+ ```
369
+
370
+ ## API
371
+
372
+ Every metric takes `reduction` (`"mean"` → scalar, `"none"` → per-image `(N,)`),
373
+ `dtype`, `luma`, `crop_border` and — where there is a kernel — `backend_hint`.
374
+
375
+ ```python
376
+ mse (x, y, *, reduction="mean", dtype=None, out_dtype=torch.float64,
377
+ luma=None, crop_border=0)
378
+ psnr (x, y, *, data_range=None, eps=0.0, ...)
379
+ ssim (x, y, *, data_range=None, win_size=11, sigma=1.5, K=(0.01, 0.03),
380
+ return_map=False, downsample=False, backend_hint="auto", ...)
381
+ ms_ssim (x, y, *, data_range=None, win_size=11, sigma=1.5, K=(0.01, 0.03),
382
+ weights=MS_SSIM_WEIGHTS, backend_hint="auto", ...)
383
+ gmsd (x, y, *, data_range=None, T=None, eps=None, downsample=True,
384
+ return_map=False, backend_hint="auto", ...)
385
+ gms (x, y, ...) # mean of the same map
386
+ l1 (x, y, ...)
387
+ charbonnier(x, y, *, eps=1e-3, ...) # mean sqrt(d^2 + eps^2)
388
+ huber (x, y, *, delta=1.0, ...) # matches torch.nn.HuberLoss
389
+ rgb_to_luma(t, mode="bt601", *, data_range=None, dtype=None)
390
+ ```
391
+
392
+ `data_range` defaults to 255 for integer input, 1.0 for float. `downsample=True`
393
+ on `ssim` applies MATLAB `ssim.m`'s automatic box-downsample (off by default, as
394
+ in `ssim_index.m` and every PyTorch library); on `gmsd` it is the paper's 2×
395
+ prefilter and is *on* by default.
396
+
397
+ Module forms `MSE`, `PSNR`, `SSIM`, `MSSSIM`, `GMSD`, `L1`, `Charbonnier`,
398
+ `Huber` cache what they can and expose `.loss()`:
399
+
400
+ ```python
401
+ crit = fa.SSIM(data_range=1.0)
402
+ loss = crit.loss(pred, target) # 1 - SSIM, fused backward on CUDA float32
403
+ loss.backward()
404
+
405
+ sm = fa.StreamingMetrics((1, 3, 1080, 1920), device="cuda", dtype=torch.uint8,
406
+ metrics=("psnr", "ssim", "ms_ssim", "gmsd"))
407
+ for ref, dist in frames:
408
+ out = sm.update(ref, dist) # one graph replay, all four metrics
409
+ ```
410
+
411
+ `StreamingMetrics` accepts any of `mse`, `psnr`, `ssim`, `ms_ssim`, `gmsd`,
412
+ `gms`, `l1`, `charbonnier`, `huber`; they all capture into the same CUDA graph,
413
+ so scoring a frame on nine metrics is still one replay.
414
+
415
+ ## License
416
+
417
+ Apache 2.0.
418
+
419
+ Wang, Bovik, Sheikh, Simoncelli. *Image Quality Assessment: From Error
420
+ Visibility to Structural Similarity.* IEEE TIP 13(4), 2004.