Android无法加载MobileNet-SSD预测模型
Created by: yeyupiaoling
我编译的是最新的PaddleMobile,使用MobileNet-SSD训练得到的模型,在加载的时候就报错,报错信息为
I/paddle_mobile LOG built on Jan 11 2019 17:32:19: loadCombined invoked
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 22479 (aoling.aicamera), pid 22479 (aoling.aicamera)
MobileNet-SSD合并预测模型: infer_model.zip
MobileNet-SSD未合并预测模型: infer_model2.zip
我使用Python代码测试是正常的。
import os
import cv2
import numpy as np
import paddle.fluid as fluid
from PIL import Image
# 定义预测图像大小
resize_w = 300
resize_h = 300
confs_threshold = 0.45
# 创建执行器
place = fluid.CUDAPlace(0)
# place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
# 保存预测模型路径
save_path = 'model/infer_model/'
# 从模型中获取预测程序、输入数据名称列表、分类器
[infer_program, feeded_var_names, target_var] = fluid.io.load_inference_model(dirname=save_path,
executor=exe,
model_filename='model.paddle',
params_filename='params.paddle')
# 对图像进行预处理
def load_image(image_path):
img = Image.open(image_path)
if img.mode == 'L':
img = img.convert('RGB')
img = img.resize((resize_w, resize_h), Image.ANTIALIAS)
img = np.array(img)
# HWC to CHW
if len(img.shape) == 3:
img = np.swapaxes(img, 1, 2)
img = np.swapaxes(img, 1, 0)
# RBG to BGR
img = img[[2, 1, 0], :, :]
img = img.astype('float32')
# 减均值
img -= 127.5
img = img * 0.007843
img = np.expand_dims(img, axis=0)
return img
# 将要预测的图片路径
imange_path = 'images/infer_image.jpg'
# 获取图片数据
img = load_image(imange_path)
# 执行预测
result = exe.run(program=infer_program,
feed={feeded_var_names[0]: img},
fetch_list=target_var,
return_numpy=False)
result = np.array(result[0])
# 获取没有类别的名称
with open("data/pascalvoc/label_list", 'r') as f:
names = f.readlines()
# 在图像上画框并保存
img = cv2.imread(imange_path)
for dt in result:
if dt[1] < confs_threshold:
continue
print(dt)
bbox = dt[2:]
h, w, c = img.shape
# 画矩形框
cv2.rectangle(img=img, pt1=(int(bbox[0] * w), int(bbox[1] * h)),
pt2=(int(bbox[2] * w), int(bbox[3] * h)), color=(255, 0, 0), thickness=1)
# 写上类别名称
cv2.putText(img, names[int(dt[0])].replace('\n', ''), (int(bbox[0] * w), int(bbox[1] * h - 2)),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 1)
# 写上识别类别的概率
cv2.putText(img, "%.3f" % dt[1], (int(bbox[2] * w - 60), int(bbox[1] * h - 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(0, 255, 255), 1)
# 保存已经画框的图像
if not os.path.exists('images'):
os.makedirs('images')
cv2.imwrite('images/result_image.jpg', img)
print("save image to: images/result_image.jpg")