Please provide complete information as applicable to your setup.
**• Hardware Platform (Jetson / GPU)**Jetson NX Xavier
**• DeepStream Version 5.0
**• JetPack Version 4.4.1
**• TensorRT Version 7.1.3-1
i have the facenet model “facenet_keras_128.h5”, and i need to convert it to .pb file using jypiter notebook using following codes
%reload_ext autoreload
%autoreload 2
from keras_to_pb_tf2 import keras_to_pb
from keras.models import load_model
#User defined values
#Input file path
MODEL_PATH = '/home/blaise/Documents/deepstreamfacerecognition/models/facenet_keras_128.h5'
#output files paths
PB_FILE_PATH = '/home/blaise/Documents/deepstreamfacerecognition/tf2trt_with_onnx/facenet_freezed.pb'
ONNX_FILE_PATH = '/home/blaise/Documents/deepstreamfacerecognition/tf2trt_wtih_onnx/facenet_onnx.onnx'
TRT_ENGINE_PATH = '/home/blaise/Documents/deepstreamfacerecognition/tf2trt_wtih_onnx/facenet_engine.plan'
#End user defined values
model = load_model(MODEL_PATH)
input_name, output_node_names = keras_to_pb(model, PB_FILE_PATH, None
keras_to_pb_tf2.py
import tensorflow as tf
from tensorflow.compat.v1 import graph_util
from tensorflow.keras.models import Model
from tensorflow.python.keras import backend as K
from tensorflow.keras.models import load_model
import argparse
tf.compat.v1.disable_eager_execution()
K.set_learning_phase(0)
def keras_to_pb(model, output_filename, output_node_names):
"""
This is the function to convert the Keras model to pb.
Args:
model: The Keras model.
output_filename: The output .pb file name.
output_node_names: The output nodes of the network. If None, then
the function gets the last layer name as the output node.
"""
# Get the names of the input and output nodes.
in_name = model.layers[0].get_output_at(0).name.split(':')[0]
if output_node_names is None:
output_node_names = [model.layers[-1].get_output_at(0).name.split(':')[0]]
sess = K.get_session()
# The TensorFlow freeze_graph expects a comma-separated string of output node names.
output_node_names_tf = ','.join(output_node_names)
frozen_graph_def = graph_util.convert_variables_to_constants(
sess,
sess.graph.as_graph_def(),
output_node_names)
sess.close()
wkdir = ''
tf.io.write_graph(frozen_graph_def, wkdir, output_filename, as_text=False)
return in_name, output_node_names
def main(args):
# load ResNet50 model pre-trained on imagenet
model = load_model(args.model_path)
# Convert keras ResNet50 model to .bp file
in_tensor_name, out_tensor_names = keras_to_pb(model, args.output_pb_file , None)
if __name__ == 'main':
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str, default='facenet_keras.h5')
parser.add_argument('--output_pb_file', type=str, default='facenet.pb')
args=parser.parse_args()
main(args)
keras_to_pb.py
import tensorflow as tf
import keras
from tensorflow.keras.models import Model
import keras.backend as K
from keras.models import load_model
import argparse
K.set_learning_phase(0)
def keras_to_pb(model, output_filename, output_node_names):
"""
This is the function to convert the Keras model to pb.
Args:
model: The Keras model.
output_filename: The output .pb file name.
output_node_names: The output nodes of the network. If None, then
the function gets the last layer name as the output node.
"""
# Get the names of the input and output nodes.
in_name = model.layers[0].get_output_at(0).name.split(':')[0]
if output_node_names is None:
output_node_names = [model.layers[-1].get_output_at(0).name.split(':')[0]]
sess = keras.backend.get_session()
# The TensorFlow freeze_graph expects a comma-separated string of output node names.
output_node_names_tf = ','.join(output_node_names)
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
sess,
sess.graph_def,
output_node_names)
sess.close()
wkdir = ''
tf.train.write_graph(frozen_graph_def, wkdir, output_filename, as_text=False)
return in_name, output_node_names
def main(args):
# load ResNet50 model pre-trained on imagenet
model = load_model(args.model_path)
# Convert keras ResNet50 model to .bp file
in_tensor_name, out_tensor_names = keras_to_pb(model, args.output_pb_file , None)
if __name__ == 'main':
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str, default='facenet_keras.h5')
parser.add_argument('--output_pb_file', type=str, default='facenet.pb')
args=parser.parse_args()
main(args)
But when i run the code, the .pb file is not generated in specified path. Am i doing it wrong? or is there something am missing? I appreciate your help.