I am having trouble using INetwork::addPlugin(). I want to add a Reshape plugin when building faster-rcnn through API rather than caffe parser:
template<int OutC>
class Reshape : public IPlugin
{
public:
Reshape() {}
Reshape(const void* buffer, size_t size)
{
assert(size == sizeof(mCopySize));
mCopySize = *reinterpret_cast<const size_t*>(buffer);
}
int getNbOutputs() const override
{
return 1;
}
Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override
{
assert(nbInputDims == 1);
assert(index == 0);
assert(inputs[index].nbDims == 3);
assert((inputs[0].d[0])*(inputs[0].d[1]) % OutC == 0);
return DimsCHW(OutC, inputs[0].d[0] * inputs[0].d[1] / OutC, inputs[0].d[2]);
}
int initialize() override
{
return 0;
}
void terminate() override
{
}
size_t getWorkspaceSize(int) const override
{
return 0;
}
// currently it is not possible for a plugin to execute "in place". Therefore we memcpy the data from the input to the output buffer
int enqueue(int batchSize, const void*const *inputs, void** outputs, void*, cudaStream_t stream) override
{
CHECK(cudaMemcpyAsync(outputs[0], inputs[0], mCopySize * batchSize, cudaMemcpyDeviceToDevice, stream));
return 0;
}
size_t getSerializationSize() override
{
return sizeof(mCopySize);
}
void serialize(void* buffer) override
{
*reinterpret_cast<size_t*>(buffer) = mCopySize;
}
void configure(const Dims*inputs, int nbInputs, const Dims* outputs, int nbOutputs, int) override
{
mCopySize = inputs[0].d[0] * inputs[0].d[1] * inputs[0].d[2] * sizeof(float);
std::cout << sizeof(mCopySize) << std::endl;
}
protected:
size_t mCopySize;
};
ICudaEngine *
createEngine(unsigned int maxBatchSize, IBuilder *builder, DataType dt)
{
INetworkDefinition* network = builder->createNetwork();
std::map<std::string, Weights> weightMap = loadWeights(locateFile("rcnn.wts"));
// Create input of shape { 1, 3, 288, 512 } with name referenced by INPUT_BLOB_NAME
auto data = network->addInput(INPUT_BLOB_NAME, dt, DimsCHW{ INPUT_C, INPUT_H, INPUT_W});
assert(data != nullptr);
// group 1
auto conv1_1 = network->addConvolution(*data, 32, DimsHW{3, 3}, weightMap["conv1_1_weight"], weightMap["conv1_1_bias"]);
assert(conv1_1 != nullptr);
conv1_1->setPadding(DimsHW{1, 1});
conv1_1->setStride(DimsHW{1, 1});
// and more layers
// ROI Proposal
Reshape<2> PluginRshp2;
auto rpn_cls_score_reshape = network->addPlugin(reinterpret_cast<ITensor* const*>(rpn_cls_score->getOutput(0)), 1, *reinterpret_cast<IPlugin*>(&PluginRshp2));
assert(rpn_cls_score_reshape != nullptr);
}
It generates error of segfault. Could anyone help?
The user guide says there are three ways to add the plugin into a network:
- Use the INetwork::addPlugin() method when defining the network.
- Create the network via a parser.
- De-serialize the network after it has been built.
Now both samplePlugin and sampleFasterRCNN use plugin via caffe parser. It would be great to have examples of using plugin via API, i.e addPlugin( ). Thanks!