安装Paddle-Lite develop版本,在树莓派4b上Python推理报“浮点数例外”
Created by: zjpipix
安装脚本命令:./lite/tools/build_linux.sh --arch=armv7hf --with_python=ON --with_cv=ON full_publish
推理代码:
# -*- coding:utf-8 -*-
from paddlelite.lite import *
import numpy as np
import cv2
def load_image(path):
#读入图片
img = cv2.imread(path)
#调整尺寸到100*100
img = cv2.resize(img, (100, 100))
#转换数据类型为float32
img = np.array(img).astype('float32')
#修改数据形状为paddle默认的(通道,高度,宽度)
img = img.transpose((2, 0, 1))
#对图像数据进行归一化
img = img/255.0
#print(img.shape)
return img
config = MobileConfig()
#config.set_model_from_file("./mydnn_opt.nb")
config.set_model_from_file("./gesture_infer_opt.nb")
predictor = create_paddle_predictor(config)
#image = Image.open('./five.png')
#resized_image = image.resize((224, 224), Image.BILINEAR)
infer_img = load_image('./five.png')
infer_img=np.array(infer_img).astype('float32')
infer_img=infer_img[np.newaxis,:, : ,:]
print("infer_img shape :", infer_img.shape)
infer_img = np.array(infer_img).flatten().tolist()
#image_data = np.array(resized_image).flatten().tolist()
#print("2. prepare image_data shape:",len(infer_img))
print("2. prepare image_data shape: ", type(infer_img))
input_tensor = predictor.get_input(0)
print("3. input_tensor")
input_tensor.resize([1, 3, 100, 100])
#input_tensor.set_float_data( [1.]*3*100*100)
#input_tensor.set_float_data( image_data )
input_tensor.set_float_data( infer_img )
print("3. before run")
predictor.run()
print("4. after run" )
output_tensor = predictor.get_output(0)
#print(output_tensor.shape())
print("5. get output_tensor")
print(output_tensor)
#print(output_tensor.float_data()[:10])
run的时候报“浮点数例外”