|
| 1 | +# Patch Filtering |
| 2 | + |
| 3 | +Patch filtering is useful if your data contains large areas with no signal. These areas can be filtered from the training process which can speed up the convergence of the model. |
| 4 | + |
| 5 | +### How does it work? |
| 6 | + |
| 7 | +CAREamics will perform a first pass through all the data before training starts to determine regions of background and regions of signal. Background regions will not be completely be excluded from training, instead their probability of being selected during an epoch will be reduced. |
| 8 | + |
| 9 | +There are two options for the filtering function, either: |
| 10 | + |
| 11 | +- [pre-computed masks](#pre-computed-masks) can be provided, or |
| 12 | +- one of the [built-in filtering functions](#filtering-functions) can be selected and parametrized. |
| 13 | + |
| 14 | +## Pre-computed masks |
| 15 | + |
| 16 | +Using precomputed masks is relatively simple, the masks in the same format as the data — either as an array or saved in files — can be provided during training. |
| 17 | + |
| 18 | +=== "Noise2Void" |
| 19 | + |
| 20 | + ```python title="Specifying a mask for Noise2Void training" |
| 21 | + --8<-- "current/careamist_training.py:train_n2v_mask" |
| 22 | + ``` |
| 23 | + |
| 24 | + 1. The mask is passed alongside the data. |
| 25 | + |
| 26 | +=== "CARE/N2N" |
| 27 | + |
| 28 | + ```python title="Specifying a mask for CARE training" |
| 29 | + --8<-- "current/careamist_training.py:train_care_mask" |
| 30 | + ``` |
| 31 | + |
| 32 | + 1. The mask is passed alongside the data. |
| 33 | + |
| 34 | +!!! note "What is masked?" |
| 35 | + |
| 36 | + The mask is a binary set of images with the same size as the training data and |
| 37 | + should have value `1` for pixels that should be included in the training and `0` |
| 38 | + for pixels that should be excluded. |
| 39 | + |
| 40 | +## Filtering functions |
| 41 | + |
| 42 | +CAREamics has 3 built-in filtering functions, which work by filtering out patches using thresholds on different metrics: |
| 43 | + |
| 44 | +- [`MaxPatchFilter`][careamics.dataset.patch_filter.MaxPatchFilter]: that filters the data based on the max value of each region. |
| 45 | +- [`MeanStdPatchFilter`][careamics.dataset.patch_filter.MeanStdPatchFilter]: that filters the data based on the mean and optionally the standard deviation of regions of the data. |
| 46 | +- [`ShannonPatchFilter`][careamics.dataset.patch_filter.ShannonPatchFilter]: that filters the data based on the shannon entropy of regions of the data. |
| 47 | + |
| 48 | +!!! note "Multi-channel data" |
| 49 | + |
| 50 | + For multi-channel data the filtering function is only applied to a single channel of your choosing. |
| 51 | + |
| 52 | +### Finding appropriate thresholds |
| 53 | + |
| 54 | +Finding appropriate thresholds requires manually inspecting some examples. The patch filter classes provide `filter_map` and `plot_filter_map` which can be used to visualize at what threshold a region will be considered background. |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +For demonstration purposes we will use the Hagen dataset which is used in other CAREamics examples; however, it doesn't have enough background area to typically require patch filtering. |
| 59 | + |
| 60 | +```python title="Download the data" |
| 61 | +--8<-- "tutorials/patch_filtering.py:download-data" |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +Now we inspect the filter maps to decide on a patch filtering function and threshold. For data with multiple samples it is generally a good idea to inspect the filter maps of a few different samples; and for 3D data one should look at multiple z-slices. |
| 67 | + |
| 68 | +```python title="Plot Filter Maps" |
| 69 | +--8<-- "tutorials/patch_filtering.py:filter-maps" |
| 70 | +``` |
| 71 | + |
| 72 | +!!! info "3D data" |
| 73 | + |
| 74 | + For 3D data `plot_filter_map` has the `z_idx` argument to control which z-slice is displayed. |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +We will choose the shannon patch filter, with a threshold of 7.5, and to confirm that this is a good choice we will look at the resulting mask, by using the `ShannonPatchFilter.apply_filter` method. |
| 85 | + |
| 86 | +```python title="Plot Filter Maps" |
| 87 | +--8<-- "tutorials/patch_filtering.py:mask" |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +### Training |
| 93 | + |
| 94 | +Next, we have to build the configuration. |
| 95 | + |
| 96 | +Each of the patch filter classes has a corresponding configuration class, where the threshold parameters can be set: |
| 97 | + |
| 98 | +- [`MaxPatchFilterConfig`][careamics.config.MaxPatchFilterConfig] |
| 99 | +- [`MeanStdPatchFilterConfig`][careamics.config.MeanStdPatchFilterConfig] |
| 100 | +- [`ShannonPatchFilterConfig`][careamics.config.ShannonPatchFilterConfig] |
| 101 | + |
| 102 | +We will create the configuration using `create_advanced_n2v_config` and passing `ShannonPatchFilterConfig` with our selected threshold to the `patch_filter_config` argument. |
| 103 | + |
| 104 | + |
| 105 | +```python title="Create Config and Train" |
| 106 | +--8<-- "tutorials/patch_filtering.py:config" |
| 107 | +``` |
| 108 | + |
| 109 | +1. Using shannon filtering with a threshold of 7.5 |
| 110 | + |
| 111 | +!!! info "Multi-channel data" |
| 112 | + |
| 113 | + For multi-channel data set the `ref_channel` parameter in the patch filter configs to the index of your desired channel. |
| 114 | + |
| 115 | +!!! info "Other algorithms" |
| 116 | + |
| 117 | + The configuration factory functions for other algorithms, such as CARE and N2N also have a `patch_filter_config` argument. |
| 118 | + |
| 119 | +!!! success Success |
| 120 | + |
| 121 | + If patch filtering was correctly applied during training, you should see a log similar to: |
| 122 | + |
| 123 | + ``` |
| 124 | + Filtering background patches with filtering function shannon: 100%|██████████| 79/79 [00:06<00:00, 12.79it/s] |
| 125 | + Found 6345 background regions. Number of patches has been reduced to 14553 from 20224. |
| 126 | + ``` |
0 commit comments