1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 // NOTE: this is a Android version of the file with the same name in parent folder. 16 // The main difference is the removal of absl, re2 and tensorflow core dependencies. 17 18 #ifndef TENSORFLOW_LITE_TESTING_ANDROID_PARSE_TESTDATA_H_ 19 #define TENSORFLOW_LITE_TESTING_ANDROID_PARSE_TESTDATA_H_ 20 21 #include <vector> 22 #include "tensorflow/lite/interpreter.h" 23 #include "test_runner.h" 24 25 namespace tflite { 26 namespace testing { 27 28 // Shape and data for a float tensor 29 struct FloatTensor { 30 std::vector<int> shape; 31 std::vector<float> flat_data; 32 }; 33 34 // A prescribed input, output example 35 struct Example { 36 std::vector<FloatTensor> inputs; 37 std::vector<FloatTensor> outputs; 38 }; 39 40 // Parses an example input and output file (used for unit tests) 41 TfLiteStatus ParseExamples(const char* filename, 42 std::vector<Example>* examples); 43 44 // Inputs Tensors into a TensorFlow lite interpreter. Note, this will run 45 // interpreter.AllocateTensors(); 46 TfLiteStatus FeedExample(tflite::Interpreter* interpreter, const Example&); 47 48 // Check outputs against (already) evaluated result. 49 TfLiteStatus CheckOutputs(tflite::Interpreter* interpreter, const Example&); 50 51 // Parses a test description and feeds the given test runner with data. 52 // The input format is similar to an ASCII proto: 53 // // Loads model 'add.bin' from the TestRunner's model directory. 54 // load_model: "add.bin" 55 // // Changes the shape of inputs, provided in the same order they appear 56 // // in the model. 57 // reshape { 58 // input: "1,224,224,3" 59 // input: "1,3,4,1" 60 // } 61 // // Fills the given persistent tensors with zeros. 62 // init_state: 0,1,2,3 63 // // Invokes the interpreter with the given input and checks that it 64 // // produces the expected output. Inputs and outputs should be specified in 65 // // the order they appear in the model. 66 // invoke { 67 // input: "1,2,3,4,56" 68 // input: "0.1,0.2,0.3,4.3,56.4" 69 // output: "12,3,4,545,3" 70 // output: "0.01,0.02" 71 // } 72 bool ParseAndRunTests(std::istream* input, TestRunner* test_runner, 73 int max_invocations = -1); 74 75 } // namespace testing 76 } // namespace tflite 77 78 #endif // TENSORFLOW_LITE_TESTING_ANDROID_PARSE_TESTDATA_H_ 79