Comments (2)
Hi @BIGBALLON ,
I tried to fix the error using only CPU support. I commented the line //#define USE_GPU
. I got the following console output:
carlos@carlos-ubuntu:~/Documents/git/Caffe2_scripts/03_cpp_forward$ ./classifier --file ./test_img/3.jpg
E0919 18:16:51.798885 31931 init_intrinsics_check.cc:43] CPU feature avx is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
E0919 18:16:51.799129 31931 init_intrinsics_check.cc:43] CPU feature avx2 is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
E0919 18:16:51.799139 31931 init_intrinsics_check.cc:43] CPU feature fma is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
== network loaded ==
== image size: [42 x 41] ==
== simply resize: [28 x 28] ==
== Tensor ==
== Blob got ==
== Data copied ==
== Net was run ==
== predictions got ==
== predicted label: 1 ==
== with probability: 70.3963% ==
*** Aborted at 1537373811 (unix time) try "date -d @1537373811" if you are using GNU date ***
PC: @ 0x7fc64079481d getenv
*** SIGSEGV (@0x0) received by PID 31931 (TID 0x7fc643069b40) from PID 0; stack trace: ***
@ 0x7fc6407904b0 (unknown)
@ 0x7fc64079481d getenv
@ 0x7fc6407d255a (unknown)
@ 0x7fc64087415c __fortify_fail
@ 0x7fc640874100 __stack_chk_fail
@ 0x4220be caffe2::run()
@ 0xbfd4bd36bfd893b0 (unknown)
Segmentation fault (core dumped)
The execution proceeded further, however, there is still an "Abortet at ..." message.
Note: I can execute your cpp predictor with GPU without any problem.
from caffe2-tutorial.
HI, @CarlosYeverino ,
see for details 11865
the full code:
/*******************************************************
* Copyright (C) 2018-2019 bigballon <[email protected]>
*
* This file is a caffe2 C++ image classification test
* by using pre-trained cifar10 model.
*
* Feel free to modify if you need.
*******************************************************/
#include "caffe2/core/common.h"
#include "caffe2/core/init.h"
#include "caffe2/core/tensor.h"
#include "caffe2/core/workspace.h"
#include "caffe2/utils/proto_utils.h"
// feel free to define USE_GPU if you want to use gpu
// #define USE_GPU
#ifdef USE_GPU
#include "caffe2/core/context_gpu.h"
#endif
// headers for opencv
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <map>
#include <string>
// define flags
CAFFE2_DEFINE_string(init_net, "./init_net.pb",
"The given path to the init protobuffer.");
CAFFE2_DEFINE_string(predict_net, "./predict_net.pb",
"The given path to the predict protobuffer.");
CAFFE2_DEFINE_string(file, "./image_file.jpg", "The image file.");
namespace caffe2 {
void loadImage(std::string file_name, float *imgArray) {
auto image = cv::imread(file_name); // CV_8UC3
std::cout << "== image size: " << image.size() << " ==" << std::endl;
// scale image to fit
cv::Size scale(28, 28);
cv::resize(image, image, scale);
std::cout << "== simply resize: " << image.size() << " ==" << std::endl;
// convert [unsigned int] to [float]
image.convertTo(image, CV_32FC1);
auto it = image.begin<float>();
for (unsigned i = 0; it != image.end<float>(); it++, i++) {
*(imgArray + i) = (*it);
}
}
void run() {
// define a caffe2 Workspace
Workspace workSpace;
// define initNet and predictNet
NetDef initNet, predictNet;
// read protobuf
CAFFE_ENFORCE(ReadProtoFromFile(FLAGS_init_net, &initNet));
CAFFE_ENFORCE(ReadProtoFromFile(FLAGS_predict_net, &predictNet));
// set device type
#ifdef USE_GPU
predictNet.mutable_device_option()->set_device_type(CUDA);
initNet.mutable_device_option()->set_device_type(CUDA);
std::cout << "== GPU processing selected "
<< " ==" << std::endl;
#else
predictNet.mutable_device_option()->set_device_type(CPU);
initNet.mutable_device_option()->set_device_type(CPU);
for (int i = 0; i < predictNet.op_size(); ++i) {
predictNet.mutable_op(i)->mutable_device_option()->set_device_type(CPU);
}
for (int i = 0; i < initNet.op_size(); ++i) {
initNet.mutable_op(i)->mutable_device_option()->set_device_type(CPU);
}
#endif
// load network
CAFFE_ENFORCE(workSpace.RunNetOnce(initNet));
CAFFE_ENFORCE(workSpace.CreateNet(predictNet));
std::cout << "== network loaded "
<< " ==" << std::endl;
// load image from file, then convert it to float array.
float imgArray[1 * 28 * 28];
loadImage(FLAGS_file, imgArray);
// define a Tensor which is used to stone input data
std::cout << "== Tensor "
<< " ==" << std::endl;
TensorCPU input;
input.Resize(std::vector<TIndex>({1, 1, 28, 28}));
input.ShareExternalPointer(imgArray);
// get "data" blob
#ifdef USE_GPU
auto data = workSpace.GetBlob("data")->GetMutable<TensorCUDA>();
#else
auto data = workSpace.GetBlob("data")->GetMutable<TensorCPU>();
#endif
std::cout << "== Blob got "
<< " ==" << std::endl;
// copy from input data
data->CopyFrom(input);
std::cout << "== Data copied "
<< " ==" << std::endl;
// forward
workSpace.RunNet(predictNet.name());
std::cout << "== Net was run "
<< " ==" << std::endl;
// get predictions blob and show the results
std::vector<std::string> labelName = {"0", "1", "2", "3", "4",
"5", "6", "7", "8", "9"};
#ifdef USE_GPU
auto predictions =
TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>());
#else
auto predictions = workSpace.GetBlob("predictions")->Get<TensorCPU>();
#endif
std::cout << "== predictions got "
<< " ==" << std::endl;
std::vector<float> probs(predictions.data<float>(),
predictions.data<float>() + predictions.size());
auto max = std::max_element(probs.begin(), probs.end());
auto index = std::distance(probs.begin(), max);
std::cout << "== predicted label: " << labelName[index]
<< " ==\n== with probability: " << (*max * 100)
<< "% ==" << std::endl;
}
} // namespace caffe2
// main function
int main(int argc, char **argv) {
caffe2::GlobalInit(&argc, &argv);
caffe2::run();
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
from caffe2-tutorial.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from caffe2-tutorial.