Skip to main content

Keras Model Extract

This is a small utility library to access Keras/TensorFlow model's structure/tree and do stuff with it.

Currently there isn't a straight-forward way to do this without manually going through the model layers.

It currently supports:

  • Model tree iteration (BFS-like)
  • Accessing node parents, node children, node output type
  • Accessing source layers

Node properties:

  • children: children nodes (sub-layers)
  • parent_names: unique names of parent nodes
  • shape: layer output shape
  • name: unique layer name (from TF/Keras)
  • __layer: reference to the instance of the layer (if include_layer_ref is True)

How it works:

  • It creates a pure Python tree clone of your model which is easy to walk through.

Installation

This package has no depenedencies.

pip install keras-model-extract

Example use

This examples show how to iterate through a model and print all the nodes.

>>> from keras_model_extract import copy_model_tree, iterate
>>> from keras.applications.vgg16 import VGG16
>>> model = VGG16()
>>> nodes = copy_model_tree(model)
>>> nodes
{'input_1': input_1, 'block1_conv1': block1_conv1, 'block1_conv2': block1_conv2, 'block1_pool': block1_pool, 'block2_conv1': block2_conv1, 'block2_conv2': block2_conv2, 'block2_pool': block2_pool, 'block3_conv1': block3_conv1, 'block3_conv2': block3_conv2, 'block3_conv3': block3_conv3, 'block3_pool': block3_pool, 'block4_conv1': block4_conv1, 'block4_conv2': block4_conv2, 'block4_conv3': block4_conv3, 'block4_pool': block4_pool, 'block5_conv1': block5_conv1, 'block5_conv2': block5_conv2, 'block5_conv3': block5_conv3, 'block5_pool': block5_pool, 'flatten': flatten, 'fc1': fc1, 'fc2': fc2, 'predictions': predictions}
>>> nodes['input_1'].children
[block1_conv1]
>>> nodes['block4_pool'].parent_names
['block4_conv3']
>>> nodes['block4_pool'].shape
(None, 14, 14, 512)
>>> iterate(nodes['input_1'], lambda layer: print(layer))
input_1
block1_conv1
block1_conv2
block1_pool
block2_conv1
block2_conv2
block2_pool
block3_conv1
block3_conv2
block3_conv3
block3_pool
block4_conv1
block4_conv2
block4_conv3
block4_pool
block5_conv1
block5_conv2
block5_conv3
block5_pool
flatten
fc1
fc2
predictions

Metadata

Release files for keras-model-extract 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keras-model-extract 0.0.2
File Size Uploaded
keras_model_extract-0.0.2.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keras-model-extract 0.0.2
File Interpreter ABI Platform
keras_model_extract-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / keras_model_extract-0.0.2.tar.gz

Download URL keras_model_extract-0.0.2.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
90bd7d6610ce6352c3aa1d4d598a2b59e56b18a706cb4247c451ef4cbfae3e49
BLAKE2b-256 checksum
How to use checksums
31927cc62557ce0ecd38158098b663bc61df207b15481663452c51812e2718a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.5

Release files / keras_model_extract-0.0.2-py3-none-any.whl

Download URL keras_model_extract-0.0.2-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
424f9970be55ef9149f98674b82cb2a5d69e87880709c3735b4e9314a06a5374
BLAKE2b-256 checksum
How to use checksums
56b8d4ab98902b498a9e7b0c6fb27824b6f2262b002768ba428819d61be9d7e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page