tobibaum / metrabs_trt

Running 3D HPE at 30fps
12 stars 3 forks source link

Error when running code in cpp #3

Closed mrpositron closed 2 years ago

mrpositron commented 2 years ago

So, I modified a 'main.cpp' code bit. Basically I just removed YOLO part and detection lines from the main() function. Here is a code:

#include <iostream>
#include <cstdlib>
#include "tensorflow/c/c_api.h"

#include "../Trt.h"
#include "class_detector.h"

void NoOpDeallocator(void *data, size_t a, void *b) {}

/*
 * https://github.com/AmirulOm/tensorflow_capi_sample/blob/master/main.c
 */
class TFModelLoader
{
private:
    // batchsize!
    int NumInputs = 1;
    int NumOutputs = 1;
    TF_Graph *Graph;
    TF_Status *Status;
    TF_SessionOptions *SessionOpts;
    TF_Output *Output;
    TF_Output *Input;
    TF_Tensor **InputValues;
    TF_Tensor **OutputValues;
    TF_Session *Session;
    int ndata;
    int ndims;
    int64_t *dims;

public:
    ~TFModelLoader()
    {
        // Free memory
        TF_DeleteGraph(Graph);
        TF_DeleteSession(Session, Status);
        TF_DeleteSessionOptions(SessionOpts);
        TF_DeleteStatus(Status);
    }

    TFModelLoader(std::string base_dir, char const *feat_name, int _ndata, int _ndims, int64_t *_dims)
    {
        dims = _dims;
        ndims = _ndims;
        ndata = _ndata;
        //********* Read model
        Graph = TF_NewGraph();
        Status = TF_NewStatus();
        SessionOpts = TF_NewSessionOptions();
        TF_Buffer *RunOpts = NULL;

        auto temp = base_dir + "/metrab_head";
        const char *saved_model_dir = temp.c_str();
        std::cout << "===metrab head loaded:=== " << saved_model_dir << std::endl;
        const char *tags = "serve";

        int ntags = 1;
        Session = TF_LoadSessionFromSavedModel(SessionOpts, RunOpts, saved_model_dir, &tags, ntags, Graph, NULL, Status);

        if (TF_GetCode(Status) == TF_OK)
            printf("TF_LoadSessionFromSavedModel OK\n");
        else
            printf("%s", TF_Message(Status));

        //****** Get input tensor
        Input = (TF_Output *)malloc(sizeof(TF_Output) * NumInputs);
        TF_Output t0 = {TF_GraphOperationByName(Graph, feat_name), 0};

        if (t0.oper == NULL)
            printf("ERROR: Failed TF_GraphOperationByName serving_default_\n");
        else
            printf("TF_GraphOperationByName serving_default_ is OK\n");

        Input[0] = t0;

        //********* Get Output tensor
        Output = (TF_Output *)malloc(sizeof(TF_Output) * NumOutputs);
        TF_Output t2 = {TF_GraphOperationByName(Graph, "StatefulPartitionedCall"), 0};

        if (t2.oper == NULL)
            printf("ERROR: Failed TF_GraphOperationByName StatefulPartitionedCall\n");
        else
            printf("TF_GraphOperationByName StatefulPartitionedCall is OK\n");

        Output[0] = t2;

        //********* Allocate data for inputs & outputs
        InputValues = (TF_Tensor **)malloc(sizeof(TF_Tensor *) * NumInputs);
        OutputValues = (TF_Tensor **)malloc(sizeof(TF_Tensor *) * NumOutputs);
    }

    template <class T>
    float *run(T *data)
    {
        TF_Tensor *int_tensor;
        if (std::is_same<T, float>::value)
        {
            int_tensor = TF_NewTensor(TF_FLOAT, dims, ndims, data, ndata, &NoOpDeallocator, 0);
        }
        else
        {
            int_tensor = TF_NewTensor(TF_UINT8, dims, ndims, data, ndata, &NoOpDeallocator, 0);
        }

        if (int_tensor == NULL)
            printf("ERROR: Failed TF_NewTensor\n");

        InputValues[0] = int_tensor;

        // Run the Session
        TF_SessionRun(Session, NULL, Input, InputValues, NumInputs, Output, OutputValues, NumOutputs, NULL, 0, NULL, Status);

        if (TF_GetCode(Status) != TF_OK)
            printf("%s", TF_Message(Status));

        void *buff = TF_TensorData(OutputValues[0]);
        float *offsets = (float *)buff;
        return offsets;
    }
};
/*
 * https://github.com/zerollzeng/tiny-tensorrt.git
 */
class EffnetBBone
{
private:
    Trt *onnx_net;
    int inputBindIndex = 0;
    int outputBindIndex = 1;

    std::vector<float> convert_mat_to_fvec(cv::Mat mat)
    {
        std::vector<float> array;
        if (mat.isContinuous())
        {
            array.assign((float *)mat.data, (float *)mat.data + mat.total() * mat.channels());
        }
        else
        {
            for (int i = 0; i < mat.rows; ++i)
            {
                array.insert(array.end(), mat.ptr<float>(i), mat.ptr<float>(i) + mat.cols * mat.channels());
            }
        }
        return array;
    }

public:
    EffnetBBone(std::string base_dir)
    {
        onnx_net = new Trt();
        onnx_net->BuildEngine(base_dir + "/bbone.onnx", base_dir + "/bbone.plan");
        onnx_net->SetLogLevel((int)Severity::kINTERNAL_ERROR);
        onnx_net->SetWorkpaceSize(2 * 1024 * 1024);
    }

    std::vector<float> run(cv::Mat crop)
    {
        cv::Mat img_f32;
        crop.convertTo(img_f32, CV_32FC3);
        img_f32 = img_f32 / 256.f;
        std::vector<float> fvec = convert_mat_to_fvec(img_f32);
        std::vector<float> output(327680 / 4);

        // inference
        onnx_net->CopyFromHostToDevice(fvec, inputBindIndex);
        onnx_net->Forward();
        onnx_net->CopyFromDeviceToHost(output, outputBindIndex);
        return output;
    }
};

int main()
{
    std::string base_dir = "/workspace/vol2/metrabs_trt/mods/effnet-l";
    std::cout << "Hello, World!" << std::endl;
    EffnetBBone effnet = EffnetBBone(base_dir);

    int dim = 1280;
    int64_t dims[] = {1, 8, 8, dim};
    TFModelLoader tf_loader = TFModelLoader(base_dir, "serving_default_feature", 8 * 8 * dim * 4, 4, dims);

    return 0;
}

If I will run this code, it will show segmentation fault. However, if I will run this code commenting either one of EffnetBBone effnet = EffnetBBone(base_dir); or TFModelLoader tf_loader = TFModelLoader(base_dir, "serving_default_feature", 8 * 8 * dim * 4, 4, dims); it will perfectly. I don't understand why this code seg faults when both of those lines are present. What's more, code seg faults even before main() function.

Thanks in advance!

mrpositron commented 2 years ago

Valgrind output:

==713457== Memcheck, a memory error detector
==713457== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==713457== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==713457== Command: ./hello_tf
==713457== 
==713457== Warning: set address range perms: large range [0x4864000, 0x2dd08000) (defined)
==713457== Warning: set address range perms: large range [0x3098b000, 0x4acd2000) (defined)
==713457== Warning: set address range perms: large range [0x65422040, 0x165422040) (undefined)
==713457== Invalid read of size 8
==713457==    at 0x4842C28: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772AF5: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457==  Address 0x65422038 is 8 bytes before a block of size 4,294,967,296 alloc'd
==713457==    at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772ADC: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457== 
==713457== Invalid read of size 8
==713457==    at 0x4842C30: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772AF5: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457==  Address 0x65422030 is 16 bytes before a block of size 4,294,967,296 alloc'd
==713457==    at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772ADC: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457== 
==713457== Invalid read of size 8
==713457==    at 0x4842C3B: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772AF5: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457==  Address 0x65422028 is 24 bytes before a block of size 4,294,967,296 alloc'd
==713457==    at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772ADC: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457== 
==713457== Invalid read of size 8
==713457==    at 0x4842C1C: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772AF5: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457==  Address 0x65422020 is 32 bytes before a block of size 4,294,971,296 in arena "client"
==713457== 
==713457== 
==713457== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==713457==  Bad permissions for mapped region at address 0x63325FF8
==713457==    at 0x4842C28: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713457==    by 0x30772AF5: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E6470: google::protobuf::SimpleDescriptorDatabase::DescriptorIndex<std::pair<void const*, int> >::AddFile(google::protobuf::FileDescriptorProto const&, std::pair<void const*, int>) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307E27DF: google::protobuf::EncodedDescriptorDatabase::Add(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307ADEC3: google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int) (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x307A4DAD: protobuf_google_2fprotobuf_2fany_2eproto::AddDescriptorsImpl() (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x306774DE: __pthread_once_slow (pthread_once.c:116)
==713457==    by 0x307A5B68: ??? (in /usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0)
==713457==    by 0x4011B99: call_init.part.0 (dl-init.c:72)
==713457==    by 0x4011CA0: call_init (dl-init.c:30)
==713457==    by 0x4011CA0: _dl_init (dl-init.c:119)
==713457==    by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
==713457== 
==713457== HEAP SUMMARY:
==713457==     in use at exit: 4,301,400,345 bytes in 49,820 blocks
==713457==   total heap usage: 58,257 allocs, 8,437 frees, 4,302,177,389 bytes allocated
==713457== 
==713457== LEAK SUMMARY:
==713457==    definitely lost: 0 bytes in 0 blocks
==713457==    indirectly lost: 0 bytes in 0 blocks
==713457==      possibly lost: 288 bytes in 6 blocks
==713457==    still reachable: 4,301,400,057 bytes in 49,814 blocks
==713457==                       of which reachable via heuristic:
==713457==                         stdstring          : 1,384,974 bytes in 11,573 blocks
==713457==         suppressed: 0 bytes in 0 blocks
==713457== Rerun with --leak-check=full to see details of leaked memory
==713457== 
==713457== For lists of detected and suppressed errors, rerun with: -s
==713457== ERROR SUMMARY: 8 errors from 4 contexts (suppressed: 4 from 4)
Segmentation fault (core dumped)
mrpositron commented 2 years ago

Installing protobuf from the source solved the problem