MCPcopy Create free account
hub / github.com/OpenPPL/ppl.nn / RunClassificationModel

Function RunClassificationModel

samples/cpp/run_model/classification.cpp:98–221  ·  view source on GitHub ↗

run classification model

Source from the content-addressed store, hash-verified

96
97// run classification model
98int RunClassificationModel(const Mat& src_img, const char* onnx_model_path) {
99 /************************ 1. preprocess image *************************/
100 const int32_t height = src_img.rows;
101 const int32_t width = src_img.cols;
102 const int32_t channels = src_img.channels();
103
104 vector<float> in_data_(height * width * channels); // network need NCHW(RGB order) fp32 data
105 float* in_data = in_data_.data();
106
107 int32_t ret = ImagePreprocess(src_img, in_data); // convert NHWC to NCHW & substract mean div std
108 if (ret != 0) {
109 fprintf(stderr, "image preprocess failed!\n");
110 return ret;
111 }
112
113 printf("image preprocess succeed!\n");
114
115 /************************ 2. create runtime builder from onnx model *************************/
116 auto x86_engine = x86::EngineFactory::Create(x86::EngineOptions()); // create x86 engine with default options
117 unique_ptr<Engine> x86_engine_guard(x86_engine);
118
119 // create onnx runtime builder according to onnx model & engines registered before
120 auto builder = unique_ptr<onnx::RuntimeBuilder>(onnx::RuntimeBuilderFactory::Create());
121 if (!builder) {
122 fprintf(stderr, "create OnnxRuntimeBuilder from onnx model %s failed!\n", onnx_model_path);
123 return -1;
124 }
125
126 // load onnx model
127 auto status = builder->LoadModel(onnx_model_path);
128 if (status != RC_SUCCESS) {
129 fprintf(stderr, "init OnnxRuntimeBuilder failed: %s\n", GetRetCodeStr(status));
130 return -1;
131 }
132
133 // register engines you want to use
134 onnx::RuntimeBuilder::Resources resources;
135 resources.engines = &x86_engine;
136 resources.engine_num = 1;
137
138 status = builder->SetResources(resources);
139 if (status != RC_SUCCESS) {
140 cerr << "onnx RuntimeBuilder SetResources failed: " << GetRetCodeStr(status) << endl;
141 return -1;
142 }
143
144 status = builder->Preprocess();
145 if (status != RC_SUCCESS) {
146 fprintf(stderr, "builder preprocess failed: %s\n", GetRetCodeStr(status));
147 return -1;
148 }
149
150 printf("successfully create runtime builder!\n");
151
152 /************************ 3. build runtime *************************/
153 // use runtime builder to build runtime, one builder can be used to build multiple runtimes sharing constant data &
154 // topo here we only build one runtime for easy to understand
155 unique_ptr<Runtime> runtime;

Callers 1

mainFunction · 0.85

Calls 14

ImagePreprocessFunction · 0.85
GetClassificationResultFunction · 0.85
SetResourcesMethod · 0.80
GetInputTensorMethod · 0.80
GetOutputTensorMethod · 0.80
LoadModelMethod · 0.45
PreprocessMethod · 0.45
CreateRuntimeMethod · 0.45
ReshapeMethod · 0.45
GetShapeMethod · 0.45
ConvertFromHostMethod · 0.45
GetNameMethod · 0.45

Tested by

no test coverage detected