温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现keras .h5转移动端的.tflite文件

发布时间:2020-07-22 11:05:42 来源:亿速云 阅读:241 作者:小猪 栏目:开发技术

小编这次要给大家分享的是如何实现keras .h5转移动端的.tflite文件,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

以前tensorflow有bug 在winodws下无法转,但现在好像没有问题了,代码如下

将keras 下的mobilenet_v2转成了tflite

from keras.backend import clear_session
import numpy as np
import tensorflow as tf
clear_session()
np.set_printoptions(suppress=True)
input_graph_name = "../models/weights.best_mobilenet224.h6"
output_graph_name = input_graph_name[:-3] + '.tflite'
converter = tf.lite.TFLiteConverter.from_keras_model_file(model_file=input_graph_name)
converter.post_training_quantize = True
#在windows平台这个函数有问题,无法正常使用
tflite_model = converter.convert()
open(output_graph_name, "wb").write(tflite_model)
print ("generate:",output_graph_name)

补充知识:如何把Tensorflow模型转换成TFLite模型

深度学习迅猛发展,目前已经可以移植到移动端使用了,TensorFlow推出的TensorFlow Lite就是一款把深度学习应用到移动端的框架技术。

使用TensorFlowLite 需要tflite文件模型,这个模型可以由TensorFlow训练的模型转换而成。所以首先需要知道如何保存训练好的TensorFlow模型。

一般有这几种保存形式:

1、Checkpoints

2、HDF5

3、SavedModel等

保存与读取CheckPoint

当模型训练结束,可以用以下代码把权重保存成checkpoint格式

model.save_weights('./MyModel',True)

checkpoints文件仅是保存训练好的权重,不带网络结构,所以做predict时需要结合model使用

如:

model = keras_segmentation.models.segnet.mobilenet_segnet(n_classes=2, input_height=224, input_width=224)
model.load_weights('./MyModel')

保存成H5

把训练好的网络保存成h6文件很简单

model.save('MyModel.h6')

H5转换成TFLite

这里是文章主要内容

我习惯使用H5文件转换成tflite文件

官网代码是这样的

converter = tf.lite.TFLiteConverter.from_keras_model_file('newModel.h6')
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

但我用的keras 2.2.4版本会报下面错误,好像说是新版的keras把relu6改掉了,找不到方法

ValueError: Unknown activation function:relu6

于是需要自己定义一个relu6

import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.utils import CustomObjectScope

def relu6(x):
 return K.relu(x, max_value=6)

with CustomObjectScope({'relu6': relu6}):
  converter = tf.lite.TFLiteConverter.from_keras_model_file('newModel.h6')
  tflite_model = converter.convert()
  open("newModel.tflite", "wb").write(tflite_model)

看到生成的tflite文件表示保存成功了

也可以这么查看tflite网络的输入输出

import numpy as np
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="newModel.tflite")
interpreter.allocate_tensors()

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

print(input_details)
print(output_details)

输出了以下信息

[{'name': 'input_1', 'index': 115, 'shape': array([ 1, 224, 224, 3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]

[{'name': 'activation_1/truediv', 'index': 6, 'shape': array([ 1, 12544, 2]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]

两个shape分别表示输入输出的numpy数组结构,dtype是数据类型

看完这篇关于如何实现keras .h5转移动端的.tflite文件的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI