簡體   English   中英

如何將 keras(h5) 文件轉換為 tflite 文件?

[英]How to convert keras(h5) file to a tflite file?

我有一個 keras(h5) 文件。 我需要將其轉換為 tflite? 我研究過,首先我需要通過 h5 -> pb -> tflite (因為 h5 - tflite 有時會導致一些問題)

from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5')
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)

您可以使用 TFLiteConverter 直接將 .h5 文件轉換為 .tflite 文件。 這在 Windows 上不起作用。

對於 Windows,請使用此Google Colab 筆記本進行轉換。 上傳 .h5 文件,它會將其轉換為 .tflite 文件。

如果您想自己嘗試,請關注:

  1. 創建一個 Google Colab 筆記本。 在左上角,單擊“上傳”按鈕並上傳您的 .h5 文件。
  2. 創建一個代碼單元並插入此代碼。

     from tensorflow.contrib import lite converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5' ) # Your model's name model = converter.convert() file = open( 'model.tflite' , 'wb' ) file.write( model )
  3. 運行單元格。 您將獲得一個 model.tflite 文件。 右鍵單擊文件並選擇“下載”選項。

這適用於我在 Windows 10 上使用 Tensorflow 2.1.0 和 Keras 2.3.1

import tensorflow as tf

model = tf.keras.models.load_model('model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

剛剛從 CoLab 使用筆記本中的此代碼執行此操作:

import tensorflow as tf
model = tf.keras.models.load_model('yourmodel.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflmodel = converter.convert()
file = open( 'yourmodel.tflite' , 'wb' ) 
file.write( tflmodel )

我在通過 CoLab 上傳 h5 模型時遇到了困難,因此我安裝了我的 Google Drive,將其上傳到那里,然后將其移至筆記本內容文件夾。

從會話轉換 GraphDef。

converter = lite.TFLiteConverter.from_session(sess, in_tensors, out_tensors)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

從文件轉換 GraphDef。

converter = lite.TFLiteConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

轉換 SavedModel。

converter = lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()

如果您使用的是Google Colab Notebook ,請嘗試以下操作:

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5') 
tfmodel = converter.convert() 
open ('model.tflite' , "wb") .write(tfmodel)
import tensorflow as tf
from tensorflow import lite
from tensorflow.keras.models import load_model
converter = lite.TFLiteConverter.from_keras_model(model)
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)

這對我有用。 我正在使用 keras==2.6.0 和 tensorflow-cpu==2.5.0 版本。 有關更多信息,您可以訪問https://www.tensorflow.org/guide/keras/save_and_serialize

必須考慮一個因素。 在轉換之前,您需要更改學習階段 當你有 Dropout 或 Batch Normalization 時,這非常重要。 您可以查看“Keras 模型到 tflite”“將 keras 模型轉換為 Tensorflow pb 后的問題”討論

只有某些特定版本的 Tensorflow 和 Keras 在所有操作系統中都能正常工作。 我什至嘗試過 toco 命令行,但它也有問題。 使用 tensorflow==1.13.0-rc1 和 keras==2.1.3

然后在這之后就可以了

from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5' ) # Your model's name
model = converter.convert()
file = open( 'model.tflite' , 'wb' ) 
file.write( model )

將 RetinaNet 轉換為 tflite

import tensorflow as tf
from keras_retinanet.models import load_model
from keras.layers import Input
from keras.models import Model

def get_file_size(file_path):
    size = os.path.getsize(file_path)
    return size
    
def convert_bytes(size, unit=None):
    if unit == "KB":
        return print('File size: ' + str(round(size / 1024, 3)) + ' Kilobytes')
    elif unit == "MB":
        return print('File size: ' + str(round(size / (1024 * 1024), 3)) + ' Megabytes')
    else:
        return print('File size: ' + str(size) + ' bytes')

def convert_model_to_tflite(model_path = "/content/drive/MyDrive/Model/resnet152_csv_180_inference.h5", filename = "converted_model.tflite"):
  model = load_model(model_path)
  fixed_input = Input((416,416,3))
  fixed_model = Model(fixed_input,model(fixed_input))
  converter = tf.lite.TFLiteConverter.from_keras_model(model)
  converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
  ]
  tflite_model = converter.convert()
  open(filename, "wb").write(tflite_model)
  print(convert_bytes(get_file_size("converted_model.tflite"), "MB"))

如果您使用的是 Tensorflow-2,那么您可以按照以下步驟操作:

import tensorflow as tf
from keras.models import load_model
model = load_model("model.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tfmodel = converter.convert()
open("model.tflite", "wb") .write(tfmodel)

在 TF-2 中,它需要加載 Keras 模型實例並返回一個轉換后的實例。 查看此鏈接了解更多詳情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM