caffe2.proto文件的重要内容 参考博客 ``` syntax = "proto2";
package = caffe2; // message相当于class 内部的定义和C++/Java类似 // required 必须含这个值 // optional 表示该值可有0或1个,repeated 表示该属性是可重复(数组/vector) // 后面的 = num 表示该属性的标识 编码时[1,15]占1个字节,[16,2047]占2字节 enum DeviceType { CPU = 0; // In default, we will use CPU. CUDA = 1; // CUDA. MKLDNN = 2; // Reserved for explicit MKLDNN OPENGL = 3; // OpenGL OPENCL = 4; // OpenCL IDEEP = 5; // IDEEP. HIP = 6; // AMD HIP // Change the following number if you add more devices in the code. COMPILE_TIME_MAX_DEVICE_TYPES = 7; ONLY_FOR_TEST = 20901701; // This device type is only for test. } message DeviceOption { optional int32 device_type = 1 [ default = 0 ]; // 0 is CPU. optional int32 cuda_gpu_id = 2; optional uint32 random_seed = 3; optional string node_name = 4; optional int32 numa_node_id = 5 [default = -1]; repeated string extra_info = 6; optional int32 hip_gpu_id = 7; }
message OperatorDef { repeated string input = 1; // the name of the input blobs repeated string output = 2; // the name of output top blobs optional string name = 3; // the operator name. This is optional. optional string type = 4; repeated Argument arg = 5; optional DeviceOption device_option = 6; optional string engine = 7; repeated string control_input = 8; optional bool is_gradient_op = 9 [default = false]; optional string debug_info = 10; } // NetDef 是caffe2定义网络的完整类,一个参数文件就是一个NetDef对象序列化输出的结果 message NetDef { optional string name = 1; // the network's name // Operators that the network contains. repeated OperatorDef op = 2; // 可以从参数文件中查看该网络用到的所有Operator optional string type = 3; optional int32 num_workers = 4 [deprecated=true]; optional DeviceOption device_option = 5; //使用CPU device_type = 0 repeated Argument arg = 6; repeated string external_input = 7; repeated string external_output = 8; }
```参数文件有二进制和text两种,使用二进制的参数文件读写速度很快,二进制和text可以使用protobuf的接口进行转换.caffe2 model zool提供的是二进制的参数文件,将其转换为text可以看到网络的结构protobuf可以将proto语言定义的数据类型转换为代码.将protobuf安装包编译成可执行文件,以此进行转换.二进制文件的名字为protoc.exe转化为C++的指令: protoc --cpp_out=./ caffe2.proto 第二个参数:输出目录,第三个参数: 输入文件编译protobuf:在protobuf下载目录下的cmake文件夹内的README.md 详细描述了编译C++版本的方法,不同版本的protobuf有细微差别. 大致过程:打开Visual Studio 本机命令提示符cd cmakemkdir build & cd buildmkdir release (debug)cd releasecmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install ../.. 生成makefilenmake 生成可执行文件和lib文件nmake install 会在install文件夹内生成include , bin,lib文件夹详情看README.md至此,项目中必须的文件获得方法都描述完成了,接下来介绍项目的配置要点
使用VS新建一个控制台项目按照上述说明,将文件复制进项目内在VS解决方案中,选择显示所有文件,右键这些文件夹,选择 包括在项目中在项目属性页面,不管是配置release还是debug,32位或是64位,统一的配置是: C/C++目录->常规->附加包含目录 添加./;./include;链接器->常规->链接库依赖项 添加./lib;C/C++目录->常规->预处理器->预处理器定义 编辑,添加一行_CRT_SECURE_NO_WARNINGSC/C++目录->常规->预编译头->设为不适用预编译头debug版本和release版本的不同配置: C/C++ ->代码生成->运行库 release版本设为多线程(/MT),debug版本设为多线程调试(/MTD)注意链接器引入lib的版本,release版本链接release版本的lib库,如果引入错误会发生无法解析的字符或者运行时错误等.一定留意 链接器->输入->附加依赖项中的lib , 点击右面的下拉框->编辑查看其引用的所有lib文件先引入的lib文件会屏蔽后面同样作用的lib文件读取文本参数文件使用ReadProtoFromTextFile(string,NetDef*)
protobuf 提供的原始读取参数接口 :使用bool message.ParseFromIstream(istream* input)读取参数文件使用bool SerializeToOstream(ostream* output)const 写入文件流
转载于:https://www.cnblogs.com/star-and-me/p/10437056.html
相关资源:数据结构—成绩单生成器