19 explicit LoadModelFixture()
24 "operator_codes": [ { "builtin_code": "AVERAGE_POOL_2D" }, { "builtin_code": "CONV_2D" } ], 29 "shape": [ 1, 1, 1, 1 ] , 32 "name": "OutputTensor", 41 "shape": [ 1, 2, 2, 1 ] , 44 "name": "InputTensor", 59 "builtin_options_type": "Pool2DOptions", 67 "fused_activation_function": "NONE" 69 "custom_options_format": "FLEXBUFFERS" 75 "shape": [ 1, 3, 3, 1 ], 78 "name": "ConvInputTensor", 85 "shape": [ 1, 1, 1, 1 ], 88 "name": "ConvOutputTensor", 97 "shape": [ 1, 3, 3, 1 ], 100 "name": "filterTensor", 116 "builtin_options_type": "Conv2DOptions", 121 "fused_activation_function": "NONE" 123 "custom_options_format": "FLEXBUFFERS" 128 "description": "Test loading a model", 129 "buffers" : [ {}, {} ] 135 void CheckModel(
const ModelPtr& model, uint32_t version,
size_t opcodeSize,
136 const std::vector<tflite::BuiltinOperator>& opcodes,
137 size_t subgraphs,
const std::string desc,
size_t buffers)
140 CHECK_EQ(version, model->version);
141 CHECK_EQ(opcodeSize, model->operator_codes.size());
142 CheckBuiltinOperators(opcodes, model->operator_codes);
143 CHECK_EQ(subgraphs, model->subgraphs.size());
144 CHECK_EQ(desc, model->description);
145 CHECK_EQ(buffers, model->buffers.size());
148 void CheckBuiltinOperators(
const std::vector<tflite::BuiltinOperator>& expectedOperators,
149 const std::vector<std::unique_ptr<tflite::OperatorCodeT>>& result)
151 CHECK_EQ(expectedOperators.size(), result.size());
152 for (
size_t i = 0; i < expectedOperators.size(); i++)
154 CHECK_EQ(expectedOperators[i], result[i]->builtin_code);
158 void CheckSubgraph(
const SubgraphPtr& subgraph,
size_t tensors,
const std::vector<int32_t>& inputs,
159 const std::vector<int32_t>& outputs,
size_t operators,
const std::string& name)
162 CHECK_EQ(tensors, subgraph->tensors.size());
163 CHECK(std::equal(inputs.begin(), inputs.end(), subgraph->inputs.begin(), subgraph->inputs.end()));
164 CHECK(std::equal(outputs.begin(), outputs.end(),
165 subgraph->outputs.begin(), subgraph->outputs.end()));
166 CHECK_EQ(operators, subgraph->operators.size());
167 CHECK_EQ(name, subgraph->name);
170 void CheckOperator(
const OperatorPtr& operatorPtr, uint32_t opcode,
const std::vector<int32_t>& inputs,
171 const std::vector<int32_t>& outputs, tflite::BuiltinOptions optionType,
172 tflite::CustomOptionsFormat custom_options_format)
175 CHECK_EQ(opcode, operatorPtr->opcode_index);
176 CHECK(std::equal(inputs.begin(), inputs.end(),
177 operatorPtr->inputs.begin(), operatorPtr->inputs.end()));
178 CHECK(std::equal(outputs.begin(), outputs.end(),
179 operatorPtr->outputs.begin(), operatorPtr->outputs.end()));
180 CHECK_EQ(optionType, operatorPtr->builtin_options.type);
181 CHECK_EQ(custom_options_format, operatorPtr->custom_options_format);
188 m_GraphBinary.size());
189 CheckModel(model, 3, 2, { tflite::BuiltinOperator_AVERAGE_POOL_2D, tflite::BuiltinOperator_CONV_2D },
190 2,
"Test loading a model", 2);
191 CheckSubgraph(model->subgraphs[0], 2, { 1 }, { 0 }, 1,
"");
192 CheckSubgraph(model->subgraphs[1], 3, { 0 }, { 1 }, 1,
"");
193 CheckOperator(model->subgraphs[0]->operators[0], 0, { 1 }, { 0 }, tflite::BuiltinOptions_Pool2DOptions,
194 tflite::CustomOptionsFormat_FLEXBUFFERS);
195 CheckOperator(model->subgraphs[1]->operators[0], 1, { 0, 2 }, { 1 }, tflite::BuiltinOptions_Conv2DOptions,
196 tflite::CustomOptionsFormat_FLEXBUFFERS);
203 bool saved = flatbuffers::SaveFile(fname.c_str(),
204 reinterpret_cast<char *
>(m_GraphBinary.data()),
205 m_GraphBinary.size(),
true);
206 CHECK_MESSAGE(saved,
"Cannot save test file");
209 CheckModel(model, 3, 2, { tflite::BuiltinOperator_AVERAGE_POOL_2D, tflite::BuiltinOperator_CONV_2D },
210 2,
"Test loading a model", 2);
211 CheckSubgraph(model->subgraphs[0], 2, { 1 }, { 0 }, 1,
"");
212 CheckSubgraph(model->subgraphs[1], 3, { 0 }, { 1 }, 1,
"");
213 CheckOperator(model->subgraphs[0]->operators[0], 0, { 1 }, { 0 }, tflite::BuiltinOptions_Pool2DOptions,
214 tflite::CustomOptionsFormat_FLEXBUFFERS);
215 CheckOperator(model->subgraphs[1]->operators[0], 1, { 0, 2 }, { 1 }, tflite::BuiltinOptions_Conv2DOptions,
216 tflite::CustomOptionsFormat_FLEXBUFFERS);
220 TEST_CASE(
"LoadNullBinary")
225 TEST_CASE(
"LoadInvalidBinary")
227 std::string testData =
"invalid data";
228 CHECK_THROWS_AS(TfLiteParserImpl::LoadModelFromBinary(reinterpret_cast<const uint8_t*>(&testData),
232 TEST_CASE(
"LoadFileNotFound")
237 TEST_CASE(
"LoadNullPtrFile")
TfLiteParserImpl::OperatorPtr OperatorPtr
bool ReadStringToBinary()
TEST_CASE_FIXTURE(ClContextControlFixture, "CopyBetweenNeonAndGpu")
TfLiteParserImpl::SubgraphPtr SubgraphPtr
TEST_SUITE("TensorflowLiteParser_LoadModel")
fs::path NamedTempFile(const char *fileName)
Returns a path to a file in the system temporary folder. If the file existed it will be deleted...
TfLiteParserImpl::ModelPtr ModelPtr