Caffe使用点滴记录

一、caffe 创建python 层

因为caffe底层是用c++编写的,所以我们有的时候想要添加某一个最新文献出来的新算法,正常的方法是直接编写c++网络层,然而这个有个前提条件是必须对caffe的底层非常熟悉,c++的编写达到一定的境界,才可灵活应用caffe,搞深度学习,走这条路对于菜鸟来说无疑很有难度。

好在caffe为我们提供了一个可以用python编写新的网络层的方法,直接用python语言编写新的网络层,然后在caffe的网络配置文件,稍作修改,就可以轻松容易创建新的网络层。其具体环境配置搭建过程如下:

1、编译caffe的时候,不能直接使用make pycaffe,而是应该使用:


WITH_PYTHON_LAYER=1 make && make pycaffe2、编写相关的网络层文件python源码filename.py,同时需要在filename.py 编写的时候,import caffe的时候,需要指向caffe所在的路径目录

3、把python源码文件filename.py拷贝到caffe/python目录下:

caffe/python/filename.py4、添加路径变量:

sudo vim  ~/.bashrc
在最后输入

export PYTHONPATH=/home/hjimce/tools/caffenewversion/python:$PYTHONPATH
重启电脑.

5、调用的时候,引用层:

layer {
  type: 'Python'
  name: 'loss'
  top: 'loss'
  bottom: 'f2'
  bottom: 'label'
  python_param {
    # the module name -- usually the filename -- that needs to be in $PYTHONPATH
    module: 'filename'
    # the layer name -- the class name in the module
    layer: 'NormlizedMSE'
  }
  # set loss weight so Caffe knows this is a loss layer.
  # since PythonLayer inherits directly from Layer, this isn't automatically
  # known to Caffe
  loss_weight: 1
}

二、caffe关闭日志输出消息

可能对于算法实验阶段来说,我们需要caffe运行网络的时候,打印网络的相关参数消息。然而对于已经走向工程之路的caffe模型来说,我们有可能需要屏蔽那些没用的输出消息,因此这个时候就需要用到caffe的消息屏蔽。

1、在程序最开始运行地方,加入如下代码:

::google::InitGoogleLogging("hjimce");    //初始化 
FLAGS_stderrthreshold = ::google::ERROR;  //只打印ERROR级别信息
2、在程序结束的时候加入代码:

google::ShutdownGoogleLogging();

猜你喜欢

转载自www.linuxidc.com/Linux/2016-09/135529.htm