Caffe Notes: Caffe.cpp

** If you find content helpful and want to quote this blog, please provide the link [ayst123] ( http://blog.csdn.net/ayst123/article/details/45965303 )


1-Google Flag

Document is How To Use Google Commandline Flags, where All materials discussed below come from.

Google Flags(gflags) is created to deal with commandline flags.

1-1. Define Flags.

#include <gflags/gflags.h>

   DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");
   DEFINE_string(languages, "english,french,german",
                 "comma-separated list of languages to offer in the 'lang' menu");

Explain:
Before defining gflags, include it at the top of file.

DEFINE_bool and DEFINE_string are flag types. Here are different types of flags.

DEFINE_bool: boolean
DEFINE_int32: 32-bit integer
DEFINE_int64: 64-bit integer
DEFINE_uint64: unsigned 64-bit integer
DEFINE_double: double
DEFINE_string: C++ string

DEFINE_xxx takes three arguments:
1. the name of the flag
2. its default value
3. ′help′ string that describe its use.

1-2. Accessing Flag

All defined flags are available with the prefix FLAGS_. In the example above, two variables, FLAGS_big_menu, and FLAGS_languages can be used.

1-3. Set up Flags

google::ParseCommandLineFlags(&argc, &argv, true);

2-Caffe. cpp

2-1. Part 1

#include "caffe/caffe.hpp"

#include <gflags/gflags.h> is included in caffe.hpp/common.hpp

DEFINE_int32(gpu, -1,
    "Run in GPU mode on given device ID.");
DEFINE_string(solver, "",
    "The solver definition protocol buffer text file.");
DEFINE_string(model, "",
    "The model definition protocol buffer text file..");
DEFINE_string(snapshot, "",
    "Optional; the snapshot solver state to resume training.");
DEFINE_string(weights, "",
    "Optional; the pretrained weights to initialize finetuning. "
    "Cannot be set simultaneously with snapshot.");
DEFINE_int32(iterations, 50,
    "The number of iterations to run.");

Initial all flags. (refer to How to define flags )

2-2. Part 2

main function:

int main(int argc, char** argv) {
  // Print output to stderr (while still logging).
  FLAGS_alsologtostderr = 1;
  // Usage message.
  gflags::SetUsageMessage("command line brew\n"
      "usage: caffe <command> <args>\n\n"
      "commands:\n"
      "  train           train or finetune a model\n"
      "  test            score a model\n"
      "  device_query    show GPU diagnostic information\n"
      "  time            benchmark model execution time");
  // Run tool or show usage.
  caffe::GlobalInit(&argc, &argv);
  if (argc == 2) {
    return GetBrewFunction(caffe::string(argv[1]))();
  } else {
    gflags::ShowUsageWithFlagsRestrict(argv[0], "tools/caffe");
  }
}

caffe.cpp is able to do four different task: train, test, device_query and time.

train: train or finetune a model\n”
test: score a model\n”
device_query: show GPU diagnostic information\n”
time: benchmark model execution time”);

caffe::GlobalInit(&argc, &argv); includes gflags parse sentence:

google::ParseCommandLineFlags(&argc, &argv, true);

So far, the corresponding flags are set up from command line.

It will call different functions by GetBrewFunction(). Here are four main functions: train(), test(), device_query() and time().


BrewFunction

typedef int (*BrewFunction)();
typedef std::map<caffe::string, BrewFunction> BrewMap;
BrewMap g_brew_map;

The first line is to define BrewFunction as a function pointer with inputting no argument and returning int.

The second typedef is to create BrewMap as a map to store the flag and its corresponding function.

But How to initial BrewMap ?
see this block:

#define RegisterBrewFunction(func) \
namespace { \
class __Registerer_##func { \
 public: /* NOLINT */ \
  __Registerer_##func() { \
    g_brew_map[#func] = &func; \
  } \
}; \
__Registerer_##func g_registerer_##func; \
}

It uses DEFINE to create a class, then use its public constructor function to initial BrewMap.

#: the parameter is replaced by a string literal

    Eg. #define str(x) #x
    cout << str(test);

    it would be cout<<’test’;

##: concatenates two arguments.

It is very important to make constructor function public, otherwise we can call it.

Actually, we make a observation that RegisterBrewFunction comes after each main function (eg, train, test). The Class is initialized with running its constructor before going into main function.

After running GetBrewFunction(), it returns &func. In main function, it executes func().



** If you find content helpful and want to quote this blog, please provide the link [ayst123] ( http://blog.csdn.net/ayst123/article/details/45965303 )

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值