Open dedoogong opened 5 years ago
Originally tensorRT engine init(deserialization) and doInference(enqueue) parts are in one function. But if I want to call the init part only one time and then call doInference part several times for different input images, I need to separate those parts. First I thought I can create the trtNet instance like
<libTRT.cpp>
#include "libTRT.h"
trtNet* foo(){
trtNet net("myEningFile.engine");
}
void bar(trtNet* net,cv::Mat img){
net->doInference(img);
}
<runTRT.cpp>
#include "libTRT.h"
main(){
void * net_handle=foo();
..do some openCV image read..
bar((trtNet*)net_handle,readImg);
}
and I saw the returned net_handle value is "NULL" !! even thogh it had some pointer address value in the foo function(that's before return, it is valid but after return, the pointer value is gone!!)
So, I modified it to declare global variable,
trtNet net("myEngine.engine");
main(){
..do some openCV image read..
bar([b]&net[/b],readImg);
}
it shows "seg fault" error while engine deserialization which runs OK in the previous cases.
I'm sorry your code is something hard to read. And I saw some efficiency problems or maybe some written mistakes on your code. As I see your bug may be that your instance is create in stack and return the object address in function stack. If you want to create an trtNet instance , try to create the factory class or function to create trtNet which the trtNet is created in heap. You can also try gdb to trace the functions.
Hello! I'm trying to make it run with other pytorch models(so, it's python). So, I made some binding interface(libtrt.cpp/libtrt.h) between C++ and python. Then, I expected that I can call your TrtNet constructor and run trt->doInference from python code. But, when I call both in one extern function like
it is called on every frames from a video file, which of course runs so slowly because of the engine init part.
So, I would like to separate those into two parts.
first, I made a init() function like ----------------------below function is in my libtrt.cpp --------------------------------
----------------------below function is in my interfaceBindingModule.cpp --------------------------------
it failed to call trtNet construnctor while engine deserealization(seg fault) even all input arguments are valid(
But it runs OK when I trtNet init part and trtNet doInference together in one function as I described firstly.
the Main problem is, the address of the trtNet instance becomes "null" after returning from Init function.
I have checked it is true clearly by printing the address of the instance(printed out like 0x123o98w in the init function, but "nil" after returned to another calling function.
So, I tried to make it "static" or "global"; that is, I wrote
but all failed with the same result( seg fault during engine deserialization).
Can anyone give me any hint for passing trtNet instance between functions from separate source code files?
Thank you!