繁体   English   中英

将 Facenet 模型 .pb 文件转换为 TFLITE 格式时出错

[英]Error converting Facenet model .pb file to TFLITE format

我正在尝试使用以下命令在 Ubuntu 上使用 Tensorflow Lite 转换器转换基于 Inception ResNet 的预训练冻结 .pb,我从David Sandbergs Github获得:

/home/nils/.local/bin/tflite_convert
--output_file=/home/nils/Documents/frozen.tflite
--graph_def_file=/home/nils/Documents/20180402-114759/20180402-114759.pb 
--input_arrays=input 
--output_arrays=embeddings 
--input_shapes=1,160,160,3

但是,我收到以下错误:

2018-12-03 15:03:16.807431: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "/home/nils/.local/bin/tflite_convert", line 11, in <module>
sys.exit(main())
File "/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 412, in main
app.run(main=run_main, argv=sys.argv[:1])
File "/home/nils/.local/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 408, in run_main
_convert_model(tflite_flags)
File "/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 162, in _convert_model
output_data = converter.convert()
File "/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/lite.py", line 453, in convert
**converter_kwargs)
File "/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/convert.py", line 342, in toco_convert_impl
input_data.SerializeToString())
File "/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/convert.py", line 135, in toco_convert_protos
(stdout, stderr))
RuntimeError: TOCO failed see console for info.
b'2018-12-03 15:03:26.006252: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1080] Converting unsupported operation: FIFOQueueV2\n2018-12-03 15:03:26.006322: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1127] Op node missing output type attribute: batch_join/fifo_queue\n2018-12-03 15:03:26.006339: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1080] Converting unsupported operation: QueueDequeueUpToV2\n2018-12-03 15:03:26.006352: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1127] Op node missing output type attribute: batch_join\n2018-12-03 15:03:27.496676: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 5601 operators, 9399 arrays (0 quantized)\n2018-12-03 15:03:28.603936: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] After Removing unused ops pass 1: 3578 operators, 6254 arrays (0 quantized)\n2018-12-03 15:03:29.418074: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 3578 operators, 6254 arrays (0 quantized)\n2018-12-03 15:03:29.420354: F tensorflow/contrib/lite/toco/graph_transformations/resolve_batch_normalization.cc:42] 
Check failed: IsConstantParameterArray(*model, bn_op->inputs[1]) && IsConstantParameterArray(*model, bn_op->inputs[2]) && IsConstantParameterArray(*model, bn_op->inputs[3]) Batch normalization resolution requires that mean, multiplier and offset arrays be constant.\nAborted (core dumped)\n'
None

如果我做对了,这可能是因为两个不受支持的操作,QueueDequeueUpToV2 和 FIFOQueueV2,但我不确定。 你有什么想法可能是什么问题或者我如何解决这个错误? 该错误甚至意味着什么? 我想让这个模型在移动 android 设备上运行,有没有其他选择? 版本:VirtualBox 上的 Tensorflow V1.12 Python 3.6.7 Ubuntu 18.04.1 LTS 提前致谢!

在这里解决了这个问题, 也在这里添加了代码片段:

我可以将 FaceNet .pb转换为.tflite模型,以下是这样做的说明:

我们将量化具有 512 个嵌入大小的预训练 Facenet 模型 该模型在量化前大小约为 95MB。

$ ls -l model_pc
total 461248
-rw-rw-r--@ 1 milinddeore  staff   95745767 Apr  9  2018 20180402-114759.pb

使用以下代码创建文件inference_graph.py

import tensorflow as tf
from src.models import inception_resnet_v1
import sys
import click
from pathlib import Path

@click.command()
@click.argument('training_checkpoint_dir', type=click.Path(exists=True, file_okay=False, resolve_path=True))
@click.argument('eval_checkpoint_dir', type=click.Path(exists=True, file_okay=False, resolve_path=True))

def main(training_checkpoint_dir, eval_checkpoint_dir):
    traning_checkpoint = Path(training_checkpoint_dir) / "model-20180402-114759.ckpt-275"
    eval_checkpoint = Path(eval_checkpoint_dir) / "imagenet_facenet.ckpt"
    data_input = tf.placeholder(name='input', dtype=tf.float32, shape=[None, 160, 160, 3])
    output, _ = inception_resnet_v1.inference(data_input, keep_probability=0.8, phase_train=False, bottleneck_layer_size=512)
    label_batch= tf.identity(output, name='label_batch')
    embeddings = tf.identity(output, name='embeddings')
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        saver = tf.train.Saver()
        saver.restore(sess, traning_checkpoint.as_posix())
        save_path = saver.save(sess, eval_checkpoint.as_posix())
        print("Model saved in file: %s" % save_path)

if __name__ == "__main__":
     main()

在预训练模型上运行此文件,将生成用于推理的模型。 下载预训练模型并将其解压到 model_pre_trained/ 目录。 确保你有 python ≥ 3.4 版本。

python3 eval_graph.py model_pre_trained/ model_inference/

FaceNet 提供了freeze_graph.py文件,我们将使用它来冻结推理模型。

python3  src/freeze_graph.py model_inference/  my_facenet.pb

生成冻结模型后,是时候将其转换为.tflite

$ tflite_convert --output_file model_mobile/my_facenet.tflite --graph_def_file my_facenet.pb  --input_arrays "input" --input_shapes "1,160,160,3" --output_arrays embeddings --output_format TFLITE --mean_values 128 --std_dev_values 128 --default_ranges_min 0  --default_ranges_max 6 --inference_type QUANTIZED_UINT8 --inference_input_type QUANTIZED_UINT8

让我们检查量化模型的大小:

$ ls -l model_mobile/
total 47232
-rw-r--r--@ 1 milinddeore  staff  23667888 Feb 25 13:39 my_facenet.tflite

互通者代码:

 import numpy as np
 import tensorflow as tf


 # Load TFLite model and allocate tensors.
 interpreter = tf.lite.Interpreter(model_path="/Users/milinddeore/facenet/model_mobile/my_facenet.tflite")
 interpreter.allocate_tensors()

 # Get input and output tensors.
 input_details = interpreter.get_input_details()
 output_details = interpreter.get_output_details()

 # Test model on random input data.
 input_shape = input_details[0]['shape']
 input_data = np.array(np.random.random_sample(input_shape), dtype=np.uint8)
 interpreter.set_tensor(input_details[0]['index'], input_data)

 interpreter.invoke()
 output_data = interpreter.get_tensor(output_details[0]['index'])

 print('INPUTS: ')
 print(input_details)
 print('OUTPUTS: ')
 print(output_details)

互通输出:

$ python inout.py
INPUTS:
[{'index': 451, 'shape': array([  1, 160, 160,   3], dtype=int32), 'quantization': (0.0078125, 128L), 'name': 'input', 'dtype': <type 'numpy.uint8'>}]
OUTPUTS:
[{'index': 450, 'shape': array([  1, 512], dtype=int32), 'quantization': (0.0235294122248888, 0L), 'name': 'embeddings', 'dtype': <type 'numpy.uint8'>}]

希望这可以帮助!

我对@milind-deore 的建议并不走运。 该模型确实减少到 23 MB,但嵌入似乎已损坏。

我找到了另一种方法:TF -> Keras -> TF Lite

David Sandberg 的 FaceNet 实现可以转为 TensorFlow Lite,先从 TensorFlow 转为 Keras,再从 Keras 转为 TensorFlow Lite

我创建了这个进行转换的Google Colab 大部分代码取自此处

它的作用如下:

  1. 下载Hiroki Taniai 的 Keras FaceNet 实现
  2. 我的补丁版本覆盖 inception_resnet_v1.py 文件(它确实为模型添加了一个额外的层,以将标准化嵌入作为输出)
  3. 这里下载 Sandberg 的预训练模型(20180402-114759),并解压
  4. 从检查点文件中提取张量并将权重写入磁盘上的 numpy 数组,映射每个相应层的名称。
  5. 创建一个具有随机权重的新 Keras 模型(重要:使用 512 个类)。
  6. 写下从 numpy 数组中读取的每个相应层的权重。
  7. 使用 Keras 格式 .h5 存储模型
  8. 使用命令“tflite_convert”将 Keras 转换为 TensorFlow Lite。

    tflite_convert --post_training_quantize --output_file facenet.tflite --keras_model_file /content/keras-facenet/model/keras/model/facenet_keras.h5

同样在我的 Colab 中,我提供了一些代码来表明转换良好,并且 TFLite 模型确实有效。

比较比尔盖茨和拉里佩奇的面孔

distance bill vs bill 0.7266881 distance bill vs larry 1.2134411

因此,即使我没有对齐人脸,大约 1.2 的阈值也有利于识别。

希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM