| 199 | } // namespace |
| 200 | |
| 201 | std::unique_ptr<Model> Import(const ModelFlags& model_flags, |
| 202 | const string& input_file_contents) { |
| 203 | ::tflite::AlwaysTrueResolver r; |
| 204 | if (!::tflite::Verify(input_file_contents.data(), input_file_contents.size(), |
| 205 | r, ::tflite::DefaultErrorReporter())) { |
| 206 | LOG(FATAL) << "Invalid flatbuffer."; |
| 207 | } |
| 208 | const ::tflite::Model* input_model = |
| 209 | ::tflite::GetModel(input_file_contents.data()); |
| 210 | |
| 211 | // Full list of all known operators. |
| 212 | const auto ops_by_name = BuildOperatorByNameMap(); |
| 213 | |
| 214 | if (!input_model->subgraphs() || input_model->subgraphs()->size() != 1) { |
| 215 | LOG(FATAL) << "Number of subgraphs in tflite should be exactly 1."; |
| 216 | } |
| 217 | std::unique_ptr<Model> model; |
| 218 | model.reset(new Model); |
| 219 | |
| 220 | details::TensorsTable tensors_table; |
| 221 | details::LoadTensorsTable(*input_model, &tensors_table); |
| 222 | |
| 223 | details::OperatorsTable operators_table; |
| 224 | details::LoadOperatorsTable(*input_model, &operators_table); |
| 225 | |
| 226 | ImportTensors(*input_model, model.get()); |
| 227 | ImportOperators(*input_model, ops_by_name, tensors_table, operators_table, |
| 228 | model.get()); |
| 229 | |
| 230 | ImportIOTensors(model_flags, *input_model, tensors_table, model.get()); |
| 231 | |
| 232 | UndoWeightsShuffling(model.get()); |
| 233 | |
| 234 | return model; |
| 235 | } |
| 236 | |
| 237 | } // namespace tflite |
| 238 | |