diff --git a/Makefile b/Makefile index 3dc76ae5697..985fffd6c0b 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,7 @@ NONEMPTY_LINT_REPORT := $(BUILD_DIR)/$(LINT_EXT) # PY$(PROJECT)_SRC is the python wrapper for $(PROJECT) PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp PY$(PROJECT)_SO := python/$(PROJECT)/_$(PROJECT).so -PY$(PROJECT)_HXX := include/$(PROJECT)/python_layer.hpp +PY$(PROJECT)_HXX := include/$(PROJECT)/layers/python_layer.hpp # MAT$(PROJECT)_SRC is the mex entrance point of matlab package for $(PROJECT) MAT$(PROJECT)_SRC := matlab/+$(PROJECT)/private/$(PROJECT)_.cpp ifneq ($(MATLAB_DIR),) diff --git a/include/caffe/caffe.hpp b/include/caffe/caffe.hpp index a339efba5c0..06882096c55 100644 --- a/include/caffe/caffe.hpp +++ b/include/caffe/caffe.hpp @@ -17,6 +17,5 @@ #include "caffe/util/benchmark.hpp" #include "caffe/util/io.hpp" #include "caffe/util/upgrade_proto.hpp" -#include "caffe/vision_layers.hpp" #endif // CAFFE_CAFFE_HPP_ diff --git a/include/caffe/common_layers.hpp b/include/caffe/common_layers.hpp deleted file mode 100644 index d42d15c47e3..00000000000 --- a/include/caffe/common_layers.hpp +++ /dev/null @@ -1,816 +0,0 @@ -#ifndef CAFFE_COMMON_LAYERS_HPP_ -#define CAFFE_COMMON_LAYERS_HPP_ - -#include -#include - -#include "caffe/blob.hpp" -#include "caffe/layer.hpp" -#include "caffe/proto/caffe.pb.h" - -namespace caffe { - -/** - * @brief Compute the index of the @f$ K @f$ max values for each datum across - * all dimensions @f$ (C \times H \times W) @f$. - * - * Intended for use after a classification layer to produce a prediction. - * If parameter out_max_val is set to true, output is a vector of pairs - * (max_ind, max_val) for each image. The axis parameter specifies an axis - * along which to maximise. - * - * NOTE: does not implement Backwards operation. - */ -template -class ArgMaxLayer : public Layer { - public: - /** - * @param param provides ArgMaxParameter argmax_param, - * with ArgMaxLayer options: - * - top_k (\b optional uint, default 1). - * the number @f$ K @f$ of maximal items to output. - * - out_max_val (\b optional bool, default false). - * if set, output a vector of pairs (max_ind, max_val) unless axis is set then - * output max_val along the specified axis. - * - axis (\b optional int). - * if set, maximise along the specified axis else maximise the flattened - * trailing dimensions for each index of the first / num dimension. - */ - explicit ArgMaxLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "ArgMax"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times 1 \times K) @f$ or, if out_max_val - * @f$ (N \times 2 \times K) @f$ unless axis set than e.g. - * @f$ (N \times K \times H \times W) @f$ if axis == 1 - * the computed outputs @f$ - * y_n = \arg\max\limits_i x_{ni} - * @f$ (for @f$ K = 1 @f$). - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - /// @brief Not implemented (non-differentiable function) - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) { - NOT_IMPLEMENTED; - } - bool out_max_val_; - size_t top_k_; - bool has_axis_; - int axis_; -}; - -/** - * @brief Normalizes the input to have 0-mean and/or unit (1) variance across - * the batch. - * - * This layer computes Batch Normalization described in [1]. For - * each channel in the data (i.e. axis 1), it subtracts the mean and divides - * by the variance, where both statistics are computed across both spatial - * dimensions and across the different examples in the batch. - * - * By default, during training time, the network is computing global mean/ - * variance statistics via a running average, which is then used at test - * time to allow deterministic outputs for each input. You can manually - * toggle whether the network is accumulating or using the statistics via the - * use_global_stats option. IMPORTANT: for this feature to work, you MUST - * set the learning rate to zero for all three parameter blobs, i.e., - * param {lr_mult: 0} three times in the layer definition. - * - * Note that the original paper also included a per-channel learned bias and - * scaling factor. It is possible (though a bit cumbersome) to implement - * this in caffe using a single-channel DummyDataLayer filled with zeros, - * followed by a Convolution layer with output the same size as the current. - * This produces a channel-specific value that can be added or multiplied by - * the BatchNorm layer's output. - * - * [1] S. Ioffe and C. Szegedy, "Batch Normalization: Accelerating Deep Network - * Training by Reducing Internal Covariate Shift." arXiv preprint - * arXiv:1502.03167 (2015). - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class BatchNormLayer : public Layer { - public: - explicit BatchNormLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "BatchNorm"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - Blob mean_, variance_, temp_, x_norm_; - bool use_global_stats_; - Dtype moving_average_fraction_; - int channels_; - Dtype eps_; - - // extra temporarary variables is used to carry out sums/broadcasting - // using BLAS - Blob batch_sum_multiplier_; - Blob num_by_chans_; - Blob spatial_sum_multiplier_; -}; - -/** - * @brief Index into the input blob along its first axis. - * - * This layer can be used to select, reorder, and even replicate examples in a - * batch. The second blob is cast to int and treated as an index into the - * first axis of the first blob. - */ -template -class BatchReindexLayer : public Layer { - public: - explicit BatchReindexLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "BatchReindex"; } - virtual inline int ExactNumBottomBlobs() const { return 2; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - /** - * @param bottom input Blob vector (length 2+) - * -# @f$ (N \times ...) @f$ - * the inputs @f$ x_1 @f$ - * -# @f$ (M) @f$ - * the inputs @f$ x_2 @f$ - * @param top output Blob vector (length 1) - * -# @f$ (M \times ...) @f$: - * the reindexed array @f$ - * y = x_1[x_2] - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the reordered input. - * - * @param top output Blob vector (length 1), providing the error gradient - * with respect to the outputs - * -# @f$ (M \times ...) @f$: - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to concatenated outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 2): - * - @f$ \frac{\partial E}{\partial y} @f$ is de-indexed (summing where - * required) back to the input x_1 - * - This layer cannot backprop to x_2, i.e. propagate_down[1] must be - * false. - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - private: - struct pair_sort_first { - bool operator()(const std::pair &left, - const std::pair &right) { - return left.first < right.first; - } - }; - void check_batch_reindex(int initial_num, int final_num, - const Dtype* ridx_data); -}; - -/** - * @brief Takes at least two Blob%s and concatenates them along either the num - * or channel dimension, outputting the result. - */ -template -class ConcatLayer : public Layer { - public: - explicit ConcatLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Concat"; } - virtual inline int MinBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - /** - * @param bottom input Blob vector (length 2+) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x_1 @f$ - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x_2 @f$ - * -# ... - * - K @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x_K @f$ - * @param top output Blob vector (length 1) - * -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or - * @f$ (N \times KC \times H \times W) @f$ if axis == 1: - * the concatenated output @f$ - * y = [\begin{array}{cccc} x_1 & x_2 & ... & x_K \end{array}] - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the concatenate inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or - * @f$ (N \times KC \times H \times W) @f$ if axis == 1: - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to concatenated outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length K), into which the top gradient - * @f$ \frac{\partial E}{\partial y} @f$ is deconcatenated back to the - * inputs @f$ - * \left[ \begin{array}{cccc} - * \frac{\partial E}{\partial x_1} & - * \frac{\partial E}{\partial x_2} & - * ... & - * \frac{\partial E}{\partial x_K} - * \end{array} \right] = - * \frac{\partial E}{\partial y} - * @f$ - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int count_; - int num_concats_; - int concat_input_size_; - int concat_axis_; -}; - -/** - * @brief Compute elementwise operations, such as product and sum, - * along multiple input Blobs. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class EltwiseLayer : public Layer { - public: - explicit EltwiseLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Eltwise"; } - virtual inline int MinBottomBlobs() const { return 2; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - EltwiseParameter_EltwiseOp op_; - vector coeffs_; - Blob max_idx_; - - bool stable_prod_grad_; -}; - -/** - * @brief A layer for learning "embeddings" of one-hot vector input. - * Equivalent to an InnerProductLayer with one-hot vectors as input, but - * for efficiency the input is the "hot" index of each column itself. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class EmbedLayer : public Layer { - public: - explicit EmbedLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Embed"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int M_; - int K_; - int N_; - bool bias_term_; - Blob bias_multiplier_; -}; - -/** - * @brief Takes two+ Blobs, interprets last Blob as a selector and - * filter remaining Blobs accordingly with selector data (0 means that - * the corresponding item has to be filtered, non-zero means that corresponding - * item needs to stay). - */ -template -class FilterLayer : public Layer { - public: - explicit FilterLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Filter"; } - virtual inline int MinBottomBlobs() const { return 2; } - virtual inline int MinTopBlobs() const { return 1; } - - protected: - /** - * @param bottom input Blob vector (length 2+) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs to be filtered @f$ x_1 @f$ - * -# ... - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs to be filtered @f$ x_K @f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the selector blob - * @param top output Blob vector (length 1+) - * -# @f$ (S \times C \times H \times W) @f$ () - * the filtered output @f$ x_1 @f$ - * where S is the number of items - * that haven't been filtered - * @f$ (S \times C \times H \times W) @f$ - * the filtered output @f$ x_K @f$ - * where S is the number of items - * that haven't been filtered - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the forwarded inputs. - * - * @param top output Blob vector (length 1+), providing the error gradient with - * respect to the outputs - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 2+), into which the top error - * gradient is copied - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool first_reshape_; - vector indices_to_forward_; -}; - -/** - * @brief Reshapes the input Blob into flat vectors. - * - * Note: because this layer does not change the input values -- merely the - * dimensions -- it can simply copy the input. The copy happens "virtually" - * (thus taking effectively 0 real time) by setting, in Forward, the data - * pointer of the top Blob to that of the bottom Blob (see Blob::ShareData), - * and in Backward, the diff pointer of the bottom Blob to that of the top Blob - * (see Blob::ShareDiff). - */ -template -class FlattenLayer : public Layer { - public: - explicit FlattenLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Flatten"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - /** - * @param bottom input Blob vector (length 2+) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs - * @param top output Blob vector (length 1) - * -# @f$ (N \times CHW \times 1 \times 1) @f$ - * the outputs -- i.e., the (virtually) copied, flattened inputs - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the concatenate inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length K), into which the top error - * gradient is (virtually) copied - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -/** - * @brief Also known as a "fully-connected" layer, computes an inner product - * with a set of learned weights, and (optionally) adds biases. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class InnerProductLayer : public Layer { - public: - explicit InnerProductLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "InnerProduct"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int M_; - int K_; - int N_; - bool bias_term_; - Blob bias_multiplier_; -}; - -/** - * @brief Normalizes the input to have 0-mean and/or unit (1) variance. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class MVNLayer : public Layer { - public: - explicit MVNLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "MVN"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - Blob mean_, variance_, temp_; - - /// sum_multiplier is used to carry out sum using BLAS - Blob sum_multiplier_; - Dtype eps_; -}; - -/* - * @brief Reshapes the input Blob into an arbitrary-sized output Blob. - * - * Note: similarly to FlattenLayer, this layer does not change the input values - * (see FlattenLayer, Blob::ShareData and Blob::ShareDiff). - */ -template -class ReshapeLayer : public Layer { - public: - explicit ReshapeLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Reshape"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top) {} - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top) {} - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - - /// @brief vector of axes indices whose dimensions we'll copy from the bottom - vector copy_axes_; - /// @brief the index of the axis whose dimension we infer, or -1 if none - int inferred_axis_; - /// @brief the product of the "constant" output dimensions - int constant_count_; -}; - -/** - * @brief Compute "reductions" -- operations that return a scalar output Blob - * for an input Blob of arbitrary size, such as the sum, absolute sum, - * and sum of squares. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class ReductionLayer : public Layer { - public: - explicit ReductionLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Reduction"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - /// @brief the reduction operation performed by the layer - ReductionParameter_ReductionOp op_; - /// @brief a scalar coefficient applied to all outputs - Dtype coeff_; - /// @brief the index of the first input axis to reduce - int axis_; - /// @brief the number of reductions performed - int num_; - /// @brief the input size of each reduction - int dim_; - /// @brief a helper Blob used for summation (op_ == SUM) - Blob sum_multiplier_; -}; - -/** - * @brief Ignores bottom blobs while producing no top blobs. (This is useful - * to suppress outputs during testing.) - */ -template -class SilenceLayer : public Layer { - public: - explicit SilenceLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top) {} - - virtual inline const char* type() const { return "Silence"; } - virtual inline int MinBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 0; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top) {} - // We can't define Forward_gpu here, since STUB_GPU will provide - // its own definition for CPU_ONLY mode. - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -/** - * @brief Computes the softmax function. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class SoftmaxLayer : public Layer { - public: - explicit SoftmaxLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Softmax"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int outer_num_; - int inner_num_; - int softmax_axis_; - /// sum_multiplier is used to carry out sum using BLAS - Blob sum_multiplier_; - /// scale is an intermediate Blob to hold temporary results. - Blob scale_; -}; - -#ifdef USE_CUDNN -/** - * @brief cuDNN implementation of SoftmaxLayer. - * Fallback to SoftmaxLayer for CPU mode. - */ -template -class CuDNNSoftmaxLayer : public SoftmaxLayer { - public: - explicit CuDNNSoftmaxLayer(const LayerParameter& param) - : SoftmaxLayer(param), handles_setup_(false) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNSoftmaxLayer(); - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t handle_; - cudnnTensorDescriptor_t bottom_desc_; - cudnnTensorDescriptor_t top_desc_; -}; -#endif - -/** - * @brief Creates a "split" path in the network by copying the bottom Blob - * into multiple top Blob%s to be used by multiple consuming layers. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class SplitLayer : public Layer { - public: - explicit SplitLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Split"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int MinTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int count_; -}; - -/** - * @brief Takes a Blob and slices it along either the num or channel dimension, - * outputting multiple sliced Blob results. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class SliceLayer : public Layer { - public: - explicit SliceLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Slice"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int MinTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int count_; - int num_slices_; - int slice_size_; - int slice_axis_; - vector slice_point_; -}; - -/** - * @brief Copy a Blob along specified dimensions. - */ -template -class TileLayer : public Layer { - public: - explicit TileLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Tile"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - unsigned int axis_, tiles_, outer_dim_, inner_dim_; -}; - -} // namespace caffe - -#endif // CAFFE_COMMON_LAYERS_HPP_ diff --git a/include/caffe/data_layers.hpp b/include/caffe/data_layers.hpp deleted file mode 100644 index aa0ab7df390..00000000000 --- a/include/caffe/data_layers.hpp +++ /dev/null @@ -1,347 +0,0 @@ -#ifndef CAFFE_DATA_LAYERS_HPP_ -#define CAFFE_DATA_LAYERS_HPP_ - -#include -#include -#include -#include "hdf5.h" - -#include "caffe/blob.hpp" -#include "caffe/common.hpp" -#include "caffe/data_reader.hpp" -#include "caffe/data_transformer.hpp" -#include "caffe/filler.hpp" -#include "caffe/internal_thread.hpp" -#include "caffe/layer.hpp" -#include "caffe/proto/caffe.pb.h" -#include "caffe/util/blocking_queue.hpp" -#include "caffe/util/db.hpp" - -#define HDF5_DATA_DATASET_NAME "data" -#define HDF5_DATA_LABEL_NAME "label" - -namespace caffe { - -/** - * @brief Provides base for data layers that feed blobs to the Net. - * - * TODO(dox): thorough documentation for Forward and proto params. - */ -template -class BaseDataLayer : public Layer { - public: - explicit BaseDataLayer(const LayerParameter& param); - // LayerSetUp: implements common data layer setup functionality, and calls - // DataLayerSetUp to do special data layer setup for individual layer types. - // This method may not be overridden except by the BasePrefetchingDataLayer. - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - // Data layers should be shared by multiple solvers in parallel - virtual inline bool ShareInParallel() const { return true; } - virtual void DataLayerSetUp(const vector*>& bottom, - const vector*>& top) {} - // Data layers have no bottoms, so reshaping is trivial. - virtual void Reshape(const vector*>& bottom, - const vector*>& top) {} - - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - - protected: - TransformationParameter transform_param_; - shared_ptr > data_transformer_; - bool output_labels_; -}; - -template -class Batch { - public: - Blob data_, label_; -}; - -template -class BasePrefetchingDataLayer : - public BaseDataLayer, public InternalThread { - public: - explicit BasePrefetchingDataLayer(const LayerParameter& param); - // LayerSetUp: implements common data layer setup functionality, and calls - // DataLayerSetUp to do special data layer setup for individual layer types. - // This method may not be overridden. - void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - // Prefetches batches (asynchronously if to GPU memory) - static const int PREFETCH_COUNT = 3; - - protected: - virtual void InternalThreadEntry(); - virtual void load_batch(Batch* batch) = 0; - - Batch prefetch_[PREFETCH_COUNT]; - BlockingQueue*> prefetch_free_; - BlockingQueue*> prefetch_full_; - - Blob transformed_data_; -}; - -template -class DataLayer : public BasePrefetchingDataLayer { - public: - explicit DataLayer(const LayerParameter& param); - virtual ~DataLayer(); - virtual void DataLayerSetUp(const vector*>& bottom, - const vector*>& top); - // DataLayer uses DataReader instead for sharing for parallelism - virtual inline bool ShareInParallel() const { return false; } - virtual inline const char* type() const { return "Data"; } - virtual inline int ExactNumBottomBlobs() const { return 0; } - virtual inline int MinTopBlobs() const { return 1; } - virtual inline int MaxTopBlobs() const { return 2; } - - protected: - virtual void load_batch(Batch* batch); - - DataReader reader_; -}; - -/** - * @brief Provides data to the Net generated by a Filler. - * - * TODO(dox): thorough documentation for Forward and proto params. - */ -template -class DummyDataLayer : public Layer { - public: - explicit DummyDataLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - // Data layers should be shared by multiple solvers in parallel - virtual inline bool ShareInParallel() const { return true; } - // Data layers have no bottoms, so reshaping is trivial. - virtual void Reshape(const vector*>& bottom, - const vector*>& top) {} - - virtual inline const char* type() const { return "DummyData"; } - virtual inline int ExactNumBottomBlobs() const { return 0; } - virtual inline int MinTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - - vector > > fillers_; - vector refill_; -}; - -/** - * @brief Provides data to the Net from HDF5 files. - * - * TODO(dox): thorough documentation for Forward and proto params. - */ -template -class HDF5DataLayer : public Layer { - public: - explicit HDF5DataLayer(const LayerParameter& param) - : Layer(param) {} - virtual ~HDF5DataLayer(); - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - // Data layers should be shared by multiple solvers in parallel - virtual inline bool ShareInParallel() const { return true; } - // Data layers have no bottoms, so reshaping is trivial. - virtual void Reshape(const vector*>& bottom, - const vector*>& top) {} - - virtual inline const char* type() const { return "HDF5Data"; } - virtual inline int ExactNumBottomBlobs() const { return 0; } - virtual inline int MinTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) {} - virtual void LoadHDF5FileData(const char* filename); - - std::vector hdf_filenames_; - unsigned int num_files_; - unsigned int current_file_; - hsize_t current_row_; - std::vector > > hdf_blobs_; - std::vector data_permutation_; - std::vector file_permutation_; -}; - -/** - * @brief Write blobs to disk as HDF5 files. - * - * TODO(dox): thorough documentation for Forward and proto params. - */ -template -class HDF5OutputLayer : public Layer { - public: - explicit HDF5OutputLayer(const LayerParameter& param) - : Layer(param), file_opened_(false) {} - virtual ~HDF5OutputLayer(); - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - // Data layers should be shared by multiple solvers in parallel - virtual inline bool ShareInParallel() const { return true; } - // Data layers have no bottoms, so reshaping is trivial. - virtual void Reshape(const vector*>& bottom, - const vector*>& top) {} - - virtual inline const char* type() const { return "HDF5Output"; } - // TODO: no limit on the number of blobs - virtual inline int ExactNumBottomBlobs() const { return 2; } - virtual inline int ExactNumTopBlobs() const { return 0; } - - inline std::string file_name() const { return file_name_; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void SaveBlobs(); - - bool file_opened_; - std::string file_name_; - hid_t file_id_; - Blob data_blob_; - Blob label_blob_; -}; - -/** - * @brief Provides data to the Net from image files. - * - * TODO(dox): thorough documentation for Forward and proto params. - */ -template -class ImageDataLayer : public BasePrefetchingDataLayer { - public: - explicit ImageDataLayer(const LayerParameter& param) - : BasePrefetchingDataLayer(param) {} - virtual ~ImageDataLayer(); - virtual void DataLayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "ImageData"; } - virtual inline int ExactNumBottomBlobs() const { return 0; } - virtual inline int ExactNumTopBlobs() const { return 2; } - - protected: - shared_ptr prefetch_rng_; - virtual void ShuffleImages(); - virtual void load_batch(Batch* batch); - - vector > lines_; - int lines_id_; -}; - -/** - * @brief Provides data to the Net from memory. - * - * TODO(dox): thorough documentation for Forward and proto params. - */ -template -class MemoryDataLayer : public BaseDataLayer { - public: - explicit MemoryDataLayer(const LayerParameter& param) - : BaseDataLayer(param), has_new_data_(false) {} - virtual void DataLayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "MemoryData"; } - virtual inline int ExactNumBottomBlobs() const { return 0; } - virtual inline int ExactNumTopBlobs() const { return 2; } - - virtual void AddDatumVector(const vector& datum_vector); -#ifdef USE_OPENCV - virtual void AddMatVector(const vector& mat_vector, - const vector& labels); -#endif // USE_OPENCV - - // Reset should accept const pointers, but can't, because the memory - // will be given to Blob, which is mutable - void Reset(Dtype* data, Dtype* label, int n); - void set_batch_size(int new_size); - - int batch_size() { return batch_size_; } - int channels() { return channels_; } - int height() { return height_; } - int width() { return width_; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - - int batch_size_, channels_, height_, width_, size_; - Dtype* data_; - Dtype* labels_; - int n_; - size_t pos_; - Blob added_data_; - Blob added_label_; - bool has_new_data_; -}; - -/** - * @brief Provides data to the Net from windows of images files, specified - * by a window data file. - * - * TODO(dox): thorough documentation for Forward and proto params. - */ -template -class WindowDataLayer : public BasePrefetchingDataLayer { - public: - explicit WindowDataLayer(const LayerParameter& param) - : BasePrefetchingDataLayer(param) {} - virtual ~WindowDataLayer(); - virtual void DataLayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "WindowData"; } - virtual inline int ExactNumBottomBlobs() const { return 0; } - virtual inline int ExactNumTopBlobs() const { return 2; } - - protected: - virtual unsigned int PrefetchRand(); - virtual void load_batch(Batch* batch); - - shared_ptr prefetch_rng_; - vector > > image_database_; - enum WindowField { IMAGE_INDEX, LABEL, OVERLAP, X1, Y1, X2, Y2, NUM }; - vector > fg_windows_; - vector > bg_windows_; - Blob data_mean_; - vector mean_values_; - bool has_mean_file_; - bool has_mean_values_; - bool cache_images_; - vector > image_database_cache_; -}; - -} // namespace caffe - -#endif // CAFFE_DATA_LAYERS_HPP_ diff --git a/include/caffe/layer_factory.hpp b/include/caffe/layer_factory.hpp index 2c2fde4d979..f385afccfee 100644 --- a/include/caffe/layer_factory.hpp +++ b/include/caffe/layer_factory.hpp @@ -44,6 +44,7 @@ #include #include "caffe/common.hpp" +#include "caffe/layer.hpp" #include "caffe/proto/caffe.pb.h" namespace caffe { diff --git a/include/caffe/layers/absval_layer.hpp b/include/caffe/layers/absval_layer.hpp new file mode 100644 index 00000000000..9b5305dceb4 --- /dev/null +++ b/include/caffe/layers/absval_layer.hpp @@ -0,0 +1,68 @@ +#ifndef CAFFE_ABSVAL_LAYER_HPP_ +#define CAFFE_ABSVAL_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Computes @f$ y = |x| @f$ + * + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ y = |x| @f$ + */ +template +class AbsValLayer : public NeuronLayer { + public: + explicit AbsValLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "AbsVal"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + /// @copydoc AbsValLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the absolute value inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} = + * \mathrm{sign}(x) \frac{\partial E}{\partial y} + * @f$ if propagate_down[0] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_ABSVAL_LAYER_HPP_ diff --git a/include/caffe/layers/accuracy_layer.hpp b/include/caffe/layers/accuracy_layer.hpp new file mode 100644 index 00000000000..fe2adb939e4 --- /dev/null +++ b/include/caffe/layers/accuracy_layer.hpp @@ -0,0 +1,95 @@ +#ifndef CAFFE_ACCURACY_LAYER_HPP_ +#define CAFFE_ACCURACY_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" + +namespace caffe { + +/** + * @brief Computes the classification accuracy for a one-of-many + * classification task. + */ +template +class AccuracyLayer : public Layer { + public: + /** + * @param param provides AccuracyParameter accuracy_param, + * with AccuracyLayer options: + * - top_k (\b optional, default 1). + * Sets the maximum rank @f$ k @f$ at which a prediction is considered + * correct. For example, if @f$ k = 5 @f$, a prediction is counted + * correct if the correct label is among the top 5 predicted labels. + */ + explicit AccuracyLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Accuracy"; } + virtual inline int ExactNumBottomBlobs() const { return 2; } + + // If there are two top blobs, then the second blob will contain + // accuracies per class. + virtual inline int MinTopBlobs() const { return 1; } + virtual inline int MaxTopBlos() const { return 2; } + + protected: + /** + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ x @f$, a Blob with values in + * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of + * the @f$ K = CHW @f$ classes. Each @f$ x_n @f$ is mapped to a predicted + * label @f$ \hat{l}_n @f$ given by its maximal index: + * @f$ \hat{l}_n = \arg\max\limits_k x_{nk} @f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels @f$ l @f$, an integer-valued Blob with values + * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ + * indicating the correct class label among the @f$ K @f$ classes + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed accuracy: @f$ + * \frac{1}{N} \sum\limits_{n=1}^N \delta\{ \hat{l}_n = l_n \} + * @f$, where @f$ + * \delta\{\mathrm{condition}\} = \left\{ + * \begin{array}{lr} + * 1 & \mbox{if condition} \\ + * 0 & \mbox{otherwise} + * \end{array} \right. + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + + + /// @brief Not implemented -- AccuracyLayer cannot be used as a loss. + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) { + for (int i = 0; i < propagate_down.size(); ++i) { + if (propagate_down[i]) { NOT_IMPLEMENTED; } + } + } + + int label_axis_, outer_num_, inner_num_; + + int top_k_; + + /// Whether to ignore instances with a certain label. + bool has_ignore_label_; + /// The label indicating that an instance should be ignored. + int ignore_label_; + /// Keeps counts of the number of samples per class. + Blob nums_buffer_; +}; + +} // namespace caffe + +#endif // CAFFE_ACCURACY_LAYER_HPP_ diff --git a/include/caffe/layers/argmax_layer.hpp b/include/caffe/layers/argmax_layer.hpp new file mode 100644 index 00000000000..4fef363e850 --- /dev/null +++ b/include/caffe/layers/argmax_layer.hpp @@ -0,0 +1,77 @@ +#ifndef CAFFE_ARGMAX_LAYER_HPP_ +#define CAFFE_ARGMAX_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Compute the index of the @f$ K @f$ max values for each datum across + * all dimensions @f$ (C \times H \times W) @f$. + * + * Intended for use after a classification layer to produce a prediction. + * If parameter out_max_val is set to true, output is a vector of pairs + * (max_ind, max_val) for each image. The axis parameter specifies an axis + * along which to maximise. + * + * NOTE: does not implement Backwards operation. + */ +template +class ArgMaxLayer : public Layer { + public: + /** + * @param param provides ArgMaxParameter argmax_param, + * with ArgMaxLayer options: + * - top_k (\b optional uint, default 1). + * the number @f$ K @f$ of maximal items to output. + * - out_max_val (\b optional bool, default false). + * if set, output a vector of pairs (max_ind, max_val) unless axis is set then + * output max_val along the specified axis. + * - axis (\b optional int). + * if set, maximise along the specified axis else maximise the flattened + * trailing dimensions for each index of the first / num dimension. + */ + explicit ArgMaxLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "ArgMax"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times 1 \times K) @f$ or, if out_max_val + * @f$ (N \times 2 \times K) @f$ unless axis set than e.g. + * @f$ (N \times K \times H \times W) @f$ if axis == 1 + * the computed outputs @f$ + * y_n = \arg\max\limits_i x_{ni} + * @f$ (for @f$ K = 1 @f$). + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + /// @brief Not implemented (non-differentiable function) + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) { + NOT_IMPLEMENTED; + } + bool out_max_val_; + size_t top_k_; + bool has_axis_; + int axis_; +}; + +} // namespace caffe + +#endif // CAFFE_ARGMAX_LAYER_HPP_ diff --git a/include/caffe/layers/base_conv_layer.hpp b/include/caffe/layers/base_conv_layer.hpp new file mode 100644 index 00000000000..f3def16c039 --- /dev/null +++ b/include/caffe/layers/base_conv_layer.hpp @@ -0,0 +1,168 @@ +#ifndef CAFFE_BASE_CONVOLUTION_LAYER_HPP_ +#define CAFFE_BASE_CONVOLUTION_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" +#include "caffe/util/im2col.hpp" + +namespace caffe { + +/** + * @brief Abstract base class that factors out the BLAS code common to + * ConvolutionLayer and DeconvolutionLayer. + */ +template +class BaseConvolutionLayer : public Layer { + public: + explicit BaseConvolutionLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline int MinBottomBlobs() const { return 1; } + virtual inline int MinTopBlobs() const { return 1; } + virtual inline bool EqualNumBottomTopBlobs() const { return true; } + + protected: + // Helper functions that abstract away the column buffer and gemm arguments. + // The last argument in forward_cpu_gemm is so that we can skip the im2col if + // we just called weight_cpu_gemm with the same input. + void forward_cpu_gemm(const Dtype* input, const Dtype* weights, + Dtype* output, bool skip_im2col = false); + void forward_cpu_bias(Dtype* output, const Dtype* bias); + void backward_cpu_gemm(const Dtype* input, const Dtype* weights, + Dtype* output); + void weight_cpu_gemm(const Dtype* input, const Dtype* output, Dtype* + weights); + void backward_cpu_bias(Dtype* bias, const Dtype* input); + +#ifndef CPU_ONLY + void forward_gpu_gemm(const Dtype* col_input, const Dtype* weights, + Dtype* output, bool skip_im2col = false); + void forward_gpu_bias(Dtype* output, const Dtype* bias); + void backward_gpu_gemm(const Dtype* input, const Dtype* weights, + Dtype* col_output); + void weight_gpu_gemm(const Dtype* col_input, const Dtype* output, Dtype* + weights); + void backward_gpu_bias(Dtype* bias, const Dtype* input); +#endif + + /// @brief The spatial dimensions of the input. + inline int input_shape(int i) { + return (*bottom_shape_)[channel_axis_ + i]; + } + // reverse_dimensions should return true iff we are implementing deconv, so + // that conv helpers know which dimensions are which. + virtual bool reverse_dimensions() = 0; + // Compute height_out_ and width_out_ from other parameters. + virtual void compute_output_shape() = 0; + + /// @brief The spatial dimensions of a filter kernel. + Blob kernel_shape_; + /// @brief The spatial dimensions of the stride. + Blob stride_; + /// @brief The spatial dimensions of the padding. + Blob pad_; + /// @brief The spatial dimensions of the convolution input. + Blob conv_input_shape_; + /// @brief The spatial dimensions of the col_buffer. + vector col_buffer_shape_; + /// @brief The spatial dimensions of the output. + vector output_shape_; + const vector* bottom_shape_; + + int num_spatial_axes_; + int bottom_dim_; + int top_dim_; + + int channel_axis_; + int num_; + int channels_; + int group_; + int out_spatial_dim_; + int weight_offset_; + int num_output_; + bool bias_term_; + bool is_1x1_; + bool force_nd_im2col_; + + private: + // wrap im2col/col2im so we don't have to remember the (long) argument lists + inline void conv_im2col_cpu(const Dtype* data, Dtype* col_buff) { + if (!force_nd_im2col_ && num_spatial_axes_ == 2) { + im2col_cpu(data, conv_in_channels_, + conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], + kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], + pad_.cpu_data()[0], pad_.cpu_data()[1], + stride_.cpu_data()[0], stride_.cpu_data()[1], col_buff); + } else { + im2col_nd_cpu(data, num_spatial_axes_, conv_input_shape_.cpu_data(), + col_buffer_shape_.data(), kernel_shape_.cpu_data(), + pad_.cpu_data(), stride_.cpu_data(), col_buff); + } + } + inline void conv_col2im_cpu(const Dtype* col_buff, Dtype* data) { + if (!force_nd_im2col_ && num_spatial_axes_ == 2) { + col2im_cpu(col_buff, conv_in_channels_, + conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], + kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], + pad_.cpu_data()[0], pad_.cpu_data()[1], + stride_.cpu_data()[0], stride_.cpu_data()[1], data); + } else { + col2im_nd_cpu(col_buff, num_spatial_axes_, conv_input_shape_.cpu_data(), + col_buffer_shape_.data(), kernel_shape_.cpu_data(), + pad_.cpu_data(), stride_.cpu_data(), data); + } + } +#ifndef CPU_ONLY + inline void conv_im2col_gpu(const Dtype* data, Dtype* col_buff) { + if (!force_nd_im2col_ && num_spatial_axes_ == 2) { + im2col_gpu(data, conv_in_channels_, + conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], + kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], + pad_.cpu_data()[0], pad_.cpu_data()[1], + stride_.cpu_data()[0], stride_.cpu_data()[1], col_buff); + } else { + im2col_nd_gpu(data, num_spatial_axes_, num_kernels_im2col_, + conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(), + kernel_shape_.gpu_data(), pad_.gpu_data(), + stride_.gpu_data(), col_buff); + } + } + inline void conv_col2im_gpu(const Dtype* col_buff, Dtype* data) { + if (!force_nd_im2col_ && num_spatial_axes_ == 2) { + col2im_gpu(col_buff, conv_in_channels_, + conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], + kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], + pad_.cpu_data()[0], pad_.cpu_data()[1], + stride_.cpu_data()[0], stride_.cpu_data()[1], data); + } else { + col2im_nd_gpu(col_buff, num_spatial_axes_, num_kernels_col2im_, + conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(), + kernel_shape_.gpu_data(), pad_.gpu_data(), stride_.gpu_data(), + data); + } + } +#endif + + int num_kernels_im2col_; + int num_kernels_col2im_; + int conv_out_channels_; + int conv_in_channels_; + int conv_out_spatial_dim_; + int kernel_dim_; + int col_offset_; + int output_offset_; + + Blob col_buffer_; + Blob bias_multiplier_; +}; + +} // namespace caffe + +#endif // CAFFE_BASE_CONVOLUTION_LAYER_HPP_ diff --git a/include/caffe/layers/base_data_layer.hpp b/include/caffe/layers/base_data_layer.hpp new file mode 100644 index 00000000000..2c49b73184b --- /dev/null +++ b/include/caffe/layers/base_data_layer.hpp @@ -0,0 +1,86 @@ +#ifndef CAFFE_DATA_LAYERS_HPP_ +#define CAFFE_DATA_LAYERS_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/data_transformer.hpp" +#include "caffe/internal_thread.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" +#include "caffe/util/blocking_queue.hpp" + +namespace caffe { + +/** + * @brief Provides base for data layers that feed blobs to the Net. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class BaseDataLayer : public Layer { + public: + explicit BaseDataLayer(const LayerParameter& param); + // LayerSetUp: implements common data layer setup functionality, and calls + // DataLayerSetUp to do special data layer setup for individual layer types. + // This method may not be overridden except by the BasePrefetchingDataLayer. + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + // Data layers should be shared by multiple solvers in parallel + virtual inline bool ShareInParallel() const { return true; } + virtual void DataLayerSetUp(const vector*>& bottom, + const vector*>& top) {} + // Data layers have no bottoms, so reshaping is trivial. + virtual void Reshape(const vector*>& bottom, + const vector*>& top) {} + + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + + protected: + TransformationParameter transform_param_; + shared_ptr > data_transformer_; + bool output_labels_; +}; + +template +class Batch { + public: + Blob data_, label_; +}; + +template +class BasePrefetchingDataLayer : + public BaseDataLayer, public InternalThread { + public: + explicit BasePrefetchingDataLayer(const LayerParameter& param); + // LayerSetUp: implements common data layer setup functionality, and calls + // DataLayerSetUp to do special data layer setup for individual layer types. + // This method may not be overridden. + void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + // Prefetches batches (asynchronously if to GPU memory) + static const int PREFETCH_COUNT = 3; + + protected: + virtual void InternalThreadEntry(); + virtual void load_batch(Batch* batch) = 0; + + Batch prefetch_[PREFETCH_COUNT]; + BlockingQueue*> prefetch_free_; + BlockingQueue*> prefetch_full_; + + Blob transformed_data_; +}; + +} // namespace caffe + +#endif // CAFFE_DATA_LAYERS_HPP_ diff --git a/include/caffe/layers/batch_norm_layer.hpp b/include/caffe/layers/batch_norm_layer.hpp new file mode 100644 index 00000000000..9b2d5126efb --- /dev/null +++ b/include/caffe/layers/batch_norm_layer.hpp @@ -0,0 +1,81 @@ +#ifndef CAFFE_BATCHNORM_LAYER_HPP_ +#define CAFFE_BATCHNORM_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Normalizes the input to have 0-mean and/or unit (1) variance across + * the batch. + * + * This layer computes Batch Normalization described in [1]. For + * each channel in the data (i.e. axis 1), it subtracts the mean and divides + * by the variance, where both statistics are computed across both spatial + * dimensions and across the different examples in the batch. + * + * By default, during training time, the network is computing global mean/ + * variance statistics via a running average, which is then used at test + * time to allow deterministic outputs for each input. You can manually + * toggle whether the network is accumulating or using the statistics via the + * use_global_stats option. IMPORTANT: for this feature to work, you MUST + * set the learning rate to zero for all three parameter blobs, i.e., + * param {lr_mult: 0} three times in the layer definition. + * + * Note that the original paper also included a per-channel learned bias and + * scaling factor. It is possible (though a bit cumbersome) to implement + * this in caffe using a single-channel DummyDataLayer filled with zeros, + * followed by a Convolution layer with output the same size as the current. + * This produces a channel-specific value that can be added or multiplied by + * the BatchNorm layer's output. + * + * [1] S. Ioffe and C. Szegedy, "Batch Normalization: Accelerating Deep Network + * Training by Reducing Internal Covariate Shift." arXiv preprint + * arXiv:1502.03167 (2015). + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class BatchNormLayer : public Layer { + public: + explicit BatchNormLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "BatchNorm"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + Blob mean_, variance_, temp_, x_norm_; + bool use_global_stats_; + Dtype moving_average_fraction_; + int channels_; + Dtype eps_; + + // extra temporarary variables is used to carry out sums/broadcasting + // using BLAS + Blob batch_sum_multiplier_; + Blob num_by_chans_; + Blob spatial_sum_multiplier_; +}; + +} // namespace caffe + +#endif // CAFFE_BATCHNORM_LAYER_HPP_ diff --git a/include/caffe/layers/batch_reindex_layer.hpp b/include/caffe/layers/batch_reindex_layer.hpp new file mode 100644 index 00000000000..ebb3a567bc4 --- /dev/null +++ b/include/caffe/layers/batch_reindex_layer.hpp @@ -0,0 +1,83 @@ +#ifndef CAFFE_BATCHREINDEX_LAYER_HPP_ +#define CAFFE_BATCHREINDEX_LAYER_HPP_ + +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Index into the input blob along its first axis. + * + * This layer can be used to select, reorder, and even replicate examples in a + * batch. The second blob is cast to int and treated as an index into the + * first axis of the first blob. + */ +template +class BatchReindexLayer : public Layer { + public: + explicit BatchReindexLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "BatchReindex"; } + virtual inline int ExactNumBottomBlobs() const { return 2; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + /** + * @param bottom input Blob vector (length 2+) + * -# @f$ (N \times ...) @f$ + * the inputs @f$ x_1 @f$ + * -# @f$ (M) @f$ + * the inputs @f$ x_2 @f$ + * @param top output Blob vector (length 1) + * -# @f$ (M \times ...) @f$: + * the reindexed array @f$ + * y = x_1[x_2] + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the reordered input. + * + * @param top output Blob vector (length 1), providing the error gradient + * with respect to the outputs + * -# @f$ (M \times ...) @f$: + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to concatenated outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 2): + * - @f$ \frac{\partial E}{\partial y} @f$ is de-indexed (summing where + * required) back to the input x_1 + * - This layer cannot backprop to x_2, i.e. propagate_down[1] must be + * false. + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + private: + struct pair_sort_first { + bool operator()(const std::pair &left, + const std::pair &right) { + return left.first < right.first; + } + }; + void check_batch_reindex(int initial_num, int final_num, + const Dtype* ridx_data); +}; + +} // namespace caffe + +#endif // CAFFE_BATCHREINDEX_LAYER_HPP_ diff --git a/include/caffe/layers/bnll_layer.hpp b/include/caffe/layers/bnll_layer.hpp new file mode 100644 index 00000000000..be07c748364 --- /dev/null +++ b/include/caffe/layers/bnll_layer.hpp @@ -0,0 +1,70 @@ +#ifndef CAFFE_BNLL_LAYER_HPP_ +#define CAFFE_BNLL_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Computes @f$ y = x + \log(1 + \exp(-x)) @f$ if @f$ x > 0 @f$; + * @f$ y = \log(1 + \exp(x)) @f$ otherwise. + * + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = \left\{ + * \begin{array}{ll} + * x + \log(1 + \exp(-x)) & \mbox{if } x > 0 \\ + * \log(1 + \exp(x)) & \mbox{otherwise} + * \end{array} \right. + * @f$ + */ +template +class BNLLLayer : public NeuronLayer { + public: + explicit BNLLLayer(const LayerParameter& param) + : NeuronLayer(param) {} + + virtual inline const char* type() const { return "BNLL"; } + + protected: + /// @copydoc BNLLLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the BNLL inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} + * @f$ if propagate_down[0] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_BNLL_LAYER_HPP_ diff --git a/include/caffe/layers/concat_layer.hpp b/include/caffe/layers/concat_layer.hpp new file mode 100644 index 00000000000..a1570249197 --- /dev/null +++ b/include/caffe/layers/concat_layer.hpp @@ -0,0 +1,87 @@ +#ifndef CAFFE_CONCAT_LAYER_HPP_ +#define CAFFE_CONCAT_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Takes at least two Blob%s and concatenates them along either the num + * or channel dimension, outputting the result. + */ +template +class ConcatLayer : public Layer { + public: + explicit ConcatLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Concat"; } + virtual inline int MinBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + /** + * @param bottom input Blob vector (length 2+) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x_1 @f$ + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x_2 @f$ + * -# ... + * - K @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x_K @f$ + * @param top output Blob vector (length 1) + * -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or + * @f$ (N \times KC \times H \times W) @f$ if axis == 1: + * the concatenated output @f$ + * y = [\begin{array}{cccc} x_1 & x_2 & ... & x_K \end{array}] + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the concatenate inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or + * @f$ (N \times KC \times H \times W) @f$ if axis == 1: + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to concatenated outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length K), into which the top gradient + * @f$ \frac{\partial E}{\partial y} @f$ is deconcatenated back to the + * inputs @f$ + * \left[ \begin{array}{cccc} + * \frac{\partial E}{\partial x_1} & + * \frac{\partial E}{\partial x_2} & + * ... & + * \frac{\partial E}{\partial x_K} + * \end{array} \right] = + * \frac{\partial E}{\partial y} + * @f$ + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int count_; + int num_concats_; + int concat_input_size_; + int concat_axis_; +}; + +} // namespace caffe + +#endif // CAFFE_CONCAT_LAYER_HPP_ diff --git a/include/caffe/layers/contrastive_loss_layer.hpp b/include/caffe/layers/contrastive_loss_layer.hpp new file mode 100644 index 00000000000..e890afb8207 --- /dev/null +++ b/include/caffe/layers/contrastive_loss_layer.hpp @@ -0,0 +1,101 @@ +#ifndef CAFFE_CONTRASTIVE_LOSS_LAYER_HPP_ +#define CAFFE_CONTRASTIVE_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" + +namespace caffe { + +/** + * @brief Computes the contrastive loss @f$ + * E = \frac{1}{2N} \sum\limits_{n=1}^N \left(y\right) d^2 + + * \left(1-y\right) \max \left(margin-d, 0\right)^2 + * @f$ where @f$ + * d = \left| \left| a_n - b_n \right| \right|_2 @f$. This can be + * used to train siamese networks. + * + * @param bottom input Blob vector (length 3) + * -# @f$ (N \times C \times 1 \times 1) @f$ + * the features @f$ a \in [-\infty, +\infty]@f$ + * -# @f$ (N \times C \times 1 \times 1) @f$ + * the features @f$ b \in [-\infty, +\infty]@f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the binary similarity @f$ s \in [0, 1]@f$ + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed contrastive loss: @f$ E = + * \frac{1}{2N} \sum\limits_{n=1}^N \left(y\right) d^2 + + * \left(1-y\right) \max \left(margin-d, 0\right)^2 + * @f$ where @f$ + * d = \left| \left| a_n - b_n \right| \right|_2 @f$. + * This can be used to train siamese networks. + */ +template +class ContrastiveLossLayer : public LossLayer { + public: + explicit ContrastiveLossLayer(const LayerParameter& param) + : LossLayer(param), diff_() {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline int ExactNumBottomBlobs() const { return 3; } + virtual inline const char* type() const { return "ContrastiveLoss"; } + /** + * Unlike most loss layers, in the ContrastiveLossLayer we can backpropagate + * to the first two inputs. + */ + virtual inline bool AllowForceBackward(const int bottom_index) const { + return bottom_index != 2; + } + + protected: + /// @copydoc ContrastiveLossLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the Contrastive error gradient w.r.t. the inputs. + * + * Computes the gradients with respect to the two input vectors (bottom[0] and + * bottom[1]), but not the similarity label (bottom[2]). + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, + * as @f$ \lambda @f$ is the coefficient of this layer's output + * @f$\ell_i@f$ in the overall Net loss + * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence + * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. + * (*Assuming that this top Blob is not used as a bottom (input) by any + * other layer of the Net.) + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times 1 \times 1) @f$ + * the features @f$a@f$; Backward fills their diff with + * gradients if propagate_down[0] + * -# @f$ (N \times C \times 1 \times 1) @f$ + * the features @f$b@f$; Backward fills their diff with gradients if + * propagate_down[1] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + Blob diff_; // cached for backward pass + Blob dist_sq_; // cached for backward pass + Blob diff_sq_; // tmp storage for gpu forward pass + Blob summer_vec_; // tmp storage for gpu forward pass +}; + +} // namespace caffe + +#endif // CAFFE_CONTRASTIVE_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/conv_layer.hpp b/include/caffe/layers/conv_layer.hpp new file mode 100644 index 00000000000..15574766de5 --- /dev/null +++ b/include/caffe/layers/conv_layer.hpp @@ -0,0 +1,81 @@ +#ifndef CAFFE_CONV_LAYER_HPP_ +#define CAFFE_CONV_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/base_conv_layer.hpp" + +namespace caffe { + +/** + * @brief Convolves the input image with a bank of learned filters, + * and (optionally) adds biases. + * + * Caffe convolves by reduction to matrix multiplication. This achieves + * high-throughput and generality of input and filter dimensions but comes at + * the cost of memory for matrices. This makes use of efficiency in BLAS. + * + * The input is "im2col" transformed to a channel K' x H x W data matrix + * for multiplication with the N x K' x H x W filter matrix to yield a + * N' x H x W output matrix that is then "col2im" restored. K' is the + * input channel * kernel height * kernel width dimension of the unrolled + * inputs so that the im2col matrix has a column for each input region to + * be filtered. col2im restores the output spatial structure by rolling up + * the output channel N' columns of the output matrix. + */ +template +class ConvolutionLayer : public BaseConvolutionLayer { + public: + /** + * @param param provides ConvolutionParameter convolution_param, + * with ConvolutionLayer options: + * - num_output. The number of filters. + * - kernel_size / kernel_h / kernel_w. The filter dimensions, given by + * kernel_size for square filters or kernel_h and kernel_w for rectangular + * filters. + * - stride / stride_h / stride_w (\b optional, default 1). The filter + * stride, given by stride_size for equal dimensions or stride_h and stride_w + * for different strides. By default the convolution is dense with stride 1. + * - pad / pad_h / pad_w (\b optional, default 0). The zero-padding for + * convolution, given by pad for equal dimensions or pad_h and pad_w for + * different padding. Input padding is computed implicitly instead of + * actually padding. + * - group (\b optional, default 1). The number of filter groups. Group + * convolution is a method for reducing parameterization by selectively + * connecting input and output channels. The input and output channel dimensions must be divisible + * by the number of groups. For group @f$ \geq 1 @f$, the + * convolutional filters' input and output channels are separated s.t. each + * group takes 1 / group of the input channels and makes 1 / group of the + * output channels. Concretely 4 input channels, 8 output channels, and + * 2 groups separate input channels 1-2 and output channels 1-4 into the + * first group and input channels 3-4 and output channels 5-8 into the second + * group. + * - bias_term (\b optional, default true). Whether to have a bias. + * - engine: convolution has CAFFE (matrix multiplication) and CUDNN (library + * kernels + stream parallelism) engines. + */ + explicit ConvolutionLayer(const LayerParameter& param) + : BaseConvolutionLayer(param) {} + + virtual inline const char* type() const { return "Convolution"; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual inline bool reverse_dimensions() { return false; } + virtual void compute_output_shape(); +}; + +} // namespace caffe + +#endif // CAFFE_CONV_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_conv_layer.hpp b/include/caffe/layers/cudnn_conv_layer.hpp new file mode 100644 index 00000000000..31fe49a71fa --- /dev/null +++ b/include/caffe/layers/cudnn_conv_layer.hpp @@ -0,0 +1,72 @@ +#ifndef CAFFE_CUDNN_CONV_LAYER_HPP_ +#define CAFFE_CUDNN_CONV_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/conv_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +/* + * @brief cuDNN implementation of ConvolutionLayer. + * Fallback to ConvolutionLayer for CPU mode. + * + * cuDNN accelerates convolution through forward kernels for filtering and bias + * plus backward kernels for the gradient w.r.t. the filters, biases, and + * inputs. Caffe + cuDNN further speeds up the computation through forward + * parallelism across groups and backward parallelism across gradients. + * + * The CUDNN engine does not have memory overhead for matrix buffers. For many + * input and filter regimes the CUDNN engine is faster than the CAFFE engine, + * but for fully-convolutional models and large inputs the CAFFE engine can be + * faster as long as it fits in memory. +*/ +template +class CuDNNConvolutionLayer : public ConvolutionLayer { + public: + explicit CuDNNConvolutionLayer(const LayerParameter& param) + : ConvolutionLayer(param), handles_setup_(false) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNConvolutionLayer(); + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t* handle_; + cudaStream_t* stream_; + + // algorithms for forward and backwards convolutions + cudnnConvolutionFwdAlgo_t *fwd_algo_; + cudnnConvolutionBwdFilterAlgo_t *bwd_filter_algo_; + cudnnConvolutionBwdDataAlgo_t *bwd_data_algo_; + + vector bottom_descs_, top_descs_; + cudnnTensorDescriptor_t bias_desc_; + cudnnFilterDescriptor_t filter_desc_; + vector conv_descs_; + int bottom_offset_, top_offset_, bias_offset_; + + size_t *workspace_fwd_sizes_; + size_t *workspace_bwd_data_sizes_; + size_t *workspace_bwd_filter_sizes_; + size_t workspaceSizeInBytes; // size of underlying storage + void *workspaceData; // underlying storage + void **workspace; // aliases into workspaceData +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_CONV_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_lcn_layer.hpp b/include/caffe/layers/cudnn_lcn_layer.hpp new file mode 100644 index 00000000000..74cf4775e51 --- /dev/null +++ b/include/caffe/layers/cudnn_lcn_layer.hpp @@ -0,0 +1,49 @@ +#ifndef CAFFE_CUDNN_LCN_LAYER_HPP_ +#define CAFFE_CUDNN_LCN_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/lrn_layer.hpp" +#include "caffe/layers/power_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +template +class CuDNNLCNLayer : public LRNLayer { + public: + explicit CuDNNLCNLayer(const LayerParameter& param) + : LRNLayer(param), handles_setup_(false), tempDataSize(0), + tempData1(NULL), tempData2(NULL) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNLCNLayer(); + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t handle_; + cudnnLRNDescriptor_t norm_desc_; + cudnnTensorDescriptor_t bottom_desc_, top_desc_; + + int size_, pre_pad_; + Dtype alpha_, beta_, k_; + + size_t tempDataSize; + void *tempData1, *tempData2; +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_LCN_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_lrn_layer.hpp b/include/caffe/layers/cudnn_lrn_layer.hpp new file mode 100644 index 00000000000..000ccc36507 --- /dev/null +++ b/include/caffe/layers/cudnn_lrn_layer.hpp @@ -0,0 +1,44 @@ +#ifndef CAFFE_CUDNN_LRN_LAYER_HPP_ +#define CAFFE_CUDNN_LRN_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/lrn_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +template +class CuDNNLRNLayer : public LRNLayer { + public: + explicit CuDNNLRNLayer(const LayerParameter& param) + : LRNLayer(param), handles_setup_(false) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNLRNLayer(); + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t handle_; + cudnnLRNDescriptor_t norm_desc_; + cudnnTensorDescriptor_t bottom_desc_, top_desc_; + + int size_; + Dtype alpha_, beta_, k_; +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_LRN_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_pooling_layer.hpp b/include/caffe/layers/cudnn_pooling_layer.hpp new file mode 100644 index 00000000000..6d0db47d660 --- /dev/null +++ b/include/caffe/layers/cudnn_pooling_layer.hpp @@ -0,0 +1,49 @@ +#ifndef CAFFE_CUDNN_POOLING_LAYER_HPP_ +#define CAFFE_CUDNN_POOLING_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/pooling_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +/* + * @brief cuDNN implementation of PoolingLayer. + * Fallback to PoolingLayer for CPU mode. +*/ +template +class CuDNNPoolingLayer : public PoolingLayer { + public: + explicit CuDNNPoolingLayer(const LayerParameter& param) + : PoolingLayer(param), handles_setup_(false) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNPoolingLayer(); + // Currently, cuDNN does not support the extra top blob. + virtual inline int MinTopBlobs() const { return -1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t handle_; + cudnnTensorDescriptor_t bottom_desc_, top_desc_; + cudnnPoolingDescriptor_t pooling_desc_; + cudnnPoolingMode_t mode_; +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_POOLING_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_relu_layer.hpp b/include/caffe/layers/cudnn_relu_layer.hpp new file mode 100644 index 00000000000..e01f568abc9 --- /dev/null +++ b/include/caffe/layers/cudnn_relu_layer.hpp @@ -0,0 +1,45 @@ +#ifndef CAFFE_CUDNN_RELU_LAYER_HPP_ +#define CAFFE_CUDNN_RELU_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" +#include "caffe/layers/relu_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +/** + * @brief CuDNN acceleration of ReLULayer. + */ +template +class CuDNNReLULayer : public ReLULayer { + public: + explicit CuDNNReLULayer(const LayerParameter& param) + : ReLULayer(param), handles_setup_(false) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNReLULayer(); + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t handle_; + cudnnTensorDescriptor_t bottom_desc_; + cudnnTensorDescriptor_t top_desc_; +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_RELU_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_sigmoid_layer.hpp b/include/caffe/layers/cudnn_sigmoid_layer.hpp new file mode 100644 index 00000000000..9c597958b0b --- /dev/null +++ b/include/caffe/layers/cudnn_sigmoid_layer.hpp @@ -0,0 +1,45 @@ +#ifndef CAFFE_CUDNN_SIGMOID_LAYER_HPP_ +#define CAFFE_CUDNN_SIGMOID_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" +#include "caffe/layers/sigmoid_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +/** + * @brief CuDNN acceleration of SigmoidLayer. + */ +template +class CuDNNSigmoidLayer : public SigmoidLayer { + public: + explicit CuDNNSigmoidLayer(const LayerParameter& param) + : SigmoidLayer(param), handles_setup_(false) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNSigmoidLayer(); + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t handle_; + cudnnTensorDescriptor_t bottom_desc_; + cudnnTensorDescriptor_t top_desc_; +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_SIGMOID_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_softmax_layer.hpp b/include/caffe/layers/cudnn_softmax_layer.hpp new file mode 100644 index 00000000000..174368e413d --- /dev/null +++ b/include/caffe/layers/cudnn_softmax_layer.hpp @@ -0,0 +1,45 @@ +#ifndef CAFFE_CUDNN_SOFTMAX_LAYER_HPP_ +#define CAFFE_CUDNN_SOFTMAX_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/softmax_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +/** + * @brief cuDNN implementation of SoftmaxLayer. + * Fallback to SoftmaxLayer for CPU mode. + */ +template +class CuDNNSoftmaxLayer : public SoftmaxLayer { + public: + explicit CuDNNSoftmaxLayer(const LayerParameter& param) + : SoftmaxLayer(param), handles_setup_(false) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNSoftmaxLayer(); + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t handle_; + cudnnTensorDescriptor_t bottom_desc_; + cudnnTensorDescriptor_t top_desc_; +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_SOFTMAX_LAYER_HPP_ diff --git a/include/caffe/layers/cudnn_tanh_layer.hpp b/include/caffe/layers/cudnn_tanh_layer.hpp new file mode 100644 index 00000000000..c0f0053f71e --- /dev/null +++ b/include/caffe/layers/cudnn_tanh_layer.hpp @@ -0,0 +1,45 @@ +#ifndef CAFFE_CUDNN_TANH_LAYER_HPP_ +#define CAFFE_CUDNN_TANH_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" +#include "caffe/layers/tanh_layer.hpp" + +namespace caffe { + +#ifdef USE_CUDNN +/** + * @brief CuDNN acceleration of TanHLayer. + */ +template +class CuDNNTanHLayer : public TanHLayer { + public: + explicit CuDNNTanHLayer(const LayerParameter& param) + : TanHLayer(param), handles_setup_(false) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual ~CuDNNTanHLayer(); + + protected: + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool handles_setup_; + cudnnHandle_t handle_; + cudnnTensorDescriptor_t bottom_desc_; + cudnnTensorDescriptor_t top_desc_; +}; +#endif + +} // namespace caffe + +#endif // CAFFE_CUDNN_TANH_LAYER_HPP_ diff --git a/include/caffe/layers/data_layer.hpp b/include/caffe/layers/data_layer.hpp new file mode 100644 index 00000000000..6c361791a0c --- /dev/null +++ b/include/caffe/layers/data_layer.hpp @@ -0,0 +1,39 @@ +#ifndef CAFFE_DATA_LAYER_HPP_ +#define CAFFE_DATA_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/data_reader.hpp" +#include "caffe/data_transformer.hpp" +#include "caffe/internal_thread.hpp" +#include "caffe/layer.hpp" +#include "caffe/layers/base_data_layer.hpp" +#include "caffe/proto/caffe.pb.h" +#include "caffe/util/db.hpp" + +namespace caffe { + +template +class DataLayer : public BasePrefetchingDataLayer { + public: + explicit DataLayer(const LayerParameter& param); + virtual ~DataLayer(); + virtual void DataLayerSetUp(const vector*>& bottom, + const vector*>& top); + // DataLayer uses DataReader instead for sharing for parallelism + virtual inline bool ShareInParallel() const { return false; } + virtual inline const char* type() const { return "Data"; } + virtual inline int ExactNumBottomBlobs() const { return 0; } + virtual inline int MinTopBlobs() const { return 1; } + virtual inline int MaxTopBlobs() const { return 2; } + + protected: + virtual void load_batch(Batch* batch); + + DataReader reader_; +}; + +} // namespace caffe + +#endif // CAFFE_DATA_LAYER_HPP_ diff --git a/include/caffe/layers/deconv_layer.hpp b/include/caffe/layers/deconv_layer.hpp new file mode 100644 index 00000000000..23ae887e61e --- /dev/null +++ b/include/caffe/layers/deconv_layer.hpp @@ -0,0 +1,51 @@ +#ifndef CAFFE_DECONV_LAYER_HPP_ +#define CAFFE_DECONV_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/base_conv_layer.hpp" + +namespace caffe { + +/** + * @brief Convolve the input with a bank of learned filters, and (optionally) + * add biases, treating filters and convolution parameters in the + * opposite sense as ConvolutionLayer. + * + * ConvolutionLayer computes each output value by dotting an input window with + * a filter; DeconvolutionLayer multiplies each input value by a filter + * elementwise, and sums over the resulting output windows. In other words, + * DeconvolutionLayer is ConvolutionLayer with the forward and backward passes + * reversed. DeconvolutionLayer reuses ConvolutionParameter for its + * parameters, but they take the opposite sense as in ConvolutionLayer (so + * padding is removed from the output rather than added to the input, and + * stride results in upsampling rather than downsampling). + */ +template +class DeconvolutionLayer : public BaseConvolutionLayer { + public: + explicit DeconvolutionLayer(const LayerParameter& param) + : BaseConvolutionLayer(param) {} + + virtual inline const char* type() const { return "Deconvolution"; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual inline bool reverse_dimensions() { return true; } + virtual void compute_output_shape(); +}; + +} // namespace caffe + +#endif // CAFFE_DECONV_LAYER_HPP_ diff --git a/include/caffe/layers/dropout_layer.hpp b/include/caffe/layers/dropout_layer.hpp new file mode 100644 index 00000000000..e83143bc3cc --- /dev/null +++ b/include/caffe/layers/dropout_layer.hpp @@ -0,0 +1,80 @@ +#ifndef CAFFE_DROPOUT_LAYER_HPP_ +#define CAFFE_DROPOUT_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief During training only, sets a random portion of @f$x@f$ to 0, adjusting + * the rest of the vector magnitude accordingly. + * + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ y = |x| @f$ + */ +template +class DropoutLayer : public NeuronLayer { + public: + /** + * @param param provides DropoutParameter dropout_param, + * with DropoutLayer options: + * - dropout_ratio (\b optional, default 0.5). + * Sets the probability @f$ p @f$ that any given unit is dropped. + */ + explicit DropoutLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Dropout"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs. At training time, we have @f$ + * y_{\mbox{train}} = \left\{ + * \begin{array}{ll} + * \frac{x}{1 - p} & \mbox{if } u > p \\ + * 0 & \mbox{otherwise} + * \end{array} \right. + * @f$, where @f$ u \sim U(0, 1)@f$ is generated independently for each + * input at each iteration. At test time, we simply have + * @f$ y_{\mbox{test}} = \mathbb{E}[y_{\mbox{train}}] = x @f$. + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + /// when divided by UINT_MAX, the randomly generated values @f$u\sim U(0,1)@f$ + Blob rand_vec_; + /// the probability @f$ p @f$ of dropping any input + Dtype threshold_; + /// the scale for undropped inputs at train time @f$ 1 / (1 - p) @f$ + Dtype scale_; + unsigned int uint_thres_; +}; + +} // namespace caffe + +#endif // CAFFE_DROPOUT_LAYER_HPP_ diff --git a/include/caffe/layers/dummy_data_layer.hpp b/include/caffe/layers/dummy_data_layer.hpp new file mode 100644 index 00000000000..4180f1d01e4 --- /dev/null +++ b/include/caffe/layers/dummy_data_layer.hpp @@ -0,0 +1,49 @@ +#ifndef CAFFE_DUMMY_DATA_LAYER_HPP_ +#define CAFFE_DUMMY_DATA_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/filler.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Provides data to the Net generated by a Filler. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class DummyDataLayer : public Layer { + public: + explicit DummyDataLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + // Data layers should be shared by multiple solvers in parallel + virtual inline bool ShareInParallel() const { return true; } + // Data layers have no bottoms, so reshaping is trivial. + virtual void Reshape(const vector*>& bottom, + const vector*>& top) {} + + virtual inline const char* type() const { return "DummyData"; } + virtual inline int ExactNumBottomBlobs() const { return 0; } + virtual inline int MinTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + + vector > > fillers_; + vector refill_; +}; + +} // namespace caffe + +#endif // CAFFE_DUMMY_DATA_LAYER_HPP_ diff --git a/include/caffe/layers/eltwise_layer.hpp b/include/caffe/layers/eltwise_layer.hpp new file mode 100644 index 00000000000..091de834362 --- /dev/null +++ b/include/caffe/layers/eltwise_layer.hpp @@ -0,0 +1,51 @@ +#ifndef CAFFE_ELTWISE_LAYER_HPP_ +#define CAFFE_ELTWISE_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Compute elementwise operations, such as product and sum, + * along multiple input Blobs. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class EltwiseLayer : public Layer { + public: + explicit EltwiseLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Eltwise"; } + virtual inline int MinBottomBlobs() const { return 2; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + EltwiseParameter_EltwiseOp op_; + vector coeffs_; + Blob max_idx_; + + bool stable_prod_grad_; +}; + +} // namespace caffe + +#endif // CAFFE_ELTWISE_LAYER_HPP_ diff --git a/include/caffe/layers/embed_layer.hpp b/include/caffe/layers/embed_layer.hpp new file mode 100644 index 00000000000..36137a625b6 --- /dev/null +++ b/include/caffe/layers/embed_layer.hpp @@ -0,0 +1,52 @@ +#ifndef CAFFE_EMBED_LAYER_HPP_ +#define CAFFE_EMBED_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief A layer for learning "embeddings" of one-hot vector input. + * Equivalent to an InnerProductLayer with one-hot vectors as input, but + * for efficiency the input is the "hot" index of each column itself. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class EmbedLayer : public Layer { + public: + explicit EmbedLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Embed"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int M_; + int K_; + int N_; + bool bias_term_; + Blob bias_multiplier_; +}; + +} // namespace caffe + +#endif // CAFFE_EMBED_LAYER_HPP_ diff --git a/include/caffe/layers/euclidean_loss_layer.hpp b/include/caffe/layers/euclidean_loss_layer.hpp new file mode 100644 index 00000000000..f564569e27a --- /dev/null +++ b/include/caffe/layers/euclidean_loss_layer.hpp @@ -0,0 +1,107 @@ +#ifndef CAFFE_EUCLIDEAN_LOSS_LAYER_HPP_ +#define CAFFE_EUCLIDEAN_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" + +namespace caffe { + +/** + * @brief Computes the Euclidean (L2) loss @f$ + * E = \frac{1}{2N} \sum\limits_{n=1}^N \left| \left| \hat{y}_n - y_n + * \right| \right|_2^2 @f$ for real-valued regression tasks. + * + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ \hat{y} \in [-\infty, +\infty]@f$ + * -# @f$ (N \times C \times H \times W) @f$ + * the targets @f$ y \in [-\infty, +\infty]@f$ + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed Euclidean loss: @f$ E = + * \frac{1}{2n} \sum\limits_{n=1}^N \left| \left| \hat{y}_n - y_n + * \right| \right|_2^2 @f$ + * + * This can be used for least-squares regression tasks. An InnerProductLayer + * input to a EuclideanLossLayer exactly formulates a linear least squares + * regression problem. With non-zero weight decay the problem becomes one of + * ridge regression -- see src/caffe/test/test_sgd_solver.cpp for a concrete + * example wherein we check that the gradients computed for a Net with exactly + * this structure match hand-computed gradient formulas for ridge regression. + * + * (Note: Caffe, and SGD in general, is certainly \b not the best way to solve + * linear least squares problems! We use it only as an instructive example.) + */ +template +class EuclideanLossLayer : public LossLayer { + public: + explicit EuclideanLossLayer(const LayerParameter& param) + : LossLayer(param), diff_() {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "EuclideanLoss"; } + /** + * Unlike most loss layers, in the EuclideanLossLayer we can backpropagate + * to both inputs -- override to return true and always allow force_backward. + */ + virtual inline bool AllowForceBackward(const int bottom_index) const { + return true; + } + + protected: + /// @copydoc EuclideanLossLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the Euclidean error gradient w.r.t. the inputs. + * + * Unlike other children of LossLayer, EuclideanLossLayer \b can compute + * gradients with respect to the label inputs bottom[1] (but still only will + * if propagate_down[1] is set, due to being produced by learnable parameters + * or if force_backward is set). In fact, this layer is "commutative" -- the + * result is the same regardless of the order of the two bottoms. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, + * as @f$ \lambda @f$ is the coefficient of this layer's output + * @f$\ell_i@f$ in the overall Net loss + * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence + * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. + * (*Assuming that this top Blob is not used as a bottom (input) by any + * other layer of the Net.) + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$\hat{y}@f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial \hat{y}} = + * \frac{1}{n} \sum\limits_{n=1}^N (\hat{y}_n - y_n) + * @f$ if propagate_down[0] + * -# @f$ (N \times C \times H \times W) @f$ + * the targets @f$y@f$; Backward fills their diff with gradients + * @f$ \frac{\partial E}{\partial y} = + * \frac{1}{n} \sum\limits_{n=1}^N (y_n - \hat{y}_n) + * @f$ if propagate_down[1] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + Blob diff_; +}; + +} // namespace caffe + +#endif // CAFFE_EUCLIDEAN_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/exp_layer.hpp b/include/caffe/layers/exp_layer.hpp new file mode 100644 index 00000000000..9fc8c396a74 --- /dev/null +++ b/include/caffe/layers/exp_layer.hpp @@ -0,0 +1,80 @@ +#ifndef CAFFE_EXP_LAYER_HPP_ +#define CAFFE_EXP_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Computes @f$ y = \gamma ^ {\alpha x + \beta} @f$, + * as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$, + * and base @f$ \gamma @f$. + */ +template +class ExpLayer : public NeuronLayer { + public: + /** + * @param param provides ExpParameter exp_param, + * with ExpLayer options: + * - scale (\b optional, default 1) the scale @f$ \alpha @f$ + * - shift (\b optional, default 0) the shift @f$ \beta @f$ + * - base (\b optional, default -1 for a value of @f$ e \approx 2.718 @f$) + * the base @f$ \gamma @f$ + */ + explicit ExpLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Exp"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = \gamma ^ {\alpha x + \beta} + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the exp inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} = + * \frac{\partial E}{\partial y} y \alpha \log_e(gamma) + * @f$ if propagate_down[0] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + Dtype inner_scale_, outer_scale_; +}; + +} // namespace caffe + +#endif // CAFFE_EXP_LAYER_HPP_ diff --git a/include/caffe/layers/filter_layer.hpp b/include/caffe/layers/filter_layer.hpp new file mode 100644 index 00000000000..e040e66612b --- /dev/null +++ b/include/caffe/layers/filter_layer.hpp @@ -0,0 +1,77 @@ +#ifndef CAFFE_FILTER_LAYER_HPP_ +#define CAFFE_FILTER_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Takes two+ Blobs, interprets last Blob as a selector and + * filter remaining Blobs accordingly with selector data (0 means that + * the corresponding item has to be filtered, non-zero means that corresponding + * item needs to stay). + */ +template +class FilterLayer : public Layer { + public: + explicit FilterLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Filter"; } + virtual inline int MinBottomBlobs() const { return 2; } + virtual inline int MinTopBlobs() const { return 1; } + + protected: + /** + * @param bottom input Blob vector (length 2+) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs to be filtered @f$ x_1 @f$ + * -# ... + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs to be filtered @f$ x_K @f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the selector blob + * @param top output Blob vector (length 1+) + * -# @f$ (S \times C \times H \times W) @f$ () + * the filtered output @f$ x_1 @f$ + * where S is the number of items + * that haven't been filtered + * @f$ (S \times C \times H \times W) @f$ + * the filtered output @f$ x_K @f$ + * where S is the number of items + * that haven't been filtered + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the forwarded inputs. + * + * @param top output Blob vector (length 1+), providing the error gradient with + * respect to the outputs + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 2+), into which the top error + * gradient is copied + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool first_reshape_; + vector indices_to_forward_; +}; + +} // namespace caffe + +#endif // CAFFE_FILTER_LAYER_HPP_ diff --git a/include/caffe/layers/flatten_layer.hpp b/include/caffe/layers/flatten_layer.hpp new file mode 100644 index 00000000000..e494bbb588f --- /dev/null +++ b/include/caffe/layers/flatten_layer.hpp @@ -0,0 +1,61 @@ +#ifndef CAFFE_FLATTEN_LAYER_HPP_ +#define CAFFE_FLATTEN_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Reshapes the input Blob into flat vectors. + * + * Note: because this layer does not change the input values -- merely the + * dimensions -- it can simply copy the input. The copy happens "virtually" + * (thus taking effectively 0 real time) by setting, in Forward, the data + * pointer of the top Blob to that of the bottom Blob (see Blob::ShareData), + * and in Backward, the diff pointer of the bottom Blob to that of the top Blob + * (see Blob::ShareDiff). + */ +template +class FlattenLayer : public Layer { + public: + explicit FlattenLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Flatten"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + /** + * @param bottom input Blob vector (length 2+) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs + * @param top output Blob vector (length 1) + * -# @f$ (N \times CHW \times 1 \times 1) @f$ + * the outputs -- i.e., the (virtually) copied, flattened inputs + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the concatenate inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length K), into which the top error + * gradient is (virtually) copied + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_FLATTEN_LAYER_HPP_ diff --git a/include/caffe/layers/hdf5_data_layer.hpp b/include/caffe/layers/hdf5_data_layer.hpp new file mode 100644 index 00000000000..b04cf8e1940 --- /dev/null +++ b/include/caffe/layers/hdf5_data_layer.hpp @@ -0,0 +1,62 @@ +#ifndef CAFFE_HDF5_DATA_LAYER_HPP_ +#define CAFFE_HDF5_DATA_LAYER_HPP_ + +#include "hdf5.h" + +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/base_data_layer.hpp" + +namespace caffe { + +/** + * @brief Provides data to the Net from HDF5 files. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class HDF5DataLayer : public Layer { + public: + explicit HDF5DataLayer(const LayerParameter& param) + : Layer(param) {} + virtual ~HDF5DataLayer(); + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + // Data layers should be shared by multiple solvers in parallel + virtual inline bool ShareInParallel() const { return true; } + // Data layers have no bottoms, so reshaping is trivial. + virtual void Reshape(const vector*>& bottom, + const vector*>& top) {} + + virtual inline const char* type() const { return "HDF5Data"; } + virtual inline int ExactNumBottomBlobs() const { return 0; } + virtual inline int MinTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + virtual void LoadHDF5FileData(const char* filename); + + std::vector hdf_filenames_; + unsigned int num_files_; + unsigned int current_file_; + hsize_t current_row_; + std::vector > > hdf_blobs_; + std::vector data_permutation_; + std::vector file_permutation_; +}; + +} // namespace caffe + +#endif // CAFFE_HDF5_DATA_LAYER_HPP_ diff --git a/include/caffe/layers/hdf5_output_layer.hpp b/include/caffe/layers/hdf5_output_layer.hpp new file mode 100644 index 00000000000..487d08fc06c --- /dev/null +++ b/include/caffe/layers/hdf5_output_layer.hpp @@ -0,0 +1,64 @@ +#ifndef CAFFE_HDF5_OUTPUT_LAYER_HPP_ +#define CAFFE_HDF5_OUTPUT_LAYER_HPP_ + +#include "hdf5.h" + +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +#define HDF5_DATA_DATASET_NAME "data" +#define HDF5_DATA_LABEL_NAME "label" + +/** + * @brief Write blobs to disk as HDF5 files. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class HDF5OutputLayer : public Layer { + public: + explicit HDF5OutputLayer(const LayerParameter& param) + : Layer(param), file_opened_(false) {} + virtual ~HDF5OutputLayer(); + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + // Data layers should be shared by multiple solvers in parallel + virtual inline bool ShareInParallel() const { return true; } + // Data layers have no bottoms, so reshaping is trivial. + virtual void Reshape(const vector*>& bottom, + const vector*>& top) {} + + virtual inline const char* type() const { return "HDF5Output"; } + // TODO: no limit on the number of blobs + virtual inline int ExactNumBottomBlobs() const { return 2; } + virtual inline int ExactNumTopBlobs() const { return 0; } + + inline std::string file_name() const { return file_name_; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void SaveBlobs(); + + bool file_opened_; + std::string file_name_; + hid_t file_id_; + Blob data_blob_; + Blob label_blob_; +}; + +} // namespace caffe + +#endif // CAFFE_HDF5_OUTPUT_LAYER_HPP_ diff --git a/include/caffe/layers/hinge_loss_layer.hpp b/include/caffe/layers/hinge_loss_layer.hpp new file mode 100644 index 00000000000..54e42bd44da --- /dev/null +++ b/include/caffe/layers/hinge_loss_layer.hpp @@ -0,0 +1,104 @@ +#ifndef CAFFE_HINGE_LOSS_LAYER_HPP_ +#define CAFFE_HINGE_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" + +namespace caffe { + +/** + * @brief Computes the hinge loss for a one-of-many classification task. + * + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ t @f$, a Blob with values in + * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of + * the @f$ K = CHW @f$ classes. In an SVM, @f$ t @f$ is the result of + * taking the inner product @f$ X^T W @f$ of the D-dimensional features + * @f$ X \in \mathcal{R}^{D \times N} @f$ and the learned hyperplane + * parameters @f$ W \in \mathcal{R}^{D \times K} @f$, so a Net with just + * an InnerProductLayer (with num_output = D) providing predictions to a + * HingeLossLayer and no other learnable parameters or losses is + * equivalent to an SVM. + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels @f$ l @f$, an integer-valued Blob with values + * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ + * indicating the correct class label among the @f$ K @f$ classes + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed hinge loss: @f$ E = + * \frac{1}{N} \sum\limits_{n=1}^N \sum\limits_{k=1}^K + * [\max(0, 1 - \delta\{l_n = k\} t_{nk})] ^ p + * @f$, for the @f$ L^p @f$ norm + * (defaults to @f$ p = 1 @f$, the L1 norm; L2 norm, as in L2-SVM, + * is also available), and @f$ + * \delta\{\mathrm{condition}\} = \left\{ + * \begin{array}{lr} + * 1 & \mbox{if condition} \\ + * -1 & \mbox{otherwise} + * \end{array} \right. + * @f$ + * + * In an SVM, @f$ t \in \mathcal{R}^{N \times K} @f$ is the result of taking + * the inner product @f$ X^T W @f$ of the features + * @f$ X \in \mathcal{R}^{D \times N} @f$ + * and the learned hyperplane parameters + * @f$ W \in \mathcal{R}^{D \times K} @f$. So, a Net with just an + * InnerProductLayer (with num_output = @f$k@f$) providing predictions to a + * HingeLossLayer is equivalent to an SVM (assuming it has no other learned + * outside the InnerProductLayer and no other losses outside the + * HingeLossLayer). + */ +template +class HingeLossLayer : public LossLayer { + public: + explicit HingeLossLayer(const LayerParameter& param) + : LossLayer(param) {} + + virtual inline const char* type() const { return "HingeLoss"; } + + protected: + /// @copydoc HingeLossLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the hinge loss error gradient w.r.t. the predictions. + * + * Gradients cannot be computed with respect to the label inputs (bottom[1]), + * so this method ignores bottom[1] and requires !propagate_down[1], crashing + * if propagate_down[1] is set. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, + * as @f$ \lambda @f$ is the coefficient of this layer's output + * @f$\ell_i@f$ in the overall Net loss + * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence + * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. + * (*Assuming that this top Blob is not used as a bottom (input) by any + * other layer of the Net.) + * @param propagate_down see Layer::Backward. + * propagate_down[1] must be false as we can't compute gradients with + * respect to the labels. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$t@f$; Backward computes diff + * @f$ \frac{\partial E}{\partial t} @f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels -- ignored as we can't compute their error gradients + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + + +} // namespace caffe + +#endif // CAFFE_HINGE_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/im2col_layer.hpp b/include/caffe/layers/im2col_layer.hpp new file mode 100644 index 00000000000..1d3b2eb67d1 --- /dev/null +++ b/include/caffe/layers/im2col_layer.hpp @@ -0,0 +1,63 @@ +#ifndef CAFFE_IM2COL_LAYER_HPP_ +#define CAFFE_IM2COL_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief A helper for image operations that rearranges image regions into + * column vectors. Used by ConvolutionLayer to perform convolution + * by matrix multiplication. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class Im2colLayer : public Layer { + public: + explicit Im2colLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Im2col"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + /// @brief The spatial dimensions of a filter kernel. + Blob kernel_shape_; + /// @brief The spatial dimensions of the stride. + Blob stride_; + /// @brief The spatial dimensions of the padding. + Blob pad_; + + int num_spatial_axes_; + int bottom_dim_; + int top_dim_; + + int channel_axis_; + int num_; + int channels_; + + bool force_nd_im2col_; +}; + +} // namespace caffe + +#endif // CAFFE_IM2COL_LAYER_HPP_ diff --git a/include/caffe/layers/image_data_layer.hpp b/include/caffe/layers/image_data_layer.hpp new file mode 100644 index 00000000000..a0d3384e4c9 --- /dev/null +++ b/include/caffe/layers/image_data_layer.hpp @@ -0,0 +1,47 @@ +#ifndef CAFFE_IMAGE_DATA_LAYER_HPP_ +#define CAFFE_IMAGE_DATA_LAYER_HPP_ + +#include +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/data_transformer.hpp" +#include "caffe/internal_thread.hpp" +#include "caffe/layer.hpp" +#include "caffe/layers/base_data_layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Provides data to the Net from image files. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class ImageDataLayer : public BasePrefetchingDataLayer { + public: + explicit ImageDataLayer(const LayerParameter& param) + : BasePrefetchingDataLayer(param) {} + virtual ~ImageDataLayer(); + virtual void DataLayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "ImageData"; } + virtual inline int ExactNumBottomBlobs() const { return 0; } + virtual inline int ExactNumTopBlobs() const { return 2; } + + protected: + shared_ptr prefetch_rng_; + virtual void ShuffleImages(); + virtual void load_batch(Batch* batch); + + vector > lines_; + int lines_id_; +}; + + +} // namespace caffe + +#endif // CAFFE_IMAGE_DATA_LAYER_HPP_ diff --git a/include/caffe/layers/infogain_loss_layer.hpp b/include/caffe/layers/infogain_loss_layer.hpp new file mode 100644 index 00000000000..633f339a28e --- /dev/null +++ b/include/caffe/layers/infogain_loss_layer.hpp @@ -0,0 +1,110 @@ +#ifndef CAFFE_INFOGAIN_LOSS_LAYER_HPP_ +#define CAFFE_INFOGAIN_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" + +namespace caffe { + +/** + * @brief A generalization of MultinomialLogisticLossLayer that takes an + * "information gain" (infogain) matrix specifying the "value" of all label + * pairs. + * + * Equivalent to the MultinomialLogisticLossLayer if the infogain matrix is the + * identity. + * + * @param bottom input Blob vector (length 2-3) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ \hat{p} @f$, a Blob with values in + * @f$ [0, 1] @f$ indicating the predicted probability of each of the + * @f$ K = CHW @f$ classes. Each prediction vector @f$ \hat{p}_n @f$ + * should sum to 1 as in a probability distribution: @f$ + * \forall n \sum\limits_{k=1}^K \hat{p}_{nk} = 1 @f$. + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels @f$ l @f$, an integer-valued Blob with values + * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ + * indicating the correct class label among the @f$ K @f$ classes + * -# @f$ (1 \times 1 \times K \times K) @f$ + * (\b optional) the infogain matrix @f$ H @f$. This must be provided as + * the third bottom blob input if not provided as the infogain_mat in the + * InfogainLossParameter. If @f$ H = I @f$, this layer is equivalent to the + * MultinomialLogisticLossLayer. + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed infogain multinomial logistic loss: @f$ E = + * \frac{-1}{N} \sum\limits_{n=1}^N H_{l_n} \log(\hat{p}_n) = + * \frac{-1}{N} \sum\limits_{n=1}^N \sum\limits_{k=1}^{K} H_{l_n,k} + * \log(\hat{p}_{n,k}) + * @f$, where @f$ H_{l_n} @f$ denotes row @f$l_n@f$ of @f$H@f$. + */ +template +class InfogainLossLayer : public LossLayer { + public: + explicit InfogainLossLayer(const LayerParameter& param) + : LossLayer(param), infogain_() {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + // InfogainLossLayer takes 2-3 bottom Blobs; if there are 3 the third should + // be the infogain matrix. (Otherwise the infogain matrix is loaded from a + // file specified by LayerParameter.) + virtual inline int ExactNumBottomBlobs() const { return -1; } + virtual inline int MinBottomBlobs() const { return 2; } + virtual inline int MaxBottomBlobs() const { return 3; } + + virtual inline const char* type() const { return "InfogainLoss"; } + + protected: + /// @copydoc InfogainLossLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the infogain loss error gradient w.r.t. the predictions. + * + * Gradients cannot be computed with respect to the label inputs (bottom[1]), + * so this method ignores bottom[1] and requires !propagate_down[1], crashing + * if propagate_down[1] is set. (The same applies to the infogain matrix, if + * provided as bottom[2] rather than in the layer_param.) + * + * @param top output Blob vector (length 1), providing the error gradient + * with respect to the outputs + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, + * as @f$ \lambda @f$ is the coefficient of this layer's output + * @f$\ell_i@f$ in the overall Net loss + * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence + * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. + * (*Assuming that this top Blob is not used as a bottom (input) by any + * other layer of the Net.) + * @param propagate_down see Layer::Backward. + * propagate_down[1] must be false as we can't compute gradients with + * respect to the labels (similarly for propagate_down[2] and the + * infogain matrix, if provided as bottom[2]) + * @param bottom input Blob vector (length 2-3) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ \hat{p} @f$; Backward computes diff + * @f$ \frac{\partial E}{\partial \hat{p}} @f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels -- ignored as we can't compute their error gradients + * -# @f$ (1 \times 1 \times K \times K) @f$ + * (\b optional) the information gain matrix -- ignored as its error + * gradient computation is not implemented. + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + Blob infogain_; +}; + +} // namespace caffe + +#endif // CAFFE_INFOGAIN_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/inner_product_layer.hpp b/include/caffe/layers/inner_product_layer.hpp new file mode 100644 index 00000000000..250576a4817 --- /dev/null +++ b/include/caffe/layers/inner_product_layer.hpp @@ -0,0 +1,51 @@ +#ifndef CAFFE_INNER_PRODUCT_LAYER_HPP_ +#define CAFFE_INNER_PRODUCT_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Also known as a "fully-connected" layer, computes an inner product + * with a set of learned weights, and (optionally) adds biases. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class InnerProductLayer : public Layer { + public: + explicit InnerProductLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "InnerProduct"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int M_; + int K_; + int N_; + bool bias_term_; + Blob bias_multiplier_; +}; + +} // namespace caffe + +#endif // CAFFE_INNER_PRODUCT_LAYER_HPP_ diff --git a/include/caffe/layers/log_layer.hpp b/include/caffe/layers/log_layer.hpp new file mode 100644 index 00000000000..7d037d2bdca --- /dev/null +++ b/include/caffe/layers/log_layer.hpp @@ -0,0 +1,82 @@ +#ifndef CAFFE_LOG_LAYER_HPP_ +#define CAFFE_LOG_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Computes @f$ y = log_{\gamma}(\alpha x + \beta) @f$, + * as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$, + * and base @f$ \gamma @f$. + */ +template +class LogLayer : public NeuronLayer { + public: + /** + * @param param provides LogParameter log_param, + * with LogLayer options: + * - scale (\b optional, default 1) the scale @f$ \alpha @f$ + * - shift (\b optional, default 0) the shift @f$ \beta @f$ + * - base (\b optional, default -1 for a value of @f$ e \approx 2.718 @f$) + * the base @f$ \gamma @f$ + */ + explicit LogLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Log"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = log_{\gamma}(\alpha x + \beta) + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the exp inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} = + * \frac{\partial E}{\partial y} y \alpha \log_e(gamma) + * @f$ if propagate_down[0] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + Dtype base_scale_; + Dtype input_scale_, input_shift_; + Dtype backward_num_scale_; +}; + +} // namespace caffe + +#endif // CAFFE_LOG_LAYER_HPP_ diff --git a/include/caffe/layers/loss_layer.hpp b/include/caffe/layers/loss_layer.hpp new file mode 100644 index 00000000000..dbdf612c062 --- /dev/null +++ b/include/caffe/layers/loss_layer.hpp @@ -0,0 +1,53 @@ +#ifndef CAFFE_LOSS_LAYER_HPP_ +#define CAFFE_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +const float kLOG_THRESHOLD = 1e-20; + +/** + * @brief An interface for Layer%s that take two Blob%s as input -- usually + * (1) predictions and (2) ground-truth labels -- and output a + * singleton Blob representing the loss. + * + * LossLayers are typically only capable of backpropagating to their first input + * -- the predictions. + */ +template +class LossLayer : public Layer { + public: + explicit LossLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp( + const vector*>& bottom, const vector*>& top); + virtual void Reshape( + const vector*>& bottom, const vector*>& top); + + virtual inline int ExactNumBottomBlobs() const { return 2; } + + /** + * @brief For convenience and backwards compatibility, instruct the Net to + * automatically allocate a single top Blob for LossLayers, into which + * they output their singleton loss, (even if the user didn't specify + * one in the prototxt, etc.). + */ + virtual inline bool AutoTopBlobs() const { return true; } + virtual inline int ExactNumTopBlobs() const { return 1; } + /** + * We usually cannot backpropagate to the labels; ignore force_backward for + * these inputs. + */ + virtual inline bool AllowForceBackward(const int bottom_index) const { + return bottom_index != 1; + } +}; + +} // namespace caffe + +#endif // CAFFE_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/lrn_layer.hpp b/include/caffe/layers/lrn_layer.hpp new file mode 100644 index 00000000000..06cf71a94cb --- /dev/null +++ b/include/caffe/layers/lrn_layer.hpp @@ -0,0 +1,94 @@ +#ifndef CAFFE_LRN_LAYER_HPP_ +#define CAFFE_LRN_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/eltwise_layer.hpp" +#include "caffe/layers/pooling_layer.hpp" +#include "caffe/layers/power_layer.hpp" +#include "caffe/layers/split_layer.hpp" + +namespace caffe { + +/** + * @brief Normalize the input in a local region across or within feature maps. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class LRNLayer : public Layer { + public: + explicit LRNLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "LRN"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + virtual void CrossChannelForward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void CrossChannelForward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void WithinChannelForward(const vector*>& bottom, + const vector*>& top); + virtual void CrossChannelBackward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void CrossChannelBackward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void WithinChannelBackward(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int size_; + int pre_pad_; + Dtype alpha_; + Dtype beta_; + Dtype k_; + int num_; + int channels_; + int height_; + int width_; + + // Fields used for normalization ACROSS_CHANNELS + // scale_ stores the intermediate summing results + Blob scale_; + + // Fields used for normalization WITHIN_CHANNEL + shared_ptr > split_layer_; + vector*> split_top_vec_; + shared_ptr > square_layer_; + Blob square_input_; + Blob square_output_; + vector*> square_bottom_vec_; + vector*> square_top_vec_; + shared_ptr > pool_layer_; + Blob pool_output_; + vector*> pool_top_vec_; + shared_ptr > power_layer_; + Blob power_output_; + vector*> power_top_vec_; + shared_ptr > product_layer_; + Blob product_input_; + vector*> product_bottom_vec_; +}; + +} // namespace caffe + +#endif // CAFFE_LRN_LAYER_HPP_ diff --git a/include/caffe/layers/memory_data_layer.hpp b/include/caffe/layers/memory_data_layer.hpp new file mode 100644 index 00000000000..8abcc8c1b68 --- /dev/null +++ b/include/caffe/layers/memory_data_layer.hpp @@ -0,0 +1,63 @@ +#ifndef CAFFE_MEMORY_DATA_LAYER_HPP_ +#define CAFFE_MEMORY_DATA_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/base_data_layer.hpp" + +namespace caffe { + +/** + * @brief Provides data to the Net from memory. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class MemoryDataLayer : public BaseDataLayer { + public: + explicit MemoryDataLayer(const LayerParameter& param) + : BaseDataLayer(param), has_new_data_(false) {} + virtual void DataLayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "MemoryData"; } + virtual inline int ExactNumBottomBlobs() const { return 0; } + virtual inline int ExactNumTopBlobs() const { return 2; } + + virtual void AddDatumVector(const vector& datum_vector); +#ifdef USE_OPENCV + virtual void AddMatVector(const vector& mat_vector, + const vector& labels); +#endif // USE_OPENCV + + // Reset should accept const pointers, but can't, because the memory + // will be given to Blob, which is mutable + void Reset(Dtype* data, Dtype* label, int n); + void set_batch_size(int new_size); + + int batch_size() { return batch_size_; } + int channels() { return channels_; } + int height() { return height_; } + int width() { return width_; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + + int batch_size_, channels_, height_, width_, size_; + Dtype* data_; + Dtype* labels_; + int n_; + size_t pos_; + Blob added_data_; + Blob added_label_; + bool has_new_data_; +}; + +} // namespace caffe + +#endif // CAFFE_MEMORY_DATA_LAYER_HPP_ diff --git a/include/caffe/layers/multinomial_logistic_loss_layer.hpp b/include/caffe/layers/multinomial_logistic_loss_layer.hpp new file mode 100644 index 00000000000..3977cf9ea57 --- /dev/null +++ b/include/caffe/layers/multinomial_logistic_loss_layer.hpp @@ -0,0 +1,92 @@ +#ifndef CAFFE_MULTINOMIAL_LOGISTIC_LOSS_LAYER_HPP_ +#define CAFFE_MULTINOMIAL_LOGISTIC_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" + +namespace caffe { + +/** + * @brief Computes the multinomial logistic loss for a one-of-many + * classification task, directly taking a predicted probability + * distribution as input. + * + * When predictions are not already a probability distribution, you should + * instead use the SoftmaxWithLossLayer, which maps predictions to a + * distribution using the SoftmaxLayer, before computing the multinomial + * logistic loss. The SoftmaxWithLossLayer should be preferred over separate + * SoftmaxLayer + MultinomialLogisticLossLayer + * as its gradient computation is more numerically stable. + * + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ \hat{p} @f$, a Blob with values in + * @f$ [0, 1] @f$ indicating the predicted probability of each of the + * @f$ K = CHW @f$ classes. Each prediction vector @f$ \hat{p}_n @f$ + * should sum to 1 as in a probability distribution: @f$ + * \forall n \sum\limits_{k=1}^K \hat{p}_{nk} = 1 @f$. + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels @f$ l @f$, an integer-valued Blob with values + * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ + * indicating the correct class label among the @f$ K @f$ classes + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed multinomial logistic loss: @f$ E = + * \frac{-1}{N} \sum\limits_{n=1}^N \log(\hat{p}_{n,l_n}) + * @f$ + */ +template +class MultinomialLogisticLossLayer : public LossLayer { + public: + explicit MultinomialLogisticLossLayer(const LayerParameter& param) + : LossLayer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "MultinomialLogisticLoss"; } + + protected: + /// @copydoc MultinomialLogisticLossLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the multinomial logistic loss error gradient w.r.t. the + * predictions. + * + * Gradients cannot be computed with respect to the label inputs (bottom[1]), + * so this method ignores bottom[1] and requires !propagate_down[1], crashing + * if propagate_down[1] is set. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, + * as @f$ \lambda @f$ is the coefficient of this layer's output + * @f$\ell_i@f$ in the overall Net loss + * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence + * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. + * (*Assuming that this top Blob is not used as a bottom (input) by any + * other layer of the Net.) + * @param propagate_down see Layer::Backward. + * propagate_down[1] must be false as we can't compute gradients with + * respect to the labels. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ \hat{p} @f$; Backward computes diff + * @f$ \frac{\partial E}{\partial \hat{p}} @f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels -- ignored as we can't compute their error gradients + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_MULTINOMIAL_LOGISTIC_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/mvn_layer.hpp b/include/caffe/layers/mvn_layer.hpp new file mode 100644 index 00000000000..3a235ceca64 --- /dev/null +++ b/include/caffe/layers/mvn_layer.hpp @@ -0,0 +1,48 @@ +#ifndef CAFFE_MVN_LAYER_HPP_ +#define CAFFE_MVN_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Normalizes the input to have 0-mean and/or unit (1) variance. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class MVNLayer : public Layer { + public: + explicit MVNLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "MVN"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + Blob mean_, variance_, temp_; + + /// sum_multiplier is used to carry out sum using BLAS + Blob sum_multiplier_; + Dtype eps_; +}; + +} // namespace caffe + +#endif // CAFFE_MVN_LAYER_HPP_ diff --git a/include/caffe/layers/neuron_layer.hpp b/include/caffe/layers/neuron_layer.hpp new file mode 100644 index 00000000000..10c108ce682 --- /dev/null +++ b/include/caffe/layers/neuron_layer.hpp @@ -0,0 +1,32 @@ +#ifndef CAFFE_NEURON_LAYER_HPP_ +#define CAFFE_NEURON_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief An interface for layers that take one blob as input (@f$ x @f$) + * and produce one equally-sized blob as output (@f$ y @f$), where + * each element of the output depends only on the corresponding input + * element. + */ +template +class NeuronLayer : public Layer { + public: + explicit NeuronLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } +}; + +} // namespace caffe + +#endif // CAFFE_NEURON_LAYER_HPP_ diff --git a/include/caffe/layers/pooling_layer.hpp b/include/caffe/layers/pooling_layer.hpp new file mode 100644 index 00000000000..f4d6803ba8e --- /dev/null +++ b/include/caffe/layers/pooling_layer.hpp @@ -0,0 +1,60 @@ +#ifndef CAFFE_POOLING_LAYER_HPP_ +#define CAFFE_POOLING_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Pools the input image by taking the max, average, etc. within regions. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class PoolingLayer : public Layer { + public: + explicit PoolingLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Pooling"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int MinTopBlobs() const { return 1; } + // MAX POOL layers can output an extra top blob for the mask; + // others can only output the pooled inputs. + virtual inline int MaxTopBlobs() const { + return (this->layer_param_.pooling_param().pool() == + PoolingParameter_PoolMethod_MAX) ? 2 : 1; + } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int kernel_h_, kernel_w_; + int stride_h_, stride_w_; + int pad_h_, pad_w_; + int channels_; + int height_, width_; + int pooled_height_, pooled_width_; + bool global_pooling_; + Blob rand_idx_; + Blob max_idx_; +}; + +} // namespace caffe + +#endif // CAFFE_POOLING_LAYER_HPP_ diff --git a/include/caffe/layers/power_layer.hpp b/include/caffe/layers/power_layer.hpp new file mode 100644 index 00000000000..6ecbafcaca8 --- /dev/null +++ b/include/caffe/layers/power_layer.hpp @@ -0,0 +1,89 @@ +#ifndef CAFFE_POWER_LAYER_HPP_ +#define CAFFE_POWER_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Computes @f$ y = (\alpha x + \beta) ^ \gamma @f$, + * as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$, + * and power @f$ \gamma @f$. + */ +template +class PowerLayer : public NeuronLayer { + public: + /** + * @param param provides PowerParameter power_param, + * with PowerLayer options: + * - scale (\b optional, default 1) the scale @f$ \alpha @f$ + * - shift (\b optional, default 0) the shift @f$ \beta @f$ + * - power (\b optional, default 1) the power @f$ \gamma @f$ + */ + explicit PowerLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Power"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = (\alpha x + \beta) ^ \gamma + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the power inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} = + * \frac{\partial E}{\partial y} + * \alpha \gamma (\alpha x + \beta) ^ {\gamma - 1} = + * \frac{\partial E}{\partial y} + * \frac{\alpha \gamma y}{\alpha x + \beta} + * @f$ if propagate_down[0] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + /// @brief @f$ \gamma @f$ from layer_param_.power_param() + Dtype power_; + /// @brief @f$ \alpha @f$ from layer_param_.power_param() + Dtype scale_; + /// @brief @f$ \beta @f$ from layer_param_.power_param() + Dtype shift_; + /// @brief Result of @f$ \alpha \gamma @f$ + Dtype diff_scale_; +}; + +} // namespace caffe + +#endif // CAFFE_POWER_LAYER_HPP_ diff --git a/include/caffe/layers/prelu_layer.hpp b/include/caffe/layers/prelu_layer.hpp new file mode 100644 index 00000000000..3ddfb484b66 --- /dev/null +++ b/include/caffe/layers/prelu_layer.hpp @@ -0,0 +1,101 @@ +#ifndef CAFFE_PRELU_LAYER_HPP_ +#define CAFFE_PRELU_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Parameterized Rectified Linear Unit non-linearity @f$ + * y_i = \max(0, x_i) + a_i \min(0, x_i) + * @f$. The differences from ReLULayer are 1) negative slopes are + * learnable though backprop and 2) negative slopes can vary across + * channels. The number of axes of input blob should be greater than or + * equal to 2. The 1st axis (0-based) is seen as channels. + */ +template +class PReLULayer : public NeuronLayer { + public: + /** + * @param param provides PReLUParameter prelu_param, + * with PReLULayer options: + * - filler (\b optional, FillerParameter, + * default {'type': constant 'value':0.25}). + * - channel_shared (\b optional, default false). + * negative slopes are shared across channels. + */ + explicit PReLULayer(const LayerParameter& param) + : NeuronLayer(param) {} + + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "PReLU"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times ...) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times ...) @f$ + * the computed outputs for each channel @f$i@f$ @f$ + * y_i = \max(0, x_i) + a_i \min(0, x_i) + * @f$. + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the PReLU inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times ...) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times ...) @f$ + * the inputs @f$ x @f$; For each channel @f$i@f$, backward fills their + * diff with gradients @f$ + * \frac{\partial E}{\partial x_i} = \left\{ + * \begin{array}{lr} + * a_i \frac{\partial E}{\partial y_i} & \mathrm{if} \; x_i \le 0 \\ + * \frac{\partial E}{\partial y_i} & \mathrm{if} \; x_i > 0 + * \end{array} \right. + * @f$. + * If param_propagate_down_[0] is true, it fills the diff with gradients + * @f$ + * \frac{\partial E}{\partial a_i} = \left\{ + * \begin{array}{lr} + * \sum_{x_i} x_i \frac{\partial E}{\partial y_i} & \mathrm{if} \; x_i \le 0 \\ + * 0 & \mathrm{if} \; x_i > 0 + * \end{array} \right. + * @f$. + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + bool channel_shared_; + Blob multiplier_; // dot multiplier for backward computation of params + Blob backward_buff_; // temporary buffer for backward computation + Blob bottom_memory_; // memory for in-place computation +}; + +} // namespace caffe + +#endif // CAFFE_PRELU_LAYER_HPP_ diff --git a/include/caffe/python_layer.hpp b/include/caffe/layers/python_layer.hpp similarity index 100% rename from include/caffe/python_layer.hpp rename to include/caffe/layers/python_layer.hpp diff --git a/include/caffe/layers/reduction_layer.hpp b/include/caffe/layers/reduction_layer.hpp new file mode 100644 index 00000000000..804a495b11c --- /dev/null +++ b/include/caffe/layers/reduction_layer.hpp @@ -0,0 +1,59 @@ +#ifndef CAFFE_REDUCTION_LAYER_HPP_ +#define CAFFE_REDUCTION_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Compute "reductions" -- operations that return a scalar output Blob + * for an input Blob of arbitrary size, such as the sum, absolute sum, + * and sum of squares. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class ReductionLayer : public Layer { + public: + explicit ReductionLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Reduction"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + /// @brief the reduction operation performed by the layer + ReductionParameter_ReductionOp op_; + /// @brief a scalar coefficient applied to all outputs + Dtype coeff_; + /// @brief the index of the first input axis to reduce + int axis_; + /// @brief the number of reductions performed + int num_; + /// @brief the input size of each reduction + int dim_; + /// @brief a helper Blob used for summation (op_ == SUM) + Blob sum_multiplier_; +}; + +} // namespace caffe + +#endif // CAFFE_REDUCTION_LAYER_HPP_ diff --git a/include/caffe/layers/relu_layer.hpp b/include/caffe/layers/relu_layer.hpp new file mode 100644 index 00000000000..d7a73f7a8d1 --- /dev/null +++ b/include/caffe/layers/relu_layer.hpp @@ -0,0 +1,85 @@ +#ifndef CAFFE_RELU_LAYER_HPP_ +#define CAFFE_RELU_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Rectified Linear Unit non-linearity @f$ y = \max(0, x) @f$. + * The simple max is fast to compute, and the function does not saturate. + */ +template +class ReLULayer : public NeuronLayer { + public: + /** + * @param param provides ReLUParameter relu_param, + * with ReLULayer options: + * - negative_slope (\b optional, default 0). + * the value @f$ \nu @f$ by which negative values are multiplied. + */ + explicit ReLULayer(const LayerParameter& param) + : NeuronLayer(param) {} + + virtual inline const char* type() const { return "ReLU"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = \max(0, x) + * @f$ by default. If a non-zero negative_slope @f$ \nu @f$ is provided, + * the computed outputs are @f$ y = \max(0, x) + \nu \min(0, x) @f$. + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the ReLU inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} = \left\{ + * \begin{array}{lr} + * 0 & \mathrm{if} \; x \le 0 \\ + * \frac{\partial E}{\partial y} & \mathrm{if} \; x > 0 + * \end{array} \right. + * @f$ if propagate_down[0], by default. + * If a non-zero negative_slope @f$ \nu @f$ is provided, + * the computed gradients are @f$ + * \frac{\partial E}{\partial x} = \left\{ + * \begin{array}{lr} + * \nu \frac{\partial E}{\partial y} & \mathrm{if} \; x \le 0 \\ + * \frac{\partial E}{\partial y} & \mathrm{if} \; x > 0 + * \end{array} \right. + * @f$. + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_RELU_LAYER_HPP_ diff --git a/include/caffe/layers/reshape_layer.hpp b/include/caffe/layers/reshape_layer.hpp new file mode 100644 index 00000000000..d11e06384ce --- /dev/null +++ b/include/caffe/layers/reshape_layer.hpp @@ -0,0 +1,52 @@ +#ifndef CAFFE_XXX_LAYER_HPP_ +#define CAFFE_XXX_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/* + * @brief Reshapes the input Blob into an arbitrary-sized output Blob. + * + * Note: similarly to FlattenLayer, this layer does not change the input values + * (see FlattenLayer, Blob::ShareData and Blob::ShareDiff). + */ +template +class ReshapeLayer : public Layer { + public: + explicit ReshapeLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Reshape"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top) {} + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top) {} + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) {} + + /// @brief vector of axes indices whose dimensions we'll copy from the bottom + vector copy_axes_; + /// @brief the index of the axis whose dimension we infer, or -1 if none + int inferred_axis_; + /// @brief the product of the "constant" output dimensions + int constant_count_; +}; + +} // namespace caffe + +#endif // CAFFE_XXX_LAYER_HPP_ diff --git a/include/caffe/layers/sigmoid_cross_entropy_loss_layer.hpp b/include/caffe/layers/sigmoid_cross_entropy_loss_layer.hpp new file mode 100644 index 00000000000..598dca5ff2c --- /dev/null +++ b/include/caffe/layers/sigmoid_cross_entropy_loss_layer.hpp @@ -0,0 +1,110 @@ +#ifndef CAFFE_SIGMOID_CROSS_ENTROPY_LOSS_LAYER_HPP_ +#define CAFFE_SIGMOID_CROSS_ENTROPY_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" +#include "caffe/layers/sigmoid_layer.hpp" + +namespace caffe { + +/** + * @brief Computes the cross-entropy (logistic) loss @f$ + * E = \frac{-1}{n} \sum\limits_{n=1}^N \left[ + * p_n \log \hat{p}_n + + * (1 - p_n) \log(1 - \hat{p}_n) + * \right] + * @f$, often used for predicting targets interpreted as probabilities. + * + * This layer is implemented rather than separate + * SigmoidLayer + CrossEntropyLayer + * as its gradient computation is more numerically stable. + * At test time, this layer can be replaced simply by a SigmoidLayer. + * + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the scores @f$ x \in [-\infty, +\infty]@f$, + * which this layer maps to probability predictions + * @f$ \hat{p}_n = \sigma(x_n) \in [0, 1] @f$ + * using the sigmoid function @f$ \sigma(.) @f$ (see SigmoidLayer). + * -# @f$ (N \times C \times H \times W) @f$ + * the targets @f$ y \in [0, 1] @f$ + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed cross-entropy loss: @f$ + * E = \frac{-1}{n} \sum\limits_{n=1}^N \left[ + * p_n \log \hat{p}_n + (1 - p_n) \log(1 - \hat{p}_n) + * \right] + * @f$ + */ +template +class SigmoidCrossEntropyLossLayer : public LossLayer { + public: + explicit SigmoidCrossEntropyLossLayer(const LayerParameter& param) + : LossLayer(param), + sigmoid_layer_(new SigmoidLayer(param)), + sigmoid_output_(new Blob()) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "SigmoidCrossEntropyLoss"; } + + protected: + /// @copydoc SigmoidCrossEntropyLossLayer + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the sigmoid cross-entropy loss error gradient w.r.t. the + * predictions. + * + * Gradients cannot be computed with respect to the target inputs (bottom[1]), + * so this method ignores bottom[1] and requires !propagate_down[1], crashing + * if propagate_down[1] is set. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, + * as @f$ \lambda @f$ is the coefficient of this layer's output + * @f$\ell_i@f$ in the overall Net loss + * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence + * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. + * (*Assuming that this top Blob is not used as a bottom (input) by any + * other layer of the Net.) + * @param propagate_down see Layer::Backward. + * propagate_down[1] must be false as gradient computation with respect + * to the targets is not implemented. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$x@f$; Backward computes diff + * @f$ \frac{\partial E}{\partial x} = + * \frac{1}{n} \sum\limits_{n=1}^N (\hat{p}_n - p_n) + * @f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels -- ignored as we can't compute their error gradients + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + /// The internal SigmoidLayer used to map predictions to probabilities. + shared_ptr > sigmoid_layer_; + /// sigmoid_output stores the output of the SigmoidLayer. + shared_ptr > sigmoid_output_; + /// bottom vector holder to call the underlying SigmoidLayer::Forward + vector*> sigmoid_bottom_vec_; + /// top vector holder to call the underlying SigmoidLayer::Forward + vector*> sigmoid_top_vec_; +}; + +} // namespace caffe + +#endif // CAFFE_SIGMOID_CROSS_ENTROPY_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/sigmoid_layer.hpp b/include/caffe/layers/sigmoid_layer.hpp new file mode 100644 index 00000000000..ac0f6927feb --- /dev/null +++ b/include/caffe/layers/sigmoid_layer.hpp @@ -0,0 +1,71 @@ +#ifndef CAFFE_SIGMOID_LAYER_HPP_ +#define CAFFE_SIGMOID_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Sigmoid function non-linearity @f$ + * y = (1 + \exp(-x))^{-1} + * @f$, a classic choice in neural networks. + * + * Note that the gradient vanishes as the values move away from 0. + * The ReLULayer is often a better choice for this reason. + */ +template +class SigmoidLayer : public NeuronLayer { + public: + explicit SigmoidLayer(const LayerParameter& param) + : NeuronLayer(param) {} + + virtual inline const char* type() const { return "Sigmoid"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = (1 + \exp(-x))^{-1} + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the sigmoid inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} + * = \frac{\partial E}{\partial y} y (1 - y) + * @f$ if propagate_down[0] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_SIGMOID_LAYER_HPP_ diff --git a/include/caffe/layers/silence_layer.hpp b/include/caffe/layers/silence_layer.hpp new file mode 100644 index 00000000000..fba087fcef0 --- /dev/null +++ b/include/caffe/layers/silence_layer.hpp @@ -0,0 +1,43 @@ +#ifndef CAFFE_SILENCE_LAYER_HPP_ +#define CAFFE_SILENCE_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Ignores bottom blobs while producing no top blobs. (This is useful + * to suppress outputs during testing.) + */ +template +class SilenceLayer : public Layer { + public: + explicit SilenceLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top) {} + + virtual inline const char* type() const { return "Silence"; } + virtual inline int MinBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 0; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top) {} + // We can't define Forward_gpu here, since STUB_GPU will provide + // its own definition for CPU_ONLY mode. + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_SILENCE_LAYER_HPP_ diff --git a/include/caffe/layers/slice_layer.hpp b/include/caffe/layers/slice_layer.hpp new file mode 100644 index 00000000000..10a0abb6eeb --- /dev/null +++ b/include/caffe/layers/slice_layer.hpp @@ -0,0 +1,51 @@ +#ifndef CAFFE_SLICE_LAYER_HPP_ +#define CAFFE_SLICE_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Takes a Blob and slices it along either the num or channel dimension, + * outputting multiple sliced Blob results. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class SliceLayer : public Layer { + public: + explicit SliceLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Slice"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int MinTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int count_; + int num_slices_; + int slice_size_; + int slice_axis_; + vector slice_point_; +}; + +} // namespace caffe + +#endif // CAFFE_SLICE_LAYER_HPP_ diff --git a/include/caffe/layers/softmax_layer.hpp b/include/caffe/layers/softmax_layer.hpp new file mode 100644 index 00000000000..c65b8703e43 --- /dev/null +++ b/include/caffe/layers/softmax_layer.hpp @@ -0,0 +1,50 @@ +#ifndef CAFFE_SOFTMAX_LAYER_HPP_ +#define CAFFE_SOFTMAX_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Computes the softmax function. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class SoftmaxLayer : public Layer { + public: + explicit SoftmaxLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Softmax"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int outer_num_; + int inner_num_; + int softmax_axis_; + /// sum_multiplier is used to carry out sum using BLAS + Blob sum_multiplier_; + /// scale is an intermediate Blob to hold temporary results. + Blob scale_; +}; + +} // namespace caffe + +#endif // CAFFE_SOFTMAX_LAYER_HPP_ diff --git a/include/caffe/layers/softmax_loss_layer.hpp b/include/caffe/layers/softmax_loss_layer.hpp new file mode 100644 index 00000000000..f07e8a02cf1 --- /dev/null +++ b/include/caffe/layers/softmax_loss_layer.hpp @@ -0,0 +1,130 @@ +#ifndef CAFFE_SOFTMAX_WITH_LOSS_LAYER_HPP_ +#define CAFFE_SOFTMAX_WITH_LOSS_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/loss_layer.hpp" +#include "caffe/layers/softmax_layer.hpp" + +namespace caffe { + +/** + * @brief Computes the multinomial logistic loss for a one-of-many + * classification task, passing real-valued predictions through a + * softmax to get a probability distribution over classes. + * + * This layer should be preferred over separate + * SoftmaxLayer + MultinomialLogisticLossLayer + * as its gradient computation is more numerically stable. + * At test time, this layer can be replaced simply by a SoftmaxLayer. + * + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ x @f$, a Blob with values in + * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of + * the @f$ K = CHW @f$ classes. This layer maps these scores to a + * probability distribution over classes using the softmax function + * @f$ \hat{p}_{nk} = \exp(x_{nk}) / + * \left[\sum_{k'} \exp(x_{nk'})\right] @f$ (see SoftmaxLayer). + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels @f$ l @f$, an integer-valued Blob with values + * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ + * indicating the correct class label among the @f$ K @f$ classes + * @param top output Blob vector (length 1) + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * the computed cross-entropy classification loss: @f$ E = + * \frac{-1}{N} \sum\limits_{n=1}^N \log(\hat{p}_{n,l_n}) + * @f$, for softmax output class probabilites @f$ \hat{p} @f$ + */ +template +class SoftmaxWithLossLayer : public LossLayer { + public: + /** + * @param param provides LossParameter loss_param, with options: + * - ignore_label (optional) + * Specify a label value that should be ignored when computing the loss. + * - normalize (optional, default true) + * If true, the loss is normalized by the number of (nonignored) labels + * present; otherwise the loss is simply summed over spatial locations. + */ + explicit SoftmaxWithLossLayer(const LayerParameter& param) + : LossLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "SoftmaxWithLoss"; } + virtual inline int ExactNumTopBlobs() const { return -1; } + virtual inline int MinTopBlobs() const { return 1; } + virtual inline int MaxTopBlobs() const { return 2; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + /** + * @brief Computes the softmax loss error gradient w.r.t. the predictions. + * + * Gradients cannot be computed with respect to the label inputs (bottom[1]), + * so this method ignores bottom[1] and requires !propagate_down[1], crashing + * if propagate_down[1] is set. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (1 \times 1 \times 1 \times 1) @f$ + * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, + * as @f$ \lambda @f$ is the coefficient of this layer's output + * @f$\ell_i@f$ in the overall Net loss + * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence + * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. + * (*Assuming that this top Blob is not used as a bottom (input) by any + * other layer of the Net.) + * @param propagate_down see Layer::Backward. + * propagate_down[1] must be false as we can't compute gradients with + * respect to the labels. + * @param bottom input Blob vector (length 2) + * -# @f$ (N \times C \times H \times W) @f$ + * the predictions @f$ x @f$; Backward computes diff + * @f$ \frac{\partial E}{\partial x} @f$ + * -# @f$ (N \times 1 \times 1 \times 1) @f$ + * the labels -- ignored as we can't compute their error gradients + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + /// Read the normalization mode parameter and compute the normalizer based + /// on the blob size. If normalization_mode is VALID, the count of valid + /// outputs will be read from valid_count, unless it is -1 in which case + /// all outputs are assumed to be valid. + virtual Dtype get_normalizer( + LossParameter_NormalizationMode normalization_mode, int valid_count); + + /// The internal SoftmaxLayer used to map predictions to a distribution. + shared_ptr > softmax_layer_; + /// prob stores the output probability predictions from the SoftmaxLayer. + Blob prob_; + /// bottom vector holder used in call to the underlying SoftmaxLayer::Forward + vector*> softmax_bottom_vec_; + /// top vector holder used in call to the underlying SoftmaxLayer::Forward + vector*> softmax_top_vec_; + /// Whether to ignore instances with a certain label. + bool has_ignore_label_; + /// The label indicating that an instance should be ignored. + int ignore_label_; + /// How to normalize the output loss. + LossParameter_NormalizationMode normalization_; + + int softmax_axis_, outer_num_, inner_num_; +}; + +} // namespace caffe + +#endif // CAFFE_SOFTMAX_WITH_LOSS_LAYER_HPP_ diff --git a/include/caffe/layers/split_layer.hpp b/include/caffe/layers/split_layer.hpp new file mode 100644 index 00000000000..8140dfc7c40 --- /dev/null +++ b/include/caffe/layers/split_layer.hpp @@ -0,0 +1,45 @@ +#ifndef CAFFE_SPLIT_LAYER_HPP_ +#define CAFFE_SPLIT_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Creates a "split" path in the network by copying the bottom Blob + * into multiple top Blob%s to be used by multiple consuming layers. + * + * TODO(dox): thorough documentation for Forward, Backward, and proto params. + */ +template +class SplitLayer : public Layer { + public: + explicit SplitLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Split"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int MinTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + int count_; +}; + +} // namespace caffe + +#endif // CAFFE_SPLIT_LAYER_HPP_ diff --git a/include/caffe/layers/spp_layer.hpp b/include/caffe/layers/spp_layer.hpp new file mode 100644 index 00000000000..9f145cc77e3 --- /dev/null +++ b/include/caffe/layers/spp_layer.hpp @@ -0,0 +1,76 @@ +#ifndef CAFFE_SPP_LAYER_HPP_ +#define CAFFE_SPP_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Does spatial pyramid pooling on the input image + * by taking the max, average, etc. within regions + * so that the result vector of different sized + * images are of the same size. + */ +template +class SPPLayer : public Layer { + public: + explicit SPPLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "SPP"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + // calculates the kernel and stride dimensions for the pooling layer, + // returns a correctly configured LayerParameter for a PoolingLayer + virtual LayerParameter GetPoolingParam(const int pyramid_level, + const int bottom_h, const int bottom_w, const SPPParameter spp_param); + + int pyramid_height_; + int bottom_h_, bottom_w_; + int num_; + int channels_; + int kernel_h_, kernel_w_; + int pad_h_, pad_w_; + bool reshaped_first_time_; + + /// the internal Split layer that feeds the pooling layers + shared_ptr > split_layer_; + /// top vector holder used in call to the underlying SplitLayer::Forward + vector*> split_top_vec_; + /// bottom vector holder used in call to the underlying PoolingLayer::Forward + vector*>*> pooling_bottom_vecs_; + /// the internal Pooling layers of different kernel sizes + vector > > pooling_layers_; + /// top vector holders used in call to the underlying PoolingLayer::Forward + vector*>*> pooling_top_vecs_; + /// pooling_outputs stores the outputs of the PoolingLayers + vector*> pooling_outputs_; + /// the internal Flatten layers that the Pooling layers feed into + vector*> flatten_layers_; + /// top vector holders used in call to the underlying FlattenLayer::Forward + vector*>*> flatten_top_vecs_; + /// flatten_outputs stores the outputs of the FlattenLayers + vector*> flatten_outputs_; + /// bottom vector holder used in call to the underlying ConcatLayer::Forward + vector*> concat_bottom_vec_; + /// the internal Concat layers that the Flatten layers feed into + shared_ptr > concat_layer_; +}; + +} // namespace caffe + +#endif // CAFFE_SPP_LAYER_HPP_ diff --git a/include/caffe/layers/tanh_layer.hpp b/include/caffe/layers/tanh_layer.hpp new file mode 100644 index 00000000000..8f95e9322d9 --- /dev/null +++ b/include/caffe/layers/tanh_layer.hpp @@ -0,0 +1,73 @@ +#ifndef CAFFE_TANH_LAYER_HPP_ +#define CAFFE_TANH_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief TanH hyperbolic tangent non-linearity @f$ + * y = \frac{\exp(2x) - 1}{\exp(2x) + 1} + * @f$, popular in auto-encoders. + * + * Note that the gradient vanishes as the values move away from 0. + * The ReLULayer is often a better choice for this reason. + */ +template +class TanHLayer : public NeuronLayer { + public: + explicit TanHLayer(const LayerParameter& param) + : NeuronLayer(param) {} + + virtual inline const char* type() const { return "TanH"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = \frac{\exp(2x) - 1}{\exp(2x) + 1} + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + /** + * @brief Computes the error gradient w.r.t. the sigmoid inputs. + * + * @param top output Blob vector (length 1), providing the error gradient with + * respect to the outputs + * -# @f$ (N \times C \times H \times W) @f$ + * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ + * with respect to computed outputs @f$ y @f$ + * @param propagate_down see Layer::Backward. + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$; Backward fills their diff with + * gradients @f$ + * \frac{\partial E}{\partial x} + * = \frac{\partial E}{\partial y} + * \left(1 - \left[\frac{\exp(2x) - 1}{exp(2x) + 1} \right]^2 \right) + * = \frac{\partial E}{\partial y} (1 - y^2) + * @f$ if propagate_down[0] + */ + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + +} // namespace caffe + +#endif // CAFFE_TANH_LAYER_HPP_ diff --git a/include/caffe/layers/threshold_layer.hpp b/include/caffe/layers/threshold_layer.hpp new file mode 100644 index 00000000000..3bf4db63e5c --- /dev/null +++ b/include/caffe/layers/threshold_layer.hpp @@ -0,0 +1,64 @@ +#ifndef CAFFE_THRESHOLD_LAYER_HPP_ +#define CAFFE_THRESHOLD_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#include "caffe/layers/neuron_layer.hpp" + +namespace caffe { + +/** + * @brief Tests whether the input exceeds a threshold: outputs 1 for inputs + * above threshold; 0 otherwise. + */ +template +class ThresholdLayer : public NeuronLayer { + public: + /** + * @param param provides ThresholdParameter threshold_param, + * with ThresholdLayer options: + * - threshold (\b optional, default 0). + * the threshold value @f$ t @f$ to which the input values are compared. + */ + explicit ThresholdLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Threshold"; } + + protected: + /** + * @param bottom input Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the inputs @f$ x @f$ + * @param top output Blob vector (length 1) + * -# @f$ (N \times C \times H \times W) @f$ + * the computed outputs @f$ + * y = \left\{ + * \begin{array}{lr} + * 0 & \mathrm{if} \; x \le t \\ + * 1 & \mathrm{if} \; x > t + * \end{array} \right. + * @f$ + */ + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + /// @brief Not implemented (non-differentiable function) + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom) { + NOT_IMPLEMENTED; + } + + Dtype threshold_; +}; + +} // namespace caffe + +#endif // CAFFE_THRESHOLD_LAYER_HPP_ diff --git a/include/caffe/layers/tile_layer.hpp b/include/caffe/layers/tile_layer.hpp new file mode 100644 index 00000000000..fbdbe2f0c53 --- /dev/null +++ b/include/caffe/layers/tile_layer.hpp @@ -0,0 +1,43 @@ +#ifndef CAFFE_TILE_LAYER_HPP_ +#define CAFFE_TILE_LAYER_HPP_ + +#include + +#include "caffe/blob.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Copy a Blob along specified dimensions. + */ +template +class TileLayer : public Layer { + public: + explicit TileLayer(const LayerParameter& param) + : Layer(param) {} + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "Tile"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Forward_gpu(const vector*>& bottom, + const vector*>& top); + + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + + unsigned int axis_, tiles_, outer_dim_, inner_dim_; +}; + +} // namespace caffe + +#endif // CAFFE_TILE_LAYER_HPP_ diff --git a/include/caffe/layers/window_data_layer.hpp b/include/caffe/layers/window_data_layer.hpp new file mode 100644 index 00000000000..35f41b80e63 --- /dev/null +++ b/include/caffe/layers/window_data_layer.hpp @@ -0,0 +1,55 @@ +#ifndef CAFFE_WINDOW_DATA_LAYER_HPP_ +#define CAFFE_WINDOW_DATA_LAYER_HPP_ + +#include +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/data_transformer.hpp" +#include "caffe/internal_thread.hpp" +#include "caffe/layer.hpp" +#include "caffe/layers/base_data_layer.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +/** + * @brief Provides data to the Net from windows of images files, specified + * by a window data file. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class WindowDataLayer : public BasePrefetchingDataLayer { + public: + explicit WindowDataLayer(const LayerParameter& param) + : BasePrefetchingDataLayer(param) {} + virtual ~WindowDataLayer(); + virtual void DataLayerSetUp(const vector*>& bottom, + const vector*>& top); + + virtual inline const char* type() const { return "WindowData"; } + virtual inline int ExactNumBottomBlobs() const { return 0; } + virtual inline int ExactNumTopBlobs() const { return 2; } + + protected: + virtual unsigned int PrefetchRand(); + virtual void load_batch(Batch* batch); + + shared_ptr prefetch_rng_; + vector > > image_database_; + enum WindowField { IMAGE_INDEX, LABEL, OVERLAP, X1, Y1, X2, Y2, NUM }; + vector > fg_windows_; + vector > bg_windows_; + Blob data_mean_; + vector mean_values_; + bool has_mean_file_; + bool has_mean_values_; + bool cache_images_; + vector > image_database_cache_; +}; + +} // namespace caffe + +#endif // CAFFE_WINDOW_DATA_LAYER_HPP_ diff --git a/include/caffe/loss_layers.hpp b/include/caffe/loss_layers.hpp deleted file mode 100644 index 53d07025f0c..00000000000 --- a/include/caffe/loss_layers.hpp +++ /dev/null @@ -1,777 +0,0 @@ -#ifndef CAFFE_LOSS_LAYERS_HPP_ -#define CAFFE_LOSS_LAYERS_HPP_ - -#include -#include -#include - -#include "caffe/blob.hpp" -#include "caffe/layer.hpp" -#include "caffe/neuron_layers.hpp" -#include "caffe/proto/caffe.pb.h" - -namespace caffe { - -const float kLOG_THRESHOLD = 1e-20; - -/** - * @brief Computes the classification accuracy for a one-of-many - * classification task. - */ -template -class AccuracyLayer : public Layer { - public: - /** - * @param param provides AccuracyParameter accuracy_param, - * with AccuracyLayer options: - * - top_k (\b optional, default 1). - * Sets the maximum rank @f$ k @f$ at which a prediction is considered - * correct. For example, if @f$ k = 5 @f$, a prediction is counted - * correct if the correct label is among the top 5 predicted labels. - */ - explicit AccuracyLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Accuracy"; } - virtual inline int ExactNumBottomBlobs() const { return 2; } - - // If there are two top blobs, then the second blob will contain - // accuracies per class. - virtual inline int MinTopBlobs() const { return 1; } - virtual inline int MaxTopBlobs() const { return 2; } - - protected: - /** - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ x @f$, a Blob with values in - * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of - * the @f$ K = CHW @f$ classes. Each @f$ x_n @f$ is mapped to a predicted - * label @f$ \hat{l}_n @f$ given by its maximal index: - * @f$ \hat{l}_n = \arg\max\limits_k x_{nk} @f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels @f$ l @f$, an integer-valued Blob with values - * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ - * indicating the correct class label among the @f$ K @f$ classes - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed accuracy: @f$ - * \frac{1}{N} \sum\limits_{n=1}^N \delta\{ \hat{l}_n = l_n \} - * @f$, where @f$ - * \delta\{\mathrm{condition}\} = \left\{ - * \begin{array}{lr} - * 1 & \mbox{if condition} \\ - * 0 & \mbox{otherwise} - * \end{array} \right. - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - - - /// @brief Not implemented -- AccuracyLayer cannot be used as a loss. - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) { - for (int i = 0; i < propagate_down.size(); ++i) { - if (propagate_down[i]) { NOT_IMPLEMENTED; } - } - } - - int label_axis_, outer_num_, inner_num_; - - int top_k_; - - /// Whether to ignore instances with a certain label. - bool has_ignore_label_; - /// The label indicating that an instance should be ignored. - int ignore_label_; - /// Keeps counts of the number of samples per class. - Blob nums_buffer_; -}; - -/** - * @brief An interface for Layer%s that take two Blob%s as input -- usually - * (1) predictions and (2) ground-truth labels -- and output a - * singleton Blob representing the loss. - * - * LossLayers are typically only capable of backpropagating to their first input - * -- the predictions. - */ -template -class LossLayer : public Layer { - public: - explicit LossLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp( - const vector*>& bottom, const vector*>& top); - virtual void Reshape( - const vector*>& bottom, const vector*>& top); - - virtual inline int ExactNumBottomBlobs() const { return 2; } - - /** - * @brief For convenience and backwards compatibility, instruct the Net to - * automatically allocate a single top Blob for LossLayers, into which - * they output their singleton loss, (even if the user didn't specify - * one in the prototxt, etc.). - */ - virtual inline bool AutoTopBlobs() const { return true; } - virtual inline int ExactNumTopBlobs() const { return 1; } - /** - * We usually cannot backpropagate to the labels; ignore force_backward for - * these inputs. - */ - virtual inline bool AllowForceBackward(const int bottom_index) const { - return bottom_index != 1; - } -}; - -/** - * @brief Computes the contrastive loss @f$ - * E = \frac{1}{2N} \sum\limits_{n=1}^N \left(y\right) d^2 + - * \left(1-y\right) \max \left(margin-d, 0\right)^2 - * @f$ where @f$ - * d = \left| \left| a_n - b_n \right| \right|_2 @f$. This can be - * used to train siamese networks. - * - * @param bottom input Blob vector (length 3) - * -# @f$ (N \times C \times 1 \times 1) @f$ - * the features @f$ a \in [-\infty, +\infty]@f$ - * -# @f$ (N \times C \times 1 \times 1) @f$ - * the features @f$ b \in [-\infty, +\infty]@f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the binary similarity @f$ s \in [0, 1]@f$ - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed contrastive loss: @f$ E = - * \frac{1}{2N} \sum\limits_{n=1}^N \left(y\right) d^2 + - * \left(1-y\right) \max \left(margin-d, 0\right)^2 - * @f$ where @f$ - * d = \left| \left| a_n - b_n \right| \right|_2 @f$. - * This can be used to train siamese networks. - */ -template -class ContrastiveLossLayer : public LossLayer { - public: - explicit ContrastiveLossLayer(const LayerParameter& param) - : LossLayer(param), diff_() {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline int ExactNumBottomBlobs() const { return 3; } - virtual inline const char* type() const { return "ContrastiveLoss"; } - /** - * Unlike most loss layers, in the ContrastiveLossLayer we can backpropagate - * to the first two inputs. - */ - virtual inline bool AllowForceBackward(const int bottom_index) const { - return bottom_index != 2; - } - - protected: - /// @copydoc ContrastiveLossLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the Contrastive error gradient w.r.t. the inputs. - * - * Computes the gradients with respect to the two input vectors (bottom[0] and - * bottom[1]), but not the similarity label (bottom[2]). - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, - * as @f$ \lambda @f$ is the coefficient of this layer's output - * @f$\ell_i@f$ in the overall Net loss - * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence - * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. - * (*Assuming that this top Blob is not used as a bottom (input) by any - * other layer of the Net.) - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times 1 \times 1) @f$ - * the features @f$a@f$; Backward fills their diff with - * gradients if propagate_down[0] - * -# @f$ (N \times C \times 1 \times 1) @f$ - * the features @f$b@f$; Backward fills their diff with gradients if - * propagate_down[1] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - Blob diff_; // cached for backward pass - Blob dist_sq_; // cached for backward pass - Blob diff_sq_; // tmp storage for gpu forward pass - Blob summer_vec_; // tmp storage for gpu forward pass -}; - -/** - * @brief Computes the Euclidean (L2) loss @f$ - * E = \frac{1}{2N} \sum\limits_{n=1}^N \left| \left| \hat{y}_n - y_n - * \right| \right|_2^2 @f$ for real-valued regression tasks. - * - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ \hat{y} \in [-\infty, +\infty]@f$ - * -# @f$ (N \times C \times H \times W) @f$ - * the targets @f$ y \in [-\infty, +\infty]@f$ - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed Euclidean loss: @f$ E = - * \frac{1}{2n} \sum\limits_{n=1}^N \left| \left| \hat{y}_n - y_n - * \right| \right|_2^2 @f$ - * - * This can be used for least-squares regression tasks. An InnerProductLayer - * input to a EuclideanLossLayer exactly formulates a linear least squares - * regression problem. With non-zero weight decay the problem becomes one of - * ridge regression -- see src/caffe/test/test_sgd_solver.cpp for a concrete - * example wherein we check that the gradients computed for a Net with exactly - * this structure match hand-computed gradient formulas for ridge regression. - * - * (Note: Caffe, and SGD in general, is certainly \b not the best way to solve - * linear least squares problems! We use it only as an instructive example.) - */ -template -class EuclideanLossLayer : public LossLayer { - public: - explicit EuclideanLossLayer(const LayerParameter& param) - : LossLayer(param), diff_() {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "EuclideanLoss"; } - /** - * Unlike most loss layers, in the EuclideanLossLayer we can backpropagate - * to both inputs -- override to return true and always allow force_backward. - */ - virtual inline bool AllowForceBackward(const int bottom_index) const { - return true; - } - - protected: - /// @copydoc EuclideanLossLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the Euclidean error gradient w.r.t. the inputs. - * - * Unlike other children of LossLayer, EuclideanLossLayer \b can compute - * gradients with respect to the label inputs bottom[1] (but still only will - * if propagate_down[1] is set, due to being produced by learnable parameters - * or if force_backward is set). In fact, this layer is "commutative" -- the - * result is the same regardless of the order of the two bottoms. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, - * as @f$ \lambda @f$ is the coefficient of this layer's output - * @f$\ell_i@f$ in the overall Net loss - * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence - * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. - * (*Assuming that this top Blob is not used as a bottom (input) by any - * other layer of the Net.) - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$\hat{y}@f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial \hat{y}} = - * \frac{1}{n} \sum\limits_{n=1}^N (\hat{y}_n - y_n) - * @f$ if propagate_down[0] - * -# @f$ (N \times C \times H \times W) @f$ - * the targets @f$y@f$; Backward fills their diff with gradients - * @f$ \frac{\partial E}{\partial y} = - * \frac{1}{n} \sum\limits_{n=1}^N (y_n - \hat{y}_n) - * @f$ if propagate_down[1] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - Blob diff_; -}; - -/** - * @brief Computes the hinge loss for a one-of-many classification task. - * - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ t @f$, a Blob with values in - * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of - * the @f$ K = CHW @f$ classes. In an SVM, @f$ t @f$ is the result of - * taking the inner product @f$ X^T W @f$ of the D-dimensional features - * @f$ X \in \mathcal{R}^{D \times N} @f$ and the learned hyperplane - * parameters @f$ W \in \mathcal{R}^{D \times K} @f$, so a Net with just - * an InnerProductLayer (with num_output = D) providing predictions to a - * HingeLossLayer and no other learnable parameters or losses is - * equivalent to an SVM. - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels @f$ l @f$, an integer-valued Blob with values - * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ - * indicating the correct class label among the @f$ K @f$ classes - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed hinge loss: @f$ E = - * \frac{1}{N} \sum\limits_{n=1}^N \sum\limits_{k=1}^K - * [\max(0, 1 - \delta\{l_n = k\} t_{nk})] ^ p - * @f$, for the @f$ L^p @f$ norm - * (defaults to @f$ p = 1 @f$, the L1 norm; L2 norm, as in L2-SVM, - * is also available), and @f$ - * \delta\{\mathrm{condition}\} = \left\{ - * \begin{array}{lr} - * 1 & \mbox{if condition} \\ - * -1 & \mbox{otherwise} - * \end{array} \right. - * @f$ - * - * In an SVM, @f$ t \in \mathcal{R}^{N \times K} @f$ is the result of taking - * the inner product @f$ X^T W @f$ of the features - * @f$ X \in \mathcal{R}^{D \times N} @f$ - * and the learned hyperplane parameters - * @f$ W \in \mathcal{R}^{D \times K} @f$. So, a Net with just an - * InnerProductLayer (with num_output = @f$k@f$) providing predictions to a - * HingeLossLayer is equivalent to an SVM (assuming it has no other learned - * outside the InnerProductLayer and no other losses outside the - * HingeLossLayer). - */ -template -class HingeLossLayer : public LossLayer { - public: - explicit HingeLossLayer(const LayerParameter& param) - : LossLayer(param) {} - - virtual inline const char* type() const { return "HingeLoss"; } - - protected: - /// @copydoc HingeLossLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the hinge loss error gradient w.r.t. the predictions. - * - * Gradients cannot be computed with respect to the label inputs (bottom[1]), - * so this method ignores bottom[1] and requires !propagate_down[1], crashing - * if propagate_down[1] is set. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, - * as @f$ \lambda @f$ is the coefficient of this layer's output - * @f$\ell_i@f$ in the overall Net loss - * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence - * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. - * (*Assuming that this top Blob is not used as a bottom (input) by any - * other layer of the Net.) - * @param propagate_down see Layer::Backward. - * propagate_down[1] must be false as we can't compute gradients with - * respect to the labels. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$t@f$; Backward computes diff - * @f$ \frac{\partial E}{\partial t} @f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels -- ignored as we can't compute their error gradients - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -/** - * @brief A generalization of MultinomialLogisticLossLayer that takes an - * "information gain" (infogain) matrix specifying the "value" of all label - * pairs. - * - * Equivalent to the MultinomialLogisticLossLayer if the infogain matrix is the - * identity. - * - * @param bottom input Blob vector (length 2-3) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ \hat{p} @f$, a Blob with values in - * @f$ [0, 1] @f$ indicating the predicted probability of each of the - * @f$ K = CHW @f$ classes. Each prediction vector @f$ \hat{p}_n @f$ - * should sum to 1 as in a probability distribution: @f$ - * \forall n \sum\limits_{k=1}^K \hat{p}_{nk} = 1 @f$. - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels @f$ l @f$, an integer-valued Blob with values - * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ - * indicating the correct class label among the @f$ K @f$ classes - * -# @f$ (1 \times 1 \times K \times K) @f$ - * (\b optional) the infogain matrix @f$ H @f$. This must be provided as - * the third bottom blob input if not provided as the infogain_mat in the - * InfogainLossParameter. If @f$ H = I @f$, this layer is equivalent to the - * MultinomialLogisticLossLayer. - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed infogain multinomial logistic loss: @f$ E = - * \frac{-1}{N} \sum\limits_{n=1}^N H_{l_n} \log(\hat{p}_n) = - * \frac{-1}{N} \sum\limits_{n=1}^N \sum\limits_{k=1}^{K} H_{l_n,k} - * \log(\hat{p}_{n,k}) - * @f$, where @f$ H_{l_n} @f$ denotes row @f$l_n@f$ of @f$H@f$. - */ -template -class InfogainLossLayer : public LossLayer { - public: - explicit InfogainLossLayer(const LayerParameter& param) - : LossLayer(param), infogain_() {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - // InfogainLossLayer takes 2-3 bottom Blobs; if there are 3 the third should - // be the infogain matrix. (Otherwise the infogain matrix is loaded from a - // file specified by LayerParameter.) - virtual inline int ExactNumBottomBlobs() const { return -1; } - virtual inline int MinBottomBlobs() const { return 2; } - virtual inline int MaxBottomBlobs() const { return 3; } - - virtual inline const char* type() const { return "InfogainLoss"; } - - protected: - /// @copydoc InfogainLossLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the infogain loss error gradient w.r.t. the predictions. - * - * Gradients cannot be computed with respect to the label inputs (bottom[1]), - * so this method ignores bottom[1] and requires !propagate_down[1], crashing - * if propagate_down[1] is set. (The same applies to the infogain matrix, if - * provided as bottom[2] rather than in the layer_param.) - * - * @param top output Blob vector (length 1), providing the error gradient - * with respect to the outputs - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, - * as @f$ \lambda @f$ is the coefficient of this layer's output - * @f$\ell_i@f$ in the overall Net loss - * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence - * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. - * (*Assuming that this top Blob is not used as a bottom (input) by any - * other layer of the Net.) - * @param propagate_down see Layer::Backward. - * propagate_down[1] must be false as we can't compute gradients with - * respect to the labels (similarly for propagate_down[2] and the - * infogain matrix, if provided as bottom[2]) - * @param bottom input Blob vector (length 2-3) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ \hat{p} @f$; Backward computes diff - * @f$ \frac{\partial E}{\partial \hat{p}} @f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels -- ignored as we can't compute their error gradients - * -# @f$ (1 \times 1 \times K \times K) @f$ - * (\b optional) the information gain matrix -- ignored as its error - * gradient computation is not implemented. - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - Blob infogain_; -}; - -/** - * @brief Computes the multinomial logistic loss for a one-of-many - * classification task, directly taking a predicted probability - * distribution as input. - * - * When predictions are not already a probability distribution, you should - * instead use the SoftmaxWithLossLayer, which maps predictions to a - * distribution using the SoftmaxLayer, before computing the multinomial - * logistic loss. The SoftmaxWithLossLayer should be preferred over separate - * SoftmaxLayer + MultinomialLogisticLossLayer - * as its gradient computation is more numerically stable. - * - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ \hat{p} @f$, a Blob with values in - * @f$ [0, 1] @f$ indicating the predicted probability of each of the - * @f$ K = CHW @f$ classes. Each prediction vector @f$ \hat{p}_n @f$ - * should sum to 1 as in a probability distribution: @f$ - * \forall n \sum\limits_{k=1}^K \hat{p}_{nk} = 1 @f$. - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels @f$ l @f$, an integer-valued Blob with values - * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ - * indicating the correct class label among the @f$ K @f$ classes - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed multinomial logistic loss: @f$ E = - * \frac{-1}{N} \sum\limits_{n=1}^N \log(\hat{p}_{n,l_n}) - * @f$ - */ -template -class MultinomialLogisticLossLayer : public LossLayer { - public: - explicit MultinomialLogisticLossLayer(const LayerParameter& param) - : LossLayer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "MultinomialLogisticLoss"; } - - protected: - /// @copydoc MultinomialLogisticLossLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the multinomial logistic loss error gradient w.r.t. the - * predictions. - * - * Gradients cannot be computed with respect to the label inputs (bottom[1]), - * so this method ignores bottom[1] and requires !propagate_down[1], crashing - * if propagate_down[1] is set. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, - * as @f$ \lambda @f$ is the coefficient of this layer's output - * @f$\ell_i@f$ in the overall Net loss - * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence - * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. - * (*Assuming that this top Blob is not used as a bottom (input) by any - * other layer of the Net.) - * @param propagate_down see Layer::Backward. - * propagate_down[1] must be false as we can't compute gradients with - * respect to the labels. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ \hat{p} @f$; Backward computes diff - * @f$ \frac{\partial E}{\partial \hat{p}} @f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels -- ignored as we can't compute their error gradients - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -/** - * @brief Computes the cross-entropy (logistic) loss @f$ - * E = \frac{-1}{n} \sum\limits_{n=1}^N \left[ - * p_n \log \hat{p}_n + - * (1 - p_n) \log(1 - \hat{p}_n) - * \right] - * @f$, often used for predicting targets interpreted as probabilities. - * - * This layer is implemented rather than separate - * SigmoidLayer + CrossEntropyLayer - * as its gradient computation is more numerically stable. - * At test time, this layer can be replaced simply by a SigmoidLayer. - * - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the scores @f$ x \in [-\infty, +\infty]@f$, - * which this layer maps to probability predictions - * @f$ \hat{p}_n = \sigma(x_n) \in [0, 1] @f$ - * using the sigmoid function @f$ \sigma(.) @f$ (see SigmoidLayer). - * -# @f$ (N \times C \times H \times W) @f$ - * the targets @f$ y \in [0, 1] @f$ - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed cross-entropy loss: @f$ - * E = \frac{-1}{n} \sum\limits_{n=1}^N \left[ - * p_n \log \hat{p}_n + (1 - p_n) \log(1 - \hat{p}_n) - * \right] - * @f$ - */ -template -class SigmoidCrossEntropyLossLayer : public LossLayer { - public: - explicit SigmoidCrossEntropyLossLayer(const LayerParameter& param) - : LossLayer(param), - sigmoid_layer_(new SigmoidLayer(param)), - sigmoid_output_(new Blob()) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "SigmoidCrossEntropyLoss"; } - - protected: - /// @copydoc SigmoidCrossEntropyLossLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the sigmoid cross-entropy loss error gradient w.r.t. the - * predictions. - * - * Gradients cannot be computed with respect to the target inputs (bottom[1]), - * so this method ignores bottom[1] and requires !propagate_down[1], crashing - * if propagate_down[1] is set. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, - * as @f$ \lambda @f$ is the coefficient of this layer's output - * @f$\ell_i@f$ in the overall Net loss - * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence - * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. - * (*Assuming that this top Blob is not used as a bottom (input) by any - * other layer of the Net.) - * @param propagate_down see Layer::Backward. - * propagate_down[1] must be false as gradient computation with respect - * to the targets is not implemented. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$x@f$; Backward computes diff - * @f$ \frac{\partial E}{\partial x} = - * \frac{1}{n} \sum\limits_{n=1}^N (\hat{p}_n - p_n) - * @f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels -- ignored as we can't compute their error gradients - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - /// The internal SigmoidLayer used to map predictions to probabilities. - shared_ptr > sigmoid_layer_; - /// sigmoid_output stores the output of the SigmoidLayer. - shared_ptr > sigmoid_output_; - /// bottom vector holder to call the underlying SigmoidLayer::Forward - vector*> sigmoid_bottom_vec_; - /// top vector holder to call the underlying SigmoidLayer::Forward - vector*> sigmoid_top_vec_; -}; - -// Forward declare SoftmaxLayer for use in SoftmaxWithLossLayer. -template class SoftmaxLayer; - -/** - * @brief Computes the multinomial logistic loss for a one-of-many - * classification task, passing real-valued predictions through a - * softmax to get a probability distribution over classes. - * - * This layer should be preferred over separate - * SoftmaxLayer + MultinomialLogisticLossLayer - * as its gradient computation is more numerically stable. - * At test time, this layer can be replaced simply by a SoftmaxLayer. - * - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ x @f$, a Blob with values in - * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of - * the @f$ K = CHW @f$ classes. This layer maps these scores to a - * probability distribution over classes using the softmax function - * @f$ \hat{p}_{nk} = \exp(x_{nk}) / - * \left[\sum_{k'} \exp(x_{nk'})\right] @f$ (see SoftmaxLayer). - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels @f$ l @f$, an integer-valued Blob with values - * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$ - * indicating the correct class label among the @f$ K @f$ classes - * @param top output Blob vector (length 1) - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * the computed cross-entropy classification loss: @f$ E = - * \frac{-1}{N} \sum\limits_{n=1}^N \log(\hat{p}_{n,l_n}) - * @f$, for softmax output class probabilites @f$ \hat{p} @f$ - */ -template -class SoftmaxWithLossLayer : public LossLayer { - public: - /** - * @param param provides LossParameter loss_param, with options: - * - ignore_label (optional) - * Specify a label value that should be ignored when computing the loss. - * - normalize (optional, default true) - * If true, the loss is normalized by the number of (nonignored) labels - * present; otherwise the loss is simply summed over spatial locations. - */ - explicit SoftmaxWithLossLayer(const LayerParameter& param) - : LossLayer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "SoftmaxWithLoss"; } - virtual inline int ExactNumTopBlobs() const { return -1; } - virtual inline int MinTopBlobs() const { return 1; } - virtual inline int MaxTopBlobs() const { return 2; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - /** - * @brief Computes the softmax loss error gradient w.r.t. the predictions. - * - * Gradients cannot be computed with respect to the label inputs (bottom[1]), - * so this method ignores bottom[1] and requires !propagate_down[1], crashing - * if propagate_down[1] is set. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (1 \times 1 \times 1 \times 1) @f$ - * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$, - * as @f$ \lambda @f$ is the coefficient of this layer's output - * @f$\ell_i@f$ in the overall Net loss - * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence - * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$. - * (*Assuming that this top Blob is not used as a bottom (input) by any - * other layer of the Net.) - * @param propagate_down see Layer::Backward. - * propagate_down[1] must be false as we can't compute gradients with - * respect to the labels. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the predictions @f$ x @f$; Backward computes diff - * @f$ \frac{\partial E}{\partial x} @f$ - * -# @f$ (N \times 1 \times 1 \times 1) @f$ - * the labels -- ignored as we can't compute their error gradients - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - /// Read the normalization mode parameter and compute the normalizer based - /// on the blob size. If normalization_mode is VALID, the count of valid - /// outputs will be read from valid_count, unless it is -1 in which case - /// all outputs are assumed to be valid. - virtual Dtype get_normalizer( - LossParameter_NormalizationMode normalization_mode, int valid_count); - - /// The internal SoftmaxLayer used to map predictions to a distribution. - shared_ptr > softmax_layer_; - /// prob stores the output probability predictions from the SoftmaxLayer. - Blob prob_; - /// bottom vector holder used in call to the underlying SoftmaxLayer::Forward - vector*> softmax_bottom_vec_; - /// top vector holder used in call to the underlying SoftmaxLayer::Forward - vector*> softmax_top_vec_; - /// Whether to ignore instances with a certain label. - bool has_ignore_label_; - /// The label indicating that an instance should be ignored. - int ignore_label_; - /// How to normalize the output loss. - LossParameter_NormalizationMode normalization_; - - int softmax_axis_, outer_num_, inner_num_; -}; - -} // namespace caffe - -#endif // CAFFE_LOSS_LAYERS_HPP_ diff --git a/include/caffe/neuron_layers.hpp b/include/caffe/neuron_layers.hpp deleted file mode 100644 index 4fa330ec783..00000000000 --- a/include/caffe/neuron_layers.hpp +++ /dev/null @@ -1,806 +0,0 @@ -#ifndef CAFFE_NEURON_LAYERS_HPP_ -#define CAFFE_NEURON_LAYERS_HPP_ - -#include -#include -#include - -#include "caffe/blob.hpp" -#include "caffe/common.hpp" -#include "caffe/layer.hpp" -#include "caffe/proto/caffe.pb.h" - -namespace caffe { - -/** - * @brief An interface for layers that take one blob as input (@f$ x @f$) - * and produce one equally-sized blob as output (@f$ y @f$), where - * each element of the output depends only on the corresponding input - * element. - */ -template -class NeuronLayer : public Layer { - public: - explicit NeuronLayer(const LayerParameter& param) - : Layer(param) {} - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } -}; - -/** - * @brief Computes @f$ y = |x| @f$ - * - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ y = |x| @f$ - */ -template -class AbsValLayer : public NeuronLayer { - public: - explicit AbsValLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "AbsVal"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - /// @copydoc AbsValLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the absolute value inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} = - * \mathrm{sign}(x) \frac{\partial E}{\partial y} - * @f$ if propagate_down[0] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -/** - * @brief Computes @f$ y = x + \log(1 + \exp(-x)) @f$ if @f$ x > 0 @f$; - * @f$ y = \log(1 + \exp(x)) @f$ otherwise. - * - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = \left\{ - * \begin{array}{ll} - * x + \log(1 + \exp(-x)) & \mbox{if } x > 0 \\ - * \log(1 + \exp(x)) & \mbox{otherwise} - * \end{array} \right. - * @f$ - */ -template -class BNLLLayer : public NeuronLayer { - public: - explicit BNLLLayer(const LayerParameter& param) - : NeuronLayer(param) {} - - virtual inline const char* type() const { return "BNLL"; } - - protected: - /// @copydoc BNLLLayer - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the BNLL inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 2) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} - * @f$ if propagate_down[0] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -/** - * @brief During training only, sets a random portion of @f$x@f$ to 0, adjusting - * the rest of the vector magnitude accordingly. - * - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ y = |x| @f$ - */ -template -class DropoutLayer : public NeuronLayer { - public: - /** - * @param param provides DropoutParameter dropout_param, - * with DropoutLayer options: - * - dropout_ratio (\b optional, default 0.5). - * Sets the probability @f$ p @f$ that any given unit is dropped. - */ - explicit DropoutLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Dropout"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs. At training time, we have @f$ - * y_{\mbox{train}} = \left\{ - * \begin{array}{ll} - * \frac{x}{1 - p} & \mbox{if } u > p \\ - * 0 & \mbox{otherwise} - * \end{array} \right. - * @f$, where @f$ u \sim U(0, 1)@f$ is generated independently for each - * input at each iteration. At test time, we simply have - * @f$ y_{\mbox{test}} = \mathbb{E}[y_{\mbox{train}}] = x @f$. - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - /// when divided by UINT_MAX, the randomly generated values @f$u\sim U(0,1)@f$ - Blob rand_vec_; - /// the probability @f$ p @f$ of dropping any input - Dtype threshold_; - /// the scale for undropped inputs at train time @f$ 1 / (1 - p) @f$ - Dtype scale_; - unsigned int uint_thres_; -}; - -/** - * @brief Computes @f$ y = \gamma ^ {\alpha x + \beta} @f$, - * as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$, - * and base @f$ \gamma @f$. - */ -template -class ExpLayer : public NeuronLayer { - public: - /** - * @param param provides ExpParameter exp_param, - * with ExpLayer options: - * - scale (\b optional, default 1) the scale @f$ \alpha @f$ - * - shift (\b optional, default 0) the shift @f$ \beta @f$ - * - base (\b optional, default -1 for a value of @f$ e \approx 2.718 @f$) - * the base @f$ \gamma @f$ - */ - explicit ExpLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Exp"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = \gamma ^ {\alpha x + \beta} - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the exp inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} = - * \frac{\partial E}{\partial y} y \alpha \log_e(gamma) - * @f$ if propagate_down[0] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - Dtype inner_scale_, outer_scale_; -}; - -/** - * @brief Computes @f$ y = log_{\gamma}(\alpha x + \beta) @f$, - * as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$, - * and base @f$ \gamma @f$. - */ -template -class LogLayer : public NeuronLayer { - public: - /** - * @param param provides LogParameter log_param, - * with LogLayer options: - * - scale (\b optional, default 1) the scale @f$ \alpha @f$ - * - shift (\b optional, default 0) the shift @f$ \beta @f$ - * - base (\b optional, default -1 for a value of @f$ e \approx 2.718 @f$) - * the base @f$ \gamma @f$ - */ - explicit LogLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Log"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = log_{\gamma}(\alpha x + \beta) - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the exp inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} = - * \frac{\partial E}{\partial y} y \alpha \log_e(gamma) - * @f$ if propagate_down[0] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - Dtype base_scale_; - Dtype input_scale_, input_shift_; - Dtype backward_num_scale_; -}; - -/** - * @brief Computes @f$ y = (\alpha x + \beta) ^ \gamma @f$, - * as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$, - * and power @f$ \gamma @f$. - */ -template -class PowerLayer : public NeuronLayer { - public: - /** - * @param param provides PowerParameter power_param, - * with PowerLayer options: - * - scale (\b optional, default 1) the scale @f$ \alpha @f$ - * - shift (\b optional, default 0) the shift @f$ \beta @f$ - * - power (\b optional, default 1) the power @f$ \gamma @f$ - */ - explicit PowerLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Power"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = (\alpha x + \beta) ^ \gamma - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the power inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} = - * \frac{\partial E}{\partial y} - * \alpha \gamma (\alpha x + \beta) ^ {\gamma - 1} = - * \frac{\partial E}{\partial y} - * \frac{\alpha \gamma y}{\alpha x + \beta} - * @f$ if propagate_down[0] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - /// @brief @f$ \gamma @f$ from layer_param_.power_param() - Dtype power_; - /// @brief @f$ \alpha @f$ from layer_param_.power_param() - Dtype scale_; - /// @brief @f$ \beta @f$ from layer_param_.power_param() - Dtype shift_; - /// @brief Result of @f$ \alpha \gamma @f$ - Dtype diff_scale_; -}; - -/** - * @brief Rectified Linear Unit non-linearity @f$ y = \max(0, x) @f$. - * The simple max is fast to compute, and the function does not saturate. - */ -template -class ReLULayer : public NeuronLayer { - public: - /** - * @param param provides ReLUParameter relu_param, - * with ReLULayer options: - * - negative_slope (\b optional, default 0). - * the value @f$ \nu @f$ by which negative values are multiplied. - */ - explicit ReLULayer(const LayerParameter& param) - : NeuronLayer(param) {} - - virtual inline const char* type() const { return "ReLU"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = \max(0, x) - * @f$ by default. If a non-zero negative_slope @f$ \nu @f$ is provided, - * the computed outputs are @f$ y = \max(0, x) + \nu \min(0, x) @f$. - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the ReLU inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} = \left\{ - * \begin{array}{lr} - * 0 & \mathrm{if} \; x \le 0 \\ - * \frac{\partial E}{\partial y} & \mathrm{if} \; x > 0 - * \end{array} \right. - * @f$ if propagate_down[0], by default. - * If a non-zero negative_slope @f$ \nu @f$ is provided, - * the computed gradients are @f$ - * \frac{\partial E}{\partial x} = \left\{ - * \begin{array}{lr} - * \nu \frac{\partial E}{\partial y} & \mathrm{if} \; x \le 0 \\ - * \frac{\partial E}{\partial y} & \mathrm{if} \; x > 0 - * \end{array} \right. - * @f$. - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -#ifdef USE_CUDNN -/** - * @brief CuDNN acceleration of ReLULayer. - */ -template -class CuDNNReLULayer : public ReLULayer { - public: - explicit CuDNNReLULayer(const LayerParameter& param) - : ReLULayer(param), handles_setup_(false) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNReLULayer(); - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t handle_; - cudnnTensorDescriptor_t bottom_desc_; - cudnnTensorDescriptor_t top_desc_; -}; -#endif - -/** - * @brief Sigmoid function non-linearity @f$ - * y = (1 + \exp(-x))^{-1} - * @f$, a classic choice in neural networks. - * - * Note that the gradient vanishes as the values move away from 0. - * The ReLULayer is often a better choice for this reason. - */ -template -class SigmoidLayer : public NeuronLayer { - public: - explicit SigmoidLayer(const LayerParameter& param) - : NeuronLayer(param) {} - - virtual inline const char* type() const { return "Sigmoid"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = (1 + \exp(-x))^{-1} - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the sigmoid inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} - * = \frac{\partial E}{\partial y} y (1 - y) - * @f$ if propagate_down[0] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -#ifdef USE_CUDNN -/** - * @brief CuDNN acceleration of SigmoidLayer. - */ -template -class CuDNNSigmoidLayer : public SigmoidLayer { - public: - explicit CuDNNSigmoidLayer(const LayerParameter& param) - : SigmoidLayer(param), handles_setup_(false) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNSigmoidLayer(); - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t handle_; - cudnnTensorDescriptor_t bottom_desc_; - cudnnTensorDescriptor_t top_desc_; -}; -#endif - -/** - * @brief TanH hyperbolic tangent non-linearity @f$ - * y = \frac{\exp(2x) - 1}{\exp(2x) + 1} - * @f$, popular in auto-encoders. - * - * Note that the gradient vanishes as the values move away from 0. - * The ReLULayer is often a better choice for this reason. - */ -template -class TanHLayer : public NeuronLayer { - public: - explicit TanHLayer(const LayerParameter& param) - : NeuronLayer(param) {} - - virtual inline const char* type() const { return "TanH"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = \frac{\exp(2x) - 1}{\exp(2x) + 1} - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the sigmoid inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times H \times W) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$; Backward fills their diff with - * gradients @f$ - * \frac{\partial E}{\partial x} - * = \frac{\partial E}{\partial y} - * \left(1 - \left[\frac{\exp(2x) - 1}{exp(2x) + 1} \right]^2 \right) - * = \frac{\partial E}{\partial y} (1 - y^2) - * @f$ if propagate_down[0] - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); -}; - -#ifdef USE_CUDNN -/** - * @brief CuDNN acceleration of TanHLayer. - */ -template -class CuDNNTanHLayer : public TanHLayer { - public: - explicit CuDNNTanHLayer(const LayerParameter& param) - : TanHLayer(param), handles_setup_(false) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNTanHLayer(); - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t handle_; - cudnnTensorDescriptor_t bottom_desc_; - cudnnTensorDescriptor_t top_desc_; -}; -#endif - -/** - * @brief Tests whether the input exceeds a threshold: outputs 1 for inputs - * above threshold; 0 otherwise. - */ -template -class ThresholdLayer : public NeuronLayer { - public: - /** - * @param param provides ThresholdParameter threshold_param, - * with ThresholdLayer options: - * - threshold (\b optional, default 0). - * the threshold value @f$ t @f$ to which the input values are compared. - */ - explicit ThresholdLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Threshold"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times H \times W) @f$ - * the computed outputs @f$ - * y = \left\{ - * \begin{array}{lr} - * 0 & \mathrm{if} \; x \le t \\ - * 1 & \mathrm{if} \; x > t - * \end{array} \right. - * @f$ - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - /// @brief Not implemented (non-differentiable function) - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom) { - NOT_IMPLEMENTED; - } - - Dtype threshold_; -}; - -/** - * @brief Parameterized Rectified Linear Unit non-linearity @f$ - * y_i = \max(0, x_i) + a_i \min(0, x_i) - * @f$. The differences from ReLULayer are 1) negative slopes are - * learnable though backprop and 2) negative slopes can vary across - * channels. The number of axes of input blob should be greater than or - * equal to 2. The 1st axis (0-based) is seen as channels. - */ -template -class PReLULayer : public NeuronLayer { - public: - /** - * @param param provides PReLUParameter prelu_param, - * with PReLULayer options: - * - filler (\b optional, FillerParameter, - * default {'type': constant 'value':0.25}). - * - channel_shared (\b optional, default false). - * negative slopes are shared across channels. - */ - explicit PReLULayer(const LayerParameter& param) - : NeuronLayer(param) {} - - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "PReLU"; } - - protected: - /** - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times ...) @f$ - * the inputs @f$ x @f$ - * @param top output Blob vector (length 1) - * -# @f$ (N \times C \times ...) @f$ - * the computed outputs for each channel @f$i@f$ @f$ - * y_i = \max(0, x_i) + a_i \min(0, x_i) - * @f$. - */ - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - - /** - * @brief Computes the error gradient w.r.t. the PReLU inputs. - * - * @param top output Blob vector (length 1), providing the error gradient with - * respect to the outputs - * -# @f$ (N \times C \times ...) @f$ - * containing error gradients @f$ \frac{\partial E}{\partial y} @f$ - * with respect to computed outputs @f$ y @f$ - * @param propagate_down see Layer::Backward. - * @param bottom input Blob vector (length 1) - * -# @f$ (N \times C \times ...) @f$ - * the inputs @f$ x @f$; For each channel @f$i@f$, backward fills their - * diff with gradients @f$ - * \frac{\partial E}{\partial x_i} = \left\{ - * \begin{array}{lr} - * a_i \frac{\partial E}{\partial y_i} & \mathrm{if} \; x_i \le 0 \\ - * \frac{\partial E}{\partial y_i} & \mathrm{if} \; x_i > 0 - * \end{array} \right. - * @f$. - * If param_propagate_down_[0] is true, it fills the diff with gradients - * @f$ - * \frac{\partial E}{\partial a_i} = \left\{ - * \begin{array}{lr} - * \sum_{x_i} x_i \frac{\partial E}{\partial y_i} & \mathrm{if} \; x_i \le 0 \\ - * 0 & \mathrm{if} \; x_i > 0 - * \end{array} \right. - * @f$. - */ - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool channel_shared_; - Blob multiplier_; // dot multiplier for backward computation of params - Blob backward_buff_; // temporary buffer for backward computation - Blob bottom_memory_; // memory for in-place computation -}; - -} // namespace caffe - -#endif // CAFFE_NEURON_LAYERS_HPP_ diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp deleted file mode 100644 index 237b05d6fa6..00000000000 --- a/include/caffe/vision_layers.hpp +++ /dev/null @@ -1,659 +0,0 @@ -#ifndef CAFFE_VISION_LAYERS_HPP_ -#define CAFFE_VISION_LAYERS_HPP_ - -#include -#include -#include - -#include "caffe/blob.hpp" -#include "caffe/common.hpp" -#include "caffe/common_layers.hpp" -#include "caffe/data_layers.hpp" -#include "caffe/layer.hpp" -#include "caffe/loss_layers.hpp" -#include "caffe/neuron_layers.hpp" -#include "caffe/proto/caffe.pb.h" - -namespace caffe { - -/** - * @brief Abstract base class that factors out the BLAS code common to - * ConvolutionLayer and DeconvolutionLayer. - */ -template -class BaseConvolutionLayer : public Layer { - public: - explicit BaseConvolutionLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline int MinBottomBlobs() const { return 1; } - virtual inline int MinTopBlobs() const { return 1; } - virtual inline bool EqualNumBottomTopBlobs() const { return true; } - - protected: - // Helper functions that abstract away the column buffer and gemm arguments. - // The last argument in forward_cpu_gemm is so that we can skip the im2col if - // we just called weight_cpu_gemm with the same input. - void forward_cpu_gemm(const Dtype* input, const Dtype* weights, - Dtype* output, bool skip_im2col = false); - void forward_cpu_bias(Dtype* output, const Dtype* bias); - void backward_cpu_gemm(const Dtype* input, const Dtype* weights, - Dtype* output); - void weight_cpu_gemm(const Dtype* input, const Dtype* output, Dtype* - weights); - void backward_cpu_bias(Dtype* bias, const Dtype* input); - -#ifndef CPU_ONLY - void forward_gpu_gemm(const Dtype* col_input, const Dtype* weights, - Dtype* output, bool skip_im2col = false); - void forward_gpu_bias(Dtype* output, const Dtype* bias); - void backward_gpu_gemm(const Dtype* input, const Dtype* weights, - Dtype* col_output); - void weight_gpu_gemm(const Dtype* col_input, const Dtype* output, Dtype* - weights); - void backward_gpu_bias(Dtype* bias, const Dtype* input); -#endif - - /// @brief The spatial dimensions of the input. - inline int input_shape(int i) { - return (*bottom_shape_)[channel_axis_ + i]; - } - // reverse_dimensions should return true iff we are implementing deconv, so - // that conv helpers know which dimensions are which. - virtual bool reverse_dimensions() = 0; - // Compute height_out_ and width_out_ from other parameters. - virtual void compute_output_shape() = 0; - - /// @brief The spatial dimensions of a filter kernel. - Blob kernel_shape_; - /// @brief The spatial dimensions of the stride. - Blob stride_; - /// @brief The spatial dimensions of the padding. - Blob pad_; - /// @brief The spatial dimensions of the convolution input. - Blob conv_input_shape_; - /// @brief The spatial dimensions of the col_buffer. - vector col_buffer_shape_; - /// @brief The spatial dimensions of the output. - vector output_shape_; - const vector* bottom_shape_; - - int num_spatial_axes_; - int bottom_dim_; - int top_dim_; - - int channel_axis_; - int num_; - int channels_; - int group_; - int out_spatial_dim_; - int weight_offset_; - int num_output_; - bool bias_term_; - bool is_1x1_; - bool force_nd_im2col_; - - private: - // wrap im2col/col2im so we don't have to remember the (long) argument lists - inline void conv_im2col_cpu(const Dtype* data, Dtype* col_buff) { - if (!force_nd_im2col_ && num_spatial_axes_ == 2) { - im2col_cpu(data, conv_in_channels_, - conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], - kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], - pad_.cpu_data()[0], pad_.cpu_data()[1], - stride_.cpu_data()[0], stride_.cpu_data()[1], col_buff); - } else { - im2col_nd_cpu(data, num_spatial_axes_, conv_input_shape_.cpu_data(), - col_buffer_shape_.data(), kernel_shape_.cpu_data(), - pad_.cpu_data(), stride_.cpu_data(), col_buff); - } - } - inline void conv_col2im_cpu(const Dtype* col_buff, Dtype* data) { - if (!force_nd_im2col_ && num_spatial_axes_ == 2) { - col2im_cpu(col_buff, conv_in_channels_, - conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], - kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], - pad_.cpu_data()[0], pad_.cpu_data()[1], - stride_.cpu_data()[0], stride_.cpu_data()[1], data); - } else { - col2im_nd_cpu(col_buff, num_spatial_axes_, conv_input_shape_.cpu_data(), - col_buffer_shape_.data(), kernel_shape_.cpu_data(), - pad_.cpu_data(), stride_.cpu_data(), data); - } - } -#ifndef CPU_ONLY - inline void conv_im2col_gpu(const Dtype* data, Dtype* col_buff) { - if (!force_nd_im2col_ && num_spatial_axes_ == 2) { - im2col_gpu(data, conv_in_channels_, - conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], - kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], - pad_.cpu_data()[0], pad_.cpu_data()[1], - stride_.cpu_data()[0], stride_.cpu_data()[1], col_buff); - } else { - im2col_nd_gpu(data, num_spatial_axes_, num_kernels_im2col_, - conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(), - kernel_shape_.gpu_data(), pad_.gpu_data(), - stride_.gpu_data(), col_buff); - } - } - inline void conv_col2im_gpu(const Dtype* col_buff, Dtype* data) { - if (!force_nd_im2col_ && num_spatial_axes_ == 2) { - col2im_gpu(col_buff, conv_in_channels_, - conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2], - kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1], - pad_.cpu_data()[0], pad_.cpu_data()[1], - stride_.cpu_data()[0], stride_.cpu_data()[1], data); - } else { - col2im_nd_gpu(col_buff, num_spatial_axes_, num_kernels_col2im_, - conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(), - kernel_shape_.gpu_data(), pad_.gpu_data(), stride_.gpu_data(), - data); - } - } -#endif - - int num_kernels_im2col_; - int num_kernels_col2im_; - int conv_out_channels_; - int conv_in_channels_; - int conv_out_spatial_dim_; - int kernel_dim_; - int col_offset_; - int output_offset_; - - Blob col_buffer_; - Blob bias_multiplier_; -}; - -/** - * @brief Convolves the input image with a bank of learned filters, - * and (optionally) adds biases. - * - * Caffe convolves by reduction to matrix multiplication. This achieves - * high-throughput and generality of input and filter dimensions but comes at - * the cost of memory for matrices. This makes use of efficiency in BLAS. - * - * The input is "im2col" transformed to a channel K' x H x W data matrix - * for multiplication with the N x K' x H x W filter matrix to yield a - * N' x H x W output matrix that is then "col2im" restored. K' is the - * input channel * kernel height * kernel width dimension of the unrolled - * inputs so that the im2col matrix has a column for each input region to - * be filtered. col2im restores the output spatial structure by rolling up - * the output channel N' columns of the output matrix. - */ -template -class ConvolutionLayer : public BaseConvolutionLayer { - public: - /** - * @param param provides ConvolutionParameter convolution_param, - * with ConvolutionLayer options: - * - num_output. The number of filters. - * - kernel_size / kernel_h / kernel_w. The filter dimensions, given by - * kernel_size for square filters or kernel_h and kernel_w for rectangular - * filters. - * - stride / stride_h / stride_w (\b optional, default 1). The filter - * stride, given by stride_size for equal dimensions or stride_h and stride_w - * for different strides. By default the convolution is dense with stride 1. - * - pad / pad_h / pad_w (\b optional, default 0). The zero-padding for - * convolution, given by pad for equal dimensions or pad_h and pad_w for - * different padding. Input padding is computed implicitly instead of - * actually padding. - * - group (\b optional, default 1). The number of filter groups. Group - * convolution is a method for reducing parameterization by selectively - * connecting input and output channels. The input and output channel dimensions must be divisible - * by the number of groups. For group @f$ \geq 1 @f$, the - * convolutional filters' input and output channels are separated s.t. each - * group takes 1 / group of the input channels and makes 1 / group of the - * output channels. Concretely 4 input channels, 8 output channels, and - * 2 groups separate input channels 1-2 and output channels 1-4 into the - * first group and input channels 3-4 and output channels 5-8 into the second - * group. - * - bias_term (\b optional, default true). Whether to have a bias. - * - engine: convolution has CAFFE (matrix multiplication) and CUDNN (library - * kernels + stream parallelism) engines. - */ - explicit ConvolutionLayer(const LayerParameter& param) - : BaseConvolutionLayer(param) {} - - virtual inline const char* type() const { return "Convolution"; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual inline bool reverse_dimensions() { return false; } - virtual void compute_output_shape(); -}; - -/** - * @brief Convolve the input with a bank of learned filters, and (optionally) - * add biases, treating filters and convolution parameters in the - * opposite sense as ConvolutionLayer. - * - * ConvolutionLayer computes each output value by dotting an input window with - * a filter; DeconvolutionLayer multiplies each input value by a filter - * elementwise, and sums over the resulting output windows. In other words, - * DeconvolutionLayer is ConvolutionLayer with the forward and backward passes - * reversed. DeconvolutionLayer reuses ConvolutionParameter for its - * parameters, but they take the opposite sense as in ConvolutionLayer (so - * padding is removed from the output rather than added to the input, and - * stride results in upsampling rather than downsampling). - */ -template -class DeconvolutionLayer : public BaseConvolutionLayer { - public: - explicit DeconvolutionLayer(const LayerParameter& param) - : BaseConvolutionLayer(param) {} - - virtual inline const char* type() const { return "Deconvolution"; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual inline bool reverse_dimensions() { return true; } - virtual void compute_output_shape(); -}; - -#ifdef USE_CUDNN -/* - * @brief cuDNN implementation of ConvolutionLayer. - * Fallback to ConvolutionLayer for CPU mode. - * - * cuDNN accelerates convolution through forward kernels for filtering and bias - * plus backward kernels for the gradient w.r.t. the filters, biases, and - * inputs. Caffe + cuDNN further speeds up the computation through forward - * parallelism across groups and backward parallelism across gradients. - * - * The CUDNN engine does not have memory overhead for matrix buffers. For many - * input and filter regimes the CUDNN engine is faster than the CAFFE engine, - * but for fully-convolutional models and large inputs the CAFFE engine can be - * faster as long as it fits in memory. -*/ -template -class CuDNNConvolutionLayer : public ConvolutionLayer { - public: - explicit CuDNNConvolutionLayer(const LayerParameter& param) - : ConvolutionLayer(param), handles_setup_(false) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNConvolutionLayer(); - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t* handle_; - cudaStream_t* stream_; - - // algorithms for forward and backwards convolutions - cudnnConvolutionFwdAlgo_t *fwd_algo_; - cudnnConvolutionBwdFilterAlgo_t *bwd_filter_algo_; - cudnnConvolutionBwdDataAlgo_t *bwd_data_algo_; - - vector bottom_descs_, top_descs_; - cudnnTensorDescriptor_t bias_desc_; - cudnnFilterDescriptor_t filter_desc_; - vector conv_descs_; - int bottom_offset_, top_offset_, bias_offset_; - - size_t *workspace_fwd_sizes_; - size_t *workspace_bwd_data_sizes_; - size_t *workspace_bwd_filter_sizes_; - size_t workspaceSizeInBytes; // size of underlying storage - void *workspaceData; // underlying storage - void **workspace; // aliases into workspaceData -}; -#endif - -/** - * @brief A helper for image operations that rearranges image regions into - * column vectors. Used by ConvolutionLayer to perform convolution - * by matrix multiplication. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class Im2colLayer : public Layer { - public: - explicit Im2colLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Im2col"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - /// @brief The spatial dimensions of a filter kernel. - Blob kernel_shape_; - /// @brief The spatial dimensions of the stride. - Blob stride_; - /// @brief The spatial dimensions of the padding. - Blob pad_; - - int num_spatial_axes_; - int bottom_dim_; - int top_dim_; - - int channel_axis_; - int num_; - int channels_; - - bool force_nd_im2col_; -}; - -// Forward declare PoolingLayer and SplitLayer for use in LRNLayer. -template class PoolingLayer; -template class SplitLayer; - -/** - * @brief Normalize the input in a local region across or within feature maps. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class LRNLayer : public Layer { - public: - explicit LRNLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "LRN"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - virtual void CrossChannelForward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void CrossChannelForward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void WithinChannelForward(const vector*>& bottom, - const vector*>& top); - virtual void CrossChannelBackward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void CrossChannelBackward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void WithinChannelBackward(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int size_; - int pre_pad_; - Dtype alpha_; - Dtype beta_; - Dtype k_; - int num_; - int channels_; - int height_; - int width_; - - // Fields used for normalization ACROSS_CHANNELS - // scale_ stores the intermediate summing results - Blob scale_; - - // Fields used for normalization WITHIN_CHANNEL - shared_ptr > split_layer_; - vector*> split_top_vec_; - shared_ptr > square_layer_; - Blob square_input_; - Blob square_output_; - vector*> square_bottom_vec_; - vector*> square_top_vec_; - shared_ptr > pool_layer_; - Blob pool_output_; - vector*> pool_top_vec_; - shared_ptr > power_layer_; - Blob power_output_; - vector*> power_top_vec_; - shared_ptr > product_layer_; - Blob product_input_; - vector*> product_bottom_vec_; -}; - -#ifdef USE_CUDNN - -template -class CuDNNLRNLayer : public LRNLayer { - public: - explicit CuDNNLRNLayer(const LayerParameter& param) - : LRNLayer(param), handles_setup_(false) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNLRNLayer(); - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t handle_; - cudnnLRNDescriptor_t norm_desc_; - cudnnTensorDescriptor_t bottom_desc_, top_desc_; - - int size_; - Dtype alpha_, beta_, k_; -}; - -template -class CuDNNLCNLayer : public LRNLayer { - public: - explicit CuDNNLCNLayer(const LayerParameter& param) - : LRNLayer(param), handles_setup_(false), tempDataSize(0), - tempData1(NULL), tempData2(NULL) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNLCNLayer(); - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t handle_; - cudnnLRNDescriptor_t norm_desc_; - cudnnTensorDescriptor_t bottom_desc_, top_desc_; - - int size_, pre_pad_; - Dtype alpha_, beta_, k_; - - size_t tempDataSize; - void *tempData1, *tempData2; -}; - -#endif - -/** - * @brief Pools the input image by taking the max, average, etc. within regions. - * - * TODO(dox): thorough documentation for Forward, Backward, and proto params. - */ -template -class PoolingLayer : public Layer { - public: - explicit PoolingLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "Pooling"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int MinTopBlobs() const { return 1; } - // MAX POOL layers can output an extra top blob for the mask; - // others can only output the pooled inputs. - virtual inline int MaxTopBlobs() const { - return (this->layer_param_.pooling_param().pool() == - PoolingParameter_PoolMethod_MAX) ? 2 : 1; - } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - int kernel_h_, kernel_w_; - int stride_h_, stride_w_; - int pad_h_, pad_w_; - int channels_; - int height_, width_; - int pooled_height_, pooled_width_; - bool global_pooling_; - Blob rand_idx_; - Blob max_idx_; -}; - -#ifdef USE_CUDNN -/* - * @brief cuDNN implementation of PoolingLayer. - * Fallback to PoolingLayer for CPU mode. -*/ -template -class CuDNNPoolingLayer : public PoolingLayer { - public: - explicit CuDNNPoolingLayer(const LayerParameter& param) - : PoolingLayer(param), handles_setup_(false) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - virtual ~CuDNNPoolingLayer(); - // Currently, cuDNN does not support the extra top blob. - virtual inline int MinTopBlobs() const { return -1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_gpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - - bool handles_setup_; - cudnnHandle_t handle_; - cudnnTensorDescriptor_t bottom_desc_, top_desc_; - cudnnPoolingDescriptor_t pooling_desc_; - cudnnPoolingMode_t mode_; -}; -#endif - -/** - * @brief Does spatial pyramid pooling on the input image - * by taking the max, average, etc. within regions - * so that the result vector of different sized - * images are of the same size. - */ -template -class SPPLayer : public Layer { - public: - explicit SPPLayer(const LayerParameter& param) - : Layer(param) {} - virtual void LayerSetUp(const vector*>& bottom, - const vector*>& top); - virtual void Reshape(const vector*>& bottom, - const vector*>& top); - - virtual inline const char* type() const { return "SPP"; } - virtual inline int ExactNumBottomBlobs() const { return 1; } - virtual inline int ExactNumTopBlobs() const { return 1; } - - protected: - virtual void Forward_cpu(const vector*>& bottom, - const vector*>& top); - virtual void Backward_cpu(const vector*>& top, - const vector& propagate_down, const vector*>& bottom); - // calculates the kernel and stride dimensions for the pooling layer, - // returns a correctly configured LayerParameter for a PoolingLayer - virtual LayerParameter GetPoolingParam(const int pyramid_level, - const int bottom_h, const int bottom_w, const SPPParameter spp_param); - - int pyramid_height_; - int bottom_h_, bottom_w_; - int num_; - int channels_; - int kernel_h_, kernel_w_; - int pad_h_, pad_w_; - bool reshaped_first_time_; - - /// the internal Split layer that feeds the pooling layers - shared_ptr > split_layer_; - /// top vector holder used in call to the underlying SplitLayer::Forward - vector*> split_top_vec_; - /// bottom vector holder used in call to the underlying PoolingLayer::Forward - vector*>*> pooling_bottom_vecs_; - /// the internal Pooling layers of different kernel sizes - vector > > pooling_layers_; - /// top vector holders used in call to the underlying PoolingLayer::Forward - vector*>*> pooling_top_vecs_; - /// pooling_outputs stores the outputs of the PoolingLayers - vector*> pooling_outputs_; - /// the internal Flatten layers that the Pooling layers feed into - vector*> flatten_layers_; - /// top vector holders used in call to the underlying FlattenLayer::Forward - vector*>*> flatten_top_vecs_; - /// flatten_outputs stores the outputs of the FlattenLayers - vector*> flatten_outputs_; - /// bottom vector holder used in call to the underlying ConcatLayer::Forward - vector*> concat_bottom_vec_; - /// the internal Concat layers that the Flatten layers feed into - shared_ptr > concat_layer_; -}; - -} // namespace caffe - -#endif // CAFFE_VISION_LAYERS_HPP_ diff --git a/python/caffe/_caffe.cpp b/python/caffe/_caffe.cpp index 1a318f8d36d..69d55332841 100644 --- a/python/caffe/_caffe.cpp +++ b/python/caffe/_caffe.cpp @@ -15,7 +15,8 @@ #include // NOLINT #include "caffe/caffe.hpp" -#include "caffe/python_layer.hpp" +#include "caffe/layers/memory_data_layer.hpp" +#include "caffe/layers/python_layer.hpp" #include "caffe/sgd_solvers.hpp" // Temporary solution for numpy < 1.7 versions: old macro, no promises. diff --git a/src/caffe/data_reader.cpp b/src/caffe/data_reader.cpp index 16378203a88..9f019bbfcb7 100644 --- a/src/caffe/data_reader.cpp +++ b/src/caffe/data_reader.cpp @@ -4,8 +4,8 @@ #include #include "caffe/common.hpp" -#include "caffe/data_layers.hpp" #include "caffe/data_reader.hpp" +#include "caffe/layers/data_layer.hpp" #include "caffe/proto/caffe.pb.h" namespace caffe { diff --git a/src/caffe/layer_factory.cpp b/src/caffe/layer_factory.cpp index 417ffe986df..76d851af9a2 100644 --- a/src/caffe/layer_factory.cpp +++ b/src/caffe/layer_factory.cpp @@ -7,11 +7,28 @@ #include "caffe/layer.hpp" #include "caffe/layer_factory.hpp" +#include "caffe/layers/conv_layer.hpp" +#include "caffe/layers/lrn_layer.hpp" +#include "caffe/layers/pooling_layer.hpp" +#include "caffe/layers/relu_layer.hpp" +#include "caffe/layers/sigmoid_layer.hpp" +#include "caffe/layers/softmax_layer.hpp" +#include "caffe/layers/tanh_layer.hpp" #include "caffe/proto/caffe.pb.h" -#include "caffe/vision_layers.hpp" + +#ifdef USE_CUDNN +#include "caffe/layers/cudnn_conv_layer.hpp" +#include "caffe/layers/cudnn_lcn_layer.hpp" +#include "caffe/layers/cudnn_lrn_layer.hpp" +#include "caffe/layers/cudnn_pooling_layer.hpp" +#include "caffe/layers/cudnn_relu_layer.hpp" +#include "caffe/layers/cudnn_sigmoid_layer.hpp" +#include "caffe/layers/cudnn_softmax_layer.hpp" +#include "caffe/layers/cudnn_tanh_layer.hpp" +#endif #ifdef WITH_PYTHON_LAYER -#include "caffe/python_layer.hpp" +#include "caffe/layers/python_layer.hpp" #endif namespace caffe { diff --git a/src/caffe/layers/absval_layer.cpp b/src/caffe/layers/absval_layer.cpp index 7e552352608..855bf0bfacb 100644 --- a/src/caffe/layers/absval_layer.cpp +++ b/src/caffe/layers/absval_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/absval_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/absval_layer.cu b/src/caffe/layers/absval_layer.cu index b5a6c25a85a..6c927e6fabc 100644 --- a/src/caffe/layers/absval_layer.cu +++ b/src/caffe/layers/absval_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/absval_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/accuracy_layer.cpp b/src/caffe/layers/accuracy_layer.cpp index ae2df1f1bad..4eddbb5c850 100644 --- a/src/caffe/layers/accuracy_layer.cpp +++ b/src/caffe/layers/accuracy_layer.cpp @@ -2,7 +2,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/accuracy_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/argmax_layer.cpp b/src/caffe/layers/argmax_layer.cpp index 354d83f7061..2d3d6f2d3ff 100644 --- a/src/caffe/layers/argmax_layer.cpp +++ b/src/caffe/layers/argmax_layer.cpp @@ -3,7 +3,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/argmax_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/base_conv_layer.cpp b/src/caffe/layers/base_conv_layer.cpp index 316cb0fdf98..f6f14cd0f17 100644 --- a/src/caffe/layers/base_conv_layer.cpp +++ b/src/caffe/layers/base_conv_layer.cpp @@ -2,9 +2,9 @@ #include #include "caffe/filler.hpp" +#include "caffe/layers/base_conv_layer.hpp" #include "caffe/util/im2col.hpp" #include "caffe/util/math_functions.hpp" -#include "caffe/vision_layers.hpp" namespace caffe { diff --git a/src/caffe/layers/base_data_layer.cpp b/src/caffe/layers/base_data_layer.cpp index d77f91c913b..989319f1a07 100644 --- a/src/caffe/layers/base_data_layer.cpp +++ b/src/caffe/layers/base_data_layer.cpp @@ -1,7 +1,13 @@ #include #include -#include "caffe/data_layers.hpp" +#include "caffe/blob.hpp" +#include "caffe/data_transformer.hpp" +#include "caffe/internal_thread.hpp" +#include "caffe/layer.hpp" +#include "caffe/layers/base_data_layer.hpp" +#include "caffe/proto/caffe.pb.h" +#include "caffe/util/blocking_queue.hpp" namespace caffe { diff --git a/src/caffe/layers/base_data_layer.cu b/src/caffe/layers/base_data_layer.cu index ff6e412aba6..4056d36a7b4 100644 --- a/src/caffe/layers/base_data_layer.cu +++ b/src/caffe/layers/base_data_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/data_layers.hpp" +#include "caffe/layers/base_data_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/batch_norm_layer.cpp b/src/caffe/layers/batch_norm_layer.cpp index b5c91b5e1b3..a69d8f99316 100644 --- a/src/caffe/layers/batch_norm_layer.cpp +++ b/src/caffe/layers/batch_norm_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/batch_norm_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/batch_norm_layer.cu b/src/caffe/layers/batch_norm_layer.cu index 2a6cac54168..c21713c81d9 100644 --- a/src/caffe/layers/batch_norm_layer.cu +++ b/src/caffe/layers/batch_norm_layer.cu @@ -1,7 +1,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/batch_norm_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/batch_reindex_layer.cpp b/src/caffe/layers/batch_reindex_layer.cpp index 3d3ce32c90d..b14e56f7c6b 100644 --- a/src/caffe/layers/batch_reindex_layer.cpp +++ b/src/caffe/layers/batch_reindex_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/batch_reindex_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/batch_reindex_layer.cu b/src/caffe/layers/batch_reindex_layer.cu index 0b5ccf099fe..83054d36d33 100644 --- a/src/caffe/layers/batch_reindex_layer.cu +++ b/src/caffe/layers/batch_reindex_layer.cu @@ -2,7 +2,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/batch_reindex_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/bnll_layer.cpp b/src/caffe/layers/bnll_layer.cpp index 1e422a54653..448d86d752d 100644 --- a/src/caffe/layers/bnll_layer.cpp +++ b/src/caffe/layers/bnll_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/bnll_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/bnll_layer.cu b/src/caffe/layers/bnll_layer.cu index 3e328ef70fa..8df8ef09afe 100644 --- a/src/caffe/layers/bnll_layer.cu +++ b/src/caffe/layers/bnll_layer.cu @@ -1,7 +1,7 @@ #include #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/bnll_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/concat_layer.cpp b/src/caffe/layers/concat_layer.cpp index 14cbfb11f8b..580bd47977d 100644 --- a/src/caffe/layers/concat_layer.cpp +++ b/src/caffe/layers/concat_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/concat_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/concat_layer.cu b/src/caffe/layers/concat_layer.cu index e1e9449e406..a3a0bf6f6ea 100644 --- a/src/caffe/layers/concat_layer.cu +++ b/src/caffe/layers/concat_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/concat_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/contrastive_loss_layer.cpp b/src/caffe/layers/contrastive_loss_layer.cpp index 45facd4a4f5..599e178e9c4 100644 --- a/src/caffe/layers/contrastive_loss_layer.cpp +++ b/src/caffe/layers/contrastive_loss_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/contrastive_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/contrastive_loss_layer.cu b/src/caffe/layers/contrastive_loss_layer.cu index ee2784077e4..fd7d67cca94 100644 --- a/src/caffe/layers/contrastive_loss_layer.cu +++ b/src/caffe/layers/contrastive_loss_layer.cu @@ -1,7 +1,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/contrastive_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/conv_layer.cpp b/src/caffe/layers/conv_layer.cpp index efd69d45edf..cff09783945 100644 --- a/src/caffe/layers/conv_layer.cpp +++ b/src/caffe/layers/conv_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/conv_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/conv_layer.cu b/src/caffe/layers/conv_layer.cu index a534b3560f5..d06e4b6244e 100644 --- a/src/caffe/layers/conv_layer.cu +++ b/src/caffe/layers/conv_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/conv_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_conv_layer.cpp b/src/caffe/layers/cudnn_conv_layer.cpp index c82cb7efd6c..1987fb096b0 100644 --- a/src/caffe/layers/cudnn_conv_layer.cpp +++ b/src/caffe/layers/cudnn_conv_layer.cpp @@ -2,7 +2,7 @@ #include #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_conv_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_conv_layer.cu b/src/caffe/layers/cudnn_conv_layer.cu index f2df4aa502f..1990e932a70 100644 --- a/src/caffe/layers/cudnn_conv_layer.cu +++ b/src/caffe/layers/cudnn_conv_layer.cu @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_conv_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_lcn_layer.cpp b/src/caffe/layers/cudnn_lcn_layer.cpp index 4c700786e6b..9c09bf26b4d 100644 --- a/src/caffe/layers/cudnn_lcn_layer.cpp +++ b/src/caffe/layers/cudnn_lcn_layer.cpp @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_lcn_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_lcn_layer.cu b/src/caffe/layers/cudnn_lcn_layer.cu index e79c745894a..b44ef4730ef 100644 --- a/src/caffe/layers/cudnn_lcn_layer.cu +++ b/src/caffe/layers/cudnn_lcn_layer.cu @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_lcn_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_lrn_layer.cpp b/src/caffe/layers/cudnn_lrn_layer.cpp index a03db3bdd9f..0495b802baf 100644 --- a/src/caffe/layers/cudnn_lrn_layer.cpp +++ b/src/caffe/layers/cudnn_lrn_layer.cpp @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_lrn_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_lrn_layer.cu b/src/caffe/layers/cudnn_lrn_layer.cu index 327e44b4d31..ca647f3c64d 100644 --- a/src/caffe/layers/cudnn_lrn_layer.cu +++ b/src/caffe/layers/cudnn_lrn_layer.cu @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_lrn_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_pooling_layer.cpp b/src/caffe/layers/cudnn_pooling_layer.cpp index 5f995d45e0c..24f14780b4f 100644 --- a/src/caffe/layers/cudnn_pooling_layer.cpp +++ b/src/caffe/layers/cudnn_pooling_layer.cpp @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_pooling_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_pooling_layer.cu b/src/caffe/layers/cudnn_pooling_layer.cu index 9aa39ed8818..6f00195fa2d 100644 --- a/src/caffe/layers/cudnn_pooling_layer.cu +++ b/src/caffe/layers/cudnn_pooling_layer.cu @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_pooling_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_relu_layer.cpp b/src/caffe/layers/cudnn_relu_layer.cpp index e6b6d5a9715..c86c6907113 100644 --- a/src/caffe/layers/cudnn_relu_layer.cpp +++ b/src/caffe/layers/cudnn_relu_layer.cpp @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_relu_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_relu_layer.cu b/src/caffe/layers/cudnn_relu_layer.cu index 2a53a49b91f..9f617183baa 100644 --- a/src/caffe/layers/cudnn_relu_layer.cu +++ b/src/caffe/layers/cudnn_relu_layer.cu @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_relu_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_sigmoid_layer.cpp b/src/caffe/layers/cudnn_sigmoid_layer.cpp index 4b489fa5b16..ccb955cdaff 100644 --- a/src/caffe/layers/cudnn_sigmoid_layer.cpp +++ b/src/caffe/layers/cudnn_sigmoid_layer.cpp @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_sigmoid_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_sigmoid_layer.cu b/src/caffe/layers/cudnn_sigmoid_layer.cu index 9de5c742c8e..e2a4b460c6c 100644 --- a/src/caffe/layers/cudnn_sigmoid_layer.cu +++ b/src/caffe/layers/cudnn_sigmoid_layer.cu @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_sigmoid_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_softmax_layer.cpp b/src/caffe/layers/cudnn_softmax_layer.cpp index f5cd04505b8..6440df9805b 100644 --- a/src/caffe/layers/cudnn_softmax_layer.cpp +++ b/src/caffe/layers/cudnn_softmax_layer.cpp @@ -3,7 +3,7 @@ #include "thrust/device_vector.h" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_softmax_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_softmax_layer.cu b/src/caffe/layers/cudnn_softmax_layer.cu index c270202f0c9..7283eb71558 100644 --- a/src/caffe/layers/cudnn_softmax_layer.cu +++ b/src/caffe/layers/cudnn_softmax_layer.cu @@ -3,7 +3,7 @@ #include "thrust/device_vector.h" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/cudnn_softmax_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_tanh_layer.cpp b/src/caffe/layers/cudnn_tanh_layer.cpp index 4629681807d..1a56418227c 100644 --- a/src/caffe/layers/cudnn_tanh_layer.cpp +++ b/src/caffe/layers/cudnn_tanh_layer.cpp @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/cudnn_tanh_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/cudnn_tanh_layer.cu b/src/caffe/layers/cudnn_tanh_layer.cu index 84f784b37c4..89df28a3e8b 100644 --- a/src/caffe/layers/cudnn_tanh_layer.cu +++ b/src/caffe/layers/cudnn_tanh_layer.cu @@ -1,7 +1,7 @@ #ifdef USE_CUDNN #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/cudnn_tanh_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/data_layer.cpp b/src/caffe/layers/data_layer.cpp index 49ac858efc9..66e6301fd45 100644 --- a/src/caffe/layers/data_layer.cpp +++ b/src/caffe/layers/data_layer.cpp @@ -5,8 +5,8 @@ #include -#include "caffe/data_layers.hpp" -#include "caffe/proto/caffe.pb.h" +#include "caffe/data_transformer.hpp" +#include "caffe/layers/data_layer.hpp" #include "caffe/util/benchmark.hpp" namespace caffe { diff --git a/src/caffe/layers/deconv_layer.cpp b/src/caffe/layers/deconv_layer.cpp index 5038b6389fa..275c05626c8 100644 --- a/src/caffe/layers/deconv_layer.cpp +++ b/src/caffe/layers/deconv_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/deconv_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/deconv_layer.cu b/src/caffe/layers/deconv_layer.cu index 0e8e2edea1e..226763223fa 100644 --- a/src/caffe/layers/deconv_layer.cu +++ b/src/caffe/layers/deconv_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/vision_layers.hpp" +#include "caffe/layers/deconv_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/dropout_layer.cpp b/src/caffe/layers/dropout_layer.cpp index eb7a8a9a20b..9cb64d9735f 100644 --- a/src/caffe/layers/dropout_layer.cpp +++ b/src/caffe/layers/dropout_layer.cpp @@ -2,7 +2,7 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/dropout_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/dropout_layer.cu b/src/caffe/layers/dropout_layer.cu index 028fc026d51..186c10ca489 100644 --- a/src/caffe/layers/dropout_layer.cu +++ b/src/caffe/layers/dropout_layer.cu @@ -1,11 +1,10 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/dropout_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { - template __global__ void DropoutForward(const int n, const Dtype* in, const unsigned int* mask, const unsigned int threshold, const float scale, @@ -68,5 +67,4 @@ void DropoutLayer::Backward_gpu(const vector*>& top, INSTANTIATE_LAYER_GPU_FUNCS(DropoutLayer); - } // namespace caffe diff --git a/src/caffe/layers/dummy_data_layer.cpp b/src/caffe/layers/dummy_data_layer.cpp index ab0478c860c..e382bfea802 100644 --- a/src/caffe/layers/dummy_data_layer.cpp +++ b/src/caffe/layers/dummy_data_layer.cpp @@ -1,7 +1,7 @@ #include -#include "caffe/data_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/dummy_data_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/eltwise_layer.cpp b/src/caffe/layers/eltwise_layer.cpp index 7924fbeec7d..21256166bfa 100644 --- a/src/caffe/layers/eltwise_layer.cpp +++ b/src/caffe/layers/eltwise_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/eltwise_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/eltwise_layer.cu b/src/caffe/layers/eltwise_layer.cu index 01404209834..c142852e03d 100644 --- a/src/caffe/layers/eltwise_layer.cu +++ b/src/caffe/layers/eltwise_layer.cu @@ -1,7 +1,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/eltwise_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/embed_layer.cpp b/src/caffe/layers/embed_layer.cpp index 52704a06d29..36b40d700fd 100644 --- a/src/caffe/layers/embed_layer.cpp +++ b/src/caffe/layers/embed_layer.cpp @@ -1,7 +1,7 @@ #include -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/embed_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/embed_layer.cu b/src/caffe/layers/embed_layer.cu index cd4b40f58fe..6324a3a8937 100644 --- a/src/caffe/layers/embed_layer.cu +++ b/src/caffe/layers/embed_layer.cu @@ -1,7 +1,7 @@ #include -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/embed_layer.hpp" #include "caffe/util/gpu_util.cuh" #include "caffe/util/math_functions.hpp" diff --git a/src/caffe/layers/euclidean_loss_layer.cpp b/src/caffe/layers/euclidean_loss_layer.cpp index 7338953d340..300d991e765 100644 --- a/src/caffe/layers/euclidean_loss_layer.cpp +++ b/src/caffe/layers/euclidean_loss_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/euclidean_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/euclidean_loss_layer.cu b/src/caffe/layers/euclidean_loss_layer.cu index 1aa79bd542d..4c221b64faf 100644 --- a/src/caffe/layers/euclidean_loss_layer.cu +++ b/src/caffe/layers/euclidean_loss_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/euclidean_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/exp_layer.cpp b/src/caffe/layers/exp_layer.cpp index f85692d6c5d..1f4a309fe25 100644 --- a/src/caffe/layers/exp_layer.cpp +++ b/src/caffe/layers/exp_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/exp_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/exp_layer.cu b/src/caffe/layers/exp_layer.cu index 9e24bbeec9d..61f7f11dd46 100644 --- a/src/caffe/layers/exp_layer.cu +++ b/src/caffe/layers/exp_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/exp_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/filter_layer.cpp b/src/caffe/layers/filter_layer.cpp index e8b62a5d5fc..e226c0b6c9b 100644 --- a/src/caffe/layers/filter_layer.cpp +++ b/src/caffe/layers/filter_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/filter_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/filter_layer.cu b/src/caffe/layers/filter_layer.cu index 746e91c9e95..b01b16f840c 100644 --- a/src/caffe/layers/filter_layer.cu +++ b/src/caffe/layers/filter_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/filter_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/flatten_layer.cpp b/src/caffe/layers/flatten_layer.cpp index d831fb5c6b5..651507e2e7d 100644 --- a/src/caffe/layers/flatten_layer.cpp +++ b/src/caffe/layers/flatten_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/flatten_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/hdf5_data_layer.cpp b/src/caffe/layers/hdf5_data_layer.cpp index c765fa02c82..2f13dc641df 100644 --- a/src/caffe/layers/hdf5_data_layer.cpp +++ b/src/caffe/layers/hdf5_data_layer.cpp @@ -14,7 +14,7 @@ #include "hdf5_hl.h" #include "stdint.h" -#include "caffe/data_layers.hpp" +#include "caffe/layers/hdf5_data_layer.hpp" #include "caffe/util/hdf5.hpp" namespace caffe { diff --git a/src/caffe/layers/hdf5_data_layer.cu b/src/caffe/layers/hdf5_data_layer.cu index 6ac499c6fbd..595d2230220 100644 --- a/src/caffe/layers/hdf5_data_layer.cu +++ b/src/caffe/layers/hdf5_data_layer.cu @@ -9,7 +9,7 @@ TODO: #include "hdf5.h" #include "hdf5_hl.h" -#include "caffe/data_layers.hpp" +#include "caffe/layers/hdf5_data_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/hdf5_output_layer.cpp b/src/caffe/layers/hdf5_output_layer.cpp index dbde65da14f..f8f1edcd18e 100644 --- a/src/caffe/layers/hdf5_output_layer.cpp +++ b/src/caffe/layers/hdf5_output_layer.cpp @@ -3,7 +3,7 @@ #include "hdf5.h" #include "hdf5_hl.h" -#include "caffe/data_layers.hpp" +#include "caffe/layers/hdf5_output_layer.hpp" #include "caffe/util/hdf5.hpp" namespace caffe { diff --git a/src/caffe/layers/hdf5_output_layer.cu b/src/caffe/layers/hdf5_output_layer.cu index ca8f2616548..c1685cd34a7 100644 --- a/src/caffe/layers/hdf5_output_layer.cu +++ b/src/caffe/layers/hdf5_output_layer.cu @@ -3,7 +3,7 @@ #include "hdf5.h" #include "hdf5_hl.h" -#include "caffe/data_layers.hpp" +#include "caffe/layers/hdf5_output_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/hinge_loss_layer.cpp b/src/caffe/layers/hinge_loss_layer.cpp index a88c8775205..374aed3c98f 100644 --- a/src/caffe/layers/hinge_loss_layer.cpp +++ b/src/caffe/layers/hinge_loss_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/hinge_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/im2col_layer.cpp b/src/caffe/layers/im2col_layer.cpp index f3b0f7101b8..c12e4f52a10 100644 --- a/src/caffe/layers/im2col_layer.cpp +++ b/src/caffe/layers/im2col_layer.cpp @@ -1,7 +1,7 @@ #include +#include "caffe/layers/im2col_layer.hpp" #include "caffe/util/im2col.hpp" -#include "caffe/vision_layers.hpp" namespace caffe { diff --git a/src/caffe/layers/im2col_layer.cu b/src/caffe/layers/im2col_layer.cu index 4633628b4c0..517b4220cb9 100644 --- a/src/caffe/layers/im2col_layer.cu +++ b/src/caffe/layers/im2col_layer.cu @@ -1,7 +1,7 @@ #include +#include "caffe/layers/im2col_layer.hpp" #include "caffe/util/im2col.hpp" -#include "caffe/vision_layers.hpp" namespace caffe { diff --git a/src/caffe/layers/image_data_layer.cpp b/src/caffe/layers/image_data_layer.cpp index 9a7df5a78ca..62fda4accce 100644 --- a/src/caffe/layers/image_data_layer.cpp +++ b/src/caffe/layers/image_data_layer.cpp @@ -7,7 +7,9 @@ #include #include -#include "caffe/data_layers.hpp" +#include "caffe/data_transformer.hpp" +#include "caffe/layers/base_data_layer.hpp" +#include "caffe/layers/image_data_layer.hpp" #include "caffe/util/benchmark.hpp" #include "caffe/util/io.hpp" #include "caffe/util/math_functions.hpp" diff --git a/src/caffe/layers/infogain_loss_layer.cpp b/src/caffe/layers/infogain_loss_layer.cpp index 88bd8aaf54f..624d3118124 100644 --- a/src/caffe/layers/infogain_loss_layer.cpp +++ b/src/caffe/layers/infogain_loss_layer.cpp @@ -2,7 +2,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/infogain_loss_layer.hpp" #include "caffe/util/io.hpp" namespace caffe { diff --git a/src/caffe/layers/inner_product_layer.cpp b/src/caffe/layers/inner_product_layer.cpp index 274744eaaf8..d9088805501 100644 --- a/src/caffe/layers/inner_product_layer.cpp +++ b/src/caffe/layers/inner_product_layer.cpp @@ -1,7 +1,7 @@ #include -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/inner_product_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/inner_product_layer.cu b/src/caffe/layers/inner_product_layer.cu index e91e94fc9c6..dc25aa33bd1 100644 --- a/src/caffe/layers/inner_product_layer.cu +++ b/src/caffe/layers/inner_product_layer.cu @@ -1,7 +1,7 @@ #include -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/inner_product_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/log_layer.cpp b/src/caffe/layers/log_layer.cpp index a1876b9da76..c70a795cf53 100644 --- a/src/caffe/layers/log_layer.cpp +++ b/src/caffe/layers/log_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/log_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/log_layer.cu b/src/caffe/layers/log_layer.cu index 055b713bef3..db466dbac29 100644 --- a/src/caffe/layers/log_layer.cu +++ b/src/caffe/layers/log_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/log_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/loss_layer.cpp b/src/caffe/layers/loss_layer.cpp index c10466dbdb0..c0b7a862181 100644 --- a/src/caffe/layers/loss_layer.cpp +++ b/src/caffe/layers/loss_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/loss_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/lrn_layer.cpp b/src/caffe/layers/lrn_layer.cpp index cc561811e95..210525e20f3 100644 --- a/src/caffe/layers/lrn_layer.cpp +++ b/src/caffe/layers/lrn_layer.cpp @@ -1,7 +1,7 @@ #include +#include "caffe/layers/lrn_layer.hpp" #include "caffe/util/math_functions.hpp" -#include "caffe/vision_layers.hpp" namespace caffe { diff --git a/src/caffe/layers/lrn_layer.cu b/src/caffe/layers/lrn_layer.cu index 4523d41095d..26e619c7569 100644 --- a/src/caffe/layers/lrn_layer.cu +++ b/src/caffe/layers/lrn_layer.cu @@ -1,7 +1,7 @@ #include +#include "caffe/layers/lrn_layer.hpp" #include "caffe/util/math_functions.hpp" -#include "caffe/vision_layers.hpp" namespace caffe { diff --git a/src/caffe/layers/memory_data_layer.cpp b/src/caffe/layers/memory_data_layer.cpp index 13a3d9f61b9..82909874054 100644 --- a/src/caffe/layers/memory_data_layer.cpp +++ b/src/caffe/layers/memory_data_layer.cpp @@ -4,7 +4,7 @@ #include -#include "caffe/data_layers.hpp" +#include "caffe/layers/memory_data_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/multinomial_logistic_loss_layer.cpp b/src/caffe/layers/multinomial_logistic_loss_layer.cpp index 59745923825..65664998d2c 100644 --- a/src/caffe/layers/multinomial_logistic_loss_layer.cpp +++ b/src/caffe/layers/multinomial_logistic_loss_layer.cpp @@ -2,7 +2,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/multinomial_logistic_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/mvn_layer.cpp b/src/caffe/layers/mvn_layer.cpp index 0e7301442ba..8fe4ef8c0a8 100644 --- a/src/caffe/layers/mvn_layer.cpp +++ b/src/caffe/layers/mvn_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/mvn_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/mvn_layer.cu b/src/caffe/layers/mvn_layer.cu index b7e3b3ceb39..739293be00e 100644 --- a/src/caffe/layers/mvn_layer.cu +++ b/src/caffe/layers/mvn_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/mvn_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/neuron_layer.cpp b/src/caffe/layers/neuron_layer.cpp index 1dcb2c06419..d7b5f389310 100644 --- a/src/caffe/layers/neuron_layer.cpp +++ b/src/caffe/layers/neuron_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/neuron_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/pooling_layer.cpp b/src/caffe/layers/pooling_layer.cpp index 3a7de42c91a..90897db0f45 100644 --- a/src/caffe/layers/pooling_layer.cpp +++ b/src/caffe/layers/pooling_layer.cpp @@ -2,8 +2,8 @@ #include #include +#include "caffe/layers/pooling_layer.hpp" #include "caffe/util/math_functions.hpp" -#include "caffe/vision_layers.hpp" namespace caffe { diff --git a/src/caffe/layers/pooling_layer.cu b/src/caffe/layers/pooling_layer.cu index 5e94ce2bc3f..1ea46cc81b1 100644 --- a/src/caffe/layers/pooling_layer.cu +++ b/src/caffe/layers/pooling_layer.cu @@ -2,8 +2,8 @@ #include #include +#include "caffe/layers/pooling_layer.hpp" #include "caffe/util/math_functions.hpp" -#include "caffe/vision_layers.hpp" namespace caffe { diff --git a/src/caffe/layers/power_layer.cpp b/src/caffe/layers/power_layer.cpp index 6304fadd489..d99b77ca839 100644 --- a/src/caffe/layers/power_layer.cpp +++ b/src/caffe/layers/power_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/power_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/power_layer.cu b/src/caffe/layers/power_layer.cu index 680faad4bca..07711c4213d 100644 --- a/src/caffe/layers/power_layer.cu +++ b/src/caffe/layers/power_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/power_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/prelu_layer.cpp b/src/caffe/layers/prelu_layer.cpp index b5a294e1c5a..853181bd5a2 100644 --- a/src/caffe/layers/prelu_layer.cpp +++ b/src/caffe/layers/prelu_layer.cpp @@ -2,7 +2,9 @@ #include #include "caffe/filler.hpp" -#include "caffe/neuron_layers.hpp" + +#include "caffe/layers/neuron_layer.hpp" +#include "caffe/layers/prelu_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/prelu_layer.cu b/src/caffe/layers/prelu_layer.cu index 992cd885a95..aeb80eacd03 100644 --- a/src/caffe/layers/prelu_layer.cu +++ b/src/caffe/layers/prelu_layer.cu @@ -1,7 +1,8 @@ #include #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/neuron_layer.hpp" +#include "caffe/layers/prelu_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/reduction_layer.cpp b/src/caffe/layers/reduction_layer.cpp index 6b7925e37b0..fa46487e6a3 100644 --- a/src/caffe/layers/reduction_layer.cpp +++ b/src/caffe/layers/reduction_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/reduction_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/reduction_layer.cu b/src/caffe/layers/reduction_layer.cu index a9a8c8d9633..4a6b2b73fc7 100644 --- a/src/caffe/layers/reduction_layer.cu +++ b/src/caffe/layers/reduction_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/reduction_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/relu_layer.cpp b/src/caffe/layers/relu_layer.cpp index 93d090263c7..92a729c81bd 100644 --- a/src/caffe/layers/relu_layer.cpp +++ b/src/caffe/layers/relu_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/relu_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/relu_layer.cu b/src/caffe/layers/relu_layer.cu index c18ab61f2ed..4bf15b3aad3 100644 --- a/src/caffe/layers/relu_layer.cu +++ b/src/caffe/layers/relu_layer.cu @@ -1,7 +1,7 @@ #include #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/relu_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/reshape_layer.cpp b/src/caffe/layers/reshape_layer.cpp index 8659049b528..82339f76d8f 100644 --- a/src/caffe/layers/reshape_layer.cpp +++ b/src/caffe/layers/reshape_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/reshape_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp b/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp index 98588637831..10ac9470832 100644 --- a/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp +++ b/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/sigmoid_cross_entropy_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cu b/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cu index 48dbec41be1..046cb9d3a31 100644 --- a/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cu +++ b/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/sigmoid_cross_entropy_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/sigmoid_layer.cpp b/src/caffe/layers/sigmoid_layer.cpp index d4a3f8773d6..85fd9676812 100644 --- a/src/caffe/layers/sigmoid_layer.cpp +++ b/src/caffe/layers/sigmoid_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/sigmoid_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/sigmoid_layer.cu b/src/caffe/layers/sigmoid_layer.cu index 5730636ef11..184c61ede83 100644 --- a/src/caffe/layers/sigmoid_layer.cu +++ b/src/caffe/layers/sigmoid_layer.cu @@ -1,7 +1,7 @@ #include #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/sigmoid_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/silence_layer.cpp b/src/caffe/layers/silence_layer.cpp index 3974f5d4e65..b2f85c52a0f 100644 --- a/src/caffe/layers/silence_layer.cpp +++ b/src/caffe/layers/silence_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/silence_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/silence_layer.cu b/src/caffe/layers/silence_layer.cu index c49ecb2338a..3494f6f6731 100644 --- a/src/caffe/layers/silence_layer.cu +++ b/src/caffe/layers/silence_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/silence_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/slice_layer.cpp b/src/caffe/layers/slice_layer.cpp index f368a249a5e..759beafe0d9 100644 --- a/src/caffe/layers/slice_layer.cpp +++ b/src/caffe/layers/slice_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/slice_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/slice_layer.cu b/src/caffe/layers/slice_layer.cu index d555f7d0dec..1be3a797d3e 100644 --- a/src/caffe/layers/slice_layer.cu +++ b/src/caffe/layers/slice_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/slice_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/softmax_layer.cpp b/src/caffe/layers/softmax_layer.cpp index 8ae7d49cfaa..f60e9b03ebf 100644 --- a/src/caffe/layers/softmax_layer.cpp +++ b/src/caffe/layers/softmax_layer.cpp @@ -1,7 +1,7 @@ #include #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/softmax_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/softmax_layer.cu b/src/caffe/layers/softmax_layer.cu index a620fcc8601..7a9e6833bf6 100644 --- a/src/caffe/layers/softmax_layer.cu +++ b/src/caffe/layers/softmax_layer.cu @@ -4,7 +4,7 @@ #include "thrust/device_vector.h" -#include "caffe/common_layers.hpp" +#include "caffe/layers/softmax_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/softmax_loss_layer.cpp b/src/caffe/layers/softmax_loss_layer.cpp index 3cdef82afd2..dddb7606573 100644 --- a/src/caffe/layers/softmax_loss_layer.cpp +++ b/src/caffe/layers/softmax_loss_layer.cpp @@ -2,7 +2,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/softmax_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/softmax_loss_layer.cu b/src/caffe/layers/softmax_loss_layer.cu index 4753a1ec24b..660e1b39fe0 100644 --- a/src/caffe/layers/softmax_loss_layer.cu +++ b/src/caffe/layers/softmax_loss_layer.cu @@ -2,7 +2,7 @@ #include #include -#include "caffe/loss_layers.hpp" +#include "caffe/layers/softmax_loss_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/split_layer.cpp b/src/caffe/layers/split_layer.cpp index 5333e578f76..1a27a9af0a1 100644 --- a/src/caffe/layers/split_layer.cpp +++ b/src/caffe/layers/split_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/split_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/split_layer.cu b/src/caffe/layers/split_layer.cu index 73d04c98fc1..bec9987c7cc 100644 --- a/src/caffe/layers/split_layer.cu +++ b/src/caffe/layers/split_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/split_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/spp_layer.cpp b/src/caffe/layers/spp_layer.cpp index 2ef4ac7ab95..b9af8e8af0e 100644 --- a/src/caffe/layers/spp_layer.cpp +++ b/src/caffe/layers/spp_layer.cpp @@ -1,7 +1,12 @@ #include #include -#include "caffe/vision_layers.hpp" +#include "caffe/layer.hpp" +#include "caffe/layers/concat_layer.hpp" +#include "caffe/layers/flatten_layer.hpp" +#include "caffe/layers/pooling_layer.hpp" +#include "caffe/layers/split_layer.hpp" +#include "caffe/layers/spp_layer.hpp" namespace caffe { @@ -217,7 +222,6 @@ void SPPLayer::Backward_cpu(const vector*>& top, split_layer_->Backward(split_top_vec_, propagate_down, bottom); } - INSTANTIATE_CLASS(SPPLayer); REGISTER_LAYER_CLASS(SPP); diff --git a/src/caffe/layers/tanh_layer.cpp b/src/caffe/layers/tanh_layer.cpp index 9d1cac76184..184e926d22a 100644 --- a/src/caffe/layers/tanh_layer.cpp +++ b/src/caffe/layers/tanh_layer.cpp @@ -3,7 +3,7 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/tanh_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/tanh_layer.cu b/src/caffe/layers/tanh_layer.cu index d87bcceced7..cbfc178e6db 100644 --- a/src/caffe/layers/tanh_layer.cu +++ b/src/caffe/layers/tanh_layer.cu @@ -3,7 +3,7 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/tanh_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/threshold_layer.cpp b/src/caffe/layers/threshold_layer.cpp index d65147368fc..63822ee5520 100644 --- a/src/caffe/layers/threshold_layer.cpp +++ b/src/caffe/layers/threshold_layer.cpp @@ -1,7 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" - +#include "caffe/layers/threshold_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/threshold_layer.cu b/src/caffe/layers/threshold_layer.cu index 1cd62d99482..b0b0665589f 100644 --- a/src/caffe/layers/threshold_layer.cu +++ b/src/caffe/layers/threshold_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/threshold_layer.hpp" namespace caffe { diff --git a/src/caffe/layers/tile_layer.cpp b/src/caffe/layers/tile_layer.cpp index 581546c4fdb..cf0c187005c 100644 --- a/src/caffe/layers/tile_layer.cpp +++ b/src/caffe/layers/tile_layer.cpp @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/tile_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/tile_layer.cu b/src/caffe/layers/tile_layer.cu index fdf960901a3..282049ebd7b 100644 --- a/src/caffe/layers/tile_layer.cu +++ b/src/caffe/layers/tile_layer.cu @@ -1,6 +1,6 @@ #include -#include "caffe/common_layers.hpp" +#include "caffe/layers/tile_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { diff --git a/src/caffe/layers/window_data_layer.cpp b/src/caffe/layers/window_data_layer.cpp index 3f937bc9dd8..4ca8315d791 100644 --- a/src/caffe/layers/window_data_layer.cpp +++ b/src/caffe/layers/window_data_layer.cpp @@ -12,7 +12,10 @@ #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" -#include "caffe/data_layers.hpp" +#include "caffe/data_transformer.hpp" +#include "caffe/internal_thread.hpp" +#include "caffe/layers/base_data_layer.hpp" +#include "caffe/layers/window_data_layer.hpp" #include "caffe/util/benchmark.hpp" #include "caffe/util/io.hpp" #include "caffe/util/math_functions.hpp" diff --git a/src/caffe/test/test_accuracy_layer.cpp b/src/caffe/test/test_accuracy_layer.cpp index 5960a666734..6fe808bd5c5 100644 --- a/src/caffe/test/test_accuracy_layer.cpp +++ b/src/caffe/test/test_accuracy_layer.cpp @@ -6,7 +6,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/accuracy_layer.hpp" #include "caffe/util/rng.hpp" #include "caffe/test/test_caffe_main.hpp" diff --git a/src/caffe/test/test_argmax_layer.cpp b/src/caffe/test/test_argmax_layer.cpp index f3f2094edeb..472e6652239 100644 --- a/src/caffe/test/test_argmax_layer.cpp +++ b/src/caffe/test/test_argmax_layer.cpp @@ -5,8 +5,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/argmax_layer.hpp" #include "caffe/test/test_caffe_main.hpp" diff --git a/src/caffe/test/test_batch_norm_layer.cpp b/src/caffe/test/test_batch_norm_layer.cpp index 22b9667f31b..936b93a1756 100644 --- a/src/caffe/test/test_batch_norm_layer.cpp +++ b/src/caffe/test/test_batch_norm_layer.cpp @@ -6,8 +6,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/batch_norm_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_batch_reindex_layer.cpp b/src/caffe/test/test_batch_reindex_layer.cpp index 17e47f05066..9ea1a2f6f47 100644 --- a/src/caffe/test/test_batch_reindex_layer.cpp +++ b/src/caffe/test/test_batch_reindex_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/batch_reindex_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_concat_layer.cpp b/src/caffe/test/test_concat_layer.cpp index 8ba51f4f7f7..23c1e8c1d29 100644 --- a/src/caffe/test/test_concat_layer.cpp +++ b/src/caffe/test/test_concat_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/concat_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_contrastive_loss_layer.cpp b/src/caffe/test/test_contrastive_loss_layer.cpp index 95901f14297..2fa055ee0de 100644 --- a/src/caffe/test/test_contrastive_loss_layer.cpp +++ b/src/caffe/test/test_contrastive_loss_layer.cpp @@ -7,7 +7,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/contrastive_loss_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_convolution_layer.cpp b/src/caffe/test/test_convolution_layer.cpp index b474735716d..e2d43f31b6a 100644 --- a/src/caffe/test/test_convolution_layer.cpp +++ b/src/caffe/test/test_convolution_layer.cpp @@ -5,7 +5,11 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/conv_layer.hpp" + +#ifdef USE_CUDNN +#include "caffe/layers/cudnn_conv_layer.hpp" +#endif #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_data_layer.cpp b/src/caffe/test/test_data_layer.cpp index 9e03954a543..3e8d113d918 100644 --- a/src/caffe/test/test_data_layer.cpp +++ b/src/caffe/test/test_data_layer.cpp @@ -7,8 +7,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/data_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/data_layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/util/db.hpp" #include "caffe/util/io.hpp" diff --git a/src/caffe/test/test_deconvolution_layer.cpp b/src/caffe/test/test_deconvolution_layer.cpp index b473dbb9c51..c4b09ad555a 100644 --- a/src/caffe/test/test_deconvolution_layer.cpp +++ b/src/caffe/test/test_deconvolution_layer.cpp @@ -5,7 +5,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/deconv_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_dummy_data_layer.cpp b/src/caffe/test/test_dummy_data_layer.cpp index c9ed38db3a5..1a01ca85f89 100644 --- a/src/caffe/test/test_dummy_data_layer.cpp +++ b/src/caffe/test/test_dummy_data_layer.cpp @@ -5,8 +5,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" +#include "caffe/layers/dummy_data_layer.hpp" #include "caffe/proto/caffe.pb.h" -#include "caffe/vision_layers.hpp" #include "caffe/test/test_caffe_main.hpp" diff --git a/src/caffe/test/test_eltwise_layer.cpp b/src/caffe/test/test_eltwise_layer.cpp index 3b56c5cae36..c06e3baab15 100644 --- a/src/caffe/test/test_eltwise_layer.cpp +++ b/src/caffe/test/test_eltwise_layer.cpp @@ -5,8 +5,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/eltwise_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_embed_layer.cpp b/src/caffe/test/test_embed_layer.cpp index 0f4caf15742..acd4b0f636b 100644 --- a/src/caffe/test/test_embed_layer.cpp +++ b/src/caffe/test/test_embed_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/embed_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_euclidean_loss_layer.cpp b/src/caffe/test/test_euclidean_loss_layer.cpp index 9dc14de4149..f253f9fd393 100644 --- a/src/caffe/test/test_euclidean_loss_layer.cpp +++ b/src/caffe/test/test_euclidean_loss_layer.cpp @@ -6,7 +6,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/euclidean_loss_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_filter_layer.cpp b/src/caffe/test/test_filter_layer.cpp index a2d0c293644..9ea2b8b2168 100644 --- a/src/caffe/test/test_filter_layer.cpp +++ b/src/caffe/test/test_filter_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/filter_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_flatten_layer.cpp b/src/caffe/test/test_flatten_layer.cpp index 5d1caac2aa7..d929ac7a720 100644 --- a/src/caffe/test/test_flatten_layer.cpp +++ b/src/caffe/test/test_flatten_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/flatten_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_hdf5_output_layer.cpp b/src/caffe/test/test_hdf5_output_layer.cpp index adc27df4ad0..3833ebff78e 100644 --- a/src/caffe/test/test_hdf5_output_layer.cpp +++ b/src/caffe/test/test_hdf5_output_layer.cpp @@ -5,7 +5,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/data_layers.hpp" +#include "caffe/layers/hdf5_output_layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/util/hdf5.hpp" #include "caffe/util/io.hpp" diff --git a/src/caffe/test/test_hdf5data_layer.cpp b/src/caffe/test/test_hdf5data_layer.cpp index 7169e7bfc41..8884ce95a23 100644 --- a/src/caffe/test/test_hdf5data_layer.cpp +++ b/src/caffe/test/test_hdf5data_layer.cpp @@ -1,11 +1,13 @@ #include #include +#include "hdf5.h" + #include "gtest/gtest.h" #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/data_layers.hpp" +#include "caffe/layers/hdf5_data_layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/test/test_caffe_main.hpp" diff --git a/src/caffe/test/test_hinge_loss_layer.cpp b/src/caffe/test/test_hinge_loss_layer.cpp index dfdd01d0291..8bf89fa6387 100644 --- a/src/caffe/test/test_hinge_loss_layer.cpp +++ b/src/caffe/test/test_hinge_loss_layer.cpp @@ -6,7 +6,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/hinge_loss_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_im2col_kernel.cu b/src/caffe/test/test_im2col_kernel.cu index bafcacf784e..3f97cf6d5ae 100644 --- a/src/caffe/test/test_im2col_kernel.cu +++ b/src/caffe/test/test_im2col_kernel.cu @@ -5,8 +5,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/im2col_layer.hpp" #include "caffe/util/im2col.hpp" -#include "caffe/vision_layers.hpp" #include "caffe/test/test_caffe_main.hpp" diff --git a/src/caffe/test/test_im2col_layer.cpp b/src/caffe/test/test_im2col_layer.cpp index ec055b20176..8274dd48971 100644 --- a/src/caffe/test/test_im2col_layer.cpp +++ b/src/caffe/test/test_im2col_layer.cpp @@ -5,7 +5,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/im2col_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_image_data_layer.cpp b/src/caffe/test/test_image_data_layer.cpp index 77690245111..a4080ccd145 100644 --- a/src/caffe/test/test_image_data_layer.cpp +++ b/src/caffe/test/test_image_data_layer.cpp @@ -7,8 +7,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/data_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/image_data_layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/util/io.hpp" diff --git a/src/caffe/test/test_infogain_loss_layer.cpp b/src/caffe/test/test_infogain_loss_layer.cpp index b2a6754fd40..a24ac683dc5 100644 --- a/src/caffe/test/test_infogain_loss_layer.cpp +++ b/src/caffe/test/test_infogain_loss_layer.cpp @@ -5,7 +5,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/infogain_loss_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_inner_product_layer.cpp b/src/caffe/test/test_inner_product_layer.cpp index 1ad2c97e75a..b888b510318 100644 --- a/src/caffe/test/test_inner_product_layer.cpp +++ b/src/caffe/test/test_inner_product_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/inner_product_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_lrn_layer.cpp b/src/caffe/test/test_lrn_layer.cpp index bd1c4fe8810..4c97b1ae07b 100644 --- a/src/caffe/test/test_lrn_layer.cpp +++ b/src/caffe/test/test_lrn_layer.cpp @@ -6,7 +6,12 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/lrn_layer.hpp" + +#ifdef USE_CUDNN +#include "caffe/layers/cudnn_lcn_layer.hpp" +#include "caffe/layers/cudnn_lrn_layer.hpp" +#endif #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_maxpool_dropout_layers.cpp b/src/caffe/test/test_maxpool_dropout_layers.cpp index 8fc944f3250..4f0e20ac3a7 100644 --- a/src/caffe/test/test_maxpool_dropout_layers.cpp +++ b/src/caffe/test/test_maxpool_dropout_layers.cpp @@ -5,7 +5,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/dropout_layer.hpp" +#include "caffe/layers/pooling_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_memory_data_layer.cpp b/src/caffe/test/test_memory_data_layer.cpp index 7269a4d441b..7998bc18262 100644 --- a/src/caffe/test/test_memory_data_layer.cpp +++ b/src/caffe/test/test_memory_data_layer.cpp @@ -5,8 +5,8 @@ #include #include -#include "caffe/data_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/memory_data_layer.hpp" #include "caffe/test/test_caffe_main.hpp" diff --git a/src/caffe/test/test_multinomial_logistic_loss_layer.cpp b/src/caffe/test/test_multinomial_logistic_loss_layer.cpp index 0404aa25af6..8cc21022305 100644 --- a/src/caffe/test/test_multinomial_logistic_loss_layer.cpp +++ b/src/caffe/test/test_multinomial_logistic_loss_layer.cpp @@ -5,7 +5,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/multinomial_logistic_loss_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_mvn_layer.cpp b/src/caffe/test/test_mvn_layer.cpp index e9a7d54ce4a..28a762d2741 100644 --- a/src/caffe/test/test_mvn_layer.cpp +++ b/src/caffe/test/test_mvn_layer.cpp @@ -2,8 +2,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/mvn_layer.hpp" #include "google/protobuf/text_format.h" #include "gtest/gtest.h" diff --git a/src/caffe/test/test_neuron_layer.cpp b/src/caffe/test/test_neuron_layer.cpp index b333fdee802..21441b4121e 100644 --- a/src/caffe/test/test_neuron_layer.cpp +++ b/src/caffe/test/test_neuron_layer.cpp @@ -6,9 +6,26 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" -#include "caffe/neuron_layers.hpp" + +#include "caffe/layers/absval_layer.hpp" +#include "caffe/layers/bnll_layer.hpp" +#include "caffe/layers/dropout_layer.hpp" +#include "caffe/layers/exp_layer.hpp" +#include "caffe/layers/inner_product_layer.hpp" +#include "caffe/layers/log_layer.hpp" +#include "caffe/layers/power_layer.hpp" +#include "caffe/layers/prelu_layer.hpp" +#include "caffe/layers/relu_layer.hpp" +#include "caffe/layers/sigmoid_layer.hpp" +#include "caffe/layers/tanh_layer.hpp" +#include "caffe/layers/threshold_layer.hpp" + +#ifdef USE_CUDNN +#include "caffe/layers/cudnn_relu_layer.hpp" +#include "caffe/layers/cudnn_sigmoid_layer.hpp" +#include "caffe/layers/cudnn_tanh_layer.hpp" +#endif #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_pooling_layer.cpp b/src/caffe/test/test_pooling_layer.cpp index 9e986e66528..bb95cae032d 100644 --- a/src/caffe/test/test_pooling_layer.cpp +++ b/src/caffe/test/test_pooling_layer.cpp @@ -5,7 +5,11 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/pooling_layer.hpp" + +#ifdef USE_CUDNN +#include "caffe/layers/cudnn_pooling_layer.hpp" +#endif #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_power_layer.cpp b/src/caffe/test/test_power_layer.cpp index 1041ddd4ee8..1aa587ac97a 100644 --- a/src/caffe/test/test_power_layer.cpp +++ b/src/caffe/test/test_power_layer.cpp @@ -6,7 +6,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/power_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_reduction_layer.cpp b/src/caffe/test/test_reduction_layer.cpp index a8d43727113..6ed7cda6adc 100644 --- a/src/caffe/test/test_reduction_layer.cpp +++ b/src/caffe/test/test_reduction_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/reduction_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_reshape_layer.cpp b/src/caffe/test/test_reshape_layer.cpp index e0f4ba42851..4f2613868d4 100644 --- a/src/caffe/test/test_reshape_layer.cpp +++ b/src/caffe/test/test_reshape_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/reshape_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_sigmoid_cross_entropy_loss_layer.cpp b/src/caffe/test/test_sigmoid_cross_entropy_loss_layer.cpp index b4f831c8f2d..5dfd7656db2 100644 --- a/src/caffe/test/test_sigmoid_cross_entropy_loss_layer.cpp +++ b/src/caffe/test/test_sigmoid_cross_entropy_loss_layer.cpp @@ -6,7 +6,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/sigmoid_cross_entropy_loss_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_slice_layer.cpp b/src/caffe/test/test_slice_layer.cpp index 45fbcffda3f..c2b231e1ef4 100644 --- a/src/caffe/test/test_slice_layer.cpp +++ b/src/caffe/test/test_slice_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/slice_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_softmax_layer.cpp b/src/caffe/test/test_softmax_layer.cpp index 4b01f5cfbab..94443576724 100644 --- a/src/caffe/test/test_softmax_layer.cpp +++ b/src/caffe/test/test_softmax_layer.cpp @@ -5,8 +5,12 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/softmax_layer.hpp" + +#ifdef USE_CUDNN +#include "caffe/layers/cudnn_softmax_layer.hpp" +#endif #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_softmax_with_loss_layer.cpp b/src/caffe/test/test_softmax_with_loss_layer.cpp index 0ae4cd6815a..c67f3e0d907 100644 --- a/src/caffe/test/test_softmax_with_loss_layer.cpp +++ b/src/caffe/test/test_softmax_with_loss_layer.cpp @@ -7,7 +7,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/loss_layers.hpp" +#include "caffe/layers/softmax_loss_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_split_layer.cpp b/src/caffe/test/test_split_layer.cpp index e27e355c77e..ba2ccbb2b18 100644 --- a/src/caffe/test/test_split_layer.cpp +++ b/src/caffe/test/test_split_layer.cpp @@ -6,8 +6,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/split_layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/util/insert_splits.hpp" diff --git a/src/caffe/test/test_spp_layer.cpp b/src/caffe/test/test_spp_layer.cpp index 1b48a842d3f..59a3af2aec1 100644 --- a/src/caffe/test/test_spp_layer.cpp +++ b/src/caffe/test/test_spp_layer.cpp @@ -5,7 +5,12 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/concat_layer.hpp" +#include "caffe/layers/flatten_layer.hpp" +#include "caffe/layers/pooling_layer.hpp" +#include "caffe/layers/split_layer.hpp" +#include "caffe/layers/spp_layer.hpp" + #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_stochastic_pooling.cpp b/src/caffe/test/test_stochastic_pooling.cpp index 5a412bd4e17..cd5db8383ab 100644 --- a/src/caffe/test/test_stochastic_pooling.cpp +++ b/src/caffe/test/test_stochastic_pooling.cpp @@ -6,7 +6,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/vision_layers.hpp" +#include "caffe/layers/pooling_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_tanh_layer.cpp b/src/caffe/test/test_tanh_layer.cpp index f31579cac40..bb8699a8e91 100644 --- a/src/caffe/test/test_tanh_layer.cpp +++ b/src/caffe/test/test_tanh_layer.cpp @@ -6,7 +6,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/tanh_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/test/test_threshold_layer.cpp b/src/caffe/test/test_threshold_layer.cpp index 903a9bc8157..1e84cc5ab84 100644 --- a/src/caffe/test/test_threshold_layer.cpp +++ b/src/caffe/test/test_threshold_layer.cpp @@ -5,7 +5,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/filler.hpp" -#include "caffe/neuron_layers.hpp" +#include "caffe/layers/threshold_layer.hpp" #include "caffe/test/test_caffe_main.hpp" diff --git a/src/caffe/test/test_tile_layer.cpp b/src/caffe/test/test_tile_layer.cpp index 5c459604a6b..7ff75520e8e 100644 --- a/src/caffe/test/test_tile_layer.cpp +++ b/src/caffe/test/test_tile_layer.cpp @@ -4,8 +4,8 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" -#include "caffe/common_layers.hpp" #include "caffe/filler.hpp" +#include "caffe/layers/tile_layer.hpp" #include "caffe/test/test_caffe_main.hpp" #include "caffe/test/test_gradient_check_util.hpp" diff --git a/src/caffe/util/blocking_queue.cpp b/src/caffe/util/blocking_queue.cpp index d1d1fa864c3..058668fe28c 100644 --- a/src/caffe/util/blocking_queue.cpp +++ b/src/caffe/util/blocking_queue.cpp @@ -1,8 +1,8 @@ #include #include -#include "caffe/data_layers.hpp" #include "caffe/data_reader.hpp" +#include "caffe/layers/base_data_layer.hpp" #include "caffe/parallel.hpp" #include "caffe/util/blocking_queue.hpp" diff --git a/tools/extract_features.cpp b/tools/extract_features.cpp index b94dbb980fd..1ef132660b8 100644 --- a/tools/extract_features.cpp +++ b/tools/extract_features.cpp @@ -11,7 +11,6 @@ #include "caffe/util/db.hpp" #include "caffe/util/format.hpp" #include "caffe/util/io.hpp" -#include "caffe/vision_layers.hpp" using caffe::Blob; using caffe::Caffe;