Apollo 11.0
自动驾驶开放平台
binary_data.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <vector>
20
21#include "cyber/common/log.h"
22
23namespace apollo {
24namespace perception {
25namespace inference {
26
27inline std::string get_dtype(const base::Blob<double> &blob) {
28 return "float64";
29}
30
31inline std::string get_dtype(const base::Blob<float> &blob) {
32 return "float32";
33}
34
35size_t BinaryReadString(FILE *fp, char *name) {
36 size_t len = 0;
37 size_t nmemb = fread(&len, sizeof(len), 1, fp);
38 if (nmemb != 1 || len == 0) {
39 return 0;
40 }
41 CHECK_LT(len, kMaxStrLen);
42 nmemb = fread(name, sizeof(name[0]), len, fp);
43 CHECK_EQ(nmemb, len);
44 name[len] = 0;
45 return len;
46}
47
48size_t BinaryWriteString(FILE *fp, const std::string &str) {
49 size_t len = str.length();
50 fwrite(&len, sizeof(len), 1, fp);
51 fwrite(str.c_str(), sizeof(str[0]), len, fp);
52 return len;
53}
54
55template <typename Dtype>
56std::shared_ptr<base::Blob<Dtype>> BinaryReadBlob(FILE *fp) {
57 int ndim;
58 std::shared_ptr<base::Blob<Dtype>> blob(new base::Blob<Dtype>());
59 char dtype[kMaxStrLen];
60
61 // read dtype
62 size_t nmemb = BinaryReadString(fp, dtype);
63 CHECK_GT(nmemb, 0U);
64 CHECK_EQ(get_dtype(*blob), dtype);
65
66 // read dims
67 nmemb = fread(&ndim, sizeof(ndim), 1, fp);
68 CHECK_EQ(nmemb, 1U);
69 std::vector<int> shape(ndim);
70 for (int i = 0; i < ndim; ++i) {
71 nmemb = fread(&shape[i], sizeof(shape[i]), 1, fp);
72 CHECK_EQ(nmemb, 1U);
73 }
74 if (ndim == 0) {
75 return blob;
76 }
77
78 // init blob
79 blob->Reshape(shape);
80 CHECK_GE(blob->count(), kMinDim);
81
82 // read data
83 nmemb = fread(blob->mutable_cpu_data(), sizeof(Dtype), blob->count(), fp);
84 CHECK_EQ(static_cast<int>(nmemb), blob->count());
85
86 return blob;
87}
88
89template <typename Dtype>
90void BinaryWriteBlob(FILE *fp, const base::Blob<Dtype> &blob) {
91 int ndim, dim;
92 // write dtype
94 // write dims
95 ndim = blob.num_axes();
96 fwrite(&ndim, sizeof(ndim), 1, fp);
97 for (int i = 0; i < ndim; ++i) {
98 dim = blob.shape(i);
99 fwrite(&dim, sizeof(dim), 1, fp);
100 }
101
102 // write data
103 if (blob.count() > 0) {
104 fwrite(blob.cpu_data(), sizeof(Dtype), blob.count(), fp);
105 }
106}
107
108template std::shared_ptr<base::Blob<float>> BinaryReadBlob(FILE *fp);
109template std::shared_ptr<base::Blob<double>> BinaryReadBlob(FILE *fp);
110
111template void BinaryWriteBlob(FILE *fp, const base::Blob<float> &blob);
112template void BinaryWriteBlob(FILE *fp, const base::Blob<double> &blob);
113
114template <typename Dtype>
115std::map<std::string, std::shared_ptr<base::Blob<Dtype>>> BinaryReadFile(
116 const char *file_path) {
117 char name[kMaxStrLen];
118 std::map<std::string, std::shared_ptr<base::Blob<Dtype>>> data_dict;
119
120 FILE *fp = fopen(file_path, "rb");
121 if (NULL == fp) {
122 AERROR << "Failed opening Binaryary file: " << file_path;
123 return data_dict;
124 }
125
126 // read blob name
127 while (BinaryReadString(fp, name)) {
128 // insert into data_dict
129 data_dict[name] = BinaryReadBlob<Dtype>(fp);
130 }
131
132 fclose(fp);
133 return data_dict;
134}
135
136template <typename Btype>
137bool BinaryWriteFile(const char *file_path,
138 const std::map<std::string, Btype> &data_dict) {
139 FILE *fp = fopen(file_path, "wb");
140 if (NULL == fp) {
141 AERROR << "Failed opening Binaryary file: " << file_path;
142 return false;
143 }
144
145 typename std::map<std::string, Btype>::const_iterator it = data_dict.begin();
146 while (it != data_dict.end()) {
147 // write blob name
148 BinaryWriteString(fp, it->first);
149 // write blob
150 BinaryWriteBlob(fp, *it->second);
151
152 ++it;
153 }
154 int end = 0;
155 fwrite(&end, sizeof(end), 1, fp);
156
157 fclose(fp);
158 return true;
159}
160
161template std::map<std::string, std::shared_ptr<base::Blob<float>>>
162BinaryReadFile(const char *file_path);
163template std::map<std::string, std::shared_ptr<base::Blob<double>>>
164BinaryReadFile(const char *file_path);
165
166template bool BinaryWriteFile(
167 const char *file_path,
168 const std::map<std::string, std::shared_ptr<base::Blob<float>>> &data_dict);
169template bool BinaryWriteFile(
170 const char *file_path,
171 const std::map<std::string, std::shared_ptr<base::Blob<double>>>
172 &data_dict);
173
174template bool BinaryWriteFile(
175 const char *file_path,
176 const std::map<std::string, base::Blob<float> *> &data_dict);
177template bool BinaryWriteFile(
178 const char *file_path,
179 const std::map<std::string, base::Blob<double> *> &data_dict);
180
181} // namespace inference
182} // namespace perception
183} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
const Dtype * cpu_data() const
Definition blob.cc:137
const std::vector< int > & shape() const
Definition blob.h:130
#define AERROR
Definition log.h:44
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
Definition binary_data.h:29
std::map< std::string, std::shared_ptr< base::Blob< Dtype > > > BinaryReadFile(const char *file_path)
class register implement
Definition arena_queue.h:37