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