在 Coral 开发板上将 运行 的 pb 文件转换为 tflite 文件(分段错误(核心转储))

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

如何使用 python3 或在终端中将 pb 文件转换为 tflite 文件。
不知道模型的任何细节。Here is the link of the pb file

(已编辑) 我已经通过以下代码将 pb 文件转换为 tflite 文件: 将 tensorflow.compat.v1 导入为 tf 将 numpy 导入为 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")

错误:获取分段错误(核心已转储)

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)

没有模型的任何细节,您无法将其转换为 .tflite 模型。我建议您再次阅读此文档以获得 Post Training Quantization。由于细节太多,这里就不一一展示了。

这里是 post 训练冻结图量化的示例。模型取自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)