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