0x00007FF9E2DC4A30 (msvcr120.dll) unhandled exception: request a serious program exit - c++

Recently, I'm trying to modify the classification.cpp that given by the official website of caffe so that I can use my trained_models with C++. Although I comples it successfully, an unhandled excption occurs. How can I solve this?
Here is the partial code that I have modified (the definition of variates and functions are not changed):
int WinMain(int argc, char** argv) {
clock_t start_time1, end_time1, start_time2, end_time2;
string model_file = "D:\\mySoftware\\caffe-windows\\examples\\mnist\\lenet.prototxt";
string trained_file = "D:\\mySoftware\\caffe-windows\\examples\\mnist\\model1\\lenet_train_test.prototxt_iter_10000.caffemodel";
string mean_file = "D:\\mySoftware\\caffe-windows\\examples\\mnist\mean_file\\mean.binaryproto";
string label_file = "D:\\mySoftware\\caffe-windows\\examples\\mnist\\label.txt";
string img_file = "D:\\mySoftware\\caffe-windows\\examples\\mnist\\data1\\my_10_images\\0.png";
cv::Mat img = cv::imread(img_file);
CHECK(!img.empty()) << "Unable to decode image " << img;
start_time1 = clock();
Classifier classifier(model_file, trained_file, mean_file, label_file);
end_time1 = clock();
double seconds1 = (double)(end_time1 - start_time1) / CLOCKS_PER_SEC;
std::cout << "init time=" << seconds1 << "s" << std::endl;
start_time2 = clock();
std::vector<Prediction> predictions = classifier.Classify(img);
end_time2 = clock();
double seconds2 = (double)(end_time2 - start_time2) / CLOCKS_PER_SEC;
std::cout << "classify time=" << seconds2 << "s" << std::endl;
/* Print the top N predictions. */
for (size_t i = 0; i < predictions.size(); ++i) {
Prediction p = predictions[i];
std::cout << std::fixed << std::setprecision(4) << p.second << " - \""
<< p.first << "\"" << std::endl;
}
}

Related

Is std::clock() broken on MSYS2's g++ compiler?

I'm trying to write a simple single header benchmarker and I understand that std::clock will give me the time that a process (thread) is in actual use.
So, given the following simplified program:
nt main() {
using namespace std::literals::chrono_literals;
auto start_cpu = std::clock();
auto start_wall = std::chrono::high_resolution_clock::now();
// clobber();
std::this_thread::sleep_for(1s);
// clobber();
auto finish_cpu = std::clock();
auto finish_wall = std::chrono::high_resolution_clock::now();
std::cerr << "cpu: "
<< start_cpu << " " << finish_cpu << " "
<< (finish_cpu - start_cpu) / (double)CLOCKS_PER_SEC << " s" << std::endl;
std::cerr << "wall: "
// << FormatTime(start_wall) << " " << FormatTime(finish_wall) << " "
<< (finish_wall - start_wall) / 1.0s << " s" << std::endl;
return 0;
}
Demo
We get the following output:
cpu: 4820 4839 1.9e-05 s
wall: 1.00007 s
I just want to clarify that the cpu time is the time that it executes the code that is not actually the sleep_for code as that is actually done by the kernel which std::clock doesn't track. So to confirm, I changed what I was timing:
int main() {
using namespace std::literals::chrono_literals;
int value = 0;
auto start_cpu = std::clock();
auto start_wall = std::chrono::high_resolution_clock::now();
// clobber();
for (int i = 0; i < 1000000; ++i) {
srand(value);
value = rand();
}
// clobber();
std::cout << "value = " << value << std::endl;
auto finish_cpu = std::clock();
auto finish_wall = std::chrono::high_resolution_clock::now();
std::cerr << "cpu: "
<< start_cpu << " " << finish_cpu << " "
<< (finish_cpu - start_cpu) / (double)CLOCKS_PER_SEC << " s" << std::endl;
std::cerr << "wall: "
// << FormatTime(start_wall) << " " << FormatTime(finish_wall) << " "
<< (finish_wall - start_wall) / 1.0s << " s" << std::endl;
return 0;
}
Demo
This gave me an output of:
cpu: 4949 1398224 1.39328 s
wall: 2.39141 s
value = 354531795
So far, so good. I then tried this on my windows box running MSYS2's g++ compiler. The output for the last program gave me:
value = 0
cpu: 15 15 0 s
wall: 0.0080039 s
std::clock() is always outputting 15? Is the compiler implementation of std::clock() broken?
Seems that I assumed that CLOCKS_PER_SEC would be the same. However, on the MSYS2 compiler, it was 1000x less then on godbolt.org.

C++//STL to OBJ Converter: How can i make my program run faster

I am creating an "STL to OBJ" format converter. The program contains a header file that reads the data from an STL file. and the main program takes that data and writes it to a new OBJ file.
everything works great but with large files the program takes so long. I know exactly which part makes the program slow and I can't find any alternative of it. It is in the part "// Create Array for the Faces" exactly in the For-Loop.
First I want to explain a bit about STL and OBJ files. In general, any 3D image in the STL format is created from a large number of triangles and each triangle has 3 vertices (each vertex has 3 points: x, y and z). But there are many repeated vertices because the triangles are connected to each other. But in the OBJ format, 2 parts are responsible for it: one is "List of Vertices" and here vertices are sorted one after another without repetition. the second part is "List of Faces" and it is the Numbers of Index of the vertices.
this is my main code:
#include "Header.h"
using namespace std;
string inputFile = "Fidgit.stl"; //Import einen STL-Datei (1.6MB)
string outputFile = "Fidgit1.obj"; //Export einen OBJ-Datei (1.1MB)
int main(int argc, char** argv)
{
auto t0 = std::chrono::system_clock::now();
std::cout << "Lesen der STL-Datei" << std::endl;
std::vector<float> coords, normals;
std::vector<unsigned int> tris, solids;
stl_reader::ReadStlFile(inputFile.c_str(), coords, normals, tris, solids);
const size_t numTris = tris.size() / 3;
std::cout << " Numbers of Triangels: " << numTris << std::endl;
auto t1 = std::chrono::system_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0);
std::cout << " duration: " << elapsed.count() << " ms" << std::endl;
std::cout << "writing OBJ-File" << std::endl;
std::ofstream fileOBJ(outputFile.c_str(), std::ios::out);
std::cout << " Erstelle Liste der Punkte" << std::endl;
fileOBJ << "# Object name:" << std::endl;
fileOBJ << outputFile << std::endl;
fileOBJ << std::endl;
fileOBJ << "# Begin list of vertices" << std::endl;
vector<string> AllVertex;
std::ifstream inFile(outputFile.c_str(), std::ios::in);
////////////////////////////////////////////////////////////////////////////
// Find Vertiecs coordinates and write into OBJ file
for (size_t itri = 0; itri < numTris; ++itri) {
for (size_t icorner = 0; icorner < 3; ++icorner) {
float* c = &coords[3 * tris[3 * itri + icorner]];
std::string VerStr = "v " + to_string(c[2]) + " " + to_string(c[1]) + " " + to_string(c[0]) ;
AllVertex.push_back(VerStr);
}
}
// here is a vertices containing the vertices coordinates read from the STL file.
// But there are many repeated vectors that we don't need in obj format,
// so they have to be removed by next step
vector <string> OldSTLVertex = AllVertex;
//Copy of STL vectors before removing the repeated vertices
// to be able to find the faces indexes
sort(AllteVertex.begin(), AllVertex.end());
auto last = unique(AllVertex.begin(), AllVertex.end());
AllVertex.erase(last, AllVertex.end());
vector <string> OBJVertex = AllVertex;
// here are the vectors without repetitions
// ready to be able to save the vector coordinates in the created obj file:
for (auto ind : OBJVertex)
{
fileOBJ << ind << endl;
}
fileOBJ << "# End list of vertices" << std::endl;
fileOBJ << std::endl;
auto t2 = std::chrono::system_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
std::cout << " duration: " << elapsed.count() << " ms" << std::endl;
//////////////////////////////////////////////////////////////////////////////
// Create Arry for the Faces
std::cout << " Create list of faces (triangles)" << std::endl;
vector <int> OBJFaces(numTris * 3);
fileOBJ << "# Begin list of faces" << std::endl;
int iCounter = 0;
int iPercent = 0;
int vcounter = 0;
// the point here is: which index in OBJVertiecs[] hat jeder vertiec in OldSTLVertex[]
for (int i = 0; i < OldSTLVertex.size(); i++) // in my example OldSTLVertex.size() have 99030 elements
{
bool bFound = false;
int vertexIndex = 0;
while (!bFound) // for (size_t vertexIndex = 0; vertexIndex < OBJVertex.size(); ++vertexIndex)
{
if (OldSTLVertex[i] == OBJVertex[vertexIndex]) // OBJVertex have 16523 elements
{
bFound = true;
OBJFaces[vcounter] = vertexIndex;
vcounter++;
}
vertexIndex++;
}
iCounter++;
if (iCounter % (OldSTLVertex.size() / 100) == 0) // every time 10% are done
{
iPercent = iPercent + 1;
auto t3 = std::chrono::system_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t2);
std::cout << " " << iPercent << "% done in " << elapsed.count() << " ms" << std::endl;
}
}
/////////////////////////////////////////////////////////////////////////////
// Write faces into OBJ file
unsigned count = 0;
for (auto ind : OBJFaces)
{
if (count++ % 3 == 0) fileOBJ << "f ";
fileOBJ << ind + 1 << " ";
if (count % 3 == 0) fileOBJ << std::endl;
}
fileOBJ << "# End list of faces" << std::endl;
fileOBJ << std::endl;
auto t4 = std::chrono::system_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(t4 - t0);
std::cout << "OBJ file written in " << elapsed.count() << " ms." << std::endl;
return 0;
}
Your current code first maps all vertices in the STL to the OBJ string format of the vertex index they reference, then uses std::unique to reduce this list, then uses an O(n) lookup on each vertex to find the original index. This is O(n*m) and is very expensive if both n and m are large.
Instead, you can do the following:
Walk the elements of tris, and for each referenced vertex idx use a std::map<std::tuple<float, float, float>, unsigned int> to deduplicate them.
If it is not present, you push the coord triple to a vector obj_coords and overwrite tris[idx] with its new index.
If it was present you simply overwrite tris[idx] with the existing index.
When rendering the vectors you can almost dump obj_coords as-is.
When rendering the faces, you simply follow the indirection in coords
So, in summary:
using Coord = std::tuple<float, float, float>;
std::map<Coord, int> coordToIndex;
std::vector<Coord> obj_coords;
for (auto &idx : tris) {
const Coord c = { coords[3*idx+0], coords[3*idx+1], coords[3*idx+2] };
if (auto it = coordToIndex.find(c); it != coordToIndex.end()) {
// We saw this vertex before
idx = it->second;
} else {
// New vertex.
obj_coords.push_back(c);
idx = obj_coords.size()-1;
coordToIndex[c] = idx; // Also create an entry in coordToIndex
}
}
Then, generating vertexes is simple: (not sure why you swapped z and x though)
for (const auto& coord : obj_coords) {
fileOBJ << "v " << std::get<2>(coord) << " " << std::get<1>(coord) << " " << std::get<0>(coord) << "\n";
}
And finally, the faces:
for (int tri = 0; tri < tris.size(); tri += 3) {
fileOBJ << "f " << tris[tri+0] << " " << tris[tri+1] << " " << tris[tri+2] << "\n"
}
You may have noticed I use "\n" instead of std::endl. This is because std::endl implies std::flush, which tries to ensure that data is written to disk. Calling this is as often as you will is wasteful.
Instead, you can just flush once manually, or trust that the destructor will do it for you:
fileOBJ << std::flush;

How to get the value of chrono c++?

I am trying to make a text game where there is a timer and once the game was finished before or in 60 seconds, there is a bonus points. However, I have no idea how can I get the value or the time from using the chrono without cout-ing it. I want to use the value for calculating the bonus point. i can cout the value through the .count() but I cannot get that value to use for the condition part.
here's my code for the scoring part:
void Game::score(auto start, auto end) {
int bonus = 0;
int total = 0;
string name;
box();
gotoxy(10,8); cout << "C O N G R A T U L A T I O N S";
gotoxy(15,10); cout << "You have successfully accomplished all the levels!";
gotoxy(15,11); cout << "You are now a certified C-O-N-N-E-C-T-o-r-I-s-T" << char(002) << char(001);
gotoxy(20,13); cout << "= = = = = = = = = = GAME STATS = = = = = = = = = =";
gotoxy(25,15); cout << "Time Taken: " << chrono::duration_cast<chrono::seconds>(end - start).count() << " seconds";
gotoxy(25,16); cout << "Points: " << pts << " points";
if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 60) {
bonus == 5000;
} else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 90) {
bonus == 3000;
} else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 120) {
bonus == 1000;
}
gotoxy(30,17); cout << "Bonus Points (Time Elapsed): " << bonus;
total = pts + bonus;
gotoxy(25,18); cout << "Total Points: " << total << " points";
gotoxy(20,20); cout << "Enter your name: ";
cin >> name;
scoreB.open("scoreboard.txt",ios::app);
scoreB << name << "\t" << total << "\n";
scoreB.close();
}
You should really use the chrono literals for comparing durations. See example here:
#include <chrono>
#include <iostream>
#include <thread>
using Clock = std::chrono::system_clock;
void compareTimes(std::chrono::time_point<Clock> startTime,
std::chrono::time_point<Clock> finishTime) {
using namespace std::chrono_literals;
std::chrono::duration<float> elapsed = finishTime - startTime;
std::cout << "elapsed = " << elapsed.count() << "\n";
if (elapsed > 10ms) {
std::cout << "over 10ms\n";
}
if (elapsed < 60s) {
std::cout << "under 60s\n";
}
}
int main() {
using namespace std::chrono_literals;
auto startTime = Clock::now();
std::this_thread::sleep_for(20ms);
auto finishTime = Clock::now();
compareTimes(startTime, finishTime);
return 0;
}
Demo: https://godbolt.org/z/hqv58acoY

Head Pose Estimation on Random Forest in G Fanelli's paper

I have been working on head pose estimation on depth data. And I have read G Fanelli's paper-"Real Time Head Pose Estimation from Consumer Depth Cameras" "Real Time Head Pose Estimation with Random Regression Forests". I test the data and the code Fanelli published on the website(http://www.vision.ee.ethz.ch/~gfanelli/head_pose/head_forest.html). However when I run the code, there is a problem. The error information is "usage: ./head_pose_estimation config_file depth_image". I think it is about file reading but I don't how to fix it.
and the code is like this:
int main(int argc, char* argv[])
{
if( argc != 3 )
{
cout << "usage: ./head_pose_estimation config_file depth_image" << endl;
exit(-1);
}
loadConfig(argv[1]);
CRForestEstimator estimator;
if( !estimator.loadForest(g_treepath.c_str(), g_ntrees) ){
cerr << "could not read forest!" << endl;
exit(-1);
}
string depth_fname(argv[2]);
//read calibration file (should be in the same directory as the depth image!)
string cal_filename = depth_fname.substr(0,depth_fname.find_last_of("/")+1);
cal_filename += "depth.cal";
ifstream is(cal_filename.c_str());
if (!is){
cerr << "depth.cal file not found in the same folder as the depth image! " << endl;
return -1;
}
//read intrinsics only
float depth_intrinsic[9]; for(int i =0; i<9; ++i) is >> depth_intrinsic[i];
is.close();
Mat depthImg;
//read depth image (compressed!)
if (!loadDepthImageCompressed( depthImg, depth_fname.c_str() ))
return -1;
Mat img3D;
img3D.create( depthImg.rows, depthImg.cols, CV_32FC3 );
//get 3D from depth
for(int y = 0; y < img3D.rows; y++)
{
Vec3f* img3Di = img3D.ptr<Vec3f>(y);
const int16_t* depthImgi = depthImg.ptr<int16_t>(y);
for(int x = 0; x < img3D.cols; x++){
float d = (float)depthImgi[x];
if ( d < g_max_z && d > 0 ){
img3Di[x][0] = d * (float(x) - depth_intrinsic[2])/depth_intrinsic[0];
img3Di[x][1] = d * (float(y) - depth_intrinsic[5])/depth_intrinsic[4];
img3Di[x][2] = d;
}
else{
img3Di[x] = 0;
}
}
}
g_means.clear();
g_votes.clear();
g_clusters.clear();
string pose_filename(depth_fname.substr(0,depth_fname.find_last_of('_')));
pose_filename += "_pose.bin";
cv::Vec<float,POSE_SIZE> gt;
bool have_gt = false;
//try to read in the ground truth from a binary file
FILE* pFile = fopen(pose_filename.c_str(), "rb");
if(pFile){
have_gt = true;
have_gt &= ( fread( &gt[0], sizeof(float),POSE_SIZE, pFile) == POSE_SIZE );
fclose(pFile);
}
//do the actual estimate
estimator.estimate( img3D,
g_means,
g_clusters,
g_votes,
g_stride,
g_maxv,
g_prob_th,
g_larger_radius_ratio,
g_smaller_radius_ratio,
false,
g_th
);
cout << "Heads found : " << g_means.size() << endl;
//assuming there's only one head in the image!
if(g_means.size()>0){
cout << "Estimated: " << g_means[0][0] << " " << g_means[0][1] << " " << g_means[0][2] << " " << g_means[0][3] << " " << g_means[0][4] << " " << g_means[0][5] <<endl;
float pt2d_est[2];
float pt2d_gt[2];
if(have_gt){
cout << "Ground T.: " << gt[0] << " " << gt[1] << " " << gt[2] << " " << gt[3] << " " << gt[4] << " " << gt[5] <<endl;
cv::Vec<float,POSE_SIZE> err = (gt-g_means[0]);
//multiply(err,err,err);
for(int n=0;n<POSE_SIZE;++n)
err[n] = err[n]*err[n];
float h_err = sqrt(err[0]+err[1]+err[2]);
float a_err = sqrt(err[3]+err[4]+err[5]);
cout << "Head error : " << h_err << " mm " << endl;
cout << "Angle error : " << a_err <<" degrees " << endl;
pt2d_gt[0] = depth_intrinsic[0]*gt[0]/gt[2] + depth_intrinsic[2];
pt2d_gt[1] = depth_intrinsic[4]*gt[1]/gt[2] + depth_intrinsic[5];
}
pt2d_est[0] = depth_intrinsic[0]*g_means[0][0]/g_means[0][2] + depth_intrinsic[2];
pt2d_est[1] = depth_intrinsic[4]*g_means[0][1]/g_means[0][2] + depth_intrinsic[5];
}
return 0;
}
can anyone could tell me how to fix the problem?Thanks so much!
You should always read the readme.txt (here attached in head_pose_estimation.tgz) before testing an application:
To run the example code, type ./head_pose_estimation config.txt
data/frame_XXXX_depth.bin. The config.txt file contains all parameters
needed for the head pose estimation, e.g., the path to the forest, the
stride, and z threshold used to segment the person from the
background.

Error using libavcodec with linux in C/C++

The problem: AVFormatContext::nb_streams has too larve value.
My C++ code (error handling, includes etc. omited to reduce the listing):
void printA(AVFormatContext* _a)
{
std::cout << "duration " << (unsigned long)_a->duration << "\n";
std::cout << "streams " << _a->nb_streams << "\n";
std::cout << "format name " << _a->iformat->name << "\n";
std::cout << "bit_rate " << _a->bit_rate << "\n";
std::cout << "long name " << _a->iformat->long_name << "\n";
}
int main(int argc, char **argv)
{
if ( argc < 2 )
{
std::cout << "Usage: " << argv[0] << " <file>\n";
return 1;
}
av_register_all();
AVFormatContext *pFormatCtx = avformat_alloc_context();
avformat_open_input (&pFormatCtx, argv[1], NULL, NULL);
avformat_find_stream_info(pFormatCtx, NULL);
printA( pFormatCtx );
return 0;
}
Running:
xx#xx /tmp/avlib $ ./avlibtest /ar/video/Nauka.Sna.2006.HDRip.AVC.mkv
[matroska,webm # 0x804c040] max_analyze_duration reached
[matroska,webm # 0x804c040] Estimating duration from bitrate, this may be inaccurate
duration 134741408
streams 134531840 <---- !!! :-O
format name matroska,webm
bit_rate 0
long name Matroska/WebM file format
But the "avplay" program works well.