run classification model
| 96 | |
| 97 | // run classification model |
| 98 | int 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; |
no test coverage detected