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

[MobileFaceNet]ONNX to tflite模型转换异常 #18

Closed
joyoki opened this issue Oct 31, 2022 · 8 comments
Closed

[MobileFaceNet]ONNX to tflite模型转换异常 #18

joyoki opened this issue Oct 31, 2022 · 8 comments

Comments

@joyoki
Copy link

joyoki commented Oct 31, 2022

Hi 博主,
感谢你提供的工具,我在转换mobilefacenet 模型的时候,遇到以下几个问题,不知能否帮忙解答:

【问题描述】
1.onnx2tflite之后,模型输出由128变成1000

//转换前:
name: fc1
type: float32[1,128]
//转换后:
name: StatefulPartitionedCall:0
type: float32[1,1000]
location: 177

2.将生成的模型导入APK验证,脸部检测功能能正常工作,但是但是人脸识别功能失效
此处暂未提供LOG,优先确认问题1

2.生成的tflite模型尺寸几乎和onnx大小相当,未见尺寸优化

(base) rsi@DESKTOP-1NGTB52:~/onnx2tflite/onnx2tflite/input$ ll
total 62388
drwxr-xr-x 2 rsi rsi     4096 Oct 31 11:39 ./
drwxr-xr-x 8 rsi rsi     4096 Oct 31 11:34 ../
-rw-r--r-- 1 rsi rsi 13965850 Oct 31 11:00 face_recognition_sface_2021dec-act_int8-wt_int8-quantized.onnx
-rw-r--r-- 1 rsi rsi 13979876 Oct 31 11:00 face_recognition_sface_2021dec-act_int8-wt_int8-quantized.tflite
-rw-r--r-- 1 rsi rsi 13965850 Oct 31 11:33 face_recognition_sface_2021dec.onnx
-rw-r--r-- 1 rsi rsi 13979876 Oct 31 11:34 face_recognition_sface_2021dec.tflite

2210311357更新:tflite模型偏大的问题不知道是不是该文档提到大的,onnx转tflite需要去冗余操作:https://zhuanlan.zhihu.com/p/363317178

【所使用转换脚本】
使用博主的参考py,改成如下onnx2tflite.py

import torch
import torchvision
_input = torch.randn(1, 3, 112, 112)
model = torchvision.models.mobilenet_v2(True)
# use default settings is ok
torch.onnx.export(model, _input, './input/face_recognition_sface_2021dec.onnx', opset_version=11)# or opset_version=13

from converter import onnx_converter
onnx_converter(
    onnx_model_path = "./input/face_recognition_sface_2021dec.onnx",
    need_simplify = True,
    output_path = "./",
    target_formats = ['tflite'], # or ['keras'], ['keras', 'tflite']
    weight_quant = False,
    int8_model = False,
    int8_mean = None,
    int8_std = None,
    image_root = None
)

[相关模型]:
1.我所转换的模型为opencv_zoo/models/face_recognition_sface/
1.face_recognition_sface_2021dec-act_int8-wt_int8-quantized.onnx
2.face_recognition_sface_2021dec.onnx
原始模型链接为:https://github.com/opencv/opencv_zoo/tree/master/models/face_recognition_sface

谢谢解答

@MPolaris
Copy link
Owner

输出128变1000是因为你导出的是mobilenet,

# 你导出的onnx就是mobilenet
model = torchvision.models.mobilenet_v2(True)

@joyoki
Copy link
Author

joyoki commented Oct 31, 2022

输出128变1000是因为你导出的是mobilenet,

# 你导出的onnx就是mobilenet
model = torchvision.models.mobilenet_v2(True)

感谢博主的指正,请问这块应该改成什么样呢?有没有对应的model输出要求的必改格式对照表,或者置为缺省,是否可以根据导入的模型,自动转换?

另外是否还有其他错误,也辛苦博主帮忙指正?

@MPolaris
Copy link
Owner

MPolaris commented Nov 1, 2022

输出128变1000是因为你导出的是mobilenet,

# 你导出的onnx就是mobilenet
model = torchvision.models.mobilenet_v2(True)

感谢博主的指正,请问这块应该改成什么样呢?有没有对应的model输出要求的必改格式对照表,或者置为缺省,是否可以根据导入的模型,自动转换?

另外是否还有其他错误,也辛苦博主帮忙指正?
先重新pull一下代码,然后

from converter import onnx_converter
onnx_converter(
    onnx_model_path = "./input/face_recognition_sface_2021dec.onnx",#这里填你的模型地址
    need_simplify = True,
    output_path = "./",
    target_formats = ['tflite'], # or ['keras'], ['keras', 'tflite']
    weight_quant = False,
    int8_model = False,
    int8_mean = None,
    int8_std = None,
    image_root = None
)

@MPolaris MPolaris closed this as completed Nov 1, 2022
@joyoki
Copy link
Author

joyoki commented Nov 1, 2022

感谢解答,我将代码同步到最新,尝试博主提供的方法,还是遇到相同的output=1000的问题,我在另外一个github上看到一个onnx2tf的工具也是引用了博主的套件,感觉博主的工具很有潜力,不知博主是否方便继续帮忙确认下?问题具体描述如下:
1.我当前的代码节点已同步到最新:

//当前代码节点:
(base) rsi@DESKTOP-1NGTB52:~/onnx2tflite/onnx2tflite$ git log
commit 24b6647c97ca0a74fb8965e0929e4e0bf6775bb4 (HEAD, origin/main, origin/HEAD)
Author: MPolaris <540492239@qq.com>
Date:   Mon Oct 31 13:54:51 2022 +0800

    update parse arguments

commit 8e7451def93c3f1886e7cfd3b54678856d93f94b
Author: MPolaris <540492239@qq.com>
Date:   Mon Oct 31 13:53:49 2022 +0800
...

转换所用脚本onnx2tflite.py

//onnx2tflite转换脚本代码:
# git clone https://github.com/MPolaris/onnx2tflite
# cd onnx2tflite
# pip install -r requirements.txt
 
#import torch
#import torchvision
#_input = torch.randn(1, 3, 112, 112)
#model = torchvision.models.mobilenet_v2(True)
## use default settings is ok
#torch.onnx.export(model, _input, './input/face_recognition_sface_2021dec.onnx', opset_version=11)# or opset_version=13
 
from converter import onnx_converter
onnx_converter(
    onnx_model_path = "./input/face_recognition_sface_2021dec.onnx",#这里填你的模型地址
    need_simplify = True,
    output_path = "./input",
    target_formats = ['tflite'], # or ['keras'], ['keras', 'tflite']
    weight_quant = False,
    int8_model = False,
    int8_mean = None,
    int8_std = None,
    image_root = None
)

//转换结果异常部分:

StatefulPartitionedCall:0177
name: StatefulPartitionedCall:0
type: float32[1,1000]  //此处应该为[1,128]
location: 177

另外我在另外一个github上看到一个组合工具:https://github.com/PINTO0309/onnx2tf 看到也是引用博主的工具,但是使用该工具,可以将onnx转换为tflite(但是从测试结果看,转换后精度不高,这个问题稍后再表)

//转换命令:
onnx2tf -i face_recognition_sface_2021dec.onnx -o saved_model
//转换后的outputs:
Identity295
name: Identity
type: float32[1,128]
location: 295

谢谢博主

@MPolaris
Copy link
Owner

MPolaris commented Nov 2, 2022

感谢解答,我将代码同步到最新,尝试博主提供的方法,还是遇到相同的output=1000的问题,我在另外一个github上看到一个onnx2tf的工具也是引用了博主的套件,感觉博主的工具很有潜力,不知博主是否方便继续帮忙确认下?问题具体描述如下: 1.我当前的代码节点已同步到最新:

//当前代码节点:
(base) rsi@DESKTOP-1NGTB52:~/onnx2tflite/onnx2tflite$ git log
commit 24b6647c97ca0a74fb8965e0929e4e0bf6775bb4 (HEAD, origin/main, origin/HEAD)
Author: MPolaris <540492239@qq.com>
Date:   Mon Oct 31 13:54:51 2022 +0800

    update parse arguments

commit 8e7451def93c3f1886e7cfd3b54678856d93f94b
Author: MPolaris <540492239@qq.com>
Date:   Mon Oct 31 13:53:49 2022 +0800
...

转换所用脚本onnx2tflite.py

//onnx2tflite转换脚本代码:
# git clone https://github.com/MPolaris/onnx2tflite
# cd onnx2tflite
# pip install -r requirements.txt
 
#import torch
#import torchvision
#_input = torch.randn(1, 3, 112, 112)
#model = torchvision.models.mobilenet_v2(True)
## use default settings is ok
#torch.onnx.export(model, _input, './input/face_recognition_sface_2021dec.onnx', opset_version=11)# or opset_version=13
 
from converter import onnx_converter
onnx_converter(
    onnx_model_path = "./input/face_recognition_sface_2021dec.onnx",#这里填你的模型地址
    need_simplify = True,
    output_path = "./input",
    target_formats = ['tflite'], # or ['keras'], ['keras', 'tflite']
    weight_quant = False,
    int8_model = False,
    int8_mean = None,
    int8_std = None,
    image_root = None
)

//转换结果异常部分:

StatefulPartitionedCall:0177
name: StatefulPartitionedCall:0
type: float32[1,1000]  //此处应该为[1,128]
location: 177

另外我在另外一个github上看到一个组合工具:https://github.com/PINTO0309/onnx2tf 看到也是引用博主的工具,但是使用该工具,可以将onnx转换为tflite(但是从测试结果看,转换后精度不高,这个问题稍后再表)

//转换命令:
onnx2tf -i face_recognition_sface_2021dec.onnx -o saved_model
//转换后的outputs:
Identity295
name: Identity
type: float32[1,128]
location: 295

谢谢博主

朋友,你还是转换的mobilenet,只是你把模型的名字写成了face_recognition_sface_2021dec.onnx。
你要不要试试把./input/face_recognition_sface_2021dec.onnx删掉,然后重新下载一个?

@joyoki
Copy link
Author

joyoki commented Nov 2, 2022

@MPolaris
博主,实在抱歉,耽误你时间了。
对于这个参考你wiki example写的转换脚本onnx2tflite.py还是不太理解。能否麻烦博主用这个模型face_recognition_sface_2021dec.onnx提供个完整的参考?十分感谢

//我已经将你提供的参考里面的mobilenet 注释掉
朋友,你还是转换的mobilenet,只是你把模型的名字写成了face_recognition_sface_2021dec.onnx。
你要不要试试把./input/face_recognition_sface_2021dec.onnx删掉,然后重新下载一个?

转换所用脚本onnx2tflite.py:

//onnx2tflite转换脚本代码:
# git clone https://github.com/MPolaris/onnx2tflite
# cd onnx2tflite
# pip install -r requirements.txt
 
#import torch
#import torchvision
#_input = torch.randn(1, 3, 112, 112)
#model = torchvision.models.mobilenet_v2(True)
## use default settings is ok
#torch.onnx.export(model, _input, './input/face_recognition_sface_2021dec.onnx', opset_version=11)# or opset_version=13
 
from converter import onnx_converter
onnx_converter(
    onnx_model_path = "./input/face_recognition_sface_2021dec.onnx",#这里填你的模型地址
    need_simplify = True,
    output_path = "./input",
    target_formats = ['tflite'], # or ['keras'], ['keras', 'tflite']
    weight_quant = False,
    int8_model = False,
    int8_mean = None,
    int8_std = None,
    image_root = None
)

谢谢

@PINTO0309
Copy link

PINTO0309 commented Nov 3, 2022

@joyoki
It appears to be a closed issue, so you can ignore it. I do not intend to disturb you all.
The onnx2tf already supports transforms for almost all OPs except RNNs, and also supports INT8 quantization, INT8 with INT16 activation and Dynamic Range Quantization. Since onnx2tflite and onnx2tf are very similar in operation, it is acceptable to use onnx2tflite. My tool may be a little difficult to use because I have added a large number of different optimization options.
https://github.com/PINTO0309/onnx2tf

The reason why @joyoki said the accuracy is poor after conversion is clear. You may have been missing a parameter that should have been specified to onnx2tf. I have included the URL of the tflite model I converted so you can compare it to the model you converted.

Also, Please see this issue for a detailed explanation of what causes accuracy degradation in my tool and how to resolve it.
PINTO0309/onnx2tf#8
PINTO0309/onnx2tf#10

Thank you very much for your time.

Update Nov. 4, 2022: In some situations, the tool has been modified so that the Flatten behavior is corrected without specifying additional parameters.
https://github.com/PINTO0309/onnx2tf/releases/tag/1.1.6

@MPolaris MPolaris reopened this Nov 4, 2022
@MPolaris
Copy link
Owner

MPolaris commented Nov 4, 2022

@joyoki 朋友,你重新去face_recognition_sface_2021dec.onnx下一个,然后再用工具转呀。
你之前导出的mobilenet你就是命名为了face_recognition_sface_2021dec.onnx。。。
这个是导出来的tflite图
face_recognition_sface_2021dec

@MPolaris MPolaris closed this as completed Nov 7, 2022
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