<jupyter_start><jupyter_text>Demo: Apply trained CARE model for joint denoising and surface projection of *Drosophila melanogaster* wingThis notebook demonstrates applying a CARE model for a denoising+projection task, assuming that training was already completed via [2_training.ipynb](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/.<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Download example dataThe example data (also for testing) should have been downloaded in [1_datagen.ipynb](1_datagen.ipynb).  Just in case, we will download it here again if it's not already present.<jupyter_code>download_and_extract_zip_file (
    url       = 'http://csbdeep.bioimagecomputing.com/example_data/flywing.zip',
    targetdir = 'data',
)<jupyter_output><empty_output><jupyter_text>Raw low-SNR 3D stack and associated high-SNR 2D surface projectionPlot the image pair and define its image axes, which will be needed later for CARE prediction.<jupyter_code>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);<jupyter_output><empty_output><jupyter_text>CARE modelLoad 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`.<jupyter_code>model = ProjectionCARE(config=None, name='my_model', basedir='models')<jupyter_output><empty_output><jupyter_text>Apply CARE network to raw imagePredict the restored image (image will be successively split into smaller tiles if there are memory issues).<jupyter_code>%%time
restored = model.predict(x, axes)<jupyter_output><empty_output><jupyter_text>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).<jupyter_code>%%time
restored = model.predict(x, axes, n_tiles=(1,4,4))<jupyter_output><empty_output><jupyter_text>Save restored imageSave the restored image stack as a ImageJ-compatible TIFF image, i.e. the image can be opened in ImageJ/Fiji with correct axes semantics.<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Raw low/high-SNR pair and denoised+projected image via CARE networkPlot the test stack pair and the predicted restored stack (middle).<jupyter_code>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);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.095947265625, 'fraction_numerical': 0.0078125, 'mean_word_length': 6.573012939001848, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '341090', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1250, 'n_words': 381, 'path': 'examples/projection/3_prediction.ipynb', 'repo_name': 'uschmidt83/CSBDeep', 'size': '4096'}