From c45e65e706e63f04d87300c5fd456e704d086c3b Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 3 Nov 2021 21:31:23 +0100 Subject: [PATCH 01/10] Update __init__.py --- utils/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/utils/__init__.py b/utils/__init__.py index e69de29bb2d1..a0b6effa729f 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -0,0 +1,13 @@ +# YOLOv5 🚀 by Ultralytics, GPL-3.0 license +""" +utils/ initialization +""" + + +def init_notebook(): + # For use in YOLOv5 notebooks + from IPython.display import Image, clear_output # to display images + from utils.torch_utils import select_device # YOLOv5 imports + + clear_output() + return select_device(), Image From 577f60efb5de4652a0d942e530a3b182939875b8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 9 Nov 2021 17:09:11 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- utils/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/__init__.py b/utils/__init__.py index a0b6effa729f..6e294f036d41 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -7,6 +7,7 @@ def init_notebook(): # For use in YOLOv5 notebooks from IPython.display import Image, clear_output # to display images + from utils.torch_utils import select_device # YOLOv5 imports clear_output() From 2e1c34dd5a44ea74f9c31d583a4be445893bf845 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 9 Nov 2021 20:38:28 +0100 Subject: [PATCH 03/10] notebook_init --- utils/__init__.py | 7 ++++--- utils/torch_utils.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/utils/__init__.py b/utils/__init__.py index 6e294f036d41..551948ed4b45 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -4,11 +4,12 @@ """ -def init_notebook(): +def notebook_init(): # For use in YOLOv5 notebooks - from IPython.display import Image, clear_output # to display images + from IPython.display import clear_output # to display images from utils.torch_utils import select_device # YOLOv5 imports clear_output() - return select_device(), Image + device, string = select_device() + print(f'Setup done! Using {string.rstrip()}') diff --git a/utils/torch_utils.py b/utils/torch_utils.py index b65b69fe1559..35de648fa9d3 100644 --- a/utils/torch_utils.py +++ b/utils/torch_utils.py @@ -6,7 +6,6 @@ import datetime import math import os -import platform import subprocess import time from contextlib import contextmanager @@ -18,7 +17,7 @@ import torch.nn as nn import torch.nn.functional as F -from utils.general import LOGGER +from utils.general import LOGGER, emojis try: import thop # for FLOPs computation @@ -53,7 +52,7 @@ def git_describe(path=Path(__file__).parent): # path must be a directory return '' # not a git repository -def select_device(device='', batch_size=None): +def select_device(device='', batch_size=None, verbose=True): # device = 'cpu' or '0' or '0,1,2,3' s = f'YOLOv5 🚀 {git_describe() or date_modified()} torch {torch.__version__} ' # string device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0' @@ -77,8 +76,13 @@ def select_device(device='', batch_size=None): else: s += 'CPU\n' - LOGGER.info(s.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else s) # emoji-safe - return torch.device('cuda:0' if cuda else 'cpu') + s = emojis(s) # emoji-safe + device = torch.device('cuda:0' if cuda else 'cpu') + if verbose: + LOGGER.info(s) + return device + else: + return device, s def time_sync(): From 87945351d2938082b718145a91318c16a49e4199 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 9 Nov 2021 20:45:24 +0100 Subject: [PATCH 04/10] notebook_init --- utils/__init__.py | 4 ++-- utils/torch_utils.py | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/utils/__init__.py b/utils/__init__.py index 551948ed4b45..5455c8f45084 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -11,5 +11,5 @@ def notebook_init(): from utils.torch_utils import select_device # YOLOv5 imports clear_output() - device, string = select_device() - print(f'Setup done! Using {string.rstrip()}') + select_device(newline=False) + print(f'Setup complete.') diff --git a/utils/torch_utils.py b/utils/torch_utils.py index 35de648fa9d3..16289104eb48 100644 --- a/utils/torch_utils.py +++ b/utils/torch_utils.py @@ -6,6 +6,7 @@ import datetime import math import os +import platform import subprocess import time from contextlib import contextmanager @@ -17,7 +18,7 @@ import torch.nn as nn import torch.nn.functional as F -from utils.general import LOGGER, emojis +from utils.general import LOGGER try: import thop # for FLOPs computation @@ -52,7 +53,7 @@ def git_describe(path=Path(__file__).parent): # path must be a directory return '' # not a git repository -def select_device(device='', batch_size=None, verbose=True): +def select_device(device='', batch_size=None, newline=True): # device = 'cpu' or '0' or '0,1,2,3' s = f'YOLOv5 🚀 {git_describe() or date_modified()} torch {torch.__version__} ' # string device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0' @@ -76,13 +77,10 @@ def select_device(device='', batch_size=None, verbose=True): else: s += 'CPU\n' - s = emojis(s) # emoji-safe - device = torch.device('cuda:0' if cuda else 'cpu') - if verbose: - LOGGER.info(s) - return device - else: - return device, s + if not newline: + s = s.rstrip() + LOGGER.info(s.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else s) # emoji-safe + return torch.device('cuda:0' if cuda else 'cpu') def time_sync(): From 00a11f8a3b948b31efeef4ad482ef36bc69e2687 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 9 Nov 2021 21:00:48 +0100 Subject: [PATCH 05/10] notebook_init --- utils/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/__init__.py b/utils/__init__.py index 5455c8f45084..eb621c140040 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -8,8 +8,9 @@ def notebook_init(): # For use in YOLOv5 notebooks from IPython.display import clear_output # to display images + from utils.general import emojis from utils.torch_utils import select_device # YOLOv5 imports clear_output() select_device(newline=False) - print(f'Setup complete.') + print(emojis('Setup complete ✅')) From 247e901158f05a985442e8181feaff27f749dd84 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 9 Nov 2021 22:33:30 +0100 Subject: [PATCH 06/10] notebook_init --- utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/__init__.py b/utils/__init__.py index eb621c140040..69f81af8bfbe 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -6,8 +6,8 @@ def notebook_init(): # For use in YOLOv5 notebooks + print('Checking setup...') from IPython.display import clear_output # to display images - from utils.general import emojis from utils.torch_utils import select_device # YOLOv5 imports From a4c41b1f031d165e82ea0acd7fafabe5e8c2c404 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 9 Nov 2021 21:33:43 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- utils/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/__init__.py b/utils/__init__.py index 69f81af8bfbe..e1296dce5e6c 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -8,6 +8,7 @@ def notebook_init(): # For use in YOLOv5 notebooks print('Checking setup...') from IPython.display import clear_output # to display images + from utils.general import emojis from utils.torch_utils import select_device # YOLOv5 imports From 063ab6af1844fdf1ee10601059900657596e0a2a Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 9 Nov 2021 22:47:43 +0100 Subject: [PATCH 08/10] notebook_init --- utils/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/utils/__init__.py b/utils/__init__.py index e1296dce5e6c..2b0c896364a2 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,17 +1,18 @@ # YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ -utils/ initialization +utils/initialization """ def notebook_init(): - # For use in YOLOv5 notebooks + # For YOLOv5 notebooks print('Checking setup...') - from IPython.display import clear_output # to display images + from IPython import display # to display images and clear console output from utils.general import emojis from utils.torch_utils import select_device # YOLOv5 imports - clear_output() + display.clear_output() select_device(newline=False) print(emojis('Setup complete ✅')) + return display From 39acb1dd08a4b20444bdfff5ac0bd8220c90daca Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 9 Nov 2021 22:56:57 +0100 Subject: [PATCH 09/10] Created using Colaboratory --- tutorial.ipynb | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tutorial.ipynb b/tutorial.ipynb index b013fe694ba4..f2be33ed15f3 100644 --- a/tutorial.ipynb +++ b/tutorial.ipynb @@ -368,7 +368,7 @@ "colab_type": "text" }, "source": [ - "\"Open" + "\"Open" ] }, { @@ -402,26 +402,24 @@ "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "e2e839d5-d6fc-409c-e44c-0b0b6aa9319d" + "outputId": "3809e5a9-dd41-4577-fe62-5531abf7cca2" }, "source": [ - "!git clone https://github.com/ultralytics/yolov5 # clone repo\n", + "!git clone https://github.com/ultralytics/yolov5 # clone\n", "%cd yolov5\n", - "%pip install -qr requirements.txt # install dependencies\n", + "%pip install -qr requirements.txt # install\n", "\n", - "import torch\n", - "from IPython.display import Image, clear_output # to display images\n", - "\n", - "clear_output()\n", - "print(f\"Setup complete. Using torch {torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})\")" + "from yolov5 import utils\n", + "display = utils.notebook_init() # checks" ], - "execution_count": 11, + "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ - "Setup complete. Using torch 1.10.0+cu102 (Tesla V100-SXM2-16GB)\n" + "YOLOv5 🚀 v6.0-48-g84a8099 torch 1.10.0+cu102 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)\n", + "Setup complete ✅\n" ] } ] @@ -458,9 +456,9 @@ }, "source": [ "!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images\n", - "Image(filename='runs/detect/exp/zidane.jpg', width=600)" + "display.Image(filename='runs/detect/exp/zidane.jpg', width=600)" ], - "execution_count": 17, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -537,7 +535,7 @@ "torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017val.zip', 'tmp.zip')\n", "!unzip -q tmp.zip -d ../datasets && rm tmp.zip" ], - "execution_count": 18, + "execution_count": null, "outputs": [ { "output_type": "display_data", @@ -568,7 +566,7 @@ "# Run YOLOv5x on COCO val\n", "!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half" ], - "execution_count": 19, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -726,7 +724,7 @@ "# Train YOLOv5s on COCO128 for 3 epochs\n", "!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache" ], - "execution_count": 24, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1085,4 +1083,4 @@ "outputs": [] } ] -} +} \ No newline at end of file From 83961077c80ef26c8b73cd9cf6a368ded2882a0b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 9 Nov 2021 21:57:11 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorial.ipynb b/tutorial.ipynb index f2be33ed15f3..7763a26066e2 100644 --- a/tutorial.ipynb +++ b/tutorial.ipynb @@ -1083,4 +1083,4 @@ "outputs": [] } ] -} \ No newline at end of file +}