Skip to content

Instantly share code, notes, and snippets.

@onauparc
Created June 17, 2014 13:36
Show Gist options
  • Save onauparc/dd80907401b26b602885 to your computer and use it in GitHub Desktop.
Save onauparc/dd80907401b26b602885 to your computer and use it in GitHub Desktop.
sample code for caffe C++ prediction
#include <cuda_runtime.h>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>
#include <stdio.h>
#include "caffe/caffe.hpp"
#include "caffe/util/io.hpp"
#include "caffe/blob.hpp"
using namespace caffe;
using namespace std;
int main(int argc, char** argv) {
if (argc < 4 || argc > 6) {
LOG(ERROR) << "test_net net_proto pretrained_net_proto iterations "
<< "[CPU/GPU] [Device ID]";
return 1;
}
Caffe::set_phase(Caffe::TEST);
//Setting CPU or GPU
if (argc >= 5 && strcmp(argv[4], "GPU") == 0) {
Caffe::set_mode(Caffe::GPU);
int device_id = 0;
if (argc == 6) {
device_id = atoi(argv[5]);
}
Caffe::SetDevice(device_id);
LOG(ERROR) << "Using GPU #" << device_id;
} else {
LOG(ERROR) << "Using CPU";
Caffe::set_mode(Caffe::CPU);
}
//get the net
Net<float> caffe_test_net(argv[1]);
//get trained net
caffe_test_net.CopyTrainedLayersFrom(argv[2]);
//get datum
Datum datum;
if (!ReadImageToDatum("./cat.png", 1, 227, 227, &datum)) {
LOG(ERROR) << "Error during file reading";
}
//get the blob
Blob<float>* blob = new Blob<float>(1, datum.channels(), datum.height(), datum.width());
//get the blobproto
BlobProto blob_proto;
blob_proto.set_num(1);
blob_proto.set_channels(datum.channels());
blob_proto.set_height(datum.height());
blob_proto.set_width(datum.width());
const int data_size = datum.channels() * datum.height() * datum.width();
int size_in_datum = std::max<int>(datum.data().size(),
datum.float_data_size());
for (int i = 0; i < size_in_datum; ++i) {
blob_proto.add_data(0.);
}
const string& data = datum.data();
if (data.size() != 0) {
for (int i = 0; i < size_in_datum; ++i) {
blob_proto.set_data(i, blob_proto.data(i) + (uint8_t)data[i]);
}
}
//set data into blob
blob->FromProto(blob_proto);
//fill the vector
vector<Blob<float>*> bottom;
bottom.push_back(blob);
float type = 0.0;
const vector<Blob<float>*>& result = caffe_test_net.Forward(bottom, &type);
//Here I can use the argmax layer, but for now I do a simple for :)
float max = 0;
float max_i = 0;
for (int i = 0; i < 1000; ++i) {
float value = result[0]->cpu_data()[i];
if (max < value){
max = value;
max_i = i;
}
}
LOG(ERROR) << "max: " << max << " i " << max_i;
return 0;
}
@wusx11
Copy link

wusx11 commented Jul 9, 2014

Hello onauparc, I've been using caffe to do some classification recently, and I saw your method to do prediction. 
But I still have confusions... 
How could I run this .cpp file? If I compile it with g++ directly, it would show the error that "cuda_runtime.h: No such file or directory,compilation terminated"...
It seems that the .sh file invokes the .bin file in /build/tools. Is this necessary, that I have to generate a .bin file from the .cpp?

@svanschalkwyk
Copy link

Hi
Where would one subtract the image mean?
Thanks
Steph

@joeupwu
Copy link

joeupwu commented Mar 10, 2015

Please check my usage with MemoryDataLayer here: BVLC/caffe#499

@tzutalin
Copy link

Hello,
If you want to use Eclispe, you can include caffe and build your own source files by following this.
http://tzutalin.blogspot.tw/2015/05/caffe-on-ubuntu-eclipse-cc.html

@zxxmac
Copy link

zxxmac commented Aug 31, 2015

Hello:
I run this code, find this error:_debugger_hook_dummy = 0;, I don't konw why

@xjtuljy
Copy link

xjtuljy commented Feb 8, 2016

Thanks for sharing the code!
I noticed that there is no pre-processing (e.g. mean subtraction, scaling, etc.) What shall I do to scale the input blob using C++?

@everal
Copy link

everal commented Mar 15, 2016

Hello:
The line 87 in you source code is
"float value = result[0]->cpu_data()[i]"
I noticed that it always result[0] in the for-loop,
Does it means that result[1],result[2]....... is store the other output layer' result in multi-task situation?

@avishayzanbar
Copy link

Hello,
I am a new user of Caffe.
I need to get the vectors after the fully-conventional layers in c++ (after fc6, fc7 and fc8 in this case)
I know i can get them in Python, for example by: net.blobs['fc7'].data[0], but I couldn't find a way to do that on C++.
Could anyone please help or refer me to the answer?
thanks a lot

@Jp1337
Copy link

Jp1337 commented May 1, 2016

Hello,

Nice sample! Im a bit of a caffe-newbie, although I managed to install all the necessary things and run the included cpp classification example, and mod it to classify webcam input, I can't find an example on how to TRAIN a network with caffe, using C++. Could you please give some hints on how to turn this into something that can train a network?
Thanks for your time!

@Arieal-zz
Copy link

How to run the .cpp file in visual studio??

@szm-R
Copy link

szm-R commented Sep 30, 2016

When I try to compile this code I get this error (along with so many others):
error: ‘set_phase’ is not a member of ‘caffe::Caffe’
I'm using this make file to compile this code:
`
CXX=g++

CFLAGS = -c -Wall -std=c++0x -g3 -Ofast -msse2

include directories

INCLUDE = -I. -I/usr/local/cuda/include -I/home/zeinab/Thesis/DeepLearning/Caffe/caffe-master/include/ -I/home/zeinab/Thesis/DeepLearning/Caffe/caffe-master/src/

library paths in addition to /usr/lib

LDFLAGS = -L/usr/local/lib -L/usr/local/cuda/lib64 -L/usr/local/cuda/lib -lcudart -lcublas -lcurand -lglog -lgflags -lprotobuf -lleveldb -lsnappy -llmdb -lboost_system -lhdf5_hl -lhdf5 -lm -lopencv_core -lopencv_highgui -lopencv_imgproc -lboost_thread -lstdc++ -lcblas -latlas -L/home/zeinab/Thesis/DeepLearning/Caffe/caffe-master/build/lib/ -lcaffe

cpp source file

SRC = Detection_v2.cpp

define the C object files

OBJS = $(SRC:.c=.o)
EXE = Detection

all: $(SRC) $(EXE)

clean:
rm -f _.o
rm -f $(EXE)
find ./ -name _.o -delete

$(EXE): $(OBJS)
$(CXX) $(OBJS) $(INCLUDE) $(LDFLAGS) -o $@

.cpp.o:
$(CXX) $(CFLAGS) $&lt; -o $@`

@szm-R
Copy link

szm-R commented Sep 30, 2016

I was able to resolve this by omitting set_phase and adding TEST to Net like: Net caffe_test_net(argv[1],TEST);
However when I try to run it with this command:
./test deploy_original.prototxt bvlc_reference_caffenet.caffemodel 1
I get this:
Trying to copy blobs of different sizes.
*** Check failure stack trace: **
Aborted

after going through all the layers with Initializing net from parameters: (why should it do this in the first place? Isn't initialization for training time?)

@xjtuljy
Copy link

xjtuljy commented Jan 12, 2017

Thank you. If there are two image inputs to the network (e.g. image0 and image1), shall I just create 2 blobs and feed them into the bottom_vec by "bottom.push_back(blob10);bottom.push_back(blob1);"? How shall we define that in the .prototxt as well?

@UTTsamir
Copy link

UTTsamir commented Sep 1, 2017

/usr/bin/ld: obj/Debug/main.o||undefined reference to symbol '_ZN6google8protobuf8internal11LogFinisheraSERNS1_10LogMessageE'|
I have this error, someone can help me please ??
the problem is with this instruction : blob_proto.set_data(i, blob_proto.data(i) + data[i]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment