common.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "common.hpp"
  2. cv::Rect get_rect(cv::Mat& img, float bbox[4]) {
  3. float l, r, t, b;
  4. float r_w = Yolo::INPUT_W / (img.cols * 1.0);
  5. float r_h = Yolo::INPUT_H / (img.rows * 1.0);
  6. if (r_h > r_w) {
  7. l = bbox[0] - bbox[2] / 2.f;
  8. r = bbox[0] + bbox[2] / 2.f;
  9. t = bbox[1] - bbox[3] / 2.f - (Yolo::INPUT_H - r_w * img.rows) / 2;
  10. b = bbox[1] + bbox[3] / 2.f - (Yolo::INPUT_H - r_w * img.rows) / 2;
  11. l = l / r_w;
  12. r = r / r_w;
  13. t = t / r_w;
  14. b = b / r_w;
  15. } else {
  16. l = bbox[0] - bbox[2] / 2.f - (Yolo::INPUT_W - r_h * img.cols) / 2;
  17. r = bbox[0] + bbox[2] / 2.f - (Yolo::INPUT_W - r_h * img.cols) / 2;
  18. t = bbox[1] - bbox[3] / 2.f;
  19. b = bbox[1] + bbox[3] / 2.f;
  20. l = l / r_h;
  21. r = r / r_h;
  22. t = t / r_h;
  23. b = b / r_h;
  24. }
  25. return cv::Rect(round(l), round(t), round(r - l), round(b - t));
  26. }
  27. float iou(float lbox[4], float rbox[4]) {
  28. float interBox[] = {
  29. (std::max)(lbox[0] - lbox[2] / 2.f , rbox[0] - rbox[2] / 2.f), //left
  30. (std::min)(lbox[0] + lbox[2] / 2.f , rbox[0] + rbox[2] / 2.f), //right
  31. (std::max)(lbox[1] - lbox[3] / 2.f , rbox[1] - rbox[3] / 2.f), //top
  32. (std::min)(lbox[1] + lbox[3] / 2.f , rbox[1] + rbox[3] / 2.f), //bottom
  33. };
  34. if (interBox[2] > interBox[3] || interBox[0] > interBox[1])
  35. return 0.0f;
  36. float interBoxS = (interBox[1] - interBox[0])*(interBox[3] - interBox[2]);
  37. return interBoxS / (lbox[2] * lbox[3] + rbox[2] * rbox[3] - interBoxS);
  38. }
  39. bool cmp(const Yolo::Detection& a, const Yolo::Detection& b) {
  40. return a.conf > b.conf;
  41. }
  42. void nms(std::vector<Yolo::Detection>& res, float *output, float conf_thresh, float nms_thresh) {
  43. int det_size = sizeof(Yolo::Detection) / sizeof(float);
  44. std::map<float, std::vector<Yolo::Detection>> m;
  45. for (int i = 0; i < output[0] && i < Yolo::MAX_OUTPUT_BBOX_COUNT; i++) {
  46. if (output[1 + det_size * i + 4] <= conf_thresh) continue;
  47. Yolo::Detection det;
  48. memcpy(&det, &output[1 + det_size * i], det_size * sizeof(float));
  49. if (m.count(det.class_id) == 0) m.emplace(det.class_id, std::vector<Yolo::Detection>());
  50. m[det.class_id].push_back(det);
  51. }
  52. for (auto it = m.begin(); it != m.end(); it++) {
  53. //std::cout << it->second[0].class_id << " --- " << std::endl;
  54. auto& dets = it->second;
  55. std::sort(dets.begin(), dets.end(), cmp);
  56. for (size_t m = 0; m < dets.size(); ++m) {
  57. auto& item = dets[m];
  58. res.push_back(item);
  59. for (size_t n = m + 1; n < dets.size(); ++n) {
  60. if (iou(item.bbox, dets[n].bbox) > nms_thresh) {
  61. dets.erase(dets.begin() + n);
  62. --n;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. std::map<std::string, Weights> loadWeights(const std::string file) {
  69. std::cout << "Loading weights: " << file << std::endl;
  70. std::map<std::string, Weights> weightMap;
  71. // Open weights file
  72. std::ifstream input(file);
  73. assert(input.is_open() && "Unable to load weight file. please check if the .wts file path is right!!!!!!");
  74. // Read number of weight blobs
  75. int32_t count;
  76. input >> count;
  77. assert(count > 0 && "Invalid weight map file.");
  78. while (count--)
  79. {
  80. Weights wt{ DataType::kFLOAT, nullptr, 0 };
  81. uint32_t size;
  82. // Read name and type of blob
  83. std::string name;
  84. input >> name >> std::dec >> size;
  85. wt.type = DataType::kFLOAT;
  86. // Load blob
  87. uint32_t* val = reinterpret_cast<uint32_t*>(malloc(sizeof(val) * size));
  88. for (uint32_t x = 0, y = size; x < y; ++x)
  89. {
  90. input >> std::hex >> val[x];
  91. }
  92. wt.values = val;
  93. wt.count = size;
  94. weightMap[name] = wt;
  95. }
  96. return weightMap;
  97. }
  98. IScaleLayer* addBatchNorm2d(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, std::string lname, float eps) {
  99. float *gamma = (float*)weightMap[lname + ".weight"].values;
  100. float *beta = (float*)weightMap[lname + ".bias"].values;
  101. float *mean = (float*)weightMap[lname + ".running_mean"].values;
  102. float *var = (float*)weightMap[lname + ".running_var"].values;
  103. int len = weightMap[lname + ".running_var"].count;
  104. float *scval = reinterpret_cast<float*>(malloc(sizeof(float) * len));
  105. for (int i = 0; i < len; i++) {
  106. scval[i] = gamma[i] / sqrt(var[i] + eps);
  107. }
  108. Weights scale{ DataType::kFLOAT, scval, len };
  109. float *shval = reinterpret_cast<float*>(malloc(sizeof(float) * len));
  110. for (int i = 0; i < len; i++) {
  111. shval[i] = beta[i] - mean[i] * gamma[i] / sqrt(var[i] + eps);
  112. }
  113. Weights shift{ DataType::kFLOAT, shval, len };
  114. float *pval = reinterpret_cast<float*>(malloc(sizeof(float) * len));
  115. for (int i = 0; i < len; i++) {
  116. pval[i] = 1.0;
  117. }
  118. Weights power{ DataType::kFLOAT, pval, len };
  119. weightMap[lname + ".scale"] = scale;
  120. weightMap[lname + ".shift"] = shift;
  121. weightMap[lname + ".power"] = power;
  122. IScaleLayer* scale_1 = network->addScale(input, ScaleMode::kCHANNEL, shift, scale, power);
  123. assert(scale_1);
  124. return scale_1;
  125. }
  126. ILayer* convBlock(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int outch, int ksize, int s, int g, std::string lname) {
  127. Weights emptywts{ DataType::kFLOAT, nullptr, 0 };
  128. int p = ksize / 3;
  129. IConvolutionLayer* conv1 = network->addConvolutionNd(input, outch, DimsHW{ ksize, ksize }, weightMap[lname + ".conv.weight"], emptywts);
  130. assert(conv1);
  131. conv1->setStrideNd(DimsHW{ s, s });
  132. conv1->setPaddingNd(DimsHW{ p, p });
  133. conv1->setNbGroups(g);
  134. IScaleLayer* bn1 = addBatchNorm2d(network, weightMap, *conv1->getOutput(0), lname + ".bn", 1e-3);
  135. // silu = x * sigmoid
  136. auto sig = network->addActivation(*bn1->getOutput(0), ActivationType::kSIGMOID);
  137. assert(sig);
  138. auto ew = network->addElementWise(*bn1->getOutput(0), *sig->getOutput(0), ElementWiseOperation::kPROD);
  139. assert(ew);
  140. return ew;
  141. }
  142. ILayer* focus(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int inch, int outch, int ksize, std::string lname) {
  143. ISliceLayer *s1 = network->addSlice(input, Dims3{ 0, 0, 0 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
  144. ISliceLayer *s2 = network->addSlice(input, Dims3{ 0, 1, 0 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
  145. ISliceLayer *s3 = network->addSlice(input, Dims3{ 0, 0, 1 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
  146. ISliceLayer *s4 = network->addSlice(input, Dims3{ 0, 1, 1 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
  147. ITensor* inputTensors[] = { s1->getOutput(0), s2->getOutput(0), s3->getOutput(0), s4->getOutput(0) };
  148. auto cat = network->addConcatenation(inputTensors, 4);
  149. auto conv = convBlock(network, weightMap, *cat->getOutput(0), outch, ksize, 1, 1, lname + ".conv");
  150. return conv;
  151. }
  152. ILayer* bottleneck(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int c1, int c2, bool shortcut, int g, float e, std::string lname) {
  153. auto cv1 = convBlock(network, weightMap, input, (int)((float)c2 * e), 1, 1, 1, lname + ".cv1");
  154. auto cv2 = convBlock(network, weightMap, *cv1->getOutput(0), c2, 3, 1, g, lname + ".cv2");
  155. if (shortcut && c1 == c2) {
  156. auto ew = network->addElementWise(input, *cv2->getOutput(0), ElementWiseOperation::kSUM);
  157. return ew;
  158. }
  159. return cv2;
  160. }
  161. ILayer* bottleneckCSP(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int c1, int c2, int n, bool shortcut, int g, float e, std::string lname) {
  162. Weights emptywts{ DataType::kFLOAT, nullptr, 0 };
  163. int c_ = (int)((float)c2 * e);
  164. auto cv1 = convBlock(network, weightMap, input, c_, 1, 1, 1, lname + ".cv1");
  165. auto cv2 = network->addConvolutionNd(input, c_, DimsHW{ 1, 1 }, weightMap[lname + ".cv2.weight"], emptywts);
  166. ITensor *y1 = cv1->getOutput(0);
  167. for (int i = 0; i < n; i++) {
  168. auto b = bottleneck(network, weightMap, *y1, c_, c_, shortcut, g, 1.0, lname + ".m." + std::to_string(i));
  169. y1 = b->getOutput(0);
  170. }
  171. auto cv3 = network->addConvolutionNd(*y1, c_, DimsHW{ 1, 1 }, weightMap[lname + ".cv3.weight"], emptywts);
  172. ITensor* inputTensors[] = { cv3->getOutput(0), cv2->getOutput(0) };
  173. auto cat = network->addConcatenation(inputTensors, 2);
  174. IScaleLayer* bn = addBatchNorm2d(network, weightMap, *cat->getOutput(0), lname + ".bn", 1e-4);
  175. auto lr = network->addActivation(*bn->getOutput(0), ActivationType::kLEAKY_RELU);
  176. lr->setAlpha(0.1);
  177. auto cv4 = convBlock(network, weightMap, *lr->getOutput(0), c2, 1, 1, 1, lname + ".cv4");
  178. return cv4;
  179. }
  180. ILayer* C3(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int c1, int c2, int n, bool shortcut, int g, float e, std::string lname) {
  181. int c_ = (int)((float)c2 * e);
  182. auto cv1 = convBlock(network, weightMap, input, c_, 1, 1, 1, lname + ".cv1");
  183. auto cv2 = convBlock(network, weightMap, input, c_, 1, 1, 1, lname + ".cv2");
  184. ITensor *y1 = cv1->getOutput(0);
  185. for (int i = 0; i < n; i++) {
  186. auto b = bottleneck(network, weightMap, *y1, c_, c_, shortcut, g, 1.0, lname + ".m." + std::to_string(i));
  187. y1 = b->getOutput(0);
  188. }
  189. ITensor* inputTensors[] = { y1, cv2->getOutput(0) };
  190. auto cat = network->addConcatenation(inputTensors, 2);
  191. auto cv3 = convBlock(network, weightMap, *cat->getOutput(0), c2, 1, 1, 1, lname + ".cv3");
  192. return cv3;
  193. }
  194. ILayer* SPP(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int c1, int c2, int k1, int k2, int k3, std::string lname) {
  195. int c_ = c1 / 2;
  196. auto cv1 = convBlock(network, weightMap, input, c_, 1, 1, 1, lname + ".cv1");
  197. auto pool1 = network->addPoolingNd(*cv1->getOutput(0), PoolingType::kMAX, DimsHW{ k1, k1 });
  198. pool1->setPaddingNd(DimsHW{ k1 / 2, k1 / 2 });
  199. pool1->setStrideNd(DimsHW{ 1, 1 });
  200. auto pool2 = network->addPoolingNd(*cv1->getOutput(0), PoolingType::kMAX, DimsHW{ k2, k2 });
  201. pool2->setPaddingNd(DimsHW{ k2 / 2, k2 / 2 });
  202. pool2->setStrideNd(DimsHW{ 1, 1 });
  203. auto pool3 = network->addPoolingNd(*cv1->getOutput(0), PoolingType::kMAX, DimsHW{ k3, k3 });
  204. pool3->setPaddingNd(DimsHW{ k3 / 2, k3 / 2 });
  205. pool3->setStrideNd(DimsHW{ 1, 1 });
  206. ITensor* inputTensors[] = { cv1->getOutput(0), pool1->getOutput(0), pool2->getOutput(0), pool3->getOutput(0) };
  207. auto cat = network->addConcatenation(inputTensors, 4);
  208. auto cv2 = convBlock(network, weightMap, *cat->getOutput(0), c2, 1, 1, 1, lname + ".cv2");
  209. return cv2;
  210. }
  211. ILayer* SPPF(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int c1, int c2, int k, std::string lname) {
  212. int c_ = c1 / 2;
  213. auto cv1 = convBlock(network, weightMap, input, c_, 1, 1, 1, lname + ".cv1");
  214. auto pool1 = network->addPoolingNd(*cv1->getOutput(0), PoolingType::kMAX, DimsHW{ k, k });
  215. pool1->setPaddingNd(DimsHW{ k / 2, k / 2 });
  216. pool1->setStrideNd(DimsHW{ 1, 1 });
  217. auto pool2 = network->addPoolingNd(*pool1->getOutput(0), PoolingType::kMAX, DimsHW{ k, k });
  218. pool2->setPaddingNd(DimsHW{ k / 2, k / 2 });
  219. pool2->setStrideNd(DimsHW{ 1, 1 });
  220. auto pool3 = network->addPoolingNd(*pool2->getOutput(0), PoolingType::kMAX, DimsHW{ k, k });
  221. pool3->setPaddingNd(DimsHW{ k / 2, k / 2 });
  222. pool3->setStrideNd(DimsHW{ 1, 1 });
  223. ITensor* inputTensors[] = { cv1->getOutput(0), pool1->getOutput(0), pool2->getOutput(0), pool3->getOutput(0) };
  224. auto cat = network->addConcatenation(inputTensors, 4);
  225. auto cv2 = convBlock(network, weightMap, *cat->getOutput(0), c2, 1, 1, 1, lname + ".cv2");
  226. return cv2;
  227. }
  228. std::vector<std::vector<float>> getAnchors(std::map<std::string, Weights>& weightMap, std::string lname) {
  229. std::vector<std::vector<float>> anchors;
  230. Weights wts = weightMap[lname + ".anchor_grid"];
  231. int anchor_len = Yolo::CHECK_COUNT * 2;
  232. for (int i = 0; i < wts.count / anchor_len; i++) {
  233. auto *p = (const float*)wts.values + i * anchor_len;
  234. std::vector<float> anchor(p, p + anchor_len);
  235. anchors.push_back(anchor);
  236. }
  237. return anchors;
  238. }
  239. IPluginV2Layer* addYoLoLayer(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, std::string lname, std::vector<IConvolutionLayer*> dets) {
  240. auto creator = getPluginRegistry()->getPluginCreator("YoloLayer_TRT", "1");
  241. auto anchors = getAnchors(weightMap, lname);
  242. PluginField plugin_fields[2];
  243. int netinfo[4] = {Yolo::CLASS_NUM, Yolo::INPUT_W, Yolo::INPUT_H, Yolo::MAX_OUTPUT_BBOX_COUNT};
  244. plugin_fields[0].data = netinfo;
  245. plugin_fields[0].length = 4;
  246. plugin_fields[0].name = "netinfo";
  247. plugin_fields[0].type = PluginFieldType::kFLOAT32;
  248. int scale = 8;
  249. std::vector<Yolo::YoloKernel> kernels;
  250. for (size_t i = 0; i < anchors.size(); i++) {
  251. Yolo::YoloKernel kernel;
  252. kernel.width = Yolo::INPUT_W / scale;
  253. kernel.height = Yolo::INPUT_H / scale;
  254. memcpy(kernel.anchors, &anchors[i][0], anchors[i].size() * sizeof(float));
  255. kernels.push_back(kernel);
  256. scale *= 2;
  257. }
  258. plugin_fields[1].data = &kernels[0];
  259. plugin_fields[1].length = kernels.size();
  260. plugin_fields[1].name = "kernels";
  261. plugin_fields[1].type = PluginFieldType::kFLOAT32;
  262. PluginFieldCollection plugin_data;
  263. plugin_data.nbFields = 2;
  264. plugin_data.fields = plugin_fields;
  265. IPluginV2 *plugin_obj = creator->createPlugin("yololayer", &plugin_data);
  266. std::vector<ITensor*> input_tensors;
  267. for (auto det: dets) {
  268. input_tensors.push_back(det->getOutput(0));
  269. }
  270. auto yolo = network->addPluginV2(&input_tensors[0], input_tensors.size(), *plugin_obj);
  271. return yolo;
  272. }