From: Sergey Karayev Date: Tue, 29 Apr 2014 02:06:07 +0000 (-0700) Subject: layer definition reorganization and documentation X-Git-Tag: submit/tizen/20180823.020014~692^2~2^2~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2779ce9e0e121a7bec6de90ca304a316cc150cf2;p=platform%2Fupstream%2Fcaffeonacl.git layer definition reorganization and documentation - split out neuron, loss, and data layers into own header files - added LossLayer class with common SetUp checks - in-progress concise documentation of each layer's purpose --- diff --git a/include/caffe/data_layers.hpp b/include/caffe/data_layers.hpp new file mode 100644 index 0000000..a1742fe --- /dev/null +++ b/include/caffe/data_layers.hpp @@ -0,0 +1,215 @@ +// Copyright 2014 BVLC and contributors. + +#ifndef CAFFE_DATA_LAYERS_HPP_ +#define CAFFE_DATA_LAYERS_HPP_ + +#include +#include +#include + +#include "leveldb/db.h" +#include "pthread.h" +#include "hdf5.h" +#include "boost/scoped_ptr.hpp" + +#include "caffe/blob.hpp" +#include "caffe/common.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" + +// This function is used to create a pthread that prefetches the data. +template +void* DataLayerPrefetch(void* layer_pointer); + +template +class DataLayer : public Layer { + // The function used to perform prefetching. + friend void* DataLayerPrefetch(void* layer_pointer); + + public: + explicit DataLayer(const LayerParameter& param) + : Layer(param) {} + virtual ~DataLayer(); + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + + virtual void CreatePrefetchThread(); + virtual void JoinPrefetchThread(); + virtual unsigned int PrefetchRand(); + + shared_ptr prefetch_rng_; + shared_ptr db_; + shared_ptr iter_; + int datum_channels_; + int datum_height_; + int datum_width_; + int datum_size_; + pthread_t thread_; + shared_ptr > prefetch_data_; + shared_ptr > prefetch_label_; + Blob data_mean_; + bool output_labels_; + Caffe::Phase phase_; +}; + +template +class HDF5OutputLayer : public Layer { + public: + explicit HDF5OutputLayer(const LayerParameter& param); + virtual ~HDF5OutputLayer(); + virtual void SetUp(const vector*>& bottom, + vector*>* top); + inline std::string file_name() const { return file_name_; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void SaveBlobs(); + + std::string file_name_; + hid_t file_id_; + Blob data_blob_; + Blob label_blob_; +}; + + +template +class HDF5DataLayer : public Layer { + public: + explicit HDF5DataLayer(const LayerParameter& param) + : Layer(param) {} + virtual ~HDF5DataLayer(); + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void LoadHDF5FileData(const char* filename); + + std::vector hdf_filenames_; + unsigned int num_files_; + unsigned int current_file_; + hsize_t current_row_; + Blob data_blob_; + Blob label_blob_; +}; + +// This function is used to create a pthread that prefetches the data. +template +void* ImageDataLayerPrefetch(void* layer_pointer); + +template +class ImageDataLayer : public Layer { + // The function used to perform prefetching. + friend void* ImageDataLayerPrefetch(void* layer_pointer); + + public: + explicit ImageDataLayer(const LayerParameter& param) + : Layer(param) {} + virtual ~ImageDataLayer(); + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + + virtual void ShuffleImages(); + + virtual void CreatePrefetchThread(); + virtual void JoinPrefetchThread(); + virtual unsigned int PrefetchRand(); + + shared_ptr prefetch_rng_; + vector > lines_; + int lines_id_; + int datum_channels_; + int datum_height_; + int datum_width_; + int datum_size_; + pthread_t thread_; + shared_ptr > prefetch_data_; + shared_ptr > prefetch_label_; + Blob data_mean_; + Caffe::Phase phase_; +}; + + +// This function is used to create a pthread that prefetches the window data. +template +void* WindowDataLayerPrefetch(void* layer_pointer); + +template +class WindowDataLayer : public Layer { + // The function used to perform prefetching. + friend void* WindowDataLayerPrefetch(void* layer_pointer); + + public: + explicit WindowDataLayer(const LayerParameter& param) + : Layer(param) {} + virtual ~WindowDataLayer(); + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + + virtual void CreatePrefetchThread(); + virtual void JoinPrefetchThread(); + virtual unsigned int PrefetchRand(); + + shared_ptr prefetch_rng_; + pthread_t thread_; + shared_ptr > prefetch_data_; + shared_ptr > prefetch_label_; + Blob data_mean_; + vector > > image_database_; + enum WindowField { IMAGE_INDEX, LABEL, OVERLAP, X1, Y1, X2, Y2, NUM }; + vector > fg_windows_; + vector > bg_windows_; +}; + +} // namespace caffe + +#endif // CAFFE_DATA_LAYERS_HPP_ diff --git a/include/caffe/loss_layers.hpp b/include/caffe/loss_layers.hpp new file mode 100644 index 0000000..6ddfcc4 --- /dev/null +++ b/include/caffe/loss_layers.hpp @@ -0,0 +1,150 @@ +// Copyright 2014 BVLC and contributors. + +#ifndef CAFFE_LOSS_LAYERS_HPP_ +#define CAFFE_LOSS_LAYERS_HPP_ + +#include +#include +#include + +#include "leveldb/db.h" +#include "pthread.h" +#include "boost/scoped_ptr.hpp" +#include "hdf5.h" + +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/layer.hpp" +#include "caffe/neuron_layers.hpp" +#include "caffe/proto/caffe.pb.h" + +namespace caffe { + +// LossLayer takes two inputs of same num, and has no output. +template +class LossLayer : public Layer { + public: + explicit LossLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp( + const vector*>& bottom, vector*>* top); + virtual void FurtherSetUp( + const vector*>& bottom, vector*>* top); +}; + +// SigmoidCrossEntropyLossLayer +template +class SigmoidCrossEntropyLossLayer : public LossLayer { + public: + explicit SigmoidCrossEntropyLossLayer(const LayerParameter& param) + : LossLayer(param), + sigmoid_layer_(new SigmoidLayer(param)), + sigmoid_output_(new Blob()) {} + virtual void FurtherSetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + + shared_ptr > sigmoid_layer_; + // sigmoid_output stores the output of the sigmoid layer. + shared_ptr > sigmoid_output_; + // Vector holders to call the underlying sigmoid layer forward and backward. + vector*> sigmoid_bottom_vec_; + vector*> sigmoid_top_vec_; +}; + +// EuclideanLossLayer: compute y = 1/2 \sum_i (x_i - x'_i)^2 +template +class EuclideanLossLayer : public LossLayer { + public: + explicit EuclideanLossLayer(const LayerParameter& param) + : LossLayer(param), difference_() {} + virtual void FurtherSetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + + Blob difference_; +}; + + +template +class InfogainLossLayer : public LossLayer { + public: + explicit InfogainLossLayer(const LayerParameter& param) + : LossLayer(param), infogain_() {} + virtual void FurtherSetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + + Blob infogain_; +}; + +template +class HingeLossLayer : public LossLayer { + public: + explicit HingeLossLayer(const LayerParameter& param) + : LossLayer(param) {} + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); +}; + +template +class MultinomialLogisticLossLayer : public LossLayer { + public: + explicit MultinomialLogisticLossLayer(const LayerParameter& param) + : LossLayer(param) {} + virtual void FurtherSetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); +}; + +// AccuracyLayer: not an actual loss layer; +// computes the accuracy and logprob of x with respect to y'. +template +class AccuracyLayer : public Layer { + public: + explicit AccuracyLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + // The accuracy layer should not be used to compute backward operations. + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { + NOT_IMPLEMENTED; + } +}; + +} // namespace caffe + +#endif // CAFFE_LOSS_LAYERS_HPP_ diff --git a/include/caffe/neuron_layers.hpp b/include/caffe/neuron_layers.hpp new file mode 100644 index 0000000..68ebc2b --- /dev/null +++ b/include/caffe/neuron_layers.hpp @@ -0,0 +1,160 @@ +// Copyright 2014 BVLC and contributors. + +#ifndef CAFFE_NEURON_LAYERS_HPP_ +#define CAFFE_NEURON_LAYERS_HPP_ + +#include +#include +#include + +#include "leveldb/db.h" +#include "pthread.h" +#include "boost/scoped_ptr.hpp" +#include "hdf5.h" + +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/layer.hpp" +#include "caffe/proto/caffe.pb.h" + +#define HDF5_DATA_DATASET_NAME "data" +#define HDF5_DATA_LABEL_NAME "label" + +namespace caffe { + +// NeuronLayer is an interface for layers that work on single elements. +template +class NeuronLayer : public Layer { + public: + explicit NeuronLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); +}; + +// BNLLLayer +template +class BNLLLayer : public NeuronLayer { + public: + explicit BNLLLayer(const LayerParameter& param) + : NeuronLayer(param) {} + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); +}; + +// DropoutLayer sets random portion of its input to 0. +template +class DropoutLayer : public NeuronLayer { + public: + explicit DropoutLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + + shared_ptr rand_vec_; + Dtype threshold_; + Dtype scale_; + unsigned int uint_thres_; +}; + +// PowerLayer computes y = (shift + scale * x)^power +template +class PowerLayer : public NeuronLayer { + public: + explicit PowerLayer(const LayerParameter& param) + : NeuronLayer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + + Dtype power_; + Dtype scale_; + Dtype shift_; + Dtype diff_scale_; +}; + +// ReLULayer computes y = max(0, x). +template +class ReLULayer : public NeuronLayer { + public: + explicit ReLULayer(const LayerParameter& param) + : NeuronLayer(param) {} + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); +}; + +// SigmoidLayer computes y = 1. / (1 + exp(-x)) +template +class SigmoidLayer : public NeuronLayer { + public: + explicit SigmoidLayer(const LayerParameter& param) + : NeuronLayer(param) {} + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); +}; + +// TanHLayer: computes y = 1. * (exp(2 * x) - 1) / (exp(2 * x) + 1) +template +class TanHLayer : public NeuronLayer { + public: + explicit TanHLayer(const LayerParameter& param) + : NeuronLayer(param) {} + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); +}; + +} // namespace caffe + +#endif // CAFFE_NEURON_LAYERS_HPP_ diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index 4765398..7334ada 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -7,197 +7,16 @@ #include #include -#include "leveldb/db.h" -#include "pthread.h" -#include "boost/scoped_ptr.hpp" -#include "hdf5.h" - #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/layer.hpp" +#include "caffe/neuron_layers.hpp" +#include "caffe/loss_layers.hpp" +#include "caffe/data_layers.hpp" #include "caffe/proto/caffe.pb.h" -#define HDF5_DATA_DATASET_NAME "data" -#define HDF5_DATA_LABEL_NAME "label" - namespace caffe { - -// The neuron layer is a specific type of layers that just works on single -// celements. -template -class NeuronLayer : public Layer { - public: - explicit NeuronLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); -}; - -template -class BNLLLayer : public NeuronLayer { - public: - explicit BNLLLayer(const LayerParameter& param) - : NeuronLayer(param) {} - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); -}; - -template -class DropoutLayer : public NeuronLayer { - public: - explicit DropoutLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - - shared_ptr rand_vec_; - Dtype threshold_; - Dtype scale_; - unsigned int uint_thres_; -}; - -template -class PowerLayer : public NeuronLayer { - public: - explicit PowerLayer(const LayerParameter& param) - : NeuronLayer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - - Dtype power_; - Dtype scale_; - Dtype shift_; - Dtype diff_scale_; -}; - -template -class ReLULayer : public NeuronLayer { - public: - explicit ReLULayer(const LayerParameter& param) - : NeuronLayer(param) {} - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); -}; - -template -class SigmoidLayer : public NeuronLayer { - public: - explicit SigmoidLayer(const LayerParameter& param) - : NeuronLayer(param) {} - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); -}; - -template -class SigmoidCrossEntropyLossLayer : public Layer { - public: - explicit SigmoidCrossEntropyLossLayer(const LayerParameter& param) - : Layer(param), - sigmoid_layer_(new SigmoidLayer(param)), - sigmoid_output_(new Blob()) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - - shared_ptr > sigmoid_layer_; - // sigmoid_output stores the output of the sigmoid layer. - shared_ptr > sigmoid_output_; - // Vector holders to call the underlying sigmoid layer forward and backward. - vector*> sigmoid_bottom_vec_; - vector*> sigmoid_top_vec_; -}; - -template -class TanHLayer : public NeuronLayer { - public: - explicit TanHLayer(const LayerParameter& param) - : NeuronLayer(param) {} - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); -}; - - -template -class AccuracyLayer : public Layer { - public: - explicit AccuracyLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - // The accuracy layer should not be used to compute backward operations. - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { - NOT_IMPLEMENTED; - } -}; - template class ConcatLayer : public Layer { public: @@ -260,50 +79,6 @@ class ConvolutionLayer : public Layer { int N_; }; -// This function is used to create a pthread that prefetches the data. -template -void* DataLayerPrefetch(void* layer_pointer); - -template -class DataLayer : public Layer { - // The function used to perform prefetching. - friend void* DataLayerPrefetch(void* layer_pointer); - - public: - explicit DataLayer(const LayerParameter& param) - : Layer(param) {} - virtual ~DataLayer(); - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } - - virtual void CreatePrefetchThread(); - virtual void JoinPrefetchThread(); - virtual unsigned int PrefetchRand(); - - shared_ptr prefetch_rng_; - shared_ptr db_; - shared_ptr iter_; - int datum_channels_; - int datum_height_; - int datum_width_; - int datum_size_; - pthread_t thread_; - shared_ptr > prefetch_data_; - shared_ptr > prefetch_label_; - Blob data_mean_; - bool output_labels_; - Caffe::Phase phase_; -}; template class EltwiseProductLayer : public Layer { @@ -325,27 +100,6 @@ class EltwiseProductLayer : public Layer { }; template -class EuclideanLossLayer : public Layer { - public: - explicit EuclideanLossLayer(const LayerParameter& param) - : Layer(param), difference_() {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - // virtual Dtype Forward_gpu(const vector*>& bottom, - // vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - // virtual void Backward_gpu(const vector*>& top, - // const bool propagate_down, vector*>* bottom); - - Blob difference_; -}; - -template class FlattenLayer : public Layer { public: explicit FlattenLayer(const LayerParameter& param) @@ -367,79 +121,6 @@ class FlattenLayer : public Layer { }; template -class HDF5OutputLayer : public Layer { - public: - explicit HDF5OutputLayer(const LayerParameter& param); - virtual ~HDF5OutputLayer(); - virtual void SetUp(const vector*>& bottom, - vector*>* top); - inline std::string file_name() const { return file_name_; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void SaveBlobs(); - - std::string file_name_; - hid_t file_id_; - Blob data_blob_; - Blob label_blob_; -}; - -template -class HDF5DataLayer : public Layer { - public: - explicit HDF5DataLayer(const LayerParameter& param) - : Layer(param) {} - virtual ~HDF5DataLayer(); - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void LoadHDF5FileData(const char* filename); - - std::vector hdf_filenames_; - unsigned int num_files_; - unsigned int current_file_; - hsize_t current_row_; - Blob data_blob_; - Blob label_blob_; -}; - -template -class HingeLossLayer : public Layer { - public: - explicit HingeLossLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - // virtual Dtype Forward_gpu(const vector*>& bottom, - // vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - // virtual void Backward_gpu(const vector*>& top, - // const bool propagate_down, vector*>* bottom); -}; - -template class Im2colLayer : public Layer { public: explicit Im2colLayer(const LayerParameter& param) @@ -465,73 +146,6 @@ class Im2colLayer : public Layer { int pad_; }; -// This function is used to create a pthread that prefetches the data. -template -void* ImageDataLayerPrefetch(void* layer_pointer); - -template -class ImageDataLayer : public Layer { - // The function used to perform prefetching. - friend void* ImageDataLayerPrefetch(void* layer_pointer); - - public: - explicit ImageDataLayer(const LayerParameter& param) - : Layer(param) {} - virtual ~ImageDataLayer(); - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } - - virtual void ShuffleImages(); - - virtual void CreatePrefetchThread(); - virtual void JoinPrefetchThread(); - virtual unsigned int PrefetchRand(); - - shared_ptr prefetch_rng_; - vector > lines_; - int lines_id_; - int datum_channels_; - int datum_height_; - int datum_width_; - int datum_size_; - pthread_t thread_; - shared_ptr > prefetch_data_; - shared_ptr > prefetch_label_; - Blob data_mean_; - Caffe::Phase phase_; -}; - -template -class InfogainLossLayer : public Layer { - public: - explicit InfogainLossLayer(const LayerParameter& param) - : Layer(param), infogain_() {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - // virtual Dtype Forward_gpu(const vector*>& bottom, - // vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - // virtual void Backward_gpu(const vector*>& top, - // const bool propagate_down, vector*>* bottom); - - Blob infogain_; -}; - template class InnerProductLayer : public Layer { public: @@ -659,25 +273,6 @@ class MemoryDataLayer : public Layer { }; template -class MultinomialLogisticLossLayer : public Layer { - public: - explicit MultinomialLogisticLossLayer(const LayerParameter& param) - : Layer(param) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - // virtual Dtype Forward_gpu(const vector*>& bottom, - // vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - // virtual void Backward_gpu(const vector*>& top, - // const bool propagate_down, vector*>* bottom); -}; - -template class PoolingLayer : public Layer { public: explicit PoolingLayer(const LayerParameter& param) @@ -782,48 +377,6 @@ class SplitLayer : public Layer { int count_; }; -// This function is used to create a pthread that prefetches the window data. -template -void* WindowDataLayerPrefetch(void* layer_pointer); - -template -class WindowDataLayer : public Layer { - // The function used to perform prefetching. - friend void* WindowDataLayerPrefetch(void* layer_pointer); - - public: - explicit WindowDataLayer(const LayerParameter& param) - : Layer(param) {} - virtual ~WindowDataLayer(); - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { return; } - - virtual void CreatePrefetchThread(); - virtual void JoinPrefetchThread(); - virtual unsigned int PrefetchRand(); - - shared_ptr prefetch_rng_; - pthread_t thread_; - shared_ptr > prefetch_data_; - shared_ptr > prefetch_label_; - Blob data_mean_; - vector > > image_database_; - enum WindowField { IMAGE_INDEX, LABEL, OVERLAP, X1, Y1, X2, Y2, NUM }; - vector > fg_windows_; - vector > bg_windows_; -}; - - } // namespace caffe #endif // CAFFE_VISION_LAYERS_HPP_ diff --git a/src/caffe/layers/loss_layer.cpp b/src/caffe/layers/loss_layer.cpp index b7f812a..3fc34a6 100644 --- a/src/caffe/layers/loss_layer.cpp +++ b/src/caffe/layers/loss_layer.cpp @@ -17,12 +17,24 @@ namespace caffe { const float kLOG_THRESHOLD = 1e-20; template -void MultinomialLogisticLossLayer::SetUp( +void LossLayer::SetUp( const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 2) << "Loss Layer takes two blobs as input."; CHECK_EQ(top->size(), 0) << "Loss Layer takes no output."; CHECK_EQ(bottom[0]->num(), bottom[1]->num()) << "The data and label should have the same number."; + FurtherSetUp(bottom, top); +} + +template +void LossLayer::FurtherSetUp( + const vector*>& bottom, vector*>* top) { + // Nothing to do +} + +template +void MultinomialLogisticLossLayer::FurtherSetUp( + const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom[1]->channels(), 1); CHECK_EQ(bottom[1]->height(), 1); CHECK_EQ(bottom[1]->width(), 1); @@ -63,18 +75,15 @@ void MultinomialLogisticLossLayer::Backward_cpu( template -void InfogainLossLayer::SetUp( +void InfogainLossLayer::FurtherSetUp( const vector*>& bottom, vector*>* top) { - CHECK_EQ(bottom.size(), 2) << "Loss Layer takes two blobs as input."; - CHECK_EQ(top->size(), 0) << "Loss Layer takes no output."; - CHECK_EQ(bottom[0]->num(), bottom[1]->num()) - << "The data and label should have the same number."; CHECK_EQ(bottom[1]->channels(), 1); CHECK_EQ(bottom[1]->height(), 1); CHECK_EQ(bottom[1]->width(), 1); + BlobProto blob_proto; - ReadProtoFromBinaryFile(this->layer_param_.infogain_loss_param().source(), - &blob_proto); + ReadProtoFromBinaryFile( + this->layer_param_.infogain_loss_param().source(), &blob_proto); infogain_.FromProto(blob_proto); CHECK_EQ(infogain_.num(), 1); CHECK_EQ(infogain_.channels(), 1); @@ -124,12 +133,8 @@ void InfogainLossLayer::Backward_cpu(const vector*>& top, template -void EuclideanLossLayer::SetUp( +void EuclideanLossLayer::FurtherSetUp( const vector*>& bottom, vector*>* top) { - CHECK_EQ(bottom.size(), 2) << "Loss Layer takes two blobs as input."; - CHECK_EQ(top->size(), 0) << "Loss Layer takes no as output."; - CHECK_EQ(bottom[0]->num(), bottom[1]->num()) - << "The data and label should have the same number."; CHECK_EQ(bottom[0]->channels(), bottom[1]->channels()); CHECK_EQ(bottom[0]->height(), bottom[1]->height()); CHECK_EQ(bottom[0]->width(), bottom[1]->width()); @@ -160,6 +165,44 @@ void EuclideanLossLayer::Backward_cpu(const vector*>& top, } template +Dtype HingeLossLayer::Forward_cpu(const vector*>& bottom, + vector*>* top) { + const Dtype* bottom_data = bottom[0]->cpu_data(); + Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); + const Dtype* label = bottom[1]->cpu_data(); + int num = bottom[0]->num(); + int count = bottom[0]->count(); + int dim = count / num; + + caffe_copy(count, bottom_data, bottom_diff); + for (int i = 0; i < num; ++i) { + bottom_diff[i * dim + static_cast(label[i])] *= -1; + } + for (int i = 0; i < num; ++i) { + for (int j = 0; j < dim; ++j) { + bottom_diff[i * dim + j] = max(Dtype(0), 1 + bottom_diff[i * dim + j]); + } + } + return caffe_cpu_asum(count, bottom_diff) / num; +} + +template +void HingeLossLayer::Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { + Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff(); + const Dtype* label = (*bottom)[1]->cpu_data(); + int num = (*bottom)[0]->num(); + int count = (*bottom)[0]->count(); + int dim = count / num; + + caffe_cpu_sign(count, bottom_diff, bottom_diff); + for (int i = 0; i < num; ++i) { + bottom_diff[i * dim + static_cast(label[i])] *= -1; + } + caffe_scal(count, Dtype(1. / num), bottom_diff); +} + +template void AccuracyLayer::SetUp( const vector*>& bottom, vector*>* top) { CHECK_EQ(bottom.size(), 2) << "Accuracy Layer takes two blobs as input."; @@ -205,55 +248,10 @@ Dtype AccuracyLayer::Forward_cpu(const vector*>& bottom, return Dtype(0); } -template -void HingeLossLayer::SetUp(const vector*>& bottom, - vector*>* top) { - CHECK_EQ(bottom.size(), 2) << "Hinge Loss Layer takes two blobs as input."; - CHECK_EQ(top->size(), 0) << "Hinge Loss Layer takes no output."; -} - -template -Dtype HingeLossLayer::Forward_cpu(const vector*>& bottom, - vector*>* top) { - const Dtype* bottom_data = bottom[0]->cpu_data(); - Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); - const Dtype* label = bottom[1]->cpu_data(); - int num = bottom[0]->num(); - int count = bottom[0]->count(); - int dim = count / num; - - caffe_copy(count, bottom_data, bottom_diff); - for (int i = 0; i < num; ++i) { - bottom_diff[i * dim + static_cast(label[i])] *= -1; - } - for (int i = 0; i < num; ++i) { - for (int j = 0; j < dim; ++j) { - bottom_diff[i * dim + j] = max(Dtype(0), 1 + bottom_diff[i * dim + j]); - } - } - return caffe_cpu_asum(count, bottom_diff) / num; -} - -template -void HingeLossLayer::Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom) { - Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff(); - const Dtype* label = (*bottom)[1]->cpu_data(); - int num = (*bottom)[0]->num(); - int count = (*bottom)[0]->count(); - int dim = count / num; - - caffe_cpu_sign(count, bottom_diff, bottom_diff); - for (int i = 0; i < num; ++i) { - bottom_diff[i * dim + static_cast(label[i])] *= -1; - } - caffe_scal(count, Dtype(1. / num), bottom_diff); -} - INSTANTIATE_CLASS(MultinomialLogisticLossLayer); INSTANTIATE_CLASS(InfogainLossLayer); INSTANTIATE_CLASS(EuclideanLossLayer); -INSTANTIATE_CLASS(AccuracyLayer); INSTANTIATE_CLASS(HingeLossLayer); +INSTANTIATE_CLASS(AccuracyLayer); } // 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 767601c..a638684 100644 --- a/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp +++ b/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp @@ -13,16 +13,10 @@ using std::max; namespace caffe { template -void SigmoidCrossEntropyLossLayer::SetUp( +void SigmoidCrossEntropyLossLayer::FurtherSetUp( const vector*>& bottom, vector*>* top) { - CHECK_EQ(bottom.size(), 2) << - "SigmoidCrossEntropyLoss Layer takes two blobs as input."; - CHECK_EQ(top->size(), 0) << - "SigmoidCrossEntropyLoss Layer takes no blob as output."; CHECK_EQ(bottom[0]->count(), bottom[1]->count()) << "SigmoidCrossEntropyLoss Layer inputs must have same count."; - CHECK_EQ(bottom[0]->num(), bottom[1]->num()) << - "SigmoidCrossEntropyLoss Layer inputs must have same num."; sigmoid_bottom_vec_.clear(); sigmoid_bottom_vec_.push_back(bottom[0]); sigmoid_top_vec_.clear();