【问题标题】:How to convert keras(h5) file to a tflite file?如何将 keras(h5) 文件转换为 tflite 文件?
【发布时间】:2019-04-14 20:41:21
【问题描述】:

我有一个 keras(h5) 文件。我需要将其转换为 tflite? 我研究过,首先我需要通过 h5 -> pb -> tflite (因为 h5 - tflite 有时会导致一些问题)

【问题讨论】:

    标签: python tensorflow machine-learning keras


    【解决方案1】:
    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 notebook 进行转换。上传 .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 文件。右键单击文件并选择“下载”选项。

    【讨论】:

    • 在较新版本的 tensorflow 中,TFLiteConverter 已移至 tf.lite.TFLiteConverter 参见:tensorflow.org/lite/convert/python_api
    • 那么我可以在 tensorflow 版本 1.14(我正在使用)中使用tf.lite.TFLiteConverter 将普通的 Keras 模型而不是 tf.keras 转换为 tflite。正如文档中所说,tf.keras 模型可以通过tf.lite.TFLiteConverter 进行转换。这是否也适用于普通的 keras 模型?在我的代码中,我使用的是较旧的 Keras pip 安装包,而不是 tensorflow 中的 tf.keras。
    【解决方案2】:

    这适用于我在 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)
    

    【讨论】:

      【解决方案3】:

      刚刚在 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,将其上传到那里,然后将其移至笔记本内容文件夹。

      【讨论】:

        【解决方案4】:

        如果您使用的是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)
        

        【讨论】:

          【解决方案5】:

          从会话转换 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()
          

          【讨论】:

          • 你好这个工具将模型转换为mode.pb
          【解决方案6】:
          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

          【讨论】:

            【解决方案7】:

            必须考虑一个因素。在转换之前,您需要更改学习阶段。当你有 Dropout 或 Batch Normalization 时,这非常重要。你可以看看'Keras model to tflite''Problem after converting keras model into Tensorflow pb'讨论

            【讨论】:

              【解决方案8】:

              只有某些特定版本的 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 )
              

              【讨论】:

                【解决方案9】:

                将 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"))
                

                【讨论】:

                • 嗨,欢迎来到 Stack Overflow。您的答案与其他答案非常相似。请在您的答案中添加更多信息,为什么您的答案更有用,或完全删除它。
                猜你喜欢
                • 2020-12-15
                • 1970-01-01
                • 2020-04-20
                • 2020-09-10
                • 1970-01-01
                • 2018-12-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多