caffe 重新用Python编译tripletloss、fine turning

Caffe通过Boost中的Boost.Python模块来支持使用Python定义Layer:

  • 使用C++增加新的Layer繁琐、耗时而且很容易出错
  • 开发速度与执行速度之间的trade-off

编译支持Python Layer的Caffe

进入caffe 的root路径

1.修改Caffe根目录下的Makefile.cinfig

uncomment

WITH_PYTHON_LAYER:=1

2.编译

如果没有编译过:

mkdir build
cd build
cmake ..
make all & make pycaffe

如果已经编译过:

make clean
mkdir build
cd build
cmake ..
make all & make pycaffe

例如现在需要编译以下的tripletloss:

#add activation layer may increase the feature's expression
layer {
  name: "triplet_select"
  type: "Python"
  bottom: "fc9_1"
  bottom: "labels"
  top: "archor"
  top: "positive"
  top: "negative"
  python_param {
    module: "tripletselectlayer"
    layer: "TripletSelectLayer"
  }
}
layer {
  name: "tripletloss"
  type: "Python"
  bottom: "archor"
  bottom: "positive"
  bottom: "negative"
  top: "loss"
  python_param {
    module: "tripletlosslayer"
    layer: "TripletLayer"
    param_str: "'margin': 0.2"
  }
  loss_weight: 1
}

需要将tripletloss 对应的py文件(https://github.com/lemonaha/tripletloss/tree/master/tripletloss)放到 $PYTHONPATH 对应的目录下

原理浅析

阅读caffe源码pythonlayer.hpp可以知道,类PythonLayer继承自Layer,并且新增私有变量boost::python::object self来表示我们自己定义的python layer的内存对象。
类PythonLayer类的成员函数LayerSetUP, Reshape, Forward_cpu和Backward_cpu分别是对我们自己定义的python layer中成员函数setup, reshape, forward和backward的封装调用。

参考链接:http://withwsf.github.io/2016/04/14/Caffe-with-Python-Layer/
进一步了解使用C++创建新的layer:
https://chrischoy.github.io/research/making-caffe-layer/

tripletloss 原理以及梯度推导

triplet是一个三元组,这个三元组是这样构成的:从训练数据集中随机选一个样本,该样本称为Anchor,然后再随机选取一个和Anchor (记为x_a)属于同一类的样本和不同类的样本,这两个样本对应的称为Positive (记为x_p)和Negative (记为x_n),由此构成一个(Anchor,Positive,Negative)三元组。
具体推导过程:http://www.voidcn.com/blog/tangwei2014/article/p-4415770.html

caffe中增加layer 的c++实现:
http://www.voidcn.com/blog/mao_kun/article/p-6246924.html

fine turning

http://bealin.github.io/2016/10/23/Caffe%E5%AD%A6%E4%B9%A0%E7%B3%BB%E5%88%97%E2%80%94%E2%80%943Fine-tuning%E5%BE%AE%E8%B0%83%E7%BD%91%E7%BB%9C/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值