caffe icon indicating copy to clipboard operation
caffe copied to clipboard

How to do regression?

Open xucong-zhang opened this issue 12 years ago • 57 comments

Hi, I am trying to modify the mnist example to be a regression network. I just changed the loss layer from "SOFTMAX_LOSS" to "EUCLIDEAN_LOSS", and "num_output" of ip2 layer to be 1 instead of 10. But I got the result like this:

I0617 15:26:45.970600 10216 solver.cpp:141] Iteration 0, Testing net (#0) I0617 15:26:47.555521 10216 solver.cpp:179] Test score #0: 1 I0617 15:26:47.555577 10216 solver.cpp:179] Test score #1: 0 I0617 15:26:51.046875 10216 solver.cpp:274] Iteration 100, lr = 0.00992565 I0617 15:26:51.047067 10216 solver.cpp:114] Iteration 100, loss = nan I0617 15:26:54.535904 10216 solver.cpp:274] Iteration 200, lr = 0.00985258 I0617 15:26:54.536092 10216 solver.cpp:114] Iteration 200, loss = nan I0617 15:26:58.024719 10216 solver.cpp:274] Iteration 300, lr = 0.00978075 I0617 15:26:58.024911 10216 solver.cpp:114] Iteration 300, loss = nan I0617 15:27:01.514154 10216 solver.cpp:274] Iteration 400, lr = 0.00971013 I0617 15:27:01.514345 10216 solver.cpp:114] Iteration 400, loss = nan I0617 15:27:05.003473 10216 solver.cpp:274] Iteration 500, lr = 0.00964069 I0617 15:27:05.003661 10216 solver.cpp:114] Iteration 500, loss = nan I0617 15:27:05.003675 10216 solver.cpp:141] Iteration 500, Testing net (#0) I0617 15:27:06.572185 10216 solver.cpp:179] Test score #0: 1 I0617 15:27:06.572234 10216 solver.cpp:179] Test score #1: nan I0617 15:27:10.060245 10216 solver.cpp:274] Iteration 600, lr = 0.0095724 I0617 15:27:10.060436 10216 solver.cpp:114] Iteration 600, loss = nan

Can anyone help me with it, or could you give me a example to do regression with Caffe?

Thank you very much!

Updates: Just in case anyone wants to work on multi-label regression problem, please refer to our project webpage: https://www.mpi-inf.mpg.de/departments/computer-vision-and-multimodal-computing/research/gaze-based-human-computer-interaction/appearance-based-gaze-estimation-in-the-wild/ In the "Method" part, you will find my configuration file as well as the Matlab code convert .mat to .h5 P.S. Thanks to https://github.com/BVLC/caffe/pull/1746, that is a time saver!

xucong-zhang avatar Jun 17 '14 13:06 xucong-zhang

As a heads-up, it does not really make sense to model classification as a regression problem: why is the distance between a "1" and "9" larger than that between "2" and "3", if we don't count in the semantic of digits?

This being said, your network probably suffers from large learning rates - decreasing it would eliminate the nan error, but again you probably won't get anything useful out of mnist regression.

Yangqing avatar Jun 19 '14 01:06 Yangqing

Hi, thank you for the comment! Sorry I didn't mention that I already replaced the mnist data with my own regression dataset, so actually it makes sense:) I decreased the learning rates and it works!

I didn't find any regression example yet (if you know some, please tell me!), so I will continue it and update my progress:)

Thank you very much!

xucong-zhang avatar Jun 19 '14 14:06 xucong-zhang

Hi, I also can't find the suitable accuracy layer for the regression, so I just changed the "accuracy_layer.cpp". I know it is very lazy, but I afraid to got more error if I create my own costumer layer:) I changed the loop in the cpp file to be:

for (int i = 0; i < num; ++i) {
    // Accuracy
    for (int j = 0; j < dim; ++j) { //xczhang change here!
      accuracy += sqrt((bottom_data[i * dim + j]-bottom_label[i * dim + j])*(bottom_data[i * dim + j]-bottom_label[i * dim + j]));
    }

    //Dtype prob = max(bottom_data[i * dim + static_cast<int>(bottom_label[i])],
    //                 Dtype(kLOG_THRESHOLD));
    //logprob -= log(prob);
  }

since I just need the accuracy, don't care the logprob, so just comment them. Now my regression finally works! Thank you for pointing out the learning rate problem@Yanqing!

xucong-zhang avatar Jun 20 '14 15:06 xucong-zhang

I think using accuracy for regression is misleading. Instead of modifying Accuracy use patch #522 which will allow you to use EuclideanLoss during test to get the loss during Test in the same way is used during Train

Replace accuracy layer in test prototxt with loss layer with a top

layers: {
 name: 'loss'
 type: EUCLIDEAN_LOSS
 bottom: 'fc7'
 bottom: 'label'
 top: 'loss'
};

sguada avatar Jun 20 '14 17:06 sguada

Hi, Thank you for the link about Accuracy layer! My regression code is running well!

xucong-zhang avatar Jun 22 '14 18:06 xucong-zhang

Glad it works :)

Yangqing avatar Jun 23 '14 14:06 Yangqing

@XucongZhang Does your regression work with many labels? I am trying to do a regression with caffe. In my problem, label is not only one value, it is a vector of float.

thuanvh avatar Jun 24 '14 11:06 thuanvh

@thuanvh Hi, glad someone mention that! Yes, I also use multi label. I used the [https://github.com/BVLC/caffe/pull/147]. The example how to generate the data is located in /caffe-dev/src/caffe/test/test_data generate_sample_data.py. You will get a txt file to store all the h5 file, and the network will read them one by one, in case you have lot of data. And also for the loading, here is how I did it:

name: "train"
layers {
  name: "MyData"
  type: HDF5_DATA
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "../train_data_list.txt"
    batch_size: 128
  }
}

xucong-zhang avatar Jun 24 '14 11:06 xucong-zhang

Thank @XucongZhang , I am using your suggestion. And I will inform you the result later.

thuanvh avatar Jun 25 '14 10:06 thuanvh

Hi @XucongZhang I have problem in using HDF5 file. I have 1500 image with size 60 x 60, and each image has 136 float values label. I create 3 .h5 files, each file contains 500 images and theirs samples.

Loading HDF5 files 0.h5 Successfully loaded 1 rows Output data size: 500 1500 60 60 Top shape: 500 1500 60 60 Top shape: 500 68000 1 1

And error at the loss layer

euclidean_loss_layer.cpp: check failed: bottom[0]->channels() == bottom[1]->channels() (136 vs 68000).

Why the label dimension is 68000?

thuanvh avatar Jun 25 '14 12:06 thuanvh

Hi @thuanvh I think you made mistake on generate the .h5 file. For your case, you have 3 files, and the image data should be "number_1_width_height", i.e. 500_1_60_60. And the output information should be "batchsize_1_width_height", But based on your output, you generated the input data as 500_1500_60_60; For the label, it should be "number_labels", i.e. 500_136. But based on the output, you generated the label data as 500_68000, which also means "batch size_labels". I believe the 68000 comes from 500*136, you mixed up the number of samples and number of labels to be one dimension. The label should be two dimensions: number of sample * labels. And it also shouldn't be "Successfully loaded 1 rows", it should be how many the samples.

For example, I have 1280 samples, each images is 36*60, the label is 3 float values, and the batch size is 128. I got this:

Loading HDF5 file/home/XXX/data.h5 Successully loaded 1280 rows output data size: 128,1,60,36 Top shape: 128 1 60 36 (276480) Top shape: 128 3 1 1 (384)

It is my experience and understanding, just for your reference.

xucong-zhang avatar Jun 25 '14 16:06 xucong-zhang

Hi @XucongZhang Well, your suggestion is very detailed and helpful. Thank you. Now I get your previous problem is that the loss is always NAN. I tried to changed base_lr in lenet_solver.prototxt from 0.01 to some tried smaller values. But no magic appears. In your problem, did you changed base_lr in lenet_solver?

thuanvh avatar Jun 26 '14 03:06 thuanvh

I tried to add some log line in Euclidiean Loss Layer file, and the loss value is well calculated. In file lenet_train.prototxt and lenet_test.prototxt, instead of using SOFTMAX_LOSS, I used EUCLIDEAN_LOSS.

layers { name: "loss" type: EUCLIDEAN_LOSS bottom: "ip2" bottom: "label" #top: "loss" } About this layer, the network does not permit me to add top layer as @sguada mentioned.

thuanvh avatar Jun 26 '14 04:06 thuanvh

That functionality requires #522 patch. So either wait until is integrated in dev or just apply the patch to your code.

Maybe is a problem in the weight or bias initialization, try bias=0.1

sguada avatar Jun 26 '14 04:06 sguada

@XucongZhang if you are willing to do an example of regression and a PR we will be happy to integrate it in Caffe.

sguada avatar Jun 26 '14 04:06 sguada

I applied the patch to master. In training progress, the loss is still nan, I wrote a log line in Euclidien Loss for loggin. I found that, after the first test, the loss become nan. Here the log I get

I0626 15:43:11.720676 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.1575
I0626 15:43:11.895576 12519 euclidean_loss_layer.cpp:59] Euclid dot: 2606.79 bottom[0]->num() : 50
I0626 15:43:11.895771 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.0679
I0626 15:43:12.071288 12519 euclidean_loss_layer.cpp:59] Euclid dot: 2615.75 bottom[0]->num() : 50
I0626 15:43:12.071467 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.1575
I0626 15:43:12.246860 12519 euclidean_loss_layer.cpp:59] Euclid dot: 2606.79 bottom[0]->num() : 50
I0626 15:43:12.247030 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.0679
I0626 15:43:12.424530 12519 euclidean_loss_layer.cpp:59] Euclid dot: 2615.75 bottom[0]->num() : 50
I0626 15:43:12.424700 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.1575
I0626 15:43:12.598495 12519 euclidean_loss_layer.cpp:59] Euclid dot: 2606.79 bottom[0]->num() : 50
I0626 15:43:12.598675 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.0679
I0626 15:43:12.772830 12519 euclidean_loss_layer.cpp:59] Euclid dot: 2615.75 bottom[0]->num() : 50
I0626 15:43:12.773038 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.1575
I0626 15:43:12.773110 12519 solver.cpp:142] Test score #0: 26.1127
I0626 15:43:12.976600 12519 euclidean_loss_layer.cpp:59] Euclid dot: 2606.79 bottom[0]->num() : 50
I0626 15:43:12.976780 12519 euclidean_loss_layer.cpp:61] Euclid loss: 26.0679
I0626 15:43:13.613327 12519 euclidean_loss_layer.cpp:59] Euclid dot: 1.74088e+10 bottom[0]->num() : 50
I0626 15:43:13.613502 12519 euclidean_loss_layer.cpp:61] Euclid loss: 1.74088e+08
I0626 15:43:14.170456 12519 euclidean_loss_layer.cpp:59] Euclid dot: inf bottom[0]->num() : 50
I0626 15:43:14.170627 12519 euclidean_loss_layer.cpp:61] Euclid loss: inf
I0626 15:43:14.707566 12519 euclidean_loss_layer.cpp:59] Euclid dot: -nan bottom[0]->num() : 50
I0626 15:43:14.707723 12519 euclidean_loss_layer.cpp:61] Euclid loss: -nan
I0626 15:43:15.249769 12519 euclidean_loss_layer.cpp:59] Euclid dot: -nan bottom[0]->num() : 50
I0626 15:43:15.249995 12519 euclidean_loss_layer.cpp:61] Euclid loss: -nan
I0626 15:43:15.781705 12519 euclidean_loss_layer.cpp:59] Euclid dot: -nan bottom[0]->num() : 50
I0626 15:43:15.781893 12519 euclidean_loss_layer.cpp:61] Euclid loss: -nan

As you see, after the line I0626 15:43:12.773110 12519 solver.cpp:142] Test score #0: 26.1127 the loss become nan, it's so crazy. I tried many time, but after the first test, the loss is nan.

thuanvh avatar Jun 26 '14 08:06 thuanvh

@thuanvh For the train part, I got the actual number after decreasing the base_lr in the solver file. And for the loss, I just changed the loss layer of train like this:

layers {
  name: "loss"
  type: EUCLIDEAN_LOSS
  bottom: "ip2"
  bottom: "label"
}

And for the test, I deleted the prob layer, and also changed the accuracy layer like this:

layers {
  name: "accuracy
  type: ACCURACY
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
}

I also modified the source code in caffe-dev/src/caffe/layers/accuracy_layer.cpp to get the euclidean distance instead of different of class. Then it works. To be honest, I don't know why there will be nan. I will also get it when I change the learn rate bigger.

xucong-zhang avatar Jun 26 '14 12:06 xucong-zhang

Why in the train part we not use output in layer loss, and in the test part we need its output (top) ?

thuanvh avatar Jun 26 '14 16:06 thuanvh

The loss is of course crucial, but it's unimportant apart from its role in the top derivative; it's purely diagnostic to report it. In training, the loss is forward computed and then backpropagation begins accordingly–there's no need for the loss to be output at the top of the network.

Look at the lenet_consolidated_solver.protoxt for an example of reporting the train and validation losses.

On Thu, Jun 26, 2014 at 9:32 AM, thuanvh [email protected] wrote:

Why in the train part we not use output in layer loss, and in the test part we need its output (top) ?

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

shelhamer avatar Jun 26 '14 16:06 shelhamer

Hi, from LeNet Mnist example, I do step by step some little changes based on your suggestion about regression so that assure the loss is not nan. Now it is a normal number. I am running the training.

I think the problem that I get is the scale of input data image. After scale to [0, 1], loss is not nan. Thank you all,

thuanvh avatar Jun 27 '14 03:06 thuanvh