我正在尝试使用tensorflow lite toco转换器来获取tflite图。 我使用 toco 命令行,以获取 tflite 图。我在转换过程中没有收到任何错误,但是,似乎我的转换包含垃圾(输出为 nans(。我正在询问有关如何调试此转换的想法。
我执行以下步骤:
-
加载 .ckpt 文件并转换为frozen_graph:
with g.as_default((, g.device(device_t(, \ TF.Session(config=soft_config( as sess: batch_shape = (batch_size,( + img_shape img_placeholder = tf.placeholder(tf.float32, shape=batch_shape, 名称='img_placeholder'(
preds = transform(img_placeholder) saver = tf.train.Saver() saver.restore(sess, checkpoint_dir) frozen_graph_def = tf.graph_util.convert_variables_to_constants(sess,sess.graph_def,['transformer/up-sample/mul']) with open('frozeen_graph.pb', 'wb') as f: f.write(frozen_graph_def.SerializeToString())
quastion:上面的代码是否与脚本tensorflow/python/tools/freeze_graph.py的使用有关?
当我使用上面的代码时,我还通过加载冻结图并通过它传递输入图像来检查冻结,输出看起来不错。所以看起来冻结有效。
- 使用 toco convert 命令行(在 tensorflow 版本 1.10 和 tensorflow nightly-GPU 中尝试(
input_shape=1,256,256,3 \ --input_array=img_placeholder:0 \ --output_array=transformer/up-sample/mul:0 \ --inference_type=FLOAT \
--input_data_type=FLOAT
执行上述行时,我没有收到任何错误。请注意,我更改了图形以摆脱一些"不支持的操作"错误。
接下来,我使用 tensorflow lite 解释器来测试转换后的图形(使用 python API(:
tflite_graph_filename = 'converted_graph.lite' # Load TFLite model and allocate tensors. interpreter = tf.contrib.lite.Interpreter(model_path=tflite_graph_filename) interpreter.allocate_tensors() # Get input and output tensors. input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() input_shape = input_details[0]['shape'] X = np.zeros(input_shape,np.float32) X[0] = content_image input_data = X interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index'])
不幸的是,output_data都是纳内斯,
有人可以给我一些调试建议或进行这种转换的正确方法吗?
谢谢前方, 瓦迪姆
这里回答了将 .pb 转换为 .lite 的简单方法:
https://stackoverflow.com/a/58583419/11517841
为了确保您不会犯任何与架构相关的错误,这里仅供参考/"当心":
https://stackoverflow.com/a/58583602/11517841