Comments (4)
predict.py如下
from paddleocr.
import os
import time
import sys
sys.path.append('PaddleDetection')
parent_path = os.path.abspath(os.path.join(file, *(['..'] * 2)))
sys.path.insert(0, parent_path)
import json
import yaml
from functools import reduce
import multiprocessing
from concurrent.futures import ThreadPoolExecutor, as_completed
from PIL import Image
import cv2
import numpy as np
import paddle
from paddle.inference import Config
from paddle.inference import create_predictor
from deploy.python.preprocess import preprocess, Resize, NormalizeImage, Permute, PadStride
from deploy.python.utils import argsparser, Timer, get_current_memory_mb
class PredictConfig():
"""set config of preprocess, postprocess and visualize
Args:
model_dir (str): root path of model.yml
"""
def __init__(self, model_dir):
# parsing Yaml config for Preprocess
deploy_file = os.path.join(model_dir, 'infer_cfg.yml')
with open(deploy_file) as f:
yml_conf = yaml.safe_load(f)
self.arch = yml_conf['arch']
self.preprocess_infos = yml_conf['Preprocess']
self.min_subgraph_size = yml_conf['min_subgraph_size']
self.labels = yml_conf['label_list']
self.mask = False
self.use_dynamic_shape = yml_conf['use_dynamic_shape']
if 'mask' in yml_conf:
self.mask = yml_conf['mask']
self.tracker = None
if 'tracker' in yml_conf:
self.tracker = yml_conf['tracker']
if 'NMS' in yml_conf:
self.nms = yml_conf['NMS']
if 'fpn_stride' in yml_conf:
self.fpn_stride = yml_conf['fpn_stride']
self.print_config()
def print_config(self):
print('%s: %s' % ('Model Arch', self.arch))
for op_info in self.preprocess_infos:
print('--%s: %s' % ('transform op', op_info['type']))
def get_test_images(infer_file):
with open(infer_file, 'r') as f:
dirs = f.readlines()
images = [eval(repr(dir.replace('\n', ''))).replace('\', '/') for dir in dirs]
assert len(images) > 0, "no image found in {}".format(infer_file)
return images
def load_predictor(model_dir):
config = Config(
os.path.join(model_dir, 'model.pdmodel'),
os.path.join(model_dir, 'model.pdiparams'))
# initial GPU memory(M), device ID
config.enable_use_gpu(2000, 0)
# optimize graph and fuse op
config.switch_ir_optim(True)
# disable print log when predict
config.disable_glog_info()
# enable shared memory
config.enable_memory_optim()
# disable feed, fetch OP, needed by zero_copy_run
config.switch_use_feed_fetch_ops(False)
predictor = create_predictor(config)
return predictor, config
def create_inputs(imgs, im_info):
inputs = {}
im_shape = [np.array((e['im_shape'], )).astype('float32') for e in im_info]
scale_factor = [np.array((e['scale_factor'], )).astype('float32') for e in im_info]
origin_scale_factor = np.concatenate(scale_factor, axis=0)
imgs_shape = [[e.shape[1], e.shape[2]] for e in imgs]
max_shape_h = max([e[0] for e in imgs_shape])
max_shape_w = max([e[1] for e in imgs_shape])
padding_imgs = []
padding_imgs_shape = []
padding_imgs_scale = []
for img in imgs:
im_c, im_h, im_w = img.shape[:]
padding_im = np.zeros(
(im_c, max_shape_h, max_shape_w), dtype=np.float32)
padding_im[:, :im_h, :im_w] = np.array(img, dtype=np.float32)
padding_imgs.append(padding_im)
padding_imgs_shape.append(
np.array([max_shape_h, max_shape_w]).astype('float32'))
rescale = [float(max_shape_h) / float(im_h), float(max_shape_w) / float(im_w)]
padding_imgs_scale.append(np.array(rescale).astype('float32'))
inputs['image'] = np.stack(padding_imgs, axis=0)
inputs['im_shape'] = np.stack(padding_imgs_shape, axis=0)
inputs['scale_factor'] = origin_scale_factor
return inputs
class Detector(object):
def __init__(self,
pred_config,
model_dir,
device='CPU',
run_mode='paddle',
batch_size=1,
trt_min_shape=1,
trt_max_shape=1280,
trt_opt_shape=640,
trt_calib_mode=False,
cpu_threads=1,
enable_mkldnn=False):
self.pred_config = pred_config
self.predictor, self.config = load_predictor(model_dir)
self.det_times = Timer()
self.cpu_mem, self.gpu_mem, self.gpu_util = 0, 0, 0
self.preprocess_ops = self.get_ops()
def get_ops(self):
preprocess_ops = []
for op_info in self.pred_config.preprocess_infos:
new_op_info = op_info.copy()
op_type = new_op_info.pop('type')
preprocess_ops.append(eval(op_type)(**new_op_info))
return preprocess_ops
def predict(self, inputs):
# preprocess
input_names = self.predictor.get_input_names()
for i in range(len(input_names)):
input_tensor = self.predictor.get_input_handle(input_names[i])
input_tensor.copy_from_cpu(inputs[input_names[i]])
np_boxes, np_boxes_num = [], []
# model_prediction
self.predictor.run()
np_boxes.clear()
np_boxes_num.clear()
output_names = self.predictor.get_output_names()
num_outs = int(len(output_names) / 2)
for out_idx in range(num_outs):
np_boxes.append(
self.predictor.get_output_handle(output_names[out_idx])
.copy_to_cpu())
np_boxes_num.append(
self.predictor.get_output_handle(output_names[
out_idx + num_outs]).copy_to_cpu())
np_boxes, np_boxes_num = np.array(np_boxes[0]), np.array(np_boxes_num[0])
return dict(boxes=np_boxes, boxes_num=np_boxes_num)
def process_image(detector, im_path, threshold):
c_result = []
image_id = os.path.basename(im_path).split('.')[0]
im, im_info = preprocess(im_path, detector.preprocess_ops)
inputs = create_inputs([im], [im_info])
det_results = detector.predict(inputs)
im_bboxes_num = det_results['boxes_num'][0]
if im_bboxes_num > 0:
bbox_results = det_results['boxes'][0:im_bboxes_num, 2:]
id_results = det_results['boxes'][0:im_bboxes_num, 0]
score_results = det_results['boxes'][0:im_bboxes_num, 1]
for idx in range(im_bboxes_num):
if float(score_results[idx]) >= threshold:
c_result.append({"image_id": image_id,
"type": int(id_results[idx]) + 1,
"x": float(bbox_results[idx][0]),
"y": float(bbox_results[idx][1]),
"width": float(bbox_results[idx][2]) - float(bbox_results[idx][0]),
"height": float(bbox_results[idx][3]) - float(bbox_results[idx][1]),
"segmentation": []})
return c_result
def predict_image(detector, image_list, result_path, threshold):
c_results = {"result": []}
with ThreadPoolExecutor(max_workers=2) as executor:
future_to_image = {executor.submit(process_image, detector, image, threshold): image for image in image_list}
for future in as_completed(future_to_image):
c_results["result"].extend(future.result())
# 写文件
with open(result_path, 'w') as ft:
json.dump(c_results, ft)
def main(infer_txt, result_path, det_model_path, threshold):
pred_config = PredictConfig(det_model_path)
detector = Detector(pred_config, det_model_path)
# predict from image
img_list = get_test_images(infer_txt)
predict_image(detector, img_list, result_path, threshold)
if name == 'main':
start_time = time.time()
det_model_path = "/home/aistudio/PaddleSlim/example/auto_compression/detection/output"
#det_model_path = "/home/aistudio/PaddleDetection/output_inference/rtdetr_r101vd_6x_coco"
threshold = 0.5
paddle.enable_static()
infer_txt = sys.argv[1]
result_path = sys.argv[2]
main(infer_txt, result_path, det_model_path, threshold)
print('total time:', time.time() - start_time)
print('FPS:',len(get_test_images(infer_txt))/(time.time() - start_time))
from paddleocr.
This is a Paddle framework issue. You can try updating your PaddlePaddle version or submit an issue to the paddlepaddle/paddle repository.
from paddleocr.
This is a Paddle framework issue. You can try updating your PaddlePaddle version or submit an issue to the paddlepaddle/paddle repository.
升级到PaddlePaddle 2.6.1最新版本之后又变成这个错误了
/home/aistudio
Model Arch: DETR
--transform op: Resize
--transform op: NormalizeImage
--transform op: Permute
Traceback (most recent call last):
File "/home/aistudio/predict.py", line 221, in
main(infer_txt, result_path, det_model_path, threshold)
File "/home/aistudio/predict.py", line 208, in main
predict_image(detector, img_list, result_path, threshold)
File "/home/aistudio/predict.py", line 196, in predict_image
c_results["result"].extend(future.result())
File "/opt/conda/envs/python35-paddle120-env/lib/python3.10/concurrent/futures/_base.py", line 451, in result
return self.__get_result()
File "/opt/conda/envs/python35-paddle120-env/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
raise self._exception
File "/opt/conda/envs/python35-paddle120-env/lib/python3.10/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/aistudio/predict.py", line 171, in process_image
det_results = detector.predict(inputs)
File "/home/aistudio/predict.py", line 149, in predict
self.predictor.run()
ValueError: (InvalidArgument) The type of data we are trying to retrieve (float32) does not match the type of data (int8) currently contained in the container.
[Hint: Expected dtype() == phi::CppTypeToDataType::Type(), but received dtype():3 != phi::CppTypeToDataType::Type():10.] (at /paddle/paddle/phi/core/dense_tensor.cc:171)
[operator < fused_fc_elementwise_layernorm > error]
from paddleocr.
Related Issues (20)
- 最新的release2.7.5代码无法运行 HOT 1
- SER index error: IndexError: (OutOfRange) label value should less than the shape of axis dimension when label value(2) not equal to ignore_index(-100)
- 自己训练ch_PP-OCRv4_rec_svtr_large.yml,导出模型时报错,怎么解决
- 自己训练rec svtr ocr4模型导出后推理报错
- KIE: question about num_classes parameter
- 中文版面分析CDLA,自己训练出来的验证集bbox ap比官方的picodet_lcnet_x1_0_fgd_layout_cdla低好几个点
- PaddleOCR returning only the first page when performing ocr on a PDF
- 短数字无法检测问题 HOT 1
- kie_ser训练问题
- 使用PP-OCRv3做文字检测微调,在T4上跑训练无法训练,报错返回-9 HOT 4
- Paddle OCR 推理模型转ONNX,固定shape后,ONNX结果相差很大,不固定shape,结果与paddle推理模型保持一致,这个问题要怎么处理哇: HOT 1
- 关于[中文混合拼音]的长文本OCR方案请教(eg: 灿烂的笑róng) HOT 5
- indonesian 是哪个字典?没有找到id 的 HOT 2
- PPStructure版面分析时对原图返回的是figure,对原图灰度化后却返回table是为什么,请问怎么指定他返回figure? HOT 1
- 有一个中文字识别不出来 HOT 7
- OSError: (External) CUDA error(209), no kernel image is available for execution on the device. HOT 3
- 测试russia识别别字典报错 HOT 3
- pyinstaller打包后不能运行,不打包只跑py程序正常,报错是RuntimeError: MKL_Free_Buffers not found. HOT 3
- 引用PaddleOCR模块后会导致logging的默认logger中添加一项haddler HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from paddleocr.