有什么方法可以将 tensorflow lite (.tflite) 文件转换回 keras 文件 (.h5)? [英] Is there any way to convert a tensorflow lite (.tflite) file back to a keras file (.h5)?

查看:62
本文介绍了有什么方法可以将 tensorflow lite (.tflite) 文件转换回 keras 文件 (.h5)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一个粗心的错误,我丢失了我的数据集.我手里只剩下我的 tflite 文件了.是否有任何解决方案可以反转 h5 文件.我在这方面做了不错的研究,但没有找到解决方案.

I had lost my dataset by a careless mistake. I have only my tflite file left in my hand. Is there any solution to reverse back h5 file. I have been done decent research in this but no solutions found.

推荐答案

从 TensorFlow SaveModel 或 tf.keras H5 模型到 .tflite 模型的转换是一个不可逆转的过程.具体而言,TFLite转换器在编译过程中对原始模型拓扑进行了优化,导致了一些信息丢失.此外,原始 tf.keras 模型的损失和优化器配置也被丢弃,因为推理不需要这些配置.

The conversion from a TensorFlow SaveModel or tf.keras H5 model to .tflite is an irreversible process. Specifically, the original model topology is optimized during the compilation by the TFLite converter, which leads to some loss of information. Also, the original tf.keras model's loss and optimizer configurations are discarded, because those aren't required for inference.

但是,.tflite 文件仍然包含一些可以帮助您恢复原始训练模型的信息.最重要的是,权重值是可用的,尽管它们可能被量化,这可能会导致一些精度损失.

However, the .tflite file still contains some information that can help you restore the original trained model. Most importantly, the weight values are available, although they might be quantized, which could lead to some loss in precision.

下面的代码示例向您展示了如何从经过简单训练的 tf.keras.Model 创建的 .tflite 文件读取权重值.

The code example below shows you how to read weight values from a .tflite file after it's created from a simple trained tf.keras.Model.

import numpy as np
import tensorflow as tf

# First, create and train a dummy model for demonstration purposes.
model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, input_shape=[5], activation="relu"),
    tf.keras.layers.Dense(1, activation="sigmoid")])
model.compile(loss="binary_crossentropy", optimizer="sgd")

xs = np.ones([8, 5])
ys = np.zeros([8, 1])
model.fit(xs, ys, epochs=1)

# Convert it to a TFLite model file.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("converted.tflite", "wb").write(tflite_model)

# Use `tf.lite.Interpreter` to load the written .tflite back from the file system.
interpreter = tf.lite.Interpreter(model_path="converted.tflite")
all_tensor_details = interpreter.get_tensor_details()
interpreter.allocate_tensors()

for tensor_item in all_tensor_details:
  print("Weight %s:" % tensor_item["name"])
  print(interpreter.tensor(tensor_item["index"])())

这些从 .tflite 文件加载回来的权重值可以与 tf.keras.Model.set_weights() 方法一起使用,这将允许您将权重值重新注入新实例您在 Python 中拥有的可训练模型.显然,这要求您仍然可以访问定义模型架构的代码.

These weight values loaded back from the .tflite file can be used with tf.keras.Model.set_weights() method, which will allow you to re-inject the weight values into a new instance of trainable Model that you have in Python. Obviously, this requires you to still have access to the code that defines the model's architecture.

这篇关于有什么方法可以将 tensorflow lite (.tflite) 文件转换回 keras 文件 (.h5)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆