Caffe C++ API
11 Jun 2016
Working with Caffe CPP API:
This tutorial explains the Caffe classification example. Implementations are present in
Required inputs:
- Model file: These is the file describing the layer configuration and its hyperparameters.
- trained_file: These is the file containing learned weights.
- mean_file: It is good idea to subtract the mean of all images in dataset. It essentially does background subtraction.
- label_file: CNN gives prediction probability for all the categories on which it was trained. The label file gives mapping between category id and class name.
Pipeline:
- set device(CPU/GPU)
- Load the network net_.reset(new Net
(model_file, TEST)); net_->CopyTrainedLayersFrom(trained_file); - Load the binaryproto mean file. SetMean(mean_file);
- Set input and output layer (Note: only single input and output layer supported.)
Blob
* input_layer = net_->input_blobs()[0]; Blob * output_layer = net_->output_blobs()[0]; - Compute prediction for given test image. std::vector
predictions = classifier.Classify(image); The prediction structure has two components. Prediction.first gives label and Prediction.second gives corresponding score.
The prediction method pipeline is given as follows:
-
Change the dimension of input layer
input_layer->Reshape(1, num_channels_, input_geometry_.height, input_geometry_.width);
-
Forward dimension change to all layers.
net_->Reshape();
- Wrap the input layer of the network in separate cv::Mat objects (one per channel). This way we save one memcpy operation and ww don’t need to rely on cudaMemcpy2D. The last preprocessing operation will write the separate channels directly to the input layer.
WrapInputLayer(&input_channels);
-
Convert the input image to the input image format of the network and write the separate BGR planes directly to the input layer of the network.
Preprocess(img, &input_channels);
-
Run forward pass for the network.
net_->ForwardPrefilled();
-
Copy the output layer to a std::vector
Blob
* output_layer = net_->output_blobs()[0]; const float* begin = output_layer->cpu_data(); const float* end = begin + output_layer->channels(); return std::vector (begin, end);
Harit Pandya