tensorflow模型转为tflite

tensorflow模型转换主要分为:步骤1、步骤2。步骤1最为关键。

1、ckpt转为pb模型

主要步骤参考MobileFaceNet模型转换问题,其它模型转换也是按此步骤,注意输入、输出节点的选择。
转换过程中出现以下错误,应该是batch_norm_params参数设置的问题。应将is_training设置为False,将trainable设置为False,则不会出现以下错误。若仍然报错不支持相关操作,可能是版本原因。之前在tensorflow1.5上进行转换,报错。但更换为tensorflow1.14之后,不再报错。

Some of the operators in the model are not supported by the standard TensorFlow Lite runtime. If those are native TensorFlow operators, you might be able to use the extended runtime by passing --enable_select_tf_ops, or by setting target_ops=TFLITE_BUILTINS,SELECT_TF_OPS when calling tf.lite.TFLiteConverter(). Otherwise, if you have a custom implementation for them you can disable this error with --allow_custom_ops, or by setting allow_custom_ops=True when calling tf.lite.TFLiteConverter(). Here is a list of builtin operators you are using: ADD, AVERAGE_POOL_2D, CONCATENATION, CONV_2D, DEPTHWISE_CONV_2D, FULLY_CONNECTED, MUL, PACK, RELU6, RESHAPE, RSQRT, SHAPE, STRIDED_SLICE. Here is a list of operators for which you will need custom implementations: BatchNormalization, Merge, Switch.

2、pb模型转TFlite模型

主要使用tflite_convert命令,如果步骤1是正确的,步骤2不会报错。所以出现报错,应该仔细检查步骤1.

tflite_convert --output_file PFLD.tflite --graph_def_file new-frozen_model.pb  --input_arrays "image_batch" --input_shapes "1,112,112,3" --output_arrays pfld_inference/fc/BiasAdd --output_format TFLITE
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
TensorFlow是一个广泛用于机器学习和深度学习的开源框架。它提供了许多功能强大的工具和接口,使得加载和解析tflite模型变得相对简单。 要加载和解析tflite模型,首先需要使用TensorFlow提供的tflite模块。我们可以使用以下代码导入tflite模块: ``` import tensorflow as tf interpreter = tf.lite.Interpreter(model_path="model.tflite") interpreter.allocate_tensors() ``` 在上述代码中,我们首先导入tensorflow模块,并创建了一个tf.lite.Interpreter对象。通过指定模型的路径"model.tflite",我们将tflite模型加载到内存中。然后,我们使用interpreter对象的allocate_tensors方法来为模型分配所需的张量。 加载完成后,可以使用interpreter对象的get_input_details和get_output_details方法获取模型的输入和输出张量的详细信息: ``` input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() ``` 通过打印input_details和output_details,我们可以获得输入和输出张量的名称、形状、数据类型等详细信息。 接下来,我们可以准备要输入模型的数据,并将其设置为输入张量的值: ``` input_data = ... interpreter.set_tensor(input_details[0]['index'], input_data) ``` 在上述代码中,我们将input_data设置为我们要输入模型的数据,并使用interpreter对象的set_tensor方法将其设置为输入张量的值。input_details[0]['index']表示输入张量的索引。 然后,我们可以使用interpreter对象的invoke方法来运行模型: ``` interpreter.invoke() ``` 运行模型后,我们可以通过获取输出张量的值来获取模型的预测结果: ``` output_data = interpreter.get_tensor(output_details[0]['index']) ``` 将output_data打印出来,我们可以获得模型的预测结果。 总结起来,使用TensorFlow加载和解析tflite模型的步骤包括:导入tflite模块、创建Interpreter对象并加载tflite模型、获取输入和输出张量的详细信息、设置输入张量的值、运行模型并获取输出张量的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值