Caffe Code Guide (1): protobuf Example _ parameter Pass

Source: Internet
Author: User
Tags unique id

PROTOBUF is a protocol interface which can realize the exchange between memory and external storage. This is the open source tool that is developed by Gu GE, use when researching Caffe source code now.

A software project = data structure + algorithm + parameter, for data structure and algorithm we have been more research, but different developers on the parameter management has its own advantages. Some people like the TXT format of the parameter file, someone like bin simple and efficient, but also some people like the intuitive graphical interface. Inconsistent parameter management brings many problems, such as a team of different members must agree on a set of unified parameter scheme, or communication protocol, so that module integration. and the Protobuf tool to solve this problem perfectly, the key part of the code automatically generated, saving a lot of development, debugging time.


First download protobuf, address (not open.) ...... No explanation)

Linux version 2.5.0 here

Extract:

Tar zxvf protobuf-2.5.0.tar.gz

Cut to home directory:

CD protobuf-2.5.0

Compile:

./configure

Make

sudo make install


To add an environment variable:

Export pkg_config_path=$ (PWD)


Compile Examples:

CD examples/

Make CPP

Here we only compile C + + code.


Compilation completed, the following executable file was generated:

Add_person_cpp

List_people_cpp

This is an example of an address book. We first run Add_person_cpp:

./add_person_cpp Zyk
zyk:file not found.  Creating a new file.
Enter Person ID number:123
Enter Name:zhaoyongke
Enter email address (blank for none): Zhaoyongke@yeah.net
E  Nter a phone number (or leave blank to finish): 188188188
are this a mobile, home, or work phone? (carriage return)
Unknown phone Type.  Using default.
Enter a phone number (or leave blank to finish): (Enter)

Then run List_people_cpp:

./list_people_cpp Zyk Person
id:123
  name:zhaoyongke
  e-mail address:zhaoyongke@yeah.net
  Home Phone #: 188188188

It can be seen that we have generated a new address book Zyk, which holds the corresponding information.


The example runs over, and we see how the code is generated.

Before using Protobuf, write the proto file, which is the data structure that describes the parameters we need to configure. The Proto in this example is as follows:

README.txt for information and build instructions.

Package tutorial;

Option Java_package = "com.example.tutorial";
Option Java_outer_classname = "Addressbookprotos";

Message person {
  Required String name = 1;
  Required Int32 id = 2;        Unique ID number for this person.
  Optional String email = 3;

  Enum Phonetype {
    MOBILE = 0;
    home = 1;
    WORK = 2;
  }

  Message PhoneNumber {
    Required String number = 1;
    Optional Phonetype type = 2 [default = home];
  }

  Repeated PhoneNumber phone = 4;
}

Our Address Book file is just one of these.
Message AddressBook {
  repeated person person = 1;
}

The first few lines define the package and can be ignored.

Message person{...} Defines a parameter structure that needs to be transmitted, which can be seen including several units: name (string type), ID (int32 type), email (string type), phone (phonenumber type, class nested within person). Marked "required" must have a value, and "optional" is optional, and "repeated" means a set of vectors with the same type as the back cell.


With the above definition, we can use the Protobuf tool to generate the interface code, which commands the following:

Protoc--cpp_out=.  Addressbook.proto

Two files were generated after running: addressbook.pb.cc and Addressbook.pb.h, the code is longer than the paste. Our application can implement serialization/deserialization of parameters through an automatically generated interface, as follows:

Add_person.c #include <iostream> #include <fstream> #include <string> #include "addressbook.pb.h"

using namespace Std;
This function fills in a person message based on user input.
  void promptforaddress (Tutorial::P erson* person) {cout << ' Enter person ID number: ';
  int id;
  CIN >> ID;
  person->set_id (ID);

  Cin.ignore (256, ' \ n ');
  cout << "Enter name:";

  Getline (CIN, *person->mutable_name ());
  cout << "Enter email address (blank for none):";
  string email;
  Getline (cin, email);
  if (!email.empty ()) {person->set_email (email);
    } while (true) {cout << Enter a phone number (or leave blank to finish): ";
    string number;
    Getline (cin, number);
    if (Number.empty ()) {break;
    Tutorial::P erson::P honenumber* phone_number = Person->add_phone ();

    Phone_number->set_number (number);
    cout << "Is this a mobile, home, or work phone?";
    String type; Getline (cin, type);
    if (type = = "mobile") {Phone_number->set_type (tutorial::P erson::mobile);
    else if (type = = "Home") {Phone_number->set_type (tutorial::P erson::home);
    else if (type = = "Work") {Phone_number->set_type (tutorial::P erson::work);  else {cout << "Unknown phone type.
    Using default. "<< Endl; }}//Main function:  reads The entire Address book from a file,//  adds one person based on user input,
Then writes it same//  file. int main (int argc, char* argv[]) { //Verify that version of the library that we linked against IS  //CO
Mpatible with the version of the headers we compiled against.


  Google_protobuf_verify_version;   if (argc!= 2) {    cerr << "Usage:  " << argv[0] << "Address_book_file" <&lt ;
Endl
    return-1;


 }   Tutorial::addressbook Address_book;   {    Read the existing address book.
    FStream input (argv[1], ios::in | ios::binary);     if (!input) {      cout << argv[1] << ": File not found.
 creating a new file. "<< Endl;    } else if (!address_book. Parsefromistream (&input)) {      Cerr << "Failed to parse Address Book." << Endl;   & nbsp
  Return-1;
   }  }  //ADD an address.


  Promptforaddress (Address_book.add_person ());   {   //Write The new address book back to disk.     fstream output (argv[1], ios::out | ios::t Runc |
Ios::binary);     if (!address_book. Serializetoostream (&output)) {      Cerr << "Failed to write Address Book." << Endl; &nbsp ;
    return-1;
   }  }  //Optional:  delete All Global objects allocated by LIBPROTOBUF.   Google::p Rotobuf::shutdownprotobuFlibrary ();
  return 0; }

You can see that only the Tutorial::addressbook class, the interface in the Person class (Add_person (), Add_phone (), Set_number (), Set_email () (), The Addressbook.pb.h () () () () () ( And so on) can manipulate the corresponding parameters, and finally serializing the parameter in memory to a file requires only the execution of Serializetoostream (). The corresponding read parameter file operation is Parsefromistream (). The second program in this example is as follows:

list_people.c #include <iostream> #include <fstream> #include <string> #include "addressbook.pb.h"

using namespace Std;
Iterates though all people in the AddressBook and prints info about them.  void Listpeople (const tutorial::addressbook& Address_book) {for (int i = 0; i < address_book.person_size (); i++)

    {Const Tutorial::P erson& person = Address_book.person (i);
    cout << "person ID:" << person.id () << Endl;
    cout << "Name:" << person.name () << Endl;
    if (Person.has_email ()) {cout << "e-mail address:" << person.email () << Endl; for (int j = 0; J < Person.phone_size (); j +) {Const Tutorial::P erson::P honenumber& phone_number = PE

      Rson.phone (j);
          Switch (Phone_number.type ()) {case Tutorial::P erson::mobile:cout << "MOBILE phone #:";
        Break Case Tutorial::P erson::home:cout << " Home phone #: ";
        Break
          Case Tutorial::P erson::work:cout << "WORK phone #:";
      Break
    } cout << Phone_number.number () << Endl;
}}//Main function:reads the entire address book from a file and prints all//the information inside. int main (int argc, char* argv[]) {//Verify that version of the library this we linked against IS//compatible W
  ith the version of the headers we compiled against.

  Google_protobuf_verify_version;
    if (argc!= 2) {cerr << "Usage:" << argv[0] << "Address_book_file" << Endl;
  return-1;

  } Tutorial::addressbook Address_book;
    {//Read the existing address book.
    FStream input (argv[1], ios::in | ios::binary); if (!address_book.
      Parsefromistream (&input)) {cerr << "Failed to parse Address Book." << Endl;
    return-1;

  } listpeople (Address_book); Optional:delete All GlobalObjects allocated by LIBPROTOBUF.

  Google::p rotobuf::shutdownprotobuflibrary ();
return 0;
 }

I believe that after this experiment, you will no longer be Caffe code in the initialization of parameters, parameter save operation is unfamiliar, everything is natural.

In addition to the simple features described above, PROTOBUF can also be used to pass parameters between different languages (C + + and Java, Python), eliminating the tedious task of manually maintaining data structures. You can also support client/server mode, passing parameters between hosts/from machines.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.