1

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)
.............
.............
Shai
  • 111,146
  • 38
  • 238
  • 371
shivang patel
  • 297
  • 1
  • 5
  • 15
  • what is your test set size? – Shai Feb 17 '16 at 12:28
  • **h5ls test_AD.hdf5** `data Dataset {3686, 1, 45, 64} label Dataset {3686, 1, 1, 1}` **h5ls test_NC.hdf5** `data Dataset {7334, 1, 45, 64} label Dataset {7334, 1, 1, 1}` **h5ls train_AD.hdf5** `data Dataset {14746, 1, 45, 64} label Dataset {14746, 1, 1, 1}` **h5ls train_NC.hdf5** `data Dataset {29338, 1, 45, 64} label Dataset {29338, 1, 1, 1}` – shivang patel Feb 17 '16 at 12:32
  • your log only prints test iterations, are you running any training? how many test iterations do you run at each test interval? – Shai Feb 17 '16 at 12:42
  • `test_iter: 5 test_interval: 100 base_lr: 0.001 momentum: 0.9 weight_decay: 0.0005 lr_policy: "inv" gamma: 0.0001 power: 0.75 # The maximum number of iterations max_iter: 10000 # snapshot intermediate results snapshot: 1000 snapshot_prefix: "/home/shivangpatel/caffe/brainMRI1/" # solver mode: CPU or GPU solver_mode: CPU` – shivang patel Feb 17 '16 at 12:44
  • with `test_iter:5` and `batch_size:10` means you only test on 5*10=50 samples at each test interval. Is it possible that the examples in the test set are not randomly ordered (w.r.t their lebels)? – Shai Feb 17 '16 at 12:47
  • 1
    see [this answer](http://stackoverflow.com/a/33786620/1714410) for more information about `test_interval` and `batch_size`. – Shai Feb 17 '16 at 12:48
  • Label for `class 1` data is `0` & `class 2` data is `1` – shivang patel Feb 17 '16 at 12:50
  • so your test set is 3686 samples of label 0 followed by 7334 samples of label 1? and your train set is 14746 samples of label 0 followed by 29338 samples of label 1? – Shai Feb 17 '16 at 12:57
  • yes... 3686 samples - label 0, 7334 samples - label 1 ---> Testing 14746 samples - label 0, 29338 samples - label 1 ----> Training – shivang patel Feb 17 '16 at 13:06

1 Answers1

7

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.

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    Ok.... I will do it. I have other 2 more class like this... i will mixup training samples and train again. Results i will post... – shivang patel Feb 17 '16 at 13:27
  • No change ! same results... I also make changes in solver and network prototxt as per your suggestion. Code for HDF5 creation... and other files :-->> https://github.com/shivangpatel/caffe.git – shivang patel Feb 19 '16 at 05:57
  • I think, problem is greayScale images... Of MRI brain... Any idea, how to make it useful for input in caffe – shivang patel Feb 19 '16 at 12:11
  • 1
    I found my mistake... Your suggestion is very helpful... Thank u very much. – shivang patel Feb 23 '16 at 04:21
  • @DCurro : Problem is our INPUT DATATA... Make sure, All data are shuffled properly.! Check your input data.... and mix-up properly. – shivang patel Apr 11 '16 at 04:06