add SILENCE layer -- takes one or more inputs and produces no output
authorJeff Donahue <jeff.donahue@gmail.com>
Fri, 4 Jul 2014 04:53:31 +0000 (21:53 -0700)
committerJonathan L Long <jonlong@cs.berkeley.edu>
Thu, 4 Sep 2014 06:11:30 +0000 (23:11 -0700)
This is useful for suppressing undesired outputs.

include/caffe/common_layers.hpp
src/caffe/layer_factory.cpp
src/caffe/layers/silence_layer.cpp [new file with mode: 0644]
src/caffe/layers/silence_layer.cu [new file with mode: 0644]
src/caffe/proto/caffe.proto

index e32dac7..3753592 100644 (file)
@@ -310,6 +310,37 @@ class MVNLayer : public Layer<Dtype> {
 };
 
 /**
+ * @brief Ignores bottom blobs while producing no top blobs. (This is useful
+ *        to suppress outputs during testing.)
+ */
+template <typename Dtype>
+class SilenceLayer : public Layer<Dtype> {
+ public:
+  explicit SilenceLayer(const LayerParameter& param)
+      : Layer<Dtype>(param) {}
+  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
+      vector<Blob<Dtype>*>* top) {}
+
+  virtual inline LayerParameter_LayerType type() const {
+    return LayerParameter_LayerType_SILENCE;
+  }
+  virtual inline int MinBottomBlobs() const { return 1; }
+  virtual inline int ExactNumTopBlobs() const { return 0; }
+
+ protected:
+  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+      vector<Blob<Dtype>*>* 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<Blob<Dtype>*>& bottom,
+      vector<Blob<Dtype>*>* top);
+  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
+      const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom);
+  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
+      const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom);
+};
+
+/**
  * @brief Computes the softmax function.
  *
  * TODO(dox): thorough documentation for Forward, Backward, and proto params.
index 1b34702..4bb7cd7 100644 (file)
@@ -66,6 +66,8 @@ Layer<Dtype>* GetLayer(const LayerParameter& param) {
     return new PowerLayer<Dtype>(param);
   case LayerParameter_LayerType_RELU:
     return new ReLULayer<Dtype>(param);
+  case LayerParameter_LayerType_SILENCE:
+    return new SilenceLayer<Dtype>(param);
   case LayerParameter_LayerType_SIGMOID:
     return new SigmoidLayer<Dtype>(param);
   case LayerParameter_LayerType_SIGMOID_CROSS_ENTROPY_LOSS:
diff --git a/src/caffe/layers/silence_layer.cpp b/src/caffe/layers/silence_layer.cpp
new file mode 100644 (file)
index 0000000..75dbbf3
--- /dev/null
@@ -0,0 +1,26 @@
+#include <vector>
+
+#include "caffe/common_layers.hpp"
+#include "caffe/layer.hpp"
+#include "caffe/util/math_functions.hpp"
+
+namespace caffe {
+
+template <typename Dtype>
+void SilenceLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+      const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom) {
+  for (int i = 0; i < bottom->size(); ++i) {
+    if (propagate_down[i]) {
+      caffe_set((*bottom)[i]->count(), Dtype(0),
+                (*bottom)[i]->mutable_cpu_data());
+    }
+  }
+}
+
+#ifdef CPU_ONLY
+STUB_GPU(SilenceLayer);
+#endif
+
+INSTANTIATE_CLASS(SilenceLayer);
+
+}  // namespace caffe
diff --git a/src/caffe/layers/silence_layer.cu b/src/caffe/layers/silence_layer.cu
new file mode 100644 (file)
index 0000000..735abe6
--- /dev/null
@@ -0,0 +1,28 @@
+#include <vector>
+
+#include "caffe/common_layers.hpp"
+#include "caffe/layer.hpp"
+#include "caffe/util/math_functions.hpp"
+
+namespace caffe {
+
+template <typename Dtype>
+void SilenceLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+      vector<Blob<Dtype>*>* top) {
+  // Do nothing.
+}
+
+template <typename Dtype>
+void SilenceLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+      const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom) {
+  for (int i = 0; i < bottom->size(); ++i) {
+    if (propagate_down[i]) {
+      caffe_gpu_set((*bottom)[i]->count(), Dtype(0),
+                    (*bottom)[i]->mutable_gpu_data());
+    }
+  }
+}
+
+INSTANTIATE_CLASS(SilenceLayer);
+
+}  // namespace caffe
index 74c2bab..dd650be 100644 (file)
@@ -220,7 +220,7 @@ message LayerParameter {
   // line above the enum. Update the next available ID when you add a new
   // LayerType.
   //
-  // LayerType next available ID: 36 (last added: ABSVAL)
+  // LayerType next available ID: 37 (last added: SILENCE)
   enum LayerType {
     // "NONE" layer type is 0th enum element so that we don't cause confusion
     // by defaulting to an existent LayerType (instead, should usually error if
@@ -254,6 +254,7 @@ message LayerParameter {
     RELU = 18;
     SIGMOID = 19;
     SIGMOID_CROSS_ENTROPY_LOSS = 27;
+    SILENCE = 36;
     SOFTMAX = 20;
     SOFTMAX_LOSS = 21;
     SPLIT = 22;