{"id":33577,"date":"2025-04-28T07:55:35","date_gmt":"2025-04-27T23:55:35","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/33577.html"},"modified":"2025-04-28T07:55:35","modified_gmt":"2025-04-27T23:55:35","slug":"kaggle%ef%bc%9a%e4%bd%bf%e7%94%a8kaggle%e6%9c%8d%e5%8a%a1%e5%99%a8%e8%ae%ad%e7%bb%83yolov5%e6%a8%a1%e5%9e%8b-%ef%bc%88%e7%99%bd%e5%ab%96%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/33577.html","title":{"rendered":"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09"},"content":{"rendered":"<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"[LOGO]:CoreKSets \" height=\"100\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235529-680ec3f1364f5.png\" width=\"100\" \/><\/p>\n<p style=\"text-align:center\"><span style=\"color:#ffd900\"><span style=\"background-color:#494949\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u3010\u6838\u77e5\u574a\u3011&#xff1a;\u91ca\u653e\u9752\u6625\u60f3\u8c61&#xff0c;\u7801\u52a8\u5168\u65b0\u89c6\u91ce\u3002\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span><\/span><\/p>\n<p style=\"text-align:center\"><span style=\"color:#ffd900\"><span style=\"background-color:#494949\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u7cbe\u7b80\u7684\u4fe1\u606f\u4f20\u8fbe\u77e5\u8bc6\u7684\u9aa8\u67b6&#xff0c;\u542f\u53d1\u521b\u9020\u8005\u5f00\u542f\u521b\u9020\u4e4b\u8def&#xff01;&#xff01;&#xff01;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span><\/span><\/p>\n<hr \/>\n<p id=\"%E5%86%85%E5%AE%B9%E6%91%98%E8%A6%81%EF%BC%9A%E5%9C%BA%E6%99%AF%2B%E9%97%AE%E9%A2%98%2B%E5%8F%97%E4%BC%97%2B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%2B%E7%BB%93%E6%9E%9C\"><span style=\"color:#4da8ee\">\u5185\u5bb9\u6458\u8981<\/span><span style=\"color:#494949\">&#xff1a;<\/span>\u6700\u8fd1\u9700\u8981\u4f7f\u7528 YOLOv5 \u6846\u67b6\u8bad\u7ec3\u4e00\u4e2a\u8bc6\u522b\u6a21\u578b&#xff0c;\u6570\u636e\u96c6\u67095000\u591a\u5f20\u56fe\u7247&#xff0c;\u76f4\u63a5\u57283000\u7b14\u8bb0\u672cCPU\u4e0a\u8dd1&#xff0c;\u6015\u662f\u8981\u51e0\u5929\u3002\u4e8e\u662f\u4ed8\u8d39\u4f7f\u7528 OpenBayes \u7684\u4e91\u670d\u52a1\u5668&#xff0c;\u4f46\u4e91\u670d\u52a1\u5668\u4f7f\u7528\u4ef7\u683c\u8f83\u8d35&#xff0c;\u914d\u7f6e\u91cd\u590d\u3002\u5bf9\u5c0f\u4f17\u9700\u6c42\u8005\u4e0d\u592a\u53cb\u597d&#xff0c;\u4e8e\u662f\u6211\u53bb Kaggle \u5c1d\u8bd5\u4e86\u767d\u5ad6\u670d\u52a1\u5668&#xff0c;\u5e76\u6210\u529f\u8bad\u7ec3\u4e86\u81ea\u5df1\u7684\u6a21\u578b\u3002<\/p>\n<p id=\"\"><span style=\"color:#4da8ee\">\u5173\u952e\u8bcd<\/span>&#xff1a;YOLOv5 Kaggle \u6a21\u578b\u8bad\u7ec3 <\/p>\n<hr \/>\n<p>\u5176\u4ed6\u76f8\u5173\u6587\u7ae0&#xff1a;<\/p>\n<p>\u26cf\u3002\u3002\u3002<\/p>\n<\/p>\n<h2 style=\"background-color:transparent;text-align:center\"><span style=\"color:#4da8ee\">\u9ad8\u6027\u80fd\u4e91\u670d\u52a1\u5668OpenBayes<\/span><\/h2>\n<hr \/>\n<p>\u82b1\u94b1\u53bbOpenBayes\u5f00\u4e91\u8ba1\u7b97\u670d\u52a1\u5668\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3&#xff0c;\u82b1\u94b1\u8bad\u7ec3\u7684\u5c31\u662f\u5feb&#xff0c;\u5927\u5185\u5b58\u53ef\u4ee5\u76f4\u63a5\u914d\u7f6e batch-size \u6279\u6b21 32 \u6216\u8005 64\u3002\u5934\u75bc\u7684\u7684\u6bcf\u6b21\u542f\u52a8\u90fd\u8981\u914d\u7f6e\u4e00\u4e0b\u73af\u5883&#xff0c;\u4e0b\u8f7d\u4e00\u4e9b\u4f9d\u8d56\u5305&#xff08;\u5173\u95ed\u4f1a\u88ab\u6e05\u9664&#xff0c;\u4e0d\u6e05\u695a\u539f\u56e0&#xff09;&#xff0c;\u6b63\u5f0f\u5f00\u59cb\u4f7f\u7528\u4e4b\u524d\u90fd\u8981\u82b1\u8d3910\u5206\u949f\u65f6\u95f4\u3002\u7528\u4e86\u51e0\u5929&#xff0c;\u82b1\u4e86\u621150\u5757\u5927\u6d0b\u540e&#xff0c;\u5fc3\u75bc\u4e0d\u5df2&#xff0c;\u4e8e\u662f\u53bbKaggle\u770b\u770b\u80fd\u4e0d\u80fd\u514d\u8d39\u8bad\u7ec3\u3002<\/p>\n<p>OpenBayes\u5bf9\u65b0\u7528\u6237\u4f18\u60e0\u529b\u5ea6\u5f88\u5927&#xff0c;\u6ce8\u518c\u8d60\u9001 240 \u5206\u949f RTX 4090 \u4f7f\u7528\u65f6\u957f&#043; 300 \u5206\u949f\u9ad8\u6027\u80fd CPU \u4f7f\u7528\u65f6\u957f\u3002\u76f4\u63a5\u70b9\u51fb\u524d\u5f80&#xff1a;signup &#8211; OpenBayes<\/p>\n<\/p>\n<h2 style=\"background-color:transparent;text-align:center\"><span style=\"color:#4da8ee\">Kaggle<\/span><\/h2>\n<hr \/>\n<p>Kaggle \u662f\u4e00\u4e2a\u5168\u7403\u9886\u5148\u7684\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u5e73\u53f0&#xff0c;\u7531 Anthony Goldbloom \u548c Ben Hamner \u4e8e 2010 \u5e74\u521b\u7acb&#xff0c;\u5e76\u4e8e 2017 \u5e74\u88ab Google \u6536\u8d2d&#xff0c;\u73b0\u4e3a Google LLC \u7684\u5b50\u516c\u53f8\u3002Kaggle \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5728\u7ebf\u793e\u533a&#xff0c;\u6c47\u805a\u4e86\u6765\u81ea\u4e16\u754c\u5404\u5730\u7684\u6570\u636e\u79d1\u5b66\u5bb6\u548c\u673a\u5668\u5b66\u4e60\u5de5\u7a0b\u5e08&#xff0c;\u81f4\u529b\u4e8e\u901a\u8fc7\u7ade\u8d5b\u3001\u6570\u636e\u96c6\u5206\u4eab\u548c\u4ee3\u7801\u534f\u4f5c\u63a8\u52a8\u4eba\u5de5\u667a\u80fd\u7684\u53d1\u5c55\u3002<\/p>\n<p>&#x1f381; Kaggle \u6bcf\u5468\u798f\u5229&#xff1a;\u514d\u8d39 GPU \u4f7f\u7528<\/p>\n<p>\u2705 \u6bcf\u5468 30 \u5c0f\u65f6\u514d\u8d39 GPU \u8d44\u6e90<\/p>\n<p>Kaggle \u4e3a\u6bcf\u4f4d\u7528\u6237\u6bcf\u5468\u63d0\u4f9b 30 \u5c0f\u65f6 \u7684\u514d\u8d39 GPU \u4f7f\u7528\u65f6\u95f4\u3002\u8fd9\u4e9b GPU \u8d44\u6e90\u53ef\u7528\u4e8e\u5728 Kaggle \u7684 Jupyter Notebook \u73af\u5883\u4e2d\u8fd0\u884c\u548c\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n<p>&#x1f4bb; \u652f\u6301\u7684 GPU \u7c7b\u578b<\/p>\n<p>Kaggle \u63d0\u4f9b\u7684 GPU \u7c7b\u578b\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\n<p>NVIDIA Tesla P100&#xff1a;16GB \u663e\u5b58&#xff0c;\u9002\u5408\u5927\u591a\u6570\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p>NVIDIA T4&#xff1a;15GB \u663e\u5b58&#xff0c;\u652f\u6301\u6df7\u5408\u7cbe\u5ea6\u8ba1\u7b97&#xff0c;\u9002\u7528\u4e8e\u63a8\u7406\u548c\u8bad\u7ec3\u4efb\u52a1\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7528\u6237\u53ef\u4ee5\u5728 Notebook \u8bbe\u7f6e\u4e2d\u9009\u62e9\u6240\u9700\u7684 GPU \u7c7b\u578b\u3002<\/p>\n<p>\u23f1\ufe0f \u4f7f\u7528\u9650\u5236<\/p>\n<ul>\n<li>\n<p>\u5355\u6b21\u8fd0\u884c\u65f6\u95f4&#xff1a;\u6bcf\u6b21 Notebook \u4f1a\u8bdd\u6700\u957f\u53ef\u8fd0\u884c 12 \u5c0f\u65f6&#xff0c;\u8d85\u8fc7\u65f6\u95f4\u5c06\u88ab\u5f3a\u5236\u4e2d\u65ad\u3002<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u5468\u603b\u65f6\u957f&#xff1a;\u6bcf\u4f4d\u7528\u6237\u6bcf\u5468\u6700\u591a\u53ef\u4f7f\u7528 30 \u5c0f\u65f6 \u7684 GPU \u8d44\u6e90\u3002<\/p>\n<\/li>\n<\/ul>\n<p>&#x1f31f; Kaggle \u7684\u6838\u5fc3\u529f\u80fd<\/p>\n<p>1. \u7ade\u8d5b&#xff08;Competitions&#xff09;<\/p>\n<p>Kaggle \u7684\u7ade\u8d5b\u5e73\u53f0\u5141\u8bb8\u4f01\u4e1a\u548c\u7814\u7a76\u673a\u6784\u53d1\u5e03\u5b9e\u9645\u95ee\u9898\u548c\u6570\u636e\u96c6&#xff0c;\u5438\u5f15\u5168\u7403\u7684\u6570\u636e\u79d1\u5b66\u5bb6\u53c2\u4e0e\u89e3\u51b3\u3002\u53c2\u8d5b\u8005\u9700\u5728\u89c4\u5b9a\u65f6\u95f4\u5185\u63d0\u4ea4\u6a21\u578b&#xff0c;\u5e73\u53f0\u6839\u636e\u9884\u8bbe\u7684\u8bc4\u4f30\u6307\u6807&#xff08;\u5982\u51c6\u786e\u7387\u3001AUC \u7b49&#xff09;\u8fdb\u884c\u6392\u540d\u3002\u4f18\u80dc\u8005\u4e0d\u4ec5\u53ef\u83b7\u5f97\u5956\u91d1&#xff0c;\u8fd8\u53ef\u80fd\u83b7\u5f97\u4e0e\u8d5e\u52a9\u4f01\u4e1a\u5408\u4f5c\u7684\u673a\u4f1a&#xff0c;\u751a\u81f3\u76f4\u63a5\u53d7\u8058\u4e8e\u8fd9\u4e9b\u516c\u53f8\u3002PyTorch Tutorial<\/p>\n<p>2.\u6570\u636e\u96c6&#xff08;Datasets&#xff09;<\/p>\n<p>Kaggle \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f00\u653e\u6570\u636e\u96c6&#xff0c;\u6db5\u76d6\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u3001\u91d1\u878d\u5206\u6790\u7b49\u591a\u4e2a\u9886\u57df\u3002\u7528\u6237\u53ef\u4ee5\u6d4f\u89c8\u3001\u4e0b\u8f7d\u8fd9\u4e9b\u6570\u636e\u96c6&#xff0c;\u7528\u4e8e\u5b66\u4e60\u3001\u7814\u7a76\u6216\u9879\u76ee\u5f00\u53d1\u3002kaggle-china.com<\/p>\n<p>3.\u4ee3\u7801\u5171\u4eab&#xff08;Code \/ Notebooks&#xff09;<\/p>\n<p>Kaggle \u63d0\u4f9b\u57fa\u4e8e\u4e91\u7684 Jupyter Notebook \u73af\u5883&#xff0c;\u79f0\u4e3a Kernels&#xff0c;\u7528\u6237\u53ef\u4ee5\u5728\u5176\u4e2d\u7f16\u5199\u3001\u8fd0\u884c\u548c\u5206\u4eab\u4ee3\u7801&#xff0c;\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u6a21\u578b\u8bad\u7ec3\u3002\u8fd9\u4fc3\u8fdb\u4e86\u793e\u533a\u6210\u5458\u4e4b\u95f4\u7684\u534f\u4f5c\u4e0e\u5b66\u4e60\u3002kaggle-china.com<\/p>\n<p>4.\u5b66\u4e60\u8d44\u6e90&#xff08;Kaggle Learn&#xff09;<\/p>\n<p>Kaggle Learn \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u514d\u8d39\u7684\u5fae\u8bfe\u7a0b&#xff0c;\u6db5\u76d6 Python \u7f16\u7a0b\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u673a\u5668\u5b66\u4e60\u3001\u6df1\u5ea6\u5b66\u4e60\u7b49\u4e3b\u9898&#xff0c;\u5e2e\u52a9\u7528\u6237\u7cfb\u7edf\u5730\u63d0\u5347\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2 style=\"background-color:transparent;text-align:center\"><span style=\"color:#4da8ee\">\u6570\u636e\u96c6\u4e0a\u4f20<\/span><\/h2>\n<hr \/>\n<p>\u6570\u636e\u96c6\u51c6\u5907&#xff1a;<\/p>\n<p>\u6309\u7167YOLO\u8981\u6c42\u7684\u683c\u5f0f&#xff0c;\u628a\u6570\u636e\u96c6\u6574\u7406\u597d\u540e\u6253\u5305\u4e3a data.zip \u538b\u7f29\u5305&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"515\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235529-680ec3f1616c5.png\" width=\"1418\" \/><\/p>\n<p>\u767b\u5f55\u6216\u6ce8\u518cKaggle\u540e\u70b9\u51fb\u5de6\u4fa7\u5bfc\u822a\u680f You Work :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"912\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235529-680ec3f1b7798.png\" width=\"1797\" \/><\/p>\n<p>\u70b9\u51fb Create,\u9009\u62e9 New Dataset:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"362\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235530-680ec3f26ce37.png\" width=\"451\" \/><\/p>\n<p>\u70b9\u51fb New Dataset:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"498\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235530-680ec3f299f19.png\" width=\"883\" \/><\/p>\n<p>\u53f3\u4fa7\u4f1a\u51fa\u73b0\u4e0a\u4f20\u754c\u9762&#xff0c;\u70b9\u51fb\u4e0a\u4f20 data.zip \u538b\u7f29\u5305&#xff0c;\u8be5\u8fc7\u7a0b\u770b\u4f60\u7684\u6570\u636e\u5927\u5c0f&#xff0c;\u9700\u8981\u8010\u5fc3\u7b49\u5f85\u3002\u8f93\u5165\u6570\u636e\u96c6\u540d\u79f0&#xff0c;\u53ef\u4ee5\u8ffd\u52a0\u4e0a\u4f20\u66f4\u591a\u6587\u4ef6\u3002\u6700\u540e\u70b9\u51fb\u53f3\u4e0b\u89d2 Create \u5b8c\u6210\u6570\u636e\u96c6\u521b\u5efa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"922\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235530-680ec3f2da47b.png\" width=\"931\" \/><\/p>\n<p>\u521b\u5efa\u6210\u529f\u540e\u4f60\u53ef\u4ee5\u5728\u5de6\u4fa7\u5bfc\u822a\u680f\u7684 You Work \u5185\u7684 DataSets \u770b\u5230\u6240\u6709\u4f60\u7684\u6570\u636e\u96c6\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"807\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235531-680ec3f33b505.png\" width=\"1451\" \/><\/p>\n<h2 style=\"background-color:transparent;text-align:center\"><span style=\"color:#4da8ee\">\u521b\u5efa\u7f16\u8f91\u5668&#xff08;NoteBook&#xff09;<\/span><\/h2>\n<hr \/>\n<p>\u70b9\u51fb Create,\u9009\u62e9 New Notebook:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"362\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235530-680ec3f26ce37.png\" width=\"451\" \/><\/p>\n<h4 style=\"background-color:transparent\"><span style=\"color:#4da8ee\">\u7ed1\u5b9a\u6570\u636e\u96c6<\/span><\/h4>\n<p>\u8fdb\u5165\u754c\u9762\u540e&#xff0c;\u70b9\u51fb\u53f3\u4fa7 &#043;Add Input \u6309\u94ae\u9884\u89c8\u6570\u636e\u96c6&#xff0c;\u5982\u679c\u4f60\u9884\u89c8\u8fc7\u81ea\u5df1\u521a\u4e0a\u4f20\u7684\u6570\u636e\u5e93&#xff0c;\u5c31\u53ef\u4ee5\u76f4\u63a5\u770b\u5230\u5b83&#xff0c;\u6ca1\u6709\u8bf7\u53bb\u9884\u89c8\u4e00\u4e0b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"935\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235531-680ec3f3a78fc.png\" width=\"1796\" \/><\/p>\n<p>\u7136\u540e\u70b9\u51fb\u4f60\u7684\u6570\u636e\u5e93\u9009\u9879\u53f3\u4e0b\u89d2\u7684 \u2795 \u53f7&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"933\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235532-680ec3f4665a1.png\" width=\"1810\" \/><\/p>\n<h4 style=\"background-color:transparent\"><span style=\"color:#4da8ee\">\u914d\u7f6eYOLOv5\u6846\u67b6<\/span><\/h4>\n<p>\u73b0\u5728\u5f00\u59cb&#xff0c;\u76f4\u63a5\u4f7f\u7528\u7f16\u8f91\u5668\u5f00\u59cb\u8fd0\u884c\u4ee3\u7801&#xff0c;\u670d\u52a1\u5668\u578b\u53f7\u53ef\u4ee5\u5728 Settings-&gt;Accelerator \u9009\u62e9&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u6b21\u8fd0\u884c\u4ee3\u7801\u65f6\u7cfb\u7edf\u81ea\u52a8\u542f\u52a8\u670d\u52a1\u5668<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"312\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235533-680ec3f527248.png\" width=\"808\" \/><\/p>\n<p>\u8bed\u6cd5\u89c4\u5219&#xff08;\u82f1\u6587&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>shell \u547d\u4ee4&#xff1a; \u4f7f\u7528\u611f\u53f9\u53f7\u201c!\u201d \u4f5c\u4e3a\u524d\u7f00&#xff0c;\u76f4\u63a5\u53ef\u4ee5\u5728\u7f16\u8f91\u5668\u4e2d\u8fd0\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>python \u4ee3\u7801&#xff1a;\u76f4\u63a5\u5199<\/p>\n<\/li>\n<\/ul>\n<p>\u62c9\u53d6YOLOv5\u6846\u67b6\u6e90\u7801&#xff0c;\u7b49\u5f85\u4e0b\u8f7d\u5b8c\u6210&#xff1a;<\/p>\n<p>\u00a0!git clone https:\/\/github.com\/ultralytics\/yolov5 <\/p>\n<p>\u5b8c\u6210\u540e&#xff0c;\u4f60\u53ef\u4ee5\u5728\u53f3\u4fa7\u72b6\u6001\u680f\u770b\u5230\u6587\u4ef6\u5939\u76ee\u5f55&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"937\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235533-680ec3f560528.png\" width=\"1817\" \/><\/p>\n<p>\u5207\u6362\u76ee\u5f55&#xff0c;\u8fdb\u5165yolov5\u6846\u67b6&#xff0c;\u5b89\u88c5\u4f9d\u8d56&#xff1a;<\/p>\n<p># \u5207\u6362\u5de5\u4f5c\u76ee\u5f55<br \/>\nimport os<br \/>\nos.chdir(&#039;\/kaggle\/working\/yolov5&#039;)<br \/>\n!ls<br \/>\n!pip install -r requirements.txt <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u73b0\u5728&#xff0c;\u53ef\u4ee5\u4e3a\u6240\u6b32\u4e3a\u4e86&#xff01;&#xff01;&#xff01;\u4f46\u662fKaggel\u7684\u7f16\u8f91\u5668\u4e0d\u80fd\u5728\u7ebf\u7f16\u8f91\u6846\u67b6\u91cc\u7684\u6e90\u7801&#xff0c;\u6240\u4ee5\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\u66ff\u6362\u547d\u4ee4&#xff1a;<span style=\"color:#fe2c24\">\u7b2c\u4e00\u884c (%%writefile &#043; \u6587\u4ef6\u8def\u5f84 ), \u7b2c\u4e00\u884c\u4e0b\u7684\u6240\u6709\u5185\u5bb9\u4f1a\u8986\u76d6\u539f\u59cb\u6587\u4ef6\u5185\u5bb9\u3002\u6211\u5efa\u8bae&#xff0c;\u81ea\u5df1\u5728\u7b14\u8bb0\u672c\u4e0a\u4fee\u6539&#xff0c;\u6539\u597d\u540e\u76f4\u63a5\u590d\u5236\u7c98\u8d34\u3002\u6ce8\u610f\u8def\u5f84\u524d\u7f00<\/span> \/kaggle\/working\/<\/p>\n<h4 style=\"background-color:transparent\"><span style=\"color:#4da8ee\"><span style=\"background-color:null\">\u914d\u7f6e\u6570\u636e\u96c6<\/span><\/span><\/h4>\n<p><span style=\"color:#fe2c24\">%%writefile \/kaggle\/working\/yolov5\/data\/coco128.yaml<\/span><\/p>\n<p><span style=\"color:#fe2c24\">path: \/kaggle\/input\/handsdata<\/span><\/p>\n<p>%%writefile \/kaggle\/working\/yolov5\/data\/coco128.yaml<br \/>\n# \u4fee\u6539coco128<br \/>\npath: \/kaggle\/input\/handsdata<br \/>\ntrain: images\/train<br \/>\nval: images\/val<br \/>\nnc: 1<br \/>\nnames: [&#039;Hand&#039;]<\/p>\n<h4><span style=\"color:#4da8ee\">\u4fee\u6539\u8d85\u53c2\u6570\u6587\u4ef6&#xff08;\u8fdb\u9636&#xff0c;\u53ef\u9009&#xff09;<\/span><\/h4>\n<p>%%writefile \/kaggle\/working\/yolov5\/data\/hyps\/hyp.scratch-low.yaml<br \/>\n# \u4fee\u6539\u8d85\u53c2\u6570\u6587\u4ef6<br \/>\nlr0: 0.01 # initial learning rate (SGD&#061;1E-2, Adam&#061;1E-3)<br \/>\nlrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf)<br \/>\nmomentum: 0.937 # SGD momentum\/Adam beta1<br \/>\nweight_decay: 0.0005 # optimizer weight decay 5e-4<br \/>\nwarmup_epochs: 3.0 # warmup epochs (fractions ok)<br \/>\nwarmup_momentum: 0.8 # warmup initial momentum<br \/>\nwarmup_bias_lr: 0.1 # warmup initial bias lr<br \/>\nbox: 0.05 # box loss gain<br \/>\ncls: 0.5 # cls loss gain<br \/>\ncls_pw: 1.0 # cls BCELoss positive_weight<br \/>\nobj: 1.0 # obj loss gain (scale with pixels)<br \/>\nobj_pw: 1.0 # obj BCELoss positive_weight<br \/>\niou_t: 0.20 # IoU training threshold<br \/>\nanchor_t: 4.0 # anchor-multiple threshold<br \/>\n# anchors: 3  # anchors per output layer (0 to ignore)<br \/>\nfl_gamma: 0.0 # focal loss gamma (efficientDet default gamma&#061;1.5)<br \/>\nhsv_h: 0.015 # image HSV-Hue augmentation (fraction)<br \/>\nhsv_s: 0.7 # image HSV-Saturation augmentation (fraction)<br \/>\nhsv_v: 0.4 # image HSV-Value augmentation (fraction)<br \/>\ndegrees: 0.0 # image rotation (&#043;\/- deg)<br \/>\ntranslate: 0.1 # image translation (&#043;\/- fraction)<br \/>\nscale: 0.5 # image scale (&#043;\/- gain)<br \/>\nshear: 0.0 # image shear (&#043;\/- deg)<br \/>\nperspective: 0.0 # image perspective (&#043;\/- fraction), range 0-0.001<br \/>\nflipud: 0.0 # image flip up-down (probability)<br \/>\nfliplr: 0.5 # image flip left-right (probability)<br \/>\nmosaic: 1.0 # image mosaic (probability)<br \/>\nmixup: 0.0 # image mixup (probability)<br \/>\ncopy_paste: 0.0 # segment copy-paste (probability)<\/p>\n<h4 style=\"background-color:transparent\"><span style=\"color:#4da8ee\">\u4fee\u6539\u6a21\u578b\u7ed3\u6784\u6587\u4ef6&#xff08;\u8fdb\u9636&#xff0c;\u53ef\u9009&#xff09;<\/span><\/h4>\n<p>%%writefile \/kaggle\/working\/yolov5\/models\/yolov5n.yaml<br \/>\n# YOLOv5 &#x1f680; by Ultralytics, GPL-3.0 license<br \/>\n# Parameters<br \/>\nnc: 1  # number of classes<br \/>\ndepth_multiple: 0.33  # model depth multiple<br \/>\nwidth_multiple: 0.25  # layer channel multiple<br \/>\nanchors:<br \/>\n  &#8211; [10,13, 16,30, 33,23]  # P3\/8<br \/>\n  &#8211; [30,61, 62,45, 59,119]  # P4\/16<br \/>\n  &#8211; [116,90, 156,198, 373,326]  # P5\/32<br \/>\nactivation: nn.SiLU()<br \/>\n# YOLOv5 v6.0 backbone<br \/>\nbackbone:<br \/>\n  # [from, number, module, args]<br \/>\n  [[-1, 1, Conv, [64, 6, 2, 2]],  # 0-P1\/2<br \/>\n   [-1, 1, Conv, [128, 3, 2]],  # 1-P2\/4<br \/>\n   [-1, 1, C3, [128, 1]],  # 2<br \/>\n   [-1, 1, Conv, [256, 3, 2]],  # 3-P3\/8<br \/>\n   [-1, 1, C3, [256, 1]],  # 4<br \/>\n   [-1, 1, Conv, [512, 3, 2]],  # 5-P4\/16<br \/>\n   [-1, 1, C3, [512, 1]],  # 6<br \/>\n   [-1, 1, Conv, [1024, 3, 2]],  # 7-P5\/32<br \/>\n   [-1, 1, C3, [1024, 1]],  # 8<br \/>\n   [-1, 1, SPPF, [1024, 5]],  # 9<br \/>\n  ]<\/p>\n<p># YOLOv5 v6.0 head<br \/>\nhead:<br \/>\n  [[-1, 1, Conv, [512, 1, 1]],<br \/>\n   [-1, 1, nn.Upsample, [None, 2, &#039;nearest&#039;]],<br \/>\n   [[-1, 6], 1, Concat, [1]],  # cat backbone P4<br \/>\n   [-1, 1, C3, [512, 1]],  # 13<\/p>\n<p>   [-1, 1, Conv, [256, 1, 1]],<br \/>\n   [-1, 1, nn.Upsample, [None, 2, &#039;nearest&#039;]],<br \/>\n   [[-1, 4], 1, Concat, [1]],  # cat backbone P3<br \/>\n   [-1, 1, C3, [256, 1]],  # 17 (P3\/8-small)<\/p>\n<p>   [-1, 1, Conv, [256, 3, 2]],<br \/>\n   [[-1, 13], 1, Concat, [1]],  # cat head P4<br \/>\n   [-1, 1, C3, [512, 1]],  # 20 (P4\/16-medium)<\/p>\n<p>   [-1, 1, Conv, [512, 3, 2]],<br \/>\n   [[-1, 9], 1, Concat, [1]],  # cat head P5<br \/>\n   [-1, 1, C3, [1024, 1]],  # 23 (P5\/32-large)<\/p>\n<p>   [[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)<br \/>\n  ]<\/p>\n<h4 style=\"background-color:transparent\"><span style=\"color:#4da8ee\">\u914d\u7f6e\u6a21\u5757\u8f93\u5165\u53c2\u6570&#xff08;\u8fdb\u9636&#xff0c;\u53ef\u9009&#xff09;<\/span><\/h4>\n<p>%%writefile \/kaggle\/working\/yolov5\/models\/common.py<br \/>\n# \u4fee\u6539 common.py<\/p>\n<p># Ultralytics &#x1f680; AGPL-3.0 License &#8211; https:\/\/ultralytics.com\/license<br \/>\n&#034;&#034;&#034;Common modules.&#034;&#034;&#034;<\/p>\n<p>import ast<br \/>\nimport contextlib<br \/>\nimport json<br \/>\nimport math<br \/>\nimport platform<br \/>\nimport warnings<br \/>\nimport zipfile<br \/>\nfrom collections import OrderedDict, namedtuple<br \/>\nfrom copy import copy<br \/>\nfrom pathlib import Path<br \/>\nfrom urllib.parse import urlparse<\/p>\n<p>import cv2<br \/>\nimport numpy as np<br \/>\nimport pandas as pd<br \/>\nimport requests<br \/>\nimport torch<br \/>\nimport torch.nn as nn<br \/>\nfrom PIL import Image<br \/>\nfrom torch.cuda import amp<\/p>\n<p># Import &#039;ultralytics&#039; package or install if missing<br \/>\ntry:<br \/>\n    import ultralytics<\/p>\n<p>    assert hasattr(ultralytics, &#034;__version__&#034;)  # verify package is not directory<br \/>\nexcept (ImportError, AssertionError):<br \/>\n    import os<\/p>\n<p>    os.system(&#034;pip install -U ultralytics&#034;)<br \/>\n    import ultralytics<\/p>\n<p>from ultralytics.utils.plotting import Annotator, colors, save_one_box<\/p>\n<p>from utils import TryExcept<br \/>\nfrom utils.dataloaders import exif_transpose, letterbox<br \/>\nfrom utils.general import (<br \/>\n    LOGGER,<br \/>\n    ROOT,<br \/>\n    Profile,<br \/>\n    check_requirements,<br \/>\n    check_suffix,<br \/>\n    check_version,<br \/>\n    colorstr,<br \/>\n    increment_path,<br \/>\n    is_jupyter,<br \/>\n    make_divisible,<br \/>\n    non_max_suppression,<br \/>\n    scale_boxes,<br \/>\n    xywh2xyxy,<br \/>\n    xyxy2xywh,<br \/>\n    yaml_load,<br \/>\n)<br \/>\nfrom utils.torch_utils import copy_attr, smart_inference_mode<\/p>\n<p>def autopad(k, p&#061;None, d&#061;1):<br \/>\n    &#034;&#034;&#034;<br \/>\n    Pads kernel to &#039;same&#039; output shape, adjusting for optional dilation; returns padding size.<\/p>\n<p>    &#096;k&#096;: kernel, &#096;p&#096;: padding, &#096;d&#096;: dilation.<br \/>\n    &#034;&#034;&#034;<br \/>\n    if d &gt; 1:<br \/>\n        k &#061; d * (k &#8211; 1) &#043; 1 if isinstance(k, int) else [d * (x &#8211; 1) &#043; 1 for x in k]  # actual kernel-size<br \/>\n    if p is None:<br \/>\n        p &#061; k \/\/ 2 if isinstance(k, int) else [x \/\/ 2 for x in k]  # auto-pad<br \/>\n    return p<\/p>\n<p>class Conv(nn.Module):<br \/>\n    &#034;&#034;&#034;Applies a convolution, batch normalization, and activation function to an input tensor in a neural network.&#034;&#034;&#034;<\/p>\n<p>    default_act &#061; nn.ReLU6()  # default activation<\/p>\n<p>    def __init__(self, c1, c2, k&#061;1, s&#061;1, p&#061;None, g&#061;1, d&#061;1, act&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes a standard convolution layer with optional batch normalization and activation.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.conv &#061; nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups&#061;g, dilation&#061;d, bias&#061;False)<br \/>\n        self.bn &#061; nn.BatchNorm2d(c2)<br \/>\n        self.act &#061; self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Applies a convolution followed by batch normalization and an activation function to the input tensor &#096;x&#096;.&#034;&#034;&#034;<br \/>\n        return self.act(self.bn(self.conv(x)))<\/p>\n<p>    def forward_fuse(self, x):<br \/>\n        &#034;&#034;&#034;Applies a fused convolution and activation function to the input tensor &#096;x&#096;.&#034;&#034;&#034;<br \/>\n        return self.act(self.conv(x))<\/p>\n<p>class DWConv(Conv):<br \/>\n    &#034;&#034;&#034;Implements a depth-wise convolution layer with optional activation for efficient spatial filtering.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;1, s&#061;1, d&#061;1, act&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes a depth-wise convolution layer with optional activation; args: input channels (c1), output<br \/>\n        channels (c2), kernel size (k), stride (s), dilation (d), and activation flag (act).<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__(c1, c2, k, s, g&#061;math.gcd(c1, c2), d&#061;d, act&#061;act)<\/p>\n<p>class DWConvTranspose2d(nn.ConvTranspose2d):<br \/>\n    &#034;&#034;&#034;A depth-wise transpose convolutional layer for upsampling in neural networks, particularly in YOLOv5 models.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;1, s&#061;1, p1&#061;0, p2&#061;0):<br \/>\n        &#034;&#034;&#034;Initializes a depth-wise transpose convolutional layer for YOLOv5; args: input channels (c1), output channels<br \/>\n        (c2), kernel size (k), stride (s), input padding (p1), output padding (p2).<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__(c1, c2, k, s, p1, p2, groups&#061;math.gcd(c1, c2))<\/p>\n<p>class TransformerLayer(nn.Module):<br \/>\n    &#034;&#034;&#034;Transformer layer with multihead attention and linear layers, optimized by removing LayerNorm.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c, num_heads):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Initializes a transformer layer, sans LayerNorm for performance, with multihead attention and linear layers.<\/p>\n<p>        See  as described in https:\/\/arxiv.org\/abs\/2010.11929.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.q &#061; nn.Linear(c, c, bias&#061;False)<br \/>\n        self.k &#061; nn.Linear(c, c, bias&#061;False)<br \/>\n        self.v &#061; nn.Linear(c, c, bias&#061;False)<br \/>\n        self.ma &#061; nn.MultiheadAttention(embed_dim&#061;c, num_heads&#061;num_heads)<br \/>\n        self.fc1 &#061; nn.Linear(c, c, bias&#061;False)<br \/>\n        self.fc2 &#061; nn.Linear(c, c, bias&#061;False)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Performs forward pass using MultiheadAttention and two linear transformations with residual connections.&#034;&#034;&#034;<br \/>\n        x &#061; self.ma(self.q(x), self.k(x), self.v(x))[0] &#043; x<br \/>\n        x &#061; self.fc2(self.fc1(x)) &#043; x<br \/>\n        return x<\/p>\n<p>class TransformerBlock(nn.Module):<br \/>\n    &#034;&#034;&#034;A Transformer block for vision tasks with convolution, position embeddings, and Transformer layers.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, num_heads, num_layers):<br \/>\n        &#034;&#034;&#034;Initializes a Transformer block for vision tasks, adapting dimensions if necessary and stacking specified<br \/>\n        layers.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.conv &#061; None<br \/>\n        if c1 !&#061; c2:<br \/>\n            self.conv &#061; Conv(c1, c2)<br \/>\n        self.linear &#061; nn.Linear(c2, c2)  # learnable position embedding<br \/>\n        self.tr &#061; nn.Sequential(*(TransformerLayer(c2, num_heads) for _ in range(num_layers)))<br \/>\n        self.c2 &#061; c2<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through an optional convolution, followed by Transformer layers and position embeddings for<br \/>\n        object detection.<br \/>\n        &#034;&#034;&#034;<br \/>\n        if self.conv is not None:<br \/>\n            x &#061; self.conv(x)<br \/>\n        b, _, w, h &#061; x.shape<br \/>\n        p &#061; x.flatten(2).permute(2, 0, 1)<br \/>\n        return self.tr(p &#043; self.linear(p)).permute(1, 2, 0).reshape(b, self.c2, w, h)<\/p>\n<p>class Bottleneck(nn.Module):<br \/>\n    &#034;&#034;&#034;A bottleneck layer with optional shortcut and group convolution for efficient feature extraction.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, shortcut&#061;True, g&#061;1, e&#061;0.5):<br \/>\n        &#034;&#034;&#034;Initializes a standard bottleneck layer with optional shortcut and group convolution, supporting channel<br \/>\n        expansion.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; int(c2 * e)  # hidden channels<br \/>\n        self.cv1 &#061; Conv(c1, c_, 1, 1)<br \/>\n        self.cv2 &#061; Conv(c_, c2, 3, 1, g&#061;g)<br \/>\n        self.add &#061; shortcut and c1 &#061;&#061; c2<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through two convolutions, optionally adds shortcut if channel dimensions match; input is a<br \/>\n        tensor.<br \/>\n        &#034;&#034;&#034;<br \/>\n        return x &#043; self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))<\/p>\n<p>class BottleneckCSP(nn.Module):<br \/>\n    &#034;&#034;&#034;CSP bottleneck layer for feature extraction with cross-stage partial connections and optional shortcuts.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, n&#061;1, shortcut&#061;True, g&#061;1, e&#061;0.5):<br \/>\n        &#034;&#034;&#034;Initializes CSP bottleneck with optional shortcuts; args: ch_in, ch_out, number of repeats, shortcut bool,<br \/>\n        groups, expansion.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; int(c2 * e)  # hidden channels<br \/>\n        self.cv1 &#061; Conv(c1, c_, 1, 1)<br \/>\n        self.cv2 &#061; nn.Conv2d(c1, c_, 1, 1, bias&#061;False)<br \/>\n        self.cv3 &#061; nn.Conv2d(c_, c_, 1, 1, bias&#061;False)<br \/>\n        self.cv4 &#061; Conv(2 * c_, c2, 1, 1)<br \/>\n        self.bn &#061; nn.BatchNorm2d(2 * c_)  # applied to cat(cv2, cv3)<br \/>\n        self.act &#061; nn.SiLU()<br \/>\n        self.m &#061; nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e&#061;1.0) for _ in range(n)))<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Performs forward pass by applying layers, activation, and concatenation on input x, returning feature-<br \/>\n        enhanced output.<br \/>\n        &#034;&#034;&#034;<br \/>\n        y1 &#061; self.cv3(self.m(self.cv1(x)))<br \/>\n        y2 &#061; self.cv2(x)<br \/>\n        return self.cv4(self.act(self.bn(torch.cat((y1, y2), 1))))<\/p>\n<p>class CrossConv(nn.Module):<br \/>\n    &#034;&#034;&#034;Implements a cross convolution layer with downsampling, expansion, and optional shortcut.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;3, s&#061;1, g&#061;1, e&#061;1.0, shortcut&#061;False):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Initializes CrossConv with downsampling, expanding, and optionally shortcutting; &#096;c1&#096; input, &#096;c2&#096; output<br \/>\n        channels.<\/p>\n<p>        Inputs are ch_in, ch_out, kernel, stride, groups, expansion, shortcut.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; int(c2 * e)  # hidden channels<br \/>\n        self.cv1 &#061; Conv(c1, c_, (1, k), (1, s))<br \/>\n        self.cv2 &#061; Conv(c_, c2, (k, 1), (s, 1), g&#061;g)<br \/>\n        self.add &#061; shortcut and c1 &#061;&#061; c2<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Performs feature sampling, expanding, and applies shortcut if channels match; expects &#096;x&#096; input tensor.&#034;&#034;&#034;<br \/>\n        return x &#043; self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))<\/p>\n<p>class C3(nn.Module):<br \/>\n    &#034;&#034;&#034;Implements a CSP Bottleneck module with three convolutions for enhanced feature extraction in neural networks.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, n&#061;1, shortcut&#061;True, g&#061;1, e&#061;0.5):<br \/>\n        &#034;&#034;&#034;Initializes C3 module with options for channel count, bottleneck repetition, shortcut usage, group<br \/>\n        convolutions, and expansion.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; int(c2 * e)  # hidden channels<br \/>\n        self.cv1 &#061; Conv(c1, c_, 1, 1)<br \/>\n        self.cv2 &#061; Conv(c1, c_, 1, 1)<br \/>\n        self.cv3 &#061; Conv(2 * c_, c2, 1)  # optional act&#061;FReLU(c2)<br \/>\n        self.m &#061; nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e&#061;1.0) for _ in range(n)))<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Performs forward propagation using concatenated outputs from two convolutions and a Bottleneck sequence.&#034;&#034;&#034;<br \/>\n        return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))<\/p>\n<p>class C3x(C3):<br \/>\n    &#034;&#034;&#034;Extends the C3 module with cross-convolutions for enhanced feature extraction in neural networks.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, n&#061;1, shortcut&#061;True, g&#061;1, e&#061;0.5):<br \/>\n        &#034;&#034;&#034;Initializes C3x module with cross-convolutions, extending C3 with customizable channel dimensions, groups,<br \/>\n        and expansion.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__(c1, c2, n, shortcut, g, e)<br \/>\n        c_ &#061; int(c2 * e)<br \/>\n        self.m &#061; nn.Sequential(*(CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)))<\/p>\n<p>class C3TR(C3):<br \/>\n    &#034;&#034;&#034;C3 module with TransformerBlock for enhanced feature extraction in object detection models.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, n&#061;1, shortcut&#061;True, g&#061;1, e&#061;0.5):<br \/>\n        &#034;&#034;&#034;Initializes C3 module with TransformerBlock for enhanced feature extraction, accepts channel sizes, shortcut<br \/>\n        config, group, and expansion.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__(c1, c2, n, shortcut, g, e)<br \/>\n        c_ &#061; int(c2 * e)<br \/>\n        self.m &#061; TransformerBlock(c_, c_, 4, n)<\/p>\n<p>class C3SPP(C3):<br \/>\n    &#034;&#034;&#034;Extends the C3 module with an SPP layer for enhanced spatial feature extraction and customizable channels.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;(5, 9, 13), n&#061;1, shortcut&#061;True, g&#061;1, e&#061;0.5):<br \/>\n        &#034;&#034;&#034;Initializes a C3 module with SPP layer for advanced spatial feature extraction, given channel sizes, kernel<br \/>\n        sizes, shortcut, group, and expansion ratio.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__(c1, c2, n, shortcut, g, e)<br \/>\n        c_ &#061; int(c2 * e)<br \/>\n        self.m &#061; SPP(c_, c_, k)<\/p>\n<p>class C3Ghost(C3):<br \/>\n    &#034;&#034;&#034;Implements a C3 module with Ghost Bottlenecks for efficient feature extraction in YOLOv5.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, n&#061;1, shortcut&#061;True, g&#061;1, e&#061;0.5):<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5&#039;s C3 module with Ghost Bottlenecks for efficient feature extraction.&#034;&#034;&#034;<br \/>\n        super().__init__(c1, c2, n, shortcut, g, e)<br \/>\n        c_ &#061; int(c2 * e)  # hidden channels<br \/>\n        self.m &#061; nn.Sequential(*(GhostBottleneck(c_, c_) for _ in range(n)))<\/p>\n<p>class SPP(nn.Module):<br \/>\n    &#034;&#034;&#034;Implements Spatial Pyramid Pooling (SPP) for feature extraction, ref: https:\/\/arxiv.org\/abs\/1406.4729.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;(5, 9, 13)):<br \/>\n        &#034;&#034;&#034;Initializes SPP layer with Spatial Pyramid Pooling, ref: https:\/\/arxiv.org\/abs\/1406.4729, args: c1 (input channels), c2 (output channels), k (kernel sizes).&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; c1 \/\/ 2  # hidden channels<br \/>\n        self.cv1 &#061; Conv(c1, c_, 1, 1)<br \/>\n        self.cv2 &#061; Conv(c_ * (len(k) &#043; 1), c2, 1, 1)<br \/>\n        self.m &#061; nn.ModuleList([nn.MaxPool2d(kernel_size&#061;x, stride&#061;1, padding&#061;x \/\/ 2) for x in k])<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Applies convolution and max pooling layers to the input tensor &#096;x&#096;, concatenates results, and returns output<br \/>\n        tensor.<br \/>\n        &#034;&#034;&#034;<br \/>\n        x &#061; self.cv1(x)<br \/>\n        with warnings.catch_warnings():<br \/>\n            warnings.simplefilter(&#034;ignore&#034;)  # suppress torch 1.9.0 max_pool2d() warning<br \/>\n            return self.cv2(torch.cat([x] &#043; [m(x) for m in self.m], 1))<\/p>\n<p>class SPPF(nn.Module):<br \/>\n    &#034;&#034;&#034;Implements a fast Spatial Pyramid Pooling (SPPF) layer for efficient feature extraction in YOLOv5 models.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;5):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Initializes YOLOv5 SPPF layer with given channels and kernel size for YOLOv5 model, combining convolution and<br \/>\n        max pooling.<\/p>\n<p>        Equivalent to SPP(k&#061;(5, 9, 13)).<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; c1 \/\/ 2  # hidden channels<br \/>\n        self.cv1 &#061; Conv(c1, c_, 1, 1)<br \/>\n        self.cv2 &#061; Conv(c_ * 4, c2, 1, 1)<br \/>\n        self.m &#061; nn.MaxPool2d(kernel_size&#061;k, stride&#061;1, padding&#061;k \/\/ 2)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through a series of convolutions and max pooling operations for feature extraction.&#034;&#034;&#034;<br \/>\n        x &#061; self.cv1(x)<br \/>\n        with warnings.catch_warnings():<br \/>\n            warnings.simplefilter(&#034;ignore&#034;)  # suppress torch 1.9.0 max_pool2d() warning<br \/>\n            y1 &#061; self.m(x)<br \/>\n            y2 &#061; self.m(y1)<br \/>\n            return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))<\/p>\n<p>class Focus(nn.Module):<br \/>\n    &#034;&#034;&#034;Focuses spatial information into channel space using slicing and convolution for efficient feature extraction.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;1, s&#061;1, p&#061;None, g&#061;1, act&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes Focus module to concentrate width-height info into channel space with configurable convolution<br \/>\n        parameters.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.conv &#061; Conv(c1 * 4, c2, k, s, p, g, act&#061;act)<br \/>\n        # self.contract &#061; Contract(gain&#061;2)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through Focus mechanism, reshaping (b,c,w,h) to (b,4c,w\/2,h\/2) then applies convolution.&#034;&#034;&#034;<br \/>\n        return self.conv(torch.cat((x[&#8230;, ::2, ::2], x[&#8230;, 1::2, ::2], x[&#8230;, ::2, 1::2], x[&#8230;, 1::2, 1::2]), 1))<br \/>\n        # return self.conv(self.contract(x))<\/p>\n<p>class GhostConv(nn.Module):<br \/>\n    &#034;&#034;&#034;Implements Ghost Convolution for efficient feature extraction, see https:\/\/github.com\/huawei-noah\/ghostnet.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;1, s&#061;1, g&#061;1, act&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes GhostConv with in\/out channels, kernel size, stride, groups, and activation; halves out channels<br \/>\n        for efficiency.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; c2 \/\/ 2  # hidden channels<br \/>\n        self.cv1 &#061; Conv(c1, c_, k, s, None, g, act&#061;act)<br \/>\n        self.cv2 &#061; Conv(c_, c_, 5, 1, None, c_, act&#061;act)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Performs forward pass, concatenating outputs of two convolutions on input &#096;x&#096;: shape (B,C,H,W).&#034;&#034;&#034;<br \/>\n        y &#061; self.cv1(x)<br \/>\n        return torch.cat((y, self.cv2(y)), 1)<\/p>\n<p>class GhostBottleneck(nn.Module):<br \/>\n    &#034;&#034;&#034;Efficient bottleneck layer using Ghost Convolutions, see https:\/\/github.com\/huawei-noah\/ghostnet.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k&#061;3, s&#061;1):<br \/>\n        &#034;&#034;&#034;Initializes GhostBottleneck with ch_in &#096;c1&#096;, ch_out &#096;c2&#096;, kernel size &#096;k&#096;, stride &#096;s&#096;; see https:\/\/github.com\/huawei-noah\/ghostnet.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; c2 \/\/ 2<br \/>\n        self.conv &#061; nn.Sequential(<br \/>\n            GhostConv(c1, c_, 1, 1),  # pw<br \/>\n            DWConv(c_, c_, k, s, act&#061;False) if s &#061;&#061; 2 else nn.Identity(),  # dw<br \/>\n            GhostConv(c_, c2, 1, 1, act&#061;False),<br \/>\n        )  # pw-linear<br \/>\n        self.shortcut &#061; (<br \/>\n            nn.Sequential(DWConv(c1, c1, k, s, act&#061;False), Conv(c1, c2, 1, 1, act&#061;False)) if s &#061;&#061; 2 else nn.Identity()<br \/>\n        )<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through conv and shortcut layers, returning their summed output.&#034;&#034;&#034;<br \/>\n        return self.conv(x) &#043; self.shortcut(x)<\/p>\n<p>class Contract(nn.Module):<br \/>\n    &#034;&#034;&#034;Contracts spatial dimensions into channel dimensions for efficient processing in neural networks.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, gain&#061;2):<br \/>\n        &#034;&#034;&#034;Initializes a layer to contract spatial dimensions (width-height) into channels, e.g., input shape<br \/>\n        (1,64,80,80) to (1,256,40,40).<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.gain &#061; gain<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input tensor to expand channel dimensions by contracting spatial dimensions, yielding output shape<br \/>\n        &#096;(b, c*s*s, h\/\/s, w\/\/s)&#096;.<br \/>\n        &#034;&#034;&#034;<br \/>\n        b, c, h, w &#061; x.size()  # assert (h \/ s &#061;&#061; 0) and (W \/ s &#061;&#061; 0), &#039;Indivisible gain&#039;<br \/>\n        s &#061; self.gain<br \/>\n        x &#061; x.view(b, c, h \/\/ s, s, w \/\/ s, s)  # x(1,64,40,2,40,2)<br \/>\n        x &#061; x.permute(0, 3, 5, 1, 2, 4).contiguous()  # x(1,2,2,64,40,40)<br \/>\n        return x.view(b, c * s * s, h \/\/ s, w \/\/ s)  # x(1,256,40,40)<\/p>\n<p>class Expand(nn.Module):<br \/>\n    &#034;&#034;&#034;Expands spatial dimensions by redistributing channels, e.g., from (1,64,80,80) to (1,16,160,160).&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, gain&#061;2):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Initializes the Expand module to increase spatial dimensions by redistributing channels, with an optional gain<br \/>\n        factor.<\/p>\n<p>        Example: x(1,64,80,80) to x(1,16,160,160).<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.gain &#061; gain<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input tensor x to expand spatial dimensions by redistributing channels, requiring C \/ gain^2 &#061;&#061;<br \/>\n        0.<br \/>\n        &#034;&#034;&#034;<br \/>\n        b, c, h, w &#061; x.size()  # assert C \/ s ** 2 &#061;&#061; 0, &#039;Indivisible gain&#039;<br \/>\n        s &#061; self.gain<br \/>\n        x &#061; x.view(b, s, s, c \/\/ s**2, h, w)  # x(1,2,2,16,80,80)<br \/>\n        x &#061; x.permute(0, 3, 4, 1, 5, 2).contiguous()  # x(1,16,80,2,80,2)<br \/>\n        return x.view(b, c \/\/ s**2, h * s, w * s)  # x(1,16,160,160)<\/p>\n<p>class Concat(nn.Module):<br \/>\n    &#034;&#034;&#034;Concatenates tensors along a specified dimension for efficient tensor manipulation in neural networks.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, dimension&#061;1):<br \/>\n        &#034;&#034;&#034;Initializes a Concat module to concatenate tensors along a specified dimension.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.d &#061; dimension<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Concatenates a list of tensors along a specified dimension; &#096;x&#096; is a list of tensors, &#096;dimension&#096; is an<br \/>\n        int.<br \/>\n        &#034;&#034;&#034;<br \/>\n        return torch.cat(x, self.d)<\/p>\n<p>class DetectMultiBackend(nn.Module):<br \/>\n    &#034;&#034;&#034;YOLOv5 MultiBackend class for inference on various backends including PyTorch, ONNX, TensorRT, and more.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, weights&#061;&#034;yolov5s.pt&#034;, device&#061;torch.device(&#034;cpu&#034;), dnn&#061;False, data&#061;None, fp16&#061;False, fuse&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes DetectMultiBackend with support for various inference backends, including PyTorch and ONNX.&#034;&#034;&#034;<br \/>\n        #   PyTorch:              weights &#061; *.pt<br \/>\n        #   TorchScript:                    *.torchscript<br \/>\n        #   ONNX Runtime:                   *.onnx<br \/>\n        #   ONNX OpenCV DNN:                *.onnx &#8211;dnn<br \/>\n        #   OpenVINO:                       *_openvino_model<br \/>\n        #   CoreML:                         *.mlpackage<br \/>\n        #   TensorRT:                       *.engine<br \/>\n        #   TensorFlow SavedModel:          *_saved_model<br \/>\n        #   TensorFlow GraphDef:            *.pb<br \/>\n        #   TensorFlow Lite:                *.tflite<br \/>\n        #   TensorFlow Edge TPU:            *_edgetpu.tflite<br \/>\n        #   PaddlePaddle:                   *_paddle_model<br \/>\n        from models.experimental import attempt_download, attempt_load  # scoped to avoid circular import<\/p>\n<p>        super().__init__()<br \/>\n        w &#061; str(weights[0] if isinstance(weights, list) else weights)<br \/>\n        pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle, triton &#061; self._model_type(w)<br \/>\n        fp16 &amp;&#061; pt or jit or onnx or engine or triton  # FP16<br \/>\n        nhwc &#061; coreml or saved_model or pb or tflite or edgetpu  # BHWC formats (vs torch BCWH)<br \/>\n        stride &#061; 32  # default stride<br \/>\n        cuda &#061; torch.cuda.is_available() and device.type !&#061; &#034;cpu&#034;  # use CUDA<br \/>\n        if not (pt or triton):<br \/>\n            w &#061; attempt_download(w)  # download if not local<\/p>\n<p>        if pt:  # PyTorch<br \/>\n            model &#061; attempt_load(weights if isinstance(weights, list) else w, device&#061;device, inplace&#061;True, fuse&#061;fuse)<br \/>\n            stride &#061; max(int(model.stride.max()), 32)  # model stride<br \/>\n            names &#061; model.module.names if hasattr(model, &#034;module&#034;) else model.names  # get class names<br \/>\n            model.half() if fp16 else model.float()<br \/>\n            self.model &#061; model  # explicitly assign for to(), cpu(), cuda(), half()<br \/>\n        elif jit:  # TorchScript<br \/>\n            LOGGER.info(f&#034;Loading {w} for TorchScript inference&#8230;&#034;)<br \/>\n            extra_files &#061; {&#034;config.txt&#034;: &#034;&#034;}  # model metadata<br \/>\n            model &#061; torch.jit.load(w, _extra_files&#061;extra_files, map_location&#061;device)<br \/>\n            model.half() if fp16 else model.float()<br \/>\n            if extra_files[&#034;config.txt&#034;]:  # load metadata dict<br \/>\n                d &#061; json.loads(<br \/>\n                    extra_files[&#034;config.txt&#034;],<br \/>\n                    object_hook&#061;lambda d: {int(k) if k.isdigit() else k: v for k, v in d.items()},<br \/>\n                )<br \/>\n                stride, names &#061; int(d[&#034;stride&#034;]), d[&#034;names&#034;]<br \/>\n        elif dnn:  # ONNX OpenCV DNN<br \/>\n            LOGGER.info(f&#034;Loading {w} for ONNX OpenCV DNN inference&#8230;&#034;)<br \/>\n            check_requirements(&#034;opencv-python&gt;&#061;4.5.4&#034;)<br \/>\n            net &#061; cv2.dnn.readNetFromONNX(w)<br \/>\n        elif onnx:  # ONNX Runtime<br \/>\n            LOGGER.info(f&#034;Loading {w} for ONNX Runtime inference&#8230;&#034;)<br \/>\n            check_requirements((&#034;onnx&#034;, &#034;onnxruntime-gpu&#034; if cuda else &#034;onnxruntime&#034;))<br \/>\n            import onnxruntime<\/p>\n<p>            providers &#061; [&#034;CUDAExecutionProvider&#034;, &#034;CPUExecutionProvider&#034;] if cuda else [&#034;CPUExecutionProvider&#034;]<br \/>\n            session &#061; onnxruntime.InferenceSession(w, providers&#061;providers)<br \/>\n            output_names &#061; [x.name for x in session.get_outputs()]<br \/>\n            meta &#061; session.get_modelmeta().custom_metadata_map  # metadata<br \/>\n            if &#034;stride&#034; in meta:<br \/>\n                stride, names &#061; int(meta[&#034;stride&#034;]), eval(meta[&#034;names&#034;])<br \/>\n        elif xml:  # OpenVINO<br \/>\n            LOGGER.info(f&#034;Loading {w} for OpenVINO inference&#8230;&#034;)<br \/>\n            check_requirements(&#034;openvino&gt;&#061;2023.0&#034;)  # requires openvino-dev: https:\/\/pypi.org\/project\/openvino-dev\/<br \/>\n            from openvino.runtime import Core, Layout, get_batch<\/p>\n<p>            core &#061; Core()<br \/>\n            if not Path(w).is_file():  # if not *.xml<br \/>\n                w &#061; next(Path(w).glob(&#034;*.xml&#034;))  # get *.xml file from *_openvino_model dir<br \/>\n            ov_model &#061; core.read_model(model&#061;w, weights&#061;Path(w).with_suffix(&#034;.bin&#034;))<br \/>\n            if ov_model.get_parameters()[0].get_layout().empty:<br \/>\n                ov_model.get_parameters()[0].set_layout(Layout(&#034;NCHW&#034;))<br \/>\n            batch_dim &#061; get_batch(ov_model)<br \/>\n            if batch_dim.is_static:<br \/>\n                batch_size &#061; batch_dim.get_length()<br \/>\n            ov_compiled_model &#061; core.compile_model(ov_model, device_name&#061;&#034;AUTO&#034;)  # AUTO selects best available device<br \/>\n            stride, names &#061; self._load_metadata(Path(w).with_suffix(&#034;.yaml&#034;))  # load metadata<br \/>\n        elif engine:  # TensorRT<br \/>\n            LOGGER.info(f&#034;Loading {w} for TensorRT inference&#8230;&#034;)<br \/>\n            import tensorrt as trt  # https:\/\/developer.nvidia.com\/nvidia-tensorrt-download<\/p>\n<p>            check_version(trt.__version__, &#034;7.0.0&#034;, hard&#061;True)  # require tensorrt&gt;&#061;7.0.0<br \/>\n            if device.type &#061;&#061; &#034;cpu&#034;:<br \/>\n                device &#061; torch.device(&#034;cuda:0&#034;)<br \/>\n            Binding &#061; namedtuple(&#034;Binding&#034;, (&#034;name&#034;, &#034;dtype&#034;, &#034;shape&#034;, &#034;data&#034;, &#034;ptr&#034;))<br \/>\n            logger &#061; trt.Logger(trt.Logger.INFO)<br \/>\n            with open(w, &#034;rb&#034;) as f, trt.Runtime(logger) as runtime:<br \/>\n                model &#061; runtime.deserialize_cuda_engine(f.read())<br \/>\n            context &#061; model.create_execution_context()<br \/>\n            bindings &#061; OrderedDict()<br \/>\n            output_names &#061; []<br \/>\n            fp16 &#061; False  # default updated below<br \/>\n            dynamic &#061; False<br \/>\n            is_trt10 &#061; not hasattr(model, &#034;num_bindings&#034;)<br \/>\n            num &#061; range(model.num_io_tensors) if is_trt10 else range(model.num_bindings)<br \/>\n            for i in num:<br \/>\n                if is_trt10:<br \/>\n                    name &#061; model.get_tensor_name(i)<br \/>\n                    dtype &#061; trt.nptype(model.get_tensor_dtype(name))<br \/>\n                    is_input &#061; model.get_tensor_mode(name) &#061;&#061; trt.TensorIOMode.INPUT<br \/>\n                    if is_input:<br \/>\n                        if -1 in tuple(model.get_tensor_shape(name)):  # dynamic<br \/>\n                            dynamic &#061; True<br \/>\n                            context.set_input_shape(name, tuple(model.get_profile_shape(name, 0)[2]))<br \/>\n                        if dtype &#061;&#061; np.float16:<br \/>\n                            fp16 &#061; True<br \/>\n                    else:  # output<br \/>\n                        output_names.append(name)<br \/>\n                    shape &#061; tuple(context.get_tensor_shape(name))<br \/>\n                else:<br \/>\n                    name &#061; model.get_binding_name(i)<br \/>\n                    dtype &#061; trt.nptype(model.get_binding_dtype(i))<br \/>\n                    if model.binding_is_input(i):<br \/>\n                        if -1 in tuple(model.get_binding_shape(i)):  # dynamic<br \/>\n                            dynamic &#061; True<br \/>\n                            context.set_binding_shape(i, tuple(model.get_profile_shape(0, i)[2]))<br \/>\n                        if dtype &#061;&#061; np.float16:<br \/>\n                            fp16 &#061; True<br \/>\n                    else:  # output<br \/>\n                        output_names.append(name)<br \/>\n                    shape &#061; tuple(context.get_binding_shape(i))<br \/>\n                im &#061; torch.from_numpy(np.empty(shape, dtype&#061;dtype)).to(device)<br \/>\n                bindings[name] &#061; Binding(name, dtype, shape, im, int(im.data_ptr()))<br \/>\n            binding_addrs &#061; OrderedDict((n, d.ptr) for n, d in bindings.items())<br \/>\n            batch_size &#061; bindings[&#034;images&#034;].shape[0]  # if dynamic, this is instead max batch size<br \/>\n        elif coreml:  # CoreML<br \/>\n            LOGGER.info(f&#034;Loading {w} for CoreML inference&#8230;&#034;)<br \/>\n            import coremltools as ct<\/p>\n<p>            model &#061; ct.models.MLModel(w)<br \/>\n        elif saved_model:  # TF SavedModel<br \/>\n            LOGGER.info(f&#034;Loading {w} for TensorFlow SavedModel inference&#8230;&#034;)<br \/>\n            import tensorflow as tf<\/p>\n<p>            keras &#061; False  # assume TF1 saved_model<br \/>\n            model &#061; tf.keras.models.load_model(w) if keras else tf.saved_model.load(w)<br \/>\n        elif pb:  # GraphDef https:\/\/www.tensorflow.org\/guide\/migrate#a_graphpb_or_graphpbtxt<br \/>\n            LOGGER.info(f&#034;Loading {w} for TensorFlow GraphDef inference&#8230;&#034;)<br \/>\n            import tensorflow as tf<\/p>\n<p>            def wrap_frozen_graph(gd, inputs, outputs):<br \/>\n                &#034;&#034;&#034;Wraps a TensorFlow GraphDef for inference, returning a pruned function.&#034;&#034;&#034;<br \/>\n                x &#061; tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name&#061;&#034;&#034;), [])  # wrapped<br \/>\n                ge &#061; x.graph.as_graph_element<br \/>\n                return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))<\/p>\n<p>            def gd_outputs(gd):<br \/>\n                &#034;&#034;&#034;Generates a sorted list of graph outputs excluding NoOp nodes and inputs, formatted as &#039;&lt;name&gt;:0&#039;.&#034;&#034;&#034;<br \/>\n                name_list, input_list &#061; [], []<br \/>\n                for node in gd.node:  # tensorflow.core.framework.node_def_pb2.NodeDef<br \/>\n                    name_list.append(node.name)<br \/>\n                    input_list.extend(node.input)<br \/>\n                return sorted(f&#034;{x}:0&#034; for x in list(set(name_list) &#8211; set(input_list)) if not x.startswith(&#034;NoOp&#034;))<\/p>\n<p>            gd &#061; tf.Graph().as_graph_def()  # TF GraphDef<br \/>\n            with open(w, &#034;rb&#034;) as f:<br \/>\n                gd.ParseFromString(f.read())<br \/>\n            frozen_func &#061; wrap_frozen_graph(gd, inputs&#061;&#034;x:0&#034;, outputs&#061;gd_outputs(gd))<br \/>\n        elif tflite or edgetpu:  # https:\/\/www.tensorflow.org\/lite\/guide\/python#install_tensorflow_lite_for_python<br \/>\n            try:  # https:\/\/coral.ai\/docs\/edgetpu\/tflite-python\/#update-existing-tf-lite-code-for-the-edge-tpu<br \/>\n                from tflite_runtime.interpreter import Interpreter, load_delegate<br \/>\n            except ImportError:<br \/>\n                import tensorflow as tf<\/p>\n<p>                Interpreter, load_delegate &#061; (<br \/>\n                    tf.lite.Interpreter,<br \/>\n                    tf.lite.experimental.load_delegate,<br \/>\n                )<br \/>\n            if edgetpu:  # TF Edge TPU https:\/\/coral.ai\/software\/#edgetpu-runtime<br \/>\n                LOGGER.info(f&#034;Loading {w} for TensorFlow Lite Edge TPU inference&#8230;&#034;)<br \/>\n                delegate &#061; {&#034;Linux&#034;: &#034;libedgetpu.so.1&#034;, &#034;Darwin&#034;: &#034;libedgetpu.1.dylib&#034;, &#034;Windows&#034;: &#034;edgetpu.dll&#034;}[<br \/>\n                    platform.system()<br \/>\n                ]<br \/>\n                interpreter &#061; Interpreter(model_path&#061;w, experimental_delegates&#061;[load_delegate(delegate)])<br \/>\n            else:  # TFLite<br \/>\n                LOGGER.info(f&#034;Loading {w} for TensorFlow Lite inference&#8230;&#034;)<br \/>\n                interpreter &#061; Interpreter(model_path&#061;w)  # load TFLite model<br \/>\n            interpreter.allocate_tensors()  # allocate<br \/>\n            input_details &#061; interpreter.get_input_details()  # inputs<br \/>\n            output_details &#061; interpreter.get_output_details()  # outputs<br \/>\n            # load metadata<br \/>\n            with contextlib.suppress(zipfile.BadZipFile):<br \/>\n                with zipfile.ZipFile(w, &#034;r&#034;) as model:<br \/>\n                    meta_file &#061; model.namelist()[0]<br \/>\n                    meta &#061; ast.literal_eval(model.read(meta_file).decode(&#034;utf-8&#034;))<br \/>\n                    stride, names &#061; int(meta[&#034;stride&#034;]), meta[&#034;names&#034;]<br \/>\n        elif tfjs:  # TF.js<br \/>\n            raise NotImplementedError(&#034;ERROR: YOLOv5 TF.js inference is not supported&#034;)<br \/>\n        elif paddle:  # PaddlePaddle<br \/>\n            LOGGER.info(f&#034;Loading {w} for PaddlePaddle inference&#8230;&#034;)<br \/>\n            check_requirements(&#034;paddlepaddle-gpu&#034; if cuda else &#034;paddlepaddle&#034;)<br \/>\n            import paddle.inference as pdi<\/p>\n<p>            if not Path(w).is_file():  # if not *.pdmodel<br \/>\n                w &#061; next(Path(w).rglob(&#034;*.pdmodel&#034;))  # get *.pdmodel file from *_paddle_model dir<br \/>\n            weights &#061; Path(w).with_suffix(&#034;.pdiparams&#034;)<br \/>\n            config &#061; pdi.Config(str(w), str(weights))<br \/>\n            if cuda:<br \/>\n                config.enable_use_gpu(memory_pool_init_size_mb&#061;2048, device_id&#061;0)<br \/>\n            predictor &#061; pdi.create_predictor(config)<br \/>\n            input_handle &#061; predictor.get_input_handle(predictor.get_input_names()[0])<br \/>\n            output_names &#061; predictor.get_output_names()<br \/>\n        elif triton:  # NVIDIA Triton Inference Server<br \/>\n            LOGGER.info(f&#034;Using {w} as Triton Inference Server&#8230;&#034;)<br \/>\n            check_requirements(&#034;tritonclient[all]&#034;)<br \/>\n            from utils.triton import TritonRemoteModel<\/p>\n<p>            model &#061; TritonRemoteModel(url&#061;w)<br \/>\n            nhwc &#061; model.runtime.startswith(&#034;tensorflow&#034;)<br \/>\n        else:<br \/>\n            raise NotImplementedError(f&#034;ERROR: {w} is not a supported format&#034;)<\/p>\n<p>        # class names<br \/>\n        if &#034;names&#034; not in locals():<br \/>\n            names &#061; yaml_load(data)[&#034;names&#034;] if data else {i: f&#034;class{i}&#034; for i in range(999)}<br \/>\n        if names[0] &#061;&#061; &#034;n01440764&#034; and len(names) &#061;&#061; 1000:  # ImageNet<br \/>\n            names &#061; yaml_load(ROOT \/ &#034;data\/ImageNet.yaml&#034;)[&#034;names&#034;]  # human-readable names<\/p>\n<p>        self.__dict__.update(locals())  # assign all variables to self<\/p>\n<p>    def forward(self, im, augment&#061;False, visualize&#061;False):<br \/>\n        &#034;&#034;&#034;Performs YOLOv5 inference on input images with options for augmentation and visualization.&#034;&#034;&#034;<br \/>\n        b, ch, h, w &#061; im.shape  # batch, channel, height, width<br \/>\n        if self.fp16 and im.dtype !&#061; torch.float16:<br \/>\n            im &#061; im.half()  # to FP16<br \/>\n        if self.nhwc:<br \/>\n            im &#061; im.permute(0, 2, 3, 1)  # torch BCHW to numpy BHWC shape(1,320,192,3)<\/p>\n<p>        if self.pt:  # PyTorch<br \/>\n            y &#061; self.model(im, augment&#061;augment, visualize&#061;visualize) if augment or visualize else self.model(im)<br \/>\n        elif self.jit:  # TorchScript<br \/>\n            y &#061; self.model(im)<br \/>\n        elif self.dnn:  # ONNX OpenCV DNN<br \/>\n            im &#061; im.cpu().numpy()  # torch to numpy<br \/>\n            self.net.setInput(im)<br \/>\n            y &#061; self.net.forward()<br \/>\n        elif self.onnx:  # ONNX Runtime<br \/>\n            im &#061; im.cpu().numpy()  # torch to numpy<br \/>\n            y &#061; self.session.run(self.output_names, {self.session.get_inputs()[0].name: im})<br \/>\n        elif self.xml:  # OpenVINO<br \/>\n            im &#061; im.cpu().numpy()  # FP32<br \/>\n            y &#061; list(self.ov_compiled_model(im).values())<br \/>\n        elif self.engine:  # TensorRT<br \/>\n            if self.dynamic and im.shape !&#061; self.bindings[&#034;images&#034;].shape:<br \/>\n                i &#061; self.model.get_binding_index(&#034;images&#034;)<br \/>\n                self.context.set_binding_shape(i, im.shape)  # reshape if dynamic<br \/>\n                self.bindings[&#034;images&#034;] &#061; self.bindings[&#034;images&#034;]._replace(shape&#061;im.shape)<br \/>\n                for name in self.output_names:<br \/>\n                    i &#061; self.model.get_binding_index(name)<br \/>\n                    self.bindings[name].data.resize_(tuple(self.context.get_binding_shape(i)))<br \/>\n            s &#061; self.bindings[&#034;images&#034;].shape<br \/>\n            assert im.shape &#061;&#061; s, f&#034;input size {im.shape} {&#039;&gt;&#039; if self.dynamic else &#039;not equal to&#039;} max model size {s}&#034;<br \/>\n            self.binding_addrs[&#034;images&#034;] &#061; int(im.data_ptr())<br \/>\n            self.context.execute_v2(list(self.binding_addrs.values()))<br \/>\n            y &#061; [self.bindings[x].data for x in sorted(self.output_names)]<br \/>\n        elif self.coreml:  # CoreML<br \/>\n            im &#061; im.cpu().numpy()<br \/>\n            im &#061; Image.fromarray((im[0] * 255).astype(&#034;uint8&#034;))<br \/>\n            # im &#061; im.resize((192, 320), Image.BILINEAR)<br \/>\n            y &#061; self.model.predict({&#034;image&#034;: im})  # coordinates are xywh normalized<br \/>\n            if &#034;confidence&#034; in y:<br \/>\n                box &#061; xywh2xyxy(y[&#034;coordinates&#034;] * [[w, h, w, h]])  # xyxy pixels<br \/>\n                conf, cls &#061; y[&#034;confidence&#034;].max(1), y[&#034;confidence&#034;].argmax(1).astype(np.float)<br \/>\n                y &#061; np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1)<br \/>\n            else:<br \/>\n                y &#061; list(reversed(y.values()))  # reversed for segmentation models (pred, proto)<br \/>\n        elif self.paddle:  # PaddlePaddle<br \/>\n            im &#061; im.cpu().numpy().astype(np.float32)<br \/>\n            self.input_handle.copy_from_cpu(im)<br \/>\n            self.predictor.run()<br \/>\n            y &#061; [self.predictor.get_output_handle(x).copy_to_cpu() for x in self.output_names]<br \/>\n        elif self.triton:  # NVIDIA Triton Inference Server<br \/>\n            y &#061; self.model(im)<br \/>\n        else:  # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)<br \/>\n            im &#061; im.cpu().numpy()<br \/>\n            if self.saved_model:  # SavedModel<br \/>\n                y &#061; self.model(im, training&#061;False) if self.keras else self.model(im)<br \/>\n            elif self.pb:  # GraphDef<br \/>\n                y &#061; self.frozen_func(x&#061;self.tf.constant(im))<br \/>\n            else:  # Lite or Edge TPU<br \/>\n                input &#061; self.input_details[0]<br \/>\n                int8 &#061; input[&#034;dtype&#034;] &#061;&#061; np.uint8  # is TFLite quantized uint8 model<br \/>\n                if int8:<br \/>\n                    scale, zero_point &#061; input[&#034;quantization&#034;]<br \/>\n                    im &#061; (im \/ scale &#043; zero_point).astype(np.uint8)  # de-scale<br \/>\n                self.interpreter.set_tensor(input[&#034;index&#034;], im)<br \/>\n                self.interpreter.invoke()<br \/>\n                y &#061; []<br \/>\n                for output in self.output_details:<br \/>\n                    x &#061; self.interpreter.get_tensor(output[&#034;index&#034;])<br \/>\n                    if int8:<br \/>\n                        scale, zero_point &#061; output[&#034;quantization&#034;]<br \/>\n                        x &#061; (x.astype(np.float32) &#8211; zero_point) * scale  # re-scale<br \/>\n                    y.append(x)<br \/>\n            if len(y) &#061;&#061; 2 and len(y[1].shape) !&#061; 4:<br \/>\n                y &#061; list(reversed(y))<br \/>\n            y &#061; [x if isinstance(x, np.ndarray) else x.numpy() for x in y]<br \/>\n            y[0][&#8230;, :4] *&#061; [w, h, w, h]  # xywh normalized to pixels<\/p>\n<p>        if isinstance(y, (list, tuple)):<br \/>\n            return self.from_numpy(y[0]) if len(y) &#061;&#061; 1 else [self.from_numpy(x) for x in y]<br \/>\n        else:<br \/>\n            return self.from_numpy(y)<\/p>\n<p>    def from_numpy(self, x):<br \/>\n        &#034;&#034;&#034;Converts a NumPy array to a torch tensor, maintaining device compatibility.&#034;&#034;&#034;<br \/>\n        return torch.from_numpy(x).to(self.device) if isinstance(x, np.ndarray) else x<\/p>\n<p>    def warmup(self, imgsz&#061;(1, 3, 640, 640)):<br \/>\n        &#034;&#034;&#034;Performs a single inference warmup to initialize model weights, accepting an &#096;imgsz&#096; tuple for image size.&#034;&#034;&#034;<br \/>\n        warmup_types &#061; self.pt, self.jit, self.onnx, self.engine, self.saved_model, self.pb, self.triton<br \/>\n        if any(warmup_types) and (self.device.type !&#061; &#034;cpu&#034; or self.triton):<br \/>\n            im &#061; torch.empty(*imgsz, dtype&#061;torch.half if self.fp16 else torch.float, device&#061;self.device)  # input<br \/>\n            for _ in range(2 if self.jit else 1):  #<br \/>\n                self.forward(im)  # warmup<\/p>\n<p>    &#064;staticmethod<br \/>\n    def _model_type(p&#061;&#034;path\/to\/model.pt&#034;):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Determines model type from file path or URL, supporting various export formats.<\/p>\n<p>        Example: path&#061;&#039;path\/to\/model.onnx&#039; -&gt; type&#061;onnx<br \/>\n        &#034;&#034;&#034;<br \/>\n        # types &#061; [pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle]<br \/>\n        from export import export_formats<br \/>\n        from utils.downloads import is_url<\/p>\n<p>        sf &#061; list(export_formats().Suffix)  # export suffixes<br \/>\n        if not is_url(p, check&#061;False):<br \/>\n            check_suffix(p, sf)  # checks<br \/>\n        url &#061; urlparse(p)  # if url may be Triton inference server<br \/>\n        types &#061; [s in Path(p).name for s in sf]<br \/>\n        types[8] &amp;&#061; not types[9]  # tflite &amp;&#061; not edgetpu<br \/>\n        triton &#061; not any(types) and all([any(s in url.scheme for s in [&#034;http&#034;, &#034;grpc&#034;]), url.netloc])<br \/>\n        return types &#043; [triton]<\/p>\n<p>    &#064;staticmethod<br \/>\n    def _load_metadata(f&#061;Path(&#034;path\/to\/meta.yaml&#034;)):<br \/>\n        &#034;&#034;&#034;Loads metadata from a YAML file, returning strides and names if the file exists, otherwise &#096;None&#096;.&#034;&#034;&#034;<br \/>\n        if f.exists():<br \/>\n            d &#061; yaml_load(f)<br \/>\n            return d[&#034;stride&#034;], d[&#034;names&#034;]  # assign stride, names<br \/>\n        return None, None<\/p>\n<p>class AutoShape(nn.Module):<br \/>\n    &#034;&#034;&#034;AutoShape class for robust YOLOv5 inference with preprocessing, NMS, and support for various input formats.&#034;&#034;&#034;<\/p>\n<p>    conf &#061; 0.25  # NMS confidence threshold<br \/>\n    iou &#061; 0.45  # NMS IoU threshold<br \/>\n    agnostic &#061; False  # NMS class-agnostic<br \/>\n    multi_label &#061; False  # NMS multiple labels per box<br \/>\n    classes &#061; None  # (optional list) filter by class, i.e. &#061; [0, 15, 16] for COCO persons, cats and dogs<br \/>\n    max_det &#061; 1000  # maximum number of detections per image<br \/>\n    amp &#061; False  # Automatic Mixed Precision (AMP) inference<\/p>\n<p>    def __init__(self, model, verbose&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5 model for inference, setting up attributes and preparing model for evaluation.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        if verbose:<br \/>\n            LOGGER.info(&#034;Adding AutoShape&#8230; &#034;)<br \/>\n        copy_attr(self, model, include&#061;(&#034;yaml&#034;, &#034;nc&#034;, &#034;hyp&#034;, &#034;names&#034;, &#034;stride&#034;, &#034;abc&#034;), exclude&#061;())  # copy attributes<br \/>\n        self.dmb &#061; isinstance(model, DetectMultiBackend)  # DetectMultiBackend() instance<br \/>\n        self.pt &#061; not self.dmb or model.pt  # PyTorch model<br \/>\n        self.model &#061; model.eval()<br \/>\n        if self.pt:<br \/>\n            m &#061; self.model.model.model[-1] if self.dmb else self.model.model[-1]  # Detect()<br \/>\n            m.inplace &#061; False  # Detect.inplace&#061;False for safe multithread inference<br \/>\n            m.export &#061; True  # do not output loss values<\/p>\n<p>    def _apply(self, fn):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Applies to(), cpu(), cuda(), half() etc.<\/p>\n<p>        to model tensors excluding parameters or registered buffers.<br \/>\n        &#034;&#034;&#034;<br \/>\n        self &#061; super()._apply(fn)<br \/>\n        if self.pt:<br \/>\n            m &#061; self.model.model.model[-1] if self.dmb else self.model.model[-1]  # Detect()<br \/>\n            m.stride &#061; fn(m.stride)<br \/>\n            m.grid &#061; list(map(fn, m.grid))<br \/>\n            if isinstance(m.anchor_grid, list):<br \/>\n                m.anchor_grid &#061; list(map(fn, m.anchor_grid))<br \/>\n        return self<\/p>\n<p>    &#064;smart_inference_mode()<br \/>\n    def forward(self, ims, size&#061;640, augment&#061;False, profile&#061;False):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Performs inference on inputs with optional augment &amp; profiling.<\/p>\n<p>        Supports various formats including file, URI, OpenCV, PIL, numpy, torch.<br \/>\n        &#034;&#034;&#034;<br \/>\n        # For size(height&#061;640, width&#061;1280), RGB images example inputs are:<br \/>\n        #   file:        ims &#061; &#039;data\/images\/zidane.jpg&#039;  # str or PosixPath<br \/>\n        #   URI:             &#061; &#039;https:\/\/ultralytics.com\/images\/zidane.jpg&#039;<br \/>\n        #   OpenCV:          &#061; cv2.imread(&#039;image.jpg&#039;)[:,:,::-1]  # HWC BGR to RGB x(640,1280,3)<br \/>\n        #   PIL:             &#061; Image.open(&#039;image.jpg&#039;) or ImageGrab.grab()  # HWC x(640,1280,3)<br \/>\n        #   numpy:           &#061; np.zeros((640,1280,3))  # HWC<br \/>\n        #   torch:           &#061; torch.zeros(16,3,320,640)  # BCHW (scaled to size&#061;640, 0-1 values)<br \/>\n        #   multiple:        &#061; [Image.open(&#039;image1.jpg&#039;), Image.open(&#039;image2.jpg&#039;), &#8230;]  # list of images<\/p>\n<p>        dt &#061; (Profile(), Profile(), Profile())<br \/>\n        with dt[0]:<br \/>\n            if isinstance(size, int):  # expand<br \/>\n                size &#061; (size, size)<br \/>\n            p &#061; next(self.model.parameters()) if self.pt else torch.empty(1, device&#061;self.model.device)  # param<br \/>\n            autocast &#061; self.amp and (p.device.type !&#061; &#034;cpu&#034;)  # Automatic Mixed Precision (AMP) inference<br \/>\n            if isinstance(ims, torch.Tensor):  # torch<br \/>\n                with amp.autocast(autocast):<br \/>\n                    return self.model(ims.to(p.device).type_as(p), augment&#061;augment)  # inference<\/p>\n<p>            # Pre-process<br \/>\n            n, ims &#061; (len(ims), list(ims)) if isinstance(ims, (list, tuple)) else (1, [ims])  # number, list of images<br \/>\n            shape0, shape1, files &#061; [], [], []  # image and inference shapes, filenames<br \/>\n            for i, im in enumerate(ims):<br \/>\n                f &#061; f&#034;image{i}&#034;  # filename<br \/>\n                if isinstance(im, (str, Path)):  # filename or uri<br \/>\n                    im, f &#061; Image.open(requests.get(im, stream&#061;True).raw if str(im).startswith(&#034;http&#034;) else im), im<br \/>\n                    im &#061; np.asarray(exif_transpose(im))<br \/>\n                elif isinstance(im, Image.Image):  # PIL Image<br \/>\n                    im, f &#061; np.asarray(exif_transpose(im)), getattr(im, &#034;filename&#034;, f) or f<br \/>\n                files.append(Path(f).with_suffix(&#034;.jpg&#034;).name)<br \/>\n                if im.shape[0] &lt; 5:  # image in CHW<br \/>\n                    im &#061; im.transpose((1, 2, 0))  # reverse dataloader .transpose(2, 0, 1)<br \/>\n                im &#061; im[&#8230;, :3] if im.ndim &#061;&#061; 3 else cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)  # enforce 3ch input<br \/>\n                s &#061; im.shape[:2]  # HWC<br \/>\n                shape0.append(s)  # image shape<br \/>\n                g &#061; max(size) \/ max(s)  # gain<br \/>\n                shape1.append([int(y * g) for y in s])<br \/>\n                ims[i] &#061; im if im.data.contiguous else np.ascontiguousarray(im)  # update<br \/>\n            shape1 &#061; [make_divisible(x, self.stride) for x in np.array(shape1).max(0)]  # inf shape<br \/>\n            x &#061; [letterbox(im, shape1, auto&#061;False)[0] for im in ims]  # pad<br \/>\n            x &#061; np.ascontiguousarray(np.array(x).transpose((0, 3, 1, 2)))  # stack and BHWC to BCHW<br \/>\n            x &#061; torch.from_numpy(x).to(p.device).type_as(p) \/ 255  # uint8 to fp16\/32<\/p>\n<p>        with amp.autocast(autocast):<br \/>\n            # Inference<br \/>\n            with dt[1]:<br \/>\n                y &#061; self.model(x, augment&#061;augment)  # forward<\/p>\n<p>            # Post-process<br \/>\n            with dt[2]:<br \/>\n                y &#061; non_max_suppression(<br \/>\n                    y if self.dmb else y[0],<br \/>\n                    self.conf,<br \/>\n                    self.iou,<br \/>\n                    self.classes,<br \/>\n                    self.agnostic,<br \/>\n                    self.multi_label,<br \/>\n                    max_det&#061;self.max_det,<br \/>\n                )  # NMS<br \/>\n                for i in range(n):<br \/>\n                    scale_boxes(shape1, y[i][:, :4], shape0[i])<\/p>\n<p>            return Detections(ims, y, files, dt, self.names, x.shape)<\/p>\n<p>class Detections:<br \/>\n    &#034;&#034;&#034;Manages YOLOv5 detection results with methods for visualization, saving, cropping, and exporting detections.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, ims, pred, files, times&#061;(0, 0, 0), names&#061;None, shape&#061;None):<br \/>\n        &#034;&#034;&#034;Initializes the YOLOv5 Detections class with image info, predictions, filenames, timing and normalization.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        d &#061; pred[0].device  # device<br \/>\n        gn &#061; [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device&#061;d) for im in ims]  # normalizations<br \/>\n        self.ims &#061; ims  # list of images as numpy arrays<br \/>\n        self.pred &#061; pred  # list of tensors pred[0] &#061; (xyxy, conf, cls)<br \/>\n        self.names &#061; names  # class names<br \/>\n        self.files &#061; files  # image filenames<br \/>\n        self.times &#061; times  # profiling times<br \/>\n        self.xyxy &#061; pred  # xyxy pixels<br \/>\n        self.xywh &#061; [xyxy2xywh(x) for x in pred]  # xywh pixels<br \/>\n        self.xyxyn &#061; [x \/ g for x, g in zip(self.xyxy, gn)]  # xyxy normalized<br \/>\n        self.xywhn &#061; [x \/ g for x, g in zip(self.xywh, gn)]  # xywh normalized<br \/>\n        self.n &#061; len(self.pred)  # number of images (batch size)<br \/>\n        self.t &#061; tuple(x.t \/ self.n * 1e3 for x in times)  # timestamps (ms)<br \/>\n        self.s &#061; tuple(shape)  # inference BCHW shape<\/p>\n<p>    def _run(self, pprint&#061;False, show&#061;False, save&#061;False, crop&#061;False, render&#061;False, labels&#061;True, save_dir&#061;Path(&#034;&#034;)):<br \/>\n        &#034;&#034;&#034;Executes model predictions, displaying and\/or saving outputs with optional crops and labels.&#034;&#034;&#034;<br \/>\n        s, crops &#061; &#034;&#034;, []<br \/>\n        for i, (im, pred) in enumerate(zip(self.ims, self.pred)):<br \/>\n            s &#043;&#061; f&#034;\\\\nimage {i &#043; 1}\/{len(self.pred)}: {im.shape[0]}x{im.shape[1]} &#034;  # string<br \/>\n            if pred.shape[0]:<br \/>\n                for c in pred[:, -1].unique():<br \/>\n                    n &#061; (pred[:, -1] &#061;&#061; c).sum()  # detections per class<br \/>\n                    s &#043;&#061; f&#034;{n} {self.names[int(c)]}{&#039;s&#039; * (n &gt; 1)}, &#034;  # add to string<br \/>\n                s &#061; s.rstrip(&#034;, &#034;)<br \/>\n                if show or save or render or crop:<br \/>\n                    annotator &#061; Annotator(im, example&#061;str(self.names))<br \/>\n                    for *box, conf, cls in reversed(pred):  # xyxy, confidence, class<br \/>\n                        label &#061; f&#034;{self.names[int(cls)]} {conf:.2f}&#034;<br \/>\n                        if crop:<br \/>\n                            file &#061; save_dir \/ &#034;crops&#034; \/ self.names[int(cls)] \/ self.files[i] if save else None<br \/>\n                            crops.append(<br \/>\n                                {<br \/>\n                                    &#034;box&#034;: box,<br \/>\n                                    &#034;conf&#034;: conf,<br \/>\n                                    &#034;cls&#034;: cls,<br \/>\n                                    &#034;label&#034;: label,<br \/>\n                                    &#034;im&#034;: save_one_box(box, im, file&#061;file, save&#061;save),<br \/>\n                                }<br \/>\n                            )<br \/>\n                        else:  # all others<br \/>\n                            annotator.box_label(box, label if labels else &#034;&#034;, color&#061;colors(cls))<br \/>\n                    im &#061; annotator.im<br \/>\n            else:<br \/>\n                s &#043;&#061; &#034;(no detections)&#034;<\/p>\n<p>            im &#061; Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im  # from np<br \/>\n            if show:<br \/>\n                if is_jupyter():<br \/>\n                    from IPython.display import display<\/p>\n<p>                    display(im)<br \/>\n                else:<br \/>\n                    im.show(self.files[i])<br \/>\n            if save:<br \/>\n                f &#061; self.files[i]<br \/>\n                im.save(save_dir \/ f)  # save<br \/>\n                if i &#061;&#061; self.n &#8211; 1:<br \/>\n                    LOGGER.info(f&#034;Saved {self.n} image{&#039;s&#039; * (self.n &gt; 1)} to {colorstr(&#039;bold&#039;, save_dir)}&#034;)<br \/>\n            if render:<br \/>\n                self.ims[i] &#061; np.asarray(im)<br \/>\n        if pprint:<br \/>\n            s &#061; s.lstrip(&#034;\\\\n&#034;)<br \/>\n            return f&#034;{s}\\\\nSpeed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {self.s}&#034; % self.t<br \/>\n        if crop:<br \/>\n            if save:<br \/>\n                LOGGER.info(f&#034;Saved results to {save_dir}\\\\n&#034;)<br \/>\n            return crops<\/p>\n<p>    &#064;TryExcept(&#034;Showing images is not supported in this environment&#034;)<br \/>\n    def show(self, labels&#061;True):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Displays detection results with optional labels.<\/p>\n<p>        Usage: show(labels&#061;True)<br \/>\n        &#034;&#034;&#034;<br \/>\n        self._run(show&#061;True, labels&#061;labels)  # show results<\/p>\n<p>    def save(self, labels&#061;True, save_dir&#061;&#034;runs\/detect\/exp&#034;, exist_ok&#061;False):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Saves detection results with optional labels to a specified directory.<\/p>\n<p>        Usage: save(labels&#061;True, save_dir&#061;&#039;runs\/detect\/exp&#039;, exist_ok&#061;False)<br \/>\n        &#034;&#034;&#034;<br \/>\n        save_dir &#061; increment_path(save_dir, exist_ok, mkdir&#061;True)  # increment save_dir<br \/>\n        self._run(save&#061;True, labels&#061;labels, save_dir&#061;save_dir)  # save results<\/p>\n<p>    def crop(self, save&#061;True, save_dir&#061;&#034;runs\/detect\/exp&#034;, exist_ok&#061;False):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Crops detection results, optionally saves them to a directory.<\/p>\n<p>        Args: save (bool), save_dir (str), exist_ok (bool).<br \/>\n        &#034;&#034;&#034;<br \/>\n        save_dir &#061; increment_path(save_dir, exist_ok, mkdir&#061;True) if save else None<br \/>\n        return self._run(crop&#061;True, save&#061;save, save_dir&#061;save_dir)  # crop results<\/p>\n<p>    def render(self, labels&#061;True):<br \/>\n        &#034;&#034;&#034;Renders detection results with optional labels on images; args: labels (bool) indicating label inclusion.&#034;&#034;&#034;<br \/>\n        self._run(render&#061;True, labels&#061;labels)  # render results<br \/>\n        return self.ims<\/p>\n<p>    def pandas(self):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Returns detections as pandas DataFrames for various box formats (xyxy, xyxyn, xywh, xywhn).<\/p>\n<p>        Example: print(results.pandas().xyxy[0]).<br \/>\n        &#034;&#034;&#034;<br \/>\n        new &#061; copy(self)  # return copy<br \/>\n        ca &#061; &#034;xmin&#034;, &#034;ymin&#034;, &#034;xmax&#034;, &#034;ymax&#034;, &#034;confidence&#034;, &#034;class&#034;, &#034;name&#034;  # xyxy columns<br \/>\n        cb &#061; &#034;xcenter&#034;, &#034;ycenter&#034;, &#034;width&#034;, &#034;height&#034;, &#034;confidence&#034;, &#034;class&#034;, &#034;name&#034;  # xywh columns<br \/>\n        for k, c in zip([&#034;xyxy&#034;, &#034;xyxyn&#034;, &#034;xywh&#034;, &#034;xywhn&#034;], [ca, ca, cb, cb]):<br \/>\n            a &#061; [[x[:5] &#043; [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)]  # update<br \/>\n            setattr(new, k, [pd.DataFrame(x, columns&#061;c) for x in a])<br \/>\n        return new<\/p>\n<p>    def tolist(self):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Converts a Detections object into a list of individual detection results for iteration.<\/p>\n<p>        Example: for result in results.tolist():<br \/>\n        &#034;&#034;&#034;<br \/>\n        r &#061; range(self.n)  # iterable<br \/>\n        return [<br \/>\n            Detections(<br \/>\n                [self.ims[i]],<br \/>\n                [self.pred[i]],<br \/>\n                [self.files[i]],<br \/>\n                self.times,<br \/>\n                self.names,<br \/>\n                self.s,<br \/>\n            )<br \/>\n            for i in r<br \/>\n        ]<\/p>\n<p>    def print(self):<br \/>\n        &#034;&#034;&#034;Logs the string representation of the current object&#039;s state via the LOGGER.&#034;&#034;&#034;<br \/>\n        LOGGER.info(self.__str__())<\/p>\n<p>    def __len__(self):<br \/>\n        &#034;&#034;&#034;Returns the number of results stored, overrides the default len(results).&#034;&#034;&#034;<br \/>\n        return self.n<\/p>\n<p>    def __str__(self):<br \/>\n        &#034;&#034;&#034;Returns a string representation of the model&#039;s results, suitable for printing, overrides default<br \/>\n        print(results).<br \/>\n        &#034;&#034;&#034;<br \/>\n        return self._run(pprint&#061;True)  # print results<\/p>\n<p>    def __repr__(self):<br \/>\n        &#034;&#034;&#034;Returns a string representation of the YOLOv5 object, including its class and formatted results.&#034;&#034;&#034;<br \/>\n        return f&#034;YOLOv5 {self.__class__} instance\\\\n&#034; &#043; self.__str__()<\/p>\n<p>class Proto(nn.Module):<br \/>\n    &#034;&#034;&#034;YOLOv5 mask Proto module for segmentation models, performing convolutions and upsampling on input tensors.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c_&#061;256, c2&#061;32):<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5 Proto module for segmentation with input, proto, and mask channels configuration.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.cv1 &#061; Conv(c1, c_, k&#061;3)<br \/>\n        self.upsample &#061; nn.Upsample(scale_factor&#061;2, mode&#061;&#034;nearest&#034;)<br \/>\n        self.cv2 &#061; Conv(c_, c_, k&#061;3)<br \/>\n        self.cv3 &#061; Conv(c_, c2)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Performs a forward pass using convolutional layers and upsampling on input tensor &#096;x&#096;.&#034;&#034;&#034;<br \/>\n        return self.cv3(self.cv2(self.upsample(self.cv1(x))))<\/p>\n<p>class Classify(nn.Module):<br \/>\n    &#034;&#034;&#034;YOLOv5 classification head with convolution, pooling, and dropout layers for channel transformation.&#034;&#034;&#034;<\/p>\n<p>    def __init__(<br \/>\n        self, c1, c2, k&#061;1, s&#061;1, p&#061;None, g&#061;1, dropout_p&#061;0.0<br \/>\n    ):  # ch_in, ch_out, kernel, stride, padding, groups, dropout probability<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5 classification head with convolution, pooling, and dropout layers for input to output<br \/>\n        channel transformation.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        c_ &#061; 1280  # efficientnet_b0 size<br \/>\n        self.conv &#061; Conv(c1, c_, k, s, autopad(k, p), g)<br \/>\n        self.pool &#061; nn.AdaptiveAvgPool2d(1)  # to x(b,c_,1,1)<br \/>\n        self.drop &#061; nn.Dropout(p&#061;dropout_p, inplace&#061;True)<br \/>\n        self.linear &#061; nn.Linear(c_, c2)  # to x(b,c2)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through conv, pool, drop, and linear layers; supports list concatenation input.&#034;&#034;&#034;<br \/>\n        if isinstance(x, list):<br \/>\n            x &#061; torch.cat(x, 1)<br \/>\n        return self.linear(self.drop(self.pool(self.conv(x)).flatten(1)))<\/p>\n<p>#<br \/>\nclass RepConv(nn.Module):<br \/>\n    &#034;&#034;&#034;<br \/>\n    RepConv module with training and deploy modes.<\/p>\n<p>    This module is used in RT-DETR and can fuse convolutions during inference for efficiency.<\/p>\n<p>    Attributes:<br \/>\n        conv1 (Conv): 3&#215;3 convolution.<br \/>\n        conv2 (Conv): 1&#215;1 convolution.<br \/>\n        bn (nn.BatchNorm2d, optional): Batch normalization for identity branch.<br \/>\n        act (nn.Module): Activation function.<br \/>\n        default_act (nn.Module): Default activation function (SiLU).<\/p>\n<p>    References:<br \/>\n        https:\/\/github.com\/DingXiaoH\/RepVGG\/blob\/main\/repvgg.py<br \/>\n    &#034;&#034;&#034;<\/p>\n<p>    default_act &#061; nn.ReLU6()  # default activation<br \/>\n    # def __init__(self, c1, c2, k&#061;1, s&#061;1, p&#061;None, g&#061;1, d&#061;1, act&#061;True)<br \/>\n    def __init__(self, c1, c2, k&#061;3, s&#061;1, p&#061;1, g&#061;1, d&#061;1, act&#061;True, bn&#061;False, deploy&#061;False):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Initialize RepConv module with given parameters.<\/p>\n<p>        Args:<br \/>\n            c1 (int): Number of input channels.<br \/>\n            c2 (int): Number of output channels.<br \/>\n            k (int): Kernel size.<br \/>\n            s (int): Stride.<br \/>\n            p (int): Padding.<br \/>\n            g (int): Groups.<br \/>\n            d (int): Dilation.<br \/>\n            act (bool | nn.Module): Activation function.<br \/>\n            bn (bool): Use batch normalization for identity branch.<br \/>\n            deploy (bool): Deploy mode for inference.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        assert k &#061;&#061; 3 and p &#061;&#061; 1<br \/>\n        self.g &#061; g<br \/>\n        self.c1 &#061; c1<br \/>\n        self.c2 &#061; c2<br \/>\n        self.act &#061; self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()<\/p>\n<p>        self.bn &#061; nn.BatchNorm2d(num_features&#061;c1) if bn and c2 &#061;&#061; c1 and s &#061;&#061; 1 else None<br \/>\n        # def __init__(self, c1, c2, k&#061;1, s&#061;1, p&#061;None, g&#061;1, d&#061;1, act&#061;True)<br \/>\n        self.conv1 &#061; Conv(c1, c2, k, s, p&#061;p, g&#061;g, act&#061;False)<br \/>\n        self.conv2 &#061; Conv(c1, c2, 1, s, p&#061;(p &#8211; k \/\/ 2), g&#061;g, act&#061;False)<\/p>\n<p>    def forward_fuse(self, x):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Forward pass for deploy mode.<\/p>\n<p>        Args:<br \/>\n            x (torch.Tensor): Input tensor.<\/p>\n<p>        Returns:<br \/>\n            (torch.Tensor): Output tensor.<br \/>\n        &#034;&#034;&#034;<br \/>\n        return self.act(self.conv(x))<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Forward pass for training mode.<\/p>\n<p>        Args:<br \/>\n            x (torch.Tensor): Input tensor.<\/p>\n<p>        Returns:<br \/>\n            (torch.Tensor): Output tensor.<br \/>\n        &#034;&#034;&#034;<br \/>\n        id_out &#061; 0 if self.bn is None else self.bn(x)<br \/>\n        return self.act(self.conv1(x) &#043; self.conv2(x) &#043; id_out)<\/p>\n<p>    def get_equivalent_kernel_bias(self):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Calculate equivalent kernel and bias by fusing convolutions.<\/p>\n<p>        Returns:<br \/>\n            (tuple): Tuple containing:<br \/>\n                &#8211; Equivalent kernel (torch.Tensor)<br \/>\n                &#8211; Equivalent bias (torch.Tensor)<br \/>\n        &#034;&#034;&#034;<br \/>\n        kernel3x3, bias3x3 &#061; self._fuse_bn_tensor(self.conv1)<br \/>\n        kernel1x1, bias1x1 &#061; self._fuse_bn_tensor(self.conv2)<br \/>\n        kernelid, biasid &#061; self._fuse_bn_tensor(self.bn)<br \/>\n        return kernel3x3 &#043; self._pad_1x1_to_3x3_tensor(kernel1x1) &#043; kernelid, bias3x3 &#043; bias1x1 &#043; biasid<\/p>\n<p>    &#064;staticmethod<br \/>\n    def _pad_1x1_to_3x3_tensor(kernel1x1):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Pad a 1&#215;1 kernel to 3&#215;3 size.<\/p>\n<p>        Args:<br \/>\n            kernel1x1 (torch.Tensor): 1&#215;1 convolution kernel.<\/p>\n<p>        Returns:<br \/>\n            (torch.Tensor): Padded 3&#215;3 kernel.<br \/>\n        &#034;&#034;&#034;<br \/>\n        if kernel1x1 is None:<br \/>\n            return 0<br \/>\n        else:<br \/>\n            return torch.nn.functional.pad(kernel1x1, [1, 1, 1, 1])<\/p>\n<p>    def _fuse_bn_tensor(self, branch):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Fuse batch normalization with convolution weights.<\/p>\n<p>        Args:<br \/>\n            branch (Conv | nn.BatchNorm2d | None): Branch to fuse.<\/p>\n<p>        Returns:<br \/>\n            (tuple): Tuple containing:<br \/>\n                &#8211; Fused kernel (torch.Tensor)<br \/>\n                &#8211; Fused bias (torch.Tensor)<br \/>\n        &#034;&#034;&#034;<br \/>\n        if branch is None:<br \/>\n            return 0, 0<br \/>\n        if isinstance(branch, Conv):<br \/>\n            kernel &#061; branch.conv.weight<br \/>\n            running_mean &#061; branch.bn.running_mean<br \/>\n            running_var &#061; branch.bn.running_var<br \/>\n            gamma &#061; branch.bn.weight<br \/>\n            beta &#061; branch.bn.bias<br \/>\n            eps &#061; branch.bn.eps<br \/>\n        elif isinstance(branch, nn.BatchNorm2d):<br \/>\n            if not hasattr(self, &#034;id_tensor&#034;):<br \/>\n                input_dim &#061; self.c1 \/\/ self.g<br \/>\n                kernel_value &#061; np.zeros((self.c1, input_dim, 3, 3), dtype&#061;np.float32)<br \/>\n                for i in range(self.c1):<br \/>\n                    kernel_value[i, i % input_dim, 1, 1] &#061; 1<br \/>\n                self.id_tensor &#061; torch.from_numpy(kernel_value).to(branch.weight.device)<br \/>\n            kernel &#061; self.id_tensor<br \/>\n            running_mean &#061; branch.running_mean<br \/>\n            running_var &#061; branch.running_var<br \/>\n            gamma &#061; branch.weight<br \/>\n            beta &#061; branch.bias<br \/>\n            eps &#061; branch.eps<br \/>\n        std &#061; (running_var &#043; eps).sqrt()<br \/>\n        t &#061; (gamma \/ std).reshape(-1, 1, 1, 1)<br \/>\n        return kernel * t, beta &#8211; running_mean * gamma \/ std<\/p>\n<p>    def fuse_convs(self):<br \/>\n        &#034;&#034;&#034;Fuse convolutions for inference by creating a single equivalent convolution.&#034;&#034;&#034;<br \/>\n        if hasattr(self, &#034;conv&#034;):<br \/>\n            return<br \/>\n        kernel, bias &#061; self.get_equivalent_kernel_bias()<\/p>\n<p>        self.conv &#061; nn.Conv2d(<br \/>\n            in_channels&#061;self.c1,<br \/>\n            out_channels&#061;self.c2,<br \/>\n            kernel_size&#061;self.s,<br \/>\n            stride&#061;self.conv1.conv.stride,<br \/>\n            padding&#061;self.conv1.conv.padding,<br \/>\n            dilation&#061;self.conv1.conv.dilation,<br \/>\n            groups&#061;self.conv1.conv.groups,<br \/>\n            bias&#061;True,<br \/>\n        ).requires_grad_(False)<br \/>\n        self.conv.weight.data &#061; kernel<br \/>\n        self.conv.bias.data &#061; bias<br \/>\n        for para in self.parameters():<br \/>\n            para.detach_()<br \/>\n        self.__delattr__(&#034;conv1&#034;)<br \/>\n        self.__delattr__(&#034;conv2&#034;)<br \/>\n        if hasattr(self, &#034;nm&#034;):<br \/>\n            self.__delattr__(&#034;nm&#034;)<br \/>\n        if hasattr(self, &#034;bn&#034;):<br \/>\n            self.__delattr__(&#034;bn&#034;)<br \/>\n        if hasattr(self, &#034;id_tensor&#034;):<br \/>\n            self.__delattr__(&#034;id_tensor&#034;)<\/p>\n<p>class ECA(nn.Module):<br \/>\n    &#034;&#034;&#034;Constructs a ECA module.<br \/>\n    Args:<br \/>\n        channel: Number of channels of the input feature map<br \/>\n        k_size: Adaptive selection of kernel size<br \/>\n    &#034;&#034;&#034;<\/p>\n<p>    def __init__(self, c1, c2, k_size&#061;3):<br \/>\n        super(ECA, self).__init__()<br \/>\n        self.avg_pool &#061; nn.AdaptiveAvgPool2d(1)<br \/>\n        self.conv &#061; nn.Conv1d(1, 1, kernel_size&#061;k_size, padding&#061;(k_size &#8211; 1) \/\/ 2, bias&#061;False)<br \/>\n        self.sigmoid &#061; nn.Sigmoid()<\/p>\n<p>    def forward(self, x):<br \/>\n        # feature descriptor on the global spatial information<br \/>\n        y &#061; self.avg_pool(x)<br \/>\n        y &#061; self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)<br \/>\n        # Multi-scale information fusion<br \/>\n        y &#061; self.sigmoid(y)<\/p>\n<p>        return x * y.expand_as(x) <\/p>\n<h4 style=\"background-color:transparent\"><span style=\"color:#4da8ee\">\u5f15\u5165\u521b\u65b0\u6a21\u5757&#xff08;\u8fdb\u9636&#xff0c;\u53ef\u9009&#xff09;<\/span><\/h4>\n<p>%%writefile \/kaggle\/working\/yolov5\/models\/yolo.py<br \/>\n# \u4fee\u6539 yolo.py<br \/>\n# Ultralytics &#x1f680; AGPL-3.0 License &#8211; https:\/\/ultralytics.com\/license<br \/>\n&#034;&#034;&#034;<br \/>\nYOLO-specific modules.<\/p>\n<p>Usage:<br \/>\n    $ python models\/yolo.py &#8211;cfg yolov5s.yaml<br \/>\n&#034;&#034;&#034;<\/p>\n<p>import argparse<br \/>\nimport contextlib<br \/>\nimport math<br \/>\nimport os<br \/>\nimport platform<br \/>\nimport sys<br \/>\nfrom copy import deepcopy<br \/>\nfrom pathlib import Path<\/p>\n<p>import torch<br \/>\nimport torch.nn as nn<\/p>\n<p>FILE &#061; Path(__file__).resolve()<br \/>\nROOT &#061; FILE.parents[1]  # YOLOv5 root directory<br \/>\nif str(ROOT) not in sys.path:<br \/>\n    sys.path.append(str(ROOT))  # add ROOT to PATH<br \/>\nif platform.system() !&#061; &#034;Windows&#034;:<br \/>\n    ROOT &#061; Path(os.path.relpath(ROOT, Path.cwd()))  # relative<br \/>\nfrom models.common import (<br \/>\n    ECA,<br \/>\n    C3,<br \/>\n    C3SPP,<br \/>\n    C3TR,<br \/>\n    SPP,<br \/>\n    SPPF,<br \/>\n    Bottleneck,<br \/>\n    BottleneckCSP,<br \/>\n    C3Ghost,<br \/>\n    C3x,<br \/>\n    Classify,<br \/>\n    Concat,<br \/>\n    Contract,<br \/>\n    Conv,<br \/>\n    RepConv,<br \/>\n    CrossConv,<br \/>\n    DetectMultiBackend,<br \/>\n    DWConv,<br \/>\n    DWConvTranspose2d,<br \/>\n    Expand,<br \/>\n    Focus,<br \/>\n    GhostBottleneck,<br \/>\n    GhostConv,<br \/>\n    Proto,<br \/>\n)<br \/>\nfrom models.experimental import MixConv2d<br \/>\nfrom utils.autoanchor import check_anchor_order<br \/>\nfrom utils.general import LOGGER, check_version, check_yaml, colorstr, make_divisible, print_args<br \/>\nfrom utils.plots import feature_visualization<br \/>\nfrom utils.torch_utils import (<br \/>\n    fuse_conv_and_bn,<br \/>\n    initialize_weights,<br \/>\n    model_info,<br \/>\n    profile,<br \/>\n    scale_img,<br \/>\n    select_device,<br \/>\n    time_sync,<br \/>\n)<\/p>\n<p>try:<br \/>\n    import thop  # for FLOPs computation<br \/>\nexcept ImportError:<br \/>\n    thop &#061; None<\/p>\n<p>class Detect(nn.Module):<br \/>\n    &#034;&#034;&#034;YOLOv5 Detect head for processing input tensors and generating detection outputs in object detection models.&#034;&#034;&#034;<\/p>\n<p>    stride &#061; None  # strides computed during build<br \/>\n    dynamic &#061; False  # force grid reconstruction<br \/>\n    export &#061; False  # export mode<\/p>\n<p>    def __init__(self, nc&#061;80, anchors&#061;(), ch&#061;(), inplace&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5 detection layer with specified classes, anchors, channels, and inplace operations.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self.nc &#061; nc  # number of classes<br \/>\n        self.no &#061; nc &#043; 5  # number of outputs per anchor<br \/>\n        self.nl &#061; len(anchors)  # number of detection layers<br \/>\n        self.na &#061; len(anchors[0]) \/\/ 2  # number of anchors<br \/>\n        self.grid &#061; [torch.empty(0) for _ in range(self.nl)]  # init grid<br \/>\n        self.anchor_grid &#061; [torch.empty(0) for _ in range(self.nl)]  # init anchor grid<br \/>\n        self.register_buffer(&#034;anchors&#034;, torch.tensor(anchors).float().view(self.nl, -1, 2))  # shape(nl,na,2)<br \/>\n        self.m &#061; nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch)  # output conv<br \/>\n        self.inplace &#061; inplace  # use inplace ops (e.g. slice assignment)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through YOLOv5 layers, altering shape for detection: &#096;x(bs, 3, ny, nx, 85)&#096;.&#034;&#034;&#034;<br \/>\n        z &#061; []  # inference output<br \/>\n        for i in range(self.nl):<br \/>\n            x[i] &#061; self.m[i](x[i])  # conv<br \/>\n            bs, _, ny, nx &#061; x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)<br \/>\n            x[i] &#061; x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()<\/p>\n<p>            if not self.training:  # inference<br \/>\n                if self.dynamic or self.grid[i].shape[2:4] !&#061; x[i].shape[2:4]:<br \/>\n                    self.grid[i], self.anchor_grid[i] &#061; self._make_grid(nx, ny, i)<\/p>\n<p>                if isinstance(self, Segment):  # (boxes &#043; masks)<br \/>\n                    xy, wh, conf, mask &#061; x[i].split((2, 2, self.nc &#043; 1, self.no &#8211; self.nc &#8211; 5), 4)<br \/>\n                    xy &#061; (xy.sigmoid() * 2 &#043; self.grid[i]) * self.stride[i]  # xy<br \/>\n                    wh &#061; (wh.sigmoid() * 2) ** 2 * self.anchor_grid[i]  # wh<br \/>\n                    y &#061; torch.cat((xy, wh, conf.sigmoid(), mask), 4)<br \/>\n                else:  # Detect (boxes only)<br \/>\n                    xy, wh, conf &#061; x[i].sigmoid().split((2, 2, self.nc &#043; 1), 4)<br \/>\n                    xy &#061; (xy * 2 &#043; self.grid[i]) * self.stride[i]  # xy<br \/>\n                    wh &#061; (wh * 2) ** 2 * self.anchor_grid[i]  # wh<br \/>\n                    y &#061; torch.cat((xy, wh, conf), 4)<br \/>\n                z.append(y.view(bs, self.na * nx * ny, self.no))<\/p>\n<p>        return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)<\/p>\n<p>    def _make_grid(self, nx&#061;20, ny&#061;20, i&#061;0, torch_1_10&#061;check_version(torch.__version__, &#034;1.10.0&#034;)):<br \/>\n        &#034;&#034;&#034;Generates a mesh grid for anchor boxes with optional compatibility for torch versions &lt; 1.10.&#034;&#034;&#034;<br \/>\n        d &#061; self.anchors[i].device<br \/>\n        t &#061; self.anchors[i].dtype<br \/>\n        shape &#061; 1, self.na, ny, nx, 2  # grid shape<br \/>\n        y, x &#061; torch.arange(ny, device&#061;d, dtype&#061;t), torch.arange(nx, device&#061;d, dtype&#061;t)<br \/>\n        yv, xv &#061; torch.meshgrid(y, x, indexing&#061;&#034;ij&#034;) if torch_1_10 else torch.meshgrid(y, x)  # torch&gt;&#061;0.7 compatibility<br \/>\n        grid &#061; torch.stack((xv, yv), 2).expand(shape) &#8211; 0.5  # add grid offset, i.e. y &#061; 2.0 * x &#8211; 0.5<br \/>\n        anchor_grid &#061; (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape)<br \/>\n        return grid, anchor_grid<\/p>\n<p>class Segment(Detect):<br \/>\n    &#034;&#034;&#034;YOLOv5 Segment head for segmentation models, extending Detect with mask and prototype layers.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, nc&#061;80, anchors&#061;(), nm&#061;32, npr&#061;256, ch&#061;(), inplace&#061;True):<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5 Segment head with options for mask count, protos, and channel adjustments.&#034;&#034;&#034;<br \/>\n        super().__init__(nc, anchors, ch, inplace)<br \/>\n        self.nm &#061; nm  # number of masks<br \/>\n        self.npr &#061; npr  # number of protos<br \/>\n        self.no &#061; 5 &#043; nc &#043; self.nm  # number of outputs per anchor<br \/>\n        self.m &#061; nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch)  # output conv<br \/>\n        self.proto &#061; Proto(ch[0], self.npr, self.nm)  # protos<br \/>\n        self.detect &#061; Detect.forward<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;Processes input through the network, returning detections and prototypes; adjusts output based on<br \/>\n        training\/export mode.<br \/>\n        &#034;&#034;&#034;<br \/>\n        p &#061; self.proto(x[0])<br \/>\n        x &#061; self.detect(self, x)<br \/>\n        return (x, p) if self.training else (x[0], p) if self.export else (x[0], p, x[1])<\/p>\n<p>class BaseModel(nn.Module):<br \/>\n    &#034;&#034;&#034;YOLOv5 base model.&#034;&#034;&#034;<\/p>\n<p>    def forward(self, x, profile&#061;False, visualize&#061;False):<br \/>\n        &#034;&#034;&#034;Executes a single-scale inference or training pass on the YOLOv5 base model, with options for profiling and<br \/>\n        visualization.<br \/>\n        &#034;&#034;&#034;<br \/>\n        return self._forward_once(x, profile, visualize)  # single-scale inference, train<\/p>\n<p>    def _forward_once(self, x, profile&#061;False, visualize&#061;False):<br \/>\n        &#034;&#034;&#034;Performs a forward pass on the YOLOv5 model, enabling profiling and feature visualization options.&#034;&#034;&#034;<br \/>\n        y, dt &#061; [], []  # outputs<br \/>\n        for m in self.model:<br \/>\n            if m.f !&#061; -1:  # if not from previous layer<br \/>\n                x &#061; y[m.f] if isinstance(m.f, int) else [x if j &#061;&#061; -1 else y[j] for j in m.f]  # from earlier layers<br \/>\n            if profile:<br \/>\n                self._profile_one_layer(m, x, dt)<br \/>\n            x &#061; m(x)  # run<br \/>\n            y.append(x if m.i in self.save else None)  # save output<br \/>\n            if visualize:<br \/>\n                feature_visualization(x, m.type, m.i, save_dir&#061;visualize)<br \/>\n        return x<\/p>\n<p>    def _profile_one_layer(self, m, x, dt):<br \/>\n        &#034;&#034;&#034;Profiles a single layer&#039;s performance by computing GFLOPs, execution time, and parameters.&#034;&#034;&#034;<br \/>\n        c &#061; m &#061;&#061; self.model[-1]  # is final layer, copy input as inplace fix<br \/>\n        o &#061; thop.profile(m, inputs&#061;(x.copy() if c else x,), verbose&#061;False)[0] \/ 1e9 * 2 if thop else 0  # FLOPs<br \/>\n        t &#061; time_sync()<br \/>\n        for _ in range(10):<br \/>\n            m(x.copy() if c else x)<br \/>\n        dt.append((time_sync() &#8211; t) * 100)<br \/>\n        if m &#061;&#061; self.model[0]:<br \/>\n            LOGGER.info(f&#034;{&#039;time (ms)&#039;:&gt;10s} {&#039;GFLOPs&#039;:&gt;10s} {&#039;params&#039;:&gt;10s}  module&#034;)<br \/>\n        LOGGER.info(f&#034;{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f}  {m.type}&#034;)<br \/>\n        if c:<br \/>\n            LOGGER.info(f&#034;{sum(dt):10.2f} {&#039;-&#039;:&gt;10s} {&#039;-&#039;:&gt;10s}  Total&#034;)<\/p>\n<p>    def fuse(self):<br \/>\n        &#034;&#034;&#034;Fuses Conv2d() and BatchNorm2d() layers in the model to improve inference speed.&#034;&#034;&#034;<br \/>\n        LOGGER.info(&#034;Fusing layers&#8230; &#034;)<br \/>\n        for m in self.model.modules():<br \/>\n            if isinstance(m, (Conv, DWConv)) and hasattr(m, &#034;bn&#034;):<br \/>\n                m.conv &#061; fuse_conv_and_bn(m.conv, m.bn)  # update conv<br \/>\n                delattr(m, &#034;bn&#034;)  # remove batchnorm<br \/>\n                m.forward &#061; m.forward_fuse  # update forward<br \/>\n        self.info()<br \/>\n        return self<\/p>\n<p>    def info(self, verbose&#061;False, img_size&#061;640):<br \/>\n        &#034;&#034;&#034;Prints model information given verbosity and image size, e.g., &#096;info(verbose&#061;True, img_size&#061;640)&#096;.&#034;&#034;&#034;<br \/>\n        model_info(self, verbose, img_size)<\/p>\n<p>    def _apply(self, fn):<br \/>\n        &#034;&#034;&#034;Applies transformations like to(), cpu(), cuda(), half() to model tensors excluding parameters or registered<br \/>\n        buffers.<br \/>\n        &#034;&#034;&#034;<br \/>\n        self &#061; super()._apply(fn)<br \/>\n        m &#061; self.model[-1]  # Detect()<br \/>\n        if isinstance(m, (Detect, Segment)):<br \/>\n            m.stride &#061; fn(m.stride)<br \/>\n            m.grid &#061; list(map(fn, m.grid))<br \/>\n            if isinstance(m.anchor_grid, list):<br \/>\n                m.anchor_grid &#061; list(map(fn, m.anchor_grid))<br \/>\n        return self<\/p>\n<p>class DetectionModel(BaseModel):<br \/>\n    &#034;&#034;&#034;YOLOv5 detection model class for object detection tasks, supporting custom configurations and anchors.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, cfg&#061;&#034;yolov5s.yaml&#034;, ch&#061;3, nc&#061;None, anchors&#061;None):<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5 model with configuration file, input channels, number of classes, and custom anchors.&#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        if isinstance(cfg, dict):<br \/>\n            self.yaml &#061; cfg  # model dict<br \/>\n        else:  # is *.yaml<br \/>\n            import yaml  # for torch hub<\/p>\n<p>            self.yaml_file &#061; Path(cfg).name<br \/>\n            with open(cfg, encoding&#061;&#034;ascii&#034;, errors&#061;&#034;ignore&#034;) as f:<br \/>\n                self.yaml &#061; yaml.safe_load(f)  # model dict<\/p>\n<p>        # Define model<br \/>\n        ch &#061; self.yaml[&#034;ch&#034;] &#061; self.yaml.get(&#034;ch&#034;, ch)  # input channels<br \/>\n        if nc and nc !&#061; self.yaml[&#034;nc&#034;]:<br \/>\n            LOGGER.info(f&#034;Overriding model.yaml nc&#061;{self.yaml[&#039;nc&#039;]} with nc&#061;{nc}&#034;)<br \/>\n            self.yaml[&#034;nc&#034;] &#061; nc  # override yaml value<br \/>\n        if anchors:<br \/>\n            LOGGER.info(f&#034;Overriding model.yaml anchors with anchors&#061;{anchors}&#034;)<br \/>\n            self.yaml[&#034;anchors&#034;] &#061; round(anchors)  # override yaml value<br \/>\n        self.model, self.save &#061; parse_model(deepcopy(self.yaml), ch&#061;[ch])  # model, savelist<br \/>\n        self.names &#061; [str(i) for i in range(self.yaml[&#034;nc&#034;])]  # default names<br \/>\n        self.inplace &#061; self.yaml.get(&#034;inplace&#034;, True)<\/p>\n<p>        # Build strides, anchors<br \/>\n        m &#061; self.model[-1]  # Detect()<br \/>\n        if isinstance(m, (Detect, Segment)):<\/p>\n<p>            def _forward(x):<br \/>\n                &#034;&#034;&#034;Passes the input &#039;x&#039; through the model and returns the processed output.&#034;&#034;&#034;<br \/>\n                return self.forward(x)[0] if isinstance(m, Segment) else self.forward(x)<\/p>\n<p>            s &#061; 256  # 2x min stride<br \/>\n            m.inplace &#061; self.inplace<br \/>\n            m.stride &#061; torch.tensor([s \/ x.shape[-2] for x in _forward(torch.zeros(1, ch, s, s))])  # forward<br \/>\n            check_anchor_order(m)<br \/>\n            m.anchors \/&#061; m.stride.view(-1, 1, 1)<br \/>\n            self.stride &#061; m.stride<br \/>\n            self._initialize_biases()  # only run once<\/p>\n<p>        # Init weights, biases<br \/>\n        initialize_weights(self)<br \/>\n        self.info()<br \/>\n        LOGGER.info(&#034;&#034;)<\/p>\n<p>    def forward(self, x, augment&#061;False, profile&#061;False, visualize&#061;False):<br \/>\n        &#034;&#034;&#034;Performs single-scale or augmented inference and may include profiling or visualization.&#034;&#034;&#034;<br \/>\n        if augment:<br \/>\n            return self._forward_augment(x)  # augmented inference, None<br \/>\n        return self._forward_once(x, profile, visualize)  # single-scale inference, train<\/p>\n<p>    def _forward_augment(self, x):<br \/>\n        &#034;&#034;&#034;Performs augmented inference across different scales and flips, returning combined detections.&#034;&#034;&#034;<br \/>\n        img_size &#061; x.shape[-2:]  # height, width<br \/>\n        s &#061; [1, 0.83, 0.67]  # scales<br \/>\n        f &#061; [None, 3, None]  # flips (2-ud, 3-lr)<br \/>\n        y &#061; []  # outputs<br \/>\n        for si, fi in zip(s, f):<br \/>\n            xi &#061; scale_img(x.flip(fi) if fi else x, si, gs&#061;int(self.stride.max()))<br \/>\n            yi &#061; self._forward_once(xi)[0]  # forward<br \/>\n            # cv2.imwrite(f&#039;img_{si}.jpg&#039;, 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1])  # save<br \/>\n            yi &#061; self._descale_pred(yi, fi, si, img_size)<br \/>\n            y.append(yi)<br \/>\n        y &#061; self._clip_augmented(y)  # clip augmented tails<br \/>\n        return torch.cat(y, 1), None  # augmented inference, train<\/p>\n<p>    def _descale_pred(self, p, flips, scale, img_size):<br \/>\n        &#034;&#034;&#034;De-scales predictions from augmented inference, adjusting for flips and image size.&#034;&#034;&#034;<br \/>\n        if self.inplace:<br \/>\n            p[&#8230;, :4] \/&#061; scale  # de-scale<br \/>\n            if flips &#061;&#061; 2:<br \/>\n                p[&#8230;, 1] &#061; img_size[0] &#8211; p[&#8230;, 1]  # de-flip ud<br \/>\n            elif flips &#061;&#061; 3:<br \/>\n                p[&#8230;, 0] &#061; img_size[1] &#8211; p[&#8230;, 0]  # de-flip lr<br \/>\n        else:<br \/>\n            x, y, wh &#061; p[&#8230;, 0:1] \/ scale, p[&#8230;, 1:2] \/ scale, p[&#8230;, 2:4] \/ scale  # de-scale<br \/>\n            if flips &#061;&#061; 2:<br \/>\n                y &#061; img_size[0] &#8211; y  # de-flip ud<br \/>\n            elif flips &#061;&#061; 3:<br \/>\n                x &#061; img_size[1] &#8211; x  # de-flip lr<br \/>\n            p &#061; torch.cat((x, y, wh, p[&#8230;, 4:]), -1)<br \/>\n        return p<\/p>\n<p>    def _clip_augmented(self, y):<br \/>\n        &#034;&#034;&#034;Clips augmented inference tails for YOLOv5 models, affecting first and last tensors based on grid points and<br \/>\n        layer counts.<br \/>\n        &#034;&#034;&#034;<br \/>\n        nl &#061; self.model[-1].nl  # number of detection layers (P3-P5)<br \/>\n        g &#061; sum(4**x for x in range(nl))  # grid points<br \/>\n        e &#061; 1  # exclude layer count<br \/>\n        i &#061; (y[0].shape[1] \/\/ g) * sum(4**x for x in range(e))  # indices<br \/>\n        y[0] &#061; y[0][:, :-i]  # large<br \/>\n        i &#061; (y[-1].shape[1] \/\/ g) * sum(4 ** (nl &#8211; 1 &#8211; x) for x in range(e))  # indices<br \/>\n        y[-1] &#061; y[-1][:, i:]  # small<br \/>\n        return y<\/p>\n<p>    def _initialize_biases(self, cf&#061;None):<br \/>\n        &#034;&#034;&#034;<br \/>\n        Initializes biases for YOLOv5&#039;s Detect() module, optionally using class frequencies (cf).<\/p>\n<p>        For details see https:\/\/arxiv.org\/abs\/1708.02002 section 3.3.<br \/>\n        &#034;&#034;&#034;<br \/>\n        # cf &#061; torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength&#061;nc) &#043; 1.<br \/>\n        m &#061; self.model[-1]  # Detect() module<br \/>\n        for mi, s in zip(m.m, m.stride):  # from<br \/>\n            b &#061; mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)<br \/>\n            b.data[:, 4] &#043;&#061; math.log(8 \/ (640 \/ s) ** 2)  # obj (8 objects per 640 image)<br \/>\n            b.data[:, 5 : 5 &#043; m.nc] &#043;&#061; (<br \/>\n                math.log(0.6 \/ (m.nc &#8211; 0.99999)) if cf is None else torch.log(cf \/ cf.sum())<br \/>\n            )  # cls<br \/>\n            mi.bias &#061; torch.nn.Parameter(b.view(-1), requires_grad&#061;True)<\/p>\n<p>Model &#061; DetectionModel  # retain YOLOv5 &#039;Model&#039; class for backwards compatibility<\/p>\n<p>class SegmentationModel(DetectionModel):<br \/>\n    &#034;&#034;&#034;YOLOv5 segmentation model for object detection and segmentation tasks with configurable parameters.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, cfg&#061;&#034;yolov5s-seg.yaml&#034;, ch&#061;3, nc&#061;None, anchors&#061;None):<br \/>\n        &#034;&#034;&#034;Initializes a YOLOv5 segmentation model with configurable params: cfg (str) for configuration, ch (int) for channels, nc (int) for num classes, anchors (list).&#034;&#034;&#034;<br \/>\n        super().__init__(cfg, ch, nc, anchors)<\/p>\n<p>class ClassificationModel(BaseModel):<br \/>\n    &#034;&#034;&#034;YOLOv5 classification model for image classification tasks, initialized with a config file or detection model.&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, cfg&#061;None, model&#061;None, nc&#061;1000, cutoff&#061;10):<br \/>\n        &#034;&#034;&#034;Initializes YOLOv5 model with config file &#096;cfg&#096;, input channels &#096;ch&#096;, number of classes &#096;nc&#096;, and &#096;cuttoff&#096;<br \/>\n        index.<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        self._from_detection_model(model, nc, cutoff) if model is not None else self._from_yaml(cfg)<\/p>\n<p>    def _from_detection_model(self, model, nc&#061;1000, cutoff&#061;10):<br \/>\n        &#034;&#034;&#034;Creates a classification model from a YOLOv5 detection model, slicing at &#096;cutoff&#096; and adding a classification<br \/>\n        layer.<br \/>\n        &#034;&#034;&#034;<br \/>\n        if isinstance(model, DetectMultiBackend):<br \/>\n            model &#061; model.model  # unwrap DetectMultiBackend<br \/>\n        model.model &#061; model.model[:cutoff]  # backbone<br \/>\n        m &#061; model.model[-1]  # last layer<br \/>\n        ch &#061; m.conv.in_channels if hasattr(m, &#034;conv&#034;) else m.cv1.conv.in_channels  # ch into module<br \/>\n        c &#061; Classify(ch, nc)  # Classify()<br \/>\n        c.i, c.f, c.type &#061; m.i, m.f, &#034;models.common.Classify&#034;  # index, from, type<br \/>\n        model.model[-1] &#061; c  # replace<br \/>\n        self.model &#061; model.model<br \/>\n        self.stride &#061; model.stride<br \/>\n        self.save &#061; []<br \/>\n        self.nc &#061; nc<\/p>\n<p>    def _from_yaml(self, cfg):<br \/>\n        &#034;&#034;&#034;Creates a YOLOv5 classification model from a specified *.yaml configuration file.&#034;&#034;&#034;<br \/>\n        self.model &#061; None<\/p>\n<p># \u6a21\u578b\u6784\u5efa<br \/>\ndef parse_model(d, ch):<br \/>\n    &#034;&#034;&#034;Parses a YOLOv5 model from a dict &#096;d&#096;, configuring layers based on input channels &#096;ch&#096; and model architecture.&#034;&#034;&#034;<br \/>\n    LOGGER.info(f&#034;\\\\n{&#039;&#039;:&gt;3}{&#039;from&#039;:&gt;18}{&#039;n&#039;:&gt;3}{&#039;params&#039;:&gt;10}  {&#039;module&#039;:&lt;40}{&#039;arguments&#039;:&lt;30}&#034;)<br \/>\n    anchors, nc, gd, gw, act, ch_mul &#061; (<br \/>\n        d[&#034;anchors&#034;],<br \/>\n        d[&#034;nc&#034;],<br \/>\n        d[&#034;depth_multiple&#034;],<br \/>\n        d[&#034;width_multiple&#034;],<br \/>\n        d.get(&#034;activation&#034;),<br \/>\n        d.get(&#034;channel_multiple&#034;),<br \/>\n    )<br \/>\n    # \u5982\u679c act \u914d\u7f6e\u9879\u5b58\u5728&#xff0c;\u5c31\u6839\u636e\u914d\u7f6e\u4fee\u6539\u5377\u79ef\u5c42\u7684\u9ed8\u8ba4\u6fc0\u6d3b\u51fd\u6570\u3002<br \/>\n    if act:<br \/>\n        Conv.default_act &#061; eval(act)  # redefine default activation, i.e. Conv.default_act &#061; nn.SiLU()<br \/>\n        RepConv.default_act &#061; eval(act)<\/p>\n<p>        LOGGER.info(f&#034;{colorstr(&#039;activation:&#039;)} {act}&#034;)  # print<br \/>\n    if not ch_mul:<br \/>\n        ch_mul &#061; 8<br \/>\n    # \u6bcf\u4e2a\u951a\u70b9\u7684\u6570\u91cf&#xff0c;\u901a\u5e38\u4e3a\u951a\u70b9\u5217\u8868\u4e2d\u7684\u6bcf\u5bf9(width, height)<br \/>\n    na &#061; (len(anchors[0]) \/\/ 2) if isinstance(anchors, list) else anchors  # number of anchors<br \/>\n    # \u6bcf\u4e2a\u8f93\u51fa\u7684\u901a\u9053\u6570&#xff0c;\u8ba1\u7b97\u516c\u5f0f\u4e3a \u951a\u70b9\u6570 * (\u7c7b\u522b\u6570 &#043; 5)&#xff0c;5 \u4ee3\u8868\u8fb9\u754c\u6846\u7684 4 \u4e2a\u5750\u6807\u52a0\u4e0a\u7f6e\u4fe1\u5ea6\u3002<br \/>\n    no &#061; na * (nc &#043; 5)  # number of outputs &#061; anchors * (classes &#043; 5)<\/p>\n<p>    layers, save, c2 &#061; [], [], ch[-1]  # layers, savelist, ch out<br \/>\n    # \u89e3\u6790\u6bcf\u4e00\u884c\u7ed3\u6784<br \/>\n    for i, (f, n, m, args) in enumerate(d[&#034;backbone&#034;] &#043; d[&#034;head&#034;]):  # from, number, module, args<br \/>\n        # \u52a8\u6001\u89e3\u6790\u5b57\u7b26\u4e32\u4e3a\u5bf9\u5e94\u7684\u7c7b<br \/>\n        m &#061; eval(m) if isinstance(m, str) else m  # eval strings<br \/>\n        for j, a in enumerate(args):<br \/>\n            with contextlib.suppress(NameError):<br \/>\n                args[j] &#061; eval(a) if isinstance(a, str) else a  # eval strings<br \/>\n        # \u91cd\u590d\u6570\u91cf<br \/>\n        n &#061; n_ &#061; max(round(n * gd), 1) if n &gt; 1 else n  # depth gain<br \/>\n        # \u5377\u79ef\u5c42\u5904\u7406<br \/>\n        if m in {<br \/>\n            Conv,<br \/>\n            RepConv,<br \/>\n            GhostConv,<br \/>\n            Bottleneck,<br \/>\n            GhostBottleneck,<br \/>\n            SPP,<br \/>\n            SPPF,<br \/>\n            DWConv,<br \/>\n            MixConv2d,<br \/>\n            Focus,<br \/>\n            CrossConv,<br \/>\n            BottleneckCSP,<br \/>\n            C3,<br \/>\n            ECA,<br \/>\n            C3TR,<br \/>\n            C3SPP,<br \/>\n            C3Ghost,<br \/>\n            nn.ConvTranspose2d,<br \/>\n            DWConvTranspose2d,<br \/>\n            C3x,<br \/>\n        }:<br \/>\n            c1, c2 &#061; ch[f], args[0]<br \/>\n            if c2 !&#061; no:  # if not output<br \/>\n                c2 &#061; make_divisible(c2 * gw, ch_mul)<\/p>\n<p>            args &#061; [c1, c2, *args[1:]]<br \/>\n            if m in {BottleneckCSP, C3, C3TR, C3Ghost, C3x}:<br \/>\n                args.insert(2, n)  # number of repeats<br \/>\n                n &#061; 1<br \/>\n        elif m is nn.BatchNorm2d:<br \/>\n            args &#061; [ch[f]]<br \/>\n        elif m is Concat:<br \/>\n            c2 &#061; sum(ch[x] for x in f)<br \/>\n        # TODO: channel, gw, gd<br \/>\n        elif m in {Detect, Segment}:<br \/>\n            args.append([ch[x] for x in f])<br \/>\n            if isinstance(args[1], int):  # number of anchors<br \/>\n                args[1] &#061; [list(range(args[1] * 2))] * len(f)<br \/>\n            if m is Segment:<br \/>\n                args[3] &#061; make_divisible(args[3] * gw, ch_mul)<br \/>\n        elif m is Contract:<br \/>\n            c2 &#061; ch[f] * args[0] ** 2<br \/>\n        elif m is Expand:<br \/>\n            c2 &#061; ch[f] \/\/ args[0] ** 2<br \/>\n        else:<br \/>\n            c2 &#061; ch[f]<\/p>\n<p>        m_ &#061; nn.Sequential(*(m(*args) for _ in range(n))) if n &gt; 1 else m(*args)  # module<br \/>\n        t &#061; str(m)[8:-2].replace(&#034;__main__.&#034;, &#034;&#034;)  # module type<br \/>\n        np &#061; sum(x.numel() for x in m_.parameters())  # number params<br \/>\n        m_.i, m_.f, m_.type, m_.np &#061; i, f, t, np  # attach index, &#039;from&#039; index, type, number params<br \/>\n        LOGGER.info(f&#034;{i:&gt;3}{str(f):&gt;18}{n_:&gt;3}{np:10.0f}  {t:&lt;40}{str(args):&lt;30}&#034;)  # print<br \/>\n        save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x !&#061; -1)  # append to savelist<br \/>\n        # \u4fdd\u5b58\u7ed3\u6784\u5c42<br \/>\n        layers.append(m_)<br \/>\n        if i &#061;&#061; 0:<br \/>\n            ch &#061; []<br \/>\n        ch.append(c2)<br \/>\n    return nn.Sequential(*layers), sorted(save)<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    parser &#061; argparse.ArgumentParser()<br \/>\n    parser.add_argument(&#034;&#8211;cfg&#034;, type&#061;str, default&#061;&#034;yolov5s_custom.yaml&#034;, help&#061;&#034;model.yaml&#034;)<br \/>\n    parser.add_argument(&#034;&#8211;batch-size&#034;, type&#061;int, default&#061;1, help&#061;&#034;total batch size for all GPUs&#034;)<br \/>\n    parser.add_argument(&#034;&#8211;device&#034;, default&#061;&#034;cpu&#034;, help&#061;&#034;cuda device, i.e. 0 or 0,1,2,3 or cpu&#034;)<br \/>\n    parser.add_argument(&#034;&#8211;profile&#034;, action&#061;&#034;store_true&#034;, help&#061;&#034;profile model speed&#034;)<br \/>\n    parser.add_argument(&#034;&#8211;line-profile&#034;, action&#061;&#034;store_true&#034;, help&#061;&#034;profile model speed layer by layer&#034;)<br \/>\n    parser.add_argument(&#034;&#8211;test&#034;, action&#061;&#034;store_true&#034;, help&#061;&#034;test all yolo*.yaml&#034;)<br \/>\n    opt &#061; parser.parse_args()<br \/>\n    opt.cfg &#061; check_yaml(opt.cfg)  # check YAML<br \/>\n    print_args(vars(opt))<br \/>\n    device &#061; select_device(opt.device)<\/p>\n<p>    # Create model<br \/>\n    im &#061; torch.rand(opt.batch_size, 3, 640, 640).to(device)<br \/>\n    model &#061; Model(opt.cfg).to(device)<\/p>\n<p>    # Options<br \/>\n    if opt.line_profile:  # profile layer by layer<br \/>\n        model(im, profile&#061;True)<\/p>\n<p>    elif opt.profile:  # profile forward-backward<br \/>\n        results &#061; profile(input&#061;im, ops&#061;[model], n&#061;3)<\/p>\n<p>    elif opt.test:  # test all models<br \/>\n        for cfg in Path(ROOT \/ &#034;models&#034;).rglob(&#034;yolo*.yaml&#034;):<br \/>\n            try:<br \/>\n                _ &#061; Model(cfg)<br \/>\n            except Exception as e:<br \/>\n                print(f&#034;Error in {cfg}: {e}&#034;)<\/p>\n<p>    else:  # report fused model summary<br \/>\n        model.fuse()<\/p>\n<h2 style=\"background-color:transparent;text-align:center\"><\/h2>\n<h2 style=\"background-color:transparent;text-align:center\"><span style=\"color:#4da8ee\">\u8bad\u7ec3\u6a21\u578b<\/span><\/h2>\n<hr \/>\n<p>\u76f4\u63a5\u8bad\u7ec3\u547d\u4ee4\u5373\u53ef\u5f00\u59cb\u7545\u60f3\u6f2b\u957f\u7684\u7b49\u5f85\u65f6\u523b&#xff0c;\u73b0\u5728\u662f\u6296\u97f3\u65f6\u95f4&#xff01;&#xff01;&#xff01;<\/p>\n<p>!python train.py &#8211;data coco128.yaml &#8211;cfg yolov5n.yaml &#8211;weights &#039;&#039; &#8211;epochs 150 &#8211;batch-size 64 &#8211;img-size 640  &#8211;hyp hyp.scratch-low.yaml &#8211;device 0  <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u9700\u8981\u4e0b\u8f7d\u6307\u5b9a\u6587\u4ef6\u5939\u7684&#xff0c;\u6bd4\u5982\u8bad\u7ec3\u7ed3\u679c&#xff0c;\u73b0\u5728\u538b\u7f29&#xff0c;\u7136\u540e\u5728\u53f3\u4fa7\u9009\u4e2d\u6587\u4ef6&#xff0c;\u53f3\u8fb9\u70b9\u51fb\u4e09\u4e2a\u5c0f\u70b9\u8fdb\u884c\u4e0b\u8f7d&#xff1a;<\/p>\n<p>\u6ca1\u53cd\u5e94\u7684\u8bf7F12\u6293\u53d6\u4e0b\u8f7d\u94fe\u63a5&#xff0c;\u76f4\u63a5\u590d\u5236\u5230\u6d4f\u89c8\u5668\u641c\u7d22\u680f\u5373\u53ef\u4e0b\u8f7d\u3002<\/p>\n<p>import os<br \/>\n\u00a0import zipfile<br \/>\n\u00a0\u200b<br \/>\n\u00a0def zip_folder(source_folder, output_zip):<br \/>\n\u00a0 \u00a0  &#034;&#034;&#034;<br \/>\n\u00a0 \u00a0  \u5c06\u6307\u5b9a\u76ee\u5f55\u6253\u5305\u4e3a ZIP \u6587\u4ef6\u3002<br \/>\n\u00a0 \u00a0 \u00a0<br \/>\n\u00a0 \u00a0  :param source_folder: \u9700\u8981\u538b\u7f29\u7684\u76ee\u5f55\u8def\u5f84<br \/>\n\u00a0 \u00a0  :param output_zip: \u8f93\u51fa ZIP \u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84&#xff08;\u5305\u542b .zip&#xff09;<br \/>\n\u00a0 \u00a0  &#034;&#034;&#034;<br \/>\n\u00a0 \u00a0  with zipfile.ZipFile(output_zip, &#039;w&#039;, zipfile.ZIP_DEFLATED) as zipf:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0  for root, dirs, files in os.walk(source_folder):<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  for file in files:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  file_path &#061; os.path.join(root, file)<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  arcname &#061; os.path.relpath(file_path, source_folder)  # \u4fdd\u6301\u76f8\u5bf9\u8def\u5f84<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  zipf.write(file_path, arcname)<br \/>\n\u00a0 \u00a0  print(f&#034;\u6587\u4ef6\u5939 &#039;{source_folder}&#039; \u5df2\u6210\u529f\u538b\u7f29\u81f3 &#039;{output_zip}&#039;&#034;)<br \/>\n\u00a0\u200b<br \/>\n\u00a0if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n\u00a0 \u00a0  source_directory &#061; &#034;.\/runs\/train\/exp2&#034;  # \u9700\u8981\u538b\u7f29\u7684\u6587\u4ef6\u5939\u8def\u5f84<br \/>\n\u00a0 \u00a0  output_zip_path &#061; &#034;.\/ECA_4_9_150.zip&#034;  # \u76ee\u6807 ZIP \u6587\u4ef6\u8def\u5f84<br \/>\n\u00a0 \u00a0 \u00a0<br \/>\n\u00a0 \u00a0  # \u786e\u4fdd\u8f93\u51fa\u76ee\u5f55\u5b58\u5728<br \/>\n\u00a0 \u00a0  os.makedirs(os.path.dirname(output_zip_path), exist_ok&#061;True)<br \/>\n\u00a0 \u00a0 \u00a0<br \/>\n\u00a0 \u00a0  zip_folder(source_directory, output_zip_path)<br \/>\n\u00a0# \u7ec8\u6b62\u8bad\u7ec3<br \/>\n\u00a0# !pkill -9 -f train.py <\/p>\n<h2 style=\"text-align:center\"><span style=\"color:#4da8ee\">\u6587\u7ae0\u603b\u7ed3<\/span><\/h2>\n<hr \/>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528 Kaggel \u514d\u8d39\u670d\u52a1\u5668\u8fdb\u884cYOLOv5\u6a21\u578b\u8bad\u7ec3&#xff0c;\u6574\u4e2a\u6d41\u7a0b\u540c\u6837\u9002\u7528\u4e8e\u5176\u4ed6\u8bad\u7ec3\u6846\u67b6\u3002\u672c\u6587\u8fd8\u63d0\u4f9b\u4e86YOLOv5\u6539\u8fdb\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p>\u611f\u8c22\u9605\u89c8&#xff0c;\u5982\u679c\u4f60\u559c\u6b22\u8be5\u5185\u5bb9\u7684\u8bdd&#xff0c;\u53ef\u4ee5\u70b9\u8d5e&#xff0c;\u6536\u85cf&#xff0c;\u8f6c\u53d1\u3002\u7531\u4e8e Koro \u80fd\u529b\u6709\u9650&#xff0c;\u6709\u4efb\u4f55\u95ee\u9898\u8bf7\u5728\u8bc4\u8bba\u533a\u5185\u63d0\u51fa&#xff0c;Koro \u770b\u5230\u540e\u7b2c\u4e00\u65f6\u95f4\u56de\u590d\u60a8&#xff01;&#xff01;&#xff01;<\/p>\n<hr \/>\n<h5><span style=\"color:#4da8ee\">\u5176\u4ed6\u7cbe\u5f69\u5185\u5bb9&#xff1a;<\/span><\/h5>\n<\/p>\n<hr \/>\n<h5><span style=\"color:#4da8ee\">\u53c2\u8003\u5185\u5bb9&#xff1a;<\/span><\/h5>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e33\u6b21\uff0c\u6536\u85cf19\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528 Kaggel \u514d\u8d39\u670d\u52a1\u5668\u8fdb\u884cYOLOv5\u6a21\u578b\u8bad\u7ec3\uff0c\u6574\u4e2a\u6d41\u7a0b\u540c\u6837\u9002\u7528\u4e8e\u5176\u4ed6\u8bad\u7ec3\u6846\u67b6\u3002\u672c\u6587\u8fd8\u63d0\u4f9b\u4e86YOLOv5\u6539\u8fdb\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n","protected":false},"author":2,"featured_media":33566,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[156,43,44],"topic":[],"class_list":["post-33577","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-yolo","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/33577.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e33\u6b21\uff0c\u6536\u85cf19\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528 Kaggel \u514d\u8d39\u670d\u52a1\u5668\u8fdb\u884cYOLOv5\u6a21\u578b\u8bad\u7ec3\uff0c\u6574\u4e2a\u6d41\u7a0b\u540c\u6837\u9002\u7528\u4e8e\u5176\u4ed6\u8bad\u7ec3\u6846\u67b6\u3002\u672c\u6587\u8fd8\u63d0\u4f9b\u4e86YOLOv5\u6539\u8fdb\u4ee3\u7801\u793a\u4f8b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/33577.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-27T23:55:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235529-680ec3f1364f5.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"55 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/33577.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/33577.html\",\"name\":\"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-27T23:55:35+00:00\",\"dateModified\":\"2025-04-27T23:55:35+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/33577.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/33577.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/33577.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/33577.html","og_locale":"zh_CN","og_type":"article","og_title":"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e33\u6b21\uff0c\u6536\u85cf19\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528 Kaggel \u514d\u8d39\u670d\u52a1\u5668\u8fdb\u884cYOLOv5\u6a21\u578b\u8bad\u7ec3\uff0c\u6574\u4e2a\u6d41\u7a0b\u540c\u6837\u9002\u7528\u4e8e\u5176\u4ed6\u8bad\u7ec3\u6846\u67b6\u3002\u672c\u6587\u8fd8\u63d0\u4f9b\u4e86YOLOv5\u6539\u8fdb\u4ee3\u7801\u793a\u4f8b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/33577.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-27T23:55:35+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250427235529-680ec3f1364f5.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"55 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/33577.html","url":"https:\/\/www.wsisp.com\/helps\/33577.html","name":"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-27T23:55:35+00:00","dateModified":"2025-04-27T23:55:35+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/33577.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/33577.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/33577.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[Kaggle]\uff1a\u4f7f\u7528Kaggle\u670d\u52a1\u5668\u8bad\u7ec3YOLOv5\u6a21\u578b \uff08\u767d\u5ad6\u670d\u52a1\u5668\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/33577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=33577"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/33577\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/33566"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=33577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=33577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=33577"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=33577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}