<jupyter_start><jupyter_text><jupyter_code>!ls -l<jupyter_output>total 8
drwx------ 5 root root 4096 Mar 17 05:35 drive
drwxr-xr-x 1 root root 4096 Mar  5 14:37 sample_data<jupyter_text>SetupClone repo, install dependencies, `%cd` into `./yolov5` folder and check GPU.<jupyter_code>!git clone https://github.com/ultralytics/yolov5  # clone repo
!pip install -qr yolov5/requirements.txt  # install dependencies (ignore errors)
%cd yolov5

import torch
from IPython.display import Image, clear_output  # to display images
from utils.google_utils import gdrive_download  # to download models/datasets

clear_output()
print('Setup complete. Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))<jupyter_output>Setup complete. Using torch 1.8.0+cu101 _CudaDeviceProperties(name='Tesla T4', major=7, minor=5, total_memory=15109MB, multi_processor_count=40)<jupyter_text>1. InferenceRun 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).<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
!pwd
!cp /content/drive/MyDrive/yolo5/bus832.pt .
%cd yolov5
!mkdir test
!cp /content/drive/MyDrive/yolo5/test/images/* /content/test
Image(filename='/content/test/2_104449_11_32_png.rf.a1b1e7bcefef7540b5410540e652eb9d.jpg', width=600)
Image(filename='/content/test/2_112206_11_33_png.rf.005cef0aa4d3a6ca7f5916142ac8764f.jpg', width=600)
Image(filename='/content/test/2_112206_11_63_png.rf.075211304f00221a75e37e6c7c6b94e3.jpg', width=600)
!python detect.py --weights bus832.pt --img 416 --conf 0.4 --source ../test/
Image(filename='/content/yolov5/runs/detect/exp2/2_104449_11_32_png.rf.a1b1e7bcefef7540b5410540e652eb9d.jpg', width=600)
Image(filename='/content/yolov5/runs/detect/exp2/2_112206_11_33_png.rf.005cef0aa4d3a6ca7f5916142ac8764f.jpg', width=600)
Image(filename='/content/yolov5/runs/detect/exp2/2_112206_11_63_png.rf.075211304f00221a75e37e6c7c6b94e3.jpg', width=600)<jupyter_output><empty_output><jupyter_text>Inference can be run on a variety of sources: images, videos, directories, webcams, rtsp and http streams as shown in the example below.<jupyter_code>!python detect.py --source 'inference/images/20201009_0.mp4' --conf-thres 0.30
!python detect.py --source '20201102.mp4' --conf-thres 0.3
#!python detect.py --source 1
!python detect.py --weights 10-10-web-best.pt --source 'inference/images/2020-10-11-web-rpa.mp4' --conf-thres 0.3
# Example syntax (do not run cell)
!python detect.py --weights yolov5s.pt  --source 'inference/images/20201009_1.mp4' --conf-thres 0.3 # video
!python detect.py --weights yolov5s.pt --source 'giraffe.jpg' --conf-thres 0.3
# Example syntax (do not run cell)
!python detect.py --source file.jpg  # image 
                           file.mp4  # video
                           dir/  # directory
                           0  # webcam
                           'rtsp://172.16.31.10/rtplive/470011e600ef003a004ee33696235daa' # rtsp
                           'http://172.16.31.10/PLTV/88888888/224/3221225900/1.m3u8'  # http<jupyter_output><empty_output><jupyter_text>2. TestTest 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.  2.1 val2017Download COCO val 2017 dataset, 1GB, 5000 images, and test model accuracy.<jupyter_code># Download COCO val2017
gdrive_download('1Y6Kou6kEB0ZEMCCpJSKStCor4KAReE43','coco2017val.zip')  # val2017 dataset
!mv ./coco ../  # move folder alongside /yolov5
# Run YOLOv5x on COCO val2017
!python test.py --weights yolov5x.pt --data coco.yaml --img 672<jupyter_output><empty_output><jupyter_text>2.2 test-dev2017Download 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.<jupyter_code># Download COCO test-dev2017
gdrive_download('1cXZR_ckHki6nddOmcysCuuJFM--T-Q6L','coco2017labels.zip')  # annotations
!f="test2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f  # 7GB,  41k images
!mv ./test2017 ./coco/images && mv ./coco ../  # move images into /coco and move /coco alongside /yolov5
# Run YOLOv5s on COCO test-dev2017 with argument --task test
!python test.py --weights yolov5s.pt --data ./data/coco.yaml --task test<jupyter_output><empty_output><jupyter_text>3. TrainDownload 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).<jupyter_code># Download coco128
gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')  # coco128 dataset
!mv ./coco128 ../  # move folder alongside /yolov5<jupyter_output>Downloading https://drive.google.com/uc?export=download&id=1n_oKgR81BJtqk75b00eAjdv03qVCQn2f as coco128.zip... unzipping... Done (5.3s)<jupyter_text>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).**All training results are saved to `runs/exp0`** for the first experiment, then `runs/exp1`, `runs/exp2` etc. for subsequent experiments.<jupyter_code># Start tensorboard (optional)
%load_ext tensorboard
%tensorboard --logdir runs
# Train YOLOv5s on coco128 for 3 epochs
!python train.py --img 640 --batch 16 --epochs 30 --data coco128.yaml --cfg yolov5s.yaml --weights yolov5s.pt --nosave --cache<jupyter_output><empty_output><jupyter_text>4. VisualizeView `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).<jupyter_code>Image(filename='runs/exp0/train_batch1.jpg', width=900)  # view augmented training mosaics<jupyter_output><empty_output><jupyter_text>View `test_batch0_gt.jpg` to see test batch 0 *ground truth* labels.<jupyter_code>Image(filename='runs/exp0/test_batch0_gt.jpg', width=900)  # view test image labels<jupyter_output><empty_output><jupyter_text>View `test_batch0_pred.jpg` to see test batch 0 *predictions*.<jupyter_code>Image(filename='runs/exp0/test_batch0_pred.jpg', width=900)  # view test image predictions<jupyter_output><empty_output><jupyter_text>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).<jupyter_code>from utils.general import plot_results; plot_results()  # plot results.txt files as results.png<jupyter_output><empty_output><jupyter_text>EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):- **Google Colab Notebook** with free GPU: - **Kaggle Notebook** with free GPU: [https://www.kaggle.com/ultralytics/yolov5](https://www.kaggle.com/ultralytics/yolov5)- **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart) - **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)  AppendixOptional extras below. Unit tests validate repo functionality and should be run on any PRs submitted.<jupyter_code># Re-clone repo
%cd ..
!rm -rf yolov5 && git clone https://github.com/ultralytics/yolov5
%cd yolov5
# Test GCP ckpt
%%shell
for x in best*
do
  gsutil cp gs://*/*/*/$x.pt .
  python test.py --weights $x.pt --data coco.yaml --img 672
done
# YOLOv5 unit tests
%%shell
cd .. && rm -rf yolov5 && git clone https://github.com/ultralytics/yolov5 && cd yolov5
export PYTHONPATH="$PWD" # to run *.py. files in subdirectories
pip install -qr requirements.txt onnx
python3 -c "from utils.google_utils import *; gdrive_download('<KEY>', 'coco128.zip')" && mv ./coco128 ../
for x in yolov5s #yolov5m yolov5l yolov5x # models
do
  python train.py --weights $x.pt --cfg $x.yaml --epochs 4 --img 320 --device 0  # train
  for di in 0 cpu # inference devices
  do
    python detect.py --weights $x.pt --device $di  # detect official
    python detect.py --weights runs/exp0/weights/last.pt --device $di  # detect custom
    python test.py --weights $x.pt --device $di # test official
    python test.py --weights runs/exp0/weights/last.pt --device $di # test custom
  done
  python models/yolo.py --cfg $x.yaml # inspect
  python models/export.py --weights $x.pt --img 640 --batch 1 # export
done<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12002892263195951, 'fraction_numerical': 0.07044726784423097, 'mean_word_length': 4.681924882629108, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 16, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '247471', 'license': 'MIT', 'n_tokens_mistral': 3846, 'n_tokens_neox': 3453, 'n_words': 867, 'path': 'yolov5-bus.ipynb', 'repo_name': 'JSJeong-me/CCTV', 'size': '9711'}