keras .h5轉(zhuǎn)移動端的.tflite文件實現(xiàn)方式
以前tensorflow有bug 在winodws下無法轉(zhuǎn),但現(xiàn)在好像沒有問題了,代碼如下
將keras 下的mobilenet_v2轉(zhuǎn)成了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平臺這個函數(shù)有問題,無法正常使用
tflite_model = converter.convert()
open(output_graph_name, "wb").write(tflite_model)
print ("generate:",output_graph_name)
補充知識:如何把Tensorflow模型轉(zhuǎn)換成TFLite模型
深度學習迅猛發(fā)展,目前已經(jīng)可以移植到移動端使用了,TensorFlow推出的TensorFlow Lite就是一款把深度學習應(yīng)用到移動端的框架技術(shù)。
使用TensorFlowLite 需要tflite文件模型,這個模型可以由TensorFlow訓練的模型轉(zhuǎn)換而成。所以首先需要知道如何保存訓練好的TensorFlow模型。
一般有這幾種保存形式:
1、Checkpoints
2、HDF5
3、SavedModel等
保存與讀取CheckPoint
當模型訓練結(jié)束,可以用以下代碼把權(quán)重保存成checkpoint格式
model.save_weights('./MyModel',True)
checkpoints文件僅是保存訓練好的權(quán)重,不帶網(wǎng)絡(luò)結(jié)構(gòu),所以做predict時需要結(jié)合model使用
如:
model = keras_segmentation.models.segnet.mobilenet_segnet(n_classes=2, input_height=224, input_width=224)
model.load_weights('./MyModel')
保存成H5
把訓練好的網(wǎng)絡(luò)保存成h5文件很簡單
model.save('MyModel.h5')
H5轉(zhuǎn)換成TFLite
這里是文章主要內(nèi)容
我習慣使用H5文件轉(zhuǎn)換成tflite文件
官網(wǎng)代碼是這樣的
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網(wǎng)絡(luò)的輸入輸出
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數(shù)組結(jié)構(gòu),dtype是數(shù)據(jù)類型
以上這篇keras .h5轉(zhuǎn)移動端的.tflite文件實現(xiàn)方式)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python報錯:ModuleNotFoundError的解決辦法
"ModuleNotFoundError: No module named 'xxx'"這個報錯是個非常常見的報錯,幾乎每個python程序員都遇到過,下面這篇文章主要給大家介紹了關(guān)于Python報:ModuleNotFoundError錯誤的解決辦法,需要的朋友可以參考下2022-06-06
python操作微信自動發(fā)消息的實現(xiàn)(微信聊天機器人)
這篇文章主要介紹了python操作微信自動發(fā)消息(微信聊天機器人),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Python操作SQLite/MySQL/LMDB數(shù)據(jù)庫的方法
這篇文章主要介紹了Python操作SQLite/MySQL/LMDB數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
Opencv圖像添加椒鹽噪聲、高斯濾波去除噪聲原理以及手寫Python代碼實現(xiàn)方法
椒鹽噪聲的特征非常明顯,為圖像上有黑色和白色的點,下面這篇文章主要給大家介紹了關(guān)于Opencv圖像添加椒鹽噪聲、高斯濾波去除噪聲原理以及手寫Python代碼實現(xiàn)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09
基于python實現(xiàn)微信收紅包自動化測試腳本(測試用例)
這篇文章主要介紹了基于python實現(xiàn)微信收紅包自動化測試腳本,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07

