DNN: load fp16 ONNX model as fp32#22337
Conversation
|
the PR is important and is ready to be merged |
rogday
left a comment
There was a problem hiding this comment.
I think we should log this behavior so that users are not surprised that OpenCV DNN takes twice as much RAM as other frameworks, otherwise looks good.
838b4a4 to
3d0bea5
Compare
|
Hi @alalek and @asmorkalov, can you please check if the update can fix the data alignment issue? |
|
@zihaomu I checked other tickets and OpenCV patches that are alignment related and found general solution for it that is managed by macro CV_STRONG_ALIGNMENT. See .I propose to use it and rework the patch in the same way as it's done for int64 tensors in the same function (Macro check + AutoBuffer). |
|
Also it looks like fp64 branch suffers from the same problem, but it's not popular and the issue is invisible. |
|
Great thanks! @asmorkalov I will follow your suggestion and re-implement it with |
fc98f49 to
4901de2
Compare
|
|
Thanks for the reminder, @asmorkalov. there seems to be a depth bug on float16 and I'm trying to fix it. |
|
Hi @asenyaev, other CI works fine, only Win10 can not download the new added testing model from Github. Can you please check what's happening? Win10 CI error |
52f5d5b to
0b155fa
Compare
|
@zihaomu sure, I'll check it. |
|
@zihaomu, the model was downloaded manually on Windows machine and the pipeline for Windows passed. |
8325b2b to
6204381
Compare
|
Hi, In my test, this path works fine with model format version >= 6. So, I think we can merge this first, and then I will discuss the specific solution with the ONNX team. Some example: |
| { | ||
| const ::google::protobuf::RepeatedField<float> field = tensor_proto.float_data(); | ||
| Mat(sizes, CV_32FC1, (void*)field.data()).copyTo(blob); | ||
| const int offset = isLittleEndianCPU() ? 0 : 1; |
There was a problem hiding this comment.
IIRC, we have this check in CMake scripts. Can we use a precomp macro here?
There was a problem hiding this comment.
Please reuse WORDS_BIGENDIAN macro (from cvconfig.h)
| { | ||
| return findDataFile(std::string("dnn/onnx/") + filename, required); | ||
| } | ||
| void printShape(InputArray blob_) |
There was a problem hiding this comment.
Thx! Fixed, wrong commit.
cb62e4f to
a970e58
Compare
|
Hi, with the help of ONNX teams, we can load all the ONNX float16 models now! |
a970e58 to
7eaec9d
Compare
9da9699 to
9638e34
Compare
Related extra data
Download link
The FP16 model is half the size of the FP32 model, which will be edge device friendly.
Currently, OpenCV
ONNX_importercannot load FP16 models correctly and DNN also doesn't have full FP16 backend support.So, my idea is that in the first step we make OpenCV DNN load FP16 ONNX model as FP32 model, and every layer will be computed as FP32.
Here is a FP16 text recognition model(CRNN) which was converted by my GSoC student Yiyao @Charles-258, and we found it even has better accuracy performance than the original FP32 model.
And I have tested these FP16 CRNN models in the existing High-Level API,
testTextRecognitionModel, and it works well. Maybe we can use this to replace the original FP32 model.How to convert ONNX fP32 model to FP16 model?
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.