如何解决TensorFlowLite 错误 interpreter.set_tensor()
我的 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 ```
解决方法
试试这个:
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。