1/*************************************************
2USAGE:
3./model_diagnostics -m <model file location>
4**************************************************/
5#include <opencv2/dnn.hpp>
6#include <opencv2/core/utils/filesystem.hpp>
7#include <opencv2/dnn/utils/debug_utils.hpp>
8
9#include <iostream>
10
11
12using namespace cv;
13using namespace dnn;
14
15
16static
17int diagnosticsErrorCallback(int /*status*/, const char* /*func_name*/,
18 const char* /*err_msg*/, const char* /*file_name*/,
19 int /*line*/, void* /*userdata*/)
20{
21 fflush(stdout);
22 fflush(stderr);
23 return 0;
24}
25
26static std::string checkFileExists(const std::string& fileName)
27{
28 if (fileName.empty() || utils::fs::exists(path: fileName))
29 return fileName;
30
31 CV_Error(Error::StsObjectNotFound, "File " + fileName + " was not found! "
32 "Please, specify a full path to the file.");
33}
34
35std::string diagnosticKeys =
36 "{ model m | | Path to the model file. }"
37 "{ config c | | Path to the model configuration file. }"
38 "{ framework f | | [Optional] Name of the model framework. }";
39
40
41
42int main( int argc, const char** argv )
43{
44 CommandLineParser argParser(argc, argv, diagnosticKeys);
45 argParser.about(message: "Use this tool to run the diagnostics of provided ONNX/TF model"
46 "to obtain the information about its support (supported layers).");
47
48 if (argc == 1)
49 {
50 argParser.printMessage();
51 return 0;
52 }
53
54 std::string model = checkFileExists(fileName: argParser.get<std::string>(name: "model"));
55 std::string config = checkFileExists(fileName: argParser.get<std::string>(name: "config"));
56 std::string frameworkId = argParser.get<std::string>(name: "framework");
57
58 CV_Assert(!model.empty());
59
60 enableModelDiagnostics(isDiagnosticsMode: true);
61 skipModelImport(skip: true);
62 redirectError(errCallback: diagnosticsErrorCallback, NULL);
63
64 Net ocvNet = readNet(model, config, framework: frameworkId);
65
66 return 0;
67}
68

source code of opencv/apps/model-diagnostics/model_diagnostics.cpp