一些TFlite模型model.process()似乎需要TensorBuffer,而另一些则需要TensorImage。我也不知道原因?
首先,我采用了一个常规的TensorFlow / Keras模型,该模型使用以下方法保存:
model.save(keras_model_path,
include_optimizer=True,
save_format='tf')然后,我使用以下方法将这个Keras模型(300 MB)压缩和量化为TFlite格式:
converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = tf.keras.utils.image_dataset_from_directory(dir_val,
batch_size=batch_size,
image_size=(150,150))
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()
with open(tflite_model_path, 'wb') as file:
file.write(tflite_model)我有一个小得多的TFlite型号(40 Mo),它在调用model.process( )时需要TensorBuffer 。
其次,我使用TFLite Lite model训练并保存为TensorFlow模型,现在我有了一个TFLite模型,在调用model.process( )时需要TensorImage 。
是否有两种不同的TFlite模型取决于您如何构建和训练它?
可能是因为Keras模型基于“盗梦空间”,而TensorFlow Lite模型制造商使用的是EfficientNet。如何从一个TFlite模型转换到另一个模型?如何更改图像的输入以处理相同的内容,例如TensorImage或位图数据输入?
发布于 2022-04-16 20:34:20
在@Farmaker非常有价值的帮助下,我解决了我的问题。我只是想将一个Keras模型转换成一个更紧凑的TFlite模型,以便在移动应用程序中安装它。我意识到生成的TFlite模型不兼容,@Farmaker非常正确地向我指出了元数据的缺失。
TensorFlow 2.6.0或更少,因为它与Flatbuffer不兼容。pip3 uninstall tensorflow
pip3 install tensorflow==2.6.0
pip3 install keras==2.6.0converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = tf.keras.utils.image_dataset_from_directory(dir_val,
batch_size=batch_size,
image_size=(150,150))
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()
with open(tflite_model_path, 'wb') as file:
file.write(tflite_model)例如,要创建这样的文件,
your_labels_list = [
'class1','class2',...]
with open('labels.txt', 'w') as labels_file:
for label in your_labels_list:
labels_file.write(label + "\n")- 3.2 Provide extra library to support TFlite metadata generationpip3 install tflite-support-nightly- 3.3 Generate the metadatafrom tflite_support.metadata_writers import image_classifier
from tflite_support.metadata_writers import writer_utils
ImageClassifierWriter = image_classifier.MetadataWriter
# Normalization parameters are required when processing the image
# https://www.tensorflow.org/lite/convert/metadata#normalization_and_quantization_parameters)
_INPUT_NORM_MEAN = 127.5
_INPUT_NORM_STD = 127.5
_TFLITE_MODEL_PATH = "<your_path_to_model.tflite>"
_LABELS_FILE = ""<your_path_to_labels.txt>""
_TFLITE_METADATA_MODEL_PATHS = ""<your_path_to_model_with_metadata.tflite>""
# Create the metadata writer
metadata_generator = ImageClassifierWriter.create_for_inference(
writer_utils.load_file(_TFLITE_MODEL_PATH),
[_INPUT_NORM_MEAN], [_INPUT_NORM_STD],
[_LABELS_FILE])
# Verify the metadata generated
print(metadata_generator.get_metadata_json())
# Integrate the metadata into the TFlite model
writer_utils.save_file(metadata_generator.populate(), _TFLITE_METADATA_MODEL_PATHS)全是这些人!
发布于 2022-03-27 07:28:38
您可以使用tdfs、dataset、dataset_image、tf.constants和其他数据格式。
您还可以在输入所需参数时使用tf.constants,也可以输入权重算法。(卷积层也能)
我确定输入和目标响应分解。
序列到序列映射
group_1_ShoryuKen_Left = tf.constant([ 0,0,0,0,0,1,0,0,0,0,0,0, 0,0,0,0,0,1,0,1,0,0,0,0, 0,0,0,0,0,0,0,1,0,0,0,0, 0,0,0,0,0,0,0,0,0,1,0,0 ], shape=(1, 1, 48), dtype=tf.float32)
# get_weights
layer1_lstm = model.get_layer( name="layer1_bidirection-lstm" )
lstm_weight_1 = layer1_lstm.get_weights()[0]
lstm_filter_1 = layer1_lstm.get_weights()[1]
# set weights
layer1_lstm = model.get_layer( name="layer1_bidirection-lstm " )
layer1_conv.set_weights([lstm_weight_1, lstm_filter_1])TDFS
builder = tfds.builder('cats_vs_dogs', data_dir='file:\\\\F:\\datasets\\downloads\\PetImages\\')
ds = tfds.load('cats_vs_dogs', split='train', shuffle_files=True)
assert isinstance(ds, tf.data.Dataset)
data = DataLoader.from_folder('F:\\datasets\\downloads\\flower_photos\\')
train_data, test_data = data.split(0.9)
for example in ds.take(1):
image, label = example["image"], example["label"]
model = image_classifier.create(train_data)..。

https://stackoverflow.com/questions/71634333
复制相似问题