如何为Caffe中的二进制分类器获取两个输出值(两个类中的每一个)? [英] How can I get two output values (for each of the two classes) for a binary classifier in Caffe?

查看:1064
本文介绍了如何为Caffe中的二进制分类器获取两个输出值(两个类中的每一个)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验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:批处理41,准确度= 1 
I0310 18:30:21.889739 5952 caffe.cpp:264]批处理41,损失= 0.578524
pre>

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


 批处理41,类0输出:0.755 
批次41,类别1输出:0.201


我修改测试配置文件以使其发生?

解决方案

您要查看Softmax概率输出(不仅仅是损失)。

为此,您可以尝试使用SoftmaxWithLoss两个top(我不是100%确定此选项是完全功能/支持):

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


b $ b




或者,如果前一个解决方案不起作用,请显式添加Softmax p>

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


I'm experimenting with LeNet network as a binary classifier (yes, no). The first and several last layers in the configuration file for testing is the following:

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

For testing I've set the batch_size=1, thus I ran testing with the following command:

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

My intent is to be able to analyze result for each test image separately. Currently I get the following info for each iteration:

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

However since I have two outputs in my network, on testing I want to see two separate values for each of the outputs: one for class "0" ("no") and one for class "1" ("yes"). It should be something like that:

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

How should I modify the testing configuration file to make it happen?

解决方案

You want to see the "Softmax" probability output (not just the loss).
For this end you might try to use "SoftmaxWithLoss" with two "top"s (I'm not 100% sure this option is fully functional/supported):

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


Alternatively, if the former solution does not work, explicitly add a "Softmax" layer:

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

这篇关于如何为Caffe中的二进制分类器获取两个输出值(两个类中的每一个)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆