【问题标题】:TensorFlowLite error interpreter.set_tensor()TensorFlowLite 错误解释器.set_tensor()
【发布时间】:2021-01-06 18:05:15
【问题描述】:

我的 keras 模型:

model = Sequential()
model.add(keras.layers.InputLayer(input_shape=(1134,), dtype='float64'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(keras.layers.Dropout(0.35))
model.add(Dense(3, activation='softmax'))

训练后,我将模型转换为 tflite

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [tf.lite.OpsSet.SELECT_TF_OPS] <-- without this I will get an error
tflite_model = converter.convert()

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

然后我要测试模型:

interpreter = tf.lite.Interpreter(model_path="/content/model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']
inp = np.expand_dims(X[0], axis=0)
interpreter.set_tensor(input_details[0]['index'], inp) <-- in this line i get error```

Error:

ValueError:无法设置张量:得到类型 NOTYPE 的值,但输入 0 的预期类型为 FLOAT64,名称:input_1 ```

【问题讨论】:

  • 请澄清X[0]里面的内容
  • np.array 由 1134 个 np.float64 数字组成
  • 以防万一检查inp 以确保它内部有浮动。还有你的input_shape 是什么?
  • inp里面确实是float64,我查了一下。 input_shape 基于文档是 - 形状元组(不包括批处理轴),或 TensorShape 实例(不包括批处理轴)。
  • 那么一切都应该正常。)但是set_tensor() 会复制数据,也许您在此过程中遇到了一些问题。尝试使用tensor 策略。

标签: python tensorflow keras tensorflow-lite


【解决方案1】:

试试这个:

interpreter = tf.lite.Interpreter(model_path="/content/model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']
inp = np.expand_dims(X[0], axis=0)

inp = inp.astype(np.float64) # This was missing

interpreter.set_tensor(input_details[0]['index'], inp)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多