| 137 | } |
| 138 | |
| 139 | void RunInference(Settings* s) { |
| 140 | if (!s->model_name.c_str()) { |
| 141 | LOG(ERROR) << "no model file name\n"; |
| 142 | exit(-1); |
| 143 | } |
| 144 | |
| 145 | std::unique_ptr<tflite::FlatBufferModel> model; |
| 146 | std::unique_ptr<tflite::Interpreter> interpreter; |
| 147 | model = tflite::FlatBufferModel::BuildFromFile(s->model_name.c_str()); |
| 148 | if (!model) { |
| 149 | LOG(FATAL) << "\nFailed to mmap model " << s->model_name << "\n"; |
| 150 | exit(-1); |
| 151 | } |
| 152 | s->model = model.get(); |
| 153 | LOG(INFO) << "Loaded model " << s->model_name << "\n"; |
| 154 | model->error_reporter(); |
| 155 | LOG(INFO) << "resolved reporter\n"; |
| 156 | |
| 157 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 158 | |
| 159 | tflite::InterpreterBuilder(*model, resolver)(&interpreter); |
| 160 | if (!interpreter) { |
| 161 | LOG(FATAL) << "Failed to construct interpreter\n"; |
| 162 | exit(-1); |
| 163 | } |
| 164 | |
| 165 | interpreter->UseNNAPI(s->old_accel); |
| 166 | interpreter->SetAllowFp16PrecisionForFp32(s->allow_fp16); |
| 167 | |
| 168 | if (s->verbose) { |
| 169 | LOG(INFO) << "tensors size: " << interpreter->tensors_size() << "\n"; |
| 170 | LOG(INFO) << "nodes size: " << interpreter->nodes_size() << "\n"; |
| 171 | LOG(INFO) << "inputs: " << interpreter->inputs().size() << "\n"; |
| 172 | LOG(INFO) << "input(0) name: " << interpreter->GetInputName(0) << "\n"; |
| 173 | |
| 174 | int t_size = interpreter->tensors_size(); |
| 175 | for (int i = 0; i < t_size; i++) { |
| 176 | if (interpreter->tensor(i)->name) |
| 177 | LOG(INFO) << i << ": " << interpreter->tensor(i)->name << ", " |
| 178 | << interpreter->tensor(i)->bytes << ", " |
| 179 | << interpreter->tensor(i)->type << ", " |
| 180 | << interpreter->tensor(i)->params.scale << ", " |
| 181 | << interpreter->tensor(i)->params.zero_point << "\n"; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (s->number_of_threads != -1) { |
| 186 | interpreter->SetNumThreads(s->number_of_threads); |
| 187 | } |
| 188 | |
| 189 | int image_width = 224; |
| 190 | int image_height = 224; |
| 191 | int image_channels = 3; |
| 192 | std::vector<uint8_t> in = read_bmp(s->input_bmp_name, &image_width, |
| 193 | &image_height, &image_channels, s); |
| 194 | |
| 195 | int input = interpreter->inputs()[0]; |
| 196 | if (s->verbose) LOG(INFO) << "input: " << input << "\n"; |