Problems loading TensorFlow model in C++

Hi,
I’m quite new to OpenCV, so excuse my noob questions.

I have to make my own object detection model, so far so good, I followed this colab tutorial(or rather notebook), and got a saved_model.pb.

From what I read online, this doesn’t work with OpenCV, so I tried converting it to frozen_graph.pb and frozen_graph.pbtxt.

The problem is, I can’t figure out how to do it properly.
So far I have tried 3 times, and all I get is “Unhandled exception at 0x00007FF84ABCCF19 in testyolo.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000084A32FDE60.”
(ignore the “testyolo” part, it’s a testing program, I decided to go with tensorflow)

How do you convert saved_model.pb to frozen_inference_graph.pb and pbtxt to use the model in OpenCV?
The program works fine with “ssd_mobilenet_v2_coco_2018_03_29”, but I need a custom model for my application.
Or how does one retrain a model on a new dataset?

Again, sorry if it’s a stupid or easy question, I just can’t find a straight answer.

Do you run your model in C++ or python?

before loading your model try to insert in C++


#include <opencv2/core/utils/logger.hpp>


utils::logging::setLogLevel(utils::logging::LogLevel::LOG_LEVEL_VERBOSE);

then post full output of your program

C++

  • [DEBUG:0@0.072] global tf_importer.cpp:2723 cv::dnn::dnn4_v20221220::`anonymous-namespace’::TFImporter::TFImporter DNN/TF: processing TensorFlow model from file: H:/proj/frozen_graph.pb
  • [DEBUG:0@0.163] global tf_importer.cpp:2728 cv::dnn::dnn4_v20221220::`anonymous-namespace’::TFImporter::TFImporter DNN/TF: processing TensorFlow config from file: H:/proj/frozen_graph.pbtxt
  • [ INFO:0@0.553] global tf_importer.cpp:3014 cv::dnn::dnn4_v20221220::`anonymous-namespace’::TFImporter::populateNet DNN/TF: parsing model produced by TF v987 (min_consumer=0). Number of nodes = 8792
  • [ INFO:0@0.553] global tf_importer.cpp:3021 cv::dnn::dnn4_v20221220::`anonymous-namespace’::TFImporter::populateNet DNN/TF: parsing config produced by TF v987 (min_consumer=0). Number of nodes = 8792
  • [DEBUG:0@0.568] global tf_importer.cpp:3024 cv::dnn::dnn4_v20221220::`anonymous-namespace’::TFImporter::populateNet DNN/TF: RemoveIdentityOps(model) => 7494 nodes
  • [DEBUG:0@0.580] global tf_importer.cpp:3026 cv::dnn::dnn4_v20221220::`anonymous-namespace’::TFImporter::populateNet DNN/TF: RemoveIdentityOps(config) => 7494 nodes

catch it, inspect it.

OpenCV(4.7.0) F:\ChromeDownloads3\opencv-4.7.0\opencv-4.7.0\modules\dnn\src\tensorflow\tf_graph_simplifier.cpp:1059: error: (-215:Assertion failed) permIds.size() == net.node_size() in function ‘cv::dnn::dnn4_v20221220::sortByExecutionOrder’

[ INFO:1@0.614] global plugin_loader.impl.hpp:74 cv::plugin::impl::DynamicLib::libraryRelease unload G:\opencv2\install\x64\vc17\bin\opencv_videoio_ffmpeg470_64.dll

Anyone?
Will 4.8.0 help?

if you think updating the library would help, then do it. you don’t need to ask.

Well, I have tried building it maybe 10 times, on 2 different computers, and on both I’m missing the install folder.
Any ideas?
I’m following Nielsen’s tutorial.
I’d ask him on his discord, however it’s private.

So, CUDA 12.2 is incompatible and caused the issue with the folder

I saw in the changenotes that there’s now tflite support, so I trained a tflite model, and I get:

G:\opencv\opencv-4.8.0\modules\dnn\src\tensorflow\tf_importer.cpp:2999: error: (-215:Assertion failed) netBinSize || netTxtSize in function ‘cv::dnn::dnn4_v20230620::`anonymous-namespace’::TFImporter::populateNet’

you should look up that line in the opencv source and figure out the context of the assertion.

I would guess that one or both of the required input files were empty or not there.

my bad, I hadn’t changed the function to readNetFromTFLite, as I thought it’s still Tensorflow

Error is actually

[ERROR:0@0.238] global tflite_importer.cpp:243 cv::dnn::dnn4_v20230620::TFLiteImporter::populateNet DNN/TFLite: Problem during import of operator [PACK]:(ssd_mobile_net_v2_fpn_keras_feature_extractor/FeatureMaps/top_down/nearest_neighbor_upsampling/nearest_neighbor_upsampling/w_stack) (65/157). Exception: OpenCV(4.8.0) G:\opencv\opencv-4.8.0\modules\dnn\src\tflite\tflite_importer.cpp:235: error: (-213:The function/feature is not implemented) Unsupported operator type PACK in function ‘cv::dnn::dnn4_v20230620::TFLiteImporter::populateNet’

OpenCV(4.8.0) G:\opencv\opencv-4.8.0\modules\dnn\src\tflite\tflite_importer.cpp:235: error: (-213:The function/feature is not implemented) Unsupported operator type PACK in function ‘cv::dnn::dnn4_v20230620::TFLiteImporter::populateNet’

Line 235 is

Tensorflow model error(not tflite)

[ERROR:0@2.428] global tf_importer.cpp:3172 cv::dnn::dnn4_v20230620::anonymous-namespace'::TFImporter::parseNode DNN/TF: Can't parse layer for node='StatefulPartitionedCall/StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/add_179' of type='Add'. Exception: OpenCV(4.8.0) G:\opencv\opencv-4.8.0\modules\dnn\src\tensorflow\tf_importer.cpp:2837: error: (-2:Unspecified error) More than one input is Const op in function 'cv::dnn::dnn4_v20230620::anonymous-namespace’::TFImporter::getConstBlob’

OpenCV(4.8.0) G:\opencv\opencv-4.8.0\modules\dnn\src\tensorflow\tf_importer.cpp:2837: error: (-2:Unspecified error) More than one input is Const op in function ‘cv::dnn::dnn4_v20230620::`anonymous-namespace’::TFImporter::getConstBlob’

4 posts were split to a new topic: AI-generated spam and ensuing confusion