A conversion tool for TensorFlow ANNs to CZModel
Project description
ANN to CZModel conversion toolbox
This project provides simple-to-use conversion tools to generate a CZModel file from a TensorFlow model that resides in memory or on disk to be usable in the ZEN Intellesis module.
System setup
The current version of this toolbox only requires a fresh Python 3.x installation. It was tested with Python 3.7 on Windows.
Model conversion
The toolbox provides a convert
module that features all supported conversion strategies. It currently supports
converting Keras models in memory or stored on disk with a corresponding metadata JSON file.
Keras models in memory
The toolbox also provides functionality that can be imported e.g. in the training script used to fit a Keras model. The function is accessible by running:
from czmodel.convert import convert_from_model_spec
It accepts a tensorflow.keras.Model
that will be exported to SavedModel
format and at the same time wrapped into a CZModel file to be compatible with the Intellesis infrastructure.
To provide the meta data, the toolbox provides a ModelSpec class that must be filled with the model and a ModelMetadata
instance containing the required information described in the [specification](TODO: insert final link to AnnSpec#model-metadata)
file.
A CZModel can be created from a Keras model with the following three steps.
####Creating a model meta data class
To export a CZModel file several meta information is needed that must be provided through a ModelMetadata
instance.
from czmodel.model_metadata import ModelMetadata
model_metadata = ModelMetadata.from_params(name='DNNModelFromKeras',
color_handling='ConvertToMonochrome',
pixel_type='Gray16',
classes=["Background", "Interesting Object", "Foreground"],
border_size=90,
license_file="C:\\some\\path\\to\\a\\LICENSE.txt")
####Creating a model specification The model and its corresponding metadata are now wrapped into a ModelSpec object.
from czmodel.model_metadata import ModelSpec
model_spec = ModelSpec(model=model, model_metadata=model_metadata)
####Converting the model The actual model conversion is finally performed with the ModelSpec object and the output path and name of the CZModel file.
from czmodel.convert import convert_from_model_spec
convert_from_model_spec(model_spec=model_spec, output_path='some/path', output_name='some_file_name')
Exported TensorFlow models
To convert an exported TensorFlow model the model and the provided meta data need to comply with (TODO: insert final link to AnnSpec)
The actual conversion is triggered by either calling:
from czmodel.convert import convert_from_json_spec
convert_from_json_spec('Path to JSON file', 'Output path', 'Model Name')
or by using the command line interface of the convert
script:
convert.py path/to/model_spec.json output/path/ output_name
Addding pre-processing layers
Both, convert_from_json_spec
and convert_from_model_spec
additionally allow specifying the following optional parameters:
spatial_dims
: Set new spatial dimensions for the new input node of the model. This parameter is expected to contain the new height and width in that order. Note: The spatial input dimensions can only be changed in ANN architectures that are invariant to the spatial dimensions of the input, e.g. FCNs.preprocessing
: One or more pre-processing layers that will be prepended to the deployed model. A pre-processing layer must be derived from thetensorflow.keras.layers.Layer
class.
While ANN models are often trained on images in RGB(A) space, the ZEN infrastructure requires models inside a CZModel to expect inputs in BGR(A) color space. This toolbox offers pre-processing layers to convert the color space before passing the input to the model to be actually deployed. The followig code shows how to add a RGB to BGR conversion layer to a model and set its spatial input dimensions to 512x512.
from czmodel.util.preprocessing import RgbToBgr
# Define dimensions and pre-processing
spatial_dims = 512, 512 # Optional: Target spatial dimensions of the model
preprocessing = RgbToBgr() # Optional: Pre-Processing layers to be prepended to the model. Can be a list of layers.
# Perform conversion
convert_from_model_spec(model_spec=model_spec, output_path='some/path', output_name='some_file_name', spatial_dims=spatial_dims, preprocessing=preprocessing)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file czmodel-0.1.0.tar.gz
.
File metadata
- Download URL: czmodel-0.1.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200209 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e02907e2c08cb1e66cf3712f42d4a2cc13b3bdd89f40ae8e0fbea055ddac832c |
|
MD5 | c4e0c3a3bf5443e6396d0250f39f92ce |
|
BLAKE2b-256 | 16de00ba01c6fa6c1b54174d6fde7f6b73a92f9f31be17a5d08fa85dba90b8df |
File details
Details for the file czmodel-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: czmodel-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200209 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47110ae5c08e5e96dc091965e3a5085c3524a18e8f72e6598c894b6f2e67e255 |
|
MD5 | ae34bcfd55a216df37d6866ec79ec28d |
|
BLAKE2b-256 | c99b6c80c4fc1321ddd5a7b43d37ef8aded7086dd044131071cdd7b6a1d15f8c |