【问题标题】:Converting Mobilenet Model to TFLite changes input size将 Mobilenet 模型转换为 TFLite 会更改输入大小
【发布时间】:2021-05-29 22:51:03
【问题描述】:

现在我正在尝试将 SavedModel 转换为 TFLite,以便在树莓派上使用。该模型是在自定义数据集上训练的 MobileNet 对象检测。 SavedModel 完美运行,并保留了与(1, 150, 150, 3) 相同的形状。但是,当我使用此代码将其转换为 TFLite 模型时:

import tensorflow as tf

saved_model_dir = input("Model dir: ")

# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory
tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

并运行这段代码来运行解释器:

import numpy as np
import tensorflow as tf
from PIL import Image

from os import listdir
from os.path import isfile, join

from random import choice, random

# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()


input_shape = input_details[0]['shape']
print(f"Required input shape: {input_shape}")

我得到了[1 1 1 3] 的输入形状,因此我不能使用 150x150 的图像作为输入。

我在 Python 3.7.10 和 Windows 10 上使用 Tensorflow 2.4。

我该如何解决这个问题?

【问题讨论】:

    标签: python tensorflow tensorflow-lite mobilenet


    【解决方案1】:

    在调用 allocate_tensors() 之前调用 resize_tensor_input() 怎么样?

    interpreter.resize_tensor_input(0, [1, 150, 150, 3], strict=True)
    interpreter.allocate_tensors()
    

    【讨论】:

      【解决方案2】:

      您可以依靠 TFLite 转换器 V1 API 来设置输入形状。请查看https://www.tensorflow.org/api_docs/python/tf/compat/v1/lite/TFLiteConverter 中的 input_shapes 参数。

      【讨论】:

      • 你能分享一个代码sn-p而不是一个链接吗?
      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 2023-01-17
      • 2019-05-06
      • 2021-02-13
      相关资源
      最近更新 更多