• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 
16 #ifndef TENSORFLOW_LITE_TOOLS_ACCURACY_EVAL_PIPELINE_H_
17 #define TENSORFLOW_LITE_TOOLS_ACCURACY_EVAL_PIPELINE_H_
18 
19 #include <string>
20 
21 #include "tensorflow/lite/tools/accuracy/accuracy_eval_stage.h"
22 #include "tensorflow/lite/tools/accuracy/stage.h"
23 #include "tensorflow/core/public/session.h"
24 
25 namespace tensorflow {
26 namespace metrics {
27 
28 // Pipeline for evaluating a model.
29 // Runs the graph and passes the output of graph to
30 // the provided instance of AccuracyEval.
31 // Example usage:
32 // AccuracyEval *eval;
33 // GraphDef graph_def;
34 // ... populate graph_def...
35 //
36 // EvalPipeline eval_pipeline(&graph_def,
37 //    {.model_input_node_name = "model_input",
38 //     .model_output_node_name = "model_output"},
39 //     eval);
40 //  std::unique_ptr<Session> session(NewSession(SessionOptions()));
41 //  TF_CHECK_OK(eval_pipeline.AttachSession(std::move(session)));
42 //  Tensor input = ... read input for the model ...
43 //  Tensor ground_truth = ... read ground truth for the model ...
44 //  TF_CHECK_OK(eval_pipeline.Run(input, ground_truth));
45 //
46 class EvalPipeline {
47  public:
48   struct Params {
49     string model_input_node_name;
50     string model_output_node_name;
51   };
52 
53   // Creates a new `EvalPipeline` object. The ownership of the `accuracy_eval`
54   // is retained by the caller. Lifetime of `accuracy_eval` instance should
55   // be longer than the lifetime of this instance of pipeline.
EvalPipeline(const GraphDef & graph,const Params & params,AccuracyEval * accuracy_eval)56   EvalPipeline(const GraphDef& graph, const Params& params,
57                AccuracyEval* accuracy_eval)
58       : model_graph_(graph),
59         params_(params),
60         eval_(accuracy_eval),
61         session_(nullptr) {}
62 
63   EvalPipeline(const EvalPipeline&) = delete;
64   EvalPipeline& operator=(const EvalPipeline&) = delete;
65 
66   EvalPipeline(const EvalPipeline&&) = delete;
67   EvalPipeline& operator=(const EvalPipeline&&) = delete;
68 
69   // Attaches the given session to this instance of pipeline.
70   // The provided session object will be reused for subsequent calls to
71   // EvalPipeline::Run.
72   Status AttachSession(std::unique_ptr<Session> session);
73 
74   // Runs the model by feeding `input` and then passes the output of the model
75   // along with provided `ground_truth` to the AccuracyEval instance by calling
76   // AccuracyEval::ComputeEval.
77   Status Run(const Tensor& input, const Tensor& ground_truth);
78 
79  private:
80   GraphDef model_graph_;
81   Params params_;
82   AccuracyEval* eval_;
83   std::unique_ptr<Session> session_;
84 };
85 }  //  namespace metrics
86 }  //  namespace tensorflow
87 #endif  // TENSORFLOW_LITE_TOOLS_ACCURACY_EVAL_PIPELINE_H_
88