From 8f661b4b15b12f74002e29ecbe85067d542541e4 Mon Sep 17 00:00:00 2001 From: wang-xinyu Date: Sat, 23 May 2020 00:05:45 +0800 Subject: [PATCH] yolov4 migrated to trt7 --- README.md | 21 +- .../README.md => tutorials/getting_started.md | 0 tutorials/migrating_from_tensorrt_4_to_7.md | 13 + yolov4/CMakeLists.txt | 4 +- yolov4/common.h | 356 ------------- yolov4/logging.h | 503 ++++++++++++++++++ yolov4/mish.cu | 126 ++++- yolov4/mish.h | 107 +++- yolov4/plugin_factory.cpp | 20 - yolov4/plugin_factory.h | 12 - yolov4/yololayer.cu | 112 +++- yolov4/yololayer.h | 119 +++-- yolov4/yolov4.cpp | 147 ++--- 13 files changed, 1004 insertions(+), 536 deletions(-) rename getting_started/README.md => tutorials/getting_started.md (100%) create mode 100644 tutorials/migrating_from_tensorrt_4_to_7.md delete mode 100644 yolov4/common.h create mode 100644 yolov4/logging.h delete mode 100644 yolov4/plugin_factory.cpp delete mode 100644 yolov4/plugin_factory.h diff --git a/README.md b/README.md index bfbcb4e..9cee18c 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,18 @@ I wrote this project to get familiar with tensorrt API, and also to share and le All the models are implemented in pytorch first, and export a weights file xxx.wts, and then use tensorrt to load weights, define network and do inference. Some pytorch implementations can be found in my repo [Pytorchx](https://github.com/wang-xinyu/pytorchx), the remaining are from polular open-source pytorch implementations. -## Getting Started +# News -There is a guide for quickly getting started, taking lenet5 as a demo. [Getting_Started.](./getting_started) +- `22 May 2020`. A new branch [trt4](https://github.com/wang-xinyu/tensorrtx/tree/trt4) created, which is using TensorRT 4 API. Now the master branch is using TensorRT 7 API. But only `yolov4` has been migrated to TensorRT 7 API for now. The rest will be migrated soon. And a tutorial for `migarating from TensorRT 4 to 7` provided. + +## Tutorials + +- [A guide for quickly getting started, taking lenet5 as a demo.](./tutorials/getting_started.md) +- [Migrating from TensorRT 4 to 7](./tutorials/migrating_from_tensorrt_4_to_7.md) ## Test Environment -1. Jetson TX1 / Ubuntu16.04 / cuda9.0 / cudnn7.1.5 / tensorrt4.0.2 / nvinfer4.1.3 / opencv3.3 - -2. GTX1080 / Ubuntu16.04 / cuda10.0 / cudnn7.6.5 / tensorrt7.0.0 / nvinfer7.0.0 / opencv3.3 - -Currently, TX1/TX2 and x86 GTX1080 were tested. trt4 api were using, some APIs are deprecated in trt7, but still can compile successfully. +1. GTX1080 / Ubuntu16.04 / cuda10.0 / cudnn7.6.5 / tensorrt7.0.0 / nvinfer7.0.0 / opencv3.3 ## How to run @@ -74,9 +75,9 @@ Some tricky operations encountered in these models, already solved, but might ha |-|-|:-:|:-:|:-:|:-:| | YOLOv3(darknet53) | Xavier | 1 | FP16 | 320x320 | 55 | | YOLOv3-spp(darknet53) | Xeon E5-2620/GTX1080 | 1 | FP32 | 256x416 | 94 | -| YOLOv4(CSPDarknet53) | Xeon E5-2620/GTX1080 | 1 | FP32 | 256x416 | 59 | -| YOLOv4(CSPDarknet53) | Xeon E5-2620/GTX1080 | 4 | FP32 | 256x416 | 74 | -| YOLOv4(CSPDarknet53) | Xeon E5-2620/GTX1080 | 8 | FP32 | 256x416 | 83 | +| YOLOv4(CSPDarknet53) | Xeon E5-2620/GTX1080 | 1 | FP16 | 608x608 | 35.7 | +| YOLOv4(CSPDarknet53) | Xeon E5-2620/GTX1080 | 4 | FP16 | 608x608 | 40.9 | +| YOLOv4(CSPDarknet53) | Xeon E5-2620/GTX1080 | 8 | FP16 | 608x608 | 41.3 | | RetinaFace(resnet50) | TX2 | 1 | FP16 | 384x640 | 15 | | RetinaFace(resnet50) | Xeon E5-2620/GTX1080 | 1 | FP32 | 928x1600 | 15 | diff --git a/getting_started/README.md b/tutorials/getting_started.md similarity index 100% rename from getting_started/README.md rename to tutorials/getting_started.md diff --git a/tutorials/migrating_from_tensorrt_4_to_7.md b/tutorials/migrating_from_tensorrt_4_to_7.md new file mode 100644 index 0000000..cebc2a0 --- /dev/null +++ b/tutorials/migrating_from_tensorrt_4_to_7.md @@ -0,0 +1,13 @@ +# Migrating from TensorRT 4 to 7 + +The following APIs are deprecated and replaced in TensorRT 7. + +- `DimsCHW`, replaced by `Dims3` +- `addConvolution()`, replaced by `addConvolutionNd()` +- `addPooling()`, replaced by `addPoolingNd()` +- `addDeconvolution()`, replaced by `addDeconvolutionNd()` +- `createNetwork()`, replaced by `createNetworkV2()` +- `buildCudaEngine()`, replaced by `buildEngineWithConfig()` +- `createPReLUPlugin()`, replaced by `addActivation()` with `ActivationType::kLEAKY_RELU` +- `IPlugin` and `IPluginExt` class, replaced by `IPluginV2IOExt` or `IPluginV2DynamicExt` +- Use the new `Logger` class defined in logging.h diff --git a/yolov4/CMakeLists.txt b/yolov4/CMakeLists.txt index 9e1a829..5901e60 100644 --- a/yolov4/CMakeLists.txt +++ b/yolov4/CMakeLists.txt @@ -31,8 +31,8 @@ cuda_add_library(myplugins SHARED ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_S find_package(OpenCV) include_directories(OpenCV_INCLUDE_DIRS) -add_executable(yolov4 ${PROJECT_SOURCE_DIR}/plugin_factory.cpp ${PROJECT_SOURCE_DIR}/yolov4.cpp) -target_link_libraries(yolov4 nvinfer nvinfer_plugin) +add_executable(yolov4 ${PROJECT_SOURCE_DIR}/yolov4.cpp) +target_link_libraries(yolov4 nvinfer) target_link_libraries(yolov4 cudart) target_link_libraries(yolov4 myplugins) target_link_libraries(yolov4 ${OpenCV_LIBS}) diff --git a/yolov4/common.h b/yolov4/common.h deleted file mode 100644 index 3b9c30c..0000000 --- a/yolov4/common.h +++ /dev/null @@ -1,356 +0,0 @@ -#ifndef _TRT_COMMON_H_ -#define _TRT_COMMON_H_ -#include "NvInfer.h" -#include "NvOnnxConfig.h" -#include "NvOnnxParser.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -#define CHECK(status) \ - do \ - { \ - auto ret = (status); \ - if (ret != 0) \ - { \ - std::cout << "Cuda failure: " << ret; \ - abort(); \ - } \ - } while (0) - -constexpr long double operator"" _GB(long double val) { return val * (1 << 30); } -constexpr long double operator"" _MB(long double val) { return val * (1 << 20); } -constexpr long double operator"" _KB(long double val) { return val * (1 << 10); } - -// These is necessary if we want to be able to write 1_GB instead of 1.0_GB. -// Since the return type is signed, -1_GB will work as expected. -constexpr long long int operator"" _GB(long long unsigned int val) { return val * (1 << 30); } -constexpr long long int operator"" _MB(long long unsigned int val) { return val * (1 << 20); } -constexpr long long int operator"" _KB(long long unsigned int val) { return val * (1 << 10); } - -// Logger for TensorRT info/warning/errors -class Logger : public nvinfer1::ILogger -{ -public: - - Logger(): Logger(Severity::kWARNING) {} - - Logger(Severity severity): reportableSeverity(severity) {} - - void log(Severity severity, const char* msg) override - { - // suppress messages with severity enum value greater than the reportable - if (severity > reportableSeverity) return; - - switch (severity) - { - case Severity::kINTERNAL_ERROR: std::cerr << "INTERNAL_ERROR: "; break; - case Severity::kERROR: std::cerr << "ERROR: "; break; - case Severity::kWARNING: std::cerr << "WARNING: "; break; - case Severity::kINFO: std::cerr << "INFO: "; break; - default: std::cerr << "UNKNOWN: "; break; - } - std::cerr << msg << std::endl; - } - - Severity reportableSeverity{Severity::kWARNING}; -}; - -// Locate path to file, given its filename or filepath suffix and possible dirs it might lie in -// Function will also walk back MAX_DEPTH dirs from CWD to check for such a file path -inline std::string locateFile(const std::string& filepathSuffix, const std::vector& directories) -{ - const int MAX_DEPTH{10}; - bool found{false}; - std::string filepath; - - for (auto& dir : directories) - { - filepath = dir + filepathSuffix; - - for (int i = 0; i < MAX_DEPTH && !found; i++) - { - std::ifstream checkFile(filepath); - found = checkFile.is_open(); - if (found) break; - filepath = "../" + filepath; // Try again in parent dir - } - - if (found) - { - break; - } - - filepath.clear(); - } - - if (filepath.empty()) { - std::string directoryList = std::accumulate(directories.begin() + 1, directories.end(), directories.front(), - [](const std::string& a, const std::string& b) { return a + "\n\t" + b; }); - throw std::runtime_error("Could not find " + filepathSuffix + " in data directories:\n\t" + directoryList); - } - return filepath; -} - -inline void readPGMFile(const std::string& fileName, uint8_t* buffer, int inH, int inW) -{ - std::ifstream infile(fileName, std::ifstream::binary); - assert(infile.is_open() && "Attempting to read from a file that is not open."); - std::string magic, h, w, max; - infile >> magic >> h >> w >> max; - infile.seekg(1, infile.cur); - infile.read(reinterpret_cast(buffer), inH * inW); -} - -namespace samples_common -{ - -inline void* safeCudaMalloc(size_t memSize) -{ - void* deviceMem; - CHECK(cudaMalloc(&deviceMem, memSize)); - if (deviceMem == nullptr) - { - std::cerr << "Out of memory" << std::endl; - exit(1); - } - return deviceMem; -} - -inline bool isDebug() -{ - return (std::getenv("TENSORRT_DEBUG") ? true : false); -} - -struct InferDeleter -{ - template - void operator()(T* obj) const - { - if (obj) { - obj->destroy(); - } - } -}; - -template -inline std::shared_ptr infer_object(T* obj) -{ - if (!obj) { - throw std::runtime_error("Failed to create object"); - } - return std::shared_ptr(obj, InferDeleter()); -} - -template -inline std::vector argsort(Iter begin, Iter end, bool reverse = false) -{ - std::vector inds(end - begin); - std::iota(inds.begin(), inds.end(), 0); - if (reverse) { - std::sort(inds.begin(), inds.end(), [&begin](size_t i1, size_t i2) { - return begin[i2] < begin[i1]; - }); - } - else - { - std::sort(inds.begin(), inds.end(), [&begin](size_t i1, size_t i2) { - return begin[i1] < begin[i2]; - }); - } - return inds; -} - -inline bool readReferenceFile(const std::string& fileName, std::vector& refVector) -{ - std::ifstream infile(fileName); - if (!infile.is_open()) { - cout << "ERROR: readReferenceFile: Attempting to read from a file that is not open." << endl; - return false; - } - std::string line; - while (std::getline(infile, line)) { - if (line.empty()) continue; - refVector.push_back(line); - } - infile.close(); - return true; -} - -template -inline std::vector classify(const vector& refVector, const result_vector_t& output, const size_t topK) -{ - auto inds = samples_common::argsort(output.cbegin(), output.cend(), true); - std::vector result; - for (size_t k = 0; k < topK; ++k) { - result.push_back(refVector[inds[k]]); - } - return result; -} - -//...LG returns top K indices, not values. -template -inline vector topK(const vector inp, const size_t k) -{ - vector result; - std::vector inds = samples_common::argsort(inp.cbegin(), inp.cend(), true); - result.assign(inds.begin(), inds.begin()+k); - return result; -} - -template -inline bool readASCIIFile(const string& fileName, const size_t size, vector& out) -{ - std::ifstream infile(fileName); - if (!infile.is_open()) { - cout << "ERROR readASCIIFile: Attempting to read from a file that is not open." << endl; - return false; - } - out.clear(); - out.reserve(size); - out.assign(std::istream_iterator(infile), std::istream_iterator()); - infile.close(); - return true; -} - -template -inline bool writeASCIIFile(const string& fileName, const vector& in) -{ - std::ofstream outfile(fileName); - if (!outfile.is_open()) { - cout << "ERROR: writeASCIIFile: Attempting to write to a file that is not open." << endl; - return false; - } - for (auto fn : in) { - outfile << fn << " "; - } - outfile.close(); - return true; -} - -inline void print_version() -{ -//... This can be only done after statically linking this support into parserONNX.library -#if 0 - std::cout << "Parser built against:" << std::endl; - std::cout << " ONNX IR version: " << nvonnxparser::onnx_ir_version_string(onnx::IR_VERSION) << std::endl; -#endif - std::cout << " TensorRT version: " - << NV_TENSORRT_MAJOR << "." - << NV_TENSORRT_MINOR << "." - << NV_TENSORRT_PATCH << "." - << NV_TENSORRT_BUILD << std::endl; -} - -inline string getFileType(const string& filepath) -{ - return filepath.substr(filepath.find_last_of(".") + 1); -} - -inline string toLower(const string& inp) -{ - string out = inp; - std::transform(out.begin(), out.end(), out.begin(), ::tolower); - return out; -} - -inline unsigned int getElementSize(nvinfer1::DataType t) -{ - switch (t) - { - case nvinfer1::DataType::kINT32: return 4; - case nvinfer1::DataType::kFLOAT: return 4; - case nvinfer1::DataType::kHALF: return 2; - case nvinfer1::DataType::kINT8: return 1; - } - throw std::runtime_error("Invalid DataType."); - return 0; -} - -inline int64_t volume(const nvinfer1::Dims& d) -{ - return std::accumulate(d.d, d.d + d.nbDims, 1, std::multiplies()); -} - -// Struct to maintain command-line arguments. -struct Args -{ - bool runInInt8 = false; -}; - -// Populates the Args struct with the provided command-line parameters. -inline void parseArgs(Args& args, int argc, char* argv[]) -{ - if (argc >= 1) - { - for (int i = 1; i < argc; ++i) - { - if (!strcmp(argv[i], "--int8")) args.runInInt8 = true; - } - } -} - -template -struct PPM -{ - std::string magic, fileName; - int h, w, max; - uint8_t buffer[C * H * W]; -}; - -struct BBox -{ - float x1, y1, x2, y2; -}; - -template -inline void writePPMFileWithBBox(const std::string& filename, PPM& ppm, const BBox& bbox) -{ - std::ofstream outfile("./" + filename, std::ofstream::binary); - assert(!outfile.fail()); - outfile << "P6" << "\n" << ppm.w << " " << ppm.h << "\n" << ppm.max << "\n"; - auto round = [](float x) -> int { return int(std::floor(x + 0.5f)); }; - const int x1 = std::min(std::max(0, round(int(bbox.x1))), W - 1); - const int x2 = std::min(std::max(0, round(int(bbox.x2))), W - 1); - const int y1 = std::min(std::max(0, round(int(bbox.y1))), H - 1); - const int y2 = std::min(std::max(0, round(int(bbox.y2))), H - 1); - for (int x = x1; x <= x2; ++x) - { - // bbox top border - ppm.buffer[(y1 * ppm.w + x) * 3] = 255; - ppm.buffer[(y1 * ppm.w + x) * 3 + 1] = 0; - ppm.buffer[(y1 * ppm.w + x) * 3 + 2] = 0; - // bbox bottom border - ppm.buffer[(y2 * ppm.w + x) * 3] = 255; - ppm.buffer[(y2 * ppm.w + x) * 3 + 1] = 0; - ppm.buffer[(y2 * ppm.w + x) * 3 + 2] = 0; - } - for (int y = y1; y <= y2; ++y) - { - // bbox left border - ppm.buffer[(y * ppm.w + x1) * 3] = 255; - ppm.buffer[(y * ppm.w + x1) * 3 + 1] = 0; - ppm.buffer[(y * ppm.w + x1) * 3 + 2] = 0; - // bbox right border - ppm.buffer[(y * ppm.w + x2) * 3] = 255; - ppm.buffer[(y * ppm.w + x2) * 3 + 1] = 0; - ppm.buffer[(y * ppm.w + x2) * 3 + 2] = 0; - } - outfile.write(reinterpret_cast(ppm.buffer), ppm.w * ppm.h * 3); -} - -} // namespace samples_common - -#endif // _TRT_COMMON_H_ diff --git a/yolov4/logging.h b/yolov4/logging.h new file mode 100644 index 0000000..602b69f --- /dev/null +++ b/yolov4/logging.h @@ -0,0 +1,503 @@ +/* + * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TENSORRT_LOGGING_H +#define TENSORRT_LOGGING_H + +#include "NvInferRuntimeCommon.h" +#include +#include +#include +#include +#include +#include +#include + +using Severity = nvinfer1::ILogger::Severity; + +class LogStreamConsumerBuffer : public std::stringbuf +{ +public: + LogStreamConsumerBuffer(std::ostream& stream, const std::string& prefix, bool shouldLog) + : mOutput(stream) + , mPrefix(prefix) + , mShouldLog(shouldLog) + { + } + + LogStreamConsumerBuffer(LogStreamConsumerBuffer&& other) + : mOutput(other.mOutput) + { + } + + ~LogStreamConsumerBuffer() + { + // std::streambuf::pbase() gives a pointer to the beginning of the buffered part of the output sequence + // std::streambuf::pptr() gives a pointer to the current position of the output sequence + // if the pointer to the beginning is not equal to the pointer to the current position, + // call putOutput() to log the output to the stream + if (pbase() != pptr()) + { + putOutput(); + } + } + + // synchronizes the stream buffer and returns 0 on success + // synchronizing the stream buffer consists of inserting the buffer contents into the stream, + // resetting the buffer and flushing the stream + virtual int sync() + { + putOutput(); + return 0; + } + + void putOutput() + { + if (mShouldLog) + { + // prepend timestamp + std::time_t timestamp = std::time(nullptr); + tm* tm_local = std::localtime(×tamp); + std::cout << "["; + std::cout << std::setw(2) << std::setfill('0') << 1 + tm_local->tm_mon << "/"; + std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_mday << "/"; + std::cout << std::setw(4) << std::setfill('0') << 1900 + tm_local->tm_year << "-"; + std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_hour << ":"; + std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_min << ":"; + std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_sec << "] "; + // std::stringbuf::str() gets the string contents of the buffer + // insert the buffer contents pre-appended by the appropriate prefix into the stream + mOutput << mPrefix << str(); + // set the buffer to empty + str(""); + // flush the stream + mOutput.flush(); + } + } + + void setShouldLog(bool shouldLog) + { + mShouldLog = shouldLog; + } + +private: + std::ostream& mOutput; + std::string mPrefix; + bool mShouldLog; +}; + +//! +//! \class LogStreamConsumerBase +//! \brief Convenience object used to initialize LogStreamConsumerBuffer before std::ostream in LogStreamConsumer +//! +class LogStreamConsumerBase +{ +public: + LogStreamConsumerBase(std::ostream& stream, const std::string& prefix, bool shouldLog) + : mBuffer(stream, prefix, shouldLog) + { + } + +protected: + LogStreamConsumerBuffer mBuffer; +}; + +//! +//! \class LogStreamConsumer +//! \brief Convenience object used to facilitate use of C++ stream syntax when logging messages. +//! Order of base classes is LogStreamConsumerBase and then std::ostream. +//! This is because the LogStreamConsumerBase class is used to initialize the LogStreamConsumerBuffer member field +//! in LogStreamConsumer and then the address of the buffer is passed to std::ostream. +//! This is necessary to prevent the address of an uninitialized buffer from being passed to std::ostream. +//! Please do not change the order of the parent classes. +//! +class LogStreamConsumer : protected LogStreamConsumerBase, public std::ostream +{ +public: + //! \brief Creates a LogStreamConsumer which logs messages with level severity. + //! Reportable severity determines if the messages are severe enough to be logged. + LogStreamConsumer(Severity reportableSeverity, Severity severity) + : LogStreamConsumerBase(severityOstream(severity), severityPrefix(severity), severity <= reportableSeverity) + , std::ostream(&mBuffer) // links the stream buffer with the stream + , mShouldLog(severity <= reportableSeverity) + , mSeverity(severity) + { + } + + LogStreamConsumer(LogStreamConsumer&& other) + : LogStreamConsumerBase(severityOstream(other.mSeverity), severityPrefix(other.mSeverity), other.mShouldLog) + , std::ostream(&mBuffer) // links the stream buffer with the stream + , mShouldLog(other.mShouldLog) + , mSeverity(other.mSeverity) + { + } + + void setReportableSeverity(Severity reportableSeverity) + { + mShouldLog = mSeverity <= reportableSeverity; + mBuffer.setShouldLog(mShouldLog); + } + +private: + static std::ostream& severityOstream(Severity severity) + { + return severity >= Severity::kINFO ? std::cout : std::cerr; + } + + static std::string severityPrefix(Severity severity) + { + switch (severity) + { + case Severity::kINTERNAL_ERROR: return "[F] "; + case Severity::kERROR: return "[E] "; + case Severity::kWARNING: return "[W] "; + case Severity::kINFO: return "[I] "; + case Severity::kVERBOSE: return "[V] "; + default: assert(0); return ""; + } + } + + bool mShouldLog; + Severity mSeverity; +}; + +//! \class Logger +//! +//! \brief Class which manages logging of TensorRT tools and samples +//! +//! \details This class provides a common interface for TensorRT tools and samples to log information to the console, +//! and supports logging two types of messages: +//! +//! - Debugging messages with an associated severity (info, warning, error, or internal error/fatal) +//! - Test pass/fail messages +//! +//! The advantage of having all samples use this class for logging as opposed to emitting directly to stdout/stderr is +//! that the logic for controlling the verbosity and formatting of sample output is centralized in one location. +//! +//! In the future, this class could be extended to support dumping test results to a file in some standard format +//! (for example, JUnit XML), and providing additional metadata (e.g. timing the duration of a test run). +//! +//! TODO: For backwards compatibility with existing samples, this class inherits directly from the nvinfer1::ILogger +//! interface, which is problematic since there isn't a clean separation between messages coming from the TensorRT +//! library and messages coming from the sample. +//! +//! In the future (once all samples are updated to use Logger::getTRTLogger() to access the ILogger) we can refactor the +//! class to eliminate the inheritance and instead make the nvinfer1::ILogger implementation a member of the Logger +//! object. + +class Logger : public nvinfer1::ILogger +{ +public: + Logger(Severity severity = Severity::kWARNING) + : mReportableSeverity(severity) + { + } + + //! + //! \enum TestResult + //! \brief Represents the state of a given test + //! + enum class TestResult + { + kRUNNING, //!< The test is running + kPASSED, //!< The test passed + kFAILED, //!< The test failed + kWAIVED //!< The test was waived + }; + + //! + //! \brief Forward-compatible method for retrieving the nvinfer::ILogger associated with this Logger + //! \return The nvinfer1::ILogger associated with this Logger + //! + //! TODO Once all samples are updated to use this method to register the logger with TensorRT, + //! we can eliminate the inheritance of Logger from ILogger + //! + nvinfer1::ILogger& getTRTLogger() + { + return *this; + } + + //! + //! \brief Implementation of the nvinfer1::ILogger::log() virtual method + //! + //! 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 + { + LogStreamConsumer(mReportableSeverity, severity) << "[TRT] " << std::string(msg) << std::endl; + } + + //! + //! \brief Method for controlling the verbosity of logging output + //! + //! \param severity The logger will only emit messages that have severity of this level or higher. + //! + void setReportableSeverity(Severity severity) + { + mReportableSeverity = severity; + } + + //! + //! \brief Opaque handle that holds logging information for a particular test + //! + //! This object is an opaque handle to information used by the Logger to print test results. + //! The sample must call Logger::defineTest() in order to obtain a TestAtom that can be used + //! with Logger::reportTest{Start,End}(). + //! + class TestAtom + { + public: + TestAtom(TestAtom&&) = default; + + private: + friend class Logger; + + TestAtom(bool started, const std::string& name, const std::string& cmdline) + : mStarted(started) + , mName(name) + , mCmdline(cmdline) + { + } + + bool mStarted; + std::string mName; + std::string mCmdline; + }; + + //! + //! \brief Define a test for logging + //! + //! \param[in] name The name of the test. This should be a string starting with + //! "TensorRT" and containing dot-separated strings containing + //! the characters [A-Za-z0-9_]. + //! For example, "TensorRT.sample_googlenet" + //! \param[in] cmdline The command line used to reproduce the test + // + //! \return a TestAtom that can be used in Logger::reportTest{Start,End}(). + //! + static TestAtom defineTest(const std::string& name, const std::string& cmdline) + { + return TestAtom(false, name, cmdline); + } + + //! + //! \brief A convenience overloaded version of defineTest() that accepts an array of command-line arguments + //! as input + //! + //! \param[in] name The name of the test + //! \param[in] argc The number of command-line arguments + //! \param[in] argv The array of command-line arguments (given as C strings) + //! + //! \return a TestAtom that can be used in Logger::reportTest{Start,End}(). + static TestAtom defineTest(const std::string& name, int argc, char const* const* argv) + { + auto cmdline = genCmdlineString(argc, argv); + return defineTest(name, cmdline); + } + + //! + //! \brief Report that a test has started. + //! + //! \pre reportTestStart() has not been called yet for the given testAtom + //! + //! \param[in] testAtom The handle to the test that has started + //! + static void reportTestStart(TestAtom& testAtom) + { + reportTestResult(testAtom, TestResult::kRUNNING); + assert(!testAtom.mStarted); + testAtom.mStarted = true; + } + + //! + //! \brief Report that a test has ended. + //! + //! \pre reportTestStart() has been called for the given testAtom + //! + //! \param[in] testAtom The handle to the test that has ended + //! \param[in] result The result of the test. Should be one of TestResult::kPASSED, + //! TestResult::kFAILED, TestResult::kWAIVED + //! + static void reportTestEnd(const TestAtom& testAtom, TestResult result) + { + assert(result != TestResult::kRUNNING); + assert(testAtom.mStarted); + reportTestResult(testAtom, result); + } + + static int reportPass(const TestAtom& testAtom) + { + reportTestEnd(testAtom, TestResult::kPASSED); + return EXIT_SUCCESS; + } + + static int reportFail(const TestAtom& testAtom) + { + reportTestEnd(testAtom, TestResult::kFAILED); + return EXIT_FAILURE; + } + + static int reportWaive(const TestAtom& testAtom) + { + reportTestEnd(testAtom, TestResult::kWAIVED); + return EXIT_SUCCESS; + } + + static int reportTest(const TestAtom& testAtom, bool pass) + { + return pass ? reportPass(testAtom) : reportFail(testAtom); + } + + Severity getReportableSeverity() const + { + return mReportableSeverity; + } + +private: + //! + //! \brief returns an appropriate string for prefixing a log message with the given severity + //! + static const char* severityPrefix(Severity severity) + { + switch (severity) + { + case Severity::kINTERNAL_ERROR: return "[F] "; + case Severity::kERROR: return "[E] "; + case Severity::kWARNING: return "[W] "; + case Severity::kINFO: return "[I] "; + case Severity::kVERBOSE: return "[V] "; + default: assert(0); return ""; + } + } + + //! + //! \brief returns an appropriate string for prefixing a test result message with the given result + //! + static const char* testResultString(TestResult result) + { + switch (result) + { + case TestResult::kRUNNING: return "RUNNING"; + case TestResult::kPASSED: return "PASSED"; + case TestResult::kFAILED: return "FAILED"; + case TestResult::kWAIVED: return "WAIVED"; + default: assert(0); return ""; + } + } + + //! + //! \brief returns an appropriate output stream (cout or cerr) to use with the given severity + //! + static std::ostream& severityOstream(Severity severity) + { + return severity >= Severity::kINFO ? std::cout : std::cerr; + } + + //! + //! \brief method that implements logging test results + //! + static void reportTestResult(const TestAtom& testAtom, TestResult result) + { + severityOstream(Severity::kINFO) << "&&&& " << testResultString(result) << " " << testAtom.mName << " # " + << testAtom.mCmdline << std::endl; + } + + //! + //! \brief generate a command line string from the given (argc, argv) values + //! + static std::string genCmdlineString(int argc, char const* const* argv) + { + std::stringstream ss; + for (int i = 0; i < argc; i++) + { + if (i > 0) + ss << " "; + ss << argv[i]; + } + return ss.str(); + } + + Severity mReportableSeverity; +}; + +namespace +{ + +//! +//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kVERBOSE +//! +//! Example usage: +//! +//! LOG_VERBOSE(logger) << "hello world" << std::endl; +//! +inline LogStreamConsumer LOG_VERBOSE(const Logger& logger) +{ + return LogStreamConsumer(logger.getReportableSeverity(), Severity::kVERBOSE); +} + +//! +//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kINFO +//! +//! Example usage: +//! +//! LOG_INFO(logger) << "hello world" << std::endl; +//! +inline LogStreamConsumer LOG_INFO(const Logger& logger) +{ + return LogStreamConsumer(logger.getReportableSeverity(), Severity::kINFO); +} + +//! +//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kWARNING +//! +//! Example usage: +//! +//! LOG_WARN(logger) << "hello world" << std::endl; +//! +inline LogStreamConsumer LOG_WARN(const Logger& logger) +{ + return LogStreamConsumer(logger.getReportableSeverity(), Severity::kWARNING); +} + +//! +//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kERROR +//! +//! Example usage: +//! +//! LOG_ERROR(logger) << "hello world" << std::endl; +//! +inline LogStreamConsumer LOG_ERROR(const Logger& logger) +{ + return LogStreamConsumer(logger.getReportableSeverity(), Severity::kERROR); +} + +//! +//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kINTERNAL_ERROR +// ("fatal" severity) +//! +//! Example usage: +//! +//! LOG_FATAL(logger) << "hello world" << std::endl; +//! +inline LogStreamConsumer LOG_FATAL(const Logger& logger) +{ + return LogStreamConsumer(logger.getReportableSeverity(), Severity::kINTERNAL_ERROR); +} + +} // anonymous namespace + +#endif // TENSORRT_LOGGING_H diff --git a/yolov4/mish.cu b/yolov4/mish.cu index a7d6eee..d05f609 100644 --- a/yolov4/mish.cu +++ b/yolov4/mish.cu @@ -1,18 +1,19 @@ #include #include #include +#include #include "mish.h" namespace nvinfer1 { - MishPlugin::MishPlugin(const int cudaThread) : thread_count_(cudaThread) + MishPlugin::MishPlugin() { } - + MishPlugin::~MishPlugin() { } - + // create the plugin at runtime from a byte stream MishPlugin::MishPlugin(const void* data, size_t length) { @@ -20,12 +21,12 @@ namespace nvinfer1 input_size_ = *reinterpret_cast(data); } - void MishPlugin::serialize(void* buffer) + void MishPlugin::serialize(void* buffer) const { *reinterpret_cast(buffer) = input_size_; } - - size_t MishPlugin::getSerializationSize() + + size_t MishPlugin::getSerializationSize() const { return sizeof(input_size_); } @@ -34,14 +35,79 @@ namespace nvinfer1 { return 0; } - + Dims MishPlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) { assert(nbInputDims == 1); assert(index == 0); input_size_ = inputs[0].d[0] * inputs[0].d[1] * inputs[0].d[2]; // Output dimensions - return DimsCHW(inputs[0].d[0], inputs[0].d[1], inputs[0].d[2]); + return Dims3(inputs[0].d[0], inputs[0].d[1], inputs[0].d[2]); + } + + // Set plugin namespace + void MishPlugin::setPluginNamespace(const char* pluginNamespace) + { + mPluginNamespace = pluginNamespace; + } + + const char* MishPlugin::getPluginNamespace() const + { + return mPluginNamespace; + } + + // Return the DataType of the plugin output at the requested index + DataType MishPlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const + { + return DataType::kFLOAT; + } + + // Return true if output tensor is broadcast across a batch. + bool MishPlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const + { + return false; + } + + // Return true if plugin can use input that is broadcast across batch without replication. + bool MishPlugin::canBroadcastInputAcrossBatch(int inputIndex) const + { + return false; + } + + void MishPlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) + { + } + + // Attach the plugin object to an execution context and grant the plugin the access to some context resource. + void MishPlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) + { + } + + // Detach the plugin object from its execution context. + void MishPlugin::detachFromContext() {} + + const char* MishPlugin::getPluginType() const + { + return "Mish_TRT"; + } + + const char* MishPlugin::getPluginVersion() const + { + return "1"; + } + + void MishPlugin::destroy() + { + delete this; + } + + // Clone the plugin + IPluginV2IOExt* MishPlugin::clone() const + { + MishPlugin *p = new MishPlugin(); + p->input_size_ = input_size_; + p->setPluginNamespace(mPluginNamespace); + return p; } __device__ float tanh_activate_kernel(float x){return (2/(1 + expf(-2*x)) - 1);} @@ -75,7 +141,6 @@ namespace nvinfer1 mish_kernel<<>>(inputs[0], output, input_size_ * batchSize); } - int MishPlugin::enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) { //assert(batchSize == 1); @@ -84,5 +149,48 @@ namespace nvinfer1 forwardGpu((const float *const *)inputs, (float*)outputs[0], stream, batchSize); return 0; } + + PluginFieldCollection MishPluginCreator::mFC{}; + std::vector MishPluginCreator::mPluginAttributes; + + MishPluginCreator::MishPluginCreator() + { + mPluginAttributes.clear(); + + mFC.nbFields = mPluginAttributes.size(); + mFC.fields = mPluginAttributes.data(); + } + + const char* MishPluginCreator::getPluginName() const + { + return "Mish_TRT"; + } + + const char* MishPluginCreator::getPluginVersion() const + { + return "1"; + } + + const PluginFieldCollection* MishPluginCreator::getFieldNames() + { + return &mFC; + } + + IPluginV2IOExt* MishPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) + { + MishPlugin* obj = new MishPlugin(); + obj->setPluginNamespace(mNamespace.c_str()); + return obj; + } + + IPluginV2IOExt* MishPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) + { + // This object will be deleted when the network is destroyed, which will + // call MishPlugin::destroy() + MishPlugin* obj = new MishPlugin(serialData, serialLength); + obj->setPluginNamespace(mNamespace.c_str()); + return obj; + } + } diff --git a/yolov4/mish.h b/yolov4/mish.h index 970fefb..1c6fccb 100644 --- a/yolov4/mish.h +++ b/yolov4/mish.h @@ -1,49 +1,106 @@ #ifndef _MISH_PLUGIN_H #define _MISH_PLUGIN_H +#include +#include #include "NvInfer.h" namespace nvinfer1 { - class MishPlugin: public IPluginExt + class MishPlugin: public IPluginV2IOExt { - public: - explicit MishPlugin(const int cudaThread = 256); - MishPlugin(const void* data, size_t length); + public: + explicit MishPlugin(); + MishPlugin(const void* data, size_t length); - ~MishPlugin(); + ~MishPlugin(); - int getNbOutputs() const override - { - return 1; - } + int getNbOutputs() const override + { + return 1; + } - Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override; + Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override; - bool supportsFormat(DataType type, PluginFormat format) const override { - return type == DataType::kFLOAT && format == PluginFormat::kNCHW; - } + int initialize() override; - void configureWithFormat(const Dims* inputDims, int nbInputs, const Dims* outputDims, int nbOutputs, DataType type, PluginFormat format, int maxBatchSize) override {}; + virtual void terminate() override {}; - int initialize() override; + virtual size_t getWorkspaceSize(int maxBatchSize) const override { return 0;} - virtual void terminate() override {}; + virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override; - virtual size_t getWorkspaceSize(int maxBatchSize) const override { return 0;} + virtual size_t getSerializationSize() const override; - virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override; + virtual void serialize(void* buffer) const override; - virtual size_t getSerializationSize() override; + bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const override { + return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT; + } - virtual void serialize(void* buffer) override; + const char* getPluginType() const override; - void forwardGpu(const float *const * inputs, float* output, cudaStream_t stream, int batchSize = 1); + const char* getPluginVersion() const override; - private: - int thread_count_ = 256; - int input_size_; + void destroy() override; + + IPluginV2IOExt* clone() const override; + + void setPluginNamespace(const char* pluginNamespace) override; + + const char* getPluginNamespace() const override; + + DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const override; + + bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const override; + + bool canBroadcastInputAcrossBatch(int inputIndex) const override; + + void attachToContext( + cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) override; + + void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) override; + + void detachFromContext() override; + + int input_size_; + private: + void forwardGpu(const float *const * inputs, float* output, cudaStream_t stream, int batchSize = 1); + int thread_count_ = 256; + const char* mPluginNamespace; + }; + + class MishPluginCreator : public IPluginCreator + { + public: + MishPluginCreator(); + + ~MishPluginCreator() override = default; + + const char* getPluginName() const override; + + const char* getPluginVersion() const override; + + const PluginFieldCollection* getFieldNames() override; + + IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) override; + + IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) override; + + void setPluginNamespace(const char* libNamespace) override + { + mNamespace = libNamespace; + } + + const char* getPluginNamespace() const override + { + return mNamespace.c_str(); + } + + private: + std::string mNamespace; + static PluginFieldCollection mFC; + static std::vector mPluginAttributes; }; }; - #endif diff --git a/yolov4/plugin_factory.cpp b/yolov4/plugin_factory.cpp deleted file mode 100644 index cd426a4..0000000 --- a/yolov4/plugin_factory.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "common.h" -#include "plugin_factory.h" -#include "NvInferPlugin.h" -#include "yololayer.h" -#include "mish.h" - -using namespace nvinfer1; -using nvinfer1::PluginFactory; - -IPlugin* PluginFactory::createPlugin(const char* layerName, const void* serialData, size_t serialLength) { - IPlugin *plugin = nullptr; - if (strstr(layerName, "leaky") != NULL) { - plugin = plugin::createPReLUPlugin(serialData, serialLength); - } else if (strstr(layerName, "yolo") != NULL) { - plugin = new YoloLayerPlugin(serialData, serialLength); - } else if (strstr(layerName, "mish") != NULL) { - plugin = new MishPlugin(serialData, serialLength); - } - return plugin; -} diff --git a/yolov4/plugin_factory.h b/yolov4/plugin_factory.h deleted file mode 100644 index 0be0225..0000000 --- a/yolov4/plugin_factory.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef MY_PLUGIN_FACTORY_H -#define MY_PLUGIN_FACTORY_H -#include - -namespace nvinfer1 { -class PluginFactory : public IPluginFactory { - public: - IPlugin* createPlugin(const char* layerName, const void* serialData, size_t serialLength) override; -}; - -} -#endif diff --git a/yolov4/yololayer.cu b/yolov4/yololayer.cu index 19719a8..fd0af16 100644 --- a/yolov4/yololayer.cu +++ b/yolov4/yololayer.cu @@ -4,7 +4,7 @@ using namespace Yolo; namespace nvinfer1 { - YoloLayerPlugin::YoloLayerPlugin(const int cudaThread /*= 512*/):mThreadCount(cudaThread) + YoloLayerPlugin::YoloLayerPlugin() { mClassCount = CLASS_NUM; mYoloKernel.clear(); @@ -35,7 +35,7 @@ namespace nvinfer1 assert(d == a + length); } - void YoloLayerPlugin::serialize(void* buffer) + void YoloLayerPlugin::serialize(void* buffer) const { using namespace Tn; char* d = static_cast(buffer), *a = d; @@ -49,7 +49,7 @@ namespace nvinfer1 assert(d == a + getSerializationSize()); } - size_t YoloLayerPlugin::getSerializationSize() + size_t YoloLayerPlugin::getSerializationSize() const { return sizeof(mClassCount) + sizeof(mThreadCount) + sizeof(mKernelCount) + sizeof(Yolo::YoloKernel) * mYoloKernel.size(); } @@ -67,6 +67,70 @@ namespace nvinfer1 return Dims3(totalsize + 1, 1, 1); } + // Set plugin namespace + void YoloLayerPlugin::setPluginNamespace(const char* pluginNamespace) + { + mPluginNamespace = pluginNamespace; + } + + const char* YoloLayerPlugin::getPluginNamespace() const + { + return mPluginNamespace; + } + + // Return the DataType of the plugin output at the requested index + DataType YoloLayerPlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const + { + return DataType::kFLOAT; + } + + // Return true if output tensor is broadcast across a batch. + bool YoloLayerPlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const + { + return false; + } + + // Return true if plugin can use input that is broadcast across batch without replication. + bool YoloLayerPlugin::canBroadcastInputAcrossBatch(int inputIndex) const + { + return false; + } + + void YoloLayerPlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) + { + } + + // Attach the plugin object to an execution context and grant the plugin the access to some context resource. + void YoloLayerPlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) + { + } + + // Detach the plugin object from its execution context. + void YoloLayerPlugin::detachFromContext() {} + + const char* YoloLayerPlugin::getPluginType() const + { + return "YoloLayer_TRT"; + } + + const char* YoloLayerPlugin::getPluginVersion() const + { + return "1"; + } + + void YoloLayerPlugin::destroy() + { + delete this; + } + + // Clone the plugin + IPluginV2IOExt* YoloLayerPlugin::clone() const + { + YoloLayerPlugin *p = new YoloLayerPlugin(); + p->setPluginNamespace(mPluginNamespace); + return p; + } + __device__ float Logist(float data){ return 1./(1. + exp(-data)); }; __global__ void CalDetection(const float *input, float *output,int noElements, @@ -150,4 +214,46 @@ namespace nvinfer1 return 0; } + PluginFieldCollection YoloPluginCreator::mFC{}; + std::vector YoloPluginCreator::mPluginAttributes; + + YoloPluginCreator::YoloPluginCreator() + { + mPluginAttributes.clear(); + + mFC.nbFields = mPluginAttributes.size(); + mFC.fields = mPluginAttributes.data(); + } + + const char* YoloPluginCreator::getPluginName() const + { + return "YoloLayer_TRT"; + } + + const char* YoloPluginCreator::getPluginVersion() const + { + return "1"; + } + + const PluginFieldCollection* YoloPluginCreator::getFieldNames() + { + return &mFC; + } + + IPluginV2IOExt* YoloPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) + { + YoloLayerPlugin* obj = new YoloLayerPlugin(); + obj->setPluginNamespace(mNamespace.c_str()); + return obj; + } + + IPluginV2IOExt* YoloPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) + { + // This object will be deleted when the network is destroyed, which will + // call MishPlugin::destroy() + YoloLayerPlugin* obj = new YoloLayerPlugin(serialData, serialLength); + obj->setPluginNamespace(mNamespace.c_str()); + return obj; + } + } diff --git a/yolov4/yololayer.h b/yolov4/yololayer.h index 5b9cbc3..5074636 100644 --- a/yolov4/yololayer.h +++ b/yolov4/yololayer.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "NvInfer.h" #include "Utils.h" @@ -26,17 +25,17 @@ namespace Yolo float anchors[CHECK_COUNT*2]; }; - static YoloKernel yolo1 = { + static constexpr YoloKernel yolo1 = { INPUT_W / 8, INPUT_H / 8, {12,16, 19,36, 40,28} }; - static YoloKernel yolo2 = { + static constexpr YoloKernel yolo2 = { INPUT_W / 16, INPUT_H / 16, {36,75, 76,55, 72,146} }; - static YoloKernel yolo3 = { + static constexpr YoloKernel yolo3 = { INPUT_W / 32, INPUT_H / 32, {142,110, 192,243, 459,401} @@ -55,48 +54,106 @@ namespace Yolo namespace nvinfer1 { - class YoloLayerPlugin: public IPluginExt + class YoloLayerPlugin: public IPluginV2IOExt { - public: - explicit YoloLayerPlugin(const int cudaThread = 256); - YoloLayerPlugin(const void* data, size_t length); + public: + explicit YoloLayerPlugin(); + YoloLayerPlugin(const void* data, size_t length); - ~YoloLayerPlugin(); + ~YoloLayerPlugin(); - int getNbOutputs() const override - { - return 1; - } + int getNbOutputs() const override + { + return 1; + } - Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override; + Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override; - bool supportsFormat(DataType type, PluginFormat format) const override { - return type == DataType::kFLOAT && format == PluginFormat::kNCHW; - } + int initialize() override; - void configureWithFormat(const Dims* inputDims, int nbInputs, const Dims* outputDims, int nbOutputs, DataType type, PluginFormat format, int maxBatchSize) override {}; + virtual void terminate() override {}; - int initialize() override; + virtual size_t getWorkspaceSize(int maxBatchSize) const override { return 0;} - virtual void terminate() override {}; + virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override; - virtual size_t getWorkspaceSize(int maxBatchSize) const override { return 0;} + virtual size_t getSerializationSize() const override; - virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override; + virtual void serialize(void* buffer) const override; - virtual size_t getSerializationSize() override; + bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const override { + return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT; + } - virtual void serialize(void* buffer) override; + const char* getPluginType() const override; - void forwardGpu(const float *const * inputs,float * output, cudaStream_t stream,int batchSize = 1); + const char* getPluginVersion() const override; - private: - int mClassCount; - int mKernelCount; - std::vector mYoloKernel; - int mThreadCount; - //int mDetNum; + void destroy() override; + + IPluginV2IOExt* clone() const override; + + void setPluginNamespace(const char* pluginNamespace) override; + + const char* getPluginNamespace() const override; + + DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const override; + + bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const override; + + bool canBroadcastInputAcrossBatch(int inputIndex) const override; + + void attachToContext( + cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) override; + + void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) override; + + void detachFromContext() override; + + private: + void forwardGpu(const float *const * inputs,float * output, cudaStream_t stream,int batchSize = 1); + int mClassCount; + int mKernelCount; + std::vector mYoloKernel; + int mThreadCount = 256; + const char* mPluginNamespace; }; + + class YoloPluginCreator : public IPluginCreator + { + public: + YoloPluginCreator(); + + ~YoloPluginCreator() override = default; + + const char* getPluginName() const override; + + const char* getPluginVersion() const override; + + const PluginFieldCollection* getFieldNames() override; + + IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) override; + + IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) override; + + void setPluginNamespace(const char* libNamespace) override + { + mNamespace = libNamespace; + } + + const char* getPluginNamespace() const override + { + return mNamespace.c_str(); + } + + private: + std::string mNamespace; + static PluginFieldCollection mFC; + static std::vector mPluginAttributes; + }; + + + }; #endif diff --git a/yolov4/yolov4.cpp b/yolov4/yolov4.cpp index 6e92057..4e86554 100644 --- a/yolov4/yolov4.cpp +++ b/yolov4/yolov4.cpp @@ -1,18 +1,28 @@ -#include "NvInfer.h" -#include "NvInferPlugin.h" -#include "cuda_runtime_api.h" -#include "common.h" #include #include #include #include #include #include -#include "plugin_factory.h" -#include "yololayer.h" -#include "mish.h" #include #include +#include "NvInfer.h" +#include "NvInferPlugin.h" +#include "cuda_runtime_api.h" +#include "logging.h" +#include "yololayer.h" +#include "mish.h" + +#define CHECK(status) \ + do\ + {\ + auto ret = (status);\ + if (ret != 0)\ + {\ + std::cerr << "Cuda failure: " << ret << std::endl;\ + abort();\ + }\ + } while (0) #define USE_FP16 // comment out this if want to use FP32 #define DEVICE 0 // GPU id @@ -30,6 +40,8 @@ static const int OUTPUT_SIZE = Yolo::MAX_OUTPUT_BBOX_COUNT * DETECTION_SIZE + 1; const char* INPUT_BLOB_NAME = "data"; const char* OUTPUT_BLOB_NAME = "prob"; static Logger gLogger; +REGISTER_TENSORRT_PLUGIN(MishPluginCreator); +REGISTER_TENSORRT_PLUGIN(YoloPluginCreator); cv::Mat preprocess_img(cv::Mat& img) { int w, h, x, y; @@ -81,10 +93,10 @@ cv::Rect get_rect(cv::Mat& img, float bbox[4]) { float iou(float lbox[4], float rbox[4]) { float interBox[] = { - max(lbox[0] - lbox[2]/2.f , rbox[0] - rbox[2]/2.f), //left - min(lbox[0] + lbox[2]/2.f , rbox[0] + rbox[2]/2.f), //right - max(lbox[1] - lbox[3]/2.f , rbox[1] - rbox[3]/2.f), //top - min(lbox[1] + lbox[3]/2.f , rbox[1] + rbox[3]/2.f), //bottom + std::max(lbox[0] - lbox[2]/2.f , rbox[0] - rbox[2]/2.f), //left + std::min(lbox[0] + lbox[2]/2.f , rbox[0] + rbox[2]/2.f), //right + std::max(lbox[1] - lbox[3]/2.f , rbox[1] - rbox[3]/2.f), //top + std::min(lbox[1] + lbox[3]/2.f , rbox[1] + rbox[3]/2.f), //bottom }; if(interBox[2] > interBox[3] || interBox[0] > interBox[1]) @@ -201,44 +213,42 @@ IScaleLayer* addBatchNorm2d(INetworkDefinition *network, std::map& weightMap, ITensor& input, int outch, int ksize, int s, int p, int linx) { std::cout << linx << std::endl; Weights emptywts{DataType::kFLOAT, nullptr, 0}; - IConvolutionLayer* conv1 = network->addConvolution(input, outch, DimsHW{ksize, ksize}, weightMap["module_list." + std::to_string(linx) + ".Conv2d.weight"], emptywts); + IConvolutionLayer* conv1 = network->addConvolutionNd(input, outch, DimsHW{ksize, ksize}, weightMap["module_list." + std::to_string(linx) + ".Conv2d.weight"], emptywts); assert(conv1); - conv1->setStride(DimsHW{s, s}); - conv1->setPadding(DimsHW{p, p}); + conv1->setStrideNd(DimsHW{s, s}); + conv1->setPaddingNd(DimsHW{p, p}); IScaleLayer* bn1 = addBatchNorm2d(network, weightMap, *conv1->getOutput(0), "module_list." + std::to_string(linx) + ".BatchNorm2d", 1e-4); - auto mish = new MishPlugin(); + auto creator = getPluginRegistry()->getPluginCreator("Mish_TRT", "1"); + const PluginFieldCollection* pluginData = creator->getFieldNames(); + IPluginV2 *pluginObj = creator->createPlugin(("mish" + std::to_string(linx)).c_str(), pluginData); ITensor* inputTensors[] = {bn1->getOutput(0)}; - auto mish_ = network->addPlugin(inputTensors, 1, *mish); - assert(mish_); - mish_->setName(("mish" + std::to_string(linx)).c_str()); - return mish_; + auto mish = network->addPluginV2(&inputTensors[0], 1, *pluginObj); + return mish; } ILayer* convBnLeaky(INetworkDefinition *network, std::map& weightMap, ITensor& input, int outch, int ksize, int s, int p, int linx) { std::cout << linx << std::endl; Weights emptywts{DataType::kFLOAT, nullptr, 0}; - IConvolutionLayer* conv1 = network->addConvolution(input, outch, DimsHW{ksize, ksize}, weightMap["module_list." + std::to_string(linx) + ".Conv2d.weight"], emptywts); + IConvolutionLayer* conv1 = network->addConvolutionNd(input, outch, DimsHW{ksize, ksize}, weightMap["module_list." + std::to_string(linx) + ".Conv2d.weight"], emptywts); assert(conv1); - conv1->setStride(DimsHW{s, s}); - conv1->setPadding(DimsHW{p, p}); + conv1->setStrideNd(DimsHW{s, s}); + conv1->setPaddingNd(DimsHW{p, p}); IScaleLayer* bn1 = addBatchNorm2d(network, weightMap, *conv1->getOutput(0), "module_list." + std::to_string(linx) + ".BatchNorm2d", 1e-4); - ITensor* inputTensors[] = {bn1->getOutput(0)}; - auto lr = plugin::createPReLUPlugin(0.1); - auto lr1 = network->addPlugin(inputTensors, 1, *lr); - assert(lr1); - lr1->setName(("leaky" + std::to_string(linx)).c_str()); - return lr1; + auto lr = network->addActivation(*bn1->getOutput(0), ActivationType::kLEAKY_RELU); + lr->setAlpha(0.1); + + return lr; } // Creat the engine using only the API and not any parser. -ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType dt) { - INetworkDefinition* network = builder->createNetwork(); +ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, IBuilderConfig* config, DataType dt) { + INetworkDefinition* network = builder->createNetworkV2(0U); - // Create input tensor of shape { 1, 1, INPUT_H, INPUT_W} with name INPUT_BLOB_NAME + // Create input tensor of shape {3, INPUT_H, INPUT_W} with name INPUT_BLOB_NAME ITensor* data = network->addInput(INPUT_BLOB_NAME, dt, Dims3{3, INPUT_H, INPUT_W}); assert(data); @@ -372,21 +382,21 @@ ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType auto l106 = convBnLeaky(network, weightMap, *l105->getOutput(0), 1024, 3, 1, 1, 106); auto l107 = convBnLeaky(network, weightMap, *l106->getOutput(0), 512, 1, 1, 0, 107); - auto pool108 = network->addPooling(*l107->getOutput(0), PoolingType::kMAX, DimsHW{5, 5}); - pool108->setPadding(DimsHW{2, 2}); - pool108->setStride(DimsHW{1, 1}); + auto pool108 = network->addPoolingNd(*l107->getOutput(0), PoolingType::kMAX, DimsHW{5, 5}); + pool108->setPaddingNd(DimsHW{2, 2}); + pool108->setStrideNd(DimsHW{1, 1}); auto l109 = l107; - auto pool110 = network->addPooling(*l109->getOutput(0), PoolingType::kMAX, DimsHW{9, 9}); - pool110->setPadding(DimsHW{4, 4}); - pool110->setStride(DimsHW{1, 1}); + auto pool110 = network->addPoolingNd(*l109->getOutput(0), PoolingType::kMAX, DimsHW{9, 9}); + pool110->setPaddingNd(DimsHW{4, 4}); + pool110->setStrideNd(DimsHW{1, 1}); auto l111 = l107; - auto pool112 = network->addPooling(*l111->getOutput(0), PoolingType::kMAX, DimsHW{13, 13}); - pool112->setPadding(DimsHW{6, 6}); - pool112->setStride(DimsHW{1, 1}); + auto pool112 = network->addPoolingNd(*l111->getOutput(0), PoolingType::kMAX, DimsHW{13, 13}); + pool112->setPaddingNd(DimsHW{6, 6}); + pool112->setStrideNd(DimsHW{1, 1}); ITensor* inputTensors113[] = {pool112->getOutput(0), pool110->getOutput(0), pool108->getOutput(0), l107->getOutput(0)}; auto cat113 = network->addConcatenation(inputTensors113, 4); @@ -401,9 +411,9 @@ ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType deval[i] = 1.0; } Weights deconvwts118{DataType::kFLOAT, deval, 256 * 2 * 2}; - IDeconvolutionLayer* deconv118 = network->addDeconvolution(*l117->getOutput(0), 256, DimsHW{2, 2}, deconvwts118, emptywts); + IDeconvolutionLayer* deconv118 = network->addDeconvolutionNd(*l117->getOutput(0), 256, DimsHW{2, 2}, deconvwts118, emptywts); assert(deconv118); - deconv118->setStride(DimsHW{2, 2}); + deconv118->setStrideNd(DimsHW{2, 2}); deconv118->setNbGroups(256); weightMap["deconv118"] = deconvwts118; @@ -421,9 +431,9 @@ ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType auto l127 = convBnLeaky(network, weightMap, *l126->getOutput(0), 128, 1, 1, 0, 127); Weights deconvwts128{DataType::kFLOAT, deval, 128 * 2 * 2}; - IDeconvolutionLayer* deconv128 = network->addDeconvolution(*l127->getOutput(0), 128, DimsHW{2, 2}, deconvwts128, emptywts); + IDeconvolutionLayer* deconv128 = network->addDeconvolutionNd(*l127->getOutput(0), 128, DimsHW{2, 2}, deconvwts128, emptywts); assert(deconv128); - deconv128->setStride(DimsHW{2, 2}); + deconv128->setStrideNd(DimsHW{2, 2}); deconv128->setNbGroups(128); auto l129 = l54; @@ -438,7 +448,7 @@ ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType auto l135 = convBnLeaky(network, weightMap, *l134->getOutput(0), 256, 3, 1, 1, 135); auto l136 = convBnLeaky(network, weightMap, *l135->getOutput(0), 128, 1, 1, 0, 136); auto l137 = convBnLeaky(network, weightMap, *l136->getOutput(0), 256, 3, 1, 1, 137); - IConvolutionLayer* conv138 = network->addConvolution(*l137->getOutput(0), 3 * (Yolo::CLASS_NUM + 5), DimsHW{1, 1}, weightMap["module_list.138.Conv2d.weight"], weightMap["module_list.138.Conv2d.bias"]); + IConvolutionLayer* conv138 = network->addConvolutionNd(*l137->getOutput(0), 3 * (Yolo::CLASS_NUM + 5), DimsHW{1, 1}, weightMap["module_list.138.Conv2d.weight"], weightMap["module_list.138.Conv2d.bias"]); assert(conv138); // 139 is yolo layer @@ -454,7 +464,7 @@ ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType auto l146 = convBnLeaky(network, weightMap, *l145->getOutput(0), 512, 3, 1, 1, 146); auto l147 = convBnLeaky(network, weightMap, *l146->getOutput(0), 256, 1, 1, 0, 147); auto l148 = convBnLeaky(network, weightMap, *l147->getOutput(0), 512, 3, 1, 1, 148); - IConvolutionLayer* conv149 = network->addConvolution(*l148->getOutput(0), 3 * (Yolo::CLASS_NUM + 5), DimsHW{1, 1}, weightMap["module_list.149.Conv2d.weight"], weightMap["module_list.149.Conv2d.bias"]); + IConvolutionLayer* conv149 = network->addConvolutionNd(*l148->getOutput(0), 3 * (Yolo::CLASS_NUM + 5), DimsHW{1, 1}, weightMap["module_list.149.Conv2d.weight"], weightMap["module_list.149.Conv2d.bias"]); assert(conv149); // 150 is yolo layer @@ -470,27 +480,27 @@ ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType auto l157 = convBnLeaky(network, weightMap, *l156->getOutput(0), 1024, 3, 1, 1, 157); auto l158 = convBnLeaky(network, weightMap, *l157->getOutput(0), 512, 1, 1, 0, 158); auto l159 = convBnLeaky(network, weightMap, *l158->getOutput(0), 1024, 3, 1, 1, 159); - IConvolutionLayer* conv160 = network->addConvolution(*l159->getOutput(0), 3 * (Yolo::CLASS_NUM + 5), DimsHW{1, 1}, weightMap["module_list.160.Conv2d.weight"], weightMap["module_list.160.Conv2d.bias"]); + IConvolutionLayer* conv160 = network->addConvolutionNd(*l159->getOutput(0), 3 * (Yolo::CLASS_NUM + 5), DimsHW{1, 1}, weightMap["module_list.160.Conv2d.weight"], weightMap["module_list.160.Conv2d.bias"]); assert(conv160); // 161 is yolo layer - auto yolo = new YoloLayerPlugin(); + auto creator = getPluginRegistry()->getPluginCreator("YoloLayer_TRT", "1"); + const PluginFieldCollection* pluginData = creator->getFieldNames(); + IPluginV2 *pluginObj = creator->createPlugin("yololayer", pluginData); ITensor* inputTensors_yolo[] = {conv138->getOutput(0), conv149->getOutput(0), conv160->getOutput(0)}; - auto yolo_ = network->addPlugin(inputTensors_yolo, 3, *yolo); - assert(yolo_); - yolo_->setName("yolo_"); + auto yolo = network->addPluginV2(inputTensors_yolo, 3, *pluginObj); - yolo_->getOutput(0)->setName(OUTPUT_BLOB_NAME); + yolo->getOutput(0)->setName(OUTPUT_BLOB_NAME); std::cout << "set name out" << std::endl; - network->markOutput(*yolo_->getOutput(0)); + network->markOutput(*yolo->getOutput(0)); // Build engine builder->setMaxBatchSize(maxBatchSize); - builder->setMaxWorkspaceSize(1 << 20); + config->setMaxWorkspaceSize(16 * (1 << 20)); // 16MB #ifdef USE_FP16 - builder->setFp16Mode(true); + config->setFlag(BuilderFlag::kFP16); #endif - ICudaEngine* engine = builder->buildCudaEngine(*network); + ICudaEngine* engine = builder->buildEngineWithConfig(*network, *config); std::cout << "build out" << std::endl; // Don't need the network any more @@ -508,9 +518,10 @@ ICudaEngine* createEngine(unsigned int maxBatchSize, IBuilder* builder, DataType void APIToModel(unsigned int maxBatchSize, IHostMemory** modelStream) { // Create builder IBuilder* builder = createInferBuilder(gLogger); + IBuilderConfig* config = builder->createBuilderConfig(); // Create model to populate the network, then set the outputs and create an engine - ICudaEngine* engine = createEngine(maxBatchSize, builder, DataType::kFLOAT); + ICudaEngine* engine = createEngine(maxBatchSize, builder, config, DataType::kFLOAT); assert(engine != nullptr); // Serialize the engine @@ -586,7 +597,7 @@ int main(int argc, char** argv) { IHostMemory* modelStream{nullptr}; APIToModel(BATCH_SIZE, &modelStream); assert(modelStream != nullptr); - std::ofstream p("yolov4.engine"); + std::ofstream p("yolov4.engine", std::ios::binary); if (!p) { std::cerr << "could not open plan output file" << std::endl; return -1; @@ -623,20 +634,20 @@ int main(int argc, char** argv) { //for (int i = 0; i < 3 * INPUT_H * INPUT_W; i++) // data[i] = 1.0; static float prob[BATCH_SIZE * OUTPUT_SIZE]; - PluginFactory pf; IRuntime* runtime = createInferRuntime(gLogger); assert(runtime != nullptr); - ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size, &pf); + ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size); assert(engine != nullptr); IExecutionContext* context = engine->createExecutionContext(); assert(context != nullptr); + delete[] trtModelStream; int fcount = 0; - for (int f = 0; f < file_names.size(); f++) { + for (int f = 0; f < (int)file_names.size(); f++) { fcount++; - if (fcount < BATCH_SIZE && f + 1 != file_names.size()) continue; + if (fcount < BATCH_SIZE && f + 1 != (int)file_names.size()) continue; for (int b = 0; b < fcount; b++) { - cv::Mat img = cv::imread(std::string(argv[2]) + "/" + file_names[f - BATCH_SIZE + 1 + b]); + cv::Mat img = cv::imread(std::string(argv[2]) + "/" + file_names[f - fcount + 1 + b]); if (img.empty()) continue; cv::Mat pr_img = preprocess_img(img); for (int i = 0; i < INPUT_H * INPUT_W; i++) { @@ -659,18 +670,18 @@ int main(int argc, char** argv) { for (int b = 0; b < fcount; b++) { auto& res = batch_res[b]; //std::cout << res.size() << std::endl; - cv::Mat img = cv::imread(std::string(argv[2]) + "/" + file_names[f - BATCH_SIZE + 1 + b]); + cv::Mat img = cv::imread(std::string(argv[2]) + "/" + file_names[f - fcount + 1 + b]); for (size_t j = 0; j < res.size(); j++) { - float *p = (float*)&res[j]; - for (size_t k = 0; k < 7; k++) { + //float *p = (float*)&res[j]; + //for (size_t k = 0; k < 7; k++) { // std::cout << p[k] << ", "; - } + //} //std::cout << std::endl; cv::Rect r = get_rect(img, res[j].bbox); cv::rectangle(img, r, cv::Scalar(0x27, 0xC1, 0x36), 2); cv::putText(img, std::to_string((int)res[j].class_id), cv::Point(r.x, r.y - 1), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0xFF, 0xFF, 0xFF), 2); } - cv::imwrite("_" + file_names[f - BATCH_SIZE + 1 + b], img); + cv::imwrite("_" + file_names[f - fcount + 1 + b], img); } fcount = 0; }