convert keras h5 model to tflite

前言:移动端的模型迁移最基本的就是生成tflite文件,以本文记录一次转换过程。

之前的文章转换的比较老的yolo.weights,发现yolov2-tiny.h5是可以直接用tflite-convert 工具直接转行成功 参考https://github.com/qqwweee/keras-yolo3

-------

env

1. tensorflow 1.11.0

2. python 3.5.2

3. h5 model saved by `model.save('tf.h5')`

直接转换

`tflite_convert --output_file=tf.tflite --keras_model_file=tf.h5`

output

`TypeError: __init__() missing 2 required positional arguments: 'filters' and 'kernel_size'`

先转成pb再转tflite

```

git clone [email protected]:amir-abdi/keras_to_tensorflow.git

cd keras_to_tensorflow

python keras_to_tensorflow.py --input_model=path/to/tf.h5 --output_model=path/to/tf.pb

tflite_convert \

  --output_file=tf.tflite \

  --graph_def_file=tf.pb \

  --input_arrays=convolution2d_1_input \

  --output_arrays=dense_3/BiasAdd \

  --input_shape=1,3,448,448

```

参数说明,input_arrays和output_arrays是model的起始输入变量名和结束变量名,input_shape是和input_arrays对应

官网是说需要用到tenorboard来查看,一个比较trick的方法

先执行上面的命令,会报convolution2d_1_input找不到,在堆栈里面有convert_saved_model.py文件,get_tensors_from_tensor_names()这个方法,添加`print(list(tensor_name_to_tensor))` 到 tensor_name_to_tensor 这个变量下面,再执行一遍,会打印出所有tensor的名字,再根据自己的模型很容易就能判断出实际的name。

参考

tflite_convert官方文档

思路主要参考的这里

你可能感兴趣的:(convert keras h5 model to tflite)