-
-
Save onauparc/dd80907401b26b602885 to your computer and use it in GitHub Desktop.
#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; | |
} |
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?)
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?
/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]);
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
.cpp.o:
$(CXX) $ (CFLAGS) $< -o $ @`