Draft
Conversation
- Add n2v_poisson_loss function with denormalisation support - Update n2v_loss to handle channel mismatches - Add N2V_POISSON to supported losses - Update loss factory to include Poisson loss - Add Softplus activation function support - Allow N2V algorithm to use n2v_poisson loss The Poisson loss uses PyTorch's optimised poisson_nll_loss with: - Automatic denormalisation to photon count scale - ReLU activation to ensure positive rates - Masked averaging over manipulated pixels only
- Add tests for n2v_poisson_loss function - Test with various batch sizes and channel configurations - Test with zero predictions and empty masks - Test multi-channel handling - Test loss factory for n2v_poisson loss type
- Add n2v_poisson to base UNetBasedAlgorithm loss types for type safety - Fix line length issues (split long comment, shorten docstring) - Add missing periods to docstrings for numpydoc validation - Auto-formatting by black and ruff
Obsolete Draft
Allow users to select between n2v (MSE) and n2v_poisson (Poisson NLL) loss functions through the configuration factory function.
- Parametrize test_n_channels_n2v to test both n2v and n2v_poisson losses - Add test_n2v_configuration_loss to verify loss parameter in create_n2v_configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Note
tldr: This PR introduces Poisson negative log-likelihood (NLL) loss for N2V, enabling proper handling of count data with Poisson noise, including automatic denormalisation and multi-channel support.
Background - why do we need this PR?
Standard N2V loss uses mean squared error (MSE), which assumes Gaussian noise. For count data, where noise follows a Poisson distribution, MSE would be suboptimal, because Poisson noise variance equals mean, whilst MSE assumes constant variance.
Examples of Poisson count data include photon counting (fluorescence microscopy), particle counting, or any discrete counting process. This PR adds
n2v_poissonloss to support Poisson noise models whilst maintaining backwards compatibility with the existing MSE-based N2V loss.Overview - what changed?
This PR adds a new Poisson NLL loss function for N2V and enhances the existing N2V loss to handle channel mismatches. Configuration support has been added to allow users to select between MSE-based (
n2v) and Poisson NLL-based (n2v_poisson) losses. The implementation includes automatic denormalisation from normalised training data back to count scale before computing the Poisson NLL.Implementation - how did you implement the changes?
The
n2v_poisson_lossfunction leverages PyTorch's optimisedF.poisson_nll_lossand implements:n2v_lossfor consistencyUsage:
The
n2v_lossfunction was also enhanced to handle channel mismatches (e.g., model outputs 1 channel but input has multiple channels), which is useful for certain multi-channel denoising scenarios.Changes Made
New features or files
n2v_poisson_loss()function insrc/careamics/losses/fcn/losses.pytests/losses/test_fcn_losses.py- comprehensive tests for Poisson loss and channel handlingN2V_POISSONenum insrc/careamics/config/support/supported_losses.pySOFTPLUSenum insrc/careamics/config/support/supported_activations.pyModified features or files
n2v_loss()insrc/careamics/losses/fcn/losses.py- handles channel mismatchesloss_factory()insrc/careamics/losses/loss_factory.py- added Poisson loss__all__insrc/careamics/losses/__init__.py- exportedn2v_poisson_losslossfield insrc/careamics/config/algorithms/n2v_algorithm_config.py- nowLiteral["n2v", "n2v_poisson"]get_activation()insrc/careamics/models/activation.py- added Softplus supportRemoved features or files
How has this been tested?
61 tests passing in
tests/losses/test_fcn_losses.pycovering:"n2v"and"n2v_poisson"string/enum typesRelated Issues
Breaking changes
None - Existing N2V configurations with
loss="n2v"continue to work exactly as before.Additional Notes and Examples
Why ReLU over Softplus for Poisson?
Denormalisation is needed
image_means/image_stdsfrom*argsPlease ensure your PR meets the following requirements: