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

paddleocr转换ncnn求助,尝试两条路径无果。 #5470

Closed
ItGql opened this issue May 23, 2024 · 3 comments
Closed

paddleocr转换ncnn求助,尝试两条路径无果。 #5470

ItGql opened this issue May 23, 2024 · 3 comments

Comments

@ItGql
Copy link

ItGql commented May 23, 2024

error log | 日志或报错信息 | ログ

路径1: 使用paddleocr转化onnx再转ncnn

模型

  1. original model
    paddleocr v4: https://github.com/PaddlePaddle/PaddleOCR/blob/main/doc/doc_ch/models_list.md#2-%E6%96%87%E6%9C%AC%E8%AF%86%E5%88%AB%E6%A8%A1%E5%9E%8B 链接中的ch_PP-OCRv4_rec推理模型

复现步骤

1.paddleocr转onnx
2.onnxsimplify生成rec_sim.onnx
3.onnx2ncnn错误提示:
image
4.按照blog修改算子,blog链接如下: https://blog.csdn.net/qq_29543997/article/details/128923330?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-128923330-blog-124155888.235%5Ev43%5Econtrol&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-128923330-blog-124155888.235%5Ev43%5Econtrol&utm_relevant_index=11
5.转换后能成功运行,但是精度降低,与原始onnx相比错误率增加挺多。
`image = cv2.imread(os.path.join(folder_path, filepath)) # 用您的测试图片路径替换 test_image.jpg

        # 图像预处理后的尺寸
        height, width = image.shape[:2]
        # print("原始imgae", image.shape)
        # print(image.transpose((2, 0, 1)))
        img = resize_norm_img(image, 320 * 1.0 / 48)
        # print("norm image", img.shape)
        # print(img)

        img = img[np.newaxis, :]

        onnx_model = onnxruntime.InferenceSession('data/models/recv4_sim.onnx')
        inputs = {onnx_model.get_inputs()[0].name: img}
        outs = onnx_model.run(None, inputs)
        process_op = process_pred('data/ppocr_keys_v1.txt', 'ch', True)
        result = process_op(outs[0])

        print(result)


        print("ncnn----------------------------------")
        # 加载ncnn模型
        net = ncnn.Net()

        net.load_param("data/models/recv4_sim.param")  # 用您的模型文件名替换 your_model.param
        net.load_model("data/models/recv4_sim.bin")  # 用您的模型文件名替换 your_model.bin
        raw_inputs = ncnn.Mat.from_pixels_resize(image, ncnn.Mat.PixelType.PIXEL_BGR, width, height, 320, 48)
        mean_vals = [0.5 * 255, 0.5 * 255, 0.5 * 255]
        norm_vals = [1/0.5 / 255, 1/0.5 / 255, 1/0.5 / 255]
        raw_inputs.substract_mean_normalize(mean_vals, norm_vals)
        # print("norm_img", raw_inputs.shape)
        # print(raw_inputs)


        ex = net.create_extractor()

        ex.input("x", raw_inputs)
        mat_out = ncnn.Mat()
        ex.extract("softmax_11.tmp_0", mat_out)
        output_data = np.array(mat_out)
        out = np.expand_dims(output_data, axis=0)
        print("ncnnresult-----")
        process_op = process_pred('data/ppocr_keys_v1.txt', 'ch', True)
        result = process_op(out)
        print(result)`

路径2: 使用pytorch,用pnnx转ncnn
复现步骤:

  1. 通过下面的仓库得到pytorch版本的ocrv4,https://github.com/frotms/PaddleOCR2Pytorch/blob/main/README_en.md

  2. 并在tools/infer/predict_rec 102行之后增加下属代码导出pt文件
    `print("导出pt开始")
    example = torch.rand(1, 3, 48, 320)

     traced_script_module = torch.jit.trace(self.net, example)
    
     # save traced TorchScript model
     traced_script_module.save("traced_model.pt")
     print("导出ptover")
    

`
3.使用pnnx工具转化pt文件,报错信息如下,不支持5d的tensor操作转换:
$HN3{52A%%83KS(FTZF2P A

@nihui
Copy link
Member

nihui commented Jun 3, 2024

#5485

@nihui
Copy link
Member

nihui commented Jun 4, 2024

更新最新的 pnnx

./pnnx ppocr-v4-rec.pnnx inputshape=[1,3,48,256] inputshape2=[1,3,48,512]

@nihui nihui closed this as completed Jun 4, 2024
@drivingchange
Copy link

drivingchange commented Jul 8, 2024

@ItGql 你好,通过pnnx转你成功了么,我这边可以转,但是跑pnnx生成的ncnn.py与pnnx.py,发现推理结果不一致,这正常吗

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

3 participants