caffe icon indicating copy to clipboard operation
caffe copied to clipboard

how to make a prediction in C++

Open onauparc opened this issue 12 years ago • 49 comments

Hi, I'm using caffe and I'm trying to make a prediction in C++ with a pretrained network like this python example http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/imagenet_classification.ipynb. But I don't find a C++ method for prediction and I don't get how to pass an input image and make the prediction. I've seen test_net.cpp but it does not pass any image in input. How can I pass an image or more images in input to the network? How can I make a prediction?

onauparc avatar Jun 13 '14 16:06 onauparc

You could use a Image-Data layer to specify which images need to be processed.

On Friday, June 13, 2014, onauparc [email protected] wrote:

Hi, I'm using caffe and I'm trying to make a prediction in C++ with a pretrained network like this python example http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/imagenet_classification.ipynb. But I don't find a C++ method for prediction and I don't get how to pass an input image and make the prediction. I've seen test_net.cpp but it does not pass any image in input. How can I pass an image or more image in input to the network? How can I make a prediction?

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/499.

Sergio

sguada avatar Jun 14 '14 14:06 sguada

Hi, I think I've found how to make a prediction. I used this code:

Datum datum; ReadImageToDatum("./cat.png", 10, 227, 227, &datum); Blob* blob = new Blob(10, datum.channels(), datum.height(), datum.width()); vector<Blob> bottom; bottom.push_back(blob); float type = 0.0; const vector<Blob>& result = caffe_test_net.Forward(bottom, &type);

Now I have another doubt. How can I get the predicted class?

Here https://github.com/BVLC/caffe/blob/a34816263c1e5181ac0eb857a20fdaabd07b80e9/tools/test_net.cpp I saw that I can get the accuracy in this way: result[0]->cpu_data()[0]; But I need the predicted class. What does cpu_data() contain?

onauparc avatar Jun 16 '14 16:06 onauparc

For doing predictions with just one image, you don't need to create a blob with 10 images. Although you will need to change the prototxt to reflect that. You just need to find the class with the maximum prob, for that you can use argmax layer #421 on top of the prob layer, then result will contain the predicted classes.

What result contains depends on the last layer in the prototxt.

sguada avatar Jun 16 '14 17:06 sguada

Ok, perfect. I'll see. Thank you for your answers.

onauparc avatar Jun 17 '14 09:06 onauparc

Hi, I found how to make the predicion in C++, like the python image_net_classification example. Here https://gist.github.com/onauparc/dd80907401b26b602885 you can find my example, and it works! (It is based on this code https://github.com/BVLC/caffe/blob/master/tools/compute_image_mean.cpp.) The predicted class of the cat image is class 274, so it seems like the python example.

The problem is that in my example I did a lot of copy and rounds: I put data into datum, than I put it in in a blobproto, than in a proto, finally I can do the Forward! Since I have a minibatch input made by some opencv Mat, is there any example easier or more direct?

onauparc avatar Jun 17 '14 13:06 onauparc

@onauparc I think you are making things a bit complicated. Also you forgot to subtract the mean_image.

I would recommend you use a image_data_layer (which take care of reading a list of images, cropping, resizing, subtracting the mean, ...) at top of your prototxt, and the argmax_layer at the bottom of your prototxt.

That way you will only need to specify a file containing the set of images you want to do prediction for, and you would only to do:

 //get the net
  Net<float> caffe_test_net(argv[1]);
  //get trained net
  caffe_test_net.CopyTrainedLayersFrom(argv[2]);
 // Run ForwardPrefilled 
  float loss;
  vector<Blob<float>*>& result =  caffe_test_net.ForwardPrefilled(&loss);
// Now result will contain the argmax results.
 float* argmaxs = result[0]->cpu_data();
  for (int i = 0; i < result[0]->num(); ++i) {
   LOG(INFO) << " Image: "<< i << " class:" << argmaxs[i];
  }

sguada avatar Jun 17 '14 18:06 sguada

@onauparc If you have an image list on filesystem the best solution is to plug an image_data layer on top like in this example https://github.com/BVLC/caffe/blob/master/examples/feature_extraction/imagenet_val.prototxt @sguada I think if your mini batch is an in memory vector of opencv Mat there is no fast/direct way to fill the top layer. Probably only trying to take some code from image_data layer could be possibile to fill it with an in memory vector of Mat. Is this correct?

bhack avatar Jun 17 '14 20:06 bhack

Or probably could be also used memory data layer?

bhack avatar Jun 17 '14 20:06 bhack

@onauparc if you need in memory fill follow this PR https://github.com/BVLC/caffe/pull/251

bhack avatar Jun 17 '14 20:06 bhack

@bhack There is not simple way to transform a opencv::Mat into a blob, since they store the data differently internally. Also usually there are several pre-processing steps to do, like mean subtraction, cropping, mirror, ... Once the data layer separates the data source from the data pre-processing #407 one could reuse some parts.

sguada avatar Jun 17 '14 20:06 sguada

@sguada I.e. try to suppose that your mini batch is a vector of opencv Mat with fixed widht and height coming from candidate ROIs regions selected from a video frame. Why #251 could not work? Probably we cannot avoid to do it with zero copy but generally i think it could work.

bhack avatar Jun 17 '14 21:06 bhack

@bhack what I meant is that one still need to do the pre-processing steps like Image_Data layers does. That means it is not simply copying pixels or using directly in-memory #196. Once data_source and data_prepocessing are separated then will be easier to add more data sources, including a vector of opencv Mat.

Although #251 was never finished or integrated, one could easily extend #196 or Image_data layer to do this. https://github.com/BVLC/caffe/blob/dev/src/caffe/layers/image_data_layer.cpp

But if you want to do PR for this it will be useful. If you tell me a bit more what do you have in mind I may be able to help more.

sguada avatar Jun 17 '14 21:06 sguada

@sguada Yes in layers we have pre-processing steps and data mixed. #407 is very interesting but also this one is still not integrated like #251. Probably taking something from #251 could be a little bit easy as inspiration to unwrap opencv Mat structure in the meantime that a more general solution like #407 will be finalized.

bhack avatar Jun 17 '14 22:06 bhack

@sguada The general idea is that users could have a standard computer vision pipeline with opencv (taking Mat from video or single image), than find (with opencv or using opencv inside caffe) some candidate regions inside the frame with an X algorithm (resulting in a vector of cv::Mat or a vector of Rect). Then do some pre-processing on this regions (still in opencv or in the preprocessing step inside Caffe) and then pass this region as fast as (minimizing copy) to the forward pass. So something from #251 actually could be useful. #407 seems a more general arch change that probably impact many things including the future of #251.

bhack avatar Jun 17 '14 22:06 bhack

Hi @sguada , as you suggested I've tried to use data_layer and then argmax layer with your code to make my prediction but I still have strong doubts.

This is the prototxt I used to make the net:

name: "CaffeNet"
layers {
  name: "data"
  type: IMAGE_DATA
  top: "data"
  top: "label"
  image_data_param {
    source: "my_path/examples/_temp/file_list.txt"
    mean_file: "my_path/data/ilsvrc12/imagenet_mean.binaryproto"
    batch_size: 4
    crop_size: 227
    mirror: false
    new_height: 256
    new_width: 256
  }
}
layers {
  name: "conv1"
  type: CONVOLUTION
  bottom: "data"       
  top: "conv1"         
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 96
    kernel_size: 11
    stride: 4
  }
}
layers {
  name: "relu1"
  type: RELU
  bottom: "conv1"      
  top: "conv1"         
}
layers {
  name: "pool1"
  type: POOLING
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layers {
  name: "norm1"
  type: LRN
  bottom: "pool1"
  top: "norm1"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
layers {
  name: "conv2"
  type: CONVOLUTION
  bottom: "norm1"
  top: "conv2"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 256
    pad: 2
    kernel_size: 5
    group: 2
  }
}
layers {
  name: "relu2"
  type: RELU
  bottom: "conv2"
  top: "conv2"
}
layers {
  name: "pool2"
  type: POOLING
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layers {
  name: "norm2"
  type: LRN
  bottom: "pool2"
  top: "norm2"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
layers {
  name: "conv3"
  type: CONVOLUTION
  bottom: "norm2"
  top: "conv3"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 384
    pad: 1
    kernel_size: 3
  }
}
layers {
  name: "relu3"
  type: RELU
  bottom: "conv3"
  top: "conv3"
}
layers {
  name: "conv4"
  type: CONVOLUTION
  bottom: "conv3"
  top: "conv4"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 384
    pad: 1
    kernel_size: 3
    group: 2
  }
}
layers {
  name: "relu4"
  type: RELU
  bottom: "conv4"
  top: "conv4"
}
layers {
  name: "conv5"
  type: CONVOLUTION
  bottom: "conv4"
  top: "conv5"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 256
    pad: 1
    kernel_size: 3
    group: 2
  }
}
layers {
  name: "relu5"
  type: RELU
  bottom: "conv5"
  top: "conv5"
}
layers {
  name: "pool5"
  type: POOLING
  bottom: "conv5"
  top: "pool5"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layers {
  name: "fc6"
  type: INNER_PRODUCT
  bottom: "pool5"
  top: "fc6"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 4096
  }
}
layers {
  name: "relu6"
  type: RELU
  bottom: "fc6"
  top: "fc6"
}
layers {
  name: "drop6"
  type: DROPOUT
  bottom: "fc6"
  top: "fc6"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layers {
  name: "fc7"
  type: INNER_PRODUCT
  bottom: "fc6"
  top: "fc7"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 4096
  }
}
layers {
  name: "relu7"
  type: RELU
  bottom: "fc7"
  top: "fc7"
}
layers {
  name: "drop7"
  type: DROPOUT
  bottom: "fc7"
  top: "fc7"
  dropout_param {
    dropout_ratio: 0.5
  }
}
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: 1000
  }
}
layers {
  name: "prob"
  type: SOFTMAX
  bottom: "fc8"
  top: "prob"
}
layers {
  name: "output"
  type: ARGMAX
  bottom: "prob"
  top: "output"
}

And the file file_list.txt contains this string: my_path/examples/images/cat.jpg 0

I noticed that argmax returns 0. Actually it returns the number near the image path. It seems it is a label and argmax returns this label. If I change the label with 1, argmax returns 1! I don't know if I'm making some mistakes, but this is not the behavior that I expect. I would like to have a network, train it, (now I'm doing these steps with pretrained examples), than pass to the net new images and make a prediction with this net. Where am I wrong?

onauparc avatar Jun 20 '14 10:06 onauparc

Since your prototxt have two free top blobs, label from the image_data layer and output from the argmax layer, then the result will contain two blobs, the first one being the image label and the second output being the predicted label. Currently you are looking at label, that's way when you change it, the result changes.

So you should look at the second blob in result. That way you can also compare if the predicted label is the same as the image label.

Sergio

2014-06-20 3:24 GMT-07:00 onauparc [email protected]:

Hi @sguada https://github.com/sguada , as you suggested I've tried to use data_layer and then argmax layer with your code to make my prediction but I still have strong doubts.

This is the prototxt I used to make the net:

name: "CaffeNet"layers { name: "data" type: IMAGE_DATA top: "data" top: "label" image_data_param { source: "my_path/examples/_temp/file_list.txt" mean_file: "my_path/data/ilsvrc12/imagenet_mean.binaryproto" batch_size: 4 crop_size: 227 mirror: false new_height: 256 new_width: 256 }}layers { name: "conv1" type: CONVOLUTION bottom: "data" top: "conv1" blobs_lr: 1 blobs_lr: 2 weight_decay: 1 weight_decay: 0 convolution_param { num_output: 96 kernel_size: 11 stride: 4 }}layers { name: "relu1" type: RELU bottom: "conv1" top: "conv1" }layers { name: "pool1" type: POOLING bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 3 stride: 2 }}layers { name: "norm1" type: LRN bottom: "pool1" top: "norm1" lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 }}layers { name: "conv2" type: CONVOLUTION bottom: "norm1" top: "conv2" blobs_lr: 1 blobs_lr: 2 weight_decay: 1 weight_decay: 0 convolution_param { num_output: 256 pad: 2 kernel_size: 5 group: 2 }}layers { name: "relu2" type: RELU bottom: "conv2" top: "conv2"}layers { name: "pool2" type: POOLING bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 3 stride: 2 }}layers { name: "norm2" type: LRN bottom: "pool2" top: "norm2" lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 }}layers { name: "conv3" type: CONVOLUTION bottom: "norm2" top: "conv3" blobs_lr: 1 blobs_lr: 2 weight_decay: 1 weight_decay: 0 convolution_param { num_output: 384 pad: 1 kernel_size: 3 }}layers { name: "relu3" type: RELU bottom: "conv3" top: "conv3"}layers { name: "conv4" type: CONVOLUTION bottom: "conv3" top: "conv4" blobs_lr: 1 blobs_lr: 2 weight_decay: 1 weight_decay: 0 convolution_param { num_output: 384 pad: 1 kernel_size: 3 group: 2 }}layers { name: "relu4" type: RELU bottom: "conv4" top: "conv4"}layers { name: "conv5" type: CONVOLUTION bottom: "conv4" top: "conv5" blobs_lr: 1 blobs_lr: 2 weight_decay: 1 weight_decay: 0 convolution_param { num_output: 256 pad: 1 kernel_size: 3 group: 2 }}layers { name: "relu5" type: RELU bottom: "conv5" top: "conv5"}layers { name: "pool5" type: POOLING bottom: "conv5" top: "pool5" pooling_param { pool: MAX kernel_size: 3 stride: 2 }}layers { name: "fc6" type: INNER_PRODUCT bottom: "pool5" top: "fc6" blobs_lr: 1 blobs_lr: 2 weight_decay: 1 weight_decay: 0 inner_product_param { num_output: 4096 }}layers { name: "relu6" type: RELU bottom: "fc6" top: "fc6"}layers { name: "drop6" type: DROPOUT bottom: "fc6" top: "fc6" dropout_param { dropout_ratio: 0.5 }}layers { name: "fc7" type: INNER_PRODUCT bottom: "fc6" top: "fc7" blobs_lr: 1 blobs_lr: 2 weight_decay: 1 weight_decay: 0 inner_product_param { num_output: 4096 }}layers { name: "relu7" type: RELU bottom: "fc7" top: "fc7"}layers { name: "drop7" type: DROPOUT bottom: "fc7" top: "fc7" dropout_param { dropout_ratio: 0.5 }}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: 1000 }}layers { name: "prob" type: SOFTMAX bottom: "fc8" top: "prob"}layers { name: "output" type: ARGMAX bottom: "prob" top: "output"}

And the file file_list.txt contains this string: my_path/examples/images/cat.jpg 0

I noticed that argmax returns 0. Actually it returns the number near the image path. It seems it is a label and argmax returns this label. If I change the label with 1, argmax returns 1! I don't know if I'm making some mistakes, but this is not the behavior that I expect. I would like to have a network, train it, (now I'm doing these steps with pretrained examples), than pass to the net new images and make a prediction with this net. Where am I wrong?

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

sguada avatar Jun 20 '14 18:06 sguada

Fatastic!!! It works :) Thank you so much!!! Now my next challenge is to take in input a series of opencv Mat :)

onauparc avatar Jun 23 '14 12:06 onauparc

Hi, @sguada , I find the image_data_param have not define a 'channel' varible that specify the input image is color or gray. How can I specify whether the input image is grayscale or color?

shuokay avatar Jun 24 '14 05:06 shuokay

@onauparc, I tried the ImageDataLayer as you asked in #251 using the code snippet of @sguada and your prototxt. It turns out that passing in a vector of cv::Mat can get exactly the same prediction results as loading the images from disk.

The predicted class label of the cat.jpg is 281 which corresponds to "n02120505 grey fox, gray fox, Urocyon cinereoargenteus" in the data/ilsvrc12/synset_words.txt. The fish-bike.jpg is predicted as 770 "n04118776 rule, ruler". If we want multiple top predictions, new parameters such as num_of_output_results need to be added to the ArgMaxLayer.

  //get the net
  Net<float> caffe_test_net(feature_extraction_proto);
  //get trained net
  caffe_test_net.CopyTrainedLayersFrom(pretrained_binary_proto);

  // Run ForwardPrefilled
  float loss;
//  const vector<Blob<float>*>& result = caffe_test_net.ForwardPrefilled(&loss);

// Run AddImagesAndLabels and Forward
  cv::Mat image = cv::imread("/path/to/caffe-fork/examples/images/fish-bike.jpg"); // or cat.jpg
  vector<cv::Mat> images(4, image);
  vector<int> labels(4, 0);
  const shared_ptr<ImageDataLayer<float> > image_data_layer =
      boost::static_pointer_cast<ImageDataLayer<float> >(
          caffe_test_net.layer_by_name("data"));
  image_data_layer->AddImagesAndLabels(images, labels);
  vector<Blob<float>* > dummy_bottom_vec;
  const vector<Blob<float>*>& result = caffe_test_net.Forward(dummy_bottom_vec, &loss);

  LOG(INFO)<< "Output result size: "<< result.size();
  // Now result will contain the argmax results.
  const float* argmaxs = result[1]->cpu_data();
  for (int i = 0; i < result[1]->num(); ++i) {
    LOG(INFO)<< " Image: "<< i << " class:" << argmaxs[i];
  }

kloudkl avatar Jun 24 '14 07:06 kloudkl

Ok, thank you @kloudki, it prefectly works! :) Though the creation of the net still depends on the "source" defined in the prototxt, though in this case it is not necessary. (I tried to remove it from the prototxt, or from file_list.txt but it doesn't work).

onauparc avatar Jun 24 '14 13:06 onauparc