From: Sergio Date: Sat, 21 Jun 2014 02:55:59 +0000 (-0700) Subject: Now AccuracyLayer only computes accuracy, one should use LossLayers to compute loss X-Git-Tag: submit/tizen/20180823.020014~653^2~100^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8d4dacf6d9b5151937927eea34f96ebc6912eb31;p=platform%2Fupstream%2Fcaffeonacl.git Now AccuracyLayer only computes accuracy, one should use LossLayers to compute loss Changed all val.prototxt in examples to add a LossLayer to compute loss in Test --- diff --git a/examples/cifar10/cifar10_full_test.prototxt b/examples/cifar10/cifar10_full_test.prototxt index 0e1957a..1f77b4f 100644 --- a/examples/cifar10/cifar10_full_test.prototxt +++ b/examples/cifar10/cifar10_full_test.prototxt @@ -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" +} diff --git a/examples/cifar10/cifar10_quick_test.prototxt b/examples/cifar10/cifar10_quick_test.prototxt index a154b9a..aa82c32 100644 --- a/examples/cifar10/cifar10_quick_test.prototxt +++ b/examples/cifar10/cifar10_quick_test.prototxt @@ -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" +} diff --git a/examples/feature_extraction/imagenet_val.prototxt b/examples/feature_extraction/imagenet_val.prototxt index 14bfe77..b0451a1 100644 --- a/examples/feature_extraction/imagenet_val.prototxt +++ b/examples/feature_extraction/imagenet_val.prototxt @@ -227,3 +227,10 @@ layers { bottom: "label" top: "accuracy" } +layers { + name: "loss" + type: SOFTMAX_LOSS + bottom: "fc8" + bottom: "label" + top: "loss" +} diff --git a/examples/imagenet/alexnet_val.prototxt b/examples/imagenet/alexnet_val.prototxt index 3fd6296..1d8d86b 100644 --- a/examples/imagenet/alexnet_val.prototxt +++ b/examples/imagenet/alexnet_val.prototxt @@ -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" } diff --git a/examples/imagenet/imagenet_val.prototxt b/examples/imagenet/imagenet_val.prototxt index dd26f40..8be5150 100644 --- a/examples/imagenet/imagenet_val.prototxt +++ b/examples/imagenet/imagenet_val.prototxt @@ -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 diff --git a/examples/mnist/lenet_consolidated_solver.prototxt b/examples/mnist/lenet_consolidated_solver.prototxt index 07cbc21..ef851e0 100644 --- a/examples/mnist/lenet_consolidated_solver.prototxt +++ b/examples/mnist/lenet_consolidated_solver.prototxt @@ -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: diff --git a/examples/mnist/lenet_test.prototxt b/examples/mnist/lenet_test.prototxt index 3b59b75..2497f02 100644 --- a/examples/mnist/lenet_test.prototxt +++ b/examples/mnist/lenet_test.prototxt @@ -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" +} diff --git a/examples/mnist/mnist_autoencoder_test.prototxt b/examples/mnist/mnist_autoencoder_test.prototxt index 5090e82..b52364c 100644 --- a/examples/mnist/mnist_autoencoder_test.prototxt +++ b/examples/mnist/mnist_autoencoder_test.prototxt @@ -142,4 +142,5 @@ layers { bottom: "flatdata" name: "loss" type: EUCLIDEAN_LOSS + top: "loss" } diff --git a/examples/pascal-finetuning/pascal_finetune_val.prototxt b/examples/pascal-finetuning/pascal_finetune_val.prototxt index ff898fe..91ded58 100644 --- a/examples/pascal-finetuning/pascal_finetune_val.prototxt +++ b/examples/pascal-finetuning/pascal_finetune_val.prototxt @@ -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" +} + diff --git a/include/caffe/loss_layers.hpp b/include/caffe/loss_layers.hpp index dd06ca0..2f98a12 100644 --- a/include/caffe/loss_layers.hpp +++ b/include/caffe/loss_layers.hpp @@ -166,7 +166,7 @@ class MultinomialLogisticLossLayer : public LossLayer { /* 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 class AccuracyLayer : public Layer { @@ -180,6 +180,9 @@ class AccuracyLayer : public Layer { 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*>& bottom, vector*>* top); diff --git a/src/caffe/layers/accuracy_layer.cpp b/src/caffe/layers/accuracy_layer.cpp index fbc943e..899750f 100644 --- a/src/caffe/layers/accuracy_layer.cpp +++ b/src/caffe/layers/accuracy_layer.cpp @@ -23,14 +23,13 @@ void AccuracyLayer::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 Dtype AccuracyLayer::Forward_cpu(const vector*>& bottom, vector*>* 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::Forward_cpu(const vector*>& bottom, if (max_id == static_cast(bottom_label[i])) { ++accuracy; } - Dtype prob = max(bottom_data[i * dim + static_cast(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); }