Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predictor.run()之后无结果 #10497

Open
Zhu-TianYu opened this issue Apr 15, 2024 · 2 comments
Open

predictor.run()之后无结果 #10497

Zhu-TianYu opened this issue Apr 15, 2024 · 2 comments
Assignees

Comments

@Zhu-TianYu
Copy link

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

@hong19860320
Copy link
Collaborator

建议升级到最新的 paddlelite
pip install paddlelite==2.13rc0
然后在运行前 export GLOG_v=5 就可以打开 log,执行的时候加上 >log.txt 2>&1 就可以将 log 重定向到 log.txt 文件,最后把 log.txt 连同原始的 paddle 模型放上来哈~

@hong19860320
Copy link
Collaborator

能把原始的 paddle 模型发上来吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants