Cookies 🍪

We use cookies to ensure you the best possible experience when using BioLib. To read our privacy policy and configure what cookies are set, click on cookie settings below.



TensorFlow

TensorFlow models can be uploaded to BioLib via two different formats: the Protocol Buffer format (.pb) or as a TensorFlow Lite graph (.tflite).

TensorFlow v2

To obtain a .pb file of your TensorFlow 2.x model (my_model), use the write_graph API:

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

# Convert Model to ConcreteFunction
model_as_function = tf.function(lambda x: my_model(x))
model_as_function = model_as_function.get_concrete_function(
    tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))

# Freeze ConcreteFunction
frozen_function = convert_variables_to_constants_v2(model_as_function)
frozen_function.graph.as_graph_def()

# Save graph from frozen ConcreteFunction
tf.io.write_graph(graph_or_graph_def=frozen_function.graph,
                  logdir="./frozen_models",
                  name="model.pb",
                  as_text=False)

This will store your model as a .pb file.

TensorFlow Lite (TensorFlow 2.x)

To obtain a Tensorflow Lite frozen graph from your TensorFlow 2.x model use the tf.lite.TFLiteConverter API to convert your model:

# From saved model to TensorFlow Lite.
import tensorflow as tf
saved_model_directory = 'models/'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_directory)
tflite_model = converter.convert()
# Keras model to TensorFlow Lite.
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# ConcreteFunctions to TensorFlow Lite.
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_concrete_functions([function])
tflite_model = converter.convert()

Then write the model to a file to save it as .tflite:

# Write the Tensorflow Lite model to a file
with open('model.tflite', 'wb') as file:
   file.write(tflite_model)

This will store your model as model.tflite.

To upload your model as a BioLib app use the designated TensorFlow template from the applications create page.

TensorFlow v1

Obtaining a .pb file of your TensorFlow 1.x model requires saving a checkpoint of its weights, and then serializing this together with the graph.

import tensorflow as tf
from tensorflow.python.tools import freeze_graph

# Training of your model goes here...
# ...
# sess = tf.Session()
# ...
# After training your model:

# Save check point for model
path_to_checkpoint_file = "./checkpoint_file.ckpt"
saver = tf.compat.v1.train.Saver()
saver.save(sess, path_to_checkpoint_file)

# Save graph definition to path_to_pbtxt_file
path_to_pbtxt_file = "./graph_definition.pbtxt"
tf.train.write_graph(graph_or_graph_def=sess.graph_def, 
                     logdir=directory, 
                     name=path_to_pbtxt_file, 
                     as_text=True)

# Save .pb file to path_to_pb_file
path_to_pb_file = "./my_model.pb"
freeze_graph.freeze_graph(input_graph=path_to_pbtxt_file, 
                          input_saver='', 
                          input_binary=False,
                          input_checkpoint=path_to_checkpoint_file, 
                          output_node_names='output_node_name', 
                          restore_op_name='save/restore_all', 
                          filename_tensor_name='save/Const:0', 
                          output_graph=path_to_pb_file, 
                          clear_devices=True, 
                          initializer_nodes='')

This will store your model as a .pb file.

TensorFlow Lite (TensorFlow 1.x)

To obtain a .tflite frozen graph from your TensorFlow 1.x model use the tf.compat.v1.lite.TFLiteConverter API:

# From saved model to TensorFlow Lite.
import tensorflow as tf
saved_model_directory = 'models/'
converter = tf.compat.v1.lite.TFLiteConverter.from_saved_model(saved_model_directory)
tflite_model = converter.convert()
# Keras model to TensorFlow Lite.
import tensorflow as tf
converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# ConcreteFunctions to TensorFlow Lite.
import tensorflow as tf
converter = tf.compat.v1.lite.TFLiteConverter.from_concrete_functions([function])
tflite_model = converter.convert()

Then write the model to a file to save it as .tflite:

# Write the Tensorflow Lite model to a file
with open('model.tflite', 'wb') as file:
   file.write(tflite_model)

This will store your model as model.tflite.

To upload your model as a BioLib app use the designated TensorFlow template from the applications create page.

Note: output_node_names can be set when you define your model with the name parameter. For instance:

output_node = tf.add(node_a, node_b, name="output_node_name")