PaddlePaddle/PaddleDetection

predictor.run()之后无结果

Zhu-TianYu opened this issue · 0 comments

问题确认 Search before asking

  • 我已经搜索过问题,但是没有找到解答。I have searched the question and found no related answer.

请提出你的问题 Please ask your question

PaddleDetection/configs/yolov3
/yolov3_mobilenet_v3_large_ssld_270e_voc.yml

_BASE_: [
  '../datasets/voc.yml',
  '../runtime.yml',
  '_base_/optimizer_270e.yml',
  '_base_/yolov3_mobilenet_v3_large.yml',
  '_base_/yolov3_reader.yml',
]

snapshot_epoch: 5
pretrain_weights:  https://paddledet.bj.bcebos.com/models/pretrained/MobileNetV3_large_x1_0_ssld_pretrained.pdparams
weights: output/yolov3_mobilenet_v3_large_ssld_270e_voc/model_final

# set collate_batch to false because ground-truth info is needed
# on voc dataset and should not collate data in batch when batch size
# is larger than 1.
EvalReader:
  collate_batch: false

LearningRate:
  base_lr: 0.001
  schedulers:
  - !PiecewiseDecay
    gamma: 0.1
    milestones:
    - 216
    - 243
  - !LinearWarmup
    start_factor: 0.
    steps: 1000

转换nb格式的代码

# encoding: utf-8
# 把__model__和__params__两个文件组成的模型转化为一个nb格式的模型
import paddlelite.lite as lite

a = lite.Opt()
# 方法一:非combined形式(文件夹格式为指定格式)
# a.set_model_dir("/home/pi/bigchuang/model/text/model")

# 方法二:conmbined形式
a.set_model_file(r"D:\Competition\bigchuang\model\Helmet_0415\model.pdmodel")
a.set_param_file(r"D:\Competition\bigchuang\model\Helmet_0415\model.pdiparams")

a.set_optimize_out(r"D:\Competition\bigchuang\model")  # 第一个model指文件夹,第二个model指文件名

# a.set_quant_model(True)
# a.set_quant_type('QUANT_INT16')

a.set_valid_places("x86")
# a.set_model_type("naive_buffer")

a.run()

预测代码

from paddlelite.lite import *
import numpy as np
import cv2
import time
from datetime import datetime
import base64
import random
import json
import requests
import threading

model_path = r"D:\Competition\bigchuang\model.nb"


def preprocess(image):
    """
    图形预处理

    :param image: 图片
    :return: 返回处理后,归一化后的图片
    """
    image = cv2.resize(image, (608, 608))
    image = np.array(image).transpose((2, 0, 1)).reshape([1, 3] + [608, 608]).astype('float32')
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.255])
    image[0, 0] = (image[0, 0] / 255. - mean[0]) / std[0]
    image[0, 1] = (image[0, 1] / 255. - mean[1]) / std[1]
    image[0, 2] = (image[0, 2] / 255. - mean[2]) / std[2]
    return image


frame = cv2.imread(r"C:\Users\John1\Desktop\hard_hat_workers0.png")

# h = int(frame.shape[0])
# w = int(frame.shape[1])
# if w / h > 608 / 608:
#     frame = frame[0:h, int((w - 608 * h / 608) / 2):int((w + 608 * h / 608) / 2)]
# elif w / h < 608 / 608:
#     frame = frame[int((h - 608 * w / 608) / 2):int((h + 608 * w / 608) / 2), 0:w]

print("预测")

Config = MobileConfig()
Config.set_model_from_file(model_path)
predictor = create_paddle_predictor(Config)

image_data = preprocess(frame)

# input_tensor1 = predictor.get_input(1)
# input_tensor1.resize([1, 3, 608, 608])
# input_tensor1.from_numpy(np.asarray(image_data, dtype=np.float32))
#
# input_tensor2 = predictor.get_input(2)
# input_tensor2.resize([1, 2])
# input_tensor2.from_numpy(np.asarray([[1, 1]], dtype=np.float32))
#
# input_tensor0 = predictor.get_input(0)
# input_tensor0.resize([1, 2])
# input_tensor0.from_numpy(np.asarray([[608, 608]], dtype=np.float32))

input_tensor1 = predictor.get_input(1)
input_tensor1.resize([1, 3, 608, 608])
input_tensor1.set_float_data(np.asarray(image_data, dtype=np.float32).flatten().tolist())

input_tensor2 = predictor.get_input(2)
input_tensor2.resize([1, 2])
input_tensor2.set_float_data(np.asarray([[1, 1]], dtype=np.float32).astype('float32').reshape(1, 2).flatten().tolist())

input_tensor0 = predictor.get_input(0)
input_tensor0.resize([1, 2])
input_tensor0.set_float_data(np.asarray([[608, 608]], dtype=np.float32).astype('float32').reshape(1, 2).flatten().tolist())

print("E")
predictor.run()
print("F")
# output_tensor0 = predictor.get_output(0)
# out_d = output_tensor0.numpy()
# print(out_d)

paddlelite版本1.8
paddledetection版本2.3
准换nb格式的模型之前可以预测,但是转换之后就卡在predictor.run()地方,然后程序直接退出
image