【问题标题】:How can i change interpreter output shape?如何更改解释器输出形状?
【发布时间】:2020-08-11 21:20:49
【问题描述】:

我正在尝试在 android 上构建一个 tflite 文件。

所以我使用 jupyter notebook 创建了以下模型。

将创建的模型转换为tflite文件后,我们检查它是否正确转换。

我希望输出解释器形状结果是1this,但结果还是1[10]我该怎么办?

我制作这样的模型层。

model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32, (3,3), padding="same", input_shape=X_train.shape[1:], activation="relu"),
  tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
  tf.keras.layers.Conv2D(32, (3,3), padding="same", activation="relu"),
  tf.keras.layers.MaxPooling2D(pool_size=(2,2)),

  tf.keras.layers.Conv2D(64, (3,3), padding="same", activation="relu"),
  tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
  tf.keras.layers.Dropout(0.25),

  tf.keras.layers.Conv2D(64, (3,3), padding="same", activation="relu"),
  tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
  tf.keras.layers.Dropout(0.25),

  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(256, activation="relu"),
  tf.keras.layers.Dropout(0.5),
  tf.keras.layers.Dense(1, activation="sigmoid")
])

将训练好的模型转换为 tflite 文件的一部分

model = tf.keras.models.load_model("./model/model.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)


interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
print("--------------")
print("shape:", input_details[0]['shape'])
print("type:", input_details[0]['dtype'])
output_details = interpreter.get_output_details()
print("--------------")
print("shape:", output_details[0]['shape'])
print("type:", output_details[0]['dtype'])

interpreter.resize_tensor_input(input_details[0]['index'], (39, 64, 64))
interpreter.resize_tensor_input(output_details[0]['index'], (39, 5))
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
print("--------------")
print("shape:", input_details[0]['shape'])
print("type:", input_details[0]['dtype'])
output_details = interpreter.get_output_details()
print("--------------")
print("shape:", output_details[0]['shape'])
print("type:", output_details[0]['dtype'])

enter image description here

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0 tensorflow-lite


    【解决方案1】:

    使用 Keras 创建模型时,您可以使用:

    from tensorflow.keras.layers import Input
    from tensorflow.keras.models import Model
    
    # some stuff your code does
    
    input = Input((THE_HEIGHT_YOU_WANT, THE_WIDTH_YOU_WANT, THE_CHANNELS_YOU_WANT))
    
    # all the Tensorflow ops you want on "input"
    
    model = Model(input, THE_OUTPUT_YOU_WANT)
    
    # any other stuff your code might do before saving
    
    model.save(THE_PATH_YOU_WANT)
    
    loaded_model = tf.keras.models.load_model(THE_PATH_YOU_WANT)
    
    # rest of your code for converting and saving the model
    

    现在在 Android 上运行时,您可以使用:

    tensorflowLiteInterpreterInstance.getInputTensor(inputTensorIndex).shape()
    

    获取模型的形状。

    形状应与(THE_HEIGHT_YOU_WANT, THE_WIDTH_YOU_WANT, THE_CHANNELS_YOU_WANT) 形状匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多