Skip to main content
 首页 » 编程设计

python之如何为 .tflite 输入转换图像

2024年11月24日9sxdcgaq8080

我正在尝试获取模型(在转换为 .tflite 之前已成功运行)来对测试图像进​​行分类。

img = Image.open( input_path ) 
img.load() 
input_data = np.array(img, dtype=np.int32) 
interpreter.set_tensor(input_details[0]['index'], input_data) 
interpreter.invoke() 

但是我得到一个错误ValueError: Cannot set tensor: Got tensor of type 2 but expected type 1 for input 13

这指的是 dtype,我相信 int32 是正确的格式,我几乎尝试了所有其他格式,但我仍然遇到错误,这不是很好描述。

因为我的 dtype 是正确的,所以我认为我一定是错误地转换了图像。有没有更好的方法来转换此图像以提交给模型?

请您参考如下方法:

试试这个:

input_data = np.array(img, dtype=np.float32) 

你可以随时检查

print(interpreter.get_input_details()) 

需要哪个数据类型