How to serialize std::vector variable in custom plugin?

How could I serial std::vector member in my plugin?
Here is my plugin.hpp and omit public member.

class YoloLayerV3 : public nvinfer1::IPlugin
{
...//omit
private:
    template <typename T>
    void write(char*& buffer, const T& val)
    {
        *reinterpret_cast<T*>(buffer) = val;
        buffer += sizeof(T);
    }

    template <typename T>
    void read(const char*& buffer, T& val)
    {
        val = *reinterpret_cast<const T*>(buffer);
        buffer += sizeof(T);
    }
    uint32_t m_NumBoxes;
    uint32_t m_NumClasses;
    uint32_t m_GridSize;
    uint64_t m_OutputSize;
    static int m_YoloCount;
    std::vector<uint32_t> m_masks_;//how to serialize
    std::vector<float> m_anchors_;
};

There are 2 std::vector member variable m_masks_, m_anchors_
How to serialize them? The plugin.cpp as following:

YoloLayerV3::YoloLayerV3(const void* data, size_t length)
{
    const char *d = static_cast<const char*>(data), *a = d;
    read(d, m_NumBoxes);
    read(d, m_NumClasses);
    read(d, m_GridSize);
    read(d, m_OutputSize);

    read(d, m_YoloCount);
    for(int i = 0; i < m_masks_.size(); ++i){
        read(d, m_masks_[i]);
    }
    for(int i = 0; i < m_anchors_.size(); ++i){
        read(d, m_anchors_[i]);
    }
    assert(d = a + length);
};

YoloLayerV3::YoloLayerV3(const uint32_t& numBoxes, const uint32_t& numClasses, const uint32_t& gridSize,
        std::vector<uint32_t> masks, std::vector<float> anchors ):
        m_NumBoxes(numBoxes),
        m_NumClasses(numClasses),
        m_GridSize(gridSize),
        m_masks_(masks),
        m_anchors_(anchors)
{
    assert(m_NumBoxes > 0);
    assert(m_NumClasses > 0);
    assert(m_GridSize > 0);
    assert(m_masks_.size()>0);
    assert(m_anchors_.size()>0);
    m_OutputSize = m_GridSize * m_GridSize * (m_NumBoxes * (4 + 1 + m_NumClasses));
};


size_t YoloLayerV3::getSerializationSize()
{
    return sizeof(m_NumBoxes) + sizeof(m_NumClasses)
        + sizeof(m_GridSize) + sizeof(m_OutputSize)
        + sizeof(m_YoloCount)
        + sizeof(uint32_t) * m_masks_.size()
        + sizeof(float) * m_anchors_.size();
}

void YoloLayerV3::serialize(void* buffer)
{
    char *d = static_cast<char*>(buffer), *a = d;
    write(d, m_NumBoxes);
    write(d, m_NumClasses);
    write(d, m_GridSize);
    write(d, m_OutputSize);

    write(d, m_YoloCount);
    for(int i = 0; i < m_masks_.size(); ++i){
        write(d, m_masks_[i]);
    }
    for(int i = 0; i < m_anchors_.size(); ++i){
        write(d, m_anchors_[i]);
    }
    assert(d == a + getSerializationSize());
}

Much appreciate!

Hi @291087211,
It’s a simple vector of values, so just copying the content into the serialized buffer should do the work
You just needs to report the correct serialized total size to TensorRT with getSerializationSize(), then in serialize(), put the content into it. And in deserialize, read it back.
Please refer to the below link

Thanks!

1 Like

Hi, thanks for your useful answer. And I have solved the problem .^ ^

1 Like