This notebook demonstrates applying a CARE model for a combined denoising and upsampling 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
.
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 UpsamplingCARE
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/synthetic_upsampling.zip',
targetdir = 'data',
)
Files found, nothing to download. data: - synthetic_upsampling - synthetic_upsampling/test_stacks_sub_4 - synthetic_upsampling/test_stacks_sub_4/stack_low_sub_4_03.tif - synthetic_upsampling/training_stacks - synthetic_upsampling/training_stacks/high - synthetic_upsampling/training_stacks/high/stack_01.tif - synthetic_upsampling/training_stacks/high/stack_02.tif - synthetic_upsampling/training_stacks/high/stack_00.tif - synthetic_upsampling/training_stacks/low - synthetic_upsampling/training_stacks/low/stack_01.tif - synthetic_upsampling/training_stacks/low/stack_02.tif - synthetic_upsampling/training_stacks/low/stack_00.tif
We plot XY and ZY slices of the stack and define the image axes and subsampling factor, which will be needed later for prediction.
x = imread('data/synthetic_upsampling/test_stacks_sub_4/stack_low_sub_4_03.tif')
axes = 'ZYX'
subsample = 4
print('image size =', x.shape)
print('image axes =', axes)
print('subsample factor =', subsample)
plt.figure(figsize=(12,12))
plt.imshow(x[20],cmap='magma')
plt.title('XY slice')
plt.axis('off')
plt.figure(figsize=(12,12))
plt.imshow(x[...,20],aspect=subsample,cmap='magma')
plt.title('ZY slice')
plt.axis('off')
None;
image size = (32, 512, 512) image axes = ZYX subsample factor = 4
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 UpsamplingCARE
is initialized with config=None
.
model = UpsamplingCARE(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).
We directly increase n_tiles
, since this is a relatively large image stack.
Important: You need to supply the subsampling factor, which must be the same as used during training data generation.
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, subsample, n_tiles=(2,2,2))
print('input size =', x.shape)
print('output size =', restored.shape)
print()
100%|██████████| 8/8 [00:04<00:00, 1.93it/s]
input size = (32, 512, 512) output size = (128, 512, 512) CPU times: user 16.1 s, sys: 1.17 s, total: 17.3 s Wall time: 16.7 s
Save the reconstructed 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)
save_tiff_imagej_compatible('results/%s_restored_stack_low_sub_4_03.tif' % model.name, restored, axes)
plt.figure(figsize=(12,12))
plt.imshow(restored[subsample*20],cmap='magma')
plt.title('XY slice')
plt.axis('off')
plt.figure(figsize=(12,12))
plt.imshow(restored[...,20],cmap='magma')
plt.title('ZY slice')
plt.axis('off')
None;