caffe学习笔记1
博客
http://blog.csdn.net/seven_first/article/details/47378697
https://zhuanlan.zhihu.com/p/25127756?refer=xiaoleimlnote
https://github.com/BUPTLdy/Caffe_Code_Analysis/tree/master/
学习计划
Caffe运行主要流程
caffe.cpp
// A simple registry for caffe commands.
typedef int (*BrewFunction)();
typedef std::map<caffe::string, BrewFunction> BrewMap;
BrewMap g_brew_map;
#define RegisterBrewFunction(func) \
namespace { \
class __Registerer_##func { \
public: /* NOLINT */ \
__Registerer_##func() { \
g_brew_map[#func] = &func; \
} \
}; \
__Registerer_##func g_registerer_##func; \
}
- typedef int (*BrewFunction)()
函数指针:可用于处理一些参数和返回值一致,但是功能不一样一组函数。
本示例:定义了一组函数指针,函数参数为(),但是返回值为int这样一组函数指针族。 - typedef std::map<caffe::string, BrewFunction> BrewMap
定义BrewMap为一个map,key为string,value为函数指针(参数为(),返回为int)。 - #define RegisterBrewFunction(func)
该宏命令完成一次注册的初始化操作; 其中在宏中#和##符号表示的意思如下: #:用来把参数转换成字符串;
##:用来连接前后两个参数。
以caffe中RegisterBrewFunction(train)为例,上面代码可转换为:
#define RegisterBrewFunction(train) \
namespace { \
class __Registerer_train { \
public: /* NOLINT */ \
__Registerer_train() { \
g_brew_map["train"] = &train; \
} \
}; \
__Registerer_train g_registerer_train; \
} //完成了全局变量的初始化操作
接来下看caffe.cpp里的main函数代码
int main(int argc, char** argv) {
// Print output to stderr (while still logging).
FLAGS_alsologtostderr = 1;
// Set version
gflags::SetVersionString(AS_STRING(CAFFE_VERSION));
// 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) {
#ifdef WITH_PYTHON_LAYER
try {
#endif
return GetBrewFunction(caffe::string(argv[1]))();
#ifdef WITH_PYTHON_LAYER
} catch (bp::error_already_set) {
PyErr_Print();
return 1;
}
#endif
} else {
gflags::ShowUsageWithFlagsRestrict(argv[0], "tools/caffe");
}
}
caffe控制台训练示例:caffe train --solver==*.prototxt
输入完全正确的情况下,函数首先进入:
- return GetBrewFunction(caffe::string(argv[1]))();
static BrewFunction GetBrewFunction(const caffe::string& name) {
if (g_brew_map.count(name)) {
return g_brew_map[name];
} else {
LOG(ERROR) << "Available caffe actions:";
for (BrewMap::iterator it = g_brew_map.begin();
it != g_brew_map.end(); ++it) {
LOG(ERROR) << "\t" << it->first;
}
LOG(FATAL) << "Unknown action: " << name;
return NULL; // not reachable, just to suppress old compiler warnings.
}
}
总结:
- C++函数指针的应用;函数指针可用于解决 函数参数和返回值一致,但功能不一样的一群函数族
- 进入主函数前,通过全局变量注册函数的方法;
举例:
#include <iostream>
#include <map>
typedef int (*mathfunc)(int,int);
typedef std::map<std::string, mathfunc> BrepMap;
BrepMap g_brew_map;
#define RegisterMathFunc(func) \
namespace { \
class __Register_##func{ \
public: \
__Register_##func() { g_brew_map[#func] = &func;}};\
__Register_##func g_register_##func;}
int add(int a, int b){
return a+b;
}
RegisterMathFunc(add)
int sub(int a, int b){
return a-b;
}
RegisterMathFunc(sub)
int mul(int a, int b){
return a*b;
}
RegisterMathFunc(mul)
int main()
{
std::cout<< "number of register functions: " << g_brew_map.size() << std::endl;
std::string math_method = "add";
std::cout << "23 + 12 = " << g_brew_map[math_method](23,12) << std::endl;
return 1;
}
/*
*******result*******
number of register functions: 3
23 + 12 = 35
*/
caffe学习笔记1的更多相关文章
- Caffe学习笔记2--Ubuntu 14.04 64bit 安装Caffe(GPU版本)
0.检查配置 1. VMWare上运行的Ubuntu,并不能支持真实的GPU(除了特定版本的VMWare和特定的GPU,要求条件严格,所以我在VMWare上搭建好了Caffe环境后,又重新在Windo ...
- Caffe学习笔记(三):Caffe数据是如何输入和输出的?
Caffe学习笔记(三):Caffe数据是如何输入和输出的? Caffe中的数据流以Blobs进行传输,在<Caffe学习笔记(一):Caffe架构及其模型解析>中已经对Blobs进行了简 ...
- Caffe学习笔记(二):Caffe前传与反传、损失函数、调优
Caffe学习笔记(二):Caffe前传与反传.损失函数.调优 在caffe框架中,前传/反传(forward and backward)是一个网络中最重要的计算过程:损失函数(loss)是学习的驱动 ...
- Caffe学习笔记(一):Caffe架构及其模型解析
Caffe学习笔记(一):Caffe架构及其模型解析 写在前面:关于caffe平台如何快速搭建以及如何在caffe上进行训练与预测,请参见前面的文章<caffe平台快速搭建:caffe+wind ...
- Caffe学习笔记4图像特征进行可视化
Caffe学习笔记4图像特征进行可视化 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit201 ...
- Caffe学习笔记3
Caffe学习笔记3 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和h ...
- Caffe 学习笔记1
Caffe 学习笔记1 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和 ...
- Caffe学习笔记2
Caffe学习笔记2-用一个预训练模型提取特征 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hi ...
- CAFFE学习笔记(五)用caffe跑自己的jpg数据
1 收集自己的数据 1-1 我的训练集与测试集的来源:表情包 由于网上一幅一幅图片下载非常麻烦,所以我干脆下载了两个eif表情包.同一个表情包里的图像都有很强的相似性,因此可以当成一类图像来使用.下载 ...
- CAFFE学习笔记(四)将自己的jpg数据转成lmdb格式
1 引言 1-1 以example_mnist为例,如何加载属于自己的测试集? 首先抛出一个问题:在example_mnist这个例子中,测试集是人家给好了的.那么如果我们想自己试着手写几个数字然后验 ...
随机推荐
- 最新版的Chrome如何设置网页编码
在最新的V55版本中已经没有了编码选项,没有了这个可能会导致一些特殊编码网页出现乱码问题.那么如何找回这个Chrome的编码功能?可以通过下载chrome扩展:Set Character Encodi ...
- PIL库的总结及运用
PIL库的总结:(以代码形式) #date: 2018/11/15 from PIL import Image,ImageFilter,ImageDraw,ImageFont #####除了缩略图的方 ...
- C51单片机_day_01(定时器和中断系统)
c51单片机 51单片机是控制电路系统的开关,当然芯片就是51芯片,现在随着科技的发展,也是出了很多,功能更多,更全的芯片. 51是用c语言做为程序编程的语言 ——我对基本基础 ...
- 使用dotenv 管理nodejs 应用的环境变量&&docker-compose 运行
说明dotenv 是一个很方便的符合12 factor 的环境变量管理工具,使用很方便,实际上里面的代码也不是很多 测试使用docker 进行环境部署,为了方便分发使用pkg 进行打包,使用alp ...
- orientdb docker-compose 运行
orientdb 很早就跑过,但是现在在跑,发现配置有些变动,原有studio 直接就可以访问的,新版本的居然还需要自己添加 server 的配置,所以为了方便使用docker-compose 运行, ...
- linux下软件安装的几种方式
linux下软件安装的几种方式(主要有源码安装, rpm安装, yum安装). 一:源码安装 几乎所有的开源软件都支持在Linux下运行,而这些软件一般都以源码形式发放,只需要Linux安装了gcc. ...
- docker nginx letsencrypt
https越来越流行了,但免费的证书一般是一年有效期.一般是够用了,但懒人都想一劳永逸, 有个免费证书颁发机构是letsencrypt.它是开源,并且完全免费的,它颁发的证书已经被几乎所有的浏览器所认 ...
- 46 Simple Python Exercises (前20道题)
46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises construct ...
- nginx配置备份
server { listen 80; server_name localhost; set $expires_duration "30d"; if ($uri ~* \.html ...
- DDS生成正弦波
DDS生成正弦波 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////// ...