首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >keras .h5转移动端的.tflite文件实现方式

keras .h5转移动端的.tflite文件实现方式

作者头像
砸漏
发布2020-11-02 11:42:05
2.8K0
发布2020-11-02 11:42:05
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

以前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.h5"
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

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

model.save(‘MyModel.h5’)

H5转换成TFLite

这里是文章主要内容

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

官网代码是这样的

converter = tf.lite.TFLiteConverter.from_keras_model_file('newModel.h5')
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.h5')
  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文件实现方式)就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档