当前位置:   article > 正文

TFlite模型转换和使用_converter = tf.lite.tfliteconverter.from_keras_mod

converter = tf.lite.tfliteconverter.from_keras_model(keras_model) nameerror:

背景

TensorFlow Lite 转换器可根据输入的 TensorFlow 模型生成 TensorFlow Lite 模型(一种优化的 FlatBuffer 格式,以 .tflite 为文件扩展名). 作用是进一步缩短模型延迟时间和减小模型大小,同时最大限度降低准确率损失和添加元数据,从而在设备上部署模型时可以更轻松地创建平台专用封装容器代码。

环境

tensorflow=2.4.1

实践例子

把Tensorflow的模型转换成tflite

  1. import tensorflow as tf
  2. def convert_to_tflite(model):
  3. converter = tf.lite.TFLiteConverter.from_keras_model(model)
  4. tfmodel = converter.convert()
  5. file = open('yourmodel.tflite', 'wb')
  6. file.write(tfmodel)
  7. file.close()

运行Tflite模型

  1. import tensorflow as tf
  2. def run_reference_by_tflite(input):
  3. interpreter = tf.lite.Interpreter(model_path="yourmodel.tflite")
  4. interpreter.allocate_tensors()
  5. # Get input and output tensors.
  6. input_details = interpreter.get_input_details()
  7. output_details = interpreter.get_output_details()
  8. # input details
  9. print(input_details, len(input_details))
  10. # output details
  11. print(output_details)
  12. # input_details[0]['index'] = the index which accepts the input
  13. interpreter.set_tensor(input_details[0]['index'], input)
  14. # run the inference
  15. interpreter.invoke()
  16. # output_details[0]['index'] = the index which provides the input
  17. output_data = interpreter.get_tensor(output_details[0]['index'])
  18. print('interpreter: ', output_data)

转tfhub中的模型

  1. !pip install -q tensorflow-text
  2. import tensorflow as tf
  3. import tensorflow_hub as hub
  4. import tensorflow_text as text
  5. hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual/3") # Caches the model in /tmp/tfhub_modules
  6. converter = tf.lite.TFLiteConverter.from_saved_model("/tmp/tfhub_modules/26c892ffbc8d7b032f5a95f316e2841ed4f1608c")
  7. converter.target_spec.supported_ops = [
  8. tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
  9. tf.lite.OpsSet.SELECT_TF_OPS, # enable TensorFlow ops.
  10. ]
  11. tflite_file = "model.tflite"
  12. with open(tflite_file, 'wb') as f:
  13. f.write(converter.convert())
  14. interpreter = tf.lite.Interpreter(tflite_file)
  15. interpreter.get_signature_list() # {'serving_default': {'inputs': ['inputs'], 'outputs': ['outputs']}}

不同版本的tensorflow或不同的格式的模型对应的转换方法


相关错误以及解决方法

1. ValueError: Cannot set tensor: Got value of type NOTYPE but expected type FLOAT32 for input 0,

ValueError: Cannot set tensor: Got value of type INT32 but expected type FLOAT32 for input 0,

ValueError: Cannot set tensor: Got value of type UINT8 but expected type FLOAT32 for input 0, name: input_1 

解决方法:

under_exp = np.array(under_exp, dtype=np.float32)

参考资料

python - How to convert keras(h5) file to a tflite file? - Stack Overflowhttps://www.tensorflow.org/lite/convert

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/154345
推荐阅读
相关标签
  

闽ICP备14008679号