Now AccuracyLayer only computes accuracy, one should use LossLayers to compute loss
authorSergio <sguada@gmail.com>
Sat, 21 Jun 2014 02:55:59 +0000 (19:55 -0700)
committerSergio <sguada@gmail.com>
Sat, 21 Jun 2014 02:56:18 +0000 (19:56 -0700)
Changed all val.prototxt in examples to add a LossLayer to compute loss in Test

examples/cifar10/cifar10_full_test.prototxt
examples/cifar10/cifar10_quick_test.prototxt
examples/feature_extraction/imagenet_val.prototxt
examples/imagenet/alexnet_val.prototxt
examples/imagenet/imagenet_val.prototxt
examples/mnist/lenet_consolidated_solver.prototxt
examples/mnist/lenet_test.prototxt
examples/mnist/mnist_autoencoder_test.prototxt
examples/pascal-finetuning/pascal_finetune_val.prototxt
include/caffe/loss_layers.hpp
src/caffe/layers/accuracy_layer.cpp

index 0e1957a..1f77b4f 100644 (file)
@@ -167,15 +167,16 @@ layers {
   }
 }
 layers {
-  name: "prob"
-  type: SOFTMAX
-  bottom: "ip1"
-  top: "prob"
-}
-layers {
   name: "accuracy"
   type: ACCURACY
-  bottom: "prob"
+  bottom: "ip1"
   bottom: "label"
   top: "accuracy"
 }
+layers {
+  name: "loss"
+  type: SOFTMAX_LOSS
+  bottom: "ip1"
+  bottom: "label"
+  top: "loss"
+}
index a154b9a..aa82c32 100644 (file)
@@ -161,15 +161,16 @@ layers {
   }
 }
 layers {
-  name: "prob"
-  type: SOFTMAX
-  bottom: "ip2"
-  top: "prob"
-}
-layers {
   name: "accuracy"
   type: ACCURACY
-  bottom: "prob"
+  bottom: "ip2"
   bottom: "label"
   top: "accuracy"
 }
+layers {
+  name: "loss"
+  type: SOFTMAX_LOSS
+  bottom: "ip2"
+  bottom: "label"
+  top: "loss"
+}
index 14bfe77..b0451a1 100644 (file)
@@ -227,3 +227,10 @@ layers {
   bottom: "label"
   top: "accuracy"
 }
+layers {
+  name: "loss"
+  type: SOFTMAX_LOSS
+  bottom: "fc8"
+  bottom: "label"
+  top: "loss"
+}
index 3fd6296..1d8d86b 100644 (file)
@@ -213,15 +213,16 @@ layers {
   top: "fc8"
 }
 layers {
-  name: "prob"
-  type: SOFTMAX
+  name: "accuracy"
+  type: ACCURACY
   bottom: "fc8"
-  top: "prob"
+  bottom: "label"
+  top: "accuracy"
 }
 layers {
-  top: "accuracy"
-  name: "accuracy"
-  type: ACCURACY
-  bottom: "prob"
+  name: "loss"
+  type: SOFTMAX_LOSS
+  bottom: "fc8"
   bottom: "label"
+  top: "loss"
 }
index dd26f40..8be5150 100644 (file)
@@ -213,15 +213,16 @@ layers {
   }
 }
 layers {
-  name: "prob"
-  type: SOFTMAX
-  bottom: "fc8"
-  top: "prob"
-}
-layers {
   name: "accuracy"
   type: ACCURACY
-  bottom: "prob"
+  bottom: "fc8"
   bottom: "label"
   top: "accuracy"
 }
+layers {
+  name: "loss"
+  type: SOFTMAX_LOSS
+  bottom: "fc8"
+  bottom: "label"
+  top: "loss"
+}
\ No newline at end of file
index 07cbc21..ef851e0 100644 (file)
@@ -263,18 +263,19 @@ test_net_param {
     }
   }
   layers {
-    name: "prob"
-    type: SOFTMAX
-    bottom: "ip2"
-    top: "prob"
-  }
-  layers {
     name: "accuracy"
     type: ACCURACY
-    bottom: "prob"
+    bottom: "ip2"
     bottom: "label"
     top: "accuracy"
   }
+  layers {
+    name: "loss"
+    type: SOFTMAX_LOSS
+    bottom: "ip2"
+    bottom: "label"
+    top: "loss"
+  }
 }
 
 # The train set has 60K images, so we run 600 test iters (600 * 100 = 60K).
@@ -386,18 +387,19 @@ test_net_param {
     }
   }
   layers {
-    name: "prob"
-    type: SOFTMAX
-    bottom: "ip2"
-    top: "prob"
-  }
-  layers {
     name: "accuracy"
     type: ACCURACY
-    bottom: "prob"
+    bottom: "ip2"
     bottom: "label"
     top: "accuracy"
   }
+  layers {
+    name: "loss"
+    type: SOFTMAX_LOSS
+    bottom: "ip2"
+    bottom: "label"
+    top: "loss"
+  }
 }
 
 # Expected results for first and last 500 iterations:
index 3b59b75..2497f02 100644 (file)
@@ -103,15 +103,16 @@ layers {
   }
 }
 layers {
-  name: "prob"
-  type: SOFTMAX
-  bottom: "ip2"
-  top: "prob"
-}
-layers {
   name: "accuracy"
   type: ACCURACY
-  bottom: "prob"
+  bottom: "ip2"
   bottom: "label"
   top: "accuracy"
 }
+layers {
+  name: "loss"
+  type: SOFTMAX_LOSS
+  bottom: "ip2"
+  bottom: "label"
+  top: "loss"
+}
index 5090e82..b52364c 100644 (file)
@@ -142,4 +142,5 @@ layers {
   bottom: "flatdata"
   name: "loss"
   type: EUCLIDEAN_LOSS
+  top: "loss"
 }
index ff898fe..91ded58 100644 (file)
@@ -314,15 +314,17 @@ layers {
   }
 }
 layers {
-  name: "prob"
-  type: SOFTMAX
-  bottom: "fc8_pascal"
-  top: "prob"
-}
-layers {
   name: "accuracy"
   type: ACCURACY
-  bottom: "prob"
+  bottom: "fc8_pascal"
   bottom: "label"
   top: "accuracy"
 }
+layers {
+  name: "prob"
+  type: SOFTMAX_LOSS
+  bottom: "fc8_pascal"
+  bottom: "label"
+  top: "loss"
+}
+
index dd06ca0..2f98a12 100644 (file)
@@ -166,7 +166,7 @@ class MultinomialLogisticLossLayer : public LossLayer<Dtype> {
 
 /* AccuracyLayer
   Note: not an actual loss layer! Does not implement backwards step.
-  Computes the accuracy of a with respect to b.
+  Computes the accuracy of argmax(a) with respect to b.
 */
 template <typename Dtype>
 class AccuracyLayer : public Layer<Dtype> {
@@ -180,6 +180,9 @@ class AccuracyLayer : public Layer<Dtype> {
     return LayerParameter_LayerType_ACCURACY;
   }
 
+  virtual inline int ExactNumBottomBlobs() const { return 2; }
+  virtual inline int ExactNumTopBlobs() const { return 1; }
+
  protected:
   virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
       vector<Blob<Dtype>*>* top);
index fbc943e..899750f 100644 (file)
@@ -23,14 +23,13 @@ void AccuracyLayer<Dtype>::SetUp(
   CHECK_EQ(bottom[1]->channels(), 1);
   CHECK_EQ(bottom[1]->height(), 1);
   CHECK_EQ(bottom[1]->width(), 1);
-  (*top)[0]->Reshape(1, 2, 1, 1);
+  (*top)[0]->Reshape(1, 1, 1, 1);
 }
 
 template <typename Dtype>
 Dtype AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
     vector<Blob<Dtype>*>* top) {
   Dtype accuracy = 0;
-  Dtype logprob = 0;
   const Dtype* bottom_data = bottom[0]->cpu_data();
   const Dtype* bottom_label = bottom[1]->cpu_data();
   int num = bottom[0]->num();
@@ -48,13 +47,10 @@ Dtype AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
     if (max_id == static_cast<int>(bottom_label[i])) {
       ++accuracy;
     }
-    Dtype prob = max(bottom_data[i * dim + static_cast<int>(bottom_label[i])],
-                     Dtype(kLOG_THRESHOLD));
-    logprob -= log(prob);
   }
   // LOG(INFO) << "Accuracy: " << accuracy;
   (*top)[0]->mutable_cpu_data()[0] = accuracy / num;
-  (*top)[0]->mutable_cpu_data()[1] = logprob / num;
+
   // Accuracy layer should not be used as a loss function.
   return Dtype(0);
 }