### Install Noise2Void Source: https://github.com/juglab/n2v/wiki/mpi-cluster Install the Noise2Void package using pip. This command should be run on the cluster account. ```bash pip3 install --user n2v ``` -------------------------------- ### Download and Load Example Data Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_convallaria/01_training.ipynb Downloads the 'flower.tif' dataset if it doesn't exist and loads it using imread. Ensure the 'data' directory exists. ```python # create a folder for our data. if not os.path.isdir('./data'): os.mkdir('./data') # check if data has been downloaded already zipPath="data/flower.tif" if not os.path.exists(zipPath): urllib.request.urlretrieve('https://download.fht.org/jug/n2v/flower.tif', zipPath) data = imread("data/flower.tif") ``` -------------------------------- ### Download and Unzip Example Data Source: https://github.com/juglab/n2v/blob/main/examples/3D/01_training.ipynb Downloads a zip file containing example 3D image data and extracts it into a 'data' directory. It checks if the data has already been downloaded to avoid re-downloading. ```python # create a folder for our data if not os.path.isdir('./data'): os.mkdir('./data') # check if data has been downloaded already zipPath='data/flywing-data.zip' if not os.path.exists(zipPath): #download and unzip data data = urllib.request.urlretrieve('https://download.fht.org/jug/n2v/flywing-data.zip', zipPath) with zipfile.ZipFile(zipPath, 'r') as zip_ref: zip_ref.extractall('data') ``` -------------------------------- ### Install n2v from Source Source: https://github.com/juglab/n2v/blob/main/README.md Installs Noise2Void (n2v) in editable mode from a local clone of the repository. This allows for code modifications to be immediately reflected. ```bash cd n2v pip install -e . ``` -------------------------------- ### Download and Unzip Example Data Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/01_training.ipynb Downloads the 'RGB.zip' dataset from a specified URL if it doesn't already exist in the 'data' directory. It then extracts the contents of the zip file. ```python # create a folder for our data if not os.path.isdir('./data'): os.mkdir('data') # check if data has been downloaded already zipPath="data/RGB.zip" if not os.path.exists(zipPath): #download and unzip data data = urllib.request.urlretrieve('https://download.fht.org/jug/n2v/RGB.zip', zipPath) with zipfile.ZipFile(zipPath, 'r') as zip_ref: zip_ref.extractall("data") ``` -------------------------------- ### Install n2v using PIP Source: https://github.com/juglab/n2v/blob/main/README.md Installs the latest stable release of the Noise2Void (n2v) package using pip. This is the recommended method for most users. ```bash pip install n2v ``` -------------------------------- ### Download and Extract Example Data Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb This snippet downloads a zip file containing SEM data, creates a 'data' directory if it doesn't exist, and extracts the contents. It checks if the data has already been downloaded to avoid redundant downloads. ```python # create a folder for our data. if not os.path.isdir('./data'): os.mkdir('./data') # check if data has been downloaded already zipPath="data/SEM.zip" if not os.path.exists(zipPath): #download and unzip data data = urllib.request.urlretrieve('https://download.fht.org/jug/n2v/SEM.zip', zipPath) with zipfile.ZipFile(zipPath, 'r') as zip_ref: zip_ref.extractall("data") ``` -------------------------------- ### Install TensorFlow 2.15 (untested) on Linux Source: https://github.com/juglab/n2v/blob/main/README.md Installs TensorFlow 2.15 on Linux using pip with CUDA support. This command simplifies the installation of TensorFlow with GPU capabilities. ```bash python3 -m pip install tensorflow[and-cuda] ``` -------------------------------- ### Install Jupyter Notebook Source: https://github.com/juglab/n2v/blob/main/README.md Installs the Jupyter Notebook environment within your active Conda environment. This is required to run the provided example notebooks. ```bash pip install jupyter ``` -------------------------------- ### Install TensorFlow on macOS Source: https://github.com/juglab/n2v/blob/main/README.md Installs TensorFlow on macOS. Note that official GPU support is not available for macOS, so this command is for CPU-based installations. ```bash python3 -m pip install tensorflow ``` -------------------------------- ### TensorBoard Output Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/01_training.ipynb Example output from TensorBoard indicating the progress of preparing validation data. ```text Output: Using TensorFlow backend. Preparing validation data: 17%|█▋ | 148/848 [00:00<00:00, 1476.89it/s] ``` -------------------------------- ### Install TensorFlow 2.10 (tested) on Windows Source: https://github.com/juglab/n2v/blob/main/README.md Installs TensorFlow 2.10 on Windows, including CUDA and cuDNN for GPU support. This is the last version compatible with Windows without WSL2. ```bash conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0 python3 -m pip install tensorflow ``` -------------------------------- ### Get Interactive GPU Node Source: https://github.com/juglab/n2v/wiki/mpi-cluster Request an interactive GPU node for running scripts. Adjust parameters like time and memory as needed. ```bash srun --exclude=r02n01 -p gpu --gres=gpu:1 --time 0-1:00:00 --mem-per-cpu 256000 --export=ALL --pty /bin/bash ``` -------------------------------- ### Install TensorFlow 2.13 (untested) on Linux Source: https://github.com/juglab/n2v/blob/main/README.md Installs TensorFlow 2.13 on Linux with specific CUDA and cuDNN versions. It also configures environment variables for cuDNN library paths. ```bash 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 ``` -------------------------------- ### Install TensorFlow 2.10 (tested) on Linux Source: https://github.com/juglab/n2v/blob/main/README.md Installs TensorFlow 2.10 along with necessary CUDA and cuDNN libraries for GPU acceleration on Linux. Ensure your system meets the CUDA version requirements. ```bash 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 ``` -------------------------------- ### Import Dependencies Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_synth_mem/train_and_predict.ipynb Initializes the environment by importing necessary libraries and configuring SSL for data downloads. ```python # We import all our dependencies from n2v.models import N2VConfig, N2V import numpy as np from csbdeep.utils import plot_history from n2v.utils.n2v_utils import manipulate_val_data, autocorrelation from n2v.internals.N2V_DataGenerator import N2V_DataGenerator from matplotlib import pyplot as plt import urllib import os import zipfile import ssl ssl._create_default_https_context = ssl._create_unverified_context ``` -------------------------------- ### Run Noise2Void Training Script Source: https://github.com/juglab/n2v/wiki/mpi-cluster Execute the training script with specified data path, file name, dimensions, and training parameters. Use `--stepsPerEpoch=5` for faster, lower-quality results. ```bash trainN2V.py --dataPath='/lustre/projects/juglab/n2vScripts/data/Romina/' --fileName='MTfmi_25deg_181020_SD4-001-hyper.tif' --dims=TZYX --patchSizeZ=16 --batchSize=8 ``` -------------------------------- ### Create Training Job Script Source: https://github.com/juglab/n2v/wiki/mpi-cluster Create a shell script containing the training command and its parameters for submission as a batch job. ```bash #!/bin/sh python3 trainN2V.py --dataPath='/lustre/projects/juglab/n2vScripts/data/Romina/' --fileName='MTfmi_25deg_181020_SD4-001.tif' --dims=ZYX --validationFraction=2 --patchSizeZ=16 --batchSize=8 ``` -------------------------------- ### Import Dependencies Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/02_prediction.ipynb Initializes the necessary libraries for N2V model operations and image handling. ```python # We import all our dependencies. from n2v.models import N2V import numpy as np from matplotlib import pyplot as plt from tifffile import imread from csbdeep.io import save_tiff_imagej_compatible ``` -------------------------------- ### Submit Training Job Source: https://github.com/juglab/n2v/wiki/mpi-cluster Submit the training script as a batch job to the GPU partition. Specify time limits, memory, and output log file. ```bash sbatch -p gpu --gres=gpu:1 --time 0-0:25:00 --mem-per-cpu 256000 --export=ALL -o out.txt trainn2v.sh ``` -------------------------------- ### Initialize N2V Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_synth_mem/train_and_predict.ipynb Define the model name and base directory, then instantiate the N2V model and prepare it for training. ```python # a name used to identify the model model_name = 'structn2v_membrane_sim_normal_withoutCP' # the base directory in which our model will live basedir = 'models' # We are now creating our network model. model = N2V(config, model_name, basedir=basedir) model.prepare_for_training(metrics=()) ``` -------------------------------- ### Import Dependencies Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/02_prediction.ipynb Initializes the necessary libraries for model loading and image processing. ```python # We import all our dependencies. from n2v.models import N2V import numpy as np from matplotlib import pyplot as plt from matplotlib.image import imread, imsave from csbdeep.io import save_tiff_imagej_compatible ``` -------------------------------- ### Initialize Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb Creates the N2V model instance and prepares it for the training process. ```python # a name used to identify the model model_name = 'BSD68_reproducability_5x5' # the base directory in which our model will live basedir = 'models' # We are now creating our network model. model = N2V(config, model_name, basedir=basedir) model.prepare_for_training(metrics=()) ``` -------------------------------- ### Initialize N2V Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_convallaria/01_training.ipynb Initializes a new N2V model with specified configuration, name, and base directory. Ensure the output path for the model is correctly set to avoid overwriting existing files. ```python model = N2V(config, model_name, basedir=basedir) ``` -------------------------------- ### Initialize N2VConfig Source: https://context7.com/juglab/n2v/llms.txt Initialize the configuration object to store network architecture and training parameters. ```python from n2v.models import N2VConfig import numpy as np # X is your training data array with shape (S, Y, X, C) or (S, Z, Y, X, C) ``` -------------------------------- ### Configure Noise2Void Model Parameters Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Initializes the N2VConfig object with network architecture and training parameters. Key parameters include kernel size, training steps/epochs, loss function, batch size, pixel manipulation settings, patch shape, and neighborhood radius. Ensure X is available for data statistics extraction. ```python 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) # Let's look at the parameters stored in the config-object. vars(config) ``` -------------------------------- ### Configure Noise2Void Training Parameters Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_convallaria/01_training.ipynb Initializes the N2VConfig object with specific training parameters and inspects the resulting configuration. ```python # train_steps_per_epoch is set to (number of training patches)/(batch size), like this each training patch # is shown once per epoch. config = N2VConfig(X, unet_kern_size=3, train_steps_per_epoch=int(X.shape[0]/128), train_epochs=10, 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, structN2Vmask = [[0,1,1,1,1,1,1,1,1,1,0]]) # Let's look at the parameters stored in the config-object. vars(config) ``` -------------------------------- ### Load Images from Directory Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_convallaria/01_training.ipynb Loads images from the specified directory with 'TYX' dimensions and prints the shape of the first loaded image. An extra channel dimension is automatically added. ```python imgs = datagen.load_imgs_from_directory(directory = "data/", dims="TYX") print(imgs[0].shape) # The function automatically added an extra "channels" dimensions to the images at the end ``` -------------------------------- ### Load and Prepare Data Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb Loads training and validation data from NumPy files and adds the required channel dimension. ```python X = np.load('data/BSD68_reproducibility_data/train/DCNN400_train_gaussian25.npy') X_val = np.load('data/BSD68_reproducibility_data/val/DCNN400_validation_gaussian25.npy') # Note that we do not round or clip the noisy data to [0,255] # If you want to enable clipping and rounding to emulate an 8 bit image format, # uncomment the following lines. # X = np.round(np.clip(X, 0, 255.)) # X_val = np.round(np.clip(X_val, 0, 255.)) # Adding channel dimension X = X[..., np.newaxis] print(X.shape) X_val = X_val[..., np.newaxis] print(X_val.shape) ``` -------------------------------- ### Configure N2V Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_synth_mem/train_and_predict.ipynb Sets up the N2V configuration with specific parameters for structured noise handling. ```python config = N2VConfig(X_train, unet_kern_size=3, train_steps_per_epoch=10, train_epochs=30, train_loss='mse', batch_norm=True, train_batch_size=128, n2v_perc_pix=0.198, n2v_patch_shape=(64, 64), unet_n_first = 96, unet_residual = True, n2v_manipulator='normal_withoutCP', n2v_neighborhood_radius=2, structN2Vmask = [[0,1,1,1,1,1,0]]) ## mask should be wide enough to cover most of the noise autocorrelation ``` -------------------------------- ### Initialize N2VConfig Source: https://github.com/juglab/n2v/blob/main/examples/3D/01_training.ipynb Creates a configuration object for the Noise2Void model using training data X and specific hyperparameters. ```python # You can increase "train_steps_per_epoch" to get even better results at the price of longer computation. 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=4, n2v_perc_pix=0.198, n2v_patch_shape=(32, 64, 64), n2v_manipulator='uniform_withCP', n2v_neighborhood_radius=5) # Let's look at the parameters stored in the config-object. vars(config) ``` -------------------------------- ### Initialize N2VConfig Object Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/01_training.ipynb Configures the Noise2Void model parameters including architecture, training steps, and pixel manipulation settings. ```python # train_steps_per_epoch is set to (number of training patches)/(batch size), like this each training patch # is shown once per epoch. config = N2VConfig(X, unet_kern_size=3, unet_n_first=64, unet_n_depth=3, train_steps_per_epoch=int(X.shape[0]/128), train_epochs=25, 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, single_net_per_channel=False) # Let's look at the parameters stored in the config-object. vars(config) ``` -------------------------------- ### Configure N2V Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb Sets up the N2V configuration object with specific training parameters. ```python config = N2VConfig(X, unet_kern_size=3, train_steps_per_epoch=400, train_epochs=200, train_loss='mse', batch_norm=True, train_batch_size=128, n2v_perc_pix=0.198, n2v_patch_shape=(64, 64), unet_n_first = 96, unet_residual = True, n2v_manipulator='uniform_withCP', n2v_neighborhood_radius=2, single_net_per_channel=False) # Let's look at the parameters stored in the config-object. vars(config) ``` -------------------------------- ### Load Trained Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/02_prediction.ipynb Instantiates the N2V model by pointing to the directory containing the trained weights. ```python # A previously trained model is loaded by creating a new N2V-object without providing a 'config'. model_name = 'n2v_2D' basedir = 'models' model = N2V(config=None, name=model_name, basedir=basedir) ``` -------------------------------- ### Visualize Data Patches Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb Displays sample training and validation patches to verify data loading. ```python # Let's look at one of our training and validation patches. plt.figure(figsize=(14,7)) plt.subplot(1,2,1) plt.imshow(X[0,...,0], cmap='gray') plt.title('Training Patch'); plt.subplot(1,2,2) plt.imshow(X_val[0,...,0], cmap='gray') plt.title('Validation Patch'); ``` -------------------------------- ### Load Images and Prepare Data Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/01_training.ipynb Loads all '.png' files from the 'data' directory using the N2V_DataGenerator. It specifies the dimension order as 'YXC' and removes the alpha channel if present. ```python # We will load all the '.png' files from the 'data' directory. In our case it is only one. # The function will return a list of images (numpy arrays). # In the 'dims' parameter we specify the order of dimensions in the image files we are reading: # 'C' stands for channels (color) imgs = datagen.load_imgs_from_directory(directory="data/", filter='*.png', dims='YXC') # Let's look at the shape of the image print('shape of loaded images: ',imgs[0].shape) # The function automatically added an extra dimension to the image. # It is used to hold a potential stack of images, such as a movie. # The image has four color channels (stored in the last dimension): RGB and Aplha. # We are not interested in Alpha and will get rid of it. imgs[0] = imgs[0][...,:3] print('shape without alpha: ',imgs[0].shape) ``` -------------------------------- ### Initialize Noise2Void Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Creates an instance of the N2V model using the provided configuration and specifies a model name and base directory for saving. Ensure N2VConfig is initialized. ```python # a name used to identify the model model_name = 'n2v_2D_sem' # the base directory in which our model will live basedir = 'models' # We are now creating our network model. model = N2V(config, model_name, basedir=basedir) ``` -------------------------------- ### Download and Extract Data Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb Checks for the existence of the BSD68 dataset and downloads it if missing. ```python # create a folder for our data if not os.path.isdir('./data'): os.mkdir('data') # check if data has been downloaded already zipPath="data/BSD68_reproducibility.zip" if not os.path.exists(zipPath): #download and unzip data data = urllib.request.urlretrieve('https://download.fht.org/jug/n2v/BSD68_reproducibility.zip', zipPath) with zipfile.ZipFile(zipPath, 'r') as zip_ref: zip_ref.extractall("data") ``` -------------------------------- ### Inspect Configuration Result Source: https://github.com/juglab/n2v/blob/main/examples/3D/01_training.ipynb The output of the vars(config) call displaying the stored configuration parameters. ```text Result: {'means': ['37.057674'], 'stds': ['5.3700876'], 'n_dim': 3, 'axes': 'ZYXC', 'n_channel_in': 1, 'n_channel_out': 1, 'unet_residual': False, 'unet_n_depth': 2, 'unet_kern_size': 3, 'unet_n_first': 32, 'unet_last_activation': 'linear', 'unet_input_shape': (None, None, None, 1), 'train_loss': 'mse', 'train_epochs': 20, 'train_steps_per_epoch': 4, 'train_learning_rate': 0.0004, 'train_batch_size': 4, 'train_tensorboard': True, 'train_checkpoint': 'weights_best.h5', 'train_reduce_lr': {'factor': 0.5, 'patience': 10}, 'batch_norm': True, 'n2v_perc_pix': 0.198, 'n2v_patch_shape': (32, 64, 64), 'n2v_manipulator': 'uniform_withCP', 'n2v_neighborhood_radius': 5, 'single_net_per_channel': True, 'structN2Vmask': None, 'probabilistic': False} ``` -------------------------------- ### Display Training and Validation Patches Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Visualizes the first training and validation patches using matplotlib. Ensure X and X_val are loaded before use. ```python plt.figure(figsize=(14,7)) plt.subplot(1,2,1) plt.imshow(X[0,...,0], cmap='magma') plt.title('Training Patch'); plt.subplot(1,2,2) plt.imshow(X_val[0,...,0], cmap='magma') plt.title('Validation Patch'); ``` -------------------------------- ### Visualize Training and Validation Patches Source: https://github.com/juglab/n2v/blob/main/examples/3D/01_training.ipynb Displays two sample patches, one from the training set (X) and one from the validation set (X_val), using Matplotlib for visual inspection. ```python # Let's look at two patches. plt.figure(figsize=(14,7)) plt.subplot(1,2,1) plt.imshow(X[0,16,...,0],cmap='magma') plt.title('Training Patch'); plt.subplot(1,2,2) plt.imshow(X_val[0,16,...,0],cmap='magma') plt.title('Validation Patch'); ``` -------------------------------- ### Visualize Clean Signal Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_synth_mem/train_and_predict.ipynb Displays the first image of the clean signal dataset. ```python plt.imshow(X[0]) ## clean signal simulated fluorescent cell membranes in 2D epithelium ``` -------------------------------- ### Define Model Name and Directory Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/01_training.ipynb Sets the model identifier and the base directory for saving model files. ```python # a name used to identify the model --> change this to something sensible! model_name = 'n2v_2D' # the base directory in which our model will live basedir = 'models' ``` -------------------------------- ### Create and Activate Conda Environment Source: https://github.com/juglab/n2v/blob/main/README.md Use these commands to create a new Conda environment named 'n2v' with Python 3.9 and then activate it. This isolates project dependencies. ```bash conda create -n 'n2v' python=3.9 conda activate n2v ``` -------------------------------- ### Display Training History Keys Source: https://github.com/juglab/n2v/blob/main/examples/3D/01_training.ipynb Print the keys available in the training history object. These keys represent the metrics recorded during training and validation, such as loss, learning rate, and specific N2V metrics. ```python print(sorted(list(history.history.keys()))) ``` -------------------------------- ### Load Alternative Weights Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/02_prediction.ipynb Optional step to load the latest weights instead of the best validation loss weights. ```python # In case you do not want to load the weights that lead to lowest validation loss during # training but the latest computed weights, you can execute the following line: # model.load_weights('weights_last.h5') ``` -------------------------------- ### Inspect N2V Configuration Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_synth_mem/train_and_predict.ipynb Access the configuration object to view training parameters and model architecture settings. ```python vars(config) ``` -------------------------------- ### Load Images from Directory Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Loads all '.tif' files from a specified directory using the N2V_DataGenerator. The loaded images are returned as a list of NumPy arrays, with added dimensions for stacks and channels. ```python # We load all the '.tif' files from the 'data' directory. # If you want to load other types of files see the RGB example. # The function will return a list of images (numpy arrays). imgs = datagen.load_imgs_from_directory(directory = "data/") # Let's look at the shape of the images. print(imgs[0].shape,imgs[1].shape) # The function automatically added two extra dimensions to the images: # One at the beginning, is used to hold a potential stack of images such as a movie. # One at the end, represents channels. ``` -------------------------------- ### Import Dependencies for Noise2Void Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Imports necessary libraries for Noise2Void, including data manipulation, model configuration, and plotting utilities. It also includes a workaround for SSL certificate verification. ```python # We import all our dependencies. from n2v.models import N2VConfig, N2V import numpy as np from csbdeep.utils import plot_history from n2v.utils.n2v_utils import manipulate_val_data from n2v.internals.N2V_DataGenerator import N2V_DataGenerator from matplotlib import pyplot as plt import urllib import os import zipfile import ssl ssl._create_default_https_context = ssl._create_unverified_context ``` -------------------------------- ### Accessing Method Docstring Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Demonstrates how to access the docstring of a method, specifically `generate_patches_from_list`, using the question mark syntax in an interactive Python environment. ```python # Just in case you don't know how to access the docstring of a method: datagen.generate_patches_from_list? ``` -------------------------------- ### Load Pre-trained Noise2Void Model Source: https://github.com/juglab/n2v/blob/main/examples/3D/02_prediction.ipynb Loads a previously trained Noise2Void model by initializing the N2V class without a configuration. Ensure the model name and base directory are correctly specified. ```python # A previously trained model is loaded by creating a new N2V-object without providing a 'config'. model_name = 'n2v_3D' basedir = 'models' model = N2V(config=None, name=model_name, basedir=basedir) ``` -------------------------------- ### Execute N2V Model Training Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_synth_mem/train_and_predict.ipynb Initiates the training process for the N2V model using training and validation datasets. ```python history = model.train(X_train, X_val) ``` -------------------------------- ### Load N2V Model Weights Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb Loads the weights from a specified HDF5 file into the model instance. ```python model.load_weights('weights_last.h5') ``` -------------------------------- ### Check Running Jobs Source: https://github.com/juglab/n2v/wiki/mpi-cluster View the status of currently running jobs on the cluster using the squeue command. ```bash squeue -u ``` -------------------------------- ### Run Noise2Void Prediction Script Source: https://github.com/juglab/n2v/wiki/mpi-cluster Execute the prediction script to generate denoised images. The `--tile` parameter helps manage GPU memory for large images. ```bash predictN2V.py --dataPath='/lustre/projects/juglab/n2vScripts/data/Romina/' --fileName='MTfmi_25deg_181020_SD4-001-hyper.tif' --dims=TZYX --tile=3 ``` -------------------------------- ### Perform 2D SEM Image Denoising Workflow Source: https://context7.com/juglab/n2v/llms.txt Complete end-to-end pipeline for loading data, training a model, predicting, and exporting results for 2D SEM images. ```python from n2v.models import N2VConfig, N2V from n2v.internals.N2V_DataGenerator import N2V_DataGenerator from tifffile import imread from csbdeep.io import save_tiff_imagej_compatible from csbdeep.utils import plot_history import matplotlib.pyplot as plt import numpy as np # 1. Load and prepare data datagen = N2V_DataGenerator() imgs = datagen.load_imgs_from_directory(directory="data/", filter="*.tif", dims='YX') # 2. Generate patches patch_shape = (64, 64) X = datagen.generate_patches_from_list(imgs[:1], shape=patch_shape, augment=True) X_val = datagen.generate_patches_from_list(imgs[1:], shape=patch_shape, augment=True) print(f"Training patches: {X.shape}, Validation patches: {X_val.shape}") # 3. Configure model config = N2VConfig( X, unet_kern_size=3, train_steps_per_epoch=int(X.shape[0] / 128), train_epochs=100, 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 ) # 4. Create and train model model = N2V(config, 'n2v_sem', basedir='models') history = model.train(X, X_val) # 5. Plot training history plt.figure(figsize=(16, 5)) plot_history(history, ['loss', 'val_loss']) plt.savefig('training_history.png') # 6. Apply to new images input_image = imread('data/test.tif').astype(np.float32) denoised = model.predict(input_image, axes='YX') # 7. Save and visualize results save_tiff_imagej_compatible('denoised_result.tif', denoised, axes='YX') plt.figure(figsize=(16, 8)) plt.subplot(1, 2, 1) plt.imshow(input_image, cmap='magma') plt.title('Noisy Input') plt.subplot(1, 2, 2) plt.imshow(denoised, cmap='magma') plt.title('Denoised Output') plt.savefig('comparison.png') # 8. Export for ImageJ/Fiji model.export_TF( name='N2V SEM Denoiser', description='Trained on SEM microscopy images', authors=['Your Name'], test_img=X_val[0], axes='YXC', patch_shape=patch_shape ) ``` -------------------------------- ### Visualize Training and Validation Patches Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/01_training.ipynb Displays training and validation patches using matplotlib. ```python plt.figure(figsize=(14,7)) plt.subplot(1,2,1) plt.imshow(X[0,...]) plt.title('Training Patch'); plt.subplot(1,2,2) plt.imshow(X_val[0,...]) plt.title('Validation Patch'); ``` -------------------------------- ### Visualize Prediction Results on Training Data Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_convallaria/02_prediction.ipynb Displays the original input image and the denoised prediction side-by-side for comparison. Note that horizontal stripes might appear, which can potentially be reduced by training for a longer duration. ```python # Let's look at the results. plt.figure(figsize=(16,8)) plt.subplot(1,2,1) plt.imshow(input_train[0,:256,:256,0],cmap="magma") plt.title('Input'); plt.subplot(1,2,2) plt.imshow(pred_train[0,:256,:256,0],cmap="magma") plt.title('Prediction'); # Horizontal stripes may appear, matching the pattern of the mask used during training. # Try training for longer time and see if they go away! ``` -------------------------------- ### Import Dependencies for Noise2Void Source: https://github.com/juglab/n2v/blob/main/examples/3D/02_prediction.ipynb Imports necessary libraries for Noise2Void model, numerical operations, plotting, image reading, and saving. ```python from n2v.models import N2V import numpy as np from matplotlib import pyplot as plt from tifffile import imread from csbdeep.io import save_tiff_imagej_compatible ``` -------------------------------- ### Visualize Noisy Input and Denoised Prediction Source: https://github.com/juglab/n2v/blob/main/examples/3D/02_prediction.ipynb Displays the noisy input image and the denoised prediction side-by-side using matplotlib. The visualization uses a 'magma' colormap and percentile-based intensity scaling for clarity. ```python # Let's look at the results. plt.figure(figsize=(30,30)) # We show the noisy input... plt.subplot(1,2,1) plt.imshow(np.max(img,axis=0), cmap='magma', vmin=np.percentile(img,0.1), vmax=np.percentile(img,99.9) ) plt.title('Input'); # and the result. plt.subplot(1,2,2) plt.imshow(np.max(pred,axis=0), cmap='magma', vmin=np.percentile(pred,0.1), vmax=np.percentile(pred,99.9) ) plt.title('Prediction'); ``` -------------------------------- ### Visualize Training Results Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/02_prediction.ipynb Displays a side-by-side comparison of the input training data and the model prediction. ```python # Let's look at the results. plt.figure(figsize=(16,8)) plt.subplot(1,2,1) plt.imshow(input_train[:1500:,:1500],cmap="magma") plt.title('Input'); plt.subplot(1,2,2) plt.imshow(pred_train[:1500,:1500],cmap="magma") plt.title('Prediction'); ``` -------------------------------- ### Predict Denoising with Noise2Void Model Source: https://github.com/juglab/n2v/blob/main/examples/3D/02_prediction.ipynb Loads the input image and uses the pre-trained Noise2Void model to denoise it. The 'n_tiles' parameter is crucial for handling images that are too large for GPU memory, enabling automatic tiling if not specified. ```python # We load the data we want to process. img = imread('data/flywing.tif') # Here we process the data. # The 'n_tiles' parameter can be used if images are too big for the GPU memory. # If we do not provide the 'n_tiles' parameter the system will automatically try to find an appropriate tiling. pred = model.predict(img, axes='ZYX', n_tiles=(2,4,4)) ``` -------------------------------- ### Load CUDA Module Source: https://github.com/juglab/n2v/wiki/mpi-cluster Load the CUDA module required for GPU acceleration. Ensure the version matches the cluster's availability. ```bash module load cuda/9.0.176 ``` -------------------------------- ### Load Trained Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/02_prediction.ipynb Instantiates the N2V model object using a previously saved configuration. ```python # A previously trained model is loaded by creating a new N2V-object without providing a 'config'. model_name = 'n2v_2D_sem' basedir = 'models' model = N2V(config=None, name=model_name, basedir=basedir) ``` -------------------------------- ### Import Dependencies for N2V Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_convallaria/01_training.ipynb Imports necessary libraries for N2V, including data manipulation, model configuration, and plotting utilities. Includes SSL context adjustment for downloads. ```python # We import all our dependencies. from n2v.models import N2VConfig, N2V import numpy as np from csbdeep.utils import plot_history from n2v.utils.n2v_utils import manipulate_val_data from n2v.internals.N2V_DataGenerator import N2V_DataGenerator from matplotlib import pyplot as plt import urllib import os import zipfile from tifffile import imread import ssl ssl._create_default_https_context = ssl._create_unverified_context ``` -------------------------------- ### Export Model to BioImage Model Zoo Source: https://context7.com/juglab/n2v/llms.txt Prepares a trained model for export to compatible tools like ImageJ/Fiji. ```python from n2v.models import N2V import numpy as np # Load or train a model model = N2V(config=None, name='n2v_2D_example', basedir='models') ``` -------------------------------- ### Configure N2V Model Source: https://context7.com/juglab/n2v/llms.txt Define the N2VConfig object to set architecture, training, and N2V-specific parameters. Statistics like mean and standard deviation are computed automatically from the provided training data X. ```python config = N2VConfig( X, # Training data for computing statistics # U-Net architecture parameters unet_kern_size=3, # Convolution kernel size unet_n_depth=2, # Number of resolution levels unet_n_first=32, # Filters in first layer (doubles each level) unet_residual=False, # Use residual learning unet_last_activation='linear', # Output activation function batch_norm=True, # Use batch normalization # Training parameters train_epochs=100, # Number of training epochs train_steps_per_epoch=400, # Steps per epoch train_batch_size=128, # Batch size train_learning_rate=0.0004, # Learning rate train_loss='mse', # Loss function: 'mse' or 'mae' train_tensorboard=True, # Enable TensorBoard logging train_checkpoint='weights_best.h5', # Save best weights train_reduce_lr={'factor': 0.5, 'patience': 10}, # LR reduction on plateau # N2V-specific parameters n2v_perc_pix=0.198, # Percentage of pixels to manipulate (~8 per 64x64 patch) n2v_patch_shape=(64, 64), # Training patch shape n2v_manipulator='uniform_withCP', # Pixel replacement strategy n2v_neighborhood_radius=5, # Neighborhood size for replacement # Channel handling single_net_per_channel=True, # Separate U-Net per channel (prevents bleedthrough) # StructN2V for structured noise (optional) structN2Vmask=None # e.g., [[0,1,1,1,1,1,1,1,1,1,0]] for horizontal stripes ) # View all configuration parameters print(vars(config)) ``` -------------------------------- ### Load Images from Directory Source: https://github.com/juglab/n2v/blob/main/examples/3D/01_training.ipynb Loads all '.tif' files from a specified directory into a list of NumPy arrays. The 'dims' parameter specifies the dimension order (e.g., 'ZYX'). The loaded images will have extra dimensions added for potential stacks and color channels. ```python # We will load all the '.tif' files from the 'data' directory. In our case it is only one. # The function will return a list of images (numpy arrays). # In the 'dims' parameter we specify the order of dimensions in the image files we are reading. imgs = datagen.load_imgs_from_directory(directory = "data/", dims='ZYX') # Let's look at the shape of the image print(imgs[0].shape) # The function automatically added two extra dimension to the images: # One at the front is used to hold a potential stack of images such as a movie. # One at the end could hold color channels such as RGB. ``` -------------------------------- ### Clone n2v Repository Source: https://github.com/juglab/n2v/blob/main/README.md Clones the Noise2Void (n2v) repository from GitHub. This is useful if you want to access the latest development version or modify the code. ```bash git clone https://github.com/juglab/n2v.git ``` -------------------------------- ### Compute PSNR with Test-Time Augmentation Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb Iterates through ground truth and test data, predicts denoised images using the loaded model with test-time augmentation (tta=True), and calculates the PSNR for each pair. The average PSNR is then printed. ```python pred = [] psnrs = [] for gt, img in zip(groundtruth_data, test_data): p_ = model.predict(img.astype(np.float32), 'YX', tta=True); pred.append(p_) psnrs.append(PSNR(gt, p_)) psnrs = np.array(psnrs) print("PSNR (with test-time augmentation):", np.round(np.mean(psnrs), 2)) ``` -------------------------------- ### Display Loaded Images Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Displays the first two loaded images using matplotlib. The extra dimensions added during loading are removed to allow for 2D image display. Uses the 'magma' colormap. ```python # Lets' look at the images. # We have to remove the added extra dimensions to display them as 2D images. plt.imshow(imgs[0][0,...,0], cmap='magma') plt.show() plt.imshow(imgs[1][0,...,0], cmap='magma') plt.show() ``` -------------------------------- ### Display Image Sample Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/01_training.ipynb Displays a portion of the loaded image using Matplotlib. The extra dimension is removed to allow for 2D display, and a specific slice (1000:3000) of the image is shown. ```python # Let's look at the image. # We have to remove the added extra dimension to display it as 2D image. plt.figure(figsize=(32,16)) plt.imshow(imgs[0][0,:,1000:3000,...]) plt.show() ``` -------------------------------- ### Train Noise2Void Model Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Initiates the training process for the N2V model using the training and validation data. The training history, including losses, is returned. Ensure the model is initialized and data (X, X_val) is prepared. ```python # We are ready to start training now. history = model.train(X, X_val) ``` -------------------------------- ### Generate Training and Validation Patches Source: https://github.com/juglab/n2v/blob/main/examples/2D/structN2V_2D_convallaria/01_training.ipynb Splits the image data into training and validation sets based on pixel coordinates and generates non-overlapping patches of a specified shape (96x96) for both sets. ```python # split up image into little non-overlapping patches for training. # y<832 (top of image) is training, y>=832 (bottom of image) is validation imgs_train = [imgs[0][:,:832]] X = datagen.generate_patches_from_list(imgs_train,shape=(96,96)) imgs_vali = [imgs[0][:,832:]] X_val = datagen.generate_patches_from_list(imgs_vali,shape=(96,96)) # Patches are created so they do not overlap. ``` -------------------------------- ### Train N2V Model Source: https://context7.com/juglab/n2v/llms.txt Prepare data using N2V_DataGenerator, initialize the N2V model with a configuration, and train it. The training process returns a Keras History object useful for visualizing loss curves. ```python from n2v.models import N2VConfig, N2V from n2v.internals.N2V_DataGenerator import N2V_DataGenerator # Prepare data datagen = N2V_DataGenerator() imgs = datagen.load_imgs_from_directory(directory="data/", dims='YX') X = datagen.generate_patches_from_list(imgs[:1], shape=(64, 64)) X_val = datagen.generate_patches_from_list(imgs[1:], shape=(64, 64)) # Create configuration config = N2VConfig( X, unet_kern_size=3, train_steps_per_epoch=int(X.shape[0] / 128), train_epochs=100, 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 ) # Create model model_name = 'n2v_2D_example' basedir = 'models' model = N2V(config, model_name, basedir=basedir) # Train the model # Returns Keras History object for plotting loss curves history = model.train(X, X_val) # Plot training history from csbdeep.utils import plot_history import matplotlib.pyplot as plt plt.figure(figsize=(16, 5)) plot_history(history, ['loss', 'val_loss']) plt.show() ``` -------------------------------- ### Export N2V Model to BioImage ModelZoo Format Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_SEM/01_training.ipynb Use this method to package a trained model with metadata and test data for the BioImage ModelZoo. Ensure the test image and axes parameters match the model's training configuration. ```python model.export_TF(name='Noise2Void - 2D SEM Example', description='This is the 2D Noise2Void example trained on SEM data in python.', authors=["Tim-Oliver Buchholz", "Alexander Krull", "Florian Jug"], test_img=X_val[0,...], axes='YXC', patch_shape=patch_shape) ``` -------------------------------- ### Configure Pixel Manipulators Source: https://context7.com/juglab/n2v/llms.txt Defines how blind-spot pixel values are replaced during training. Choose based on the noise characteristics of the dataset. ```python from n2v.models import N2VConfig # Available manipulators and their behavior: # 'uniform_withCP' (default): Random pixel from uniform neighborhood including center config = N2VConfig(X, n2v_manipulator='uniform_withCP', n2v_neighborhood_radius=5) # 'uniform_withoutCP': Random pixel from neighborhood excluding center pixel config = N2VConfig(X, n2v_manipulator='uniform_withoutCP', n2v_neighborhood_radius=5) # 'normal_withoutCP': Sample from neighborhood with normal distribution (no center) config = N2VConfig(X, n2v_manipulator='normal_withoutCP', n2v_neighborhood_radius=5) # 'normal_additive': Add Gaussian noise to original pixel value # n2v_neighborhood_radius acts as sigma for the Gaussian config = N2VConfig(X, n2v_manipulator='normal_additive', n2v_neighborhood_radius=5) # 'normal_fitted': Sample from Gaussian fitted to local neighborhood statistics config = N2VConfig(X, n2v_manipulator='normal_fitted', n2v_neighborhood_radius=5) # 'mean': Replace with mean of neighborhood config = N2VConfig(X, n2v_manipulator='mean', n2v_neighborhood_radius=5) # 'median': Replace with median of neighborhood config = N2VConfig(X, n2v_manipulator='median', n2v_neighborhood_radius=5) # 'identity': No manipulation (for debugging/comparison) config = N2VConfig(X, n2v_manipulator='identity', n2v_neighborhood_radius=5) ``` -------------------------------- ### Perform Image Prediction Source: https://github.com/juglab/n2v/blob/main/examples/2D/denoising2D_RGB/02_prediction.ipynb Reads an input image and runs the denoising prediction using the loaded model. ```python # We read the image we want to process and get rid of the Alpha channel. img = imread('data/longBeach.png')[...,:3] # Here we process the image. pred = model.predict(img, axes='YXC') ```