caffe源码学习之Proto数据格式1

Posted Gxjun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 <string>
  8 
  9 #include <google/protobuf/stubs/common.h>
 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 <google/protobuf/generated_message_util.h>
 23 #include <google/protobuf/message.h>
 24 #include <google/protobuf/repeated_field.h>
 25 #include <google/protobuf/extension_set.h>
 26 #include <google/protobuf/unknown_field_set.h>
 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<const char*>(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 <algorithm>
  8 
  9 #include <google/protobuf/stubs/common.h>
 10 #include <google/protobuf/stubs/once.h>
 11 #include <google/protobuf/io/coded_stream.h>
 12 #include <google/protobuf/wire_format_lite_inl.h>
 13 #include <google/protobuf/descriptor.h>
 14 #include <google/protobuf/generated_message_reflection.h>
 15 #include <google/protobuf/reflection_ops.h>
 16 #include <google/protobuf/wire_format.h>
 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     "\\n\\013caffe.proto\\022\\005caffe\\"3\\n\\007student\\022\\013\\n\\003age\\030\\001"
 84     " \\002(\\005\\022\\014\\n\\004name\\030\\002 \\002(\\t\\022\\r\\n\\005grade\\030\\003 \\001(\\005", 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_caf

以上是关于caffe源码学习之Proto数据格式1的主要内容,如果未能解决你的问题,请参考以下文章

ImportError: No module named caffe.proto解决办法

caffe源码阅读

不同格式下计算图片的均值和caffe.proto

CP2003-Python做深度学习之Caffe设计实战

caffe日常学习之:编译examples中的cpp文件描述文件——makefile

深度学习之caffe1——软件配置与测试