Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert trained .hdf5 model to a single TensorFlow .pb file #98

Open
VViiepan opened this issue Nov 11, 2020 · 2 comments
Open

Comments

@VViiepan
Copy link

Hi! Thank you for your sharing.
I was trying to convert the .hdf5 model to convert .pb file,
(h5_model = load_model(weight_file ,custom_objects={'RoiPoolingConv' :RoiPoolingConv}))
however, when I do that I get the following error:
ValueError: No model found in config file.
Is it related to config.pickle this file?. How do deal with it?

@VViiepan
Copy link
Author

This is my convert code:

`
from keras_frcnn.RoiPoolingConv import RoiPoolingConv
from keras.models import load_model
import tensorflow as tf
import os
import os.path as osp
from keras import backend as K

// input dir
input_path = './'
weight_file = 'model_frcnn.hdf5'
weight_file_path = osp.join(input_path ,weight_file)
output_graph_name = "fr" + '.pb'

// convert
def h5_to_pb(h5_model ,output_dir ,model_name ,out_prefix = "output_" ,log_tensorboard = True):
if osp.exists(output_dir) == False:
os.mkdir(output_dir)
out_nodes = []
for i in range(len(h5_model.outputs)):
out_nodes.append(out_prefix + str(i + 1))
tf.identity(h5_model.output[i] ,out_prefix + str(i + 1))
sess = K.get_session()
from tensorflow.python.framework import graph_util ,graph_io
init_graph = sess.graph.as_graph_def()
main_graph = graph_util.convert_variables_to_constants(sess ,init_graph ,out_nodes)
graph_io.write_graph(main_graph ,output_dir ,name = model_name ,as_text = False)
if log_tensorboard:
from tensorflow.python.tools import import_pb_to_tensorboard
import_pb_to_tensorboard.import_to_tensorboard(osp.join(output_dir ,model_name) ,output_dir)

// output dir
output_dir = osp.join(os.getcwd() ,"trans_model")
// load model
h5_model = load_model(weight_file ,custom_objects={'RoiPoolingConv' :RoiPoolingConv})
h5_to_pb(h5_model ,output_dir = output_dir ,model_name = output_graph_name)
print('model saved')`

@SHREYAS290601
Copy link

please use indentations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants