Caffe
io.hpp
1 #ifndef CAFFE_UTIL_IO_H_
2 #define CAFFE_UTIL_IO_H_
3 
4 #include <boost/filesystem.hpp>
5 #include <iomanip>
6 #include <iostream> // NOLINT(readability/streams)
7 #include <string>
8 
9 #include "google/protobuf/message.h"
10 
11 #include "caffe/common.hpp"
12 #include "caffe/proto/caffe.pb.h"
13 #include "caffe/util/format.hpp"
14 
15 #ifndef CAFFE_TMP_DIR_RETRIES
16 #define CAFFE_TMP_DIR_RETRIES 100
17 #endif
18 
19 namespace caffe {
20 
21 using ::google::protobuf::Message;
22 using ::boost::filesystem::path;
23 
24 inline void MakeTempDir(string* temp_dirname) {
25  temp_dirname->clear();
26  const path& model =
27  boost::filesystem::temp_directory_path()/"caffe_test.%%%%-%%%%";
28  for ( int i = 0; i < CAFFE_TMP_DIR_RETRIES; i++ ) {
29  const path& dir = boost::filesystem::unique_path(model).string();
30  bool done = boost::filesystem::create_directory(dir);
31  if ( done ) {
32  *temp_dirname = dir.string();
33  return;
34  }
35  }
36  LOG(FATAL) << "Failed to create a temporary directory.";
37 }
38 
39 inline void MakeTempFilename(string* temp_filename) {
40  static path temp_files_subpath;
41  static uint64_t next_temp_file = 0;
42  temp_filename->clear();
43  if ( temp_files_subpath.empty() ) {
44  string path_string="";
45  MakeTempDir(&path_string);
46  temp_files_subpath = path_string;
47  }
48  *temp_filename =
49  (temp_files_subpath/caffe::format_int(next_temp_file++, 9)).string();
50 }
51 
52 bool ReadProtoFromTextFile(const char* filename, Message* proto);
53 
54 inline bool ReadProtoFromTextFile(const string& filename, Message* proto) {
55  return ReadProtoFromTextFile(filename.c_str(), proto);
56 }
57 
58 inline void ReadProtoFromTextFileOrDie(const char* filename, Message* proto) {
59  CHECK(ReadProtoFromTextFile(filename, proto));
60 }
61 
62 inline void ReadProtoFromTextFileOrDie(const string& filename, Message* proto) {
63  ReadProtoFromTextFileOrDie(filename.c_str(), proto);
64 }
65 
66 void WriteProtoToTextFile(const Message& proto, const char* filename);
67 inline void WriteProtoToTextFile(const Message& proto, const string& filename) {
68  WriteProtoToTextFile(proto, filename.c_str());
69 }
70 
71 bool ReadProtoFromBinaryFile(const char* filename, Message* proto);
72 
73 inline bool ReadProtoFromBinaryFile(const string& filename, Message* proto) {
74  return ReadProtoFromBinaryFile(filename.c_str(), proto);
75 }
76 
77 inline void ReadProtoFromBinaryFileOrDie(const char* filename, Message* proto) {
78  CHECK(ReadProtoFromBinaryFile(filename, proto));
79 }
80 
81 inline void ReadProtoFromBinaryFileOrDie(const string& filename,
82  Message* proto) {
83  ReadProtoFromBinaryFileOrDie(filename.c_str(), proto);
84 }
85 
86 
87 void WriteProtoToBinaryFile(const Message& proto, const char* filename);
88 inline void WriteProtoToBinaryFile(
89  const Message& proto, const string& filename) {
90  WriteProtoToBinaryFile(proto, filename.c_str());
91 }
92 
93 bool ReadFileToDatum(const string& filename, const int label, Datum* datum);
94 
95 inline bool ReadFileToDatum(const string& filename, Datum* datum) {
96  return ReadFileToDatum(filename, -1, datum);
97 }
98 
99 bool ReadImageToDatum(const string& filename, const int label,
100  const int height, const int width, const bool is_color,
101  const std::string & encoding, Datum* datum);
102 
103 inline bool ReadImageToDatum(const string& filename, const int label,
104  const int height, const int width, const bool is_color, Datum* datum) {
105  return ReadImageToDatum(filename, label, height, width, is_color,
106  "", datum);
107 }
108 
109 inline bool ReadImageToDatum(const string& filename, const int label,
110  const int height, const int width, Datum* datum) {
111  return ReadImageToDatum(filename, label, height, width, true, datum);
112 }
113 
114 inline bool ReadImageToDatum(const string& filename, const int label,
115  const bool is_color, Datum* datum) {
116  return ReadImageToDatum(filename, label, 0, 0, is_color, datum);
117 }
118 
119 inline bool ReadImageToDatum(const string& filename, const int label,
120  Datum* datum) {
121  return ReadImageToDatum(filename, label, 0, 0, true, datum);
122 }
123 
124 inline bool ReadImageToDatum(const string& filename, const int label,
125  const std::string & encoding, Datum* datum) {
126  return ReadImageToDatum(filename, label, 0, 0, true, encoding, datum);
127 }
128 
129 bool DecodeDatumNative(Datum* datum);
130 bool DecodeDatum(Datum* datum, bool is_color);
131 
132 #ifdef USE_OPENCV
133 cv::Mat ReadImageToCVMat(const string& filename,
134  const int height, const int width, const bool is_color);
135 
136 cv::Mat ReadImageToCVMat(const string& filename,
137  const int height, const int width);
138 
139 cv::Mat ReadImageToCVMat(const string& filename,
140  const bool is_color);
141 
142 cv::Mat ReadImageToCVMat(const string& filename);
143 
144 cv::Mat DecodeDatumToCVMatNative(const Datum& datum);
145 cv::Mat DecodeDatumToCVMat(const Datum& datum, bool is_color);
146 
147 void CVMatToDatum(const cv::Mat& cv_img, Datum* datum);
148 #endif // USE_OPENCV
149 
150 } // namespace caffe
151 
152 #endif // CAFFE_UTIL_IO_H_
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14