How can I get two output values (for each of the t

2019-08-12 20:18发布

问题:

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?

回答1:

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