MNN icon indicating copy to clipboard operation
MNN copied to clipboard

转换模型pytorch -> onnx -> mnn成功,但用python和C++验证模型运行时,runSession 出现 Segmentation Fault (core dump)?

Open SomeO opened this issue 4 years ago • 4 comments

模型转换成功,但运行时出现segmentation fault错误

从pytorch转换onnx,以及onnx转换mnn均成功转换,并验证pytorch和onnx模型输出无误,但转换成mnn模型后,在python和C++环境下测试mnn模型,均出现Segmentation Fault(core dump) 错误,没有更多输出信息,以下为python测试代码:

MNN : 1.1.0 版本 使用的MNNConvert模型转换工具也是对应版本的代码编译的,且可以成功转换其他模型并成功运行

from __future__ import print_function

import numpy as np
import MNN
import time
import cv2
from PIL import Image
from memory_profiler import profile

def mnn_input_transform(input_image):
    image = input_image.copy()
    mean = (128, 128, 128)
    normal = (0.007843137, 0.01750700, 0.01742919)
    image = image - mean
    image = image * normal

    return image.astype(np.float32)

def image_process(image):
    '''
    description: 
    param {*}
    return {*}
    '''    
    image = cv2.imread(image)
    image = cv2.resize(image, (512, 512))
    image = mnn_input_transform(image)
    #preprocess it
    image = image.transpose((2, 0, 1))
    print("[*] image shape is : {}".format(image.shape))

    return image

# @profile
def inference():
    """ inference hrnet.onnx.mnn using a specific picture """
    interpreter = MNN.Interpreter("/path/to/net.mnn")
    session = interpreter.createSession()
    # input tensor info
    input_tensor = interpreter.getSessionInput(session)
    print("[*] input tensor shape : ", input_tensor.getShape())
    # output tensor info
    output_tensor = interpreter.getSessionOutput(session)
    print("[*] output tensor shape : ", output_tensor.getShape())
    
    image = "/path/to/image.jpg"
    image = image_process(image)
    _, h, w = image.shape
    tmp_input = MNN.Tensor((1, 3, h, w), MNN.Halide_Type_Float,\
                    image, MNN.Tensor_DimensionType_Caffe)
    input_tensor.copyFrom(tmp_input)
    interpreter.runSession(session)
    
    #constuct a tmp tensor and copy/convert in case output_tensor is nc4hw4
    tmp_output = MNN.Tensor((1, 1, h, w), MNN.Halide_Type_Float, np.ones([1, 1, h, w]).astype(np.float32), MNN.Tensor_DimensionType_Caffe)
    output_tensor.copyToHostTensor(tmp_output)

    # output mask 
    matte = output_tensor[0][0]
    Image.fromarray(((matte * 255).astype('uint8')), mode='L').save("/path/to/output.png")

if __name__ == "__main__":
    start = time.time()
    inference()
    print("[*] inference cost time : {}".format(time.time() - start))

C++的测试代码与demo/exec/segment.cpp类似,此处不贴出。

以下为转换后的mnn模型:

net.zip

没有更多的报错信息,多次尝试定位错误无果,还望各位大佬帮忙

SomeO avatar Jan 19 '21 09:01 SomeO

我也遇到一樣的問題

JackLin-Authme avatar Feb 23 '21 03:02 JackLin-Authme

遇到过类似的问题,我是把版本更新到1.1.4 解决的;

phecda-xu avatar May 12 '21 06:05 phecda-xu

我也遇到这样的问题,现在都没有解决,请问最后是怎么解决了吗

zengchan avatar May 14 '21 07:05 zengchan

我遇到同样的问题,用MNN 1.2 从onnx转到mnn,fastTestOnnx.py测试能通过,但是推理时直接报错Segmentation Fault (core dump)。MNN 版本降低到 1.1就好了。

newcoder01n avatar Mar 20 '22 08:03 newcoder01n

Marking as stale. No activity in 60 days.

github-actions[bot] avatar Jan 29 '24 09:01 github-actions[bot]