Skip to main content

Python package to export TensorFlow models as DeepImageJ bundled models

Project description

PyDeepImageJ

GitHub minimal Python version

Python code to export trained models and read them in Fiji & ImageJ using DeepImageJ plugin

  • Create a configuration class in python with all the information about the trained model, needed for its correct use in Fiji & ImageJ.
  • Include the metadata of an example image.
  • Include all expected results and needed pre / post-processing routines.
  • See DeepImageJ webpage for more information.

Installation

Requirements

  • PyDeepImageJ requires Python 3 to run.
  • TensorFlow: It runs using the local installation of TensorFlow, i.e. the one corresponding to the trained model. However, DeepImageJ is only compatible with TensorFlow versions <= 2.2.1.

To install PyDeepImageJ either clone this repository or use PyPi via pip:

$ pip install pydeepimagej

or

$ git clone https://github.com/deepimagej/pydeepimagej.git
$ cd pydeepimagej
$ python setup.py install

Example of how to use it

Let model be a Keras or TensorFlow trained model. Initialize the configuration class with the trained model model

dij_config = DeepImageJConfig(model)

Update model information

dij_config.Name = 'My trained model v0.1'
dij_config.Authors = 'First author; Secon Author; Third Author'
dij_config.Credits = 'Institution 1; Institution 2; Institution 3'
dij_config.URL        = 'https://github.com/user/your_model'
dij_config.Version    = '0.1'
dij_config.References = 'C. García-López-de-Haro et al., bioRxiv 2019'
dij_config.Date       = 'September-2020'

Prepare an ImageJ pre/post-processing macro. You may need to preprocess the input image before the inference. Some ImageJ macro routines can be downloaded from here and included in the model specifications. Note that ImageJ macros are text files so it is easy to modify them inside a Python script (see an example):

path_preprocessing = "PercentileNormalization.ijm"`
# Download the macro file
urllib.request.urlretrieve("https://raw.githubusercontent.com/deepimagej/imagej-macros/master/PercentileNormalization.ijm", path_preprocessing )
# Include it in the configuration class
dij_config.add_preprocessing(path_preprocessing, "preprocessing")

The same holds for the postprocessing.

path_postprocessing = "8bitBinarize.ijm"
urllib.request.urlretrieve("https://raw.githubusercontent.com/deepimagej/imagej-macros/master/8bitBinarize.ijm", path_postprocessing )
# Include the info about the postprocessing 
dij_config.add_postprocessing(path_postprocessing,"postprocessing")

It is even possible to include different pre/post-processing routines in the same model as long as their names always start with "preprocessing" or "postprocessing" respectively:

path_second_postprocessing = "./folder/another_macro.ijm"
dij_config.add_postprocessing(path_second_postprocessing, "postprocessing_2")

Add information about the example image. Let test_img be an example image to test the model inference and test_prediction be the resulting image after the post-processing. It is possible to export the trained model with these two, so an end user can see an example. PixelSize should be given in microns (µm).

dij_config.add_test_info(test_img, test_prediction, PixelSize)

EXPORT THE MODEL

deepimagej_model_path = './my_trained_model_deepimagej'
dij_config.export_model(model, deepimagej_model_path)

Additional commands

Change one line in an ImageJ macro

# Download the macro file
path_postprocessing = "8bitBinarize.ijm"
urllib.request.urlretrieve("https://raw.githubusercontent.com/deepimagej/imagej-macros/master/8bitBinarize.ijm", path_postprocessing )
# Modify the threshold in the macro to the chosen threshold
ijmacro = open(path_postprocessing,"r")  
list_of_lines = ijmacro. readlines()
# Line 21 is the one corresponding to the optimal threshold
list_of_lines[21] = "optimalThreshold = {}\n".format(128)
ijmacro.close()
ijmacro = open(path_postprocessing,"w")  
ijmacro. writelines(list_of_lines)
ijmacro. close()

Estimation of the step size for the shape of the input image. If the model is an encoder-decoder with skip connections, and the input shape of your trained model is not fixed (i.e. [None, None, 1] ), the input shape still needs to fit some requirements. You can caluculate it knowing the number of poolings in the encoder path of the network:

import numpy as np
pooling_steps = 0
for keras_layer in model.layers:
    if keras_layer.name.startswith('max') or "pool" in keras_layer.name:
      pooling_steps += 1
dij_config.MinimumSize = np.str(2**(pooling_steps))

Todo list

  • Write bioimage.IO config.yaml class

Reference:

E. Gomez-de-Mariscal, C. Garcia-Lopez-de-Haro, L. Donati, M. Unser, A. Munoz-Barrutia, D. Sage. DeepImageJ: A user-friendly plugin to run deep learning models in ImageJ, bioRxiv 2019 DOI: https://doi.org/10.1101/799270

  • Bioengineering and Aerospace Engineering Department, Universidad Carlos III de Madrid, Spain
  • Biomedical Imaging Group, Ecole polytechnique federale de Lausanne (EPFL), Switzerland

Corresponding authors: mamunozb@ing.uc3m.es, daniel.sage@epfl.ch Copyright © 2019. Universidad Carlos III, Madrid; Spain and EPFL, Lausanne, Switzerland.

License

BSD 2-Clause License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydeepimagej-1.0.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydeepimagej-1.0.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pydeepimagej-1.0.0.tar.gz.

File metadata

  • Download URL: pydeepimagej-1.0.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.5.5

File hashes

Hashes for pydeepimagej-1.0.0.tar.gz
Algorithm Hash digest
SHA256 801779f066407da979a21155980d6628cb481596dd869d8a65f3ebdc0cddad8b
MD5 bce334e12b65f7e8d2b847ed8508f4cd
BLAKE2b-256 8ab14183c38991511a1a376c192f520f788dd12d5fbcc27d494361f94d27a759

See more details on using hashes here.

File details

Details for the file pydeepimagej-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pydeepimagej-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.5.5

File hashes

Hashes for pydeepimagej-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa10009b1fac1d1115433f55c8c8280f6113326b9a597170a3da1a066934506c
MD5 c68d7fd06771b134eebeef154b7b0fb4
BLAKE2b-256 37e721e232829d664f898a5dd017b74fe4cf5e6a5e7f95cf5e79301d4d49b3d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page