psnr
PSNR metrics compatible with torchmetrics.
SIPSNR #
Bases: Metric
Scale Invariant PSNR metric using a global data range.
By default, the metric is averaged over channels, but it can also be computed for a specific channel by setting output_channel to the desired channel index.
Adapted from juglab/ScaleInvPSNR, this version of PSNR rescales the predictions and ground truth to have similar range, then computes the PSNR using a global data range accumulated over all batches. For a scale-invariant version of PSNR with per-sample data range, see SampleSIPSNR.
Scale invariance can be turned off using use_scale_invariance=False, in which case the metric is equivalent to torchmetrics.image.PeakSignalNoiseRatio, with data_range equal to the difference between the global max and min over all batches.
Note that as opposed to torchmetrics.image.PeakSignalNoiseRatio, this implementation is compatible with 3D and can be computed on a single channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_channels | int | Number of channels in the input images. | required |
output_channel | int | Channel to compute the metric on. If -1, the metric is computed on all channels. | -1 |
use_scale_invariance | bool | Whether to use scale invariance. If False, the metric is equivalent to PSNR with global data range. | True |
**kwargs | Any | Additional keyword arguments passed to the parent Metric class. | {} |
Attributes:
| Name | Type | Description |
|---|---|---|
glob_max | Tensor | Global maximum values for each channel. |
glob_min | Tensor | Global minimum values for each channel. |
mse_log | Tensor | Logarithm of the mean squared error summed over batches. |
total | Tensor | Total number of samples processed. |
Source code in src/careamics/lightning/dataset_ng/metrics/psnr.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
__init__(n_channels, output_channel=-1, use_scale_invariance=True, **kwargs) #
Initialize a global scale invariant PSNR metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_channels | int | Number of channels in the input images. | required |
output_channel | int | Channel to compute the metric on. If -1, the metric is computed on all channels. | -1 |
use_scale_invariance | bool | Whether to use scale invariance. If False, the metric is equivalent to PSNR with global data range. | True |
**kwargs | Any | Additional keyword arguments passed to the parent Metric class. | {} |
Source code in src/careamics/lightning/dataset_ng/metrics/psnr.py
compute() #
Compute the final metric value.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of length C containing the computed PSNR for each channel. |
Source code in src/careamics/lightning/dataset_ng/metrics/psnr.py
update(preds, target) #
Update the metric states with values computed from a new batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds | Tensor | Predicted images tensor of shape (B, C, (Z), Y, X). | required |
target | Tensor | Ground truth images tensor of shape (B, C, (Z), Y, X). | required |
Source code in src/careamics/lightning/dataset_ng/metrics/psnr.py
SampleSIPSNR #
Bases: Metric
Scale Invariant PSNR metric with per-sample data range.
By default, the metric is averaged over channels, but it can also be computed for a specific channel by setting output_channel to the desired channel index.
Adapted from juglab/ScaleInvPSNR, this version of PSNR rescales the predictions and ground truth to have similar range, then computes the PSNR using each patch's data range.
Scale invariance can be turned off using use_scale_invariance=False, in which case the metric is equivalent to torchmetrics.image.PeakSignalNoiseRatio, with data_range equal to the difference between each patch's max and min, for each patch, then averaged.
Note that as opposed to torchmetrics.image.PeakSignalNoiseRatio, this implementation is compatible with 3D and multi-channel images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_channels | int | Number of channels in the input images. | required |
output_channel | int | Channel to compute the metric on. If -1, the metric is computed on all channels. | -1 |
use_scale_invariance | bool | Whether to use scale invariance. If False, the metric is equivalent to PSNR with per-sample data range. | True |
**kwargs | Any | Additional keyword arguments passed to the parent Metric class. | {} |
Attributes:
| Name | Type | Description |
|---|---|---|
psnr_sum | Tensor | Sum of PSNR values for each channel. |
total | Tensor | Total number of samples processed. |
Source code in src/careamics/lightning/dataset_ng/metrics/psnr.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
__init__(n_channels, output_channel=-1, use_scale_invariance=True, **kwargs) #
Initialize a per-sample scale invariant PSNR metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_channels | int | Number of channels in the input images. | required |
output_channel | int | Channel to compute the metric on. If -1, the metric is computed on all channels. | -1 |
use_scale_invariance | bool | Whether to use scale invariance. If False, the metric is equivalent to PSNR with per-sample data range. | True |
**kwargs | Any | Additional keyword arguments passed to the parent Metric class. | {} |
Source code in src/careamics/lightning/dataset_ng/metrics/psnr.py
compute() #
Compute the final metric value.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of length C containing the computed PSNR for each channel. |
Source code in src/careamics/lightning/dataset_ng/metrics/psnr.py
update(preds, target) #
Update the metric states with values computed from a new batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds | Tensor | Predicted images tensor of shape (B, C, (Z), Y, X). | required |
target | Tensor | Ground truth images tensor of shape (B, C, (Z), Y, X). | required |