{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "YOLOv5 Tutorial",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<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>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "HvhYZrIZCEyo"
      },
      "source": [
        "<img src=\"https://user-images.githubusercontent.com/26833433/82952157-51b7db00-9f5d-11ea-8f4b-dda1ffecf992.jpg\">\n",
        "\n",
        "This notebook was written by Ultralytics LLC, and is freely available for redistribution under the [GPL-3.0 license](https://choosealicense.com/licenses/gpl-3.0/). \n",
        "For more information please visit https://github.com/ultralytics/yolov5 and https://www.ultralytics.com."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7mGmQbAO5pQb",
        "colab_type": "text"
      },
      "source": [
        "# Setup\n",
        "\n",
        "Clone repo, install dependencies, `%cd` into `./yolov5` folder and check GPU."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wbvMlHd_QwMG",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        },
        "outputId": "669566b2-391f-4596-f290-110e2e177946"
      },
      "source": [
        "!git clone https://github.com/ultralytics/yolov5  # clone repo\n",
        "!pip install -qr yolov5/requirements.txt  # install dependencies (ignore errors)\n",
        "%cd yolov5\n",
        "\n",
        "import torch\n",
        "from IPython.display import Image, clear_output  # to display images\n",
        "from utils.google_utils import gdrive_download  # to download models/datasets\n",
        "\n",
        "clear_output()\n",
        "print('Setup complete. Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Setup complete. Using torch 1.5.1+cu101 _CudaDeviceProperties(name='Tesla T4', major=7, minor=5, total_memory=15079MB, multi_processor_count=40)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "N3qM6T0W53gh",
        "colab_type": "text"
      },
      "source": [
        "# 1. Inference\n",
        "\n",
        "Run inference with a pretrained checkpoint on contents of `/inference/images` folder. Models are auto-downloaded from [Google Drive](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zR9ZbuQCH7FX",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 488
        },
        "outputId": "528fcc04-2393-437a-84d2-092becbaefbe"
      },
      "source": [
        "!python detect.py --weights yolov5s.pt --img 416 --conf 0.4 --source inference/images/\n",
        "Image(filename='inference/output/zidane.jpg', width=600)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.4, device='', fourcc='mp4v', half=False, img_size=416, iou_thres=0.5, output='inference/output', save_txt=False, source='./inference/images/', view_img=False, weights='yolov5s.pt')\n",
            "Using CUDA device0 _CudaDeviceProperties(name='Tesla P100-PCIE-16GB', total_memory=16280MB)\n",
            "\n",
            "image 1/2 inference/images/bus.jpg: 416x352 3 persons, 1 buss, Done. (0.009s)\n",
            "image 2/2 inference/images/zidane.jpg: 288x416 2 persons, 2 ties, Done. (0.009s)\n",
            "Results saved to /content/yolov5/inference/output\n",
            "Done. (0.100s)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "image/jpeg": "[encoded data, 339772 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {
            "tags": [],
            "image/jpeg": {
              "width": 600
            }
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4JnkELT0cIJg",
        "colab_type": "text"
      },
      "source": [
        "Inference can be run on a variety of sources: images, videos, directories, webcams, rtsp and http streams as shown in the example below."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WwosXmgDahte",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Example syntax (do not run cell)\n",
        "!python detect.py --source file.jpg  # image \n",
        "                           file.mp4  # video\n",
        "                           dir/  # directory\n",
        "                           0  # webcam\n",
        "                           'rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa' # rtsp\n",
        "                           'http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8'  # http"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0eq1SMWl6Sfn",
        "colab_type": "text"
      },
      "source": [
        "# 2. Test\n",
        "Test a model on COCO val or test-dev dataset to determine trained accuracy. Models are auto-downloaded from [Google Drive](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J). To show results by class use the `--verbose` flag. Note that `pycocotools` metrics may be 1-2% better than the equivalent repo metrics, as is visible below, due to slight differences in mAP computation."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eyTZYGgRjnMc",
        "colab_type": "text"
      },
      "source": [
        "### 2.1 val2017\n",
        "Download COCO val 2017 dataset, 1GB, 5000 images, and test model accuracy."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WQPtK1QYVaD_",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 33
        },
        "outputId": "df037a5d-efae-4687-9ff7-22a48fd7f801"
      },
      "source": [
        "# Download COCO val2017\n",
        "gdrive_download('1Y6Kou6kEB0ZEMCCpJSKStCor4KAReE43','coco2017val.zip')  # val2017 dataset\n",
        "!mv ./coco ../  # move folder alongside /yolov5"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading https://drive.google.com/uc?export=download&id=1Y6Kou6kEB0ZEMCCpJSKStCor4KAReE43 as coco2017val.zip... unzipping... Done (11.2s)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "X58w8JLpMnjH",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 606
        },
        "outputId": "8c62a086-e312-46d1-b475-d90542eae545"
      },
      "source": [
        "# Run YOLOv5x on COCO val2017\n",
        "!python test.py --weights yolov5x.pt --data coco.yaml --img 672"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Namespace(augment=False, batch_size=32, conf_thres=0.001, data='./data/coco.yaml', device='', img_size=672, iou_thres=0.65, merge=False, save_json=True, single_cls=False, task='val', verbose=False, weights=['yolov5x.pt'])\n",
            "Using CUDA device0 _CudaDeviceProperties(name='Tesla T4', total_memory=15079MB)\n",
            "\n",
            "Fusing layers... Model Summary: 284 layers, 8.89222e+07 parameters, 8.89222e+07 gradients\n",
            "Scanning labels ../coco/labels/val2017.cache (4952 found, 0 missing, 48 empty, 0 duplicate, for 5000 images): 100% 5000/5000 [00:00<00:00, 22899.17it/s]\n",
            "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 157/157 [02:38<00:00,  1.01s/it]\n",
            "                 all       5e+03    3.63e+04       0.426       0.746        0.66       0.469\n",
            "Speed: 22.3/1.7/24.0 ms inference/NMS/total per 672x672 image at batch-size 32\n",
            "\n",
            "COCO mAP with pycocotools... saving detections_val2017__results.json...\n",
            "loading annotations into memory...\n",
            "Done (t=0.41s)\n",
            "creating index...\n",
            "index created!\n",
            "Loading and preparing results...\n",
            "DONE (t=4.39s)\n",
            "creating index...\n",
            "index created!\n",
            "Running per image evaluation...\n",
            "Evaluate annotation type *bbox*\n",
            "DONE (t=76.56s).\n",
            "Accumulating evaluation results...\n",
            "DONE (t=11.02s).\n",
            " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.484\n",
            " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.668\n",
            " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.528\n",
            " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.311\n",
            " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.534\n",
            " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.628\n",
            " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.371\n",
            " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.609\n",
            " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.662\n",
            " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.501\n",
            " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.714\n",
            " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.807\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rc_KbFk0juX2",
        "colab_type": "text"
      },
      "source": [
        "### 2.2 test-dev2017\n",
        "Download COCO test2017 dataset, 7GB, 40,000 images, to test model accuracy on test-dev set, 20,000 images. Results are saved to a `*.json` file which can be submitted to the evaluation server at https://competitions.codalab.org/competitions/20794."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "V0AJnSeCIHyJ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Download COCO test-dev2017\n",
        "gdrive_download('1cXZR_ckHki6nddOmcysCuuJFM--T-Q6L','coco2017labels.zip')  # annotations\n",
        "!f=\"test2017.zip\" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f  # 7GB,  41k images\n",
        "!mv ./test2017 ./coco/images && mv ./coco ../  # move images into /coco and move /coco alongside /yolov5"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "29GJXAP_lPrt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run YOLOv5s on COCO test-dev2017 with argument --task test\n",
        "!python test.py --weights yolov5s.pt --data ./data/coco.yaml --task test"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VUOiNLtMP5aG",
        "colab_type": "text"
      },
      "source": [
        "# 3. Train\n",
        "\n",
        "Download https://www.kaggle.com/ultralytics/coco128, a small 128-image tutorial dataset, start tensorboard and train YOLOv5s from a pretrained checkpoint for 3 epochs (actual training is much longer, around **300-1000 epochs**, depending on your dataset)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Knxi2ncxWffW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 33
        },
        "outputId": "35815e93-7d6e-4fee-c050-a4a565d51648"
      },
      "source": [
        "# Download coco128\n",
        "gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')  # coco128 dataset\n",
        "!mv ./coco128 ../  # move folder alongside /yolov5"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading https://drive.google.com/uc?export=download&id=1n_oKgR81BJtqk75b00eAjdv03qVCQn2f as coco128.zip... unzipping... Done (5.3s)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_pOkGLv1dMqh",
        "colab_type": "text"
      },
      "source": [
        "Train a YOLOv5s model on coco128 by specifying model config file `--cfg models/yolo5s.yaml`, and dataset config file `--data data/coco128.yaml`. Start training from pretrained `--weights yolov5s.pt`, or from randomly initialized `--weights ''`. Pretrained weights are auto-downloaded from [Google Drive](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J).\n",
        "\n",
        "**All training results are saved to `runs/exp0`** for the first experiment, then `runs/exp1`, `runs/exp2` etc. for subsequent experiments.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bOy5KI2ncnWd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Start tensorboard (optional)\n",
        "%load_ext tensorboard\n",
        "%tensorboard --logdir runs"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1NcFxRcFdJ_O",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "outputId": "121b5b2e-bc8e-4648-ee1c-8d2795176db6"
      },
      "source": [
        "# Train YOLOv5s on coco128 for 3 epochs\n",
        "!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --cfg yolov5s.yaml --weights yolov5s.pt --nosave --cache"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Namespace(batch_size=16, bucket='', cache_images=True, cfg='./models/yolov5s.yaml', data='./data/coco128.yaml', device='', epochs=3, evolve=False, hyp='', img_size=[640], multi_scale=False, name='', noautoanchor=False, nosave=True, notest=False, rect=False, resume=False, single_cls=False, weights='yolov5s.pt')\n",
            "Using CUDA device0 _CudaDeviceProperties(name='Tesla T4', total_memory=15079MB)\n",
            "\n",
            "2020-07-11 20:37:09.422496: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1\n",
            "Start Tensorboard with \"tensorboard --logdir=runs\", view at http://localhost:6006/\n",
            "Hyperparameters {'optimizer': 'SGD', 'lr0': 0.01, 'momentum': 0.937, 'weight_decay': 0.0005, 'giou': 0.05, 'cls': 0.58, '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.014, 'hsv_s': 0.68, 'hsv_v': 0.36, 'degrees': 0.0, 'translate': 0.0, 'scale': 0.5, 'shear': 0.0}\n",
            "\n",
            "                 from  n    params  module                                  arguments                     \n",
            "  0                -1  1      3520  models.common.Focus                     [3, 32, 3]                    \n",
            "  1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                \n",
            "  2                -1  1     19904  models.common.BottleneckCSP             [64, 64, 1]                   \n",
            "  3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               \n",
            "  4                -1  1    161152  models.common.BottleneckCSP             [128, 128, 3]                 \n",
            "  5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              \n",
            "  6                -1  1    641792  models.common.BottleneckCSP             [256, 256, 3]                 \n",
            "  7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]              \n",
            "  8                -1  1    656896  models.common.SPP                       [512, 512, [5, 9, 13]]        \n",
            "  9                -1  1   1248768  models.common.BottleneckCSP             [512, 512, 1, False]          \n",
            " 10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]              \n",
            " 11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          \n",
            " 12           [-1, 6]  1         0  models.common.Concat                    [1]                           \n",
            " 13                -1  1    378624  models.common.BottleneckCSP             [512, 256, 1, False]          \n",
            " 14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              \n",
            " 15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          \n",
            " 16           [-1, 4]  1         0  models.common.Concat                    [1]                           \n",
            " 17                -1  1     95104  models.common.BottleneckCSP             [256, 128, 1, False]          \n",
            " 18                -1  1     32895  torch.nn.modules.conv.Conv2d            [128, 255, 1, 1]              \n",
            " 19                -2  1    147712  models.common.Conv                      [128, 128, 3, 2]              \n",
            " 20          [-1, 14]  1         0  models.common.Concat                    [1]                           \n",
            " 21                -1  1    313088  models.common.BottleneckCSP             [256, 256, 1, False]          \n",
            " 22                -1  1     65535  torch.nn.modules.conv.Conv2d            [256, 255, 1, 1]              \n",
            " 23                -2  1    590336  models.common.Conv                      [256, 256, 3, 2]              \n",
            " 24          [-1, 10]  1         0  models.common.Concat                    [1]                           \n",
            " 25                -1  1   1248768  models.common.BottleneckCSP             [512, 512, 1, False]          \n",
            " 26                -1  1    130815  torch.nn.modules.conv.Conv2d            [512, 255, 1, 1]              \n",
            " 27      [-1, 22, 18]  1         0  models.yolo.Detect                      [80, [[116, 90, 156, 198, 373, 326], [30, 61, 62, 45, 59, 119], [10, 13, 16, 30, 33, 23]]]\n",
            "Model Summary: 191 layers, 7.46816e+06 parameters, 7.46816e+06 gradients\n",
            "\n",
            "Optimizer groups: 62 .bias, 70 conv.weight, 59 other\n",
            "Scanning labels ../coco128/labels/train2017.cache (126 found, 0 missing, 2 empty, 0 duplicate, for 128 images): 100% 128/128 [00:00<00:00, 20484.22it/s]\n",
            "Caching images (0.1GB): 100% 128/128 [00:00<00:00, 156.07it/s]\n",
            "Scanning labels ../coco128/labels/train2017.cache (126 found, 0 missing, 2 empty, 0 duplicate, for 128 images): 100% 128/128 [00:00<00:00, 22082.55it/s]\n",
            "Caching images (0.1GB): 100% 128/128 [00:00<00:00, 152.91it/s]\n",
            "\n",
            "Analyzing anchors... Best Possible Recall (BPR) = 0.9935\n",
            "Image sizes 640 train, 640 test\n",
            "Using 2 dataloader workers\n",
            "Starting training for 3 epochs...\n",
            "\n",
            "     Epoch   gpu_mem      GIoU       obj       cls     total   targets  img_size\n",
            "       0/2     6.84G   0.04376   0.06831      0.02    0.1321       225       640: 100% 8/8 [00:09<00:00,  1.22s/it]\n",
            "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 8/8 [00:09<00:00,  1.24s/it]\n",
            "                 all         128         929        0.34       0.762        0.69       0.446\n",
            "\n",
            "     Epoch   gpu_mem      GIoU       obj       cls     total   targets  img_size\n",
            "       1/2     6.06G   0.04333   0.08225   0.02207    0.1476       182       640: 100% 8/8 [00:03<00:00,  2.17it/s]\n",
            "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 8/8 [00:02<00:00,  3.28it/s]\n",
            "                 all         128         929       0.342       0.755       0.687       0.447\n",
            "\n",
            "     Epoch   gpu_mem      GIoU       obj       cls     total   targets  img_size\n",
            "       2/2     6.06G    0.0444   0.07251   0.01855    0.1355       216       640: 100% 8/8 [00:03<00:00,  2.15it/s]\n",
            "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 8/8 [00:02<00:00,  3.46it/s]\n",
            "                 all         128         929       0.354       0.759       0.689        0.45\n",
            "Optimizer stripped from runs/exp0/weights/last.pt, 15.2MB\n",
            "3 epochs completed in 0.009 hours.\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DLI1JmHU7B0l",
        "colab_type": "text"
      },
      "source": [
        "# 4. Visualize\n",
        "\n",
        "View `runs/exp0/train*.jpg` images to see training images, labels and augmentation effects. A **Mosaic Dataloader** is used for training (shown below), a new concept developed by Ultralytics and first featured in [YOLOv4](https://arxiv.org/abs/2004.10934)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W40tI99_7BcH",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 917
        },
        "outputId": "1c838e44-79fe-433f-a334-59a037ee322e"
      },
      "source": [
        "Image(filename='runs/exp0/train_batch1.jpg', width=900)  # view augmented training mosaics"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/jpeg": "[encoded data, 1056892 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {
            "tags": [],
            "image/jpeg": {
              "width": 900
            }
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7uOowJKI-Qak",
        "colab_type": "text"
      },
      "source": [
        "View `test_batch0_gt.jpg` to see test batch 0 *ground truth* labels."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PF9MLHDb7tB6",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 647
        },
        "outputId": "b7a874f7-dad3-4611-e777-56c724c7ee81"
      },
      "source": [
        "Image(filename='runs/exp0/test_batch0_gt.jpg', width=900)  # view test image labels"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/jpeg": "[encoded data, 751160 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {
            "tags": [],
            "image/jpeg": {
              "width": 900
            }
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "EGrb16Mu-jif",
        "colab_type": "text"
      },
      "source": [
        "View `test_batch0_pred.jpg` to see test batch 0 *predictions*."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ycP4UTEZ82_I",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 647
        },
        "outputId": "c7c1238d-e0fa-4fc5-f393-bf5bce55d245"
      },
      "source": [
        "Image(filename='runs/exp0/test_batch0_pred.jpg', width=900)  # view test image predictions"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/jpeg": "[encoded data, 753584 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {
            "tags": [],
            "image/jpeg": {
              "width": 900
            }
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7KN5ghjE6ZWh",
        "colab_type": "text"
      },
      "source": [
        "Training losses and performance metrics are saved to Tensorboard and also to a `runs/exp0/results.txt` logfile. `results.txt` is plotted as `results.png` after training completes. Partially completed `results.txt` files can be plotted with `from utils.general import plot_results; plot_results()`. Here we show YOLOv5s trained on coco128 to 300 epochs, starting from scratch (blue), and from pretrained `yolov5s.pt` (orange)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MDznIqPF7nk3",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 517
        },
        "outputId": "c1146425-643e-49ab-de25-73216f0dde23"
      },
      "source": [
        "from utils.general import plot_results; plot_results()  # plot results.txt files as results.png"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/png": "[encoded data, 343576 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {
            "tags": [],
            "image/png": {
              "width": 1000
            }
          },
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Zelyeqbyt3GD",
        "colab_type": "text"
      },
      "source": [
        "# Environments\n",
        "\n",
        "YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):\n",
        "\n",
        "- **Google Colab Notebook** 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>\n",
        "- **Kaggle Notebook** with free GPU: [https://www.kaggle.com/ultralytics/yolov5](https://www.kaggle.com/ultralytics/yolov5)\n",
        "- **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart) \n",
        "- **Docker Image** https://hub.docker.com/r/ultralytics/yolov5. See [Docker Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/Docker-Quickstart) ![Docker Pulls](https://img.shields.io/docker/pulls/ultralytics/yolov5?logo=docker)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IEijrePND_2I",
        "colab_type": "text"
      },
      "source": [
        "# Appendix\n",
        "\n",
        "Optional extras below. Unit tests validate repo functionality and should be run on any PRs submitted.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gI6NoBev8Ib1",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Re-clone repo\n",
        "%cd ..\n",
        "!rm -rf yolov5 && git clone https://github.com/ultralytics/yolov5\n",
        "%cd yolov5"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Z2AvpeKfrbsT",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Test GCP ckpt\n",
        "%%shell\n",
        "for x in best*\n",
        "do\n",
        "  gsutil cp gs://*/*/*/$x.pt .\n",
        "  python test.py --weights $x.pt --data coco.yaml --img 672\n",
        "done"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FGH0ZjkGjejy",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# YOLOv5 unit tests\n",
        "%%shell\n",
        "cd .. && rm -rf yolov5 && git clone https://github.com/ultralytics/yolov5 && cd yolov5\n",
        "export PYTHONPATH=\"$PWD\" # to run *.py. files in subdirectories\n",
        "pip install -qr requirements.txt onnx\n",
        "python3 -c \"from utils.google_utils import *; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', 'coco128.zip')\" && mv ./coco128 ../\n",
        "for x in yolov5s #yolov5m yolov5l yolov5x # models\n",
        "do\n",
        "  python train.py --weights $x.pt --cfg $x.yaml --epochs 4 --img 320 --device 0  # train\n",
        "  for di in 0 cpu # inference devices\n",
        "  do\n",
        "    python detect.py --weights $x.pt --device $di  # detect official\n",
        "    python detect.py --weights runs/exp0/weights/last.pt --device $di  # detect custom\n",
        "    python test.py --weights $x.pt --device $di # test official\n",
        "    python test.py --weights runs/exp0/weights/last.pt --device $di # test custom\n",
        "  done\n",
        "  python models/yolo.py --cfg $x.yaml # inspect\n",
        "  python models/export.py --weights $x.pt --img 640 --batch 1 # export\n",
        "done"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}