Search code examples
deep-learningtensorflow2.0google-coraltensorflow-lite

convert pb file to tflite file for running on Coral Dev Board (Segmentation fault (core dumped) )


How to convert a pb file into tflite file using python3 or in terminal.
Don't know any of the details of the model.Here is the link of the pb file.

(Edited) I have done converting pb file to tflite file by the following code : import tensorflow.compat.v1 as tf import numpy as np

graph_def_file = "./models/20170512-110547.pb"

def representative_dataset_gen():
  for _ in range(num_calibration_steps):
    # Get sample input data as a numpy array in a method of your choosing.
    yield [input]


converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file,
                                                      input_arrays=["input","phase_train"],
                                                      output_arrays=["embeddings"],
                                                      input_shapes={"input":[1,160,160,3],"phase_train":False})                                                                 

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()

print("converting")
open("./models/converted_model.tflite", "wb").write(tflite_model)
print("Done")

Error :Getting Segmentation fault(core dumped)

2020-01-20 11:42:18.153263: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer.so.6'; dlerror: libnvinfer.so.6: cannot open shared object file: No such file or directory
2020-01-20 11:42:18.153363: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer_plugin.so.6'; dlerror: libnvinfer_plugin.so.6: cannot open shared object file: No such file or directory
2020-01-20 11:42:18.153385: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:30] Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
2020-01-20 11:42:18.905028: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-01-20 11:42:18.906845: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2020-01-20 11:42:18.906874: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (kalgudi-GA-78LMT-USB3-6-0): /proc/driver/nvidia/version does not exist
2020-01-20 11:42:18.934144: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3616020000 Hz
2020-01-20 11:42:18.934849: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x39aa0f0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-01-20 11:42:18.934910: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Segmentation fault (core dumped)

Solution

  • with out any details of the model, you cannot convert it to a .tflite model. I suggest that you go through this document for Post Training Quantization again. As there are too many details that is redundant to show here.

    Here is an example for post training quantization of a frozen graph. The model is taken from here (you can see that it's a full tarball of infomation about the model)

    import sys, os, glob
    import tensorflow as tf
    import pathlib
    import numpy as np
    
    if len(sys.argv) != 2:
      print('Usage: <' + sys.argv[0] + '> <frozen_graph_file> <representative_image_dir>')
      exit()
    
    tf.compat.v1.enable_eager_execution()
    tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.DEBUG)
    
    def fake_representative_data_gen():
      for _ in range(100):
        fake_image = np.random.random((1,192,192,3)).astype(np.float32)
        yield [fake_image]
    
    frozen_graph = sys.argv[1]
    input_array = ['input']
    output_array = ['MobilenetV1/Predictions/Reshape_1']
    
    converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(frozen_graph, input_array, output_array)
    
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.representative_dataset = fake_representative_data_gen
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    converter.inference_input_type = tf.uint8
    converter.inference_output_type = tf.uint8
    
    tflite_model = converter.convert()
    
    quant_dir = pathlib.Path(os.getcwd(), 'output')
    quant_dir.mkdir(exist_ok=True, parents=True)
    
    tflite_model_file = quant_dir/'mobilenet_v1_0.25_192_quant.tflite'
    tflite_model_file.write_bytes(tflite_model)