Downloading Your Model
Sometimes, you might want to have access to the raw model itself and run it on your end. If the need arise, you can download the model and the associated files such as:
  • Raw model: either a Tensorflow, Caffe or Darknet file containing the model weights.
  • Preprocess: all the preprocessing operations to be applied for each image.
  • Postprocess: all the postprocessing operations such as Non-Maximum Suppression.
  • Outputs: the correspondance between the output tensor and the different labels.

Python

First you need to setup your Python environment. We use Pipenv for this purpose but the instructions are easily adaptable to your python management tool of choice.
Install python packages and start a new shell
1
pipenv --python 3.6 # setup pipenv with python 3.6
2
pipenv install requests
3
pipenv install tensorflow
4
pipenv install deepomatic-api
Copied!
Then you have to setup your Deepomatic Studio credentials as environment variables to be passed to the Deepomatic Python Client. COMMENTS: App id still required?
Setup Deepomatic Studio credentials and start Python
1
export DEEPOMATIC_APP_ID=xxxxxxxxxxxx
2
export DEEPOMATIC_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3
pipenv run python
Copied!
Your python shell is now all set!

Associated Files

In the next section we will proceed in several steps to retrieve this different files:
  1. 1.
    Initialize the Deepomatic Client with our credentials
  2. 2.
    Retrieve the recognition version and download the postprocess file
  3. 3.
    Retrieve the network and download the preprocess file
  4. 4.
    Retrieve the recognition specification and download the outputs file
First, you will need to specify the recognition version that you want to retrieve.
Specify the recognition version
1
export DEEPOMATIC_VERSION_ID=xxxxx
Copied!
You are now ready to download the different files.
Retrieve associated files
1
import os
2
import json
3
from deepomatic.api.client import Client
4
5
def pretty_save_json_to_file(json_data, json_path):
6
"""Helper function to save json to file in readable fashion."""
7
try:
8
with open(json_path, 'w') as json_file:
9
json.dump(json_data, json_file, indent=4, sort_keys=True)
10
except:
11
print(f"Could not save file {json_path} in json format.")
12
raise
13
14
# Intialize client
15
app_id = os.getenv('DEEPOMATIC_APP_ID')
16
api_key = os.getenv('DEEPOMATIC_API_KEY')
17
client = Client(app_id, api_key)
18
19
# Retrieve the recognition version
20
version_id = os.getenv('DEEPOMATIC_VERSION_ID')
21
version = client.RecognitionVersion.retrieve(version_id)
22
version_data = version.data()
23
postprocess_file = 'postprocess.json'
24
pretty_save_json_to_file(version_data['post_processings'], postprocess_file)
25
print(f"Recognition Version number {version_id} saved to {postprocess_file}")
26
27
# Retrieve the network
28
network_id = version_data['network_id']
29
network = client.Network.retrieve(network_id)
30
network_data = network.data()
31
preprocess_file = 'preprocess.json'
32
pretty_save_json_to_file(network_data['preprocessing'], preprocess_file)
33
print(f"Network number {network_id} saved to {preprocess_file}")
34
35
# Retrieve the recognition specification
36
spec_id = version_data['spec_id']
37
spec = client.RecognitionSpec.retrieve(spec_id)
38
spec_data = spec.data()
39
outputs_file = 'outputs.json'
40
pretty_save_json_to_file(spec_data['outputs'], outputs_file)
41
print(f"Recognition Specification {spec_id} saved to {outputs_file}")
Copied!

Download Raw Model

Now that you have retrieve all the additional files, you can download the raw model with all the architecture weights. Note that this may take a while as the file can weight up to several gigabytes.
Download and Extract Raw Model
1
import zipfile
2
import requests
3
4
# Download the raw model
5
model_url = f"https://api.deepomatic.com/v0.7/networks/{network_id}/download"
6
print("Starting network download...")
7
r = requests.get(model_url, headers={'X-APP-ID': app_id, 'X-API-KEY': api_key})
8
model_file = 'network.zip'
9
with open(model_file, 'wb') as f:
10
f.write(r.content)
11
print(f"Raw model saved to {model_file}")
12
13
# Extract network file
14
zip_ref = zipfile.ZipFile(model_file, 'r')
15
zip_ref.extractall('network')
16
zip_ref.close()
Copied!
The raw model is located in the new network directory.

Exploring The Model

As an illustration, we provide the code bellow to load a Tensorflow model and display the node names of the computation graph.
1
2
import tensorflow as tf
3
from tensorflow.python.platform import gfile
4
from tensorflow.core.protobuf import saved_model_pb2
5
from tensorflow.python.util import compat
6
7
# Open tensorflow model and list all nodes
8
model_filename = 'network/saved_model.pb'
9
nodes_file = 'nodes.json'
10
with tf.Session() as sess:
11
with gfile.FastGFile(model_filename, 'rb') as f:
12
# Convert pb file to tensorflow graph
13
data = compat.as_bytes(f.read())
14
sm = saved_model_pb2.SavedModel()
15
sm.ParseFromString(data)
16
g_in = tf.import_graph_def(sm.meta_graphs[0].graph_def)
17
18
# Print all nodes in graph
19
nodes = [n.name for n in tf.get_default_graph().as_graph_def().node]
20
pretty_save_json_to_file(nodes, nodes_file)
21
print(f"Models Nodes saved to {nodes_file}")
22
Copied!

For The Lazy

In case you don't want to go through all the steps detailed above, you will find all the scripts and generated files bellow.
Pipfile
191B
Text
Pipfile for Python environment setup
download.py
3KB
Text
Python download.py script including all steps
preprocess.json
472B
Code
Preprocessing JSON file
postprocess.json
23KB
Code
Postprocessing JSON file
outputs.json
117KB
Code
Outpus JSON file
nodes.json
388KB
Code
Nodes JSON file

Going Further

We do not provide any additional support on how to deploy and exploit the models on your own infrastructures outside of the Deepomatic Run framework.
Indeed, industrializing the deployment of neural networks is a complex task and one of the core feature of the whole Deepomatic Platform.