This notebook demonstrates applying a CARE model for a denoising+projection task, assuming that training was already completed via 2_training.ipynb.
The trained model is assumed to be located in the folder models
with the name my_model
.
Note: The CARE model is here applied to an image that the model was also trained on.
Of course, in practice one would typically use it to restore images that the model hasn't seen during training.
More documentation is available at http://csbdeep.bioimagecomputing.com/doc/.
from __future__ import print_function, unicode_literals, absolute_import, division
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
from tifffile import imread
from csbdeep.utils import Path, download_and_extract_zip_file, plot_some
from csbdeep.io import save_tiff_imagej_compatible
from csbdeep.models import ProjectionCARE
The example data (also for testing) should have been downloaded in 1_datagen.ipynb.
Just in case, we will download it here again if it's not already present.
download_and_extract_zip_file (
url = 'http://csbdeep.bioimagecomputing.com/example_data/flywing.zip',
targetdir = 'data',
)
Files found, nothing to download. data: - flywing - flywing/low_C2 - flywing/low_C2/exposures_1_P06.tif - flywing/low_C2/exposures_1_P04.tif - flywing/low_C0 - flywing/low_C0/session_4_P18.tif - flywing/low_C0/session_5_P21.tif - flywing/low_C0/session_5_P23.tif - flywing/low_C0/session_6_P08.tif - flywing/low_C0/session_5_P15.tif - flywing/low_C0/session_6_P11.tif - flywing/low_C0/session_6_P02.tif - flywing/low_C0/session_6_P20.tif - flywing/low_C0/session_5_P17.tif - flywing/low_C0/session_6_P13.tif - flywing/low_C0/session_5_P11.tif - flywing/low_C0/session_5_P13.tif - flywing/low_C0/session_5_P20.tif - flywing/low_C0/session_4_P14.tif - flywing/low_C0/session_4_P12.tif - flywing/low_C0/session_6_P17.tif - flywing/low_C0/session_4_P20.tif - flywing/low_C0/session_4_P24.tif - flywing/low_C0/session_6_P05.tif - flywing/low_C0/session_6_P14.tif - flywing/low_C0/session_4_P08.tif - flywing/low_C0/session_5_P05.tif - flywing/low_C0/session_5_P14.tif - flywing/low_C0/session_5_P08.tif - flywing/low_C3 - flywing/low_C3/session_6_P19.tif - flywing/low_C3/session_7_P23.tif - flywing/low_C3/session_7_P03.tif - flywing/low_C3/session_6_P11.tif - flywing/low_C3/session_7_P17.tif - flywing/low_C3/session_6_P15.tif - flywing/low_C3/session_7_P13.tif - flywing/low_C3/session_6_P13.tif - flywing/low_C3/session_7_P19.tif - flywing/low_C3/session_6_P09.tif - flywing/low_C3/session_6_P21.tif - flywing/low_C3/session_7_P21.tif - flywing/low_C3/session_6_P07.tif - flywing/low_C3/session_7_P15.tif - flywing/low_C3/session_7_P07.tif - flywing/low_C3/session_6_P17.tif - flywing/low_C3/session_7_P11.tif - flywing/low_C3/session_7_P01.tif - flywing/low_C3/session_6_P05.tif - flywing/low_C3/session_6_P03.tif - flywing/low_C3/session_7_P09.tif - flywing/low_C3/session_7_P05.tif - flywing/low_C3/session_6_P23.tif - flywing/GT - flywing/GT/session_4_P18.tif - flywing/GT/session_4_P22.tif - flywing/GT/session_5_P21.tif - flywing/GT/session_6_P19.tif - flywing/GT/exposures_1_P06.tif - flywing/GT/session_5_P23.tif - flywing/GT/session_7_P23.tif - flywing/GT/session_7_P03.tif - flywing/GT/session_6_P08.tif - flywing/GT/session_5_P15.tif - flywing/GT/session_5_P09.tif - flywing/GT/session_6_P11.tif - flywing/GT/session_6_P02.tif - flywing/GT/session_5_P03.tif - flywing/GT/session_7_P17.tif - flywing/GT/session_6_P15.tif - flywing/GT/session_6_P20.tif - flywing/GT/session_7_P13.tif - flywing/GT/session_5_P17.tif - flywing/GT/session_4_P10.tif - flywing/GT/session_6_P13.tif - flywing/GT/session_4_P04.tif - flywing/GT/session_7_P19.tif - flywing/GT/session_5_P02.tif - flywing/GT/session_6_P09.tif - flywing/GT/session_5_P11.tif - flywing/GT/session_6_P21.tif - flywing/GT/session_7_P21.tif - flywing/GT/session_5_P19.tif - flywing/GT/session_6_P07.tif - flywing/GT/session_4_P06.tif - flywing/GT/session_5_P13.tif - flywing/GT/session_5_P20.tif - flywing/GT/session_7_P15.tif - flywing/GT/exposures_1_P04.tif - flywing/GT/session_4_P02.tif - flywing/GT/session_4_P14.tif - flywing/GT/session_7_P07.tif - flywing/GT/session_4_P12.tif - flywing/GT/session_6_P17.tif - flywing/GT/session_4_P16.tif - flywing/GT/session_5_P07.tif - flywing/GT/session_4_P20.tif - flywing/GT/session_7_P11.tif - flywing/GT/session_7_P01.tif - flywing/GT/session_4_P24.tif - flywing/GT/session_6_P05.tif - flywing/GT/session_5_P01.tif - flywing/GT/session_6_P03.tif - flywing/GT/session_6_P14.tif - flywing/GT/session_7_P09.tif - flywing/GT/session_4_P08.tif - flywing/GT/session_7_P05.tif - flywing/GT/session_6_P23.tif - flywing/GT/session_5_P05.tif - flywing/GT/session_5_P14.tif - flywing/GT/session_5_P08.tif
Plot the image pair and define its image axes, which will be needed later for CARE prediction.
y = imread('data/flywing/GT/session_7_P17.tif')
x = imread('data/flywing/low_C3/session_7_P17.tif')
axes = 'ZYX'
print('source image size =', x.shape)
print('source image axes =', axes)
print('target image size =', y.shape)
plt.figure(figsize=(16,10))
plot_some(np.stack([x,np.broadcast_to(y,x.shape)]),
title_list=[['low-SNR stack (maximum projection)','high-SNR (surface extracted with PreMosa)']],
pmin=2,pmax=99.8);
source image size = (50, 520, 692) source image axes = ZYX target image size = (520, 692)
Load trained model (located in base directory models
with name my_model
) from disk.
The configuration was saved during training and is automatically loaded when ProjectionCARE
is initialized with config=None
.
model = ProjectionCARE(config=None, name='my_model', basedir='models')
Loading network weights from 'weights_best.h5'.
Predict the restored image (image will be successively split into smaller tiles if there are memory issues).
%%time
restored = model.predict(x, axes)
CPU times: user 4.39 s, sys: 475 ms, total: 4.86 s Wall time: 4.78 s
Alternatively, one can directly set n_tiles
to avoid the time overhead from multiple retries in case of memory issues.
Note: Out of memory problems during model.predict
can also indicate that the GPU is used by another process. In particular, shut down the training notebook before running the prediction (you may need to restart this notebook).
%%time
restored = model.predict(x, axes, n_tiles=(1,4,4))
100%|██████████| 16/16 [00:01<00:00, 10.00it/s]
CPU times: user 3.41 s, sys: 252 ms, total: 3.67 s Wall time: 3.39 s
Save the restored image stack as a ImageJ-compatible TIFF image, i.e. the image can be opened in ImageJ/Fiji with correct axes semantics.
Path('results').mkdir(exist_ok=True)
axes_restored = axes.replace(model.proj_params.axis,'')
save_tiff_imagej_compatible('results/%s_denoising+projection_example.tif' % model.name, restored, axes_restored)
Plot the test stack pair and the predicted restored stack (middle).
plt.figure(figsize=(16,10))
plot_some(np.stack([x,np.broadcast_to(restored,x.shape),np.broadcast_to(y,x.shape)]),
title_list=[['low-SNR stack (maximum projection)','CARE','high-SNR (surface extracted with PreMosa)']],
pmin=2,pmax=99.8);