From 3accb0d8e2e80cb82e9d37a95660785f8fe7ba9e Mon Sep 17 00:00:00 2001 From: Subtain Malik <33795294+sbmalik@users.noreply.github.com> Date: Fri, 19 Aug 2022 14:55:08 +0500 Subject: [PATCH] Arcface trt8 (#1076) * gen_weights for latest pretrained_models * tensorrt-8 migrations * updated docs --- arcface/README.md | 25 ++++++++++++-------- arcface/gen_wts.py | 4 ++-- arcface/logging.h | 9 +++++++- arcface/macros.h | 12 ++++++++++ arcface/prelu.cu | 44 +++++++++++++++++------------------ arcface/prelu.h | 57 +++++++++++++++++++++++----------------------- 6 files changed, 89 insertions(+), 62 deletions(-) create mode 100644 arcface/macros.h diff --git a/arcface/README.md b/arcface/README.md index 5cfbab2..a8235ef 100644 --- a/arcface/README.md +++ b/arcface/README.md @@ -1,8 +1,15 @@ # arcface +### TensortRT 8 The mxnet implementation is from [deepinsight/insightface.](https://github.com/deepinsight/insightface) -The pretrained models are from [LResNet50E-IR,ArcFace@ms1m-refine-v1](https://github.com/deepinsight/insightface/wiki/Model-Zoo#32-lresnet50e-irarcfacems1m-refine-v1), [LResNet100E-IR,ArcFace@ms1m-refine-v2](https://github.com/deepinsight/insightface/wiki/Model-Zoo#31-lresnet100e-irarcfacems1m-refine-v2) and [MobileFaceNet,ArcFace@ms1m-refine-v1](https://github.com/deepinsight/insightface/wiki/Model-Zoo#34-mobilefacenetarcfacems1m-refine-v1) +**Updated Pretrained Weights:** ArcFace-R100 [Insight Face Google Drive](https://drive.google.com/file/d/1Hc5zUfBATaXUgcU2haUNa7dcaZSw95h2/view) + +--- + +**Previous Pre-trained models:** The pretrained models are from [LResNet50E-IR,ArcFace@ms1m-refine-v1](https://github.com/deepinsight/insightface/wiki/Model-Zoo#32-lresnet50e-irarcfacems1m-refine-v1), [LResNet100E-IR,ArcFace@ms1m-refine-v2](https://github.com/deepinsight/insightface/wiki/Model-Zoo#31-lresnet100e-irarcfacems1m-refine-v2) and [MobileFaceNet,ArcFace@ms1m-refine-v1](https://github.com/deepinsight/insightface/wiki/Model-Zoo#34-mobilefacenetarcfacems1m-refine-v1) + +--- The two input images used in this project are joey0.ppm and joey1.ppm, download them from [Google Drive.](https://drive.google.com/drive/folders/1ctqpkRCRKyBZRCNwo9Uq4eUoMRLtFq1e). The input image is 112x112, and generated from `get_input()` in `insightface/deploy/face_model.py`, which is cropped and aligned face image. @@ -17,18 +24,18 @@ The two input images used in this project are joey0.ppm and joey1.ppm, download ## Run -1.Generate .wts file from mxnet implementation of pretrained model. The following example described how to generate arcface-r50.wts from mxnet implementation of LResNet50E-IR,ArcFace@ms1m-refine-v1. +1.Generate .wts file from mxnet implementation of pretrained model. The following example described how to generate arcface-r100.wts from mxnet implementation of LResNet100E-IR,ArcFace@ms1m-refine-v1. ``` git clone https://github.com/deepinsight/insightface cd insightface git checkout 3866cd77a6896c934b51ed39e9651b791d78bb57 cd deploy // copy tensorrtx/arcface/gen_wts.py to here(insightface/deploy) -// download model-r50-am-lfw.zip and unzip here(insightface/deploy) +// download model-r100-ii.zip and unzip here(insightface/deploy) python gen_wts.py -// a file 'arcface-r50.wts' will be generated. +// a file 'arcface-r100.wts' will be generated. // the master branch of insightface should work, if not, you can checkout 94ad870abb3203d6f31b049b70dd080dc8f33fca -// arcface-r100.wts/arcface-mobilefacenet.wts can be generated in similar way from mxnet implementation of LResNet100E-IR,ArcFace@ms1m-refine-v1/MobileFaceNet,ArcFace@ms1m-refine-v1 pretrained model. +// arcface-r50.wts/arcface-mobilefacenet.wts can be generated in similar way from mxnet implementation of LResNet50E-IR,ArcFace@ms1m-refine-v1/MobileFaceNet,ArcFace@ms1m-refine-v1 pretrained model. ``` 2.Put .wts file into tensorrtx/arcface, build and run @@ -40,13 +47,13 @@ mkdir build cd build cmake .. make -sudo ./arcface-r50 -s // serialize model to plan file i.e. 'arcface-r50.engine' -sudo ./arcface-r50 -d // deserialize plan file and run inference +sudo ./arcface-r100 -s // serialize model to plan file i.e. 'arcface-r100.engine' +sudo ./arcface-r100 -d // deserialize plan file and run inference or -sudo ./arcface-r100 -s // serialize model to plan file i.e. 'arcface-r100.engine' -sudo ./arcface-r100 -d // deserialize plan file and run inference +sudo ./arcface-r50 -s // serialize model to plan file i.e. 'arcface-r50.engine' +sudo ./arcface-r50 -d // deserialize plan file and run inference or diff --git a/arcface/gen_wts.py b/arcface/gen_wts.py index 852cde0..4955195 100644 --- a/arcface/gen_wts.py +++ b/arcface/gen_wts.py @@ -8,7 +8,7 @@ import numpy as np parser = argparse.ArgumentParser(description='face model test') # general parser.add_argument('--image-size', default='112,112', help='') -parser.add_argument('--model', default='model-r50-am-lfw/model,1', help='path to load model.') +parser.add_argument('--model', default='model-r100-ii/model,0', help='path to load model.') parser.add_argument('--ga-model', default='', help='path to load model.') parser.add_argument('--gpu', default=0, type=int, help='gpu id') parser.add_argument('--det', default=0, type=int, help='mtcnn option, 1 means using R+O, 0 means detect from begining') @@ -18,7 +18,7 @@ args = parser.parse_args() model = face_model.FaceModel(args) -f = open('arcface-r50.wts', 'w') +f = open('arcface-r100.wts', 'w') f.write('{}\n'.format(len(model.model.get_params()[0].keys()) + len(model.model.get_params()[1].keys()))) for k, v in model.model.get_params()[0].items(): vr = v.reshape(-1).asnumpy() diff --git a/arcface/logging.h b/arcface/logging.h index 602b69f..322e8a7 100644 --- a/arcface/logging.h +++ b/arcface/logging.h @@ -25,6 +25,13 @@ #include #include #include +#include "macros.h" + +#if NV_TENSORRT_MAJOR >= 8 +#define TRT_NOEXCEPT noexcept +#else +#define TRT_NOEXCEPT +#endif using Severity = nvinfer1::ILogger::Severity; @@ -236,7 +243,7 @@ public: //! Note samples should not be calling this function directly; it will eventually go away once we eliminate the //! inheritance from nvinfer1::ILogger //! - void log(Severity severity, const char* msg) override + void log(Severity severity, const char* msg) TRT_NOEXCEPT override { LogStreamConsumer(mReportableSeverity, severity) << "[TRT] " << std::string(msg) << std::endl; } diff --git a/arcface/macros.h b/arcface/macros.h new file mode 100644 index 0000000..82d74b1 --- /dev/null +++ b/arcface/macros.h @@ -0,0 +1,12 @@ +#ifndef __MACROS_H +#define __MACROS_H + +#if NV_TENSORRT_MAJOR >= 8 +#define TRT_NOEXCEPT noexcept +#define TRT_CONST_ENQUEUE const +#else +#define TRT_NOEXCEPT +#define TRT_CONST_ENQUEUE +#endif + +#endif // __MACROS_H \ No newline at end of file diff --git a/arcface/prelu.cu b/arcface/prelu.cu index 0fea0e2..4de20e7 100644 --- a/arcface/prelu.cu +++ b/arcface/prelu.cu @@ -23,7 +23,7 @@ namespace nvinfer1 gamma_.assign((float*)p, (float*)p + (length - sizeof(int)) / sizeof(float)); } - void PReluPlugin::serialize(void* buffer) const + void PReluPlugin::serialize(void* buffer) const TRT_NOEXCEPT { *reinterpret_cast(buffer) = input_size_; char *p = reinterpret_cast(buffer); @@ -31,17 +31,17 @@ namespace nvinfer1 memcpy(p, gamma_.data(), gamma_.size() * sizeof(float)); } - size_t PReluPlugin::getSerializationSize() const + size_t PReluPlugin::getSerializationSize() const TRT_NOEXCEPT { return sizeof(input_size_) + gamma_.size() * sizeof(float); } - int PReluPlugin::initialize() + int PReluPlugin::initialize() TRT_NOEXCEPT { return 0; } - Dims PReluPlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) + Dims PReluPlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) TRT_NOEXCEPT { assert(nbInputDims == 1); assert(index == 0); @@ -51,63 +51,63 @@ namespace nvinfer1 } // Set plugin namespace - void PReluPlugin::setPluginNamespace(const char* pluginNamespace) + void PReluPlugin::setPluginNamespace(const char* pluginNamespace) TRT_NOEXCEPT { mPluginNamespace = pluginNamespace; } - const char* PReluPlugin::getPluginNamespace() const + const char* PReluPlugin::getPluginNamespace() const TRT_NOEXCEPT { return mPluginNamespace; } // Return the DataType of the plugin output at the requested index - DataType PReluPlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const + DataType PReluPlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const TRT_NOEXCEPT { return DataType::kFLOAT; } // Return true if output tensor is broadcast across a batch. - bool PReluPlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const + bool PReluPlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const TRT_NOEXCEPT { return false; } // Return true if plugin can use input that is broadcast across batch without replication. - bool PReluPlugin::canBroadcastInputAcrossBatch(int inputIndex) const + bool PReluPlugin::canBroadcastInputAcrossBatch(int inputIndex) const TRT_NOEXCEPT { return false; } - void PReluPlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) + void PReluPlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) TRT_NOEXCEPT { } // Attach the plugin object to an execution context and grant the plugin the access to some context resource. - void PReluPlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) + void PReluPlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) TRT_NOEXCEPT { } // Detach the plugin object from its execution context. - void PReluPlugin::detachFromContext() {} + void PReluPlugin::detachFromContext() TRT_NOEXCEPT {} - const char* PReluPlugin::getPluginType() const + const char* PReluPlugin::getPluginType() const TRT_NOEXCEPT { return "PRelu_TRT"; } - const char* PReluPlugin::getPluginVersion() const + const char* PReluPlugin::getPluginVersion() const TRT_NOEXCEPT { return "1"; } - void PReluPlugin::destroy() + void PReluPlugin::destroy() TRT_NOEXCEPT { delete this; } // Clone the plugin - IPluginV2IOExt* PReluPlugin::clone() const + IPluginV2IOExt* PReluPlugin::clone() const TRT_NOEXCEPT { PReluPlugin *p = new PReluPlugin(gamma_); p->input_size_ = input_size_; @@ -138,7 +138,7 @@ namespace nvinfer1 assert(cudaFree(dev_gamma) == cudaSuccess); } - int PReluPlugin::enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) + int PReluPlugin::enqueue(int batchSize, const void*const * inputs, void* TRT_CONST_ENQUEUE* outputs, void* workspace, cudaStream_t stream) TRT_NOEXCEPT { //assert(batchSize == 1); //GPU @@ -158,22 +158,22 @@ namespace nvinfer1 mFC.fields = mPluginAttributes.data(); } - const char* PReluPluginCreator::getPluginName() const + const char* PReluPluginCreator::getPluginName() const TRT_NOEXCEPT { return "PRelu_TRT"; } - const char* PReluPluginCreator::getPluginVersion() const + const char* PReluPluginCreator::getPluginVersion() const TRT_NOEXCEPT { return "1"; } - const PluginFieldCollection* PReluPluginCreator::getFieldNames() + const PluginFieldCollection* PReluPluginCreator::getFieldNames() TRT_NOEXCEPT { return &mFC; } - IPluginV2IOExt* PReluPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) + IPluginV2IOExt* PReluPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) TRT_NOEXCEPT { std::vector gamma; const PluginField* fields = fc->fields; @@ -197,7 +197,7 @@ namespace nvinfer1 return obj; } - IPluginV2IOExt* PReluPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) + IPluginV2IOExt* PReluPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) TRT_NOEXCEPT { // This object will be deleted when the network is destroyed, which will // call PReluPlugin::destroy() diff --git a/arcface/prelu.h b/arcface/prelu.h index 2a6c033..86e6fcb 100644 --- a/arcface/prelu.h +++ b/arcface/prelu.h @@ -4,6 +4,7 @@ #include #include #include "NvInfer.h" +#include "macros.h" namespace nvinfer1 { @@ -15,53 +16,53 @@ namespace nvinfer1 ~PReluPlugin(); - int getNbOutputs() const override + int getNbOutputs() const TRT_NOEXCEPT override { return 1; } - Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override; + Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) TRT_NOEXCEPT override; - int initialize() override; + int initialize() TRT_NOEXCEPT override; - virtual void terminate() override {}; + virtual void terminate() TRT_NOEXCEPT override {}; - virtual size_t getWorkspaceSize(int maxBatchSize) const override { return 0;} + virtual size_t getWorkspaceSize(int maxBatchSize) const TRT_NOEXCEPT override { return 0;} - virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override; + virtual int enqueue(int batchSize, const void*const * inputs, void*TRT_CONST_ENQUEUE* outputs, void* workspace, cudaStream_t stream) TRT_NOEXCEPT override; - virtual size_t getSerializationSize() const override; + virtual size_t getSerializationSize() const TRT_NOEXCEPT override; - virtual void serialize(void* buffer) const override; + virtual void serialize(void* buffer) const TRT_NOEXCEPT override; - bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const override { + bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const TRT_NOEXCEPT override { return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT; } - const char* getPluginType() const override; + const char* getPluginType() const TRT_NOEXCEPT override; - const char* getPluginVersion() const override; + const char* getPluginVersion() const TRT_NOEXCEPT override; - void destroy() override; + void destroy() TRT_NOEXCEPT override; - IPluginV2IOExt* clone() const override; + IPluginV2IOExt* clone() const TRT_NOEXCEPT override; - void setPluginNamespace(const char* pluginNamespace) override; + void setPluginNamespace(const char* pluginNamespace) TRT_NOEXCEPT override; - const char* getPluginNamespace() const override; + const char* getPluginNamespace() const TRT_NOEXCEPT override; - DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const override; + DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const TRT_NOEXCEPT override; - bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const override; + bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const TRT_NOEXCEPT override; - bool canBroadcastInputAcrossBatch(int inputIndex) const override; + bool canBroadcastInputAcrossBatch(int inputIndex) const TRT_NOEXCEPT override; void attachToContext( - cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) override; + cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) TRT_NOEXCEPT override; - void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) override; + void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) TRT_NOEXCEPT override; - void detachFromContext() override; + void detachFromContext() TRT_NOEXCEPT override; int input_size_; private: @@ -78,22 +79,22 @@ namespace nvinfer1 ~PReluPluginCreator() override = default; - const char* getPluginName() const override; + const char* getPluginName() const TRT_NOEXCEPT override; - const char* getPluginVersion() const override; + const char* getPluginVersion() const TRT_NOEXCEPT override; - const PluginFieldCollection* getFieldNames() override; + const PluginFieldCollection* getFieldNames() TRT_NOEXCEPT override; - IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) override; + IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) TRT_NOEXCEPT override; - IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) override; + IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) TRT_NOEXCEPT override; - void setPluginNamespace(const char* libNamespace) override + void setPluginNamespace(const char* libNamespace) TRT_NOEXCEPT override { mNamespace = libNamespace; } - const char* getPluginNamespace() const override + const char* getPluginNamespace() const TRT_NOEXCEPT override { return mNamespace.c_str(); }