Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1089 lignes
55KB

  1. {
  2. "nbformat": 4,
  3. "nbformat_minor": 0,
  4. "metadata": {
  5. "colab": {
  6. "name": "YOLOv5 Tutorial",
  7. "provenance": [],
  8. "collapsed_sections": [],
  9. "include_colab_link": true
  10. },
  11. "kernelspec": {
  12. "name": "python3",
  13. "display_name": "Python 3"
  14. },
  15. "accelerator": "GPU",
  16. "widgets": {
  17. "application/vnd.jupyter.widget-state+json": {
  18. "eb95db7cae194218b3fcefb439b6352f": {
  19. "model_module": "@jupyter-widgets/controls",
  20. "model_name": "HBoxModel",
  21. "model_module_version": "1.5.0",
  22. "state": {
  23. "_view_name": "HBoxView",
  24. "_dom_classes": [],
  25. "_model_name": "HBoxModel",
  26. "_view_module": "@jupyter-widgets/controls",
  27. "_model_module_version": "1.5.0",
  28. "_view_count": null,
  29. "_view_module_version": "1.5.0",
  30. "box_style": "",
  31. "layout": "IPY_MODEL_769ecde6f2e64bacb596ce972f8d3d2d",
  32. "_model_module": "@jupyter-widgets/controls",
  33. "children": [
  34. "IPY_MODEL_384a001876054c93b0af45cd1e960bfe",
  35. "IPY_MODEL_dded0aeae74440f7ba2ffa0beb8dd612",
  36. "IPY_MODEL_5296d28be75740b2892ae421bbec3657"
  37. ]
  38. }
  39. },
  40. "769ecde6f2e64bacb596ce972f8d3d2d": {
  41. "model_module": "@jupyter-widgets/base",
  42. "model_name": "LayoutModel",
  43. "model_module_version": "1.2.0",
  44. "state": {
  45. "_view_name": "LayoutView",
  46. "grid_template_rows": null,
  47. "right": null,
  48. "justify_content": null,
  49. "_view_module": "@jupyter-widgets/base",
  50. "overflow": null,
  51. "_model_module_version": "1.2.0",
  52. "_view_count": null,
  53. "flex_flow": null,
  54. "width": null,
  55. "min_width": null,
  56. "border": null,
  57. "align_items": null,
  58. "bottom": null,
  59. "_model_module": "@jupyter-widgets/base",
  60. "top": null,
  61. "grid_column": null,
  62. "overflow_y": null,
  63. "overflow_x": null,
  64. "grid_auto_flow": null,
  65. "grid_area": null,
  66. "grid_template_columns": null,
  67. "flex": null,
  68. "_model_name": "LayoutModel",
  69. "justify_items": null,
  70. "grid_row": null,
  71. "max_height": null,
  72. "align_content": null,
  73. "visibility": null,
  74. "align_self": null,
  75. "height": null,
  76. "min_height": null,
  77. "padding": null,
  78. "grid_auto_rows": null,
  79. "grid_gap": null,
  80. "max_width": null,
  81. "order": null,
  82. "_view_module_version": "1.2.0",
  83. "grid_template_areas": null,
  84. "object_position": null,
  85. "object_fit": null,
  86. "grid_auto_columns": null,
  87. "margin": null,
  88. "display": null,
  89. "left": null
  90. }
  91. },
  92. "384a001876054c93b0af45cd1e960bfe": {
  93. "model_module": "@jupyter-widgets/controls",
  94. "model_name": "HTMLModel",
  95. "model_module_version": "1.5.0",
  96. "state": {
  97. "_view_name": "HTMLView",
  98. "style": "IPY_MODEL_9f09facb2a6c4a7096810d327c8b551c",
  99. "_dom_classes": [],
  100. "description": "",
  101. "_model_name": "HTMLModel",
  102. "placeholder": "​",
  103. "_view_module": "@jupyter-widgets/controls",
  104. "_model_module_version": "1.5.0",
  105. "value": "100%",
  106. "_view_count": null,
  107. "_view_module_version": "1.5.0",
  108. "description_tooltip": null,
  109. "_model_module": "@jupyter-widgets/controls",
  110. "layout": "IPY_MODEL_25621cff5d16448cb7260e839fd0f543"
  111. }
  112. },
  113. "dded0aeae74440f7ba2ffa0beb8dd612": {
  114. "model_module": "@jupyter-widgets/controls",
  115. "model_name": "FloatProgressModel",
  116. "model_module_version": "1.5.0",
  117. "state": {
  118. "_view_name": "ProgressView",
  119. "style": "IPY_MODEL_0ce7164fc0c74bb9a2b5c7037375a727",
  120. "_dom_classes": [],
  121. "description": "",
  122. "_model_name": "FloatProgressModel",
  123. "bar_style": "success",
  124. "max": 818322941,
  125. "_view_module": "@jupyter-widgets/controls",
  126. "_model_module_version": "1.5.0",
  127. "value": 818322941,
  128. "_view_count": null,
  129. "_view_module_version": "1.5.0",
  130. "orientation": "horizontal",
  131. "min": 0,
  132. "description_tooltip": null,
  133. "_model_module": "@jupyter-widgets/controls",
  134. "layout": "IPY_MODEL_c4c4593c10904cb5b8a5724d60c7e181"
  135. }
  136. },
  137. "5296d28be75740b2892ae421bbec3657": {
  138. "model_module": "@jupyter-widgets/controls",
  139. "model_name": "HTMLModel",
  140. "model_module_version": "1.5.0",
  141. "state": {
  142. "_view_name": "HTMLView",
  143. "style": "IPY_MODEL_473371611126476c88d5d42ec7031ed6",
  144. "_dom_classes": [],
  145. "description": "",
  146. "_model_name": "HTMLModel",
  147. "placeholder": "​",
  148. "_view_module": "@jupyter-widgets/controls",
  149. "_model_module_version": "1.5.0",
  150. "value": " 780M/780M [00:11<00:00, 91.9MB/s]",
  151. "_view_count": null,
  152. "_view_module_version": "1.5.0",
  153. "description_tooltip": null,
  154. "_model_module": "@jupyter-widgets/controls",
  155. "layout": "IPY_MODEL_65efdfd0d26c46e79c8c5ff3b77126cc"
  156. }
  157. },
  158. "9f09facb2a6c4a7096810d327c8b551c": {
  159. "model_module": "@jupyter-widgets/controls",
  160. "model_name": "DescriptionStyleModel",
  161. "model_module_version": "1.5.0",
  162. "state": {
  163. "_view_name": "StyleView",
  164. "_model_name": "DescriptionStyleModel",
  165. "description_width": "",
  166. "_view_module": "@jupyter-widgets/base",
  167. "_model_module_version": "1.5.0",
  168. "_view_count": null,
  169. "_view_module_version": "1.2.0",
  170. "_model_module": "@jupyter-widgets/controls"
  171. }
  172. },
  173. "25621cff5d16448cb7260e839fd0f543": {
  174. "model_module": "@jupyter-widgets/base",
  175. "model_name": "LayoutModel",
  176. "model_module_version": "1.2.0",
  177. "state": {
  178. "_view_name": "LayoutView",
  179. "grid_template_rows": null,
  180. "right": null,
  181. "justify_content": null,
  182. "_view_module": "@jupyter-widgets/base",
  183. "overflow": null,
  184. "_model_module_version": "1.2.0",
  185. "_view_count": null,
  186. "flex_flow": null,
  187. "width": null,
  188. "min_width": null,
  189. "border": null,
  190. "align_items": null,
  191. "bottom": null,
  192. "_model_module": "@jupyter-widgets/base",
  193. "top": null,
  194. "grid_column": null,
  195. "overflow_y": null,
  196. "overflow_x": null,
  197. "grid_auto_flow": null,
  198. "grid_area": null,
  199. "grid_template_columns": null,
  200. "flex": null,
  201. "_model_name": "LayoutModel",
  202. "justify_items": null,
  203. "grid_row": null,
  204. "max_height": null,
  205. "align_content": null,
  206. "visibility": null,
  207. "align_self": null,
  208. "height": null,
  209. "min_height": null,
  210. "padding": null,
  211. "grid_auto_rows": null,
  212. "grid_gap": null,
  213. "max_width": null,
  214. "order": null,
  215. "_view_module_version": "1.2.0",
  216. "grid_template_areas": null,
  217. "object_position": null,
  218. "object_fit": null,
  219. "grid_auto_columns": null,
  220. "margin": null,
  221. "display": null,
  222. "left": null
  223. }
  224. },
  225. "0ce7164fc0c74bb9a2b5c7037375a727": {
  226. "model_module": "@jupyter-widgets/controls",
  227. "model_name": "ProgressStyleModel",
  228. "model_module_version": "1.5.0",
  229. "state": {
  230. "_view_name": "StyleView",
  231. "_model_name": "ProgressStyleModel",
  232. "description_width": "",
  233. "_view_module": "@jupyter-widgets/base",
  234. "_model_module_version": "1.5.0",
  235. "_view_count": null,
  236. "_view_module_version": "1.2.0",
  237. "bar_color": null,
  238. "_model_module": "@jupyter-widgets/controls"
  239. }
  240. },
  241. "c4c4593c10904cb5b8a5724d60c7e181": {
  242. "model_module": "@jupyter-widgets/base",
  243. "model_name": "LayoutModel",
  244. "model_module_version": "1.2.0",
  245. "state": {
  246. "_view_name": "LayoutView",
  247. "grid_template_rows": null,
  248. "right": null,
  249. "justify_content": null,
  250. "_view_module": "@jupyter-widgets/base",
  251. "overflow": null,
  252. "_model_module_version": "1.2.0",
  253. "_view_count": null,
  254. "flex_flow": null,
  255. "width": null,
  256. "min_width": null,
  257. "border": null,
  258. "align_items": null,
  259. "bottom": null,
  260. "_model_module": "@jupyter-widgets/base",
  261. "top": null,
  262. "grid_column": null,
  263. "overflow_y": null,
  264. "overflow_x": null,
  265. "grid_auto_flow": null,
  266. "grid_area": null,
  267. "grid_template_columns": null,
  268. "flex": null,
  269. "_model_name": "LayoutModel",
  270. "justify_items": null,
  271. "grid_row": null,
  272. "max_height": null,
  273. "align_content": null,
  274. "visibility": null,
  275. "align_self": null,
  276. "height": null,
  277. "min_height": null,
  278. "padding": null,
  279. "grid_auto_rows": null,
  280. "grid_gap": null,
  281. "max_width": null,
  282. "order": null,
  283. "_view_module_version": "1.2.0",
  284. "grid_template_areas": null,
  285. "object_position": null,
  286. "object_fit": null,
  287. "grid_auto_columns": null,
  288. "margin": null,
  289. "display": null,
  290. "left": null
  291. }
  292. },
  293. "473371611126476c88d5d42ec7031ed6": {
  294. "model_module": "@jupyter-widgets/controls",
  295. "model_name": "DescriptionStyleModel",
  296. "model_module_version": "1.5.0",
  297. "state": {
  298. "_view_name": "StyleView",
  299. "_model_name": "DescriptionStyleModel",
  300. "description_width": "",
  301. "_view_module": "@jupyter-widgets/base",
  302. "_model_module_version": "1.5.0",
  303. "_view_count": null,
  304. "_view_module_version": "1.2.0",
  305. "_model_module": "@jupyter-widgets/controls"
  306. }
  307. },
  308. "65efdfd0d26c46e79c8c5ff3b77126cc": {
  309. "model_module": "@jupyter-widgets/base",
  310. "model_name": "LayoutModel",
  311. "model_module_version": "1.2.0",
  312. "state": {
  313. "_view_name": "LayoutView",
  314. "grid_template_rows": null,
  315. "right": null,
  316. "justify_content": null,
  317. "_view_module": "@jupyter-widgets/base",
  318. "overflow": null,
  319. "_model_module_version": "1.2.0",
  320. "_view_count": null,
  321. "flex_flow": null,
  322. "width": null,
  323. "min_width": null,
  324. "border": null,
  325. "align_items": null,
  326. "bottom": null,
  327. "_model_module": "@jupyter-widgets/base",
  328. "top": null,
  329. "grid_column": null,
  330. "overflow_y": null,
  331. "overflow_x": null,
  332. "grid_auto_flow": null,
  333. "grid_area": null,
  334. "grid_template_columns": null,
  335. "flex": null,
  336. "_model_name": "LayoutModel",
  337. "justify_items": null,
  338. "grid_row": null,
  339. "max_height": null,
  340. "align_content": null,
  341. "visibility": null,
  342. "align_self": null,
  343. "height": null,
  344. "min_height": null,
  345. "padding": null,
  346. "grid_auto_rows": null,
  347. "grid_gap": null,
  348. "max_width": null,
  349. "order": null,
  350. "_view_module_version": "1.2.0",
  351. "grid_template_areas": null,
  352. "object_position": null,
  353. "object_fit": null,
  354. "grid_auto_columns": null,
  355. "margin": null,
  356. "display": null,
  357. "left": null
  358. }
  359. }
  360. }
  361. }
  362. },
  363. "cells": [
  364. {
  365. "cell_type": "markdown",
  366. "metadata": {
  367. "id": "view-in-github",
  368. "colab_type": "text"
  369. },
  370. "source": [
  371. "<a href=\"https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
  372. ]
  373. },
  374. {
  375. "cell_type": "markdown",
  376. "metadata": {
  377. "id": "t6MPjfT5NrKQ"
  378. },
  379. "source": [
  380. "<a align=\"left\" href=\"https://ultralytics.com/yolov5\" target=\"_blank\">\n",
  381. "<img width=\"1024\", src=\"https://user-images.githubusercontent.com/26833433/125273437-35b3fc00-e30d-11eb-9079-46f313325424.png\"></a>\n",
  382. "\n",
  383. "This is the **official YOLOv5 🚀 notebook** by **Ultralytics**, and is freely available for redistribution under the [GPL-3.0 license](https://choosealicense.com/licenses/gpl-3.0/). \n",
  384. "For more information please visit https://github.com/ultralytics/yolov5 and https://ultralytics.com. Thank you!"
  385. ]
  386. },
  387. {
  388. "cell_type": "markdown",
  389. "metadata": {
  390. "id": "7mGmQbAO5pQb"
  391. },
  392. "source": [
  393. "# Setup\n",
  394. "\n",
  395. "Clone repo, install dependencies and check PyTorch and GPU."
  396. ]
  397. },
  398. {
  399. "cell_type": "code",
  400. "metadata": {
  401. "id": "wbvMlHd_QwMG",
  402. "colab": {
  403. "base_uri": "https://localhost:8080/"
  404. },
  405. "outputId": "e2e839d5-d6fc-409c-e44c-0b0b6aa9319d"
  406. },
  407. "source": [
  408. "!git clone https://github.com/ultralytics/yolov5 # clone repo\n",
  409. "%cd yolov5\n",
  410. "%pip install -qr requirements.txt # install dependencies\n",
  411. "\n",
  412. "import torch\n",
  413. "from IPython.display import Image, clear_output # to display images\n",
  414. "\n",
  415. "clear_output()\n",
  416. "print(f\"Setup complete. Using torch {torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})\")"
  417. ],
  418. "execution_count": 11,
  419. "outputs": [
  420. {
  421. "output_type": "stream",
  422. "name": "stdout",
  423. "text": [
  424. "Setup complete. Using torch 1.10.0+cu102 (Tesla V100-SXM2-16GB)\n"
  425. ]
  426. }
  427. ]
  428. },
  429. {
  430. "cell_type": "markdown",
  431. "metadata": {
  432. "id": "4JnkELT0cIJg"
  433. },
  434. "source": [
  435. "# 1. Inference\n",
  436. "\n",
  437. "`detect.py` runs YOLOv5 inference on a variety of sources, downloading models automatically from the [latest YOLOv5 release](https://github.com/ultralytics/yolov5/releases), and saving results to `runs/detect`. Example inference sources are:\n",
  438. "\n",
  439. "```shell\n",
  440. "python detect.py --source 0 # webcam\n",
  441. " file.jpg # image \n",
  442. " file.mp4 # video\n",
  443. " path/ # directory\n",
  444. " path/*.jpg # glob\n",
  445. " 'https://youtu.be/NUsoVlDFqZg' # YouTube\n",
  446. " 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream\n",
  447. "```"
  448. ]
  449. },
  450. {
  451. "cell_type": "code",
  452. "metadata": {
  453. "id": "zR9ZbuQCH7FX",
  454. "colab": {
  455. "base_uri": "https://localhost:8080/"
  456. },
  457. "outputId": "8f7e6588-215d-4ebd-93af-88b871e770a7"
  458. },
  459. "source": [
  460. "!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images\n",
  461. "Image(filename='runs/detect/exp/zidane.jpg', width=600)"
  462. ],
  463. "execution_count": 17,
  464. "outputs": [
  465. {
  466. "output_type": "stream",
  467. "name": "stdout",
  468. "text": [
  469. "\u001b[34m\u001b[1mdetect: \u001b[0mweights=['yolov5s.pt'], source=data/images, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False\n",
  470. "YOLOv5 🚀 v6.0-48-g84a8099 torch 1.10.0+cu102 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)\n",
  471. "\n",
  472. "Fusing layers... \n",
  473. "Model Summary: 213 layers, 7225885 parameters, 0 gradients\n",
  474. "image 1/2 /content/yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, Done. (0.007s)\n",
  475. "image 2/2 /content/yolov5/data/images/zidane.jpg: 384x640 2 persons, 1 tie, Done. (0.007s)\n",
  476. "Speed: 0.5ms pre-process, 6.9ms inference, 1.3ms NMS per image at shape (1, 3, 640, 640)\n",
  477. "Results saved to \u001b[1mruns/detect/exp\u001b[0m\n"
  478. ]
  479. }
  480. ]
  481. },
  482. {
  483. "cell_type": "markdown",
  484. "metadata": {
  485. "id": "hkAzDWJ7cWTr"
  486. },
  487. "source": [
  488. "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n",
  489. "<img align=\"left\" src=\"https://user-images.githubusercontent.com/26833433/127574988-6a558aa1-d268-44b9-bf6b-62d4c605cc72.jpg\" width=\"600\">"
  490. ]
  491. },
  492. {
  493. "cell_type": "markdown",
  494. "metadata": {
  495. "id": "0eq1SMWl6Sfn"
  496. },
  497. "source": [
  498. "# 2. Validate\n",
  499. "Validate a model's accuracy on [COCO](https://cocodataset.org/#home) val or test-dev datasets. Models are downloaded automatically from the [latest YOLOv5 release](https://github.com/ultralytics/yolov5/releases). To show results by class use the `--verbose` flag. Note that `pycocotools` metrics may be ~1% better than the equivalent repo metrics, as is visible below, due to slight differences in mAP computation."
  500. ]
  501. },
  502. {
  503. "cell_type": "markdown",
  504. "metadata": {
  505. "id": "eyTZYGgRjnMc"
  506. },
  507. "source": [
  508. "## COCO val\n",
  509. "Download [COCO val 2017](https://github.com/ultralytics/yolov5/blob/74b34872fdf41941cddcf243951cdb090fbac17b/data/coco.yaml#L14) dataset (1GB - 5000 images), and test model accuracy."
  510. ]
  511. },
  512. {
  513. "cell_type": "code",
  514. "metadata": {
  515. "id": "WQPtK1QYVaD_",
  516. "colab": {
  517. "base_uri": "https://localhost:8080/",
  518. "height": 48,
  519. "referenced_widgets": [
  520. "eb95db7cae194218b3fcefb439b6352f",
  521. "769ecde6f2e64bacb596ce972f8d3d2d",
  522. "384a001876054c93b0af45cd1e960bfe",
  523. "dded0aeae74440f7ba2ffa0beb8dd612",
  524. "5296d28be75740b2892ae421bbec3657",
  525. "9f09facb2a6c4a7096810d327c8b551c",
  526. "25621cff5d16448cb7260e839fd0f543",
  527. "0ce7164fc0c74bb9a2b5c7037375a727",
  528. "c4c4593c10904cb5b8a5724d60c7e181",
  529. "473371611126476c88d5d42ec7031ed6",
  530. "65efdfd0d26c46e79c8c5ff3b77126cc"
  531. ]
  532. },
  533. "outputId": "bcf9a448-1f9b-4a41-ad49-12f181faf05a"
  534. },
  535. "source": [
  536. "# Download COCO val\n",
  537. "torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017val.zip', 'tmp.zip')\n",
  538. "!unzip -q tmp.zip -d ../datasets && rm tmp.zip"
  539. ],
  540. "execution_count": 18,
  541. "outputs": [
  542. {
  543. "output_type": "display_data",
  544. "data": {
  545. "application/vnd.jupyter.widget-view+json": {
  546. "model_id": "eb95db7cae194218b3fcefb439b6352f",
  547. "version_minor": 0,
  548. "version_major": 2
  549. },
  550. "text/plain": [
  551. " 0%| | 0.00/780M [00:00<?, ?B/s]"
  552. ]
  553. },
  554. "metadata": {}
  555. }
  556. ]
  557. },
  558. {
  559. "cell_type": "code",
  560. "metadata": {
  561. "id": "X58w8JLpMnjH",
  562. "colab": {
  563. "base_uri": "https://localhost:8080/"
  564. },
  565. "outputId": "74f1dfa9-6b6d-4b36-f67e-bbae243869f9"
  566. },
  567. "source": [
  568. "# Run YOLOv5x on COCO val\n",
  569. "!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half"
  570. ],
  571. "execution_count": 19,
  572. "outputs": [
  573. {
  574. "output_type": "stream",
  575. "name": "stdout",
  576. "text": [
  577. "\u001b[34m\u001b[1mval: \u001b[0mdata=/content/yolov5/data/coco.yaml, weights=['yolov5x.pt'], batch_size=32, imgsz=640, conf_thres=0.001, iou_thres=0.65, task=val, device=, single_cls=False, augment=False, verbose=False, save_txt=False, save_hybrid=False, save_conf=False, save_json=True, project=runs/val, name=exp, exist_ok=False, half=True\n",
  578. "YOLOv5 🚀 v6.0-48-g84a8099 torch 1.10.0+cu102 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)\n",
  579. "\n",
  580. "Downloading https://github.com/ultralytics/yolov5/releases/download/v6.0/yolov5x.pt to yolov5x.pt...\n",
  581. "100% 166M/166M [00:03<00:00, 54.1MB/s]\n",
  582. "\n",
  583. "Fusing layers... \n",
  584. "Model Summary: 444 layers, 86705005 parameters, 0 gradients\n",
  585. "\u001b[34m\u001b[1mval: \u001b[0mScanning '../datasets/coco/val2017' images and labels...4952 found, 48 missing, 0 empty, 0 corrupted: 100% 5000/5000 [00:01<00:00, 2636.64it/s]\n",
  586. "\u001b[34m\u001b[1mval: \u001b[0mNew cache created: ../datasets/coco/val2017.cache\n",
  587. " Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 157/157 [01:12<00:00, 2.17it/s]\n",
  588. " all 5000 36335 0.729 0.63 0.683 0.496\n",
  589. "Speed: 0.1ms pre-process, 4.9ms inference, 1.9ms NMS per image at shape (32, 3, 640, 640)\n",
  590. "\n",
  591. "Evaluating pycocotools mAP... saving runs/val/exp/yolov5x_predictions.json...\n",
  592. "loading annotations into memory...\n",
  593. "Done (t=0.46s)\n",
  594. "creating index...\n",
  595. "index created!\n",
  596. "Loading and preparing results...\n",
  597. "DONE (t=5.15s)\n",
  598. "creating index...\n",
  599. "index created!\n",
  600. "Running per image evaluation...\n",
  601. "Evaluate annotation type *bbox*\n",
  602. "DONE (t=90.39s).\n",
  603. "Accumulating evaluation results...\n",
  604. "DONE (t=14.54s).\n",
  605. " Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.507\n",
  606. " Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.689\n",
  607. " Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.552\n",
  608. " Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.345\n",
  609. " Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.559\n",
  610. " Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.652\n",
  611. " Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.381\n",
  612. " Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.630\n",
  613. " Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.682\n",
  614. " Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.526\n",
  615. " Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.732\n",
  616. " Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.829\n",
  617. "Results saved to \u001b[1mruns/val/exp\u001b[0m\n"
  618. ]
  619. }
  620. ]
  621. },
  622. {
  623. "cell_type": "markdown",
  624. "metadata": {
  625. "id": "rc_KbFk0juX2"
  626. },
  627. "source": [
  628. "## COCO test\n",
  629. "Download [COCO test2017](https://github.com/ultralytics/yolov5/blob/74b34872fdf41941cddcf243951cdb090fbac17b/data/coco.yaml#L15) dataset (7GB - 40,000 images), to test model accuracy on test-dev set (**20,000 images, no labels**). Results are saved to a `*.json` file which should be **zipped** and submitted to the evaluation server at https://competitions.codalab.org/competitions/20794."
  630. ]
  631. },
  632. {
  633. "cell_type": "code",
  634. "metadata": {
  635. "id": "V0AJnSeCIHyJ"
  636. },
  637. "source": [
  638. "# Download COCO test-dev2017\n",
  639. "torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017labels.zip', 'tmp.zip')\n",
  640. "!unzip -q tmp.zip -d ../datasets && rm tmp.zip\n",
  641. "!f=\"test2017.zip\" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f -d ../datasets/coco/images"
  642. ],
  643. "execution_count": null,
  644. "outputs": []
  645. },
  646. {
  647. "cell_type": "code",
  648. "metadata": {
  649. "id": "29GJXAP_lPrt"
  650. },
  651. "source": [
  652. "# Run YOLOv5x on COCO test\n",
  653. "!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half --task test"
  654. ],
  655. "execution_count": null,
  656. "outputs": []
  657. },
  658. {
  659. "cell_type": "markdown",
  660. "metadata": {
  661. "id": "ZY2VXXXu74w5"
  662. },
  663. "source": [
  664. "# 3. Train\n",
  665. "\n",
  666. "<p align=\"\"><a href=\"https://roboflow.com/?ref=ultralytics\"><img width=\"1000\" src=\"https://uploads-ssl.webflow.com/5f6bc60e665f54545a1e52a5/615627e5824c9c6195abfda9_computer-vision-cycle.png\"/></a></p>\n",
  667. "Close the active learning loop by sampling images from your inference conditions with the `roboflow` pip package\n",
  668. "<br><br>\n",
  669. "\n",
  670. "Train a YOLOv5s model on the [COCO128](https://www.kaggle.com/ultralytics/coco128) dataset with `--data coco128.yaml`, starting from pretrained `--weights yolov5s.pt`, or from randomly initialized `--weights '' --cfg yolov5s.yaml`.\n",
  671. "\n",
  672. "- **Pretrained [Models](https://github.com/ultralytics/yolov5/tree/master/models)** are downloaded\n",
  673. "automatically from the [latest YOLOv5 release](https://github.com/ultralytics/yolov5/releases)\n",
  674. "- **[Datasets](https://github.com/ultralytics/yolov5/tree/master/data)** available for autodownload include: [COCO](https://github.com/ultralytics/yolov5/blob/master/data/coco.yaml), [COCO128](https://github.com/ultralytics/yolov5/blob/master/data/coco128.yaml), [VOC](https://github.com/ultralytics/yolov5/blob/master/data/VOC.yaml), [Argoverse](https://github.com/ultralytics/yolov5/blob/master/data/Argoverse.yaml), [VisDrone](https://github.com/ultralytics/yolov5/blob/master/data/VisDrone.yaml), [GlobalWheat](https://github.com/ultralytics/yolov5/blob/master/data/GlobalWheat2020.yaml), [xView](https://github.com/ultralytics/yolov5/blob/master/data/xView.yaml), [Objects365](https://github.com/ultralytics/yolov5/blob/master/data/Objects365.yaml), [SKU-110K](https://github.com/ultralytics/yolov5/blob/master/data/SKU-110K.yaml).\n",
  675. "- **Training Results** are saved to `runs/train/` with incrementing run directories, i.e. `runs/train/exp2`, `runs/train/exp3` etc.\n",
  676. "<br><br>\n",
  677. "\n",
  678. "## Train on Custom Data with Roboflow 🌟 NEW\n",
  679. "\n",
  680. "[Roboflow](https://roboflow.com/?ref=ultralytics) enables you to easily **organize, label, and prepare** a high quality dataset with your own custom data. Roboflow also makes it easy to establish an active learning pipeline, collaborate with your team on dataset improvement, and integrate directly into your model building workflow with the `roboflow` pip package.\n",
  681. "\n",
  682. "- Custom Training Example: [https://blog.roboflow.com/how-to-train-yolov5-on-a-custom-dataset/](https://blog.roboflow.com/how-to-train-yolov5-on-a-custom-dataset/?ref=ultralytics)\n",
  683. "- Custom Training Notebook: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow-ai/yolov5-custom-training-tutorial/blob/main/yolov5-custom-training.ipynb)\n",
  684. "<br>\n",
  685. "\n",
  686. "<p align=\"\"><a href=\"https://roboflow.com/?ref=ultralytics\"><img width=\"480\" src=\"https://uploads-ssl.webflow.com/5f6bc60e665f54545a1e52a5/6152a275ad4b4ac20cd2e21a_roboflow-annotate.gif\"/></a></p>Label images lightning fast (including with model-assisted labeling)"
  687. ]
  688. },
  689. {
  690. "cell_type": "code",
  691. "metadata": {
  692. "id": "bOy5KI2ncnWd"
  693. },
  694. "source": [
  695. "# Tensorboard (optional)\n",
  696. "%load_ext tensorboard\n",
  697. "%tensorboard --logdir runs/train"
  698. ],
  699. "execution_count": null,
  700. "outputs": []
  701. },
  702. {
  703. "cell_type": "code",
  704. "metadata": {
  705. "id": "2fLAV42oNb7M"
  706. },
  707. "source": [
  708. "# Weights & Biases (optional)\n",
  709. "%pip install -q wandb\n",
  710. "import wandb\n",
  711. "wandb.login()"
  712. ],
  713. "execution_count": null,
  714. "outputs": []
  715. },
  716. {
  717. "cell_type": "code",
  718. "metadata": {
  719. "id": "1NcFxRcFdJ_O",
  720. "colab": {
  721. "base_uri": "https://localhost:8080/"
  722. },
  723. "outputId": "8724d13d-6711-4a12-d96a-1c655e5c3549"
  724. },
  725. "source": [
  726. "# Train YOLOv5s on COCO128 for 3 epochs\n",
  727. "!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache"
  728. ],
  729. "execution_count": 24,
  730. "outputs": [
  731. {
  732. "output_type": "stream",
  733. "name": "stdout",
  734. "text": [
  735. "\u001b[34m\u001b[1mtrain: \u001b[0mweights=yolov5s.pt, cfg=, data=coco128.yaml, hyp=data/hyps/hyp.scratch.yaml, epochs=3, batch_size=16, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, evolve=None, bucket=, cache=ram, image_weights=False, device=, multi_scale=False, single_cls=False, adam=False, sync_bn=False, workers=8, project=runs/train, name=exp, exist_ok=False, quad=False, linear_lr=False, label_smoothing=0.0, patience=100, freeze=0, save_period=-1, local_rank=-1, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest\n",
  736. "\u001b[34m\u001b[1mgithub: \u001b[0mup to date with https://github.com/ultralytics/yolov5 ✅\n",
  737. "YOLOv5 🚀 v6.0-48-g84a8099 torch 1.10.0+cu102 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)\n",
  738. "\n",
  739. "\u001b[34m\u001b[1mhyperparameters: \u001b[0mlr0=0.01, lrf=0.1, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0\n",
  740. "\u001b[34m\u001b[1mWeights & Biases: \u001b[0mrun 'pip install wandb' to automatically track and visualize YOLOv5 🚀 runs (RECOMMENDED)\n",
  741. "\u001b[34m\u001b[1mTensorBoard: \u001b[0mStart with 'tensorboard --logdir runs/train', view at http://localhost:6006/\n",
  742. "\n",
  743. " from n params module arguments \n",
  744. " 0 -1 1 3520 models.common.Conv [3, 32, 6, 2, 2] \n",
  745. " 1 -1 1 18560 models.common.Conv [32, 64, 3, 2] \n",
  746. " 2 -1 1 18816 models.common.C3 [64, 64, 1] \n",
  747. " 3 -1 1 73984 models.common.Conv [64, 128, 3, 2] \n",
  748. " 4 -1 2 115712 models.common.C3 [128, 128, 2] \n",
  749. " 5 -1 1 295424 models.common.Conv [128, 256, 3, 2] \n",
  750. " 6 -1 3 625152 models.common.C3 [256, 256, 3] \n",
  751. " 7 -1 1 1180672 models.common.Conv [256, 512, 3, 2] \n",
  752. " 8 -1 1 1182720 models.common.C3 [512, 512, 1] \n",
  753. " 9 -1 1 656896 models.common.SPPF [512, 512, 5] \n",
  754. " 10 -1 1 131584 models.common.Conv [512, 256, 1, 1] \n",
  755. " 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
  756. " 12 [-1, 6] 1 0 models.common.Concat [1] \n",
  757. " 13 -1 1 361984 models.common.C3 [512, 256, 1, False] \n",
  758. " 14 -1 1 33024 models.common.Conv [256, 128, 1, 1] \n",
  759. " 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
  760. " 16 [-1, 4] 1 0 models.common.Concat [1] \n",
  761. " 17 -1 1 90880 models.common.C3 [256, 128, 1, False] \n",
  762. " 18 -1 1 147712 models.common.Conv [128, 128, 3, 2] \n",
  763. " 19 [-1, 14] 1 0 models.common.Concat [1] \n",
  764. " 20 -1 1 296448 models.common.C3 [256, 256, 1, False] \n",
  765. " 21 -1 1 590336 models.common.Conv [256, 256, 3, 2] \n",
  766. " 22 [-1, 10] 1 0 models.common.Concat [1] \n",
  767. " 23 -1 1 1182720 models.common.C3 [512, 512, 1, False] \n",
  768. " 24 [17, 20, 23] 1 229245 models.yolo.Detect [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]\n",
  769. "Model Summary: 270 layers, 7235389 parameters, 7235389 gradients, 16.5 GFLOPs\n",
  770. "\n",
  771. "Transferred 349/349 items from yolov5s.pt\n",
  772. "Scaled weight_decay = 0.0005\n",
  773. "\u001b[34m\u001b[1moptimizer:\u001b[0m SGD with parameter groups 57 weight, 60 weight (no decay), 60 bias\n",
  774. "\u001b[34m\u001b[1malbumentations: \u001b[0mversion 1.0.3 required by YOLOv5, but version 0.1.12 is currently installed\n",
  775. "\u001b[34m\u001b[1mtrain: \u001b[0mScanning '../datasets/coco128/labels/train2017.cache' images and labels... 128 found, 0 missing, 2 empty, 0 corrupted: 100% 128/128 [00:00<?, ?it/s]\n",
  776. "\u001b[34m\u001b[1mtrain: \u001b[0mCaching images (0.1GB ram): 100% 128/128 [00:00<00:00, 296.04it/s]\n",
  777. "\u001b[34m\u001b[1mval: \u001b[0mScanning '../datasets/coco128/labels/train2017.cache' images and labels... 128 found, 0 missing, 2 empty, 0 corrupted: 100% 128/128 [00:00<?, ?it/s]\n",
  778. "\u001b[34m\u001b[1mval: \u001b[0mCaching images (0.1GB ram): 100% 128/128 [00:01<00:00, 121.58it/s]\n",
  779. "Plotting labels... \n",
  780. "\n",
  781. "\u001b[34m\u001b[1mautoanchor: \u001b[0mAnalyzing anchors... anchors/target = 4.27, Best Possible Recall (BPR) = 0.9935\n",
  782. "Image sizes 640 train, 640 val\n",
  783. "Using 2 dataloader workers\n",
  784. "Logging results to \u001b[1mruns/train/exp\u001b[0m\n",
  785. "Starting training for 3 epochs...\n",
  786. "\n",
  787. " Epoch gpu_mem box obj cls labels img_size\n",
  788. " 0/2 3.62G 0.04621 0.0711 0.02112 203 640: 100% 8/8 [00:04<00:00, 1.99it/s]\n",
  789. " Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:00<00:00, 4.37it/s]\n",
  790. " all 128 929 0.655 0.547 0.622 0.41\n",
  791. "\n",
  792. " Epoch gpu_mem box obj cls labels img_size\n",
  793. " 1/2 5.31G 0.04564 0.06898 0.02116 143 640: 100% 8/8 [00:01<00:00, 4.77it/s]\n",
  794. " Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:00<00:00, 4.27it/s]\n",
  795. " all 128 929 0.68 0.554 0.632 0.419\n",
  796. "\n",
  797. " Epoch gpu_mem box obj cls labels img_size\n",
  798. " 2/2 5.31G 0.04487 0.06883 0.01998 253 640: 100% 8/8 [00:01<00:00, 4.91it/s]\n",
  799. " Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:00<00:00, 4.30it/s]\n",
  800. " all 128 929 0.71 0.544 0.629 0.423\n",
  801. "\n",
  802. "3 epochs completed in 0.003 hours.\n",
  803. "Optimizer stripped from runs/train/exp/weights/last.pt, 14.9MB\n",
  804. "Optimizer stripped from runs/train/exp/weights/best.pt, 14.9MB\n",
  805. "\n",
  806. "Validating runs/train/exp/weights/best.pt...\n",
  807. "Fusing layers... \n",
  808. "Model Summary: 213 layers, 7225885 parameters, 0 gradients, 16.5 GFLOPs\n",
  809. " Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:03<00:00, 1.04it/s]\n",
  810. " all 128 929 0.71 0.544 0.63 0.423\n",
  811. " person 128 254 0.816 0.669 0.774 0.507\n",
  812. " bicycle 128 6 0.799 0.667 0.614 0.371\n",
  813. " car 128 46 0.803 0.355 0.486 0.209\n",
  814. " motorcycle 128 5 0.704 0.6 0.791 0.583\n",
  815. " airplane 128 6 1 0.795 0.995 0.717\n",
  816. " bus 128 7 0.656 0.714 0.72 0.606\n",
  817. " train 128 3 0.852 1 0.995 0.682\n",
  818. " truck 128 12 0.521 0.25 0.395 0.215\n",
  819. " boat 128 6 0.795 0.333 0.445 0.137\n",
  820. " traffic light 128 14 0.576 0.143 0.24 0.161\n",
  821. " stop sign 128 2 0.636 0.5 0.828 0.713\n",
  822. " bench 128 9 0.972 0.444 0.575 0.25\n",
  823. " bird 128 16 0.939 0.968 0.988 0.645\n",
  824. " cat 128 4 0.984 0.75 0.822 0.694\n",
  825. " dog 128 9 0.888 0.667 0.903 0.54\n",
  826. " horse 128 2 0.689 1 0.995 0.697\n",
  827. " elephant 128 17 0.96 0.882 0.943 0.681\n",
  828. " bear 128 1 0.549 1 0.995 0.995\n",
  829. " zebra 128 4 0.86 1 0.995 0.952\n",
  830. " giraffe 128 9 0.822 0.778 0.905 0.57\n",
  831. " backpack 128 6 1 0.309 0.457 0.195\n",
  832. " umbrella 128 18 0.775 0.576 0.74 0.418\n",
  833. " handbag 128 19 0.628 0.105 0.167 0.111\n",
  834. " tie 128 7 0.96 0.571 0.701 0.441\n",
  835. " suitcase 128 4 1 0.895 0.995 0.621\n",
  836. " frisbee 128 5 0.641 0.8 0.798 0.664\n",
  837. " skis 128 1 0.627 1 0.995 0.497\n",
  838. " snowboard 128 7 0.988 0.714 0.768 0.556\n",
  839. " sports ball 128 6 0.671 0.5 0.579 0.339\n",
  840. " kite 128 10 0.631 0.515 0.598 0.221\n",
  841. " baseball bat 128 4 0.47 0.456 0.277 0.137\n",
  842. " baseball glove 128 7 0.459 0.429 0.334 0.182\n",
  843. " skateboard 128 5 0.7 0.48 0.736 0.548\n",
  844. " tennis racket 128 7 0.559 0.571 0.538 0.315\n",
  845. " bottle 128 18 0.607 0.389 0.484 0.282\n",
  846. " wine glass 128 16 0.722 0.812 0.82 0.385\n",
  847. " cup 128 36 0.881 0.361 0.532 0.312\n",
  848. " fork 128 6 0.384 0.167 0.239 0.191\n",
  849. " knife 128 16 0.908 0.616 0.681 0.443\n",
  850. " spoon 128 22 0.836 0.364 0.536 0.264\n",
  851. " bowl 128 28 0.793 0.536 0.633 0.471\n",
  852. " banana 128 1 0 0 0.142 0.0995\n",
  853. " sandwich 128 2 0 0 0.0951 0.0717\n",
  854. " orange 128 4 1 0 0.67 0.317\n",
  855. " broccoli 128 11 0.345 0.182 0.283 0.243\n",
  856. " carrot 128 24 0.688 0.459 0.612 0.402\n",
  857. " hot dog 128 2 0.424 0.771 0.497 0.473\n",
  858. " pizza 128 5 0.622 1 0.824 0.551\n",
  859. " donut 128 14 0.703 1 0.952 0.853\n",
  860. " cake 128 4 0.733 1 0.945 0.777\n",
  861. " chair 128 35 0.512 0.486 0.488 0.222\n",
  862. " couch 128 6 0.68 0.36 0.746 0.406\n",
  863. " potted plant 128 14 0.797 0.714 0.808 0.482\n",
  864. " bed 128 3 1 0 0.474 0.318\n",
  865. " dining table 128 13 0.852 0.445 0.478 0.315\n",
  866. " toilet 128 2 0.512 0.5 0.554 0.487\n",
  867. " tv 128 2 0.754 1 0.995 0.895\n",
  868. " laptop 128 3 1 0 0.39 0.147\n",
  869. " mouse 128 2 1 0 0.0283 0.0226\n",
  870. " remote 128 8 0.747 0.625 0.636 0.488\n",
  871. " cell phone 128 8 0.555 0.166 0.417 0.222\n",
  872. " microwave 128 3 0.417 1 0.995 0.732\n",
  873. " oven 128 5 0.37 0.4 0.432 0.249\n",
  874. " sink 128 6 0.356 0.167 0.269 0.149\n",
  875. " refrigerator 128 5 0.705 0.8 0.814 0.45\n",
  876. " book 128 29 0.628 0.138 0.298 0.136\n",
  877. " clock 128 9 0.857 0.778 0.893 0.574\n",
  878. " vase 128 2 0.242 1 0.663 0.622\n",
  879. " scissors 128 1 1 0 0.0207 0.00207\n",
  880. " teddy bear 128 21 0.847 0.381 0.622 0.345\n",
  881. " toothbrush 128 5 0.99 0.6 0.662 0.45\n",
  882. "Results saved to \u001b[1mruns/train/exp\u001b[0m\n"
  883. ]
  884. }
  885. ]
  886. },
  887. {
  888. "cell_type": "markdown",
  889. "metadata": {
  890. "id": "15glLzbQx5u0"
  891. },
  892. "source": [
  893. "# 4. Visualize"
  894. ]
  895. },
  896. {
  897. "cell_type": "markdown",
  898. "metadata": {
  899. "id": "DLI1JmHU7B0l"
  900. },
  901. "source": [
  902. "## Weights & Biases Logging 🌟 NEW\n",
  903. "\n",
  904. "[Weights & Biases](https://wandb.ai/site?utm_campaign=repo_yolo_notebook) (W&B) is now integrated with YOLOv5 for real-time visualization and cloud logging of training runs. This allows for better run comparison and introspection, as well improved visibility and collaboration for teams. To enable W&B `pip install wandb`, and then train normally (you will be guided through setup on first use). \n",
  905. "\n",
  906. "During training you will see live updates at [https://wandb.ai/home](https://wandb.ai/home?utm_campaign=repo_yolo_notebook), and you can create and share detailed [Reports](https://wandb.ai/glenn-jocher/yolov5_tutorial/reports/YOLOv5-COCO128-Tutorial-Results--VmlldzozMDI5OTY) of your results. For more information see the [YOLOv5 Weights & Biases Tutorial](https://github.com/ultralytics/yolov5/issues/1289). \n",
  907. "\n",
  908. "<p align=\"left\"><img width=\"900\" alt=\"Weights & Biases dashboard\" src=\"https://user-images.githubusercontent.com/26833433/135390767-c28b050f-8455-4004-adb0-3b730386e2b2.png\"></p>"
  909. ]
  910. },
  911. {
  912. "cell_type": "markdown",
  913. "metadata": {
  914. "id": "-WPvRbS5Swl6"
  915. },
  916. "source": [
  917. "## Local Logging\n",
  918. "\n",
  919. "All results are logged by default to `runs/train`, with a new experiment directory created for each new training as `runs/train/exp2`, `runs/train/exp3`, etc. View train and val jpgs to see mosaics, labels, predictions and augmentation effects. Note an Ultralytics **Mosaic Dataloader** is used for training (shown below), which combines 4 images into 1 mosaic during training.\n",
  920. "\n",
  921. "> <img src=\"https://user-images.githubusercontent.com/26833433/131255960-b536647f-7c61-4f60-bbc5-cb2544d71b2a.jpg\" width=\"700\"> \n",
  922. "`train_batch0.jpg` shows train batch 0 mosaics and labels\n",
  923. "\n",
  924. "> <img src=\"https://user-images.githubusercontent.com/26833433/131256748-603cafc7-55d1-4e58-ab26-83657761aed9.jpg\" width=\"700\"> \n",
  925. "`test_batch0_labels.jpg` shows val batch 0 labels\n",
  926. "\n",
  927. "> <img src=\"https://user-images.githubusercontent.com/26833433/131256752-3f25d7a5-7b0f-4bb3-ab78-46343c3800fe.jpg\" width=\"700\"> \n",
  928. "`test_batch0_pred.jpg` shows val batch 0 _predictions_\n",
  929. "\n",
  930. "Training results are automatically logged to [Tensorboard](https://www.tensorflow.org/tensorboard) and [CSV](https://github.com/ultralytics/yolov5/pull/4148) as `results.csv`, which is plotted as `results.png` (below) after training completes. You can also plot any `results.csv` file manually:\n",
  931. "\n",
  932. "```python\n",
  933. "from utils.plots import plot_results \n",
  934. "plot_results('path/to/results.csv') # plot 'results.csv' as 'results.png'\n",
  935. "```\n",
  936. "\n",
  937. "<img align=\"left\" width=\"800\" alt=\"COCO128 Training Results\" src=\"https://user-images.githubusercontent.com/26833433/126906780-8c5e2990-6116-4de6-b78a-367244a33ccf.png\">"
  938. ]
  939. },
  940. {
  941. "cell_type": "markdown",
  942. "metadata": {
  943. "id": "Zelyeqbyt3GD"
  944. },
  945. "source": [
  946. "# Environments\n",
  947. "\n",
  948. "YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including [CUDA](https://developer.nvidia.com/cuda)/[CUDNN](https://developer.nvidia.com/cudnn), [Python](https://www.python.org/) and [PyTorch](https://pytorch.org/) preinstalled):\n",
  949. "\n",
  950. "- **Google Colab and Kaggle** notebooks with free GPU: <a href=\"https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a> <a href=\"https://www.kaggle.com/ultralytics/yolov5\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" alt=\"Open In Kaggle\"></a>\n",
  951. "- **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart)\n",
  952. "- **Amazon** Deep Learning AMI. See [AWS Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/AWS-Quickstart)\n",
  953. "- **Docker Image**. See [Docker Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/Docker-Quickstart) <a href=\"https://hub.docker.com/r/ultralytics/yolov5\"><img src=\"https://img.shields.io/docker/pulls/ultralytics/yolov5?logo=docker\" alt=\"Docker Pulls\"></a>\n"
  954. ]
  955. },
  956. {
  957. "cell_type": "markdown",
  958. "metadata": {
  959. "id": "6Qu7Iesl0p54"
  960. },
  961. "source": [
  962. "# Status\n",
  963. "\n",
  964. "![CI CPU testing](https://github.com/ultralytics/yolov5/workflows/CI%20CPU%20testing/badge.svg)\n",
  965. "\n",
  966. "If this badge is green, all [YOLOv5 GitHub Actions](https://github.com/ultralytics/yolov5/actions) Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training ([train.py](https://github.com/ultralytics/yolov5/blob/master/train.py)), testing ([val.py](https://github.com/ultralytics/yolov5/blob/master/val.py)), inference ([detect.py](https://github.com/ultralytics/yolov5/blob/master/detect.py)) and export ([export.py](https://github.com/ultralytics/yolov5/blob/master/export.py)) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.\n"
  967. ]
  968. },
  969. {
  970. "cell_type": "markdown",
  971. "metadata": {
  972. "id": "IEijrePND_2I"
  973. },
  974. "source": [
  975. "# Appendix\n",
  976. "\n",
  977. "Optional extras below. Unit tests validate repo functionality and should be run on any PRs submitted.\n"
  978. ]
  979. },
  980. {
  981. "cell_type": "code",
  982. "metadata": {
  983. "id": "mcKoSIK2WSzj"
  984. },
  985. "source": [
  986. "# Reproduce\n",
  987. "for x in 'yolov5s', 'yolov5m', 'yolov5l', 'yolov5x':\n",
  988. " !python val.py --weights {x}.pt --data coco.yaml --img 640 --conf 0.25 --iou 0.45 # speed\n",
  989. " !python val.py --weights {x}.pt --data coco.yaml --img 640 --conf 0.001 --iou 0.65 # mAP"
  990. ],
  991. "execution_count": null,
  992. "outputs": []
  993. },
  994. {
  995. "cell_type": "code",
  996. "metadata": {
  997. "id": "GMusP4OAxFu6"
  998. },
  999. "source": [
  1000. "# PyTorch Hub\n",
  1001. "import torch\n",
  1002. "\n",
  1003. "# Model\n",
  1004. "model = torch.hub.load('ultralytics/yolov5', 'yolov5s')\n",
  1005. "\n",
  1006. "# Images\n",
  1007. "dir = 'https://ultralytics.com/images/'\n",
  1008. "imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')] # batch of images\n",
  1009. "\n",
  1010. "# Inference\n",
  1011. "results = model(imgs)\n",
  1012. "results.print() # or .show(), .save()"
  1013. ],
  1014. "execution_count": null,
  1015. "outputs": []
  1016. },
  1017. {
  1018. "cell_type": "code",
  1019. "metadata": {
  1020. "id": "FGH0ZjkGjejy"
  1021. },
  1022. "source": [
  1023. "# CI Checks\n",
  1024. "%%shell\n",
  1025. "export PYTHONPATH=\"$PWD\" # to run *.py. files in subdirectories\n",
  1026. "rm -rf runs # remove runs/\n",
  1027. "for m in yolov5n; do # models\n",
  1028. " python train.py --img 64 --batch 32 --weights $m.pt --epochs 1 --device 0 # train pretrained\n",
  1029. " python train.py --img 64 --batch 32 --weights '' --cfg $m.yaml --epochs 1 --device 0 # train scratch\n",
  1030. " for d in 0 cpu; do # devices\n",
  1031. " python val.py --weights $m.pt --device $d # val official\n",
  1032. " python val.py --weights runs/train/exp/weights/best.pt --device $d # val custom\n",
  1033. " python detect.py --weights $m.pt --device $d # detect official\n",
  1034. " python detect.py --weights runs/train/exp/weights/best.pt --device $d # detect custom\n",
  1035. " done\n",
  1036. " python hubconf.py # hub\n",
  1037. " python models/yolo.py --cfg $m.yaml # build PyTorch model\n",
  1038. " python models/tf.py --weights $m.pt # build TensorFlow model\n",
  1039. " python export.py --img 64 --batch 1 --weights $m.pt --include torchscript onnx # export\n",
  1040. "done"
  1041. ],
  1042. "execution_count": null,
  1043. "outputs": []
  1044. },
  1045. {
  1046. "cell_type": "code",
  1047. "metadata": {
  1048. "id": "gogI-kwi3Tye"
  1049. },
  1050. "source": [
  1051. "# Profile\n",
  1052. "from utils.torch_utils import profile\n",
  1053. "\n",
  1054. "m1 = lambda x: x * torch.sigmoid(x)\n",
  1055. "m2 = torch.nn.SiLU()\n",
  1056. "results = profile(input=torch.randn(16, 3, 640, 640), ops=[m1, m2], n=100)"
  1057. ],
  1058. "execution_count": null,
  1059. "outputs": []
  1060. },
  1061. {
  1062. "cell_type": "code",
  1063. "metadata": {
  1064. "id": "RVRSOhEvUdb5"
  1065. },
  1066. "source": [
  1067. "# Evolve\n",
  1068. "!python train.py --img 640 --batch 64 --epochs 100 --data coco128.yaml --weights yolov5s.pt --cache --noautoanchor --evolve\n",
  1069. "!d=runs/train/evolve && cp evolve.* $d && zip -r evolve.zip $d && gsutil mv evolve.zip gs://bucket # upload results (optional)"
  1070. ],
  1071. "execution_count": null,
  1072. "outputs": []
  1073. },
  1074. {
  1075. "cell_type": "code",
  1076. "metadata": {
  1077. "id": "BSgFCAcMbk1R"
  1078. },
  1079. "source": [
  1080. "# VOC\n",
  1081. "for b, m in zip([64, 48, 32, 16], ['yolov5s', 'yolov5m', 'yolov5l', 'yolov5x']): # zip(batch_size, model)\n",
  1082. " !python train.py --batch {b} --weights {m}.pt --data VOC.yaml --epochs 50 --cache --img 512 --nosave --hyp hyp.finetune.yaml --project VOC --name {m}"
  1083. ],
  1084. "execution_count": null,
  1085. "outputs": []
  1086. }
  1087. ]
  1088. }