|
| 1 | +#ifndef UNET_COMMON_H_ |
| 2 | +#define UNET_COMMON_H_ |
| 3 | + |
| 4 | +#include <fstream> |
| 5 | +#include <map> |
| 6 | +#include <sstream> |
| 7 | +#include <vector> |
| 8 | +#include <opencv2/opencv.hpp> |
| 9 | +#include <dirent.h> |
| 10 | +#include "NvInfer.h" |
| 11 | + |
| 12 | + |
| 13 | +#define CHECK(status) \ |
| 14 | + do\ |
| 15 | + {\ |
| 16 | + auto ret = (status);\ |
| 17 | + if (ret != 0)\ |
| 18 | + {\ |
| 19 | + std::cerr << "Cuda failure: " << ret << std::endl;\ |
| 20 | + abort();\ |
| 21 | + }\ |
| 22 | + } while (0) |
| 23 | + |
| 24 | +using namespace nvinfer1; |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +// TensorRT weight files have a simple space delimited format: |
| 31 | +// [type] [size] <data x size in hex> |
| 32 | +std::map<std::string, Weights> loadWeights(const std::string file) { |
| 33 | + std::cout << "Loading weights: " << file << std::endl; |
| 34 | + std::map<std::string, Weights> weightMap; |
| 35 | + |
| 36 | + // Open weights file |
| 37 | + std::ifstream input(file); |
| 38 | + assert(input.is_open() && "Unable to load weight file. please check if the .wts file path is right!!!!!!"); |
| 39 | + |
| 40 | + // Read number of weight blobs |
| 41 | + int32_t count; |
| 42 | + input >> count; |
| 43 | + assert(count > 0 && "Invalid weight map file."); |
| 44 | + |
| 45 | + while (count--) |
| 46 | + { |
| 47 | + Weights wt{DataType::kFLOAT, nullptr, 0}; |
| 48 | + uint32_t size; |
| 49 | + |
| 50 | + // Read name and type of blob |
| 51 | + std::string name; |
| 52 | + input >> name >> std::dec >> size; |
| 53 | + wt.type = DataType::kFLOAT; |
| 54 | + |
| 55 | + // Load blob |
| 56 | + uint32_t* val = reinterpret_cast<uint32_t*>(malloc(sizeof(val) * size)); |
| 57 | + for (uint32_t x = 0, y = size; x < y; ++x) |
| 58 | + { |
| 59 | + input >> std::hex >> val[x]; |
| 60 | + } |
| 61 | + wt.values = val; |
| 62 | + |
| 63 | + wt.count = size; |
| 64 | + weightMap[name] = wt; |
| 65 | + } |
| 66 | + |
| 67 | + return weightMap; |
| 68 | +} |
| 69 | + |
| 70 | +IScaleLayer* addBatchNorm2d(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, std::string lname, float eps) { |
| 71 | + float *gamma = (float*)weightMap[lname + ".weight"].values; |
| 72 | + float *beta = (float*)weightMap[lname + ".bias"].values; |
| 73 | + float *mean = (float*)weightMap[lname + ".running_mean"].values; |
| 74 | + float *var = (float*)weightMap[lname + ".running_var"].values; |
| 75 | + int len = weightMap[lname + ".running_var"].count; |
| 76 | + |
| 77 | + float *scval = reinterpret_cast<float*>(malloc(sizeof(float) * len)); |
| 78 | + for (int i = 0; i < len; i++) { |
| 79 | + scval[i] = gamma[i] / sqrt(var[i] + eps); |
| 80 | + } |
| 81 | + Weights scale{DataType::kFLOAT, scval, len}; |
| 82 | + |
| 83 | + float *shval = reinterpret_cast<float*>(malloc(sizeof(float) * len)); |
| 84 | + for (int i = 0; i < len; i++) { |
| 85 | + shval[i] = beta[i] - mean[i] * gamma[i] / sqrt(var[i] + eps); |
| 86 | + } |
| 87 | + Weights shift{DataType::kFLOAT, shval, len}; |
| 88 | + |
| 89 | + float *pval = reinterpret_cast<float*>(malloc(sizeof(float) * len)); |
| 90 | + for (int i = 0; i < len; i++) { |
| 91 | + pval[i] = 1.0; |
| 92 | + } |
| 93 | + Weights power{DataType::kFLOAT, pval, len}; |
| 94 | + |
| 95 | + weightMap[lname + ".scale"] = scale; |
| 96 | + weightMap[lname + ".shift"] = shift; |
| 97 | + weightMap[lname + ".power"] = power; |
| 98 | + IScaleLayer* scale_1 = network->addScale(input, ScaleMode::kCHANNEL, shift, scale, power); |
| 99 | + assert(scale_1); |
| 100 | + return scale_1; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +ILayer* convBlock(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int outch, int ksize, int s, int g, std::string lname) { |
| 105 | + Weights emptywts{DataType::kFLOAT, nullptr, 0}; |
| 106 | + int p = ksize / 2; |
| 107 | + IConvolutionLayer* conv1 = network->addConvolutionNd(input, outch, DimsHW{ksize, ksize}, weightMap[lname + ".conv.weight"], emptywts); |
| 108 | + assert(conv1); |
| 109 | + conv1->setStrideNd(DimsHW{s, s}); |
| 110 | + conv1->setPaddingNd(DimsHW{p, p}); |
| 111 | + conv1->setNbGroups(g); |
| 112 | + IScaleLayer* bn1 = addBatchNorm2d(network, weightMap, *conv1->getOutput(0), lname + ".bn", 1e-3); |
| 113 | + |
| 114 | + // hard_swish = x * hard_sigmoid |
| 115 | + auto hsig = network->addActivation(*bn1->getOutput(0), ActivationType::kHARD_SIGMOID); |
| 116 | + assert(hsig); |
| 117 | + hsig->setAlpha(1.0 / 6.0); |
| 118 | + hsig->setBeta(0.5); |
| 119 | + auto ew = network->addElementWise(*bn1->getOutput(0), *hsig->getOutput(0), ElementWiseOperation::kPROD); |
| 120 | + assert(ew); |
| 121 | + return ew; |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +int read_files_in_dir(const char *p_dir_name, std::vector<std::string> &file_names) { |
| 127 | + DIR *p_dir = opendir(p_dir_name); |
| 128 | + if (p_dir == nullptr) { |
| 129 | + return -1; |
| 130 | + } |
| 131 | + |
| 132 | + struct dirent* p_file = nullptr; |
| 133 | + while ((p_file = readdir(p_dir)) != nullptr) { |
| 134 | + if (strcmp(p_file->d_name, ".") != 0 && |
| 135 | + strcmp(p_file->d_name, "..") != 0) { |
| 136 | + //std::string cur_file_name(p_dir_name); |
| 137 | + //cur_file_name += "/"; |
| 138 | + //cur_file_name += p_file->d_name; |
| 139 | + std::string cur_file_name(p_file->d_name); |
| 140 | + file_names.push_back(cur_file_name); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + closedir(p_dir); |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +#endif |
0 commit comments