tensorflowtensorflow-litebert-language-model

Cannot parse file error while .pb file to tflite conversion


Hi I was making tflite for custom albert model with pb file in tf1.15 but raised error of

raise IOError("Cannot parse file %s: %s." % (path_to_pb, str(e)))
OSError: Cannot parse file b'/home/choss/test2/freeze2/saved_model.pb': Error parsing message.

Code below is How I made .pb file

meta_path = 'model.ckpt-400.meta'  # Your .meta file
output_node_names = ['loss/Softmax']    

with tf.Session() as sess:

    # Restore the graph
    saver = tf.train.import_meta_graph(meta_path)

    # Load weights
    ckpt ='/home/choss/test2/freeze2/model.ckpt-400'
    print(ckpt) 
    saver.restore(sess, ckpt)

    output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]

    # Freeze the graph
    frozen_graph_def = tf.graph_util.convert_variables_to_constants(
        sess,
        sess.graph_def,
        output_node_names)

    # Save the frozen graph
    with open('saved_model.pb', 'wb') as f:
        f.write(frozen_graph_def.SerializeToString())

And I tried to make tflite file with code below

saved_model_dir = "/home/choss/test2/freeze2"
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

I used f.graph_util.convert_variables_to_constants because of freeze_graph because

freeze_graph.freeze_graph('./graph.pbtxt', saver, False, 'model.ckpt-400', 'loss/ArgMax', "", "", 'frozen.pb', True, "")

gave me an error message

File "/home/pgb/anaconda3/envs/myenv/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 2154, in __getitem__
    return self._inputs[i]
IndexError: list index out of range

Is it because I did not use freeze_graph? If so is there any other way aside from freeze_graph?


Solution

  • Instead of freezing the graph by yourself, I recommend exporting as a TF saved model and using the saved model converter with the recent TF version. You can decouple the TensorFlow versions for training and converting. For example, training can be done in the TF 1.15 and the saved model can be exported from it. And then, it is possible to bring the saved model to the TFLite converter API in TensorFlow 2.4.1 version or beyonds.