Caffe : train network accuracy = 1 constant ! Accu

2019-06-07 15:57发布

问题:

Right now, I am train network with with 2 class data... but accuracy is constant 1 after first iteration !

Input data is grayscale images. both class images are randomly selected when HDF5Data creation.

Why is that happened ? What's wrong or where is mistake !

network.prototxt :

name: "brainMRI"
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  include: {
    phase: TRAIN
  }
  hdf5_data_param {
    source: "/home/shivangpatel/caffe/brainMRI1/train_file_location.txt"
    batch_size: 10
  }
}
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  include: {
    phase: TEST
  }
  hdf5_data_param {
    source: "/home/shivangpatel/caffe/brainMRI1/test_file_location.txt"
    batch_size: 10
  }
}

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"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
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: "softmax"
  type: "Softmax"
  bottom: "ip2"
  top: "smip2"
}

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "smip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}

Output :

I0217 17:41:07.912580  2913 net.cpp:270] This network produces output loss
I0217 17:41:07.912607  2913 net.cpp:283] Network initialization done.
I0217 17:41:07.912739  2913 solver.cpp:60] Solver scaffolding done.
I0217 17:41:07.912789  2913 caffe.cpp:212] Starting Optimization
I0217 17:41:07.912813  2913 solver.cpp:288] Solving brainMRI
I0217 17:41:07.912832  2913 solver.cpp:289] Learning Rate Policy: inv
I0217 17:41:07.920737  2913 solver.cpp:341] Iteration 0, Testing net (#0)
I0217 17:41:08.235076  2913 solver.cpp:409]     Test net output #0: accuracy = 0.98
I0217 17:41:08.235194  2913 solver.cpp:409]     Test net output #1: loss = 0.0560832 (* 1 = 0.0560832 loss)
I0217 17:41:35.831647  2913 solver.cpp:341] Iteration 100, Testing net (#0)
I0217 17:41:36.140849  2913 solver.cpp:409]     Test net output #0: accuracy = 1
I0217 17:41:36.140949  2913 solver.cpp:409]     Test net output #1: loss = 0.00757247 (* 1 = 0.00757247 loss)
I0217 17:42:05.465395  2913 solver.cpp:341] Iteration 200, Testing net (#0)
I0217 17:42:05.775877  2913 solver.cpp:409]     Test net output #0: accuracy = 1
I0217 17:42:05.776000  2913 solver.cpp:409]     Test net output #1: loss = 0.0144996 (* 1 = 0.0144996 loss)
.............
.............

回答1:

Summarizing some information from the comments:
- You run test at intervals of test_interval:100 iterations.
- Each test interval goes over test_iter:5 * batch_size:10 = 50 samples.
- Your train and test sets seems to be very nit: all the negative samples (label=0) are grouped together before all the positive samples.


Consider your SGD iterative solver, you feed it batches of batch_size:10 during training. Your training set has 14,746 negative samples (that is 1474 batches) before any positive sample. So, for the first 1474 iterations your solver only "sees" negative examples and no positive ones.
What do you expect this solver will learn?

The problem

Your solver only sees negative examples, thus learns that no matter what the input is it should output "0". Your test set is also ordered in the same fashion, so testing only 50 samples at each test_interval, you only test on the negative examples in the test set resulting with a perfect accuracy of 1.
But as you noted, your net actually learned nothing.

Solution

I suppose you already guess what the solution should be by now. You need to shuffle your training set, and test your net on your entire test set.