Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher committed Oct 25, 2022
1 parent 54f49fa commit 8236d88
Showing 1 changed file with 6 additions and 135 deletions.
141 changes: 6 additions & 135 deletions tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@
"source": [
"# Appendix\n",
"\n",
"Additional content below for PyTorch Hub, CI, reproducing results, profiling speeds, VOC training, classification training and TensorRT example."
"Additional content below."
]
},
{
Expand All @@ -963,145 +963,16 @@
"id": "GMusP4OAxFu6"
},
"source": [
"# YOLOv5 PyTorch HUB Inference (DetectionModels only)\n",
"import torch\n",
"\n",
"# PyTorch Hub Model\n",
"model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5n - yolov5x6, custom\n",
"\n",
"# Images\n",
"img = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list\n",
"\n",
"# Inference\n",
"results = model(img)\n",
"\n",
"# Results\n",
"model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # yolov5n - yolov5x6 or custom\n",
"im = 'https://ultralytics.com/images/zidane.jpg' # file, Path, PIL.Image, OpenCV, nparray, list\n",
"results = model(im) # inference\n",
"results.print() # or .show(), .save(), .crop(), .pandas(), etc."
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "FGH0ZjkGjejy"
},
"source": [
"# YOLOv5 CI\n",
"%%shell\n",
"rm -rf runs # remove runs/\n",
"m=yolov5n # official weights\n",
"b=runs/train/exp/weights/best # best.pt checkpoint\n",
"python train.py --imgsz 64 --batch 32 --weights $m.pt --cfg $m.yaml --epochs 1 --device 0 # train\n",
"for d in 0 cpu; do # devices\n",
" for w in $m $b; do # weights\n",
" python val.py --imgsz 64 --batch 32 --weights $w.pt --device $d # val\n",
" python detect.py --imgsz 64 --weights $w.pt --device $d # detect\n",
" done\n",
"done\n",
"python hubconf.py --model $m # hub\n",
"python models/tf.py --weights $m.pt # build TF model\n",
"python models/yolo.py --cfg $m.yaml # build PyTorch model\n",
"python export.py --weights $m.pt --img 64 --include torchscript # export"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mcKoSIK2WSzj"
},
"source": [
"# Reproduce\n",
"for x in (f'yolov5{x}' for x in 'nsmlx'):\n",
" !python val.py --weights {x}.pt --data coco.yaml --img 640 --task speed # speed\n",
" !python val.py --weights {x}.pt --data coco.yaml --img 640 --conf 0.001 --iou 0.65 # mAP"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "gogI-kwi3Tye"
},
"source": [
"# Profile\n",
"from utils.torch_utils import profile\n",
"\n",
"m1 = lambda x: x * torch.sigmoid(x)\n",
"m2 = torch.nn.SiLU()\n",
"results = profile(input=torch.randn(16, 3, 640, 640), ops=[m1, m2], n=100)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BSgFCAcMbk1R"
},
"source": [
"# VOC\n",
"for b, m in zip([64, 64, 64, 32, 16], [f'yolov5{x}' for x in 'nsmlx']): # batch, model\n",
" !python train.py --batch {b} --weights {m}.pt --data VOC.yaml --epochs 50 --img 512 --hyp hyp.VOC.yaml --project VOC --name {m} --cache"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Classification train\n",
"for m in [*(f'yolov5{x}-cls.pt' for x in 'nsmlx'), 'resnet50.pt', 'resnet101.pt', 'efficientnet_b0.pt', 'efficientnet_b1.pt']:\n",
" for d in 'mnist', 'fashion-mnist', 'cifar10', 'cifar100', 'imagenette160', 'imagenette320', 'imagenette', 'imagewoof160', 'imagewoof320', 'imagewoof':\n",
" !python classify/train.py --model {m} --data {d} --epochs 10 --project YOLOv5-cls --name {m}-{d}"
],
"metadata": {
"id": "UWGH7H6yakVl"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Classification val\n",
"!bash data/scripts/get_imagenet.sh --val # download ImageNet val split (6.3G - 50000 images)\n",
"!python classify/val.py --weights yolov5m-cls.pt --data ../datasets/imagenet --img 224 # validate"
],
"metadata": {
"id": "yYgOiFNHZx-1"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Validate on COCO test. Zip results.json and submit to eval server at https://competitions.codalab.org/competitions/20794\n",
"!bash data/scripts/get_coco.sh --test # download COCO test-dev2017 (7G - 40000 images, test 20000)\n",
"!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half --task test"
],
"metadata": {
"id": "aq4DPWGu0Bl1"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "VTRwsvA9u7ln"
},
"source": [
"# TensorRT \n",
"!pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # install\n",
"!python export.py --weights yolov5s.pt --include engine --imgsz 640 --device 0 # export\n",
"!python detect.py --weights yolov5s.engine --imgsz 640 --device 0 # inference"
],
"execution_count": null,
"outputs": []
}
]
}
}

0 comments on commit 8236d88

Please sign in to comment.