Home | History | Annotate | Download | only in testing
      1 /* Copyright 2017 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 #ifndef TENSORFLOW_CONTRIB_LITE_TESTING_TFLITE_DRIVER_H_
     16 #define TENSORFLOW_CONTRIB_LITE_TESTING_TFLITE_DRIVER_H_
     17 
     18 #include <map>
     19 
     20 #include "tensorflow/contrib/lite/interpreter.h"
     21 #include "tensorflow/contrib/lite/kernels/register.h"
     22 #include "tensorflow/contrib/lite/model.h"
     23 #include "tensorflow/contrib/lite/testing/test_runner.h"
     24 
     25 namespace tflite {
     26 namespace testing {
     27 
     28 // A test runner that feeds inputs into TF Lite and verifies its outputs.
     29 class TfLiteDriver : public TestRunner {
     30  public:
     31   explicit TfLiteDriver(bool use_nnapi);
     32   ~TfLiteDriver() override;
     33 
     34   void LoadModel(const string& bin_file_path) override;
     35   const std::vector<int>& GetInputs() override {
     36     return interpreter_->inputs();
     37   }
     38   const std::vector<int>& GetOutputs() override {
     39     return interpreter_->outputs();
     40   }
     41   void ReshapeTensor(int id, const string& csv_values) override;
     42   void AllocateTensors() override;
     43   void ResetTensor(int id) override;
     44   void SetInput(int id, const string& csv_values) override;
     45   void SetExpectation(int id, const string& csv_values) override;
     46   void Invoke() override;
     47   bool CheckResults() override;
     48   string ReadOutput(int id) override { return "no-op"; }
     49 
     50  private:
     51   class Expectation;
     52 
     53   bool use_nnapi_ = false;
     54   std::unique_ptr<FlatBufferModel> model_;
     55   std::unique_ptr<Interpreter> interpreter_;
     56   std::map<int, std::unique_ptr<Expectation>> expected_output_;
     57   bool must_allocate_tensors_ = true;
     58 };
     59 
     60 }  // namespace testing
     61 }  // namespace tflite
     62 
     63 #endif  // TENSORFLOW_CONTRIB_LITE_TESTING_TFLITE_DRIVER_H_
     64