zoukankan      html  css  js  c++  java
  • caffe中是如何运用protobuf构建神经网络的?

     caffe这个框架设计的比较小巧精妙,它采用了protobuf来作为交互的媒介,避免了繁重的去设计各个语言的接口,开发者可以使用任意语言通过这个protobuf这个媒介,来运行这个框架.

      我们这里不过多的去阐述caffe的过往以及未来,只是简单的描述一下,caffe框架中的protobuf的作用,以及它的背后原理. 一般来说cafe.proto中有对应的solve,solve中悠悠Layer,通过prototxt解析生成一个大对象sovle,然后solve底下有一个Layer数组对象,我们所定义的网络就是Layer数组,通过解析Layer数组,反射到对应layer对应的,遍历Layer数组的过程也就是勾结神经网络的过程,遍历完成之后,也就构成了一张神经网络图,然后就是执行这个图,也就是依据这个对象数组一步步的,喂数据,forward操作,和backward操作,计算loss,等. 我们可以这样类比,我们可以模仿这个原理简单的设计一个框架,这里先不考虑C++的反射机制问题,这里只讨论如何将prototxt文件解析出来,至于如何反射到实际的类上,下次有时间可以在记录一个备忘录.

      比如,我们设计一个这样的demo.proto 来定义我们的对象属性:

       

     1 name: "三年级23班"
     2 
     3 teacher {
     4   name: "tom"
     5   age: 17
     6   work {
     7     isworker: 1 ;#中文
     8     isjiaban: 1;
     9    }
    10 }
    11 
    12 stu {
    13     age: 19;
    14     name: "demo"; ##中文
    15     grade: 134;
    16 }
    17 
    18 stu {
    19     age: 19;
    20     name: "google"; ##中文
    21     grade: 134;
    22 }
    23 
    24 stu {
    25     age: 19;
    26     name: "snake"; ##中文
    27     grade: 134;
    28 };
    29 
    30 num:"127.0.0.1:1";
    31 num:"127.0.0.1:2";
    32 num:"127.0.0.1:3";
    33 num:"127.0.0.1:4";

    然后我们来依次解析出这个param.prototxt文件中的信息:

     1 //
     2 // Created by xijun1 on 2017/12/22.
     3 //
     4 #include <google/protobuf/io/coded_stream.h>
     5 #include <google/protobuf/io/zero_copy_stream_impl.h>
     6 #include <google/protobuf/text_format.h>
     7 
     8 //反射机制
     9 #include <google/protobuf/compiler/importer.h>
    10 #include <google/protobuf/dynamic_message.h>
    11 
    12 #include "proto/demo.pb.h"
    13 #include<iostream>
    14 #include <fstream>
    15 #include<ios>
    16 #include <cstdlib>
    17 #include <cstring>
    18 #include <cstdio>
    19 
    20 #include <fcntl.h>   // open
    21 using namespace std;
    22 
    23 void InfoStudents(const caffe::Student & stu){
    24     cout<< "student info:"<<endl;
    25     cout<<"     name: "<<stu.name()<<endl;
    26     cout<<"     age: "<<stu.age()<<endl;
    27     cout<<"     grade: "<<stu.grade()<<endl;
    28 }
    29 
    30 void InfoTeacher(const caffe::Teacher & teacher) {
    31     cout << "teacher info:" << endl;
    32     cout << "       name: " << teacher.name() << endl;
    33     cout << "       age: " << teacher.age() << endl;
    34     cout<< "            is worker: "<<teacher.work().isworker()<<endl;
    35     cout<< "            is jiaban: "<<teacher.work().isjiaban()<<endl;
    36 }
    37 
    38 
    39 int main(void)
    40 {
    41     caffe::Class cls;
    42     int file_desc = open("./param.prototxt",O_NDELAY);
    43 
    44     google::protobuf::io::FileInputStream fileInputStream(file_desc);
    45     if(!google::protobuf::TextFormat::Parse(&fileInputStream,&cls)){
    46         std::cout<<"parse failure."<<std::endl;
    47         return -1;
    48     }
    49     std::cout<<cls.name()<<std::endl;
    50 
    51 
    52     //按照索引进行读取
    53     for(int i=1;i<cls.GetMetadata().descriptor->field_count(); ++i){
    54         std::cout<<cls.descriptor()->field(i)->name()<<std::endl;
    55         //cout<<cls.descriptor()->field(i)->full_name()<<endl;
    56         if(cls.descriptor()->field(i)->name()=="stu"){
    57             for (auto &stu_info : cls.stu()){
    58 
    59                  InfoStudents(stu_info);
    60             }
    61         }
    62 
    63         if(cls.descriptor()->field(i)->name()=="teacher"){
    64             for (auto &teacher_info : cls.teacher()){
    65 
    66                 InfoTeacher(teacher_info);
    67             }
    68         }
    69     }
    70 
    71     return 0;
    72 }

    我们试着运行一下,会看到这个结果:

    这样之后是不是对caffe有了很直观的认识了呢.....

    详细的代码,我放到github上了,附上地址:

    https://github.com/gongxijun/protoc

    ----完----

  • 相关阅读:
    进程通信之信号通信
    分数化小数
    台湾大学公开课《概率》第五周一道不会作的作业题 ,一种看不懂的解法
    网络子系统53_ip协议分片重组_内存阈值
    Centos 6.3 Realtek Audio Driver Compile
    I.MX6 PHY fixup 调用流程 hacking
    I.MX6 AR8031 寄存器操作
    I.MX6 ethtool 移植
    I.MX6 U-Boot ping网络
    Android tcpdump 使用
  • 原文地址:https://www.cnblogs.com/gongxijun/p/8981086.html
Copyright © 2011-2022 走看看