tensorflow TFlite model.process()有时需要输入数据TensorImage,有时需要TensorBuffer来处理图像?是否有不同的图像输入数据?

f87krz0w  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(131)

一些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),<input_data>在调用model.process()时需要TensorBuffer*<input_data>*
其次,我已经使用TensorFlow Lite Model Maker训练并保存为TFLite模型,现在我有一个TFLite模型,<input_data>在调用model.process()时需要
TensorImage**<input_data>。
是否有两种不同的TFlite模型取决于您如何构建和训练它?
也许这与Keras模型基于Inception而TensorFlow Lite Model Maker使用EfficientNet的事实有关。如何从一个TFlite模型转换到另一个?如何更改图像的输入以便能够处理相同的图像,例如TensorImage或位图数据输入?

sqxo8psd

sqxo8psd1#

在@Farmaker的宝贵帮助下,我解决了我的问题。我只是想将Keras模型转换为更紧凑的TFlite模型,以便将其安装在移动的应用程序中。我意识到生成的TFlite模型不兼容,@Farmaker非常正确地向我指出缺少元数据。
1.您应该使用TensorFlow 2.6.0或更低版本,因为与Flatbuffer不兼容。

pip3 uninstall tensorflow
pip3 install tensorflow==2.6.0
pip3 install keras==2.6.0

1.将Keras模型转换为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)

1.添加元数据,如« TensorFlow Lite元数据编写器API » tutorial中所示

  • 3.1提供一个labels.txt文件(一个包含所有目标类标签的文件,一行一个标签)

例如,要创建这样一个文件,

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提供额外的库以支持TFlite元数据生成
pip3 install tflite-support-nightly
  • 3.3生成元数据
from 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)

大家都到齐了!

vmdwslir

vmdwslir2#

您可以使用tdfs、dataset、dataset_image、tf.constants和其他数据格式。
您也可以使用tf.常量在您输入所需的参数或您可以输入权重算法。(卷积层也可以)
我确定输入和目标响应分类。

[序列到序列Map]:

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)

Sample

相关问题