.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_apply_segm.py: Example 04: Apply trained network for segmentation ================================================== This script applies a trained MS-D network for segmentation (i.e. labeling) Run generatedata.py first to generate required training data and train_segm.py to train a network. .. code-block:: default # Import code import msdnet import glob import tifffile import os import numpy as np # Make folder for output os.makedirs('results', exist_ok=True) # Load network from file n = msdnet.network.SegmentationMSDNet.from_file('segm_params.h5', gpu=True) # Process all test images flsin = sorted(glob.glob('test/noisy/*.tiff')) for i in range(len(flsin)): # Create datapoint with only input image d = msdnet.data.ImageFileDataPoint(flsin[i]) # Compute network output output = n.forward(d.input) # Save labels with maximum probability to file (i.e. prediceted labels for each pixel) tifffile.imsave('results/segm_label_{:05d}.tiff'.format(i), np.argmax(output,0).astype(np.uint8)) # Save probability map of a single channel (here, channel 2) to file tifffile.imsave('results/segm_prob_lab2_{:05d}.tiff'.format(i), output[2]) .. _sphx_glr_download_auto_examples_apply_segm.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: apply_segm.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: apply_segm.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_