TensorFlow

TensorFlow models can be uploaded to BioLib as a Frozen Graph via the Protocol Buffer format (.pb).

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. 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. 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")