如何为 Caffe 中的二元分类器获取两个输出值(对于两个 类 中的每一个)?

How can I get two output values (for each of the two classes) for a binary classifier in Caffe?

我正在试验 LeNet 网络作为二进制 classifier(是,不是)。 用于测试的配置文件中的第一层和最后几层如下:

    layer {
      name: "data"
      type: "ImageData"
      top: "data"
      top: "label"
      include {
        phase: TEST
      }
      transform_param {
        scale: 0.00390625
      }
      image_data_param {
        source: "examples/my_example/test_images_labels.txt"
        batch_size: 1
        new_height: 128
        new_width: 128
      }
    }
...
    layer {
      name: "ip2"
      type: "InnerProduct"
      bottom: "ip1"
      top: "ip2"
      param {
        lr_mult: 1
      }
      param {
        lr_mult: 2
      }
      inner_product_param {
        num_output: 2
        weight_filler {
          type: "xavier"
        }
        bias_filler {
          type: "constant"
        }
      }
    }
    layer {
      name: "accuracy"
      type: "Accuracy"
      bottom: "ip2"
      bottom: "label"
      top: "accuracy"
    }
    layer {
      name: "loss"
      type: "SoftmaxWithLoss"
      bottom: "ip2"
      bottom: "label"
      top: "loss"
    }

为了测试,我设置了 batch_size=1,因此我 运行 使用以下命令进行测试:

./build/tools/caffe test -model examples/my_example/lenet_test.prototxt -weights=examples/my_example/lenet_iter_528.caffemodel -iterations 200

我的目的是能够分别分析每个测试图像的结果。 目前我得到每次迭代的以下信息:

I0310 18:30:21.889688  5952 caffe.cpp:264] Batch 41, accuracy = 1
I0310 18:30:21.889739  5952 caffe.cpp:264] Batch 41, loss = 0.578524

然而,由于我的网络中有两个输出,在测试时我想为每个输出看到两个单独的值:一个用于 class "0" ("no") 和一个用于class“1”("yes")。应该是这样的:

Batch 41, class 0 output: 0.755
Batch 41, class 1 output: 0.201

我应该如何修改测试配置文件才能实现?

您想查看 "Softmax" 概率输出(而不仅仅是损失)。
为此,您可以尝试将 "SoftmaxWithLoss" 与两个 "top" 一起使用(我不是 100% 确定此选项完全 functional/supported):

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
  top: "prob" # add class probability output
}

或者,如果前一个解决方案不起作用,显式添加一个 "Softmax" 层:

layer {
  name: "prob"
  type: "Softmax"
  bottom: "ip2"
  top: "prob"
}