當前位置:
首頁 > 知識 > caffe源碼學習之Proto數據格式1

caffe源碼學習之Proto數據格式1

前言:

由於業務需要,接觸caffe已經有接近半年,一直忙著閱讀各種論文,重現大大小小的模型. 期間也總結過一些caffe源碼學習筆記,斷斷續續,這次打算系統的記錄一下caffe源碼學習筆記,鞏固一下C++,同時也梳理一下自己之前的理解。

正文:

我們先不看caffe的框架結構,先介紹一下caffe.proto,是google開源的一種數據交互格式--Google Protobuf,這種數據的格式,我們可以看到caffe.proto中內容:

syntax = "proto2";

package caffe; //caffe.prto中的各個結構封裝在caffe包中,可以通過using namespace caffe; 或者caffe::** 調用

// Specifies the shape (dimensions) of a Blob.
message BlobShape {
repeated int64 dim = 1 [packed = true];
}

message BlobProto {
optional BlobShape shape = 7;
repeated float data = 5 [packed = true];
repeated float diff = 6 [packed = true];
repeated double double_data = 8 [packed = true];
repeated double double_diff = 9 [packed = true];

// 4D dimensions -- deprecated. Use "shape" instead.
optional int32 num = 1 [default = 0];
optional int32 channels = 2 [default = 0];
optional int32 height = 3 [default = 0];
optional int32 width = 4 [default = 0];
}

.......

當我們在編譯完成caffe之後,會自動在src/caffe/proto中生成兩個文件caffe.pb.h 和caffe.pb.cc

那麼這種數據格式在程序中是如何被使用的呢? 我們舉一個簡單的例子,來演示caffe.proto生成caffe.pb.h和caffe.pb.cc以及被調用的過程

如果你之前能夠編譯caffe成功,則說明你已經成功安裝了Protobuf,那麼我簡單的編寫一個使用Protobuf的例子吧.

我們先編寫一個文件caffe.proto:

package caffe;
message student
{
required int32 age = 1; //ID required 表示必要欄位
required string name = 2; //str 必要欄位
optional int32 grade = 3 ; //optional field 可選欄位,可以有無,最多b
}

然後我們執行如下操作。

protoc -I=. --cpp_out=. ./caffe.proto

protobuf會自動生成.cc和.h文件

1 // Generated by the protocol buffer compiler. DO NOT EDIT!
2 // source: caffe.proto
3
4 #ifndef PROTOBUF_caffe_2eproto__INCLUDED
5 #define PROTOBUF_caffe_2eproto__INCLUDED
6
7 #include
8
9 #include
10
11 #if GOOGLE_PROTOBUF_VERSION < 2005000 12 #error This file was generated by a newer version of protoc which is 13 #error incompatible with your Protocol Buffer headers. Please update 14 #error your headers. 15 #endif 16 #if 2005000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 17 #error This file was generated by an older version of protoc which is 18 #error incompatible with your Protocol Buffer headers. Please 19 #error regenerate this file with a newer version of protoc. 20 #endif 21 22 #include
23 #include
24 #include
25 #include
26 #include
27 // @@protoc_insertion_point(includes)
28
29 namespace caffe {
30
31 // Internal implementation detail -- do not call these.
32 void protobuf_AddDesc_caffe_2eproto;
33 void protobuf_AssignDesc_caffe_2eproto;
34 void protobuf_ShutdownFile_caffe_2eproto;
35
36 class student;
37
38 // ===================================================================
39
40 class student : public ::google::protobuf::Message {
41 public:
42 student;
43 virtual ~student;
44
45 student(const student& from);
46
47 inline student& operator=(const student& from) {
48 CopyFrom(from);
49 return *this;
50 }
51
52 inline const ::google::protobuf::UnknownFieldSet& unknown_fields const {
53 return _unknown_fields_;
54 }
55
56 inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields {
57 return &_unknown_fields_;
58 }
59
60 static const ::google::protobuf::Descriptor* descriptor;
61 static const student& default_instance;
62
63 void Swap(student* other);
64
65 // implements Message ----------------------------------------------
66
67 student* New const;
68 void CopyFrom(const ::google::protobuf::Message& from);
69 void MergeFrom(const ::google::protobuf::Message& from);
70 void CopyFrom(const student& from);
71 void MergeFrom(const student& from);
72 void Clear;
73 bool IsInitialized const;
74
75 int ByteSize const;
76 bool MergePartialFromCodedStream(
77 ::google::protobuf::io::CodedInputStream* input);
78 void SerializeWithCachedSizes(
79 ::google::protobuf::io::CodedOutputStream* output) const;
80 ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
81 int GetCachedSize const { return _cached_size_; }
82 private:
83 void SharedCtor;
84 void SharedDtor;
85 void SetCachedSize(int size) const;
86 public:
87
88 ::google::protobuf::Metadata GetMetadata const;
89
90 // nested types ----------------------------------------------------
91
92 // accessors -------------------------------------------------------
93
94 // required int32 age = 1;
95 inline bool has_age const;
96 inline void clear_age;
97 static const int kAgeFieldNumber = 1;
98 inline ::google::protobuf::int32 age const;
99 inline void set_age(::google::protobuf::int32 value);
100
101 // required string name = 2;
102 inline bool has_name const;
103 inline void clear_name;
104 static const int kNameFieldNumber = 2;
105 inline const ::std::string& name const;
106 inline void set_name(const ::std::string& value);
107 inline void set_name(const char* value);
108 inline void set_name(const char* value, size_t size);
109 inline ::std::string* mutable_name;
110 inline ::std::string* release_name;
111 inline void set_allocated_name(::std::string* name);
112
113 // optional int32 grade = 3;
114 inline bool has_grade const;
115 inline void clear_grade;
116 static const int kGradeFieldNumber = 3;
117 inline ::google::protobuf::int32 grade const;
118 inline void set_grade(::google::protobuf::int32 value);
119
120 // @@protoc_insertion_point(class_scope:caffe.student)
121 private:
122 inline void set_has_age;
123 inline void clear_has_age;
124 inline void set_has_name;
125 inline void clear_has_name;
126 inline void set_has_grade;
127 inline void clear_has_grade;
128
129 ::google::protobuf::UnknownFieldSet _unknown_fields_;
130
131 ::std::string* name_;
132 ::google::protobuf::int32 age_;
133 ::google::protobuf::int32 grade_;
134
135 mutable int _cached_size_;
136 ::google::protobuf::uint32 _has_bits_[(3 + 31) / 32];
137
138 friend void protobuf_AddDesc_caffe_2eproto;
139 friend void protobuf_AssignDesc_caffe_2eproto;
140 friend void protobuf_ShutdownFile_caffe_2eproto;
141
142 void InitAsDefaultInstance;
143 static student* default_instance_;
144 };
145 // ===================================================================
146
147
148 // ===================================================================
149
150 // student
151
152 // required int32 age = 1;
153 inline bool student::has_age const {
154 return (_has_bits_[0] & 0x00000001u) != 0;
155 }
156 inline void student::set_has_age {
157 _has_bits_[0] |= 0x00000001u;
158 }
159 inline void student::clear_has_age {
160 _has_bits_[0] &= ~0x00000001u;
161 }
162 inline void student::clear_age {
163 age_ = 0;
164 clear_has_age;
165 }
166 inline ::google::protobuf::int32 student::age const {
167 return age_;
168 }
169 inline void student::set_age(::google::protobuf::int32 value) {
170 set_has_age;
171 age_ = value;
172 }
173
174 // required string name = 2;
175 inline bool student::has_name const {
176 return (_has_bits_[0] & 0x00000002u) != 0;
177 }
178 inline void student::set_has_name {
179 _has_bits_[0] |= 0x00000002u;
180 }
181 inline void student::clear_has_name {
182 _has_bits_[0] &= ~0x00000002u;
183 }
184 inline void student::clear_name {
185 if (name_ != &::google::protobuf::internal::kEmptyString) {
186 name_->clear;
187 }
188 clear_has_name;
189 }
190 inline const ::std::string& student::name const {
191 return *name_;
192 }
193 inline void student::set_name(const ::std::string& value) {
194 set_has_name;
195 if (name_ == &::google::protobuf::internal::kEmptyString) {
196 name_ = new ::std::string;
197 }
198 name_->assign(value);
199 }
200 inline void student::set_name(const char* value) {
201 set_has_name;
202 if (name_ == &::google::protobuf::internal::kEmptyString) {
203 name_ = new ::std::string;
204 }
205 name_->assign(value);
206 }
207 inline void student::set_name(const char* value, size_t size) {
208 set_has_name;
209 if (name_ == &::google::protobuf::internal::kEmptyString) {
210 name_ = new ::std::string;
211 }
212 name_->assign(reinterpret_cast(value), size);
213 }
214 inline ::std::string* student::mutable_name {
215 set_has_name;
216 if (name_ == &::google::protobuf::internal::kEmptyString) {
217 name_ = new ::std::string;
218 }
219 return name_;
220 }
221 inline ::std::string* student::release_name {
222 clear_has_name;
223 if (name_ == &::google::protobuf::internal::kEmptyString) {
224 return NULL;
225 } else {
226 ::std::string* temp = name_;
227 name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString);
228 return temp;
229 }
230 }
231 inline void student::set_allocated_name(::std::string* name) {
232 if (name_ != &::google::protobuf::internal::kEmptyString) {
233 delete name_;
234 }
235 if (name) {
236 set_has_name;
237 name_ = name;
238 } else {
239 clear_has_name;
240 name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString);
241 }
242 }
243
244 // optional int32 grade = 3;
245 inline bool student::has_grade const {
246 return (_has_bits_[0] & 0x00000004u) != 0;
247 }
248 inline void student::set_has_grade {
249 _has_bits_[0] |= 0x00000004u;
250 }
251 inline void student::clear_has_grade {
252 _has_bits_[0] &= ~0x00000004u;
253 }
254 inline void student::clear_grade {
255 grade_ = 0;
256 clear_has_grade;
257 }
258 inline ::google::protobuf::int32 student::grade const {
259 return grade_;
260 }
261 inline void student::set_grade(::google::protobuf::int32 value) {
262 set_has_grade;
263 grade_ = value;
264 }
265
266
267 // @@protoc_insertion_point(namespace_scope)
268
269 } // namespace caffe
270
271 #ifndef SWIG
272 namespace google {
273 namespace protobuf {
274
275
276 } // namespace google
277 } // namespace protobuf
278 #endif // SWIG
279
280 // @@protoc_insertion_point(global_scope)
281
282 #endif // PROTOBUF_caffe_2eproto__INCLUDED

caffe.pb.h

1 // Generated by the protocol buffer compiler. DO NOT EDIT!
2 // source: caffe.proto
3
4 #define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
5 #include "caffe.pb.h"
6
7 #include
8
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 // @@protoc_insertion_point(includes)
18
19 namespace caffe {
20
21 namespace {
22
23 const ::google::protobuf::Descriptor* student_descriptor_ = NULL;
24 const ::google::protobuf::internal::GeneratedMessageReflection*
25 student_reflection_ = NULL;
26
27 } // namespace
28
29
30 void protobuf_AssignDesc_caffe_2eproto {
31 protobuf_AddDesc_caffe_2eproto;
32 const ::google::protobuf::FileDescriptor* file =
33 ::google::protobuf::DescriptorPool::generated_pool->FindFileByName(
34 "caffe.proto");
35 GOOGLE_CHECK(file != NULL);
36 student_descriptor_ = file->message_type(0);
37 static const int student_offsets_[3] = {
38 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(student, age_),
39 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(student, name_),
40 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(student, grade_),
41 };
42 student_reflection_ =
43 new ::google::protobuf::internal::GeneratedMessageReflection(
44 student_descriptor_,
45 student::default_instance_,
46 student_offsets_,
47 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(student, _has_bits_[0]),
48 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(student, _unknown_fields_),
49 -1,
50 ::google::protobuf::DescriptorPool::generated_pool,
51 ::google::protobuf::MessageFactory::generated_factory,
52 sizeof(student));
53 }
54
55 namespace {
56
57 GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_);
58 inline void protobuf_AssignDescriptorsOnce {
59 ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_,
60 &protobuf_AssignDesc_caffe_2eproto);
61 }
62
63 void protobuf_RegisterTypes(const ::std::string&) {
64 protobuf_AssignDescriptorsOnce;
65 ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
66 student_descriptor_, &student::default_instance);
67 }
68
69 } // namespace
70
71 void protobuf_ShutdownFile_caffe_2eproto {
72 delete student::default_instance_;
73 delete student_reflection_;
74 }
75
76 void protobuf_AddDesc_caffe_2eproto {
77 static bool already_here = false;
78 if (already_here) return;
79 already_here = true;
80 GOOGLE_PROTOBUF_VERIFY_VERSION;
81
82 ::google::protobuf::DescriptorPool::InternalAddGeneratedFile(
83 "
13caffe.proto2205caffe"3
07student2213
03age3001"
84 " 02(052214
04name3002 02( 22
05grade3003 01(05", 73);
85 ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
86 "caffe.proto", &protobuf_RegisterTypes);
87 student::default_instance_ = new student;
88 student::default_instance_->InitAsDefaultInstance;
89 ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_caffe_2eproto);
90 }
91
92 // Force AddDescriptors to be called at static initialization time.
93 struct StaticDescriptorInitializer_caffe_2eproto {
94 StaticDescriptorInitializer_caffe_2eproto {
95 protobuf_AddDesc_caffe_2eproto;
96 }
97 } static_descriptor_initializer_caffe_2eproto_;
98
99 // ===================================================================
100
101 #ifndef _MSC_VER
102 const int student::kAgeFieldNumber;
103 const int student::kNameFieldNumber;
104 const int student::kGradeFieldNumber;
105 #endif // !_MSC_VER
106
107 student::student
108 : ::google::protobuf::Message {
109 SharedCtor;
110 }
111
112 void student::InitAsDefaultInstance {
113 }
114
115 student::student(const student& from)
116 : ::google::protobuf::Message {
117 SharedCtor;
118 MergeFrom(from);
119 }
120
121 void student::SharedCtor {
122 _cached_size_ = 0;
123 age_ = 0;
124 name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString);
125 grade_ = 0;
126 ::memset(_has_bits_, 0, sizeof(_has_bits_));
127 }
128
129 student::~student {
130 SharedDtor;
131 }
132
133 void student::SharedDtor {
134 if (name_ != &::google::protobuf::internal::kEmptyString) {
135 delete name_;
136 }
137 if (this != default_instance_) {
138 }
139 }
140
141 void student::SetCachedSize(int size) const {
142 GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN;
143 _cached_size_ = size;
144 GOOGLE_SAFE_CONCURRENT_WRITES_END;
145 }
146 const ::google::protobuf::Descriptor* student::descriptor {
147 protobuf_AssignDescriptorsOnce;
148 return student_descriptor_;
149 }
150
151 const student& student::default_instance {
152 if (default_instance_ == NULL) protobuf_AddDesc_caffe_2eproto;
153 return *default_instance_;
154 }
155
156 student* student::default_instance_ = NULL;
157
158 student* student::New const {
159 return new student;
160 }
161
162 void student::Clear {
163 if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { 164 age_ = 0; 165 if (has_name) { 166 if (name_ != &::google::protobuf::internal::kEmptyString) { 167 name_->clear;
168 }
169 }
170 grade_ = 0;
171 }
172 ::memset(_has_bits_, 0, sizeof(_has_bits_));
173 mutable_unknown_fields->Clear;
174 }
175
176 bool student::MergePartialFromCodedStream(
177 ::google::protobuf::io::CodedInputStream* input) {
178 #define DO_(EXPRESSION) if (!(EXPRESSION)) return false
179 ::google::protobuf::uint32 tag;
180 while ((tag = input->ReadTag) != 0) {
181 switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
182 // required int32 age = 1;
183 case 1: {
184 if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
185 ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) {
186 DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< 187 ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>(
188 input, &age_)));
189 set_has_age;
190 } else {
191 goto handle_uninterpreted;
192 }
193 if (input->ExpectTag(18)) goto parse_name;
194 break;
195 }
196
197 // required string name = 2;
198 case 2: {
199 if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
200 ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
201 parse_name:
202 DO_(::google::protobuf::internal::WireFormatLite::ReadString(
203 input, this->mutable_name));
204 ::google::protobuf::internal::WireFormat::VerifyUTF8String(
205 this->name.data, this->name.length,
206 ::google::protobuf::internal::WireFormat::PARSE);
207 } else {
208 goto handle_uninterpreted;
209 }
210 if (input->ExpectTag(24)) goto parse_grade;
211 break;
212 }
213
214 // optional int32 grade = 3;
215 case 3: {
216 if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
217 ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) {
218 parse_grade:
219 DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< 220 ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>(
221 input, &grade_)));
222 set_has_grade;
223 } else {
224 goto handle_uninterpreted;
225 }
226 if (input->ExpectAtEnd) return true;
227 break;
228 }
229
230 default: {
231 handle_uninterpreted:
232 if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
233 ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
234 return true;
235 }
236 DO_(::google::protobuf::internal::WireFormat::SkipField(
237 input, tag, mutable_unknown_fields));
238 break;
239 }
240 }
241 }
242 return true;
243 #undef DO_
244 }
245
246 void student::SerializeWithCachedSizes(
247 ::google::protobuf::io::CodedOutputStream* output) const {
248 // required int32 age = 1;
249 if (has_age) {
250 ::google::protobuf::internal::WireFormatLite::WriteInt32(1, this->age, output);
251 }
252
253 // required string name = 2;
254 if (has_name) {
255 ::google::protobuf::internal::WireFormat::VerifyUTF8String(
256 this->name.data, this->name.length,
257 ::google::protobuf::internal::WireFormat::SERIALIZE);
258 ::google::protobuf::internal::WireFormatLite::WriteString(
259 2, this->name, output);
260 }
261
262 // optional int32 grade = 3;
263 if (has_grade) {
264 ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->grade, output);
265 }
266
267 if (!unknown_fields.empty) {
268 ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
269 unknown_fields, output);
270 }
271 }
272
273 ::google::protobuf::uint8* student::SerializeWithCachedSizesToArray(
274 ::google::protobuf::uint8* target) const {
275 // required int32 age = 1;
276 if (has_age) {
277 target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(1, this->age, target);
278 }
279
280 // required string name = 2;
281 if (has_name) {
282 ::google::protobuf::internal::WireFormat::VerifyUTF8String(
283 this->name.data, this->name.length,
284 ::google::protobuf::internal::WireFormat::SERIALIZE);
285 target =
286 ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
287 2, this->name, target);
288 }
289
290 // optional int32 grade = 3;
291 if (has_grade) {
292 target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->grade, target);
293 }
294
295 if (!unknown_fields.empty) {
296 target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
297 unknown_fields, target);
298 }
299 return target;
300 }
301
302 int student::ByteSize const {
303 int total_size = 0;
304
305 if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { 306 // required int32 age = 1; 307 if (has_age) { 308 total_size += 1 + 309 ::google::protobuf::internal::WireFormatLite::Int32Size( 310 this->age);
311 }
312
313 // required string name = 2;
314 if (has_name) {
315 total_size += 1 +
316 ::google::protobuf::internal::WireFormatLite::StringSize(
317 this->name);
318 }
319
320 // optional int32 grade = 3;
321 if (has_grade) {
322 total_size += 1 +
323 ::google::protobuf::internal::WireFormatLite::Int32Size(
324 this->grade);
325 }
326
327 }
328 if (!unknown_fields.empty) {
329 total_size +=
330 ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
331 unknown_fields);
332 }
333 GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN;
334 _cached_size_ = total_size;
335 GOOGLE_SAFE_CONCURRENT_WRITES_END;
336 return total_size;
337 }
338
339 void student::MergeFrom(const ::google::protobuf::Message& from) {
340 GOOGLE_CHECK_NE(&from, this);
341 const student* source =
342 ::google::protobuf::internal::dynamic_cast_if_available(
343 &from);
344 if (source == NULL) {
345 ::google::protobuf::internal::ReflectionOps::Merge(from, this);
346 } else {
347 MergeFrom(*source);
348 }
349 }
350
351 void student::MergeFrom(const student& from) {
352 GOOGLE_CHECK_NE(&from, this);
353 if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { 354 if (from.has_age) { 355 set_age(from.age); 356 } 357 if (from.has_name) { 358 set_name(from.name); 359 } 360 if (from.has_grade) { 361 set_grade(from.grade); 362 } 363 } 364 mutable_unknown_fields->MergeFrom(from.unknown_fields);
365 }
366
367 void student::CopyFrom(const ::google::protobuf::Message& from) {
368 if (&from == this) return;
369 Clear;
370 MergeFrom(from);
371 }
372
373 void student::CopyFrom(const student& from) {
374 if (&from == this) return;
375 Clear;
376 MergeFrom(from);
377 }
378
379 bool student::IsInitialized const {
380 if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false;
381
382 return true;
383 }
384
385 void student::Swap(student* other) {
386 if (other != this) {
387 std::swap(age_, other->age_);
388 std::swap(name_, other->name_);
389 std::swap(grade_, other->grade_);
390 std::swap(_has_bits_[0], other->_has_bits_[0]);
391 _unknown_fields_.Swap(&other->_unknown_fields_);
392 std::swap(_cached_size_, other->_cached_size_);
393 }
394 }
395
396 ::google::protobuf::Metadata student::GetMetadata const {
397 protobuf_AssignDescriptorsOnce;
398 ::google::protobuf::Metadata metadata;
399 metadata.descriptor = student_descriptor_;
400 metadata.reflection = student_reflection_;
401 return metadata;
402 }
403
404
405 // @@protoc_insertion_point(namespace_scope)
406
407 } // namespace caffe
408
409 // @@protoc_insertion_point(global_scope)

caffe.pb.cc

生成之後,我們編寫一個讀取文件caffeReader.cpp

#include "caffe.pb.h"
#include
#include

using namespace std;

void InfoStudents(const caffe::student & stu){
cout<< "student info:"<

編譯完成之後,我們執行如下命令:

g++ caffeRead.cpp -o caffeReader caffe.pb.cc -I /usr/local/protobuf/include -L /usr/local/protobuf/lib -lprotobuf -pthread

生成二進位執行文件caffeReader.我們運行該文件就會顯示如下信息:

知道了proto文件是如何使用之,再去caffe.proto中看看caffe中定義的結構體:

message 表示需要傳輸的的參數的結構體.

caffe.proto中保存的有二進位大文件Blob的結構信息:BlobProto, BlobProtoVector, Datum

BlobProto:

1 // Specifies the shape (dimensions) of a Blob.
2 //用來表示圖片的shape
3 message BlobShape {
4 repeated int64 dim = 1 [packed = true];
5 }
6
7 message BlobProto {
8 optional BlobShape shape = 7; //現在用來替換下面的num,channels ,height,width,推薦使用
9 repeated float data = 5 [packed = true]; //forward計算數據
10 repeated float diff = 6 [packed = true]; //backward的殘差數據
11 repeated double double_data = 8 [packed = true]; //雙精度forward計算數據
12 repeated double double_diff = 9 [packed = true]; //雙精度backward的殘差數據
13
14 // 4D dimensions -- deprecated. Use "shape" instead.
15 optional int32 num = 1 [default = 0]; // 圖片個數or 維度
16 optional int32 channels = 2 [default = 0]; //通過比如rgb便是3維
17 optional int32 height = 3 [default = 0]; //圖片高度
18 optional int32 width = 4 [default = 0]; //圖片寬度
19 }

BlobProtoVector:BlobProto的動態數組

message BlobProtoVector {
repeated BlobProto blobs = 1;
}

message Datum {//lmdb中的數據格式
optional int32 channels = 1;//圖片通道數比如rgb的通道數為3
optional int32 height = 2;//圖片的高度
optional int32 width = 3;//圖片的寬度
// the actual image data, in bytes
optional bytes data = 4;//圖片的數據.比如rgb的三維數組格式
optional int32 label = 5;//這張圖片對應的標籤,或者這塊圖像對應的標籤[比如20個分類轉換成數字之後對應的[0~19]
// Optionally, the datum could also hold float data.
repeated float float_data = 6;//圖片的數據,有時候圖片在轉換過程中會變成浮點型
// If true data contains an encoded image that need to be decoded
optional bool encoded = 7 [default = false];//是否需要解碼
}

剩下的大部分都是Layer部分和Net部分和Solver部分的參數,關於Blob和Layer,Net還有Solver這四個部分的關係:

blob作為貫穿整個框架的數據單元,Sovler通過sovler.prototxt【我覺得我可能需要說明一下:.proto和.prototxt的區別吧,這兩個都是google protobuff的文件,.proto用來定義結構體參數,.prototxt用來相應的.proto中的結構體的初始化數據】配置初始化Net,然後Net通過調用trainval.prototxt這些參數,來調用對應的Layer,並將數據blob輸入到相應的Layer中,Layer來對流入的數據進行計算處理,然後再將計算後的blob數據返回,通過Net流向下一個Layer,每次執行一次,Solver就會計數一次,然後調整learn_rate,descay_weith等權值,參考的是caffe論壇上的這個話題:這裡附上鏈接

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 科技優家 的精彩文章:

EF之通過不同條件查找去重複
Github 開源:高效好用的對象間屬性拷貝工具:升訊威 Mapper
Java 動態載入Jar包,並使用
python每天一個小練習-強壯的密碼
C# ftp 圖片上傳多快好省

TAG:科技優家 |

您可能感興趣

阻止Facebook跟蹤數據的Firefox開源插件Facebook Container
Twitter 開源數據分析工具——tinfoleak
Veritas收購雲數據管理公司fluid Operations AG
數據挖掘平台Discover解鎖Tensorflow
Jupyter Notebook數據科學高效技巧!
Python modbus 浮點型數據處理
Women in Data Science Beijing:與數據科學的美妙邂逅
Salesforce數據現在可以導入到Google Analytics 360中了
uCloudlink 推出創新移動數據服務 「GlocalMe Inside」
Netty-整合Protobuf高性能數據傳輸
talmo-design-cables數據線品牌與包裝設計
如果Facebook告訴你 你的數據被Cambridge Analytica使用
互動式數據可視化處理工具Microsoft Power BI簡介
基於Markov Chain Monte Carlo的智能手錶睡眠數據分析
Young Academy:unleash young的群體智慧,解碼26個月長PFS數據背後的真相
Facebook 稱谷歌和 Twitter 也會收集數據
Section 14-Halcon實戰寶典之數據結構
Spring Boot與Kotlin使用Spring-data-jpa簡化數據訪問層
Plotly Dash數據可視化
TalkingData:曝光iPhoneX真實在用量數據