Noise2Void (n2v)

repository·main·Indexed 19 days ago

https://github.com/juglab/n2v

A self-supervised deep learning framework for image denoising that works on single noisy images without requiring clean ground truth targets, particularly useful for biomedical microscopy data. It includes support for N2V2 to fix checkerboard artifacts and StructN2V for structured noise. The package is compatible with TensorFlow versions prior to 2.16 and provides plugins for the napari image viewer. Note: n2v is being deprecated in favor of CAREamics.

Tokens
4.3K
Snippets
15
Records
20
Agent score
67%

What's inside n2v

  1. Migrate to CAREamics

    main
    The n2v package is being deprecated. For modern development, especially if you prefer PyTorch or want a more user-oriented library with a napari plugin, it is recommended to use CAREamics. CAREamics supports a variety of algorithms including Noise2Void.
  2. Install n2v from source

    main

    If you intend to edit the code, clone the repository and install it in editable mode using pip install -e ..

    $ git clone https://github.com/juglab/n2v.git
    $ cd n2v
    $ pip install -e .
  3. Configure TensorFlow for n2v compatibility

    main

    Because n2v is incompatible with TensorFlow 2.16+, you must install specific older versions. It is recommended to use Miniconda to manage your environment.

    Linux/Windows (GPU):

    conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/
    python3 -m pip install tensorflow

    macOS (No official GPU support):

    python3 -m pip install tensorflow

    Alternative Setup (TensorFlow 2.13 - Untested)

    Linux/Windows WSL2:

    conda install -c conda-forge cudatoolkit=11.8.0
    python3 -m pip install nvidia-cudnn-cu11==8.6.0.163 tensorflow==2.13.*
    mkdir -p $CONDA_PREFIX/etc/conda/activate.d
    echo 'CUDNN_PATH=$(dirname $(python -c "import nvidia.cudnn;print(nvidia.cudnn.__file__)"))' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
    echo 'export LD_LIBRARY_PATH=$CUDNN_PATH/lib:$CONDA_PREFIX/lib/:$LD_LIBRARY_PATH' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
    source $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
  4. Enable N2V2 functionality in N2V-Config

    main

    You can use N2V2 (which fixes checkerboard artifacts) by passing specific parameters to the N2V-Config object.

    Note: N2V2 currently only supports 2D data and has not been tested with struct-N2V.

    Use these parameters to activate N2V2 features:

    • blurpool=True (default is False)
    • skip_skipone=True (default is False)
    • n2v_manipulator="median" (default is "uniform_withCP")
    • unet_residual=False (default is False)
  5. Prepare 2D image data for N2V

    main

    N2V expects input data to have a channel dimension. If your input data is 3D (N, H, W), you must add a new axis to make it 4D (N, H, W, 1).

    Note: While not strictly required, you can emulate an 8-bit image format by clipping and rounding the data to the [0, 255] range using np.round(np.clip(X, 0, 255.)).

    import numpy as np
    
    # Assuming X is loaded from a .npy file
    # Add channel dimension: (N, H, W) -> (N, H, W, 1)
    X = X[..., np.newaxis]
    
    # Optional: Emulate 8-bit format
    # X = np.round(np.clip(X, 0, 255.))
  6. Troubleshoot TensorFlow GPU memory allocation

    main

    If TensorFlow fails to allocate GPU memory, you can attempt to resolve this by setting the TF_FORCE_GPU_ALLOW_GROWTH environment variable to true.

    export TF_FORCE_GPU_ALLOW_GROWTH=true
  7. Configure N2V training with N2VConfig

    main

    The N2VConfig object stores network architecture and training parameters. It automatically calculates mean and std from the provided training data X for normalization.

    Key Configuration Parameters:

    • X: The training patches.
    • train_batch_size: Recommended to be increased compared to supervised training.
    • batch_norm: Set to True for better results.
    • n2v_manipulator: Defines how input pixels are manipulated to prevent the network from learning the identity. Options include:
      • 'uniform_withCP': (Default) Uniform replacement.
      • 'normal_withoutCP': Samples neighborhood via Gaussian distribution without the center pixel.
      • 'normal_additive': Adds random Gaussian noise (sigma = n2v_neighborhood_radius).
      • 'normal_fitted': Uses Gaussian distribution with mean and std of the neighborhood.
      • 'identity': No manipulation.
    • n2v_neighborhood_radius: Controls the size of the neighborhood for pixel manipulation.
    • n2v_perc_pix: The fraction of input pixels to manipulate per patch (e.g., 0.198).
    • n2v_patch_shape: The shape of random subpatches extracted during training (default: (64, 64)).
    • single_net_per_channel: (Default: True) Creates a separate U-Net for each channel to prevent bleedthrough artifacts. Note that this increases memory requirements.
    from n2v.models import N2VConfig
    
    config = N2VConfig(
        X, 
        unet_kern_size=3, 
        train_steps_per_epoch=int(X.shape[0]/128), 
        train_epochs=20, 
        train_loss='mse', 
        batch_norm=True, 
        train_batch_size=128, 
        n2v_perc_pix=0.198, 
        n2v_patch_shape=(64, 64), 
        n2v_manipulator='uniform_withCP', 
        n2v_neighborhood_radius=5
    )
  8. Save denoised results as ImageJ-compatible TIFFs

    main

    To save the denoised output for use in other software like ImageJ, use save_tiff_imagej_compatible from the csbdeep.io module. Ensure you specify the correct axes to match your data dimensions.

    from csbdeep.io import save_tiff_imagej_compatible
    
    # Save the prediction
    save_tiff_imagej_compatible('pred_train.tif', pred_train, axes='YX')
  9. Load a previously trained N2V model

    main

    To load a model that has already been trained, instantiate the N2V class by passing config=None along with the name of the model and the basedir where the model files are stored. By default, this loads the weights that resulted in the lowest validation loss during training.

    If you specifically want to load the latest computed weights instead of the best weights, use the load_weights method with the appropriate filename (e.g., 'weights_last.h5').

    from n2v.models import N2V
    
    model_name = 'n2v_2D_sem'
    basedir = 'models'
    # Load model with best weights
    model = N2V(config=None, name=model_name, basedir=basedir)
    
    # Optionally load the latest weights instead
    # model.load_weights('weights_last.h5')