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

tflite转换为mnn模型后,结果不一致 #2332

Closed
gdyshi opened this issue Apr 14, 2023 · 10 comments
Closed

tflite转换为mnn模型后,结果不一致 #2332

gdyshi opened this issue Apr 14, 2023 · 10 comments
Assignees
Labels
bug Something isn't working Converter

Comments

@gdyshi
Copy link

gdyshi commented Apr 14, 2023

平台: windows python

版本: 使用pip安装mnn ,版本2.4.2

因为testMNNFromTflite.py只有转换模型,没有进行测试,我自己写代码进行测试。

测试模型输入8个神经元,输出6个神经元。
tflite模型测试代码为:

import tensorflow as tf
import numpy

def predict(interpreter, input_data):
    # Get input and output tensors.
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    input_shape = input_details[0]['shape']
    output_shape = output_details[0]['shape']

    d=numpy.reshape(input_data,(input_shape))
    interpreter.set_tensor(input_details[0]['index'], d)
    interpreter.invoke()
    _out= interpreter.get_tensor(output_details[0]['index'])
    return _out[0]

if __name__ == '__main__':
    # Load TFLite model and allocate tensors.
    interpreter = tf.lite.Interpreter('model.tflite')
    interpreter.allocate_tensors()
    _in = numpy.array([-46,57,-6,-10,46,74,66,-51],dtype=numpy.float32)
    _out = predict(interpreter, _in)
    print(_out)

运行结果:[ 0.62054753 4.4321437 8.301928 18.445957 8.4631405 26.668837 ]

mnn模型测试代码为:

import MNN
import numpy

def predict(interpreter,input_data):
    # 获取会话的输入输出
    input_tensor = interpreter.getSessionInput(session)
    output_tensor = interpreter.getSessionOutput(session)

    # input_data = MNN.Tensor(input_data) # Var -> Tensor
    input_data=input_data.reshape((1,8))
    i_data = MNN.Tensor(input_tensor.getShape(), MNN.Halide_Type_Float,input_data,MNN.Tensor_DimensionType_Caffe) # Var -> Tensor

    # 拷贝数据到输入Tensor
    input_tensor.copyFrom(i_data)

    # 执行会话推理
    interpreter.runSession(session)

    # 从输出Tensor拷贝出数据
    output_data = MNN.Tensor(output_tensor.getShape(), MNN.Halide_Type_Float, MNN.Tensor_DimensionType_Caffe)
    output_tensor.copyToHostTensor(output_data)

    return output_data.getData()

if __name__ == '__main__':
    interpreter = MNN.Interpreter("model.mnn")
    config = {}
    config['precision'] = 'high'
    config['backend'] = 'CPU'
    config['thread'] = 4
    session = interpreter.createSession(config)
    _in = numpy.array([-46,57,-6,-10,46,74,66,-51],dtype=numpy.float32)
    _out = predict(interpreter, _in)
    print(_out)

运行结果:(-71.7250747680664, -50.912940979003906, -34.28445053100586, -0.3217184543609619, 0.9764547348022461, 11.506729125976562)

两个结果不一致。mnn模型是由tflite模型经命令MNNConvert -f TFLITE --modelFile model.tflite --MNNModel model.mnn --bizCode biz转换而来。如果需要模型文件,请提供邮箱,模型文件可邮件发送。
如有什么需要核对的点,请告知,谢谢。

@jxt1234
Copy link
Collaborator

jxt1234 commented Apr 17, 2023

testMNNFromTflite.py 里面有对 MNN 和 Tflite 的结果进行对比测试的,你运行这个脚本的结果是什么?

@gdyshi
Copy link
Author

gdyshi commented Apr 17, 2023

testMNNFromTflite.py 里面有对 MNN 和 Tflite 的结果进行对比测试的,你运行这个脚本的结果是什么?

我运行 testMNNFromTflite.py 只有模型转换成功的信息,没有对比测试。结果如下:

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
tflite/test.tflite                                     
['StatefulPartitionedCall:0']                          
inputs:                                                
serving_default_input_1:0                              
outputs:                                               
tflite/StatefulPartitionedCall:0.txt                   
The device support dot:0, support fp16:0, support i8mm: 0
Start to Convert Other Model Format To MNN Model...      
Start to Optimize the MNN Net...                         
inputTensors : [ serving_default_input_1:0, ]            
outputTensors: [ StatefulPartitionedCall:0, ]            
Converted Success!                                       
Check convert result by tflite, thredhold is 0.01        

testMNNFromTflite.py 是源码中的代码 https://github.com/alibaba/MNN/blob/master/tools/script/testMNNFromTflite.py 。代码中我也没看到对mnn模型的测试。只有转换代码,见 代码第52行。

是否还有其他的测试工具?

@jxt1234
Copy link
Collaborator

jxt1234 commented Apr 17, 2023

第 52 行就是转换 + 测试了。参考文档 https://mnn-docs.readthedocs.io/en/latest/tools/convert.html

@jxt1234
Copy link
Collaborator

jxt1234 commented Apr 17, 2023

看上去测试文件夹已经产生,你可以在 MNNConvert 后面加上 --testdir=tflite 来同步执行 MNN 运行测试的

@gdyshi
Copy link
Author

gdyshi commented Apr 17, 2023

谢谢回复。
后面没有log了。我手动执行MNNConvert,显示测试失败。以下是终端输出:

> mnnconvert.exe -f TFLITE --bizCode MNN --modelFile tflite/test.tflite --MNNModel convert_cache.mnn --keepInputFormat --testdir tflite
The device support dot:0, support fp16:0, support i8mm: 0
The device support dot:0, support fp16:0, support i8mm: 0
Start to Convert Other Model Format To MNN Model...
Start to Optimize the MNN Net...
inputTensors : [ serving_default_input_1:0, ]
outputTensors: [ StatefulPartitionedCall:0, ]
Converted Success!
Check convert result by tflite, thredhold is 0.01
serving_default_input_1:0
output: StatefulPartitionedCall:0
StatefulPartitionedCall:0: (1, 6, )
TESTERROR StatefulPartitionedCall:0 value error : absMaxV:5.449783 - DiffMax 59.129807
Error for output StatefulPartitionedCall:0
Save mnn result to  .error director

@jxt1234
Copy link
Collaborator

jxt1234 commented Apr 18, 2023

tflite 模型发到我邮箱吧,[email protected]

@jxt1234 jxt1234 added bug Something isn't working Converter labels Apr 18, 2023
@gdyshi
Copy link
Author

gdyshi commented Apr 19, 2023

好的,已发邮件

@gdyshi
Copy link
Author

gdyshi commented May 26, 2023

请问有什么进展吗?是否正在排查

@v0jiuqi
Copy link
Collaborator

v0jiuqi commented Jun 1, 2023

请问有什么进展吗?是否正在排查

正在查

@jxt1234
Copy link
Collaborator

jxt1234 commented Jul 31, 2023

2.6.2 修正,增加了 fc 的relu / relu6激活函数处理

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Converter
Projects
None yet
Development

No branches or pull requests

3 participants