|
| 1 | +#ifndef LANE_DET_COMMON_H_ |
| 2 | +#define LANE_DET_COMMON_H_ |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <fstream> |
| 6 | +#include <map> |
| 7 | +#include <string> |
| 8 | +#include <sstream> |
| 9 | +#include <vector> |
| 10 | +#include <opencv2/opencv.hpp> |
| 11 | +#include "dirent.h" |
| 12 | +#include "NvInfer.h" |
| 13 | +#include <chrono> |
| 14 | + |
| 15 | +#define CHECK(status) \ |
| 16 | + do\ |
| 17 | + {\ |
| 18 | + auto ret = (status);\ |
| 19 | + if (ret != 0)\ |
| 20 | + {\ |
| 21 | + std::cerr << "Cuda failure: " << ret << std::endl;\ |
| 22 | + abort();\ |
| 23 | + }\ |
| 24 | + } while (0) |
| 25 | + |
| 26 | +using namespace nvinfer1; |
| 27 | + |
| 28 | +// TensorRT weight files have a simple space delimited format: |
| 29 | +// [type] [size] <data x size in hex> |
| 30 | +std::map<std::string, Weights> loadWeights(const std::string file) { |
| 31 | + std::cout << "Loading weights: " << file << std::endl; |
| 32 | + std::map<std::string, Weights> weightMap; |
| 33 | + |
| 34 | + // Open weights file |
| 35 | + std::ifstream input(file); |
| 36 | + assert(input.is_open() && "Unable to load weight file."); |
| 37 | + |
| 38 | + // Read number of weight blobs |
| 39 | + int32_t count; |
| 40 | + input >> count; |
| 41 | + assert(count > 0 && "Invalid weight map file."); |
| 42 | + |
| 43 | + while (count--) |
| 44 | + { |
| 45 | + Weights wt{DataType::kFLOAT, nullptr, 0}; |
| 46 | + uint32_t size; |
| 47 | + |
| 48 | + // Read name and type of blob |
| 49 | + std::string name; |
| 50 | + input >> name >> std::dec >> size; |
| 51 | + wt.type = DataType::kFLOAT; |
| 52 | + |
| 53 | + // Load blob |
| 54 | + uint32_t* val = reinterpret_cast<uint32_t*>(malloc(sizeof(val) * size)); |
| 55 | + for (uint32_t x = 0, y = size; x < y; ++x) |
| 56 | + { |
| 57 | + input >> std::hex >> val[x]; |
| 58 | + } |
| 59 | + wt.values = val; |
| 60 | + |
| 61 | + wt.count = size; |
| 62 | + weightMap[name] = wt; |
| 63 | + } |
| 64 | + |
| 65 | + return weightMap; |
| 66 | +} |
| 67 | + |
| 68 | +IScaleLayer* addBatchNorm2d(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, std::string lname, float eps) { |
| 69 | + float *gamma = (float*)weightMap[lname + ".weight"].values; |
| 70 | + float *beta = (float*)weightMap[lname + ".bias"].values; |
| 71 | + float *mean = (float*)weightMap[lname + ".running_mean"].values; |
| 72 | + float *var = (float*)weightMap[lname + ".running_var"].values; |
| 73 | + int len = weightMap[lname + ".running_var"].count; |
| 74 | + |
| 75 | + float *scval = reinterpret_cast<float*>(malloc(sizeof(float) * len)); |
| 76 | + for (int i = 0; i < len; i++) { |
| 77 | + scval[i] = gamma[i] / sqrt(var[i] + eps); |
| 78 | + } |
| 79 | + Weights scale{DataType::kFLOAT, scval, len}; |
| 80 | + |
| 81 | + float *shval = reinterpret_cast<float*>(malloc(sizeof(float) * len)); |
| 82 | + for (int i = 0; i < len; i++) { |
| 83 | + shval[i] = beta[i] - mean[i] * gamma[i] / sqrt(var[i] + eps); |
| 84 | + } |
| 85 | + Weights shift{DataType::kFLOAT, shval, len}; |
| 86 | + |
| 87 | + float *pval = reinterpret_cast<float*>(malloc(sizeof(float) * len)); |
| 88 | + for (int i = 0; i < len; i++) { |
| 89 | + pval[i] = 1.0; |
| 90 | + } |
| 91 | + Weights power{DataType::kFLOAT, pval, len}; |
| 92 | + |
| 93 | + weightMap[lname + ".scale"] = scale; |
| 94 | + weightMap[lname + ".shift"] = shift; |
| 95 | + weightMap[lname + ".power"] = power; |
| 96 | + IScaleLayer* scale_1 = network->addScale(input, ScaleMode::kCHANNEL, shift, scale, power); |
| 97 | + assert(scale_1); |
| 98 | + return scale_1; |
| 99 | +} |
| 100 | + |
| 101 | +ILayer* convBnLeaky( INetworkDefinition *network, std::map<std::string, Weights>& weightMap, |
| 102 | + ITensor& input, int outch, int ksize, int s, int p, int g, |
| 103 | + std::string lname, int i, bool use_bn = false ) |
| 104 | +{ |
| 105 | + Weights emptywts{DataType::kFLOAT, nullptr, 0}; |
| 106 | + |
| 107 | + IConvolutionLayer* conv1 = network->addConvolution(input, outch, DimsHW{ ksize, ksize }, weightMap[lname + ".conv"+ std::to_string(i) + ".weight"], weightMap[lname + ".conv" + std::to_string(i)+".bias"]); |
| 108 | + assert(conv1); |
| 109 | + conv1->setStride(DimsHW{s, s}); |
| 110 | + conv1->setPadding(DimsHW{p, p}); |
| 111 | + conv1->setNbGroups(g); |
| 112 | + if (use_bn) |
| 113 | + { |
| 114 | + IScaleLayer* bn1 = addBatchNorm2d(network, weightMap, *conv1->getOutput(0), lname + ".batchnorm"+std::to_string(i), 1e-5); |
| 115 | + auto relu = network->addActivation(*bn1->getOutput(0), ActivationType::kRELU); |
| 116 | + assert(relu); |
| 117 | + return relu; |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + auto relu = network->addActivation(*conv1->getOutput(0), ActivationType::kRELU); |
| 122 | + assert(relu); |
| 123 | + return relu; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +IActivationLayer* basicBlock(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int inch, int outch, int stride, std::string lname) { |
| 128 | + Weights emptywts{ DataType::kFLOAT, nullptr, 0 }; |
| 129 | + |
| 130 | + IConvolutionLayer* conv1 = network->addConvolution(input, outch, DimsHW{ 3, 3 }, weightMap[lname + "conv1.weight"], emptywts); |
| 131 | + assert(conv1); |
| 132 | + conv1->setStride(DimsHW{ stride, stride }); |
| 133 | + conv1->setPadding(DimsHW{ 1, 1 }); |
| 134 | + |
| 135 | + IScaleLayer* bn1 = addBatchNorm2d(network, weightMap, *conv1->getOutput(0), lname + "bn1", 1e-5); |
| 136 | + |
| 137 | + IActivationLayer* relu1 = network->addActivation(*bn1->getOutput(0), ActivationType::kRELU); |
| 138 | + assert(relu1); |
| 139 | + |
| 140 | + IConvolutionLayer* conv2 = network->addConvolution(*relu1->getOutput(0), outch, DimsHW{ 3, 3 }, weightMap[lname + "conv2.weight"], emptywts); |
| 141 | + assert(conv2); |
| 142 | + conv2->setPadding(DimsHW{ 1, 1 }); |
| 143 | + |
| 144 | + IScaleLayer* bn2 = addBatchNorm2d(network, weightMap, *conv2->getOutput(0), lname + "bn2", 1e-5); |
| 145 | + |
| 146 | + IElementWiseLayer* ew1; |
| 147 | + if (inch != outch) { |
| 148 | + IConvolutionLayer* conv3 = network->addConvolution(input, outch, DimsHW{ 1, 1 }, weightMap[lname + "downsample.0.weight"], emptywts); |
| 149 | + assert(conv3); |
| 150 | + conv3->setStride(DimsHW{ stride, stride }); |
| 151 | + IScaleLayer* bn3 = addBatchNorm2d(network, weightMap, *conv3->getOutput(0), lname + "downsample.1", 1e-5); |
| 152 | + ew1 = network->addElementWise(*bn3->getOutput(0), *bn2->getOutput(0), ElementWiseOperation::kSUM); |
| 153 | + } |
| 154 | + else { |
| 155 | + ew1 = network->addElementWise(input, *bn2->getOutput(0), ElementWiseOperation::kSUM); |
| 156 | + } |
| 157 | + IActivationLayer* relu2 = network->addActivation(*ew1->getOutput(0), ActivationType::kRELU); |
| 158 | + assert(relu2); |
| 159 | + return relu2; |
| 160 | +} |
| 161 | + |
| 162 | +int read_files_in_dir(const char *p_dir_name, std::vector<std::string> &file_names) { |
| 163 | + DIR *p_dir = opendir(p_dir_name); |
| 164 | + if (p_dir == nullptr) { |
| 165 | + return -1; |
| 166 | + } |
| 167 | + |
| 168 | + struct dirent* p_file = nullptr; |
| 169 | + while ((p_file = readdir(p_dir)) != nullptr) { |
| 170 | + if (strcmp(p_file->d_name, ".") != 0 && |
| 171 | + strcmp(p_file->d_name, "..") != 0) { |
| 172 | + //std::string cur_file_name(p_dir_name); |
| 173 | + //cur_file_name += "/"; |
| 174 | + //cur_file_name += p_file->d_name; |
| 175 | + std::string cur_file_name(p_file->d_name); |
| 176 | + file_names.push_back(cur_file_name); |
| 177 | + } |
| 178 | + } |
| 179 | + closedir(p_dir); |
| 180 | + return 0; |
| 181 | +} |
| 182 | + |
| 183 | +#endif |
| 184 | + |
0 commit comments