【问题标题】:How can I get two output values (for each of the two classes) for a binary classifier in Caffe?如何为 Caffe 中的二进制分类器获取两个输出值(对于两个类中的每一个)?
【发布时间】:2023-04-01 20:50:02
【问题描述】:

我正在尝试将 LeNet 网络用作二元分类器(是,否)。 用于测试的配置文件的第一层和最后几层如下:

    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

但是,由于我的网络中有两个输出,因此在测试时,我希望看到每个输出的两个单独值:一个用于“0”类(“否”),一个用于“1”类(“是”) )。应该是这样的:

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

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

【问题讨论】:

  • 请不要编辑问题来提出新问题。发布一个新问题。如果您认为它们相关,您可以添加上下文链接。
  • 另外,如果“上述问题已经解决”,为什么不点击旁边的“v”图标“接受”答案?
  • 为什么你声称“有时它是由其他方式制造的(首先是 1 类,然后是 0 类)”?这似乎是一个错误。你确定吗?你能重现它吗?如果是这样,您可能需要在 BVLC/caffe github 页面中报告此错误。

标签: machine-learning computer-vision neural-network deep-learning caffe


【解决方案1】:

您想查看"Softmax" 概率输出(不仅仅是损失)。
为此,您可能会尝试将"SoftmaxWithLoss" 与两个"top"s 一起使用(我不能100% 确定此选项是否功能齐全/受支持):

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"
}

【讨论】:

  • 第一个解决方案不起作用,尽管第二个解决方案起作用。非常感谢!
  • @evaluate 您能否更具体地说明为什么第一个解决方案不起作用?
  • Shai,出现错误,显示无法指定两个最高值。
  • @evaluate 好的。我猜这个功能还没有被合并
猜你喜欢
  • 1970-01-01
  • 2015-09-04
  • 2016-07-18
  • 1970-01-01
  • 2012-04-21
  • 2018-08-19
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多