3.1 MiB
3.1 MiB
In [0]:
!git clone https://github.com/ultralytics/yolov5 # clone repo
!pip install -r yolov5/requirements.txt # install dependencies
%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'))In [14]:
!python detect.py --weights yolov5s.pt --img 416 --conf 0.4 --source ./inference/images/
Image(filename='inference/output/zidane.jpg', width=600)Out [14]:
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') Using CUDA device0 _CudaDeviceProperties(name='Tesla P100-PCIE-16GB', total_memory=16280MB) image 1/2 inference/images/bus.jpg: 416x352 3 persons, 1 buss, Done. (0.009s) image 2/2 inference/images/zidane.jpg: 288x416 2 persons, 2 ties, Done. (0.009s) Results saved to /content/yolov5/inference/output Done. (0.100s)
In [0]:
# Example syntax (do not run cell)
!python detect.py --source ./file.jpg # image
./file.mp4 # video
./dir # directory
0 # webcam
'rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa' # rtsp
'http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8' # httpIn [7]:
# Download COCO val2017
gdrive_download('1Y6Kou6kEB0ZEMCCpJSKStCor4KAReE43','coco2017val.zip') # val2017 dataset
!mv ./coco ../ # move folder alongside /yolov5Downloading https://drive.google.com/uc?export=download&id=1Y6Kou6kEB0ZEMCCpJSKStCor4KAReE43 as coco2017val.zip... unzipping... Done (37.1s)
In [8]:
# Run YOLOv5s on COCO val2017
!python test.py --weights yolov5s.pt --data ./data/coco.yaml --img 640Namespace(augment=False, batch_size=32, conf_thres=0.001, data='././data/coco.yaml', device='', img_size=640, iou_thres=0.65, save_json=True, single_cls=False, task='val', verbose=False, weights='yolov5s.pt')
Using CUDA device0 _CudaDeviceProperties(name='Tesla T4', total_memory=15079MB)
Model Summary: 165 layers, 7.07417e+06 parameters, 7.07417e+06 gradients
Caching labels ../coco/val2017.txt (4952 found, 48 missing, 0 empty, 0 duplicate, for 5000 images): 100% 5000/5000 [00:00<00:00, 7822.09it/s]
Saving labels to ../coco/labels/val2017.npy for faster future loading
Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 157/157 [01:33<00:00, 1.67it/s]
all 5e+03 3.63e+04 0.334 0.633 0.534 0.336
Speed: 6.0/2.9/8.9 ms inference/NMS/total per 640x640 image at batch-size 32
COCO mAP with pycocotools... saving detections_val2017_yolov5s_results.json...
loading annotations into memory...
Done (t=0.41s)
creating index...
index created!
Loading and preparing results...
DONE (t=5.70s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=89.04s).
Accumulating evaluation results...
DONE (t=14.04s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.352
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.544
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.378
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.188
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.397
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.459
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.296
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.496
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.557
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.358
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.618
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.700
In [10]:
# Run YOLOv5x on COCO val2017
!python test.py --weights yolov5x.pt --data ./data/coco.yaml --img 640Namespace(augment=False, batch_size=32, conf_thres=0.001, data='././data/coco.yaml', device='', img_size=640, iou_thres=0.65, save_json=True, single_cls=False, task='val', verbose=False, weights='yolov5x.pt')
Using CUDA device0 _CudaDeviceProperties(name='Tesla T4', total_memory=15079MB)
Model Summary: 381 layers, 9.59219e+07 parameters, 9.59219e+07 gradients
Caching labels ../coco/labels/val2017.npy (4952 found, 0 missing, 48 empty, 0 duplicate, for 5000 images): 100% 5000/5000 [00:00<00:00, 16516.13it/s]
Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 157/157 [05:05<00:00, 1.95s/it]
all 5e+03 3.63e+04 0.393 0.742 0.652 0.455
Speed: 51.1/2.2/53.3 ms inference/NMS/total per 640x640 image at batch-size 32
COCO mAP with pycocotools... saving detections_val2017_yolov5x_results.json...
loading annotations into memory...
Done (t=0.39s)
creating index...
index created!
Loading and preparing results...
DONE (t=3.59s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=79.47s).
Accumulating evaluation results...
DONE (t=12.20s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.470
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.659
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.515
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.310
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.516
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.610
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.362
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.597
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.656
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.504
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.704
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.787
In [0]:
# 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 /yolov5In [0]:
# Run YOLOv5s on COCO test-dev2017 with argument --task test
!python test.py --weights yolov5s.pt --data ./data/coco.yaml --task testIn [2]:
# Download tutorial dataset coco128.yaml
gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip') # tutorial dataset
!mv ./coco128 ../ # move folder alongside /yolov5Downloading https://drive.google.com/uc?export=download&id=1n_oKgR81BJtqk75b00eAjdv03qVCQn2f as coco128.zip... unzipping... Done (5.8s)
In [0]:
# Start tensorboard
%load_ext tensorboard
%tensorboard --logdir runsIn [5]:
# Train YOLOv5s on coco128 for 5 epochs
!python train.py --img 640 --batch 16 --epochs 5 --data ./data/coco128.yaml --cfg ./models/yolov5s.yaml --weights yolov5s.pt --name tutorial --nosave --cacheApex recommended for faster mixed precision training: https://github.com/NVIDIA/apex
{'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}
Namespace(adam=False, batch_size=16, bucket='', cache_images=True, cfg='././models/yolov5s.yaml', data='././data/coco128.yaml', device='', epochs=5, evolve=False, img_size=[640], multi_scale=False, name='tutorial', nosave=True, notest=False, rect=False, resume=False, single_cls=False, weights='yolov5s.pt')
Using CUDA device0 _CudaDeviceProperties(name='Tesla T4', total_memory=15079MB)
2020-06-14 19:45:19.668043: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
Start Tensorboard with "tensorboard --logdir=runs", view at http://localhost:6006/
from n params module arguments
0 -1 1 3520 models.common.Focus [3, 32, 3]
1 -1 1 18560 models.common.Conv [32, 64, 3, 2]
2 -1 1 20672 models.common.Bottleneck [64, 64]
3 -1 1 73984 models.common.Conv [64, 128, 3, 2]
4 -1 1 161152 models.common.BottleneckCSP [128, 128, 3]
5 -1 1 295424 models.common.Conv [128, 256, 3, 2]
6 -1 1 641792 models.common.BottleneckCSP [256, 256, 3]
7 -1 1 1180672 models.common.Conv [256, 512, 3, 2]
8 -1 1 656896 models.common.SPP [512, 512, [5, 9, 13]]
9 -1 1 1905152 models.common.BottleneckCSP [512, 512, 2]
10 -1 1 1248768 models.common.BottleneckCSP [512, 512, 1, False]
11 -1 1 130815 torch.nn.modules.conv.Conv2d [512, 255, 1, 1]
12 -2 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
13 [-1, 6] 1 0 models.common.Concat [1]
14 -1 1 197120 models.common.Conv [768, 256, 1, 1]
15 -1 1 313088 models.common.BottleneckCSP [256, 256, 1, False]
16 -1 1 65535 torch.nn.modules.conv.Conv2d [256, 255, 1, 1]
17 -2 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
18 [-1, 4] 1 0 models.common.Concat [1]
19 -1 1 49408 models.common.Conv [384, 128, 1, 1]
20 -1 1 78720 models.common.BottleneckCSP [128, 128, 1, False]
21 -1 1 32895 torch.nn.modules.conv.Conv2d [128, 255, 1, 1]
22 [-1, 16, 11] 1 0 models.yolo.Detect [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]]]
Model Summary: 165 layers, 7.07417e+06 parameters, 7.07417e+06 gradients
Optimizer groups: 54 .bias, 60 conv.weight, 51 other
Reading image shapes: 100% 128/128 [00:00<00:00, 8158.02it/s]
Caching labels ../coco128/labels/train2017 (126 found, 0 missing, 2 empty, 0 duplicate, for 128 images): 100% 128/128 [00:00<00:00, 7608.18it/s]
Caching images (0.1GB): 100% 128/128 [00:00<00:00, 149.70it/s]
Caching labels ../coco128/labels/train2017 (126 found, 0 missing, 2 empty, 0 duplicate, for 128 images): 100% 128/128 [00:00<00:00, 8165.09it/s]
Caching images (0.1GB): 100% 128/128 [00:00<00:00, 145.34it/s]
Label width-height: n mean min max matching recall
929 105.8 1.23 640 0.4736 0.9903
Image sizes 640 train, 640 test
Using 2 dataloader workers
Starting training for 5 epochs...
Epoch gpu_mem GIoU obj cls total targets img_size
0/4 7.3G 0.04362 0.07954 0.02032 0.1435 208 640: 100% 8/8 [00:09<00:00, 1.24s/it]
Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 8/8 [00:07<00:00, 1.03it/s]
all 128 929 0.421 0.704 0.66 0.42
Epoch gpu_mem GIoU obj cls total targets img_size
1/4 13.9G 0.04609 0.07819 0.01886 0.1431 167 640: 100% 8/8 [00:03<00:00, 2.34it/s]
Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 8/8 [00:02<00:00, 3.34it/s]
all 128 929 0.418 0.713 0.659 0.421
Epoch gpu_mem GIoU obj cls total targets img_size
2/4 13.9G 0.04508 0.07012 0.01977 0.135 198 640: 100% 8/8 [00:03<00:00, 2.33it/s]
Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 8/8 [00:02<00:00, 3.50it/s]
all 128 929 0.409 0.71 0.664 0.421
Epoch gpu_mem GIoU obj cls total targets img_size
3/4 13.9G 0.04577 0.08308 0.02038 0.1492 222 640: 100% 8/8 [00:03<00:00, 2.34it/s]
Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 8/8 [00:02<00:00, 3.50it/s]
all 128 929 0.429 0.71 0.67 0.426
Epoch gpu_mem GIoU obj cls total targets img_size
4/4 13.9G 0.04608 0.07352 0.01855 0.1382 149 640: 100% 8/8 [00:03<00:00, 2.32it/s]
Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 8/8 [00:02<00:00, 3.46it/s]
all 128 929 0.423 0.716 0.663 0.422
Optimizer stripped from weights/last_tutorial.pt
5 epochs completed in 0.012 hours.
In [0]:
Image(filename='./train_batch1.jpg', width=900) # view augmented training mosaicsOut [0]:
In [0]:
Image(filename='./test_batch0_gt.jpg', width=900) # view test image labelsOut [0]:
In [0]:
Image(filename='./test_batch0_pred.jpg', width=900) # view test image predictionsOut [0]:
In [0]:
from utils.utils import plot_results; plot_results() # plot results.txt as results.png
Image(filename='./results.png', width=1000) # view results.pngOut [0]:
In [0]:
# Re-clone
%cd ..
!rm -rf yolov5 && git clone https://github.com/ultralytics/yolov5
%cd yolov5In [0]:
# Apex install
git clone https://github.com/NVIDIA/apex && cd apex && pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" . --user && cd .. && rm -rf apexIn [0]:
# Test GCP checkpoint on COCO val2017
%%shell
x=best*.pt
gsutil cp gs://*/*/weights/$x .
python test.py --weights $x --data ./data/coco.yaml --img 736In [0]:
# Test multiple models on COCO val2017
%%shell
for x in yolov5s yolov5m yolov5l yolov5x
do
python test.py --weights $x.pt --data ./data/coco.yaml --img 640 --conf 0.001
done