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_TF_DRIVER_H_
     16 #define TENSORFLOW_CONTRIB_LITE_TESTING_TF_DRIVER_H_
     17 
     18 #include <unordered_map>
     19 #include <vector>
     20 
     21 #include "tensorflow/contrib/lite/testing/split.h"
     22 #include "tensorflow/contrib/lite/testing/test_runner.h"
     23 #include "tensorflow/core/framework/tensor.h"
     24 #include "tensorflow/core/framework/tensor_shape.h"
     25 #include "tensorflow/core/framework/types.h"
     26 #include "tensorflow/core/platform/logging.h"
     27 #include "tensorflow/core/public/session.h"
     28 
     29 namespace tflite {
     30 namespace testing {
     31 
     32 // A test runner that feeds inputs into Tensorflow and generates outputs.
     33 class TfDriver : public TestRunner {
     34  public:
     35   explicit TfDriver(const std::vector<string>& input_layer,
     36                     const std::vector<string>& input_layer_type,
     37                     const std::vector<string>& input_layer_shape,
     38                     const std::vector<string>& output_layer);
     39   ~TfDriver() override {}
     40 
     41   void LoadModel(const string& bin_file_path) override;
     42   void SetInput(int id, const string& csv_values) override;
     43   void Invoke() override;
     44   string ReadOutput(int id) override;
     45 
     46   const std::vector<int>& GetInputs() override { return input_ids_; }
     47   const std::vector<int>& GetOutputs() override { return output_ids_; }
     48   void ReshapeTensor(int id, const string& csv_values) override;
     49   // Note: ResetTensor only works for input tensor.
     50   void ResetTensor(int id) override;
     51 
     52   // no-op. SetInput will overwrite existing data .
     53   void AllocateTensors() override {}
     54   // no-op. Tf driver is not supposed to check the results.
     55   void SetExpectation(int id, const string& csv_values) override {}
     56   // tf driver is not supposed to check the results.
     57   bool CheckResults() override { return false; }
     58 
     59  private:
     60   std::unique_ptr<tensorflow::Session> session_;
     61   std::vector<int> input_ids_;
     62   std::vector<string> input_names_;
     63   std::vector<std::vector<int64_t>> input_shapes_;
     64   std::vector<tensorflow::DataType> input_types_;
     65   std::unordered_map<string, tensorflow::Tensor> input_tensors_;
     66 
     67   std::vector<int> output_ids_;
     68   std::vector<string> output_names_;
     69   std::vector<::tensorflow::Tensor> output_tensors_;
     70 };
     71 
     72 }  // namespace testing
     73 }  // namespace tflite
     74 
     75 #endif  // TENSORFLOW_CONTRIB_LITE_TESTING_TF_DRIVER_H_
     76