caffe相关--Layers

http://caffe.berkeleyvision.org/tutorial/layers.html

Google Protocol Buffers 简介(一):http://www.jianshu.com/p/7de98349cadd
Data Layers
Data enters Caffe through data layers: they lie at the bottom of nets.
Note that the Python Layer can be useful for create custom data layers.

import caffe
import numpy as np


class EuclideanLossLayer(caffe.Layer):
    """
    Compute the Euclidean Loss in the same manner as the C++ EuclideanLossLayer
    to demonstrate the class interface for developing layers in Python.
    """

    def setup(self, bottom, top):
        # check input pair
        if len(bottom) != 2:
            raise Exception("Need two inputs to compute distance.")

    def reshape(self, bottom, top):
        # check input dimensions match
        if bottom[0].count != bottom[1].count:
            raise Exception("Inputs must have the same dimension.")
        # difference is shape of inputs
        self.diff = np.zeros_like(bottom[0].data, dtype=np.float32)
        # loss output is scalar
        top[0].reshape(1)

    def forward(self, bottom, top):
        self.diff[...] = bottom[0].data - bottom[1].data
        top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

    def backward(self, top, propagate_down, bottom):
        for i in range(2):
            if not propagate_down[i]:
                continue
            if i == 0:
                sign = 1
            else:
                sign = -1
            bottom[i].diff[...] = sign * self.diff / bottom[i].num

propagate_down: const vector< bool > &
a vector with equal length to bottom, with each index indicating whether to propagate the error gradients down to the bottom blob at the corresponding index

http://www.jianshu.com/p/622b8bb24589

reshape函数中里面必须把top的shape给定下来,比如说,这是一个loss layer,那么这个层的输出就是一个loss值,所以这个top的shape就是1*1,所以就这样写:

top[0].reshape(1,1)

因为这里比较简单,就不啰嗦了,只是比较奇怪的是,为什么反馈的时候会对标签y做偏导计算呢?好奇怪!!当然如果不要将y理解成标签,将这种欧氏loss理解层和上面介绍的loss同样的效果,那么对y求偏导也是可以理解的。

Vision Layers
Vision layers usually take images as input and produce other images as output, although they can take data of other types and dimensions.

Convolution Layer
Input:
n * c_i * h_i * w_i
Output:
n * c_o * h_o * w_o, where h_o = (h_i + 2 * pad_h - kernel_h) / stride_h + 1 and w_o likewise.

学习率 权值衰减相关
http://yufeigan.github.io/2014/11/29/Deep-Learning-%E4%BC%98%E5%8C%96%E6%96%B9%E6%B3%95%E6%80%BB%E7%BB%93/

权值初始化:http://www.jianshu.com/p/03009cfdf733

CPU implementation: ./src/caffe/layers/conv_layer.cpp:
https://github.com/BVLC/caffe/blob/master/src/caffe/layers/conv_layer.cpp#L61

this指针:
this 是 C++ 中的一个关键字,也是一个 const 指针,它指向当前对象,通过它可以访问当前对象的所有成员。

所谓当前对象,是指正在使用的对象。例如对于stu.show();,stu 就是当前对象,this 就指向 stu。

Blob< int > kernel_shape_
The spatial dimensions of a filter kernel.

http://caffe.berkeleyvision.org/doxygen/classcaffe_1_1BaseConvolutionLayer.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装依赖 首先需要安装 Caffe 和一些相关的依赖项: sudo apt-get update sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install --no-install-recommends libboost-all-dev sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev 2. 下载源码 下载 Caffe 的源码,建议使用 GitHub 上的官方仓库: git clone https://github.com/weiliu89/caffe.git 3. 编译 Caffe 进入 Caffe 的根目录,然后执行以下命令进行编译: cd caffe cp Makefile.config.example Makefile.config make all -j8 make test -j8 make runtest -j8 4. 下载预训练模型 下载 SSD 的预训练模型,可以从以下链接中选择: https://github.com/weiliu89/caffe/tree/ssd#models 将下载好的模型放到 Caffe 的根目录下的 models 目录中。 5. 测试 SSD 在 Caffe 的根目录下执行以下命令,测试 SSD 的效果: ./build/tools/caffe test \ --model=models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt \ --weights=models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel \ --iterations=5000 \ --gpu=0 6. 使用 SSD 如果已经成功测试了 SSD,就可以在自己的代码中使用它了。需要引入以下头文件: #include "caffe/caffe.hpp" #include "caffe/util/db.hpp" #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/net.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/layers/input_layer.hpp" #include "caffe/layers/conv_layer.hpp" #include "caffe/layers/pooling_layer.hpp" #include "caffe/layers/softmax_layer.hpp" #include "caffe/layers/relu_layer.hpp" #include "caffe/layers/detection_output_layer.hpp" 然后可以通过以下方式加载模型: caffe::Net<float> net("models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt", caffe::TEST); net.CopyTrainedLayersFrom("models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel"); 最后就可以使用 SSD 进行目标检测了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值