Я решил эту проблему здесь , добавив здесь фрагмент также:
Я мог бы преобразовать FaceNet .pb
в .tflite
модель, и следуйте инструкциям, чтобы сделатьитак:
Будем количественно оценивать предварительно обученную Facenet модель с 512 размерами встраивания.Эта модель составляет около 95 МБ в размере до квантования.
$ 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'>}]
Надеюсь, это поможет!