Skip to content

Commit

Permalink
fix cuda error when terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
happynear committed Nov 4, 2016
1 parent 835834e commit b3e7d2e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 16 deletions.
14 changes: 8 additions & 6 deletions code/codes/vs/CascadeFaceDetection/CascadeFaceDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "pyboostcvconverter.hpp"

using namespace std;
std::shared_ptr<caffe::CaffeBinding> kCaffeBinding = std::make_shared<caffe::CaffeBinding>();
caffe::CaffeBinding* kCaffeBinding = new caffe::CaffeBinding();

namespace FaceInception {
CascadeCNN* cascade;
Expand Down Expand Up @@ -49,15 +49,17 @@ namespace FaceInception {
}

CascadeFaceDetection::CascadeFaceDetection(std::string net12_definition, std::string net12_weights,
std::string net12_stitch_definition, std::string net12_stitch_weights,
std::string net24_definition, std::string net24_weights,
std::string net48_definition, std::string net48_weights,
std::string netLoc_definition, std::string netLoc_weights,
int gpu_id) {
cascade = new CascadeCNN(net12_definition, net12_weights,
net24_definition, net24_weights,
net48_definition, net48_weights,
netLoc_definition, netLoc_weights,
gpu_id);
net12_stitch_definition, net12_stitch_weights,
net24_definition, net24_weights,
net48_definition, net48_weights,
netLoc_definition, netLoc_weights,
gpu_id);
}

std::vector<FaceInformation> CascadeFaceDetection::Predict(cv::Mat& input_image, double min_confidence, double min_face) {
Expand Down Expand Up @@ -89,6 +91,6 @@ namespace FaceInception {

CascadeFaceDetection::~CascadeFaceDetection() {
delete cascade;
kCaffeBinding = nullptr;
delete kCaffeBinding;
}
}
1 change: 1 addition & 0 deletions code/codes/vs/CascadeFaceDetection/CascadeFaceDetection.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace FaceInception {
public:
CascadeFaceDetection();
CascadeFaceDetection(std::string net12_definition, std::string net12_weights,
std::string net12_stitch_definition, std::string net12_stitch_weights,
std::string net24_definition, std::string net24_weights,
std::string net48_definition, std::string net48_weights,
std::string netLoc_definition, std::string netLoc_weights,
Expand Down
1 change: 1 addition & 0 deletions code/codes/vs/CascadeFaceDetection/python_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace FaceInception {
std::string, std::string,
std::string, std::string,
std::string, std::string,
std::string, std::string,
int>())
.def("Predict", Predict_func1);
}
Expand Down
7 changes: 4 additions & 3 deletions code/codes/vs/Test/TestFaceDetection.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#else
#pragma comment(lib,"opencv_world310.lib")
#endif
extern std::shared_ptr<caffe::CaffeBinding> kCaffeBinding;
extern caffe::CaffeBinding* kCaffeBinding;

using namespace cv;
using namespace std;
Expand Down Expand Up @@ -73,7 +73,8 @@ namespace FaceInception {
if (start_scale != 1) resize(start_image, start_image, Size(0, 0), start_scale, start_scale);
vector<pair<Rect2d, float>> accumulate_rects;

while (start_image.rows > 100 && start_image.cols > 100) {//This number depends on how much GPU memory you have. Please test and modify it to get maximal speed.
//This number(20000) depends on how much GPU memory you have. Please test and modify it to get maximal speed.
while (start_image.rows * start_image.cols > 20000 && start_image.rows > 12 && start_image.cols > 12) {
auto net12output = kCaffeBinding->Forward({ start_image }, net12);
if (!(net12output["bounding_box"].size[1] == 1 && net12output["bounding_box"].data[0] == 0)) {
vector<pair<Rect2d, float>> before_nms;
Expand All @@ -93,7 +94,7 @@ namespace FaceInception {
}
}
start_scale *= scale_decay_;
resize(input_image, start_image, Size(0, 0), start_scale, start_scale);
resize(start_image, start_image, Size(input_image.cols * start_scale, input_image.rows * start_scale));
}
if (start_image.rows > 12 && start_image.cols > 12) {
std::vector<std::pair<Rect, double>> location_and_scale;
Expand Down
11 changes: 6 additions & 5 deletions code/codes/vs/Test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "boost/make_shared.hpp"
#include "TestFaceDetection.inc.h"

std::shared_ptr<caffe::CaffeBinding> kCaffeBinding = std::make_shared<caffe::CaffeBinding>();
caffe::CaffeBinding* kCaffeBinding = new caffe::CaffeBinding();

using namespace FaceInception;

Expand Down Expand Up @@ -170,10 +170,10 @@ int main(int argc, char* argv[])
0);
//CaptureDemo(cascade);

double min_face_size = 12;
double min_face_size = 24;

//ScanList("H:\\lfw\\list.txt", cascade);
Mat image = imread("D:\\face project\\WIDER\\face_detection\\test_image.jpg");
Mat image = imread("D:\\face project\\images\\08.jpg");

//Mat image = imread("G:\\WIDER\\face_detection\\pack\\1[00_00_26][20160819-181452-0].BMP");
//Mat image = imread("D:\\face project\\FDDB\\2002/07/25/big/img_1047.jpg");
Expand All @@ -195,7 +195,7 @@ int main(int argc, char* argv[])

for (int i = 0; i < result.size(); i++) {
//cout << "face box:" << result[i].first << " confidence:" << result[i].second << endl;
rectangle(image, result[i].first, Scalar(255, 0, 0), 4);
rectangle(image, result[i].first, Scalar(255, 0, 0), 2);
if (points.size() >= i + 1) {
for (int p = 0; p < 5; p++) {
circle(image, points[i][p], 2, Scalar(0, 255, 255), -1);
Expand All @@ -208,7 +208,8 @@ int main(int argc, char* argv[])
imshow("final", image);
waitKey(0);
//imwrite("output.jpg", image);
TestFDDBPrecision(cascade, "D:\\face project\\FDDB\\", true, true);
//TestFDDBPrecision(cascade, "D:\\face project\\FDDB\\", true, true);
delete kCaffeBinding;
system("pause");
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion code/codes/vs/Test/util/BoundingBox.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ namespace FaceInception {

//Only support 2 scaling
Mat getPyramidStitchingImage2(Mat& input_image, std::vector<std::pair<Rect, double>>& location_and_scale, double scaling = 0.707,
Scalar background_color = Scalar(0,0,0), int min_side = 12, int interval = 4) {
Scalar background_color = Scalar(0,0,0), int min_side = 12, int interval = 2) {
using namespace std;
bool stitch_x = input_image.cols < input_image.rows;
Size current_size = input_image.size();
Expand Down
3 changes: 2 additions & 1 deletion code/codes/vs/testPyCascadeCNN/testPyCascadeCNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

model_folder = "D:/face project/MTCNN_face_detection_alignment/code/codes/MTCNNv2/model/"

print "loading model from" + model_folder + "..."
print "loading model from " + model_folder + "..."
CascadeCNN = CascadeFaceDetection.CascadeCNN(model_folder + "det1-memory.prototxt", model_folder + "det1.caffemodel",
model_folder + "det1-memory-stitch.prototxt", model_folder + "det1.caffemodel",
model_folder + "det2-memory.prototxt", model_folder + "det2.caffemodel",
model_folder + "det3-memory.prototxt", model_folder + "det3.caffemodel",
model_folder + "det4-memory.prototxt", model_folder + "det4.caffemodel",
Expand Down

4 comments on commit b3e7d2e

@zhengmzong
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 Hi,I still got the cuda error when  terminate when I used the latest code.
 My environment is win10, VS2015 sp1, cuda 8.0, and use the caffe you suggested in this github. The GPU is gtx 750Ti. When I run the program in Release x64 mode , the project TestCascadeCNN got the cuda error when terminated. However, when I try to run the program in Debug X64 mode, the program could't start. I got the error : 0x00007FFF4C082F20 (ntdll.dll)处(位于 TestCascadeCNN.exe 中)引发的异常: 0xC0000139: Entry Point Not Found。And I have tried the simple program about HelloWorld in Debug X64 mode . It worked !

 Would you give me some suggestion how to sovle the cuda error ?

@zhengmzong
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came here to answer my own problem!
The key point is that the static buffer causes this crash due to CUDA ! Thanks to@shelhamer'post BVLC/caffe#2016 .

I solved this problem by edit this two files: CaffeBing.cpp and CaffeBing.h .
Just move this codes in CaffeBing.cpp
std::vector<caffe::Net*> nets_;//no shared_ptr here.
std::vector<std::shared_ptr<boost::thread_specific_ptr<caffe::Net>>> predictors_;
std::vector prototxts;
to CaffeBing.h as private members of CaffeBinding.

This changes work in Linux !

However , I still got problem when I compiled the project TestCasecadeCNN in windows .
E:\FDDB\caffe-windows-ms\include\caffe/proto/caffe.pb.h(2687): error C2059: 语法错误:“常量”
1>E:\FDDB\caffe-windows-ms\include\caffe/proto/caffe.pb.h(2688): error C2238: 意外的标记位于“;”之前
1>E:\FDDB\caffe-windows-ms\include\caffe/proto/caffe.pb.h(10702): error C2059: 语法错误:“常量”
1>E:\FDDB\caffe-windows-ms\include\caffe/proto/caffe.pb.h(10703): error C2238: 意外的标记位于“;”之前
I have no idea about it .

@happynear
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhengmzong,

The reason why I put the nets_ in CaffeBinding.cpp is that I don't want to include the Caffe header files in a new project. Can you find another way to preserve the variables still in CaffeBinding.cpp?

@happynear
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhengmzong
I just submitted the solution. I waste too much time on this problem. The final solution is quite simple:
remove CUDA_CHECK().
sigh.
happynear/caffe-windows@ada2fee

Please sign in to comment.