losses
Methods for Loss Computation.
denoisplit_loss(model_outputs, targets, config, gaussian_likelihood=None, noise_model_likelihood=None) #
Loss function for DenoiSplit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_outputs | tuple[Tensor, dict[str, Any]] | Tuple containing the model predictions (shape is (B, | required |
targets | Tensor | The target image used to compute the reconstruction loss. Shape is (B, | required |
config | LVAELossConfig | The config for loss function containing all loss hyperparameters. | required |
gaussian_likelihood | GaussianLikelihood | The Gaussian likelihood object. | None |
noise_model_likelihood | NoiseModelLikelihood | The noise model likelihood object. | None |
Returns:
| Name | Type | Description |
|---|---|---|
output | Optional[dict[str, Tensor]] | A dictionary containing the overall loss |
Source code in src/careamics/losses/lvae/losses.py
denoisplit_musplit_loss(model_outputs, targets, config, gaussian_likelihood, noise_model_likelihood) #
Loss function for DenoiSplit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_outputs | tuple[Tensor, dict[str, Any]] | Tuple containing the model predictions (shape is (B, | required |
targets | Tensor | The target image used to compute the reconstruction loss. Shape is (B, | required |
config | LVAELossConfig | The config for loss function containing all loss hyperparameters. | required |
gaussian_likelihood | GaussianLikelihood | The Gaussian likelihood object. | required |
noise_model_likelihood | NoiseModelLikelihood | The noise model likelihood object. | required |
Returns:
| Name | Type | Description |
|---|---|---|
output | Optional[dict[str, Tensor]] | A dictionary containing the overall loss |
Source code in src/careamics/losses/lvae/losses.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
get_kl_divergence_loss(kl_type, topdown_data, rescaling, aggregation, free_bits_coeff, img_shape=None) #
Compute the KL divergence loss.
NOTE: Description of rescaling methods: - If "latent_dim", the KL-loss values are rescaled w.r.t. the latent space dimensions (spatial + number of channels, i.e., (C, [Z], Y, X)). In this way they have the same magnitude across layers. - If "image_dim", the KL-loss values are rescaled w.r.t. the input image spatial dimensions. In this way, the lower layers have a larger KL-loss value compared to the higher layers, since the latent space and hence the KL tensor has more entries. Specifically, at hierarchy i, the total KL loss is larger by a factor (128/i**2).
NOTE: the type of aggregation determines the magnitude of the KL-loss. Clearly, "sum" aggregation results in a larger KL-loss value compared to "mean" by a factor of n_layers.
NOTE: recall that sample-wise KL is obtained by summing over all dimensions, including Z. Also recall that in current 3D implementation of LVAE, no downsampling is done on Z. Therefore, to avoid emphasizing KL loss too much, we divide it by the Z dimension of input image in every case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kl_type | Literal['kl', 'kl_restricted'] | The type of KL divergence loss to compute. | required |
topdown_data | dict[str, Tensor] | A dictionary containing information computed for each layer during the top-down pass. The dictionary must include the following keys: - "kl": The KL-loss values for each layer. Shape of each tensor is (B,). - "z": The sampled latents for each layer. Shape of each tensor is (B, layers, | required |
rescaling | Literal['latent_dim', 'image_dim'] | The rescaling method used for the KL-loss values. If "latent_dim", the KL-loss values are rescaled w.r.t. the latent space dimensions (spatial + number of channels, i.e., (C, [Z], Y, X)). If "image_dim", the KL-loss values are rescaled w.r.t. the input image spatial dimensions. | required |
aggregation | Literal['mean', 'sum'] | The aggregation method used to combine the KL-loss values across layers. If "mean", the KL-loss values are averaged across layers. If "sum", the KL-loss values are summed across layers. | required |
free_bits_coeff | float | The free bits coefficient used for the KL-loss computation. | required |
img_shape | Optional[tuple[int]] | The shape of the input image to the LVAE model. Shape is ([Z], Y, X). | None |
Returns:
| Name | Type | Description |
|---|---|---|
kl_loss | Tensor | The KL divergence loss. Shape is (1, ). |
Source code in src/careamics/losses/lvae/losses.py
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 | |
get_reconstruction_loss(reconstruction, target, likelihood_obj) #
Compute the reconstruction loss (negative log-likelihood).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reconstruction | Tensor | The output of the LVAE decoder. Shape is (B, C, [Z], Y, X), where C is the number of output channels (e.g., 1 in HDN, >1 in muSplit/denoiSplit). | required |
target | Tensor | The target image used to compute the reconstruction loss. Shape is (B, C, [Z], Y, X), where C is the number of output channels (e.g., 1 in HDN, >1 in muSplit/denoiSplit). | required |
likelihood_obj | Likelihood | The likelihood object used to compute the reconstruction loss. | required |
Returns:
| Type | Description |
|---|---|
Tensor | The recontruction loss (negative log-likelihood). |
Source code in src/careamics/losses/lvae/losses.py
hdn_loss(model_outputs, targets, config, gaussian_likelihood, noise_model_likelihood) #
Loss function for HDN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_outputs | tuple[Tensor, dict[str, Any]] | Tuple containing the model predictions (shape is (B, | required |
targets | Tensor | The target image used to compute the reconstruction loss. In this case we use the input patch itself as target. Shape is (B, | required |
config | LVAELossConfig | The config for loss function containing all loss hyperparameters. | required |
gaussian_likelihood | GaussianLikelihood | The Gaussian likelihood object. | required |
noise_model_likelihood | NoiseModelLikelihood | The noise model likelihood object. | required |
Returns:
| Name | Type | Description |
|---|---|---|
output | Optional[dict[str, Tensor]] | A dictionary containing the overall loss |
Source code in src/careamics/losses/lvae/losses.py
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 | |
musplit_loss(model_outputs, targets, config, gaussian_likelihood, noise_model_likelihood=None) #
Loss function for muSplit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_outputs | tuple[Tensor, dict[str, Any]] | Tuple containing the model predictions (shape is (B, | required |
targets | Tensor | The target image used to compute the reconstruction loss. Shape is (B, | required |
config | LVAELossConfig | The config for loss function (e.g., KL hyperparameters, likelihood module, noise model, etc.). | required |
gaussian_likelihood | GaussianLikelihood | The Gaussian likelihood object. | required |
noise_model_likelihood | Optional[NoiseModelLikelihood] | The noise model likelihood object. Not used here. | None |
Returns:
| Name | Type | Description |
|---|---|---|
output | Optional[dict[str, Tensor]] | A dictionary containing the overall loss |
Source code in src/careamics/losses/lvae/losses.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |