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