glenn-jocher pre-commit-ci[bot] commited on
Commit
7ebb5e5
β€’
1 Parent(s): 7207fe9

Add `notebook_init()` to utils/__init__.py (#5488)

Browse files

* Update __init__.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* notebook_init

* notebook_init

* notebook_init

* notebook_init

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* notebook_init

* Created using Colaboratory

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Files changed (3) hide show
  1. tutorial.ipynb +14 -16
  2. utils/__init__.py +18 -0
  3. utils/torch_utils.py +3 -1
tutorial.ipynb CHANGED
@@ -368,7 +368,7 @@
368
  "colab_type": "text"
369
  },
370
  "source": [
371
- "<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>"
372
  ]
373
  },
374
  {
@@ -402,26 +402,24 @@
402
  "colab": {
403
  "base_uri": "https://localhost:8080/"
404
  },
405
- "outputId": "e2e839d5-d6fc-409c-e44c-0b0b6aa9319d"
406
  },
407
  "source": [
408
- "!git clone https://github.com/ultralytics/yolov5 # clone repo\n",
409
  "%cd yolov5\n",
410
- "%pip install -qr requirements.txt # install dependencies\n",
411
  "\n",
412
- "import torch\n",
413
- "from IPython.display import Image, clear_output # to display images\n",
414
- "\n",
415
- "clear_output()\n",
416
- "print(f\"Setup complete. Using torch {torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})\")"
417
  ],
418
- "execution_count": 11,
419
  "outputs": [
420
  {
421
  "output_type": "stream",
422
  "name": "stdout",
423
  "text": [
424
- "Setup complete. Using torch 1.10.0+cu102 (Tesla V100-SXM2-16GB)\n"
 
425
  ]
426
  }
427
  ]
@@ -458,9 +456,9 @@
458
  },
459
  "source": [
460
  "!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images\n",
461
- "Image(filename='runs/detect/exp/zidane.jpg', width=600)"
462
  ],
463
- "execution_count": 17,
464
  "outputs": [
465
  {
466
  "output_type": "stream",
@@ -537,7 +535,7 @@
537
  "torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017val.zip', 'tmp.zip')\n",
538
  "!unzip -q tmp.zip -d ../datasets && rm tmp.zip"
539
  ],
540
- "execution_count": 18,
541
  "outputs": [
542
  {
543
  "output_type": "display_data",
@@ -568,7 +566,7 @@
568
  "# Run YOLOv5x on COCO val\n",
569
  "!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half"
570
  ],
571
- "execution_count": 19,
572
  "outputs": [
573
  {
574
  "output_type": "stream",
@@ -726,7 +724,7 @@
726
  "# Train YOLOv5s on COCO128 for 3 epochs\n",
727
  "!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache"
728
  ],
729
- "execution_count": 24,
730
  "outputs": [
731
  {
732
  "output_type": "stream",
 
368
  "colab_type": "text"
369
  },
370
  "source": [
371
+ "<a href=\"https://colab.research.google.com/github/ultralytics/yolov5/blob/update%2Fnotebook/tutorial.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
372
  ]
373
  },
374
  {
 
402
  "colab": {
403
  "base_uri": "https://localhost:8080/"
404
  },
405
+ "outputId": "3809e5a9-dd41-4577-fe62-5531abf7cca2"
406
  },
407
  "source": [
408
+ "!git clone https://github.com/ultralytics/yolov5 # clone\n",
409
  "%cd yolov5\n",
410
+ "%pip install -qr requirements.txt # install\n",
411
  "\n",
412
+ "from yolov5 import utils\n",
413
+ "display = utils.notebook_init() # checks"
 
 
 
414
  ],
415
+ "execution_count": 2,
416
  "outputs": [
417
  {
418
  "output_type": "stream",
419
  "name": "stdout",
420
  "text": [
421
+ "YOLOv5 πŸš€ v6.0-48-g84a8099 torch 1.10.0+cu102 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)\n",
422
+ "Setup complete βœ…\n"
423
  ]
424
  }
425
  ]
 
456
  },
457
  "source": [
458
  "!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images\n",
459
+ "display.Image(filename='runs/detect/exp/zidane.jpg', width=600)"
460
  ],
461
+ "execution_count": null,
462
  "outputs": [
463
  {
464
  "output_type": "stream",
 
535
  "torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017val.zip', 'tmp.zip')\n",
536
  "!unzip -q tmp.zip -d ../datasets && rm tmp.zip"
537
  ],
538
+ "execution_count": null,
539
  "outputs": [
540
  {
541
  "output_type": "display_data",
 
566
  "# Run YOLOv5x on COCO val\n",
567
  "!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half"
568
  ],
569
+ "execution_count": null,
570
  "outputs": [
571
  {
572
  "output_type": "stream",
 
724
  "# Train YOLOv5s on COCO128 for 3 epochs\n",
725
  "!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache"
726
  ],
727
+ "execution_count": null,
728
  "outputs": [
729
  {
730
  "output_type": "stream",
utils/__init__.py CHANGED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # YOLOv5 πŸš€ by Ultralytics, GPL-3.0 license
2
+ """
3
+ utils/initialization
4
+ """
5
+
6
+
7
+ def notebook_init():
8
+ # For YOLOv5 notebooks
9
+ print('Checking setup...')
10
+ from IPython import display # to display images and clear console output
11
+
12
+ from utils.general import emojis
13
+ from utils.torch_utils import select_device # YOLOv5 imports
14
+
15
+ display.clear_output()
16
+ select_device(newline=False)
17
+ print(emojis('Setup complete βœ…'))
18
+ return display
utils/torch_utils.py CHANGED
@@ -53,7 +53,7 @@ def git_describe(path=Path(__file__).parent): # path must be a directory
53
  return '' # not a git repository
54
 
55
 
56
- def select_device(device='', batch_size=None):
57
  # device = 'cpu' or '0' or '0,1,2,3'
58
  s = f'YOLOv5 πŸš€ {git_describe() or date_modified()} torch {torch.__version__} ' # string
59
  device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
@@ -77,6 +77,8 @@ def select_device(device='', batch_size=None):
77
  else:
78
  s += 'CPU\n'
79
 
 
 
80
  LOGGER.info(s.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else s) # emoji-safe
81
  return torch.device('cuda:0' if cuda else 'cpu')
82
 
 
53
  return '' # not a git repository
54
 
55
 
56
+ def select_device(device='', batch_size=None, newline=True):
57
  # device = 'cpu' or '0' or '0,1,2,3'
58
  s = f'YOLOv5 πŸš€ {git_describe() or date_modified()} torch {torch.__version__} ' # string
59
  device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
 
77
  else:
78
  s += 'CPU\n'
79
 
80
+ if not newline:
81
+ s = s.rstrip()
82
  LOGGER.info(s.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else s) # emoji-safe
83
  return torch.device('cuda:0' if cuda else 'cpu')
84