Caffe2 - C++ API
A deep learning, cross platform ML framework
summarize_op.h
1 
17 #ifndef CAFFE2_OPERATORS_SUMMARIZE_OP_H_
18 #define CAFFE2_OPERATORS_SUMMARIZE_OP_H_
19 
20 #include <fstream>
21 
22 #include "caffe2/core/context.h"
23 #include "caffe2/core/operator.h"
24 #include "caffe2/utils/math.h"
25 
26 namespace caffe2 {
27 
28 constexpr char kSummaryzeOpExtension[] = ".summary";
29 
30 template <typename T, class Context>
31 class SummarizeOp final : public Operator<Context> {
32  public:
33  SummarizeOp(const OperatorDef& def, Workspace* ws)
34  : Operator<Context>(def, ws),
35  to_file_(OperatorBase::GetSingleArgument<int>("to_file", 0)) {
36  if (to_file_) {
37  // We will output to file instead of printing on screen.
38  const string& target_folder = ws->RootFolder();
39  // We will write each individual tensor to its individual file.
40  // Also, since the namescope is currently represented by "/", we will
41  // need to replace it with a symbol that does not conflict with the
42  // folder separator in Linux.
43  string proper_name = def.input(0);
44  std::replace(proper_name.begin(), proper_name.end(), '/', '#');
45  log_file_.reset(new std::ofstream(
46  target_folder + "/" + proper_name + kSummaryzeOpExtension,
47  std::ofstream::out | std::ofstream::trunc));
48  CAFFE_ENFORCE(
49  log_file_->good(),
50  "Failed to open summarize file for tensor ",
51  def.input(0),
52  ". rdstate() = ",
53  log_file_->rdstate());
54  }
55  }
56  ~SummarizeOp() {
57  if (to_file_)
58  log_file_->close();
59  }
60  USE_OPERATOR_CONTEXT_FUNCTIONS;
61  bool RunOnDevice() override;
62 
63  static constexpr int MIN_IDX = 0;
64  static constexpr int MAX_IDX = 1;
65  static constexpr int MEAN_IDX = 2;
66  static constexpr int STD_IDX = 3;
67 
68  static constexpr int NUM_STATS = 4;
69 
70  protected:
71  bool to_file_;
72  std::unique_ptr<std::ofstream> log_file_;
73 };
74 
75 } // namespace caffe2
76 
77 #endif // CAFFE2_OPERATORS_SUMMARIZE_OP_H_
const string & RootFolder()
Return the root folder of the workspace.
Definition: workspace.h:183
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:63
Copyright (c) 2016-present, Facebook, Inc.