【发布时间】: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