caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Any input produce the same output

Open mender05 opened this issue 12 years ago • 52 comments

I try to ues caffe to implement the DeepPose proposed in this paper: http://arxiv.org/abs/1312.4659 DeepPose has 3 stages. And each stage is almost the same as AlexNet (DeepPose changes the loss layer in AlexNet to euclidean loss). It is a regression problem in fact.

The train.prototxt is:

name: "CaffeNet"
layers {
  name: "image"
  type: DATA
  top: "image"
  data_param {
    source: "examples/lsp/lsp_train_images_lmdb"
    backend: LMDB
    batch_size: 30
    scale: 0.00390625
  }
}
layers {
  name: "label"
  type: DATA
  top: "label"
  data_param {
    source: "examples/lsp/lsp_train_labels_lmdb"
    backend: LMDB
    batch_size: 30
    scale: 0.00454545
  }
}
layers {
  name: "conv1"
  type: CONVOLUTION
  bottom: "image"
  top: "conv1"
...  THIS IS THE SAME AS ALEXNET ...
layers {
  name: "fc8"
  type: INNER_PRODUCT
  bottom: "fc7"
  top: "fc8"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 28
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layers {
  name: "loss"
  type: EUCLIDEAN_LOSS
  bottom: "fc8"
  bottom: "label"
  top: "loss"
}

The solve.prototxt is:

net: "models/lsp/deeppose_train.prototxt"
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 7500
display: 50
max_iter: 36500
momentum: 0.9
weight_decay: 0.0000005
snapshot: 2000
snapshot_prefix: "models/lsp/caffenet_train"
solver_mode: GPU

After trainning completed, I use python interface to do prediction on testset. The test.prototxt is:

name: "CaffeNet"
layers {
  name: "image"
  type: MEMORY_DATA
  top: "image"
    top: "useless"
  memory_data_param {
    batch_size: 30
    channels: 3
    height: 220
    width: 220
  }
}
layers {
  name: "conv1"
  type: CONVOLUTION
  bottom: "image"
... 
layers {
  name: "fc8"
  type: INNER_PRODUCT
  bottom: "fc7"
  top: "fc8"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 28
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

but the output is very strange. Dumpping the output of "fc8" layer, I find that all the images produce the same output:

array([[ 0.48381898,  0.02326088,  0.02317634,  0.02317682,  0.48248914,
         0.01622555,  0.0161516 ,  0.01615119,  0.48646507,  0.03201264,
         0.03185751,  0.03185739,  0.52191395,  0.03508802,  0.03494693,
         0.03494673,  0.52380753,  0.01708153,  0.01701014,  0.01700996,
         0.52726734,  0.02286946,  0.02277863,  0.0227785 ,  0.46513146,
         0.02239206,  0.02227863,  0.02227836],
       [ 0.48381898,  0.02326088,  0.02317634,  0.02317682,  0.48248914,
         0.01622555,  0.0161516 ,  0.01615119,  0.48646507,  0.03201264,
         0.03185751,  0.03185739,  0.52191395,  0.03508802,  0.03494693,
         0.03494673,  0.52380753,  0.01708153,  0.01701014,  0.01700996,
         0.52726734,  0.02286946,  0.02277863,  0.0227785 ,  0.46513146,
         0.02239206,  0.02227863,  0.02227836],
       [ 0.48381898,  0.02326088,  0.02317634,  0.02317682,  0.48248914,
         0.01622555,  0.0161516 ,  0.01615119,  0.48646507,  0.03201264,
         0.03185751,  0.03185739,  0.52191395,  0.03508802,  0.03494693,
         0.03494673,  0.52380753,  0.01708153,  0.01701014,  0.01700996,
         0.52726734,  0.02286946,  0.02277863,  0.0227785 ,  0.46513146,
         0.02239206,  0.02227863,  0.02227836],

In fact, no mater what the inputs are, the outputs are always the same with the values above. How the problem caused?

mender05 avatar Nov 04 '14 14:11 mender05

Maybe you should take the fc8 features like this: net.blobs['fc8'].data[4].copy()

jiangdong123 avatar Nov 11 '14 10:11 jiangdong123

@jiangdong123 Thank you for your advice! I'll try it. This is my previous code: I take the output of fc8 like this (just a BATCH_SIZE of test images):

net.set_input_arrays(\
    data4D.astype(np.float32), data4DL.astype(np.float32))
pred = net.forward()
for i in range(0,BATCH_SIZE):
  for c in range(0,28):
    pred_normal[i][c] = pred['fc8'][i][c][0][0]
print pred_normal

Is there any mistake?

mender05 avatar Nov 12 '14 13:11 mender05

The loss variation looks very strange: figure_1 What causes the loss changing periodically?

mender05 avatar Nov 14 '14 02:11 mender05

Probably your training data is not randomized.

On Thursday, November 13, 2014, mender05 [email protected] wrote:

The loss variation looks very strange: [image: figure_1] https://cloud.githubusercontent.com/assets/7811449/5040242/bab9f878-6be6-11e4-99dc-675424c37cbd.png What causes the loss changing periodically?

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1396#issuecomment-63000773.

Sergio

sguada avatar Nov 14 '14 05:11 sguada

@jiangdong123

  1. The outputs always the same for all test images
  2. I have tried bigger learning rate (0.1) but get the same results.
  3. I also tried to change the input layer type from DATA to IMAGE_DATA, but the loss still changes periadically.
  4. I'll check if the labels are read properly.

As @sguada said, the training data is not randomized. I'll randomize them before training.

mender05 avatar Nov 14 '14 10:11 mender05

@sguada @jiangdong123 The reason is that labels are interpreted inproperly! for example, the following 2 groups of 28-dim label:

label_1 = { 189, 116, 165, 259, 95, 144, 122, 151, 88, 125, 218, 160, 68, 32, 95, 110, 165, 266, 123, 32, 151, 182, 189, 284, 294, 218, 173, 157 }
label_2 = { 64, 71, 91, 115, 126, 105, 24, 51, 92, 144, 170, 197, 114, 132, 188, 138, 97, 103, 148, 201, 20, 29, 30, 39, 68, 99, 34, 22 }

are interpreted as:

label_1 = { 189, 0, 0, 0, 116, 0, 0, 0, 165, 0, 0, 0, 3, 1, 0, 0, 95, 0, 0, 0, 144, 0, 0, 0, 122, 0, 0, 0 }
label_2 = { 64, 0, 0, 0, 71, 0, 0, 0, 91, 0, 0, 0, 115, 0, 0, 0, 126, 0, 0, 0, 105, 0, 0, 0, 24, 0, 0, 0 }

It seems that CAFFE reads the input label by byte. As a result, the 259 is read as 3,1,0,0. In the little-endian machine, 259 is 3,1,0,0 in memory.

Previously, my labels are stored to lmdb in this way:

int datum_size = sizeof(int)*28;
data_file.read(str_buffer, datum_size);
...
datum.set_data(str_buffer, datum_size);
datum.SerializeToString(&value);
...
mdb_data.mv_data = reinterpret_cast<void*>(&value[0]);
mdb_put(mdb_txn, mdb_dbi, &mdb_key, &mdb_data, 0)

CAFFE uses c++ template: template <typename Dtype>. How can I specify the Dtype to be int?

mender05 avatar Nov 18 '14 07:11 mender05

I have corrected the labels, changed the input type to float and randomlized the training samples, but this problem still there. figure_1 a period == 2400 iterations. A iteration processes 2400*30 = 72000 images. There are 22000 training images, which is equivalent to 72000/22000 = 3.3 epochs

mender05 avatar Nov 24 '14 07:11 mender05

When you shuffle the training data did you made sure the labels align?

Can you increase the batch size? Also try to increase the dropout.

On Sunday, November 23, 2014, mender05 [email protected] wrote:

I have corrected the labels, changed the input type to float and randomlized the training samples, but this problem still there. [image: figure_1] https://cloud.githubusercontent.com/assets/7811449/5161721/8ee3061a-73eb-11e4-8c93-48e7c7bee80d.jpg a period == 2400 iterations. A iteration processes 2400*30 = 72000 images. There are 22000 training images, which is equivalent to 72000/22000 = 3.3 epochs

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1396#issuecomment-64158231.

Sergio

sguada avatar Nov 24 '14 15:11 sguada

Thank you @sguada.

  1. The labels and images are shuffled synchronously.
  2. The limited VRAM restricts the batch size. 30 is the maximum size for me.
  3. I'll try to increase the dropout.

mender05 avatar Nov 25 '14 02:11 mender05

@mender05 you could also try https://github.com/shelhamer/caffe/tree/accum-grad to allow having bigger batch size by doing several iterations before updating the gradients.

sguada avatar Nov 25 '14 07:11 sguada

@sguada I have tried this branch. But what parameters should be set to enable bigger batch size? After batch_size was increased from 30 to 35, it ran out of memory.

F1126 16:43:02.337970  7332 syncedmem.cpp:51] Check failed: error == cudaSuc    cess (2 vs. 0)  out of memory

mender05 avatar Nov 26 '14 08:11 mender05

In the solver.prototxt add

iter_size: 2

That will mean that it would do 2 iterations of batch_size: 30 before updating the weights. This means that effectively you would using a batch_size: 60.

You can change your batch_size and iter_size to define the desired batch_size.

sguada avatar Nov 26 '14 10:11 sguada

It is so strange. As the batch_size increasing from 30 to 60, the loss variation pattern changed, but still periodic.

figure_1

mender05 avatar Nov 28 '14 11:11 mender05

There must be something weird with your data, the loss decrease very quickly and then oscillates with periodicity. Could you shuffle your data again?

Sergio

2014-11-28 3:34 GMT-08:00 mender05 [email protected]:

It is so strange. As the batch_size increasing from 30 to 60, the loss variation pattern changed, but still periodic.

[image: figure_1] https://cloud.githubusercontent.com/assets/7811449/5227188/dac48942-7732-11e4-8386-0cbd491695be.png

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1396#issuecomment-64884867.

sguada avatar Nov 28 '14 11:11 sguada

I use the snapshot at the 2000th iteration to predict, the outputs are all the same.

array([[ 0.49006659,  0.48892561,  0.49674234,  0.52244973,  0.52458155,
         0.52957731,  0.46845111,  0.47450158,  0.49067837,  0.52837992,
         0.53714836,  0.54056102,  0.52498746,  0.50657398,  0.53844237,
         0.5057267 ,  0.42278934,  0.42133904,  0.50450838,  0.5381543 ,
         0.45289528,  0.42029274,  0.37055418,  0.36709356,  0.41887969,
         0.44862145,  0.32116845,  0.36128747],
       [ 0.49006659,  0.48892561,  0.49674234,  0.52244973,  0.52458155,
         0.52957731,  0.46845111,  0.47450158,  0.49067837,  0.52837992,
         0.53714836,  0.54056102,  0.52498746,  0.50657398,  0.53844237,
         0.5057267 ,  0.42278934,  0.42133904,  0.50450838,  0.5381543 ,
         0.45289528,  0.42029274,  0.37055418,  0.36709356,  0.41887969,
         0.44862145,  0.32116845,  0.36128747],
       [ 0.49006659,  0.48892561,  0.49674234,  0.52244973,  0.52458155,
         0.52957731,  0.46845111,  0.47450158,  0.49067837,  0.52837992,
         0.53714836,  0.54056102,  0.52498746,  0.50657398,  0.53844237,
         0.5057267 ,  0.42278934,  0.42133904,  0.50450838,  0.5381543 ,
         0.45289528,  0.42029274,  0.37055418,  0.36709356,  0.41887969,
         0.44862145,  0.32116845,  0.36128747],

Outputs of the final model at the 36500th iteration:

array([[ 0.482418  ,  0.48542902,  0.49439543,  0.52315784,  0.52507049,
         0.52752018,  0.47199821,  0.47462174,  0.49217641,  0.52927047,
         0.54133612,  0.54410964,  0.52102458,  0.50839245,  0.53855455,
         0.5059635 ,  0.41948465,  0.4194364 ,  0.50593352,  0.53848571,
         0.44772175,  0.41696107,  0.36593205,  0.36593369,  0.41697961,
         0.44766867,  0.31933263,  0.36117038],
       [ 0.482418  ,  0.48542902,  0.49439543,  0.52315784,  0.52507049,
         0.52752018,  0.47199821,  0.47462174,  0.49217641,  0.52927047,
         0.54133612,  0.54410964,  0.52102458,  0.50839245,  0.53855455,
         0.5059635 ,  0.41948465,  0.4194364 ,  0.50593352,  0.53848571,
         0.44772175,  0.41696107,  0.36593205,  0.36593369,  0.41697961,
         0.44766867,  0.31933263,  0.36117038],
       [ 0.482418  ,  0.48542902,  0.49439543,  0.52315784,  0.52507049,
         0.52752018,  0.47199821,  0.47462174,  0.49217641,  0.52927047,
         0.54133612,  0.54410964,  0.52102458,  0.50839245,  0.53855455,
         0.5059635 ,  0.41948465,  0.4194364 ,  0.50593352,  0.53848571,
         0.44772175,  0.41696107,  0.36593205,  0.36593369,  0.41697961,
         0.44766867,  0.31933263,  0.36117038],

mender05 avatar Nov 28 '14 13:11 mender05

hi, @mender05 do you mind to show some code on how you make prediction on test data and get the array in your last post?

StevenLOL avatar Dec 23 '14 01:12 StevenLOL

Hi, I have the same problem. I am using regression for video processing and therefore I used 9 consecutive frames as input of of the network. I changed convert_imageset.cpp to store data as 9 frames in each blob, reading data in train_val.prototxt as:

name: "CaffeNet"
layers {
  name: "data"
  type: DATA
  top: "data"
  top: "label"
  data_param {
    source: "examples/project/train_lmdb"
    backend: LMDB   
    batch_size: 256
  }
  transform_param {
    crop_size: 227
    mean_file: "examples/project//train_mean.binaryproto"
    mirror: true
  }
  include: { phase: TRAIN }
}
layers {
  name: "data"
  type: DATA
  top: "data"
  top: "label"
  data_param {
    source: "examples/project//val_lmdb"
    backend: LMDB   
    batch_size: 50
  }
  transform_param {
    crop_size: 227
    mean_file: "examples/project/train_mean.binaryproto"
    mirror: false
  }
  include: { phase: TEST }
}

and changed the accuracy layer to EUCLIDEAN_LOSS in train_val.prototxt

layers {
  name: "fc8"
  type: INNER_PRODUCT
  bottom: "fc7"
  top: "fc8"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 1
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layers {
  name: "loss"
  type: EUCLIDEAN_LOSS
  bottom: "fc8"
  bottom: "label"
  top: "loss"
}

for deploying I used:

input: "data"
input_dim: 10
input_dim: 9
input_dim: 227
input_dim: 227
layers {
  name: "conv1"
  type: CONVOLUTION
  bottom: "data"
  top: "conv1"
  convolution_param {
    num_output: 96
    kernel_size: 11
    stride: 4
  }
}

<rest the same>
layers {
  name: "fc8"
  type: INNER_PRODUCT
  bottom: "fc7"
  top: "fc8"
  inner_product_param {
    num_output: 1
  }
}

I have

base_lr: 0.001
batch_size: 256 for train
batch_size: 50 for val

The same is like image_net network. I have the same loss behavior as @mender05. It decreased dramatically at first and then fluctuated till end. I have not shuffled the data, and labels are integer 1 to 100. To test, I used Matlab interface, i.e. read 9 images, concatenate them together and use

scores = matcaffe_demo(imgFrames, 1);

As I am cropping the images, the result is a score vector with length of 10, all having the same value, e.g. 71.4674 regardless of input images. I also tried different snapshots of the network and the result changed a bit but still the same for all crops, all images.

@mender05, could you solve your problem? Do you still have the same output for all images?

@sguada, do I do every steps right for the regression? I am going to shuffle my data but I don't know if it is due to shuffling or something else!

mollahosseini avatar Jan 06 '15 18:01 mollahosseini

A possible explanation is that the model is not learning much, it probably got trapped in a local minimum which is similar to random weights. Try to change the way you initialize the weights, change gaussian to xavier for the convolutional layers.

sguada avatar Jan 06 '15 19:01 sguada

Thanks @sguada,
I changed the weights from gaussian to xavier. But it gives me nan loss even with learning rate 0.001. I've read that people decreased lr to overcome nan loss, however I am afraid if I decrease lr more than 0.001 my network doesn't learn at all. I will work on shuffling data and see if it changes anything. BTW, I have about 2700 inputs (each has 9 images). considering 10 crops for each input, network is only trained with about 27000 inputs. Do you think it can be the reason of trapping in local minima?

mollahosseini avatar Jan 06 '15 19:01 mollahosseini

Don't worry about decreasing the learning rate, it is relative to the magnitude of the loss, which in case of euclidean loss can be huge. And yes having only 9 images will cause problems of overfitting.

All questions about usage, installation, code, and applications should be searched for and asked on the caffe-users mailing list.

sguada avatar Jan 06 '15 19:01 sguada