Accuracy always 1 Caffe Regression

2019-01-29 09:55发布

问题:

My dataset contains 400 images 32x32x3 and the labels contain float number (-1,1). Example:

faceCroppedImages/img1.jpg 0
faceCroppedImages/img2.jpg 0.0128
faceCroppedImages/img3.jpg 0.0128
faceCroppedImages/img4.jpg 0.0128
faceCroppedImages/img22.jpg 0.0128
faceCroppedImages/img23.jpg 0.0085
faceCroppedImages/img24.jpg 0.0077
faceCroppedImages/img25.jpg 0.0077
faceCroppedImages/img293.jpg -0.023
faceCroppedImages/img294.jpg -0.023
faceCroppedImages/img295.jpg -0.0204
faceCroppedImages/img296.jpg -0.0179
faceCroppedImages/img297.jpg -0.017
faceCroppedImages/img298.jpg -0.0128

My 'solver.prototxt' is:

net: "train_test_hdf5.prototxt"
test_iter: 100
test_interval: 500
base_lr: 0.003
momentum: 0.9
weight_decay: 0.0005
lr_policy: "inv"
gamma: 0.0001
power: 0.75
display: 100
max_iter: 10000
snapshot: 5000
snapshot_prefix: "lenet_hdf5"
solver_mode: CPU

and 'train_test_hdf5.prototxt' is:

name: "MSE regression"
layer{
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "train_hdf5file.txt"
    batch_size: 64
    shuffle: true
  }
  include: { phase: TRAIN }
}

layer{
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "test_hdf5file.txt"
    batch_size: 128
  }
  include: { phase: TEST }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param { lr_mult: 1 }
  param { lr_mult: 2 }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "conv1"
  top: "conv1"
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "dropout1"
  type: "Dropout"
  bottom: "pool1"
  top: "pool1"
  dropout_param {
    dropout_ratio: 0.1
  }
}

layer{
  name: "fc1"
  type: "InnerProduct"
  bottom: "pool1"
  top: "fc1"
  param { lr_mult: 1 decay_mult: 1 }
  param { lr_mult: 2 decay_mult: 0 }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "dropout2"
  type: "Dropout"
  bottom: "fc1"
  top: "fc1"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer{
  name: "fc2"
  type: "InnerProduct"
  bottom: "fc1"
  top: "fc2"
  param { lr_mult: 1 decay_mult: 1 }
  param { lr_mult: 2 decay_mult: 0 }
  inner_product_param {
    num_output: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
 }
}
layer {
  name: "accuracy1"
  type: "Accuracy"
  bottom: "fc2"
  bottom: "label"
  top: "accuracy1"
  include {
    phase: TEST
  }
}
layer{
  name: "loss"
  type: "EuclideanLoss"
  bottom: "fc2"
  bottom: "label"
  top: "loss"
}

However when I'm testing the data the accuracy is always 1:

I tried using integer labels my multiplying my current labels by 1000, but I'm getting the nan error:

Can you please tell me where I'm doing wrong? I'm a beginner in caffe and neural networks. Any suggestions will be valuable. TIA.

回答1:

No point using "Accuracy" layer for regression task: this layer measures the accuracy of classification output.
For instance, if you are trying to predict one of L labels, num_output of fc2 layer will be L - that is, predicting a probability per class. Then "Accuracy" layer checks that the probability of the l-th entry, corresponding to the expected output l is maximal.
How can you compute such accuracy when fc2 output is only 1 dimensional?

In your case, you can only check the Euclidean loss and see it decreasing for both test and train.