** If you find content helpful and want to quote this blog, please provide the link [ayst123] ( http://blog.csdn.net/ayst123/article/details/45965303 )
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.
#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.
All defined flags are available with the prefix FLAGS_. In the example above, two variables, FLAGS_big_menu, and FLAGS_languages can be used.
google::ParseCommandLineFlags(&argc, &argv, true);
#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 )
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().
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 )