Skip to content

How to make top-k predict #598

@shuokay

Description

@shuokay

I have made top-1 predict using ARGMAX layer, but now I want to make a top-k predict. I have found #531 ,but I cant find how to make predict using accuracy layer with the top-k field.
secondly, I found this code https://gist.github.com/onauparc/dd80907401b26b602885 mentioned in #499 , however, as I used a IMAGE_DATA layer, I cannot get right result using that code.
here is my code:

  Net<float> caffe_test_net(argv[1]);
  caffe_test_net.CopyTrainedLayersFrom(argv[2]);
  float loss;
  float right=0;
  const vector<Blob<float>*>& result =  caffe_test_net.ForwardPrefilled(&loss);
  for (int j=0; j<result[1]->num(); ++j)
  {
    float max = 0;
    float max_i = 0;
    for (int i = 0; i < 3904; ++i) {
        float value = result[1]->cpu_data()[j*3904+i];
        if (max < value){
        max = value;
        max_i = i;
        }
    }
  }

here is just the top-1 code, I will change it to top-k code later.
where 3904 is the number of categories, because I dont know how to get them from the blobs, I just write them in code.
Will someone kindly tell me the last layer is SOFTMAX or ACCURACY if i want to make predict in C++ without an ARGMAX layer ? I think it is SOFTMAX , however because I want to get the labels, if I make the last layer SOFTMAX, more errors come out that ACCURACY.
the last two layers of my proto file:

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

updated:
I have solved most problems of this code, the answers are followed:

  1. I have updated my code, in the code, the only problem is how to get the numbers of categories(3904) from the blobs instead of just fix it in code.
  2. the last layer should beACCURACY layer.

I have not really understand the difference between SOFTMAX layer and ACCURACY layer, and I will go on to read the caffe source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions