Softmax loss on 13 classes, get "Number of labels must match number of predictions"

3,913 views
Skip to first unread message

David Chotard

unread,
Jun 9, 2015, 3:56:58 PM6/9/15
to caffe...@googlegroups.com
Hi,

I am trying to train a very simple classification network on 13 classes defined as:

name: "myOwnNet"
layers {
  name: "data"
  type: HDF5_DATA
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "/path/to/input.txt"
    batch_size: 1
  }
  include: { phase: TRAIN }
}
layers {
  name: "data"
  type: HDF5_DATA
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "/path/to/input2.txt"
    batch_size: 1
  }
  include: { phase: TEST }
}
layers {
  name: "fc1"
  type: INNER_PRODUCT
  bottom: "data"
  top: "fc1"
  inner_product_param {
    num_output: 13
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layers {
  name: "loss"
  type: SOFTMAX_LOSS
  bottom: "fc1"
  bottom: "label"
  top: "loss"
}

However when I try to train it I get the following error:

" F0609 14:23:01.101641 12048 softmax_loss_layer.cpp:42] Check failed: outer_num_ * inner_num_ == bottom[1]->count() (1 vs. 13) Number of labels must match number of predictions; e.g., if softmax axis == 1 and prediction shape is (N, C, H, W), label count (number of labels) must be N*H*W, with integer values in {0, 1, ..., C-1}. "

It looks like there is a dimension mismatch with my labels but I cannot figure out where.
I have built the hdf5 file using matlab from a label vector of 13 * 140 (where is 140 is my number of sample).
I used code from #1746
Here is the hdf5 content using h5disp in matlab:

Group '/'
    Dataset 'data'
        Size:  100x100x8x140
        MaxSize:  100x100x8xInf
        Datatype:   H5T_IEEE_F64LE (double)
        ChunkSize:  100x100x8x10
        Filters:  deflate(9)
        FillValue:  0.000000
    Dataset 'label'
        Size:  13x140
        MaxSize:  13xInf
        Datatype:   H5T_IEEE_F32LE (single)
        ChunkSize:  13x10
        Filters:  deflate(9)
        FillValue:  0.000000

Here is what caffe loads:

I0609 14:23:01.023202 12048 net.cpp:127] Top shape: 1 8 100 100 (80000)
I0609 14:23:01.023232 12048 net.cpp:127] Top shape: 1 13 (13)

Any idea about what is wrong?
Thanks

David Chotard

unread,
Jun 16, 2015, 12:39:29 PM6/16/15
to caffe...@googlegroups.com
Finally found my problem.
I was assuming labels should be binary vectors. E.G:
C0 = [1 0 0 0 0 0 0 0 0 0 0 0 0]T
C1 = [0 1 0 0 0 0 0 0 0 0 0 0 0]T
...
I was actually taught like that in my machine learning class.

However, caffe expects labels directly as integers. C0 = 0 ; C1 = 1 ; ... ; C12 = 12 even if you have 13 neurons on the output layer...
I changed my labels and used a softmax_loss layer that deals with integer labels. That works really well now!
I found the information at http://caffe.berkeleyvision.org/doxygen/classcaffe_1_1SoftmaxWithLossLayer.html#details , but nowhere in any tutorial or example.

So I hope this can be useful to others.

little solider Chang

unread,
Oct 4, 2015, 10:46:09 AM10/4/15
to Caffe Users
Hi, I have the similar problem with softmaxloss. 

But I saw many examples from others and they said softmaxloss should work with multi-labels.

Have you solved this problems?

Thanks.

在 2015年6月16日星期二 UTC+1下午5:39:29,David Chotard写道:

张逸凡

unread,
Jan 9, 2016, 4:25:18 AM1/9/16
to Caffe Users
hi i want to know how u changed your label format? thx

在 2015年6月17日星期三 UTC+8上午12:39:29,David Chotard写道:

Sultana Tania

unread,
May 17, 2018, 11:15:54 PM5/17/18
to Caffe Users
Hi, I have also the same problem in softmaxloss.I am new in caffe.Could you please help me?
Reply all
Reply to author
Forward
0 new messages