Debug Assertion when using Interpreter in TensorFlowLite - c++

I am trying to use the Mediapipe Hand tflite file using TensorFlowLite. But whenever I am getting an error when referring to the input tensor data and dimensions.\
This is the main.cpp:
#include <iostream>
#include <cstdio>
#include <vector>
#include <tensorflow/lite/interpreter.h>
#include <tensorflow/lite/kernels/register.h>
#include <tensorflow/lite/model.h>
#include <tensorflow/lite/optional_debug_tools.h>
#include <flatbuffers/flatbuffers.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <tensorflow/lite/model_builder.h>
#include <tensorflow/lite/string_util.h>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/builtin_op_data.h"
#include "tensorflow/lite/kernels/register.h"
using namespace std;
int main()
{
const char* filename = "C:/Users/naren/OneDrive/Documents/MediapipeHands/hand_landmark_full.tflite";
const char* image_filename = "C:/Users/naren/OneDrive/Documents/MediapipeHands/hand.jpg";
std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(filename);
if (model == nullptr) {
cerr << "Fail to build FlatBufferModel from file: " << filename << std::endl;
exit(1);
}
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
if (tflite::InterpreterBuilder(*model, resolver) (&interpreter) != kTfLiteOk) {
std::cerr << "Failed to build interpreter." << std::endl;
exit(1);
}
interpreter->SetNumThreads(-1);
if (interpreter->AllocateTensors() != kTfLiteOk) {
cerr << "Failed to allocate tensors." << endl;
exit(1);
}
cv::Mat image;
auto frame = cv::imread(image_filename);
if (frame.empty()) {
cout << "Image not loaded";
exit(1);
}
cv::cvtColor(frame, image, cv::COLOR_BGR2RGB);
cv::flip(image, image, 1);
cv::imshow("Image", image);
cv::waitKey(10);
const std::vector<int>& inputs = interpreter->inputs();
TfLiteTensor* inputTensor = interpreter->tensor(inputs[0]);
return 0;
}
The program is failing at the last line before the return 0 line. If we run only uptil cv::waitKey(10), then a debug assertion window pops up. I also have the cmake file, please comment if you need it.

Related

Having issues with mmap from within a function

I'm trying to write a function that has mmap within it, however when I try to access the memory from main(), it gets a segfault. Does anyone have any idea why?
Please ignore the MPI headers - it's for the later part of the project. I have commented out the mprotect line to see that it is an mmap fault as opposed to the handler not necessarily working
net_map.cpp:
#include <iostream>
#include <signal.h>
#include <sys/mman.h>
#include <mpi.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "net_map.h"
using namespace std;
void handler(int sig, siginfo_t* info, void* other)
{
void* address = info->si_addr;
cout << "\nSegfault Detected! [Address: " << address << "]\n" << endl;
int unprotectresult = mprotect(address, SIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
if (unprotectresult == 0)
{
cout << "\nSuccessful mprotect (memory unprotected)!\n" << endl;
}else
{
cout << "\nMprotect unsuccessful\n" << endl;
}
}
void netmap (char filename[], char* mapped)
{
int file;
file = open(filename, O_RDWR);
mapped = (char*) mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, file, 0);
if (mapped == MAP_FAILED)
{
cout << "\nMap Failed!" << endl;
}
cout << mapped << endl;
}
main.cpp
#include <iostream>
#include <signal.h>
#include <sys/mman.h>
#include <mpi.h>
#include <unistd.h>
#include <array>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <array>
#include "net_map.h"
using namespace std;
int main(int argc, char* argv[])
{
struct sigaction segaction;
memset(&segaction, 0, sizeof(segaction));
segaction.sa_flags = SA_SIGINFO;
sigemptyset(&segaction.sa_mask);
segaction.sa_sigaction = handler;
sigaction(SIGSEGV, &segaction, NULL);
int i;
char* mapped;
networkpage sheet;
char filename[] = "hello.txt";
netmap(filename, mapped);
sheet.address = (void*)mapped;
cout << "\nAddress: " << sheet.address << endl;
//mprotect(sheet.address, SIZE, PROT_NONE);
memcpy(sheet.address, "k", 1);
munmap(sheet.address, SIZE);
return 0;
}

how to correctly write a protobuf message in text format to a file

My question is how to correctly write a protobuf message in text format to a file. In the program below I create a NetParameter object which is a message
field in message field in caffe.proto.
When I call print() and PrintToString() they return failure.
I can print the mesaage on the console with printf("%s", data.c_str()); but notting is written to file?
#include <fcntl.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <stdint.h>
#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <vector>
#include <string>
#include "caffe.pb.h"
#include <iostream>
#include <caffe/caffe.hpp>
using namespace std;
using google::protobuf::Message;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
using google::protobuf::TextFormat;
int main()
{
caffe::NetParameter param;
const char *filename = "./test.prototxt";
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
cout << "File not found: " << filename;
google::protobuf::io::FileOutputStream* output = new google::protobuf::io::FileOutputStream(fd);
param.set_name("AlexNet");
param.add_input_dim(0);
param.add_input_dim(1);
param.add_input_dim(2);
param.set_input_dim(0, 3);
param.set_input_dim(1, 3);
param.set_input_dim(2, 3);
std::string data;
bool success = google::protobuf::TextFormat::PrintToString(param, &data);
std::cout << "bool is " << success << std::endl;
printf("%s", data.c_str());
bool success1 = google::protobuf::TextFormat::Print(param, output);
std::cout << "bool is " << success << std::endl;
close(fd);
return 0;
}

Writing a program taking LLVM IR file as a Command line argument

I am writing a main.cpp file for testing LLVM IR Command line argument .
I am using llvm version : 6.0.1
#include<iostream>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include <llvm/Support/Error.h>
#include <llvm/IRReader/IRReader.h>
using namespace llvm;
using namespace std;
static cl::opt<string> input(cl::Positional, cl::desc("Bitcode file"), cl::Required);
int main(int argc, char** argv)
{
cl::ParseCommandLineOptions(argc, argv, "LLVM IR to Bytecode \n");
LLVMContext context;
ErrorOr<std::unique_ptr<MemoryBuffer>> mb = MemoryBuffer::getFile(input);
if (error_code ec = mb.getError()) {
errs() << ec.message();
return -1;
}
ErrorOr<std::unique_ptr<Module>> m=parseBitcodeFile(mb->get()->getMemBufferRef(), context);
if (error_code ec = m.getError())
{
errs() << "Error reading bitcode: " << ec.message() << "\n";
return -1;
}
return 0;
}
I got this error :
error: ‘class llvm::Expected >’ has no member named ‘getError’
if (error_code ec = m.getError())
I google many times but I do not found answer any where. Please suggest me some solution.
The above error was due to change in LLVM API over a time. Change module reader functions to return an llvm::Expected <T> []https://reviews.llvm.org/D26562
. This class parallels ErrorOr, but replaces error_code with Error. Since Error cannot be copied, this class replaces getError() with takeError().
So above code changes to :
Expected<std::unique_ptr<Module>> m = parseBitcodeFile(mb->get()->getMemBufferRef(), context);
if (std::error_code ec = errorToErrorCode(m.takeError())) {
errs() << "Error reading bitcode: " << ec.message() << "\n";
return -1;
}

VideoWriter Error in OpenCv

#include<sstream>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cassert>
#include <cmath>
#include <fstream>
#include <time.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/types_c.h"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/video/tracking.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/calib3d/calib3d.hpp"
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
VideoCapture cap("test2.mp4");
assert(cap.isOpened());
cap>>prev;
VideoWriter outputVideo;
if (!outputVideo.isOpened())outputVideo.open("compare.mp4" , CV_FOURCC('X','V','I','D'), 24,cvSize(cur.rows, cur.cols*2+10), true);
outputVideo<< prev;
while(true) {
stringstream ss;
string name = "cropped_";
string type = ".jpg";
ss<<name<<(chk)<<type;
string filename = ss.str();
ss.str("");
imwrite(filename, canvas);
outputVideo<< canvas;
prev = cur.clone();//cur.copyTo(prev);
prev_ref = cur_ref.clone();//cur.copyTo(prev);
cur_grey.copyTo(prev_grey);
cout << "Frame: " << k << "/" << max_frames << " - good optical flow: " << prev_corner2.size() << endl;
k++;
}
Question:It is a small description of my code.Now when I try to write my canvas(variable of type Mat in OpenCV) for creating a video.It's giving an error.Even if I try for .avi file or .mp4 file it doesn't write/create a video.Please help me out.It shows an error "[mp4 # 0xa0aac0] Tag XVID/0x44495658 incompatible with output codec id '13' error ".I am using ubuntu 14.04 with intel core processor-i5

Creating a sound manager in sdl

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <iostream>
#include "Constants.h"
#include "Texture2D.h"
#include "GameScreenManager.h"
#include "Audio.h"
using namespace::std;
Audio::Audio(string paths)
{
gMusic = NULL;
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
cout << "Mixer could not initialise. error: " << Mix_GetError();
}
LoadMusic(paths);
Update();
}
Audio::~Audio()
{
Mix_FreeMusic(gMusic);
gMusic = NULL;
}
void Audio::LoadMusic(string path)
{
gMusic = Mix_LoadMUS(path.c_str());
if(gMusic == NULL)
{
cout << "Failed to load background music! Error: " << Mix_GetError() << endl;
}
}
bool Audio::Update()
{
if(Mix_PlayingMusic() == 0)
{
Mix_PlayMusic(gMusic, -1);
}
return false;
}
this is my sound manager so far. I call it in the source with Audio::Audio("Music/bubble-bobble.mp3"); yet it doesn't play anything. I think something is wrong with the update as the "string path" is being sent through and things. Anyone have any ideas?