{"id":3193,"date":"2022-03-29T22:00:12","date_gmt":"2022-03-29T14:00:12","guid":{"rendered":"http:\/\/139.9.1.231\/?p=3193"},"modified":"2022-06-07T21:57:39","modified_gmt":"2022-06-07T13:57:39","slug":"yolov3-tiny","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/29\/yolov3-tiny\/","title":{"rendered":"yolov3 -tiny \u7f51\u7edc\u5b9e\u73b0\u548c\u6e90\u7801\u5206\u6790"},"content":{"rendered":"\n<p class=\"has-light-pink-background-color has-background\">\u6458\u81ea\uff1a<a href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\">https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746<\/a><\/p>\n\n\n\n\n\n<h1>\u76f8\u5173\u63a8\u8350<\/h1>\n\n\n\n<p>\u7f51\u4e0a\u7684\u8d44\u6e90\u5f88\u591a\uff0c\u4e5f\u6709\u5f88\u591a\u535a\u4e3b\u7684\u539f\u7406\u8bb2\u89e3\u3002\u5728\u8fd9\u91cc\u63a8\u8350\u51e0\u4e2a\u8d44\u6e90\uff1a<br>darknet\u5b98\u7f51\uff1a<a href=\"https:\/\/pjreddie.com\/darknet\/yolo\/ linux\u7cfb\u7edf\">https:\/\/pjreddie.com\/darknet\/yolo\/ linux\u7cfb\u7edf<\/a><br>github\u4e0a\u57fa\u4e8eVS\u7684\u5de5\u7a0b\uff1a<a href=\"https:\/\/github.com\/AlexeyAB\/darknet\">https:\/\/github.com\/AlexeyAB\/darknet<\/a><br>github\u4e0a\u5e26\u6709\u6ce8\u91ca\u7684\u5de5\u7a0b\uff1a<a href=\"https:\/\/github.com\/hgpvision\/darknet\">https:\/\/github.com\/hgpvision\/darknet<\/a><\/p>\n\n\n\n<h1><a href=\"https:\/\/so.csdn.net\/so\/search?q=yolov3&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">yolov3<\/a>-tiny \u539f\u7406<\/h1>\n\n\n\n<p>Yolo\u7b97\u6cd5\u91c7\u7528\u4e00\u4e2a\u5355\u72ec\u7684CNN\u6a21\u578b\u5b9e\u73b0end-to-end\u7684\u76ee\u6807\u68c0\u6d4b\uff0c\u9996\u5148\u5c06\u8f93\u5165\u56fe\u7247resize\u5230448&#215;448\uff0c\u7136\u540e\u9001\u5165CNN\u7f51\u7edc\uff0c\u6700\u540e\u5904\u7406\u7f51\u7edc\u9884\u6d4b\u7ed3\u679c\u5f97\u5230\u68c0\u6d4b\u7684\u76ee\u6807\u3002<br>YOLO \u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f\u5229\u7528\u6574\u5f20\u56fe\u4f5c\u4e3a\u7f51\u7edc\u7684\u8f93\u5165\uff0c\u76f4\u63a5\u5728\u8f93\u51fa\u5c42\u56de\u5f52 bounding box\uff08\u8fb9\u754c\u6846\uff09 \u7684\u4f4d\u7f6e\u53ca\u5176\u6240\u5c5e\u7684\u7c7b\u522b\u3002\u5c06\u4e00\u5e45\u56fe\u50cf\u5206\u6210 SxS \u4e2a\u7f51\u683c\uff08grid cell\uff09\uff0c\u5982\u679c\u67d0\u4e2a object \u7684\u4e2d\u5fc3\u843d\u5728\u8fd9\u4e2a\u7f51\u683c\u4e2d\uff0c\u5219\u8fd9\u4e2a\u7f51\u683c\u5c31\u8d1f\u8d23\u9884\u6d4b\u8fd9\u4e2a object\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"697\" height=\"673\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-83.png\" alt=\"\" class=\"wp-image-3196\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-83.png 697w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-83-300x290.png 300w\" sizes=\"(max-width: 697px) 100vw, 697px\" \/><\/figure>\n\n\n\n<p>\u6bcf\u4e2a bounding box \u8981\u9884\u6d4b (x, y, w, h) \u548c confidence \u51715\u4e2a\u503c\uff0c\u6bcf\u4e2a\u7f51\u683c\u8fd8\u8981\u9884\u6d4b\u4e00\u4e2a\u7c7b\u522b\u4fe1\u606f\uff0c\u8bb0\u4e3a C \u7c7b\u3002\u5219 SxS\u4e2a \u7f51\u683c\uff0c\u6bcf\u4e2a\u7f51\u683c\u8981\u9884\u6d4b B \u4e2a bounding box\uff0c \u6bcf\u4e2abox\u4e2d\u90fd\u6709 C \u4e2a classes\u5bf9\u5e94\u7684\u6982\u7387\u503c\u3002\u8f93\u51fa\u5c31\u662f S x S x B x(5+C) \u7684\u4e00\u4e2a tensor\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1aclass \u4fe1\u606f\u662f\u9488\u5bf9\u6bcf\u4e2a\u7f51\u683c\u7684\uff0cconfidence \u4fe1\u606f\u662f\u9488\u5bf9\u6bcf\u4e2a bounding box \u7684\u3002<\/p>\n\n\n\n<p>yolov3-tiny\u4e2d\uff0c\u5171\u6709\u4e24\u4e2a\u8f93\u51fa\u5c42\uff08yolo\u5c42\uff09\uff0c\u5206\u522b\u4e3a13&#215;13\u548c26&#215;26\uff0c\u6bcf\u4e2a\u7f51\u683c\u53ef\u4ee5\u9884\u6d4b3\u4e2abounding box\uff0c\u5171\u670980\u4e2a\u5206\u7c7b\u6570\u3002\u6240\u4ee5\u6700\u540e\u7684yolo\u5c42\u7684\u5c3a\u5bf8\u4e3a\uff1a13x13x255\u548c26x26x255\u3002<br>yolov3-tiny\u7f51\u7edc\u5c42\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"749\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-84-1024x749.png\" alt=\"\" class=\"wp-image-3197\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-84-1024x749.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-84-300x219.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-84-768x561.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-84.png 1056w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/img-blog.csdnimg.cn\/20200415145059824.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FsYW5nYWl4aWFveGlhbw==,size_16,color_FFFFFF,t_70\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0cyolov3-tiny\u5171\u670923\u5c42\u7f51\u7edc\uff0c\u5176\u4e2d\u5305\u542b\u4e94\u79cd\u4e0d\u540c\u7684\u7f51\u7edc\u5c42\uff1a\u5377\u79ef\u5c42<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\" target=\"_blank\">convolutional<\/a>(13\u4e2a)\uff0c\u6c60\u5316\u5c42<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\" target=\"_blank\">maxpool<\/a>(6\u4e2a)\uff0c\u8def\u7531\u5c42<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\" target=\"_blank\">route<\/a>(2\u4e2a)\uff0c\u4e0a\u91c7\u6837\u5c42<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\" target=\"_blank\">upsample<\/a>(1\u4e2a)\uff0c\u8f93\u51fa\u5c42<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\" target=\"_blank\">yolo<\/a>(2\u4e2a)\u3002Yolov3-tiny\u4e2d\uff0c\u9664\u4e86Yolo\u5c42\u4e4b\u524d\u7684\u90a3\u4e2a\u5377\u79ef\u5c42\uff0c\u6bcf\u4e2a\u5377\u79ef\u5c42\u4e4b\u540e\u90fd\u6709BN\u5c42,\u4e14\u6bcf\u4e2a\u5377\u79ef\u5c42\u4e4b\u540e\u90fd\u6709\u6fc0\u6d3b\u51fd\u6570LEAKY\uff08yolo\u5c42\u4e4b\u524d\u662flinear\uff09\u3002<\/p>\n\n\n\n<h1>yolov3-tiny&nbsp;<a href=\"https:\/\/so.csdn.net\/so\/search?q=%E6%BA%90%E7%A0%81&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">\u6e90\u7801<\/a>\u5206\u6790<\/h1>\n\n\n\n<h2><a><\/a><a><\/a>\u914d\u7f6e\u7f51\u7edc\u7ed3\u6784<\/h2>\n\n\n\n<p>yolov3-tiny\u524d\u5411\u4f20\u64ad\u4e3b\u8981\u5728detector.c\u4e2d\u7684test_detector\u51fd\u6570\u4e2d\u5b8c\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/** \u672c\u51fd\u6570\u662f\u68c0\u6d4b\u6a21\u578b\u7684\u4e00\u4e2a\u524d\u5411\u63a8\u7406\u6d4b\u8bd5\u51fd\u6570.\n* @param datacfg       \u6570\u636e\u96c6\u4fe1\u606f\u6587\u4ef6\u8def\u5f84\uff08\u4e5f\u5373cfg\/*.data\u6587\u4ef6\uff09\uff0c\u6587\u4ef6\u4e2d\u5305\u542b\u6709\u5173\u6570\u636e\u96c6\u7684\u4fe1\u606f\uff0c\u6bd4\u5982cfg\/coco.data\n* @param cfgfile       \u7f51\u7edc\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\uff08\u4e5f\u5373cfg\/*.cfg\u6587\u4ef6\uff09\uff0c\u5305\u542b\u4e00\u4e2a\u7f51\u7edc\u6240\u6709\u7684\u7ed3\u6784\u53c2\u6570\uff0c\u6bd4\u5982cfg\/yolo.cfg\n* @param weightfile    \u5df2\u7ecf\u8bad\u7ec3\u597d\u7684\u7f51\u7edc\u6743\u91cd\u6587\u4ef6\u8def\u5f84\uff0c\u6bd4\u5982darknet\u7f51\u7ad9\u4e0a\u4e0b\u8f7d\u7684yolo.weights\u6587\u4ef6\n* @param filename      \u5f85\u8fdb\u884c\u68c0\u6d4b\u7684\u56fe\u7247\u8def\u5f84\uff08\u5355\u5f20\u56fe\u7247\uff09\n* @param thresh        \u9608\u503c\uff0c\u7c7b\u522b\u68c0\u6d4b\u6982\u7387\u5927\u4e8e\u8be5\u9608\u503c\u624d\u8ba4\u4e3a\u5176\u68c0\u6d4b\u7ed3\u679c\u6709\u6548\n* @param hier_thresh\n* @param outfile\n* @param fullscreen\n* @details \u8be5\u51fd\u6570\u4e3a\u4e00\u4e2a\u524d\u5411\u63a8\u7406\u6d4b\u8bd5\u51fd\u6570\uff0c\u4e0d\u5305\u62ec\u8bad\u7ec3\u8fc7\u7a0b\uff0c\u56e0\u6b64\u5982\u679c\u8981\u4f7f\u7528\u8be5\u51fd\u6570\uff0c\u5fc5\u987b\u63d0\u524d\u8bad\u7ec3\u597d\u7f51\u7edc\uff0c\u5e76\u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u7f51\u7edc\u53c2\u6570\u6587\u4ef6\uff0c\n*          \u8fd9\u4e9b\u6587\u4ef6\u53ef\u4ee5\u5728\u4f5c\u8005\u7f51\u7ad9\u4e0a\u6839\u636e\u4f5c\u8005\u7684\u63d0\u793a\u4e0b\u8f7d\u5230\u3002\u672c\u51fd\u6570\u7531darknet.c\u4e2d\u7684\u4e3b\u51fd\u6570\u8c03\u7528\uff0c\u4e25\u683c\u6765\u8bf4\uff0c\u672c\u6587\u4ef6\u4e0d\u5e94\u7eb3\u5165darknet\u7f51\u7edc\u7ed3\u6784\u6587\u4ef6\u5939\u4e2d\uff0c\n*          \u5176\u53ea\u662f\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\uff0c\u6216\u8005\u8bf4\u662f\u4e00\u4e2aexample\uff0c\u5e94\u8be5\u653e\u5165\u5230example\u6587\u4ef6\u5939\u4e2d\uff08\u65b0\u7248\u7684darknet\u5df2\u7ecf\u8fd9\u6837\u505a\u4e86\uff0c\u53ef\u4ee5\u5728github\u4e0a\u67e5\u770b\uff09\u3002\n*          \u672c\u51fd\u6570\u7684\u6d41\u7a0b\u4e3a\uff1a.\n*\/<\/em>\nvoid test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh,\n    float hier_thresh, int dont_show, int ext_output, int save_labels, char *outfile, int letter_box, int benchmark_layers)\n{\n\t<em>\/\/ \u4ece\u6307\u5b9a\u6570\u636e\u6587\u4ef6datacfg\uff08.data\u6587\u4ef6\uff09\u4e2d\u8bfb\u5165\u6570\u636e\u4fe1\u606f\uff08\u6d4b\u8bd5\u3001\u8bad\u7ec3\u6570\u636e\u4fe1\u606f\uff09\u5230options\u4e2d<\/em>\n\t<em>\/\/ options\u662flist\u7c7b\u578b\u6570\u636e\uff0c\u5176\u4e2d\u7684node\u5305\u542b\u7684void\u6307\u9488\u5177\u4f53\u662fkvp\u6570\u636e\u7c7b\u578b\uff0c\u5177\u6709\u952e\u503c\u548c\u503c\uff08\u7c7b\u4f3cC++\u4e2d\u7684Map\uff09<\/em>\n    list *options = read_data_cfg(datacfg);\n\t<em>\/\/ \u83b7\u53d6\u6570\u636e\u96c6\u7684\u540d\u79f0\uff08\u5305\u62ec\u8def\u5f84\uff09\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\"names\"\u8868\u660e\u8981\u4eceoptions\u4e2d\u83b7\u53d6\u6240\u7528\u6570\u636e\u96c6\u7684\u540d\u79f0\u4fe1\u606f\uff08\u5982names = data\/coco.names\uff09<\/em>\n    char *name_list = option_find_str(options, \"names\", \"data\/names.list\");\n    int names_size = 0;\n\t<em>\/\/ \u4ecedata\/**.names\u4e2d\u8bfb\u53d6\u7269\u4f53\u540d\u79f0\/\u6807\u7b7e\u4fe1\u606f<\/em>\n    char **names = get_labels_custom(name_list, &amp;names_size); <em>\/\/get_labels(name_list);<\/em>\n\t\n    <em>\/\/ \u52a0\u8f7ddata\/labels\/\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u7684\u5b57\u7b26\u6807\u7b7e\u56fe\u7247<\/em>\n    image **alphabet = load_alphabet();\n\n    network net = parse_network_cfg_custom(cfgfile, 1, 1); <em>\/\/ set batch=1  \u914d\u7f6e\u5404\u7f51\u7edc\u5c42\u53c2\u6570\uff0c\u91cd\u8981<\/em>\n<\/code><\/pre>\n\n\n\n<p>\u5728parser.c\u4e2d\u7684parse_network_cfg_custom\u51fd\u6570\u4e2d\uff0c\u6839\u636eyolov3-tiny.cfg\u6587\u4ef6\u5bf9\u7f51\u7edc\u7ed3\u6784\u8fdb\u884c\u914d\u7f6e\uff0c\u660e\u786e\u5404\u5c42\u7f51\u7edc\u7684\u7c7b\u578b\u3001\u8f93\u5165\u8f93\u51fa\u901a\u9053\u6570\u3001\u56fe\u50cf\u5c3a\u5bf8\u3001\u5377\u79ef\u6838\u5927\u5c0f\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/\u914d\u7f6e\u5404\u7f51\u7edc\u5c42\u53c2\u6570<\/em>\nnetwork parse_network_cfg_custom(char *filename, int batch, int time_steps)\n{\n\t<em>\/\/ \u4ece\u795e\u7ecf\u7f51\u7edc\u7ed3\u6784\u53c2\u6570\u6587\u4ef6\u4e2d\u8bfb\u5165\u6240\u6709\u795e\u7ecf\u7f51\u7edc\u5c42\u7684\u7ed3\u6784\u53c2\u6570\uff0c\u5b58\u50a8\u5230sections\u4e2d\uff0c<\/em>\n\t<em>\/\/ sections\u7684\u6bcf\u4e2anode\u5305\u542b\u4e00\u5c42\u795e\u7ecf\u7f51\u7edc\u7684\u6240\u6709\u7ed3\u6784\u53c2\u6570<\/em>\n    list *sections = read_cfg(filename);\n\t<em>\/\/ \u83b7\u53d6sections\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\uff0c\u53ef\u4ee5\u67e5\u770b\u4e00\u4e0bcfg\/***.cfg\u6587\u4ef6\uff0c\u5176\u5b9e\u7b2c\u4e00\u5757\u53c2\u6570\uff08\u4ee5&#091;net]\u5f00\u5934\uff09\u4e0d\u662f\u67d0\u5c42\u795e\u7ecf\u7f51\u7edc\u7684\u53c2\u6570\uff0c<\/em>\n\t<em>\/\/ \u800c\u662f\u5173\u4e8e\u6574\u4e2a\u7f51\u7edc\u7684\u4e00\u4e9b\u901a\u7528\u53c2\u6570\uff0c\u6bd4\u5982\u5b66\u4e60\u7387\uff0c\u8870\u51cf\u7387\uff0c\u8f93\u5165\u56fe\u50cf\u5bbd\u9ad8\uff0cbatch\u5927\u5c0f\u7b49\uff0c<\/em>\n\t<em>\/\/ \u5177\u4f53\u7684\u5173\u4e8e\u67d0\u4e2a\u7f51\u7edc\u5c42\u7684\u53c2\u6570\u662f\u4ece\u7b2c\u4e8c\u5757\u5f00\u59cb\u7684\uff0c\u5982&#091;convolutional],&#091;maxpool]...\uff0c<\/em>\n\t<em>\/\/ \u8fd9\u4e9b\u5c42\u5e76\u6ca1\u6709\u7f16\u53f7\uff0c\u53ea\u8bf4\u660e\u4e86\u5c42\u7684\u5c5e\u6027\uff0c\u4f46\u5c42\u7684\u53c2\u6570\u90fd\u662f\u6309\u987a\u5e8f\u5728\u6587\u4ef6\u4e2d\u6392\u597d\u7684\uff0c\u8bfb\u5165\u65f6\uff0c<\/em>\n\t<em>\/\/ sections\u94fe\u8868\u4e0a\u7684\u987a\u5e8f\u5c31\u662f\u6587\u4ef6\u4e2d\u7684\u6392\u5217\u987a\u5e8f\u3002<\/em>\n    node *n = sections-&gt;front;\n    if(!n) error(\"Config file has no sections\");\n\t<em>\/\/ \u521b\u5efa\u7f51\u7edc\u7ed3\u6784\u5e76\u52a8\u6001\u5206\u914d\u5185\u5b58\uff1a\u8f93\u5165\u7f51\u7edc\u5c42\u6570\u4e3asections-&gt;size - 1\uff0csections\u7684\u7b2c\u4e00\u6bb5\u4e0d\u662f\u7f51\u7edc\u5c42\uff0c\u800c\u662f\u901a\u7528\u7f51\u7edc\u53c2\u6570<\/em>\n    network net = make_network(sections-&gt;size - 1);\n\t<em>\/\/ \u6240\u7528\u663e\u5361\u7684\u5361\u53f7\uff08gpu_index\u5728cuda.c\u4e2d\u7528extern\u5173\u952e\u5b57\u58f0\u660e\uff09<\/em>\n\t<em>\/\/ \u5728\u8c03\u7528parse_network_cfg()\u4e4b\u524d\uff0c\u4f7f\u7528\u4e86cuda_set_device()\u8bbe\u7f6e\u4e86gpu_index\u7684\u503c\u53f7\u4e3a\u5f53\u524d\u6d3b\u8dc3GPU\u5361\u53f7<\/em>\n    net.gpu_index = gpu_index;\n\t<em>\/\/ size_params\u7ed3\u6784\u4f53\u5143\u7d20\u4e0d\u542b\u6307\u9488\u53d8\u91cf<\/em>\n    size_params params;\n\n    if (batch &gt; 0) params.train = 0;    <em>\/\/ allocates memory for Detection only<\/em>\n    else params.train = 1;              <em>\/\/ allocates memory for Detection &amp; Training<\/em>\n\n    section *s = (section *)n-&gt;val;\n    list *options = s-&gt;options;\n    if(!is_network(s)) error(\"First section must be &#091;net] or &#091;network]\");\n    parse_net_options(options, &amp;net);\n\n#ifdef GPU\n    printf(\"net.optimized_memory = %d \\n\", net.optimized_memory);\n    if (net.optimized_memory &gt;= 2 &amp;&amp; params.train) {\n        pre_allocate_pinned_memory((size_t)1024 * 1024 * 1024 * 8);   <em>\/\/ pre-allocate 8 GB CPU-RAM for pinned memory<\/em>\n    }\n#endif  <em>\/\/ GPU<\/em>\n\n    params.h = net.h;\n    params.w = net.w;\n    params.c = net.c;\n    params.inputs = net.inputs;\n    if (batch &gt; 0) net.batch = batch;\n    if (time_steps &gt; 0) net.time_steps = time_steps;\n    if (net.batch &lt; 1) net.batch = 1;\n    if (net.time_steps &lt; 1) net.time_steps = 1;\n    if (net.batch &lt; net.time_steps) net.batch = net.time_steps;\n    params.batch = net.batch;\n    params.time_steps = net.time_steps;\n    params.net = net;\n    printf(\"mini_batch = %d, batch = %d, time_steps = %d, train = %d \\n\", net.batch, net.batch * net.subdivisions, net.time_steps, params.train);\n\n    int avg_outputs = 0;\n    float bflops = 0;\n    size_t workspace_size = 0;\n    size_t max_inputs = 0;\n    size_t max_outputs = 0;\n    n = n-&gt;next;\n    int count = 0;\n    free_section(s);\n\n\t<em>\/\/ \u6b64\u5904stderr\u4e0d\u662f\u9519\u8bef\u63d0\u793a\uff0c\u800c\u662f\u8f93\u51fa\u7ed3\u679c\u63d0\u793a\uff0c\u63d0\u793a\u7f51\u7edc\u7ed3\u6784<\/em>\n    fprintf(stderr, \"   layer   filters  size\/strd(dil)      input                output\\n\");\n    while(n){\n        params.index = count;\n        fprintf(stderr, \"%4d \", count);\n        s = (section *)n-&gt;val;\n        options = s-&gt;options;\n\t\t<em>\/\/ \u5b9a\u4e49\u7f51\u7edc\u5c42<\/em>\n        layer l = { (LAYER_TYPE)0 };\n\t\t<em>\/\/ \u83b7\u53d6\u7f51\u7edc\u5c42\u7684\u7c7b\u522b<\/em>\n\n        LAYER_TYPE lt = string_to_layer_type(s-&gt;type);\n\t\t\n\t\t<em>\/\/\u901a\u8fc7\u8bfb\u53d6\u7f51\u7edc\u7c7b\u578b\uff0c\u4ece\u800c\u914d\u7f6e\u5404\u7f51\u7edc\u5c42\u7684\u53c2\u6570<\/em>\n        if(lt == CONVOLUTIONAL){<em>\/\/yolov3-tiny  \u5377\u79ef\u5c42  13\u5c42<\/em>\n            l = parse_convolutional(options, params);\n        }else if(lt == LOCAL){\n            l = parse_local(options, params);\n        }else if(lt == ACTIVE){\n            l = parse_activation(options, params);\n        }else if(lt == RNN){\n            l = parse_rnn(options, params);\n        }else if(lt == GRU){\n            l = parse_gru(options, params);\n        }else if(lt == LSTM){\n            l = parse_lstm(options, params);\n        }else if (lt == CONV_LSTM) {\n            l = parse_conv_lstm(options, params);\n        }else if(lt == CRNN){\n            l = parse_crnn(options, params);\n        }else if(lt == CONNECTED){\n            l = parse_connected(options, params);\n        }else if(lt == CROP){\n            l = parse_crop(options, params);\n        }else if(lt == COST){\n            l = parse_cost(options, params);\n            l.keep_delta_gpu = 1;\n        }else if(lt == REGION){\n            l = parse_region(options, params);\n            l.keep_delta_gpu = 1;\n        }else if (lt == YOLO) {<em>\/\/yolov3-tiny YOLO\u5c42  \u4e24\u5c42<\/em>\n            l = parse_yolo(options, params);\n            l.keep_delta_gpu = 1;\n        }else if (lt == GAUSSIAN_YOLO) {\n            l = parse_gaussian_yolo(options, params);\n            l.keep_delta_gpu = 1;\n        }else if(lt == DETECTION){\n            l = parse_detection(options, params);\n        }else if(lt == SOFTMAX){\n            l = parse_softmax(options, params);\n            net.hierarchy = l.softmax_tree;\n            l.keep_delta_gpu = 1;\n        }else if(lt == NORMALIZATION){\n            l = parse_normalization(options, params);\n        }else if(lt == BATCHNORM){\n            l = parse_batchnorm(options, params);\n        }else if(lt == MAXPOOL){<em>\/\/yolov3-tiny \u6c60\u5316\u5c42 maxpool  6\u5c42<\/em>\n            l = parse_maxpool(options, params);\n        }else if (lt == LOCAL_AVGPOOL) {\n            l = parse_local_avgpool(options, params);\n        }else if(lt == REORG){\n            l = parse_reorg(options, params);        }\n        else if (lt == REORG_OLD) {\n            l = parse_reorg_old(options, params);\n        }else if(lt == AVGPOOL){\n            l = parse_avgpool(options, params);\n        }else if(lt == ROUTE){<em>\/\/yolov3-tiny \u8def\u7531\u5c42 2\u5c42<\/em>\n            l = parse_route(options, params);\n            int k;\n            for (k = 0; k &lt; l.n; ++k) {\n                net.layers&#091;l.input_layers&#091;k]].use_bin_output = 0;\n                net.layers&#091;l.input_layers&#091;k]].keep_delta_gpu = 1;\n            }\n        }else if (lt == UPSAMPLE) {<em>\/\/yolov3-tiny \u4e0a\u91c7\u6837\u5c42 1\u5c42<\/em>\n            l = parse_upsample(options, params, net);\n        }else if(lt == SHORTCUT){\n            l = parse_shortcut(options, params, net);\n            net.layers&#091;count - 1].use_bin_output = 0;\n            net.layers&#091;l.index].use_bin_output = 0;\n            net.layers&#091;l.index].keep_delta_gpu = 1;\n        }else if (lt == SCALE_CHANNELS) {\n            l = parse_scale_channels(options, params, net);\n            net.layers&#091;count - 1].use_bin_output = 0;\n            net.layers&#091;l.index].use_bin_output = 0;\n            net.layers&#091;l.index].keep_delta_gpu = 1;\n        }\n        else if (lt == SAM) {\n            l = parse_sam(options, params, net);\n            net.layers&#091;count - 1].use_bin_output = 0;\n            net.layers&#091;l.index].use_bin_output = 0;\n            net.layers&#091;l.index].keep_delta_gpu = 1;\n        }else if(lt == DROPOUT){\n            l = parse_dropout(options, params);\n            l.output = net.layers&#091;count-1].output;\n            l.delta = net.layers&#091;count-1].delta;\n            .........\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u4e0b\u8f7d\u6743\u91cd\u6587\u4ef6<\/h2>\n\n\n\n<p>\u5728parser.c\u7684load_weights_upto\u4e2d\uff0c\u6839\u636e\u5377\u79ef\u5c42\u7684\u7f51\u7edc\u914d\u7f6e\uff0c\u5f00\u59cb\u4e0b\u8f7d\u8bfb\u53d6\u5404\u5c42\u7684\u6743\u91cd\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/\u8bfb\u53d6\u6743\u91cd\u6587\u4ef6\u51fd\u6570<\/em>\nvoid load_weights_upto(network *net, char *filename, int cutoff)<em>\/\/cutoff = net-&gt;n<\/em>\n{\n#ifdef GPU\n    if(net-&gt;gpu_index &gt;= 0){\n        cuda_set_device(net-&gt;gpu_index);\n    }\n#endif\n    fprintf(stderr, \"Loading weights from %s...\\n\", filename);\n    fflush(stdout);\n    FILE *fp = fopen(filename, \"rb\");\n    if(!fp) file_error(filename);\n\n    int major;\n    int minor;\n    int revision;\n    fread(&amp;major, sizeof(int), 1, fp);<em>\/\/\u8bfb\u53d6\u4e00\u4e2a4\u5b57\u8282\u7684\u6570\u636e<\/em>\n    fread(&amp;minor, sizeof(int), 1, fp);<em>\/\/\u8bfb\u53d6\u4e00\u4e2a4\u5b57\u8282\u7684\u6570\u636e<\/em>\n    fread(&amp;revision, sizeof(int), 1, fp);<em>\/\/\u8bfb\u53d6\u4e00\u4e2a4\u5b57\u8282\u7684\u6570\u636e<\/em>\n\tprintf(\"the size of int in x64 is %d bytes,attention!!!\\n\", sizeof(int));<em>\/\/x86 x64: 4<\/em>\n\tprintf(\"major ,minor,revision of weight is %d, %d ,%d\\n\", major, minor, revision);<em>\/\/0.2.0<\/em>\n    if ((major * 10 + minor) &gt;= 2) {<em>\/\/\u8fd0\u884c\u8fd9\u4e00\u90e8\u5206<\/em>\n        printf(\"\\n seen 64\");\n        uint64_t iseen = 0;\n        fread(&amp;iseen, sizeof(uint64_t), 1, fp);<em>\/\/\u8bfb\u53d6\u4e00\u4e2a8\u5b57\u8282\u7684\u6570\u636e<\/em>\n\t\tprintf(\"the size of uint64_t is %d\\n\", sizeof(uint64_t));\n        *net-&gt;seen = iseen;\n    }\n    else {\n        printf(\"\\n seen 32\");\n        uint32_t iseen = 0;\n        fread(&amp;iseen, sizeof(uint32_t), 1, fp);\n        *net-&gt;seen = iseen;\n    }\n    *net-&gt;cur_iteration = get_current_batch(*net);\n    printf(\", trained: %.0f K-images (%.0f Kilo-batches_64) \\n\", (float)(*net-&gt;seen \/ 1000), (float)(*net-&gt;seen \/ 64000));\n    int transpose = (major &gt; 1000) || (minor &gt; 1000);\n\n    int i;\n    for(i = 0; i &lt; net-&gt;n &amp;&amp; i &lt; cutoff; ++i){<em>\/\/cutoff = net-&gt;n<\/em>\n        layer l = net-&gt;layers&#091;i];\n        if (l.dontload) continue;<em>\/\/always 0\t\t\u8df3\u8fc7\u4e4b\u540e\u7684\u5faa\u73af\u4f53\uff0c\u76f4\u63a5\u8fd0\u884c++i<\/em>\n        if(l.type == CONVOLUTIONAL &amp;&amp; l.share_layer == NULL){ <em>\/\/\u53ea\u8fd0\u884c\u8fd9\u4e00\u4e2a\u5206\u652f\u7684\u4ee3\u7801<\/em>\n            load_convolutional_weights(l, fp);\n\t\t\t<em>\/\/printf(\"network layer &#091;%d] is CONVOLUTIONAL \\n\",i);<\/em>\n        }\n        .......\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8bfb\u53d6yolov3-tiny\u5404\u5c42\u6743\u91cd\u6587\u4ef6\u524d\uff0c\u5148\u8bfb\u53d64\u4e2a\u548c\u8bad\u7ec3\u6709\u5173\u7684\u53c2\u6570\uff1amajor\uff0cminor, revision\u548ciseen\u3002\u5728\u524d\u5411\u4f20\u64ad\u7684\u5de5\u7a0b\u5f53\u4e2d\uff0c\u5e76\u6ca1\u6709\u5b9e\u9645\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<p>parser.c\u4e2d\u7684load_convolutional_weights\u51fd\u6570\uff0c\u5177\u4f53\u6267\u884c\u5bf9yolov3-tiny\u6743\u91cd\u6587\u4ef6\u7684\u4e0b\u8f7d\uff0c\u5305\u62ec\u8282\u70b9\u53c2\u6570weight\uff0c\u504f\u7f6e\u53c2\u6570bias\u548c\u6279\u91cf\u5f52\u4e00\u5316\u53c2\u6570BN\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void load_convolutional_weights(layer l, FILE *fp)\n{\n\tstatic int flipped_num;\n    if(l.binary){\n        <em>\/\/load_convolutional_weights_binary(l, fp);<\/em>\n        <em>\/\/return;<\/em>\n    }\n    int num = l.nweights;\n\t<em>\/\/int num = l.n*l.c*l.size*l.size;\/\/l.n \u8f93\u51fa\u7684\u5c42\u6570 l.c\u8f93\u5165\u7684\u5c42\u6570 <\/em>\n    int read_bytes;\n    read_bytes = fread(l.biases, sizeof(float), l.n, fp);<em>\/\/\u8bfb\u53d6\u504f\u7f6e\u53c2\u6570 l.n\u4e2afloat\u6570\u636e<\/em>\n    if (read_bytes &gt; 0 &amp;&amp; read_bytes &lt; l.n) printf(\"\\n Warning: Unexpected end of wights-file! l.biases - l.index = %d \\n\", l.index);\n    <em>\/\/fread(l.weights, sizeof(float), num, fp); \/\/ as in connected layer<\/em>\n    if (l.batch_normalize &amp;&amp; (!l.dontloadscales)){\n        read_bytes = fread(l.scales, sizeof(float), l.n, fp);<em>\/\/\u8bfb\u53d6batch normalize \u53c2\u6570  l.n\u4e2afloat\u6570\u636e<\/em>\n        if (read_bytes &gt; 0 &amp;&amp; read_bytes &lt; l.n) printf(\"\\n Warning: Unexpected end of wights-file! l.scales - l.index = %d \\n\", l.index);\n        read_bytes = fread(l.rolling_mean, sizeof(float), l.n, fp);<em>\/\/\u8bfb\u53d6batch normalize \u53c2\u6570  l.n\u4e2afloat\u6570\u636e<\/em>\n        if (read_bytes &gt; 0 &amp;&amp; read_bytes &lt; l.n) printf(\"\\n Warning: Unexpected end of wights-file! l.rolling_mean - l.index = %d \\n\", l.index);\n        read_bytes = fread(l.rolling_variance, sizeof(float), l.n, fp);<em>\/\/\u8bfb\u53d6batch normalize \u53c2\u6570  l.n\u4e2afloat\u6570\u636e<\/em>\n        if (read_bytes &gt; 0 &amp;&amp; read_bytes &lt; l.n) printf(\"\\n Warning: Unexpected end of wights-file! l.rolling_variance - l.index = %d \\n\", l.index);\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u5c06\u6743\u91cd\u53c2\u6570\u6279\u91cf\u5f52\u4e00\u5316<\/h2>\n\n\n\n<p>yolov3-tiny\u6bcf\u4e2a\u5377\u79ef\u5c42\u4e4b\u540e\uff0c\u6fc0\u6d3b\u51fd\u6570\u4e4b\u524d\uff0c\u90fd\u8981\u5bf9\u7ed3\u679c\u8fdb\u884cBatch Normalization\uff1a<img src=\"https:\/\/img-blog.csdnimg.cn\/20200415165511572.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FsYW5nYWl4aWFveGlhbw==,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>\u7531\u4e8eBN\u5c42\u548c\u5377\u79ef\u64cd\u4f5c\u90fd\u662f\u7ebf\u6027\u7684\uff0c\u5c06\u6743\u91cd\u6587\u4ef6\u8fdb\u884c\u6279\u91cf\u5f52\u4e00\u5316\uff0c\u53ef\u4ee5\u4ee3\u66ff\u5377\u79ef\u5c42\u4e4b\u540e\u7684BN\u5c42\uff1a<br><img src=\"https:\/\/img-blog.csdnimg.cn\/20200415165729177.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FsYW5nYWl4aWFveGlhbw==,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>\u5728network.c\u7684fuse_conv_batchnorm\u51fd\u6570\u4e2d\u5b9e\u73b0\u6743\u91cd\u6587\u4ef6\u548cBN\u5c42\u7684\u5408\u5e76\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void fuse_conv_batchnorm(network net)\n{\n    int j;\n    for (j = 0; j &lt; net.n; ++j) {\n        layer *l = &amp;net.layers&#091;j];\n\t\t    <em>\/\/ printf(\"the %d layer batch_normalize is %d,   groups is %d \\n\", j, l-&gt;batch_normalize, l-&gt;groups);<\/em>\n        if (l-&gt;type == CONVOLUTIONAL) { <em>\/\/\u53ea\u8fd0\u884c\u8fd9\u4e00\u5206\u652f   \u5408\u5e76\u5377\u79ef\u5c42\u548cbatch_normal<\/em>\n             <em>\/\/printf(\" Merges Convolutional-%d and batch_norm \\n\", j);<\/em>\n\n            if (l-&gt;share_layer != NULL) {<em>\/\/l-&gt;share_layer always is 0,\u4e0d\u8fd0\u884c\u8fd9\u4e2a\u5206\u652f<\/em>\n                l-&gt;batch_normalize = 0;\n            }\n\n            if (l-&gt;batch_normalize) {<em>\/\/#15,22\u5c42\u5377\u79ef\uff0c\u5377\u79ef\u4e4b\u540e\u6ca1\u6709batch normalize\uff0c\u5176\u4ed6\u90fd\u8981\u8fd0\u884c\u8fd9\u4e00\u5206\u652f<\/em>\n                int f;\n                for (f = 0; f &lt; l-&gt;n; ++f)<em>\/\/\u8be5\u5c42\u795e\u7ecf\u7f51\u7edc 1-&gt;n \u4e2a\u8f93\u51fa\u5c42\u6743\u91cd<\/em>\n                {\n                    l-&gt;biases&#091;f] = l-&gt;biases&#091;f] - (double)l-&gt;scales&#091;f] * l-&gt;rolling_mean&#091;f] \/ (sqrt((double)l-&gt;rolling_variance&#091;f] + .00001));\n\n                    const size_t filter_size = l-&gt;size*l-&gt;size*l-&gt;c \/ l-&gt;groups;<em>\/\/kernel_size * kernel_size * c\/\u5206\u7ec4  l-&gt;groups\u5b58\u5728\u4e8e\u5377\u79ef\u5c42always is 1<\/em>\n                    int i;\n                    for (i = 0; i &lt; filter_size; ++i) {\n                        int w_index = f*filter_size + i;\n\n                        l-&gt;weights&#091;w_index] = (double)l-&gt;weights&#091;w_index] * l-&gt;scales&#091;f] \/ (sqrt((double)l-&gt;rolling_variance&#091;f] + .00001));\n                    }\n                }\n\n                free_convolutional_batchnorm(l);<em>\/\/no use<\/em>\n                l-&gt;batch_normalize = 0;\n                ......\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u8f93\u5165\u56fe\u50cf<\/h2>\n\n\n\n<p>yolov3-tiny\u8f93\u5165\u795e\u7ecf\u7f51\u7edc\u7684\u56fe\u50cf\u5c3a\u5bf8\u4e3a416&#215;416\uff0c\u5bf9\u4e0d\u7b26\u5408\u8be5\u5c3a\u5bf8\u7684\u56fe\u50cf\uff0c\u8981\u8fdb\u884c\u88c1\u526a\u3002\u5728image.c\u7684resize_image\u51fd\u6570\u4e2d\u5b8c\u6210\u3002\u8fd9\u4e2a\u53ef\u4ee5\u8bf4\u662f\u6574\u4e2ayolo\u7b97\u6cd5\u5bf9\u8f93\u5165\u56fe\u50cf\u552f\u4e00\u8fdb\u884c\u9884\u5904\u7406\u7684\u5730\u65b9\u4e86\u3002\u8fd9\u4e5f\u662fyolo\u7b97\u6cd5\u5728\u5de5\u7a0b\u5e94\u7528\u4e2d\u6781\u597d\u7684\u5730\u65b9\uff0c\u6ca1\u6709\u90a3\u4e48\u591a\u7c7b\u4f3c\u4e8e\u964d\u566a\u3001\u6ee4\u6ce2\u4e4b\u7c7b\u7684\u9884\u5904\u7406\uff0c\u76f4\u63a5\u9001\u5230\u7f51\u7edc\u91cc\u5c31\u5b8c\u4e8b\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/im\uff1a\u8f93\u5165\u56fe\u7247  w:416 h:416<\/em>\n<em>\/\/\u51fd\u6570\u4f5c\u7528\uff1a\u5c06\u8f93\u5165\u56fe\u7247\u70edsize\u5230416x416\u7684\u5c3a\u5bf8\uff0c\u57fa\u672c\u6309\u7167\u7f29\u653e\/\u6269\u5927\u7684\u7b56\u7565<\/em>\nimage resize_image(image im, int w, int h)\n{\n    if (im.w == w &amp;&amp; im.h == h) return copy_image(im);\n\n    image resized = make_image(w, h, im.c);<em>\/\/416 x 416 x 3\u7a7a\u7684\u5730\u5740\u7a7a\u95f4<\/em>\n    image part = make_image(w, im.h, im.c);<em>\/\/416 x im.h x im.c\u7a7a\u7684\u5730\u5740\u7a7a\u95f4<\/em>\n    int r, c, k;\n    float w_scale = (float)(im.w - 1) \/ (w - 1);<em>\/\/\u5bbd\u5ea6\u7f29\u653e\u56e0\u5b50<\/em>\n    float h_scale = (float)(im.h - 1) \/ (h - 1);<em>\/\/\u9ad8\u5ea6\u7f29\u653e\u56e0\u5b50<\/em>\n    for(k = 0; k &lt; im.c; ++k){\n        for(r = 0; r &lt; im.h; ++r){\n            for(c = 0; c &lt; w; ++c){<em>\/\/416<\/em>\n                float val = 0;\n                if(c == w-1 || im.w == 1){<em>\/\/c =415 \u6700\u540e\u4e00\u5217<\/em>\n                    val = get_pixel(im, im.w-1, r, k);<em>\/\/\u53d6\u539f\u56fe\u7247\u6700\u540e\u4e00\u5217\u7684\u50cf\u7d20<\/em>\n                } else {\n                    float sx = c*w_scale;\n                    int ix = (int) sx;\n                    float dx = sx - ix;\n                    val = (1 - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+1, r, k);\n                }\n                set_pixel(part, c, r, k, val);\n            }\n        }\n    }\n    for(k = 0; k &lt; im.c; ++k){\n        for(r = 0; r &lt; h; ++r){\n            float sy = r*h_scale;\n            int iy = (int) sy;\n            float dy = sy - iy;\n            for(c = 0; c &lt; w; ++c){\n                float val = (1-dy) * get_pixel(part, c, iy, k);\n                set_pixel(resized, c, r, k, val);\n            }\n            if(r == h-1 || im.h == 1) continue;\n            for(c = 0; c &lt; w; ++c){\n                float val = dy * get_pixel(part, c, iy+1, k);\n                add_pixel(resized, c, r, k, val);\n            }\n        }\n    }\n\n    free_image(part);\n    return resized;\n}\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u524d\u5411\u4f20\u64ad\u7f51\u7edc<\/h2>\n\n\n\n<p>network.c\u4e2d\u7684forward_network\u51fd\u6570\u662f\u6574\u4e2a\u795e\u7ecf\u7f51\u7edc\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u5404\u5c42\u7684\u7f51\u7edc\u90fd\u5728\u51fd\u6570\u6307\u9488l.forward(l, state)\u4e2d\u5b8c\u6210\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void forward_network(network net, network_state state)\n{\n    state.workspace = net.workspace;\n    int i;\n\t   <em>\/\/\/ \u904d\u5386\u6240\u6709\u5c42\uff0c\u4ece\u7b2c\u4e00\u5c42\u5230\u6700\u540e\u4e00\u5c42\uff0c\u9010\u5c42\u8fdb\u884c\u524d\u5411\u4f20\u64ad\uff08\u7f51\u7edc\u603b\u5171\u6709net.n\u5c42\uff09<\/em>\n    for(i = 0; i &lt; net.n; ++i){\t\t  \n        state.index = i;<em>\/\/\/ \u7f6e\u7f51\u7edc\u5f53\u524d\u6d3b\u8dc3\u5c42\u4e3a\u5f53\u524d\u5c42\uff0c\u5373\u7b2ci\u5c42\t\t  <\/em>\n        layer l = net.layers&#091;i];<em>\/\/\/ \u83b7\u53d6\u5f53\u524d\u5c42\t\t  <\/em>\n        if(l.delta &amp;&amp; state.train){<em>\/\/\u4e0d\u6267\u884c\u6b64\u5206\u652f\u7684\u4ee3\u7801<\/em>\n\t\t\t<em>\/\/\/ \u5982\u679c\u5f53\u524d\u5c42\u7684l.delta\u5df2\u7ecf\u52a8\u6001\u5206\u914d\u4e86\u5185\u5b58\uff0c\u5219\u8c03\u7528fill_cpu()\u51fd\u6570\uff0c\u5c06\u5176\u6240\u6709\u5143\u7d20\u7684\u503c\u521d\u59cb\u5316\u4e3a0\t\t\t   <\/em>\n            scal_cpu(l.outputs * l.batch, 0, l.delta, 1);<em>\/\/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3al.delta\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u521d\u59cb\u5316\u503c\uff0c\u4e3a0<\/em>\n\t\t\tprintf(\"forward_network scal_cpu of %d layer done!\\n \", i);\n        }\n           <em>\/\/double time = get_time_point();<\/em>\n\t\tl.forward(l, state);<em>\/\/\u8fdb\u884c\u5377\u79ef\u8fd0\u7b97\uff0c\u6fc0\u6d3b\u51fd\u6570\uff0c\u6c60\u5316\u8fd0\u7b97\/<\/em>\n\t\t   <em>\/\/if layer_type = convolutional ;   l.forward = forward_convolutional_layer;<\/em>\n\t\t   <em>\/\/if layer_type = maxpool           l.forward = forward_maxpool_layer;<\/em>\n\t\t   <em>\/\/if layer_type = yolo              l.forward = forward_yolo_layer;<\/em>\n\t\t   <em>\/\/if layer_type = ROUTE             l.forward = forward_route_layer;\u5176\u5b9e\u5c31\u662f\u6570\u636e\u7684\u590d\u5236\u548c\u642c\u79fb<\/em>\n\t\t   <em>\/\/if layer_type = upsample          l.forward = forward_upsample_layer;;\t\t  <\/em>\n           <em>\/\/printf(\"%d - Predicted in %lf milli-seconds.\\n\", i, ((double)get_time_point() - time) \/ 1000);<\/em>\n\t\t   <em>\/\/\/ \u5b8c\u6210\u67d0\u4e00\u5c42\u7684\u63a8\u7406\u65f6\uff0c\u7f6e\u7f51\u7edc\u7684\u8f93\u5165\u4e3a\u5f53\u524d\u5c42\u7684\u8f93\u51fa\uff08\u8fd9\u5c06\u6210\u4e3a\u4e0b\u4e00\u5c42\u7f51\u7edc\u7684\u8f93\u5165\uff09\uff0c\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6b64\u5904\u662f\u76f4\u63a5\u66f4\u6539\u6307\u9488\u53d8\u91cfnet.input\u672c\u8eab\u7684\u503c\uff0c<\/em>\n\t\t   <em>\/\/\/ \u4e5f\u5c31\u662f\u6b64\u5904\u662f\u901a\u8fc7\u6539\u53d8\u6307\u9488net.input\u6240\u6307\u7684\u5730\u5740\u6765\u6539\u53d8\u5176\u4e2d\u6240\u5b58\u5185\u5bb9\u7684\u503c\uff0c\u5e76\u4e0d\u662f\u76f4\u63a5\u6539\u53d8\u5176\u6240\u6307\u7684\u5185\u5bb9\u800c\u6307\u9488\u6240\u6307\u7684\u5730\u5740\u6ca1\u53d8\uff0c<\/em>\n\t\t   <em>\/\/\/ \u6240\u4ee5\u5728\u9000\u51faforward_network()\u51fd\u6570\u540e\uff0c\u5176\u5bf9net.input\u7684\u6539\u53d8\u90fd\u5c06\u5931\u6548\uff0cnet.input\u5c06\u56de\u5230\u8fdb\u5165forward_network()\u4e4b\u524d\u65f6\u7684\u503c\u3002\t<\/em>\n\t\t   ......\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u5377\u79ef\u5c42[convolution]<\/h2>\n\n\n\n<p>\u5377\u79ef\u5c42\u5728convolutional_layer.c\u4e2d\u7684forward_convolutional_layer\u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void forward_convolutional_layer(convolutional_layer l, network_state state)\n{\n    \n\tint out_h = convolutional_out_height(l);<em>\/\/\u83b7\u5f97\u672c\u5c42\u5377\u79ef\u5c42\u8f93\u51fa\u7279\u5f81\u56fe\u7684\u9ad8\u3001\u5bbd<\/em>\n    int out_w = convolutional_out_width(l);\n    int i, j;\n\t\n\t<em>\/\/ l.outputs = l.out_h * l.out_w * l.out_c\u5728make\u5404\u7f51\u7edc\u5c42\u51fd\u6570\u4e2d\u8d4b\u503c\uff08\u6bd4\u5982make_convolutional_layer()\uff09\uff0c<\/em>\n\t<em>\/\/ \u5bf9\u5e94\u6bcf\u5f20\u8f93\u5165\u56fe\u7247\u7684\u6240\u6709\u8f93\u51fa\u7279\u5f81\u56fe\u7684\u603b\u5143\u7d20\u4e2a\u6570\uff08\u6bcf\u5f20\u8f93\u5165\u56fe\u7247\u4f1a\u5f97\u5230n\u4e5f\u5373l.out_c\u5f20\u7279\u5f81\u56fe\uff09<\/em>\n\t<em>\/\/ \u521d\u59cb\u5316\u8f93\u51fal.output\u5168\u4e3a0.0\uff1b\u8f93\u5165l.outputs*l.batch\u4e3a\u8f93\u51fa\u7684\u603b\u5143\u7d20\u4e2a\u6570\uff0c\u5176\u4e2dl.outputs\u4e3abatch<\/em>\n\t<em>\/\/ \u4e2d\u4e00\u4e2a\u8f93\u5165\u5bf9\u5e94\u7684\u8f93\u51fa\u7684\u6240\u6709\u5143\u7d20\u7684\u4e2a\u6570\uff0cl.batch\u4e3a\u4e00\u4e2abatch\u8f93\u5165\u5305\u542b\u7684\u56fe\u7247\u5f20\u6570\uff1b0\u8868\u793a\u521d\u59cb\u5316\u6240\u6709\u8f93\u51fa\u4e3a0\uff1b<\/em>\n    fill_cpu(l.outputs*l.batch, 0, l.output, 1);<em>\/\/\u5c06\u5730\u5740l.output\uff0cl.outputs*l.batch\u4e2afloat\u5730\u5740\u7a7a\u95f4\u7684\u6570\u636e\u521d\u59cb\u53160<\/em>\n    .......\n<\/code><\/pre>\n\n\n\n<p>\u4f5c\u8005\u5728\u8fdb\u884c\u5377\u79ef\u8fd0\u7b97\u524d\uff0c\u5c06\u8f93\u5165\u7279\u5f81\u56fe\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n```c\nvoid im2col_cpu(float* data_im,\n     int channels,  int height,  int width,\n     int ksize,  int stride, int pad, float* data_col)\n{\n    int c,h,w;\n\t<em>\/\/ \u8ba1\u7b97\u8be5\u5c42\u795e\u7ecf\u7f51\u7edc\u7684\u8f93\u51fa\u56fe\u50cf\u5c3a\u5bf8\uff08\u5176\u5b9e\u6ca1\u6709\u5fc5\u8981\u518d\u6b21\u8ba1\u7b97\u7684\uff0c\u56e0\u4e3a\u5728\u6784\u5efa\u5377\u79ef\u5c42\u65f6\uff0cmake_convolutional_layer()\u51fd\u6570<\/em>\n\t<em>\/\/ \u5df2\u7ecf\u8c03\u7528convolutional_out_width()\uff0cconvolutional_out_height()\u51fd\u6570\u6c42\u53d6\u4e86\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c<\/em>\n\t<em>\/\/ \u6b64\u5904\u76f4\u63a5\u4f7f\u7528l.out_h,l.out_w\u5373\u53ef\uff0c\u51fd\u6570\u53c2\u6570\u53ea\u8981\u4f20\u5165\u8be5\u5c42\u7f51\u7edc\u6307\u9488\u5c31\u53ef\u4e86\uff0c\u6ca1\u5fc5\u8981\u5f04\u8fd9\u4e48\u591a\u53c2\u6570\uff09<\/em>\n    int height_col = (height + 2*pad - ksize) \/ stride + 1;\n    int width_col = (width + 2*pad - ksize) \/ stride + 1;\n\t\n\t<em>\/\/\/ \u5377\u79ef\u6838\u5927\u5c0f\uff1aksize*ksize\u662f\u4e00\u4e2a\u5377\u79ef\u6838\u7684\u5927\u5c0f\uff0c\u4e4b\u6240\u4ee5\u4e58\u4ee5\u901a\u9053\u6570channels\uff0c\u662f\u56e0\u4e3a\u8f93\u5165\u56fe\u50cf\u6709\u591a\u901a\u9053\uff0c\u6bcf\u4e2a\u5377\u79ef\u6838\u5728\u505a\u5377\u79ef\u65f6\uff0c<\/em>\n\t<em>\/\/\/ \u662f\u540c\u65f6\u5bf9\u540c\u4e00\u4f4d\u7f6e\u5904\u591a\u901a\u9053\u7684\u56fe\u50cf\u8fdb\u884c\u5377\u79ef\u8fd0\u7b97\uff0c\u8fd9\u91cc\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\uff0c\u5c06\u4e09\u901a\u9053\u4e0a\u7684\u5377\u79ef\u6838\u5e76\u5728\u4e00\u8d77\u4ee5\u4fbf\u8fdb\u884c\u8ba1\u7b97\uff0c\u56e0\u6b64\u5377\u79ef\u6838<\/em>\n\t<em>\/\/\/ \u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u4e8c\u7ef4\u7684\uff0c\u800c\u662f\u4e09\u7ef4\u7684\uff0c\u6bd4\u5982\u5bf9\u4e8e3\u901a\u9053\u56fe\u50cf\uff0c\u5377\u79ef\u6838\u5c3a\u5bf8\u4e3a3*3\uff0c\u8be5\u5377\u79ef\u6838\u5c06\u540c\u65f6\u4f5c\u7528\u4e8e\u4e09\u901a\u9053\u56fe\u50cf\u4e0a\uff0c\u8fd9\u6837\u5e76\u8d77\u6765\u5c31\u5f97<\/em>\n\t<em>\/\/\/ \u5230\u542b\u670927\u4e2a\u5143\u7d20\u7684\u5377\u79ef\u6838\uff0c\u4e14\u8fd927\u4e2a\u5143\u7d20\u90fd\u662f\u72ec\u7acb\u7684\u9700\u8981\u8bad\u7ec3\u7684\u53c2\u6570\u3002\u6240\u4ee5\u5728\u8ba1\u7b97\u8bad\u7ec3\u53c2\u6570\u4e2a\u6570\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u6bcf\u4e00\u4e2a\u5377\u79ef\u6838\u7684\u5b9e\u9645<\/em>\n\t<em>\/\/\/ \u8bad\u7ec3\u53c2\u6570\u9700\u8981\u4e58\u4ee5\u8f93\u5165\u901a\u9053\u6570\u3002<\/em>\n    int channels_col = channels * ksize * ksize;<em>\/\/\u8f93\u5165\u901a\u9053<\/em>\n\t<em>\/\/ \u5916\u5faa\u73af\u6b21\u6570\u4e3a\u4e00\u4e2a\u5377\u79ef\u6838\u7684\u5c3a\u5bf8\u6570\uff0c\u5faa\u73af\u6b21\u6570\u5373\u4e3a\u6700\u7ec8\u5f97\u5230\u7684data_col\u7684\u603b\u884c\u6570<\/em>\n    for (c = 0; c &lt; channels_col; ++c) {\n\n\t\t<em>\/\/\u884c\uff0c\u5217\u504f\u7f6e\u90fd\u662f\u5bf9\u5e94\u7740\u672c\u6b21\u5faa\u73af\u8981\u64cd\u4f5c\u7684\u8f93\u51fa\u4f4d\u7f6e\u7684\u50cf\u7d20\u800c\u8a00\u7684\uff0c\u901a\u9053\u504f\u7f6e\uff0c\u662f\u8be5\u4f4d\u7f6e\u50cf\u7d20\u6240\u5728\u7684\u8f93\u51fa\u901a\u9053\u7684\u7edd\u5bf9\u4f4d\u7f6e\uff08\u901a\u9053\u6570\uff09<\/em>\n\n\t\t<em>\/\/ \u5217\u504f\u79fb\uff0c\u5377\u79ef\u6838\u662f\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff0c\u5e76\u6309\u884c\u5b58\u50a8\u5728\u4e00\u7ef4\u6570\u7ec4\u4e2d\uff0c\u5229\u7528\u6c42\u4f59\u8fd0\u7b97\u83b7\u53d6\u5bf9\u5e94\u5728\u5377\u79ef\u6838\u4e2d\u7684\u5217\u6570\uff0c\u6bd4\u5982\u5bf9\u4e8e<\/em>\n\t\t<em>\/\/ 3*3\u7684\u5377\u79ef\u6838\uff083\u901a\u9053\uff09\uff0c\u5f53c=0\u65f6\uff0c\u663e\u7136\u5728\u7b2c\u4e00\u5217\uff0c\u5f53c=5\u65f6\uff0c\u663e\u7136\u5728\u7b2c2\u5217\uff0c\u5f53c=9\u65f6\uff0c\u5728\u7b2c\u4e8c\u901a\u9053\u4e0a\u7684\u5377\u79ef\u6838\u7684\u7b2c\u4e00\u5217\uff0c<\/em>\n\t\t<em>\/\/ \u5f53c=26\u65f6\uff0c\u5728\u7b2c\u4e09\u5217\uff08\u7b2c\u4e09\u8f93\u5165\u901a\u9053\u4e0a\uff09<\/em>\n        int w_offset = c % ksize;<em>\/\/0,1,2<\/em>\n\t\t<em>\/\/ \u884c\u504f\u79fb\uff0c\u5377\u79ef\u6838\u662f\u4e00\u4e2a\u4e8c\u7ef4\u7684\u77e9\u9635\uff0c\u4e14\u662f\u6309\u884c\uff08\u5377\u79ef\u6838\u6240\u6709\u884c\u5e76\u6210\u4e00\u884c\uff09\u5b58\u50a8\u5728\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\uff0c<\/em>\n\t\t<em>\/\/ \u6bd4\u5982\u5bf9\u4e8e3*3\u7684\u5377\u79ef\u6838\uff0c\u5904\u74063\u901a\u9053\u7684\u56fe\u50cf\uff0c\u90a3\u4e48\u4e00\u4e2a\u5377\u79ef\u6838\u5177\u670927\u4e2a\u5143\u7d20\uff0c\u6bcf9\u4e2a\u5143\u7d20\u5bf9\u5e94\u4e00\u4e2a\u901a\u9053\u4e0a\u7684\u5377\u79ef\u6838\uff08\u4e92\u4e3a\u4e00\u6837\uff09\uff0c<\/em>\n\t\t<em>\/\/ \u6bcf\u5f53c\u4e3a3\u7684\u500d\u6570\uff0c\u5c31\u610f\u5473\u7740\u5377\u79ef\u6838\u6362\u4e86\u4e00\u884c\uff0ch_offset\u53d6\u503c\u4e3a0,1,2\uff0c\u5bf9\u5e943*3\u5377\u79ef\u6838\u4e2d\u7684\u7b2c1, 2, 3\u884c<\/em>\n        int h_offset = (c \/ ksize) % ksize;<em>\/\/0,1,2<\/em>\n\t\t<em>\/\/ \u901a\u9053\u504f\u79fb\uff0cchannels_col\u662f\u591a\u901a\u9053\u7684\u5377\u79ef\u6838\u5e76\u5728\u4e00\u8d77\u7684\uff0c\u6bd4\u5982\u5bf9\u4e8e3\u901a\u9053\uff0c3*3\u5377\u79ef\u6838\uff0c\u6bcf\u8fc79\u4e2a\u5143\u7d20\u5c31\u8981\u6362\u4e00\u901a\u9053\u6570\uff0c<\/em>\n\t\t<em>\/\/ \u5f53c=0~8\u65f6\uff0cc_im=0;c=9~17\u65f6\uff0cc_im=1;c=18~26\u65f6\uff0cc_im=2,\u64cd\u4f5c\u5bf9\u8c61\u662f\u6392\u5e8f\u540e\u7684\u50cf\u7d20\u4f4d\u7f6e<\/em>\n        int c_im = c \/ ksize \/ ksize;\n\t\t<em>\/\/ \u4e2d\u5faa\u73af\u6b21\u6570\u7b49\u4e8e\u8be5\u5c42\u8f93\u51fa\u56fe\u50cf\u884c\u6570height_col\uff0c\u8bf4\u660edata_col\u4e2d\u7684\u6bcf\u4e00\u884c\u5b58\u50a8\u4e86\u4e00\u5f20\u7279\u5f81\u56fe\uff0c\u8fd9\u5f20\u7279\u5f81\u56fe\u53c8\u662f\u6309\u884c\u5b58\u50a8\u5728data_col\u4e2d\u7684\u67d0\u884c\u4e2d<\/em>\n        for (h = 0; h &lt; height_col; ++h) {\n\t\t\t<em>\/\/ \u5185\u5faa\u73af\u7b49\u4e8e\u8be5\u5c42\u8f93\u51fa\u56fe\u50cf\u5217\u6570width_col\uff0c\u8bf4\u660e\u6700\u7ec8\u5f97\u5230\u7684data_col\u603b\u6709channels_col\u884c\uff0cheight_col*width_col\u5217<\/em>\n            for (w = 0; w &lt; width_col; ++w) {\n\t\t\t\t<em>\/\/ \u7531\u4e0a\u9762\u53ef\u77e5\uff0c\u5bf9\u4e8e3*3\u7684\u5377\u79ef\u6838\uff0c\u884c\u504f\u7f6eh_offset\u53d6\u503c\u4e3a0,1,2,\u5f53h_offset=0\u65f6\uff0c\u4f1a\u63d0\u53d6\u51fa\u6240\u6709\u4e0e\u5377\u79ef\u6838\u7b2c\u4e00\u884c\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u7684\u50cf\u7d20\uff0c<\/em>\n\t\t\t\t<em>\/\/ \u4f9d\u6b21\u7c7b\u63a8\uff1b\u52a0\u4e0ah*stride\u662f\u5bf9\u5377\u79ef\u6838\u8fdb\u884c\u884c\u79fb\u4f4d\u64cd\u4f5c\uff0c\u6bd4\u5982\u5377\u79ef\u6838\u4ece\u56fe\u50cf(0,0)\u4f4d\u7f6e\u5f00\u59cb\u505a\u5377\u79ef\uff0c\u90a3\u4e48\u6700\u5148\u5f00\u59cb\u6d89\u53ca(0,0)~(3,3)<\/em>\n\t\t\t\t<em>\/\/ \u4e4b\u95f4\u7684\u50cf\u7d20\u503c\uff0c\u82e5stride=2\uff0c\u90a3\u4e48\u5377\u79ef\u6838\u8fdb\u884c\u4e00\u6b21\u884c\u79fb\u4f4d\u65f6\uff0c\u4e0b\u4e00\u884c\u7684\u5377\u79ef\u64cd\u4f5c\u662f\u4ece\u5143\u7d20(2,0)\uff082\u4e3a\u56fe\u50cf\u884c\u53f7\uff0c0\u4e3a\u5217\u53f7\uff09\u5f00\u59cb<\/em>\n                int im_row = h_offset + h * stride;<em>\/\/yolov3-tiny stride = 1<\/em>\n\t\t\t\t<em>\/\/ \u5bf9\u4e8e3*3\u7684\u5377\u79ef\u6838\uff0cw_offset\u53d6\u503c\u4e5f\u4e3a0,1,2\uff0c\u5f53w_offset\u53d61\u65f6\uff0c\u4f1a\u63d0\u53d6\u51fa\u6240\u6709\u4e0e\u5377\u79ef\u6838\u4e2d\u7b2c2\u5217\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u7684\u50cf\u7d20\uff0c<\/em>\n\t\t\t\t<em>\/\/ \u5b9e\u9645\u5728\u505a\u5377\u79ef\u64cd\u4f5c\u65f6\uff0c\u5377\u79ef\u6838\u5bf9\u56fe\u50cf\u9010\u884c\u626b\u63cf\u505a\u5377\u79ef\uff0c\u52a0\u4e0aw*stride\u5c31\u662f\u4e3a\u4e86\u505a\u5217\u79fb\u4f4d\uff0c<\/em>\n\t\t\t\t<em>\/\/ \u6bd4\u5982\u524d\u4e00\u6b21\u5377\u79ef\u5176\u5b9e\u50cf\u7d20\u5143\u7d20\u4e3a(0,0)\uff0c\u82e5stride=2,\u90a3\u4e48\u4e0b\u6b21\u5377\u79ef\u5143\u7d20\u8d77\u59cb\u50cf\u7d20\u4f4d\u7f6e\u4e3a(0,2)\uff080\u4e3a\u884c\u53f7\uff0c2\u4e3a\u5217\u53f7\uff09<\/em>\n                int im_col = w_offset + w * stride;\n\t\t\t\t<em>\/\/ col_index\u4e3a\u91cd\u6392\u540e\u56fe\u50cf\u4e2d\u7684\u50cf\u7d20\u7d22\u5f15\uff0c\u7b49\u4e8ec * height_col * width_col + h * width_col +w\uff08\u8fd8\u662f\u6309\u884c\u5b58\u50a8\uff0c\u6240\u6709\u901a\u9053\u518d\u5e76\u6210\u4e00\u884c\uff09\uff0c<\/em>\n\t\t\t\t<em>\/\/ \u5bf9\u5e94\u7b2cc\u901a\u9053\uff0ch\u884c\uff0cw\u5217\u7684\u5143\u7d20<\/em>\n                int col_index = (c * height_col + h) * width_col + w;<em>\/\/\u5c06\u91cd\u6392\u540e\u7684\u56fe\u7247\u50cf\u7d20\uff0c\u6309\u7167\u5de6\u4e0a-&gt;\u53f3\u4e0b\u7684\u987a\u5e8f\uff0c\u8ba1\u7b97\u4e00\u7ef4\u7d22\u5f15<\/em>\n\n\t\t\t\t<em>\/\/im_col + width*im_row +  width*height*channel \u91cd\u6392\u524d\u7684\u7279\u5f81\u56fe\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u7d22\u5f15<\/em>\n\t\t\t\t<em>\/\/ im2col_get_pixel\u51fd\u6570\u83b7\u53d6\u8f93\u5165\u56fe\u50cfdata_im\u4e2d\u7b2cc_im\u901a\u9053\uff0cim_row,im_col\u7684\u50cf\u7d20\u503c\u5e76\u8d4b\u503c\u7ed9\u91cd\u6392\u540e\u7684\u56fe\u50cf\uff0c<\/em>\n\t\t\t\t<em>\/\/ height\u548cwidth\u4e3a\u8f93\u5165\u56fe\u50cfdata_im\u7684\u771f\u5b9e\u9ad8\u3001\u5bbd\uff0cpad\u4e3a\u56db\u5468\u88650\u7684\u957f\u5ea6\uff08\u6ce8\u610fim_row,im_col\u662f\u88650\u4e4b\u540e\u7684\u884c\u5217\u53f7\uff0c<\/em>\n\t\t\t\t<em>\/\/ \u4e0d\u662f\u771f\u5b9e\u8f93\u5165\u56fe\u50cf\u4e2d\u7684\u884c\u5217\u53f7\uff0c\u56e0\u6b64\u9700\u8981\u51cf\u53bbpad\u83b7\u53d6\u771f\u5b9e\u7684\u884c\u5217\u53f7\uff09<\/em>\n                data_col&#091;col_index] = im2col_get_pixel(data_im, height, width, channels,\n                        im_row, im_col, c_im, pad);\n\t\t\t\t<em>\/\/ return data_im&#091;im_col + width*im_row +  width*height*channel)];<\/em>\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7gemm\u8fdb\u884c\u5377\u79ef\u4e58\u52a0\u64cd\u4f5c\uff0c\u901a\u8fc7add_bias\u6dfb\u52a0\u504f\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/\u8fdb\u884c\u5377\u79ef\u7684\u4e58\u52a0\u8fd0\u7b97\uff0c\u6ca1\u6709bias\u504f\u7f6e\u53c2\u6570\u53c2\u4e0e\u8fd0\u7b97\uff1b<\/em>\ngemm(0, 0, m, n, k, 1, a, k, b, n, 1, c, n);\n\nadd_bias(l.output, l.biases, l.batch, l.n, out_h*out_w);<em>\/\/\u6bcf\u4e2a\u8f93\u51fa\u7279\u5f81\u56fe\u7684\u5143\u7d20\u90fd\u52a0\u4e0a\u5bf9\u5e94\u901a\u9053\u7684\u504f\u7f6e\u53c2\u6570<\/em>\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u6c60\u5316\u5c42[maxpool]<\/h2>\n\n\n\n<p>maxpool_layer.c\u4e2d\u7684forward_maxpool_layer\u51fd\u6570\u5b8c\u6210\u6c60\u5316\u64cd\u4f5c\u3002yolov3-tiny\u4fdd\u7559\u4e86\u6c60\u5316\u5c42\uff0c\u5e76\u4f7f\u7528\u6700\u5927\u503c\u6c60\u5316\uff0c\u5c06\u5c3a\u5bf8\u4e3a2&#215;2\u7684\u6838\u4e2d\u6700\u5927\u503c\u4fdd\u7559\u4e0b\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void forward_maxpool_layer_avx(float *src, float *dst, int *indexes, int size, int w, int h, int out_w, int out_h, int c,\n    int pad, int stride, int batch)\n{\n\n    const int w_offset = -pad \/ 2;\n    const int h_offset = -pad \/ 2;\n    int b, k;\n\n    for (b = 0; b &lt; batch; ++b) {\n\t\t<em>\/\/ \u5bf9\u4e8e\u6bcf\u5f20\u8f93\u5165\u56fe\u7247\uff0c\u5c06\u5f97\u5230\u901a\u9053\u6570\u4e00\u6837\u7684\u8f93\u51fa\u56fe\uff0c\u4ee5\u8f93\u51fa\u56fe\u4e3a\u57fa\u51c6\uff0c\u6309\u8f93\u51fa\u56fe\u901a\u9053\uff0c\u884c\uff0c\u5217\u4f9d\u6b21\u904d\u5386<\/em>\n\t\t<em>\/\/ \uff08\u8fd9\u5bf9\u5e94\u56fe\u50cf\u5728l.output\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u6bcf\u5f20\u56fe\u7247\u6309\u884c\u94fa\u6392\u6210\u4e00\u5927\u884c\uff0c\u7136\u540e\u56fe\u7247\u4e0e\u56fe\u7247\u4e4b\u95f4\u518d\u5e76\u6210\u4e00\u884c\uff09\u3002<\/em>\n\t\t<em>\/\/ \u4ee5\u8f93\u51fa\u56fe\u4e3a\u57fa\u51c6\u8fdb\u884c\u904d\u5386\uff0c\u6700\u7ec8\u5faa\u73af\u7684\u603b\u6b21\u6570\u521a\u597d\u8986\u76d6\u6c60\u5316\u6838\u5728\u8f93\u5165\u56fe\u7247\u4e0d\u540c\u4f4d\u7f6e\u8fdb\u884c\u6c60\u5316\u64cd\u4f5c\u3002<\/em>\n        #pragma omp parallel for\n        for (k = 0; k &lt; c; ++k) {\n            int i, j, m, n;\n            for (i = 0; i &lt; out_h; ++i) {\n                <em>\/\/for (j = 0; j &lt; out_w; ++j) {<\/em>\n                j = 0;\n                for (; j &lt; out_w; ++j) {\n\t\t\t\t\t<em>\/\/ out_index\u4e3a\u8f93\u51fa\u56fe\u4e2d\u7684\u7d22\u5f15<\/em>\n                    int out_index = j + out_w*(i + out_h*(k + c*b));<em>\/\/j + out_w * i + out_w * iout_h * k<\/em>\n                    float max = -FLT_MAX;<em>\/\/ FLT_MAX\u4e3ac\u8bed\u8a00\u4e2dfloat.h\u5b9a\u4e49\u7684\u5bf9\u5927\u6d6e\u70b9\u6570\uff0c\u6b64\u5904\u521d\u59cb\u5316\u6700\u5927\u5143\u7d20\u503c\u4e3a\u6700\u5c0f\u6d6e\u70b9\u6570<\/em>\n                    int max_i = -1;<em>\/\/ \u6700\u5927\u5143\u7d20\u503c\u7684\u7d22\u5f15\u521d\u59cb\u5316\u4e3a-1<\/em>\n                    <em>\/\/ \u4e0b\u9762\u4e24\u4e2a\u5faa\u73af\u56de\u5230\u4e86\u8f93\u5165\u56fe\u7247\uff0c\u8ba1\u7b97\u5f97\u5230\u7684cur_h\u4ee5\u53cacur_w\u90fd\u662f\u5728\u5f53\u524d\u5c42\u6240\u6709\u8f93\u5165\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5185\u5916\u5faa\u73af\u7684\u76ee\u7684\u662f\u627e\u5bfb\u8f93\u5165\u56fe\u50cf\u4e2d\uff0c<\/em>\n                    <em>\/\/ \u4ee5(h_offset + i*l.stride, w_offset + j*l.stride)\u4e3a\u5de6\u4e0a\u8d77\u70b9\uff0c\u5c3a\u5bf8\u4e3al.size\u6c60\u5316\u533a\u57df\u4e2d\u7684\u6700\u5927\u5143\u7d20\u503cmax\u53ca\u5176\u5728\u6240\u6709\u8f93\u5165\u5143\u7d20\u4e2d\u7684\u7d22\u5f15max_i<\/em>\n                    for (n = 0; n &lt; size; ++n) {<em>\/\/2<\/em>\n                        for (m = 0; m &lt; size; ++m) {<em>\/\/2<\/em>\n                            <em>\/\/ cur_h\uff0ccur_w\u662f\u5728\u6240\u6709\u8f93\u5165\u56fe\u50cf\u4e2d\u7b2ck\u901a\u9053\u4e2d\u7684cur_h\u884c\u4e0ecur_w\u5217\uff0cindex\u662f\u5728\u6240\u6709\u8f93\u5165\u56fe\u50cf\u5143\u7d20\u4e2d\u7684\u603b\u7d22\u5f15\u3002<\/em>\n                            <em>\/\/ \u4e3a\u4ec0\u4e48\u8fd9\u91cc\u5c11\u4e00\u5c42\u5bf9\u8f93\u5165\u901a\u9053\u6570\u7684\u904d\u5386\u5faa\u73af\u5462\uff1f\u56e0\u4e3a\u5bf9\u4e8e\u6700\u5927\u6c60\u5316\u5c42\u6765\u8bf4\u8f93\u5165\u4e0e\u8f93\u51fa\u901a\u9053\u6570\u662f\u4e00\u6837\u7684\uff0c\u5e76\u5728\u4e0a\u9762\u7684\u901a\u9053\u6570\u5faa\u73af\u4e86\uff01<\/em>\n                            int cur_h = h_offset + i*stride + n;\n                            int cur_w = w_offset + j*stride + m;\n                            int index = cur_w + w*(cur_h + h*(k + b*c));\n\t\t\t\t\t\t\t<em>\/\/ \u8fb9\u754c\u68c0\u67e5\uff1a\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u662f\u4e0d\u4f1a\u8d8a\u754c\u7684\uff0c\u4f46\u662f\u5982\u679c\u6709\u88650\u64cd\u4f5c\uff0c\u5c31\u4f1a\u8d8a\u754c\u4e86\uff0c\u8fd9\u91cc\u7684\u5904\u7406\u65b9\u5f0f\u662f\u76f4\u63a5\u8ba9\u8fd9\u4e9b\u5143\u7d20\u503c\u4e3a-FLT_MAX<\/em>\n\t\t\t\t\t\t\t<em>\/\/ \uff08\u6ce8\u610f\u867d\u7136\u79f0\u4e4b\u4e3a\u88650\u64cd\u4f5c\uff0c\u4f46\u5b9e\u9645\u4e0d\u662f\u88650\uff09\uff0c\u603b\u4e4b\uff0c\u8fd9\u4e9b\u8865\u7684\u5143\u7d20\u6c38\u8fdc\u4e0d\u4f1a\u5145\u5f53\u6700\u5927\u5143\u7d20\u503c\u3002<\/em>\n                            int valid = (cur_h &gt;= 0 &amp;&amp; cur_h &lt; h &amp;&amp;\n                                cur_w &gt;= 0 &amp;&amp; cur_w &lt; w);\n                            float val = (valid != 0) ? src&#091;index] : -FLT_MAX;\n\t\t\t\t\t\t\t<em>\/\/ \u8bb0\u5f55\u8fd9\u4e2a\u6c60\u5316\u533a\u57df\u4e2d\u7684\u6700\u5927\u7684\u5143\u7d20\u503c\u53ca\u5176\u5728\u6240\u6709\u8f93\u5165\u5143\u7d20\u4e2d\u7684\u603b\u7d22\u5f15<\/em>\n                            max_i = (val &gt; max) ? index : max_i;\n                            max = (val &gt; max) ? val : max;\n                        }\n                    }\n\t\t\t\t\t<em>\/\/ \u7531\u6b64\u5f97\u5230\u6700\u5927\u6c60\u5316\u5c42\u6bcf\u4e00\u4e2a\u8f93\u51fa\u5143\u7d20\u503c\u53ca\u5176\u5728\u6240\u6709\u8f93\u5165\u5143\u7d20\u4e2d\u7684\u603b\u7d22\u5f15\u3002<\/em>\n\t\t\t\t\t<em>\/\/ \u4e3a\u4ec0\u4e48\u9700\u8981\u8bb0\u5f55\u6bcf\u4e2a\u8f93\u51fa\u5143\u7d20\u503c\u5bf9\u5e94\u5728\u8f93\u5165\u5143\u7d20\u4e2d\u7684\u603b\u7d22\u5f15\u5462\uff1f\u56e0\u4e3a\u5728\u4e0b\u9762\u7684\u53cd\u5411\u8fc7\u7a0b\u4e2d\u9700\u8981\u7528\u5230\uff0c\u5728\u8ba1\u7b97\u5f53\u524d\u6700\u5927\u6c60\u5316\u5c42\u4e0a\u4e00\u5c42\u7f51\u7edc\u7684\u654f\u611f\u5ea6\u65f6\uff0c<\/em>\n\t\t\t\t\t<em>\/\/ \u9700\u8981\u8be5\u7d22\u5f15\u660e\u786e\u5f53\u524d\u5c42\u7684\u6bcf\u4e2a\u5143\u7d20\u7a76\u7adf\u662f\u53d6\u4e0a\u4e00\u5c42\u8f93\u51fa\uff08\u4e5f\u5373\u4e0a\u524d\u5c42\u8f93\u5165\uff09\u7684\u54ea\u4e00\u4e2a\u5143\u7d20\u7684\u503c\uff0c\u5177\u4f53\u89c1\u4e0b\u9762backward_maxpool_layer()\u51fd\u6570\u7684\u6ce8\u91ca\u3002<\/em>\n                    dst&#091;out_index] = max;\n                    if (indexes) indexes&#091;out_index] = max_i;\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u8def\u7531\u5c42[route]<\/h2>\n\n\n\n<p>yolov3-tiny\u4e2d\u5171\u6709\u4e24\u5c42\u8def\u7531\u5c42\u3002\u7b2c17\u5c42\u8def\u7531\u5c42\uff08\u4ece0\u5c42\u5f00\u59cb\uff09\uff0c\u5176\u5b9e\u76f4\u63a5\u5c06\u7b2c13\u5c42\u7f51\u7edc\u7684\u8f93\u51fa\u7ed3\u679c\u8f93\u5165\u3002\u7b2c20\u5c42\u8def\u7531\u5c42\uff0c\u5c06\u7b2c19\u5c42\u548c\u7b2c8\u5c42\u7f51\u7edc\u7ed3\u679c\u5408\u5e76\u5728\u4e00\u8d77\uff0c19\u5c42\u5728\u524d\uff0c8\u5c42\u5728\u540e\u3002\u5728route_layer.c\u4e2d\u7684forward_route_layer\u51fd\u6570\u4e2d\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void forward_route_layer(const route_layer l, network_state state)\n{\n    int i, j;\n    int offset = 0;\n    for(i = 0; i &lt; l.n; ++i){<em>\/\/l.n\uff1a  \u5377\u79ef\u5c42\uff1a\u8f93\u51fa\u7279\u5f81\u56fe\u901a\u9053\u6570 \u8def\u7531\u5c42\uff1a\u6709\u51e0\u5c42\u7f51\u7edc\u5c42\u8f93\u5165\u672c\u5c42  17\u5c42\uff1a1\uff08\u8def\u7531\u7b2c13\u5c42\uff09   20\uff1a2\uff08\u8def\u7531\u7b2c19\u30018\u5c42\uff09<\/em>\n        int index = l.input_layers&#091;i];<em>\/\/\u8f93\u5165\u672c\u7f51\u7edc\u5c42\u7684\u7f51\u7edc\u5c42\u7684\u7d22\u5f15\uff1a\u598213\uff0c19\uff0c8<\/em>\n        float *input = state.net.layers&#091;index].output;<em>\/\/\u8f93\u5165\u7b49\u4e8e \u4e4b\u524d\u7f51\u7edc\u5c42\u7d22\u5f15\u503c\u5f97\u8f93\u51fa\uff08.output\uff09<\/em>\n        int input_size = l.input_sizes&#091;i];<em>\/\/\u8f93\u5165\u7684\u7f51\u7edc\u5c42\u7684\u6570\u636e\u91cf<\/em>\n        int part_input_size = input_size \/ l.groups;<em>\/\/\u672a\u5206\u7ec4<\/em>\n        for(j = 0; j &lt; l.batch; ++j){\n            <em>\/\/copy_cpu(input_size, input + j*input_size, 1, l.output + offset + j*l.outputs, 1);<\/em>\n\t\t\t<em>\/\/\u4ece\u9996\u5730\u5740input\u5904\u590d\u5236input_size \u4e2a\u6570\u636e\u5230 l.output\u4e2d<\/em>\n            copy_cpu(part_input_size, input + j*input_size + part_input_size*l.group_id, 1, l.output + offset + j*l.outputs, 1);<em>\/\/l.group_id = 0<\/em>\n\t\t\t<em>\/\/\u5176\u5b9e\u5c31\u662fcopy_cpu(part_input_size, input, 1, l.output + offset, 1);<\/em>\n        }\n        <em>\/\/offset += input_size;<\/em>\n        offset += part_input_size;\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u4e0a\u91c7\u6837\u5c42[upsample]<\/h2>\n\n\n\n<p>yolov3-tiny\u4e2d\u7b2c19\u5c42\u662f\u4e0a\u91c7\u6837\u5c42\uff0c\u5c0618\u5c4213x13x128\u7684\u8f93\u5165\u7279\u5f81\u56fe\u8f6c\u53d8\u4e3a26x26x128\u7684\u8f93\u51fa\u7279\u5f81\u56fe\u3002\u5728upsample_layer.c\u4e2d\u7684forward_upsample_layer\u51fd\u6570\u4e2d\u5b8c\u6210\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void upsample_cpu(float *in, int w, int h, int c, int batch, int stride, int forward, float scale, float *out)\n{\n\t\n    int i, j, k, b;\n    for (b = 0; b &lt; batch; ++b) {\n        for (k = 0; k &lt; c; ++k) {\n            for (j = 0; j &lt; h*stride; ++j) {\n                for (i = 0; i &lt; w*stride; ++i) {\n                    int in_index = b*w*h*c + k*w*h + (j \/ stride)*w + i \/ stride;\n                    int out_index = b*w*h*c*stride*stride + k*w*h*stride*stride + j*w*stride + i;\n                    if (forward) out&#091;out_index] = scale*in&#091;in_index];\n                    else in&#091;in_index] += scale*out&#091;out_index];\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u91c7\u6837\u6548\u679c\uff1a<br><img src=\"https:\/\/img-blog.csdnimg.cn\/20200415173027192.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FsYW5nYWl4aWFveGlhbw==,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n\n\n\n<h2><a><\/a><a><\/a>\u8f93\u51fa\u5c42[yolo]<\/h2>\n\n\n\n<p>yolo\u5c42\u5b8c\u6210\u4e86\u5bf913x13x255\u548c26x26x255\u8f93\u5165\u7279\u8bca\u56fe\u7684logistic\u903b\u8f91\u56de\u5f52\u8ba1\u7b97\u3002\u6bcf\u4e2abox\u7684\u9884\u6d4b\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u4e0d\u53c2\u4e0e\u903b\u8f91\u56de\u5f52\uff0c\u5728yolo_layer.c\u4e2d\u7684forward_yolo_layer\u51fd\u6570\u4e2d\u5b8c\u6210\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/\u4e24\u4e2ayolo\u5c42 \u53ea\u5bf9\u6570\u636e\u8fdb\u884c\u4e86logistic\u5904\u7406\uff0c\u5e76\u6ca1\u6709\u9884\u6d4bbox\u7684\u4f4d\u7f6e<\/em>\n<em>\/\/\u5c060-1\u901a\u9053\uff08x,y\uff09 4-84(confidence+class)\u8ba1\u7b97logistic\uff0c\u4e09\u4e2aprior(\u9884\u6d4b\u6846\u90fd\u662f\u8fd9\u6837)<\/em>\nvoid forward_yolo_layer(const layer l, network_state state)\n{\n    int i, j, b, t, n;\n\t<em>\/\/\u4ecestate.input\u590d\u5236\u6570\u636e\u5230l.output<\/em>\n    memcpy(l.output, state.input, l.outputs*l.batch * sizeof(float));\n\n#ifndef GPU\n\tprintf(\"yolo v3 tiny l.n and l.batch of yolo layer is %d and %d  \\n \",l.n,l.batch);\n    for (b = 0; b &lt; l.batch; ++b) {<em>\/\/l.batch = 1<\/em>\n        for (n = 0; n &lt; l.n; ++n) {<em>\/\/l.n\uff1a3\uff08yolo\u5c42\uff09mask 0,1,2  \u8868\u793a\u6bcf\u4e2a\u7f51\u7edc\u5355\u5143\u9884\u6d4b\u4e09\u4e2abox?<\/em>\n\t\t\t\n\t\t\t<em>\/\/printf(\"l.coords is %d in yolov3 tiny yolo layer ,l.scale_x_y is %f \\n\", l.coords, l.scale_x_y);<\/em>\n            <em>\/\/ l.coords \u5750\u6807\uff1a0  l.classes\u5206\u7c7b\u6570\u91cf\uff1a80   l.scale_x_y:1<\/em>\n\t\t\t<em>\/\/l.w:\u8f93\u5165\u7279\u5f81\u56fe\u5bbd\u5ea6 l.h\u8f93\u51fa\u7279\u5f81\u56fe\u9ad8\u5ea6  <\/em>\n\t\t\tint index = entry_index(l, b, n*l.w*l.h, 0);<em>\/\/index = n*l.w*l.h*(4 + l.classes + 1)<\/em>\n\t\t\t\n\t\t   <em>\/\/\u8d77\u59cb\u5730\u5740\u4e3a\uff1al.output + index \u4e2a\u6570\u4e3a\uff1a2 * l.w*l.h  \u8ba1\u7b97\u903b\u8f91\u56de\u5f52\u503c\uff0c\u5e76\u4fdd\u5b58<\/em>\n            activate_array(l.output + index, 2 * l.w*l.h, LOGISTIC);  <em>\/\/ x,y,<\/em>\n\n\t\t\t<em>\/\/\u8d77\u59cb\u5730\u5740\u4e3a\uff1al.output + index \u4e2a\u6570\u4e3a\uff1a2 * l.w*l.h  \u8ba1\u7b97\u65b9\u5f0f\u4e3a\uff1ax = x*l.scale_x_y + -0.5*(l.scale_x_y - 1) \u7b80\u5316\u540e\uff1ax = x<\/em>\n\t\t\t<em>\/\/yolov3-tiny l.scale_x_y = 1  \u5b9e\u9645\u4e0a\u8be5\u51fd\u6570\u6ca1\u6709\u53c2\u4e0e\u4efb\u4f55\u7684\u8fd0\u7b97   scal_add_cpu<\/em>\n            scal_add_cpu(2 * l.w*l.h, l.scale_x_y, -0.5*(l.scale_x_y - 1), l.output + index, 1);    <em>\/\/ scale x,y<\/em>\n            \n\t\t\t<em>\/\/<\/em>\n\t\t\tindex = entry_index(l, b, n*l.w*l.h, 4);<em>\/\/index = n*l.w*l.h*(4 + l.classes + 1)+ 4*l.w*l.h<\/em>\n            \n\t\t\t<em>\/\/\u8d77\u59cb\u5730\u5740\u4e3a\uff1al.output + index,\u4e2a\u6570\u4e3a\uff1a\uff081+80\uff09*l.w*l.h   \u8ba1\u7b97\u5668\u5176\u903b\u8f91\u56de\u5f52\u503c<\/em>\n\t\t\tactivate_array(l.output + index, (1 + l.classes)*l.w*l.h, LOGISTIC);\n        }\n    }\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u9884\u6d4b\u7ed3\u679c\u7edf\u8ba1[detection ]<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n<em>\/\/w:\u8f93\u5165\u56fe\u50cf\u5bbd\u5ea6640,\u4e0d\u4e00\u5b9a\u662f416 h:\u8f93\u5165\u56fe\u50cf\u9ad8\u5ea6424,\u4e0d\u4e00\u5b9a\u662f416  thresh:\u56fe\u50cf\u7f6e\u4fe1\u5ea6\u9608\u503c0.25   hier:0.5<\/em>\n<em>\/\/map:0   relative:1  num:0   letter:0<\/em>\n<em>\/\/\u51fd\u6570\u4f5c\u7528\uff1a\u7edf\u8ba1\u4e24\u4e2ayolo\u5c42\u4e2d \u7f6e\u4fe1\u5ea6\u5927\u4e8e\u9608\u503c\u7684box\u4e2a\u6570\uff0c\u5e76\u5bf9\u8fd9\u4e2abox\u521d\u59cb\u5316\u4e00\u6bb5\u5730\u5740\u7a7a\u95f4 dets<\/em>\n<em>\/\/\u6839\u636e\u7f51\u7edc\u6765\u586b\u5145\u8be5\u5730\u5740\u7a7a\u95f4dets\uff1a<\/em>\n<em>\/\/\u6839\u636eyolo\u5c42 \u8ba1\u7b97\u6ee1\u8db3\u7f6e\u4fe1\u5ea6\u9608\u503c\u8981\u6c42\u7684box\u76f8\u5bf9\u7684\u9884\u6d4b\u5750\u6807\u3001\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728dets&#091;count].bbox\u7ed3\u6784\u4f53\u4e2d<\/em>\n<em>\/\/\u6bcf\u4e2abox\u670980\u4e2a\u7c7b\u522b\uff0c\u6709\u4e00\u4e2a\u7f6e\u4fe1\u5ea6\uff0c\u8be5\u7c7b\u522b\u5bf9\u5e94\u7684\u53ef\u80fd\u6027prob\uff1aclass\u6982\u7387*\u7f6e\u4fe1\u5ea6<\/em>\n<em>\/\/\/\u820d\u5f03prob\u5c0f\u4e8e\u9608\u503c0.25\u7684box<\/em>\n<em>\/\/\u5c06\u6ee1\u8db3\u9608\u503c\u7684box\u4e2a\u6570\u4fdd\u5b58\u5230num\u4e2d<\/em>\ndetection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, int *num, int letter)\n{\n\t<em>\/\/printf(\"w\u3001h\u3001thresh\u3001hier and letter is %d \u3001%d \u3001%f \u3001%f and %d\\n\", w, h, thresh, hier, letter);<\/em>\n\n\t<em>\/\/\u51fd\u6570\u4f5c\u7528\uff1a\u7edf\u8ba1\u4e24\u4e2ayolo\u5c42\u4e2d \u7f6e\u4fe1\u5ea6\u5927\u4e8e\u9608\u503c\u7684box\u4e2a\u6570\uff0c\u5e76\u5bf9\u8fd9\u4e2abox\u521d\u59cb\u5316\u4e00\u6bb5\u5730\u5740\u7a7a\u95f4 dets<\/em>\n\t<em>\/\/\u5c06\u6ee1\u8db3\u9608\u503c\u7684box\u4e2a\u6570\u4fdd\u5b58\u5230num\u4e2d<\/em>\n    detection *dets = make_network_boxes(net, thresh, num);\n\t\n\t<em>\/\/\u6839\u636e\u7f51\u7edc\u6765\u586b\u5145\u8be5\u5730\u5740\u7a7a\u95f4dets\uff1a<\/em>\n\t<em>\/\/\u6839\u636eyolo\u5c42 \u8ba1\u7b97\u6ee1\u8db3\u7f6e\u4fe1\u5ea6\u9608\u503c\u8981\u6c42\u7684box\u76f8\u5bf9\u7684\u9884\u6d4b\u5750\u6807\u3001\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728dets&#091;count].bbox\u7ed3\u6784\u4f53\u4e2d<\/em>\n\t<em>\/\/\u6bcf\u4e2abox\u670980\u4e2a\u7c7b\u522b\uff0c\u6709\u4e00\u4e2a\u7f6e\u4fe1\u5ea6\uff0c\u8be5\u7c7b\u522b\u5bf9\u5e94\u7684\u53ef\u80fd\u6027prob\uff1aclass\u6982\u7387*\u7f6e\u4fe1\u5ea6<\/em>\n\t<em>\/\/\/\u820d\u5f03prob\u5c0f\u4e8e\u9608\u503c0.25\u7684box<\/em>\n    fill_network_boxes(net, w, h, thresh, hier, map, relative, dets, letter);\n    return dets;\n}\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528<a href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\" target=\"_blank\" rel=\"noreferrer noopener\">make_network_boxes<\/a>\u6765\u521b\u5efa\u9884\u6d4b\u4fe1\u606f\u7684\u6307\u9488\u53d8\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ thresh:  \u7f6e\u4fe1\u5ea6\u9608\u503c\n\/\/num: 0\n\/\/\u51fd\u6570\u4f5c\u7528\uff1a\u7edf\u8ba1\u7f6e\u4fe1\u5ea6\u5927\u4e8e\u9608\u503c\u7684box\u4e2a\u6570\uff0c\u5e76\u5bf9\u8fd9\u4e2abox\u521d\u59cb\u5316\u4e00\u6bb5\u5730\u5740\u7a7a\u95f4\ndetection *make_network_boxes(network *net, float thresh, int *num)\n{\n    layer l = net-&gt;layers&#091;net-&gt;n - 1];\/\/\u5e94\u8be5\u662f\u795e\u7ecf\u7f51\u7edc\u6700\u540e\u4e00\u5c42 net-&gt;n:24 \u6700\u540e\u4e00\u5c42yolo\u5c42\n\t\/\/printf(\" net-&gt;n  of network is %d\\n \" ,(net-&gt;n));\n    int i;\n\t\/\/ -thresh 0.25\n\t\/\/yolo\u5c42\uff1ayolov3-tiny\u4e2d\u5171\u6709\u4e24\u5c42\n\t\/\/\u4e09\u4e2aprior\u9884\u6d4b\u6846\uff0c\u5bf9\u6bcf\u4e2a\u9884\u6d4b\u6846\u4e2d\uff0c\u7f6e\u4fe1\u5ea6\u5927\u4e8ethresh 0.25\uff0c\u8bb0\u4e3a\u4e00\u6b21\uff0c\u5c06\u6b21\u6570\u8fdb\u884c\u7d2f\u52a0\uff0c\u5e76\u8f93\u51fa\n\t\/\/nboxes:\u5373\u4e3a\u8981\u4fdd\u7559\u7684box\u7684\u4e2a\u6570 \u4e24\u4e2ayolo\u5c42\u4e2d\u7684\u7f6e\u4fe1\u5ea6\u4e2a\u6570\u4e00\u8d77\u7d2f\u52a0\n    int nboxes = num_detections(net, thresh);\/\/-thresh 0.25\n\n\tif (num) {\n\t\tprintf(\"nbox = %d \\n\", num);\n\t\t*num = nboxes;\/\/\u4e0d\u6267\u884c\u8be5\u8bed\u53e5\n\t}\n    \/\/\u7533\u8bf7\u5185\u5b58\uff0c\u4e2a\u6570\u4e3anboxes\uff0c\u6bcf\u4e2a\u5185\u5b58\u5927\u5c0f\u4e3a\uff1asizeof(detection)\n    detection* dets = (detection*)xcalloc(nboxes, sizeof(detection));\n\n\t\/\/\u904d\u5386\u6bcf\u4e2abox,\u6bcf\u4e2adets.prob\u7533\u8bf780\u4e2afloat\u7c7b\u578b\u7684\u5185\u5b58\uff1a\n\t\/\/dets.uc\uff0c\u7533\u8bf74\u4e2afloat\u7c7b\u578b\u7684\u7a7a\u95f4\uff1a\u4f4d\u7f6e\u4fe1\u606f\n    for (i = 0; i &lt; nboxes; ++i) {\n        dets&#091;i].prob = (float*)xcalloc(l.classes, sizeof(float));\n        \/\/ tx,ty,tw,th uncertainty\n        dets&#091;i].uc = (float*)xcalloc(4, sizeof(float)); \/\/ Gaussian_YOLOv3\n        \n\t\tif (l.coords &gt; 4) {\/\/\u4e0d\u6267\u884c\u8fd9\u4e2a\u5206\u652f l.coords\uff1a0\n            dets&#091;i].mask = (float*)xcalloc(l.coords - 4, sizeof(float));\n        }\n    }\n    return dets;\n}\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528<a href=\"https:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/details\/105533746\" target=\"_blank\" rel=\"noreferrer noopener\">get_yolo_detections<\/a>\u6765\u7edf\u8ba1\u4e24\u5c42yolo\u5c42\u7684\u9884\u6d4b\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/w,h:640,424    netw, neth:416,416 thresh:\u56fe\u50cf\u7f6e\u4fe1\u5ea6\u9608\u503c0.25   hier:0.5<\/em>\n<em>\/\/map:0   relative:1    letter:0<\/em>\n<em>\/\/\u6839\u636eyolo\u5c42 \u8ba1\u7b97\u6ee1\u8db3\u7f6e\u4fe1\u5ea6\u9608\u503c\u8981\u6c42\u7684box\u76f8\u5bf9\u7684\u9884\u6d4b\u5750\u6807\u3001\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728dets&#091;count].bbox\u7ed3\u6784\u4f53\u4e2d<\/em>\n<em>\/\/\u6bcf\u4e2abox\u670980\u4e2a\u7c7b\u522b\uff0c\u6709\u4e00\u4e2a\u7f6e\u4fe1\u5ea6\uff0c\u8be5\u7c7b\u522b\u5bf9\u5e94\u7684\u53ef\u80fd\u6027prob\uff1aclass\u6982\u7387*\u7f6e\u4fe1\u5ea6<\/em>\n<em>\/\/\/\u820d\u5f03prob\u5c0f\u4e8e\u9608\u503c0.25\u7684box<\/em>\nint get_yolo_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, int relative, detection *dets, int letter)\n{\n    printf(\"\\n l.batch = %d, l.w = %d, l.h = %d, l.n = %d ,netw = %d, neth = %d \\n\", l.batch, l.w, l.h, l.n, netw, neth);\n    int i,j,n;\n    float *predictions = l.output;<em>\/\/yolo\u5c42\u7684\u8f93\u51fa<\/em>\n    <em>\/\/ This snippet below is not necessary<\/em>\n    <em>\/\/ Need to comment it in order to batch processing &gt;= 2 images<\/em>\n    <em>\/\/if (l.batch == 2) avg_flipped_yolo(l);<\/em>\n    int count = 0;\n\n\t<em>\/\/printf(\"yolo layer l.mask&#091;0] is %d, l.mask&#091;1] is %d, l.mask&#091;2] is %d\\n\", l.mask&#091;0], l.mask&#091;1], l.mask&#091;2]);<\/em>\n\t<em>\/\/printf(\"yolo layer l.biases&#091;l.mask&#091;0]*2] is %f, l.biases&#091;l.mask&#091;1]*2] is %f, l.biases&#091;l.mask&#091;2]*2] is %f\\n\", l.biases&#091;l.mask&#091;0] * 2], l.biases&#091;l.mask&#091;1] * 2], l.biases&#091;l.mask&#091;2] * 2]);<\/em>\n\t<em>\/\/\u904d\u5386yolo\u5c42<\/em>\n    for (i = 0; i &lt; l.w*l.h; ++i){<em>\/\/\u8be5yolo\u5c42\u8f93\u51fa\u7279\u5f81\u56fe\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\uff1a13x13 26x26<\/em>\n        int row = i \/ l.w;\n        int col = i % l.w;\n        for(n = 0; n &lt; l.n; ++n){<em>\/\/yolo\u5c42\uff0cl.n = 3<\/em>\n\t\t\t\n            <em>\/\/obj_index:\u7f6e\u4fe1\u5ea6\u5c42\u7d22\u5f15<\/em>\n            int obj_index  = entry_index(l, 0, n*l.w*l.h + i, 4);<em>\/\/obj_index  = n*l.w*l.h*(4+l.classes+1) + 4*l.w*l.h + i;<\/em>\n            float objectness = predictions&#091;obj_index];<em>\/\/\u83b7\u5f97\u5bf9\u5e94\u7684\u7f6e\u4fe1\u5ea6<\/em>\n            <em>\/\/if(objectness &lt;= thresh) continue;    \/\/ incorrect behavior for Nan values<\/em>\n            \n\t\t\tif (objectness &gt; thresh) {<em>\/\/\u53ea\u6709\u7f6e\u4fe1\u5ea6\u5927\u4e8e\u9608\u503c\u624d\u5f00\u59cb\u6267\u884c\u8be5\u5206\u652f<\/em>\n                <em>\/\/printf(\"\\n objectness = %f, thresh = %f, i = %d, n = %d \\n\", objectness, thresh, i, n);<\/em>\n                \n\t\t\t\t<em>\/\/box_index:yolo\u5c42\u6bcf\u4e2a\u50cf\u7d20\u70b9\u6709\u4e09\u4e2abox,\u8868\u793a\u6bcf\u4e2abox\u7684\u7d22\u5f15\u503c<\/em>\n\t\t\t\tint box_index = entry_index(l, 0, n*l.w*l.h + i, 0);<em>\/\/box_index = n*l.w*l.h*(4+l.classes+1)+ i;<\/em>\n\n\t\t\t\t<em>\/\/l.biases-&gt;\u504f\u7f6e\u53c2\u6570\u8d77\u59cb\u5730\u5740    l.mask&#091;n]\uff1a\u5206\u522b\u4e3a3\uff0c4\uff0c5\uff0c0\uff0c1\uff0c2\uff0cbiases\u504f\u7f6e\u53c2\u6570\u504f\u79fb\u91cf<\/em>\n\t\t\t\t<em>\/\/\u6839\u636eyolo\u5c42 \u8ba1\u7b97\u6ee1\u8db3\u7f6e\u4fe1\u5ea6\u9608\u503c\u8981\u6c42\u7684box\u76f8\u5bf9\u7684\u9884\u6d4b\u5750\u6807\u3001\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728dets&#091;count].bbox\u7ed3\u6784\u4f53\u4e2d<\/em>\n                dets&#091;count].bbox = get_yolo_box(predictions, l.biases, l.mask&#091;n], box_index, col, row, l.w, l.h, netw, neth, l.w*l.h);\n\n\t\t\t\t<em>\/\/\u83b7\u53d6\u5bf9\u5e94\u7684\u7f6e\u4fe1\u5ea6\uff0c\u8be5\u7f6e\u4fe1\u5ea6\u7ecf\u8fc7\u4e86logistic<\/em>\n                dets&#091;count].objectness = objectness;\n\n\t\t\t\t<em>\/\/\u83b7\u5f97\u5206\u7c7b\u6570\uff1a80\uff08int\u7c7b\u578b\uff09<\/em>\n                dets&#091;count].classes = l.classes;\n                for (j = 0; j &lt; l.classes; ++j) {\n\t\t\t\t\t<em>\/\/80\u4e2a\u7c7b\u522b\uff0c\u6bcf\u4e2a\u7c7b\u522b\u5bf9\u5e94\u7684\u6982\u7387\uff0cclass_index\u4e3a\u5176\u6240\u5728\u5c42\u7684\u7d22\u5f15<\/em>\n                    int class_index = entry_index(l, 0, n*l.w*l.h + i, 4 + 1 + j);<em>\/\/class_index  = n*l.w*l.h*(4+l.classes+1) + \uff084+1+j\uff09*l.w*l.h + i;<\/em>\n                    <em>\/\/\u6bcf\u4e2abox\u670980\u4e2a\u7c7b\u522b\uff0c\u6709\u4e00\u4e2a\u7f6e\u4fe1\u5ea6\uff0c\u8be5\u7c7b\u522b\u5bf9\u5e94\u7684\u53ef\u80fd\u6027prob\uff1aclass\u6982\u7387*\u7f6e\u4fe1\u5ea6<\/em>\n\t\t\t\t\tfloat prob = objectness*predictions&#091;class_index];\n\t\t\t\t\t\n\t\t\t\t\t<em>\/\/\u820d\u5f03prob\u5c0f\u4e8e\u9608\u503c0.25\u7684box<\/em>\n                    dets&#091;count].prob&#091;j] = (prob &gt; thresh) ? prob : 0;\n                }\n                ++count;\n            }\n        }\n    }\n    correct_yolo_boxes(dets, count, w, h, netw, neth, relative, letter);\n    return count;\n}\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>\u975e\u6781\u5927\u503c\u6291\u5236[NMS]<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/dets:box\u7ed3\u6784\u4f53 nboxes:\u6ee1\u8db3\u9608\u503c\u7684box\u4e2a\u6570   l.classe:80    thresh=0.45f<\/em>\n<em>\/\/\u4e24\u4e2abox,\u540c\u4e00\u7c7b\u522b\u8fdb\u884c\u975e\u6781\u5927\u503c\u6291\u5236\uff0c\u904d\u5386<\/em>\nvoid do_nms_sort(detection *dets, int total, int classes, float thresh)\n{\n    int i, j, k;\n    k = total - 1;\n    for (i = 0; i &lt;= k; ++i) {<em>\/\/box\u4e2a\u6570<\/em>\n        if (dets&#091;i].objectness == 0) {<em>\/\/\u7f6e\u4fe1\u5ea6==0  \u4e0d\u6267\u884c\u8be5\u5206\u652f\uff0c\u7406\u8bba\u4e0a\u6ca1\u6709objectness = 0<\/em>\n\t\t\tprintf(\"there is no objectness == 0 !!! \\n\");\n            detection swap = dets&#091;i];\n            dets&#091;i] = dets&#091;k];\n            dets&#091;k] = swap;\n            --k;\n            --i;\n        }\n    }\n    total = k + 1;\n\t<em>\/\/\u540c\u4e00\u7c7b\u522b\u8fdb\u884c\u6bd4\u8f83<\/em>\n    for (k = 0; k &lt; classes; ++k) {<em>\/\/80\u4e2a        <\/em>\n        <em>\/\/box\u9884\u6d4b\u7684\u7c7b\u522b<\/em>\n\t\tfor (i = 0; i &lt; total; ++i) {<em>\/\/box\u4e2a\u6570<\/em>\n            dets&#091;i].sort_class = k;\n        }\n\t\t<em>\/\/\u51fd\u6570\u4f5c\u7528\uff1a\u5c06prob\u8f83\u5927\u7684box\u6392\u5217\u5230\u524d\u9762<\/em>\n        qsort(dets, total, sizeof(detection), nms_comparator_v3);\n        for (i = 0; i &lt; total; ++i) {<em>\/\/\u4e24\u4e2abox,\u540c\u4e00\u7c7b\u522b\u8fdb\u884c\u975e\u6781\u5927\u503c\u6291\u5236<\/em>\n            <em>\/\/printf(\"  k = %d, \\t i = %d \\n\", k, i);<\/em>\n            if (dets&#091;i].prob&#091;k] == 0) continue;\n            box a = dets&#091;i].bbox;\n            for (j = i + 1; j &lt; total;++j){\n\t\t\t\tbox b = dets&#091;j].bbox;\n\t\t\t\tif( box_iou(a, b) &gt; thresh) dets&#091;j].prob&#091;k] = 0;\n            }\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81ea\uff1ahttps:\/\/blog.csdn.net\/alangaixiaoxiao\/article\/detail &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/29\/yolov3-tiny\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">yolov3 -tiny \u7f51\u7edc\u5b9e\u73b0\u548c\u6e90\u7801\u5206\u6790<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,4],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/3193"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=3193"}],"version-history":[{"count":4,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/3193\/revisions"}],"predecessor-version":[{"id":3224,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/3193\/revisions\/3224"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=3193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=3193"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=3193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}