37 size_t nmemb = fread(&len,
sizeof(len), 1, fp);
38 if (nmemb != 1 || len == 0) {
42 nmemb = fread(name,
sizeof(name[0]), len, fp);
49 size_t len = str.length();
50 fwrite(&len,
sizeof(len), 1, fp);
51 fwrite(str.c_str(),
sizeof(str[0]), len, fp);
55template <
typename Dtype>
67 nmemb = fread(&ndim,
sizeof(ndim), 1, fp);
69 std::vector<int> shape(ndim);
70 for (
int i = 0; i < ndim; ++i) {
71 nmemb = fread(&shape[i],
sizeof(shape[i]), 1, fp);
80 CHECK_GE(blob->count(),
kMinDim);
83 nmemb = fread(blob->mutable_cpu_data(),
sizeof(Dtype), blob->count(), fp);
84 CHECK_EQ(
static_cast<int>(nmemb), blob->count());
89template <
typename Dtype>
96 fwrite(&ndim,
sizeof(ndim), 1, fp);
97 for (
int i = 0; i < ndim; ++i) {
99 fwrite(&dim,
sizeof(dim), 1, fp);
103 if (blob.
count() > 0) {
109template std::shared_ptr<base::Blob<double>>
BinaryReadBlob(FILE *fp);
114template <
typename Dtype>
116 const char *file_path) {
118 std::map<std::string, std::shared_ptr<base::Blob<Dtype>>> data_dict;
120 FILE *fp = fopen(file_path,
"rb");
122 AERROR <<
"Failed opening Binaryary file: " << file_path;
129 data_dict[name] = BinaryReadBlob<Dtype>(fp);
136template <
typename Btype>
138 const std::map<std::string, Btype> &data_dict) {
139 FILE *fp = fopen(file_path,
"wb");
141 AERROR <<
"Failed opening Binaryary file: " << file_path;
145 typename std::map<std::string, Btype>::const_iterator it = data_dict.begin();
146 while (it != data_dict.end()) {
155 fwrite(&end,
sizeof(end), 1, fp);
161template std::map<std::string, std::shared_ptr<base::Blob<float>>>
163template std::map<std::string, std::shared_ptr<base::Blob<double>>>
167 const char *file_path,
170 const char *file_path,
175 const char *file_path,
178 const char *file_path,
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
const Dtype * cpu_data() const
const std::vector< int > & shape() const
size_t BinaryWriteString(FILE *fp, const std::string &str)
size_t BinaryReadString(FILE *fp, char *name)
std::string get_dtype(const base::Blob< double > &blob)
void BinaryWriteBlob(FILE *fp, const base::Blob< Dtype > &blob)
std::shared_ptr< base::Blob< Dtype > > BinaryReadBlob(FILE *fp)
bool BinaryWriteFile(const char *file_path, const std::map< std::string, Btype > &data_dict)
constexpr size_t kMaxStrLen
std::map< std::string, std::shared_ptr< base::Blob< Dtype > > > BinaryReadFile(const char *file_path)