From eada9dbdb5084fdcfb89b9da4bbcf825c1a84477 Mon Sep 17 00:00:00 2001 From: Kai Li Date: Thu, 3 Jul 2014 20:15:53 +0800 Subject: [PATCH] Separate layers relatively independent of images out of vision_layers --- include/caffe/common_layers.hpp | 182 ++++++++++++++++++++++++++++++++++++++++ include/caffe/vision_layers.hpp | 165 +----------------------------------- 2 files changed, 185 insertions(+), 162 deletions(-) create mode 100644 include/caffe/common_layers.hpp diff --git a/include/caffe/common_layers.hpp b/include/caffe/common_layers.hpp new file mode 100644 index 0000000..761c19f --- /dev/null +++ b/include/caffe/common_layers.hpp @@ -0,0 +1,182 @@ +// Copyright 2014 BVLC and contributors. + +#ifndef CAFFE_COMMON_LAYERS_HPP_ +#define CAFFE_COMMON_LAYERS_HPP_ + +#include +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/layer.hpp" +#include "caffe/neuron_layers.hpp" +#include "caffe/loss_layers.hpp" +#include "caffe/data_layers.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/* ArgmaxLayer + Compute the index of the max value across all (channels x height x width). + [In the future, can take specific dimension.] + Intended for use after a classification layer to produce prediction. + If parameter out_max_val is set to true, then output is a vector of pairs + (max_ind, max_val) for each image. + + NOTE: does not implement Backwards operation. +*/ +template +class ArgMaxLayer : public Layer { + public: + explicit ArgMaxLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_ARGMAX; + } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom) { + NOT_IMPLEMENTED; + } + bool out_max_val_; +}; + +/* ConcatLayer + Takes at least two blobs and concatenates them along either num or + channel dim, outputting the result. +*/ +template +class ConcatLayer : public Layer { + public: + explicit ConcatLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_CONCAT; + } + virtual inline int MinBottomBlobs() const { return 2; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + + Blob col_bob_; + int count_; + int num_; + int channels_; + int height_; + int width_; + int concat_dim_; +}; + +/* FlattenLayer +*/ +template +class FlattenLayer : public Layer { + public: + explicit FlattenLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_FLATTEN; + } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + + int count_; +}; + +/* SoftmaxLayer +*/ +template +class SoftmaxLayer : public Layer { + public: + explicit SoftmaxLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_SOFTMAX; + } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + + // sum_multiplier is just used to carry out sum using blas + Blob sum_multiplier_; + // scale is an intermediate blob to hold temporary results. + Blob scale_; +}; + +/* SplitLayer +*/ +template +class SplitLayer : public Layer { + public: + explicit SplitLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_SPLIT; + } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int MinTopBlobs() const { return 1; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + + int count_; +}; + +} // namespace caffe + +#endif // CAFFE_COMMON_LAYERS_HPP_ diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index b68dcbf..ef04772 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -9,84 +9,15 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" +#include "caffe/common_layers.hpp" +#include "caffe/data_layers.hpp" #include "caffe/layer.hpp" -#include "caffe/neuron_layers.hpp" #include "caffe/loss_layers.hpp" -#include "caffe/data_layers.hpp" +#include "caffe/neuron_layers.hpp" #include "caffe/proto/caffe.pb.h" namespace caffe { -/* ArgmaxLayer - Compute the index of the max value across all (channels x height x width). - [In the future, can take specific dimension.] - Intended for use after a classification layer to produce prediction. - If parameter out_max_val is set to true, then output is a vector of pairs - (max_ind, max_val) for each image. - - NOTE: does not implement Backwards operation. -*/ -template -class ArgMaxLayer : public Layer { - public: - explicit ArgMaxLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_ARGMAX; - } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom) { - NOT_IMPLEMENTED; - } - bool out_max_val_; -}; - -/* ConcatLayer - Takes at least two blobs and concatenates them along either num or - channel dim, outputting the result. -*/ -template -class ConcatLayer : public Layer { - public: - explicit ConcatLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_CONCAT; - } - virtual inline int MinBottomBlobs() const { return 2; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - - Blob col_bob_; - int count_; - int num_; - int channels_; - int height_; - int width_; - int concat_dim_; -}; - /* ConvolutionLayer */ template @@ -161,35 +92,6 @@ class EltwiseLayer : public Layer { vector coeffs_; }; -/* FlattenLayer -*/ -template -class FlattenLayer : public Layer { - public: - explicit FlattenLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_FLATTEN; - } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - - int count_; -}; - /* Im2colLayer */ template @@ -373,67 +275,6 @@ class PoolingLayer : public Layer { shared_ptr > max_idx_; }; -/* SoftmaxLayer -*/ -template -class SoftmaxLayer : public Layer { - public: - explicit SoftmaxLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_SOFTMAX; - } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - - // sum_multiplier is just used to carry out sum using blas - Blob sum_multiplier_; - // scale is an intermediate blob to hold temporary results. - Blob scale_; -}; - -/* SplitLayer -*/ -template -class SplitLayer : public Layer { - public: - explicit SplitLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_SPLIT; - } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int MinTopBlobs() const { return 1; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - - int count_; -}; - } // namespace caffe #endif // CAFFE_VISION_LAYERS_HPP_ -- 2.7.4