Marc glenn-jocher commited on
Commit
1f92422
1 Parent(s): 4949401

Github release assets model autodownload (#711)

Browse files

* assets autodownload

* Update google_utils.py

* Update google_utils.py

* Update google_utils.py

* Update google_utils.py

* Update google_utils.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

Files changed (1) hide show
  1. utils/google_utils.py +36 -26
utils/google_utils.py CHANGED
@@ -6,40 +6,50 @@ import os
6
  import platform
7
  import time
8
  from pathlib import Path
 
9
 
10
 
11
  def attempt_download(weights):
12
  # Attempt to download pretrained weights if not found locally
13
  weights = weights.strip().replace("'", '')
14
- msg = weights + ' missing, try downloading from https://drive.google.com/drive/folders/1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J'
15
-
16
- r = 1 # return
17
- if len(weights) > 0 and not os.path.isfile(weights):
18
- d = {'yolov3-spp.pt': '1mM67oNw4fZoIOL1c8M3hHmj66d8e-ni_', # yolov3-spp.yaml
19
- 'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO', # yolov5s.yaml
20
- 'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr', # yolov5m.yaml
21
- 'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV', # yolov5l.yaml
22
- 'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS', # yolov5x.yaml
23
- }
24
-
25
- file = Path(weights).name
26
- if file in d:
27
- r = gdrive_download(id=d[file], name=weights)
28
-
29
- if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
30
- os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
31
- s = 'curl -L -o %s "storage.googleapis.com/ultralytics/yolov5/ckpt/%s"' % (weights, file)
32
- r = os.system(s) # execute, capture return values
33
-
34
- # Error check
35
- if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
 
 
 
 
 
 
 
 
36
  os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
37
- raise Exception(msg)
 
 
38
 
39
 
40
  def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'):
41
- # Downloads a file from Google Drive, accepting presented query
42
- # from utils.google_utils import *; gdrive_download()
43
  t = time.time()
44
 
45
  print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='')
@@ -53,7 +63,7 @@ def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'):
53
  s = 'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm=%s&id=%s" -o %s' % (get_token(), id, name)
54
  else: # small file
55
  s = 'curl -s -L -o %s "drive.google.com/uc?export=download&id=%s"' % (name, id)
56
- r = os.system(s) # execute, capture return values
57
  os.remove('cookie') if os.path.exists('cookie') else None
58
 
59
  # Error check
 
6
  import platform
7
  import time
8
  from pathlib import Path
9
+ import torch
10
 
11
 
12
  def attempt_download(weights):
13
  # Attempt to download pretrained weights if not found locally
14
  weights = weights.strip().replace("'", '')
15
+ file = Path(weights).name
16
+
17
+ msg = weights + ' missing, try downloading from https://github.com/ultralytics/yolov5/releases/'
18
+ models = ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt'] # available models
19
+
20
+ if file in models and not os.path.isfile(weights):
21
+ # Google Drive
22
+ # d = {'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO',
23
+ # 'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr',
24
+ # 'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV',
25
+ # 'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS'}
26
+ # r = gdrive_download(id=d[file], name=weights) if file in d else 1
27
+ # if r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6: # check
28
+ # return
29
+
30
+ try: # GitHub
31
+ url = 'https://github.com/ultralytics/yolov5/releases/download/v2.0/' + file
32
+ print('Downloading %s to %s...' % (url, weights))
33
+ if platform.system() == 'Darwin': # avoid MacOS python requests certificate error
34
+ r = os.system('curl -L %s -o %s' % (url, weights))
35
+ else:
36
+ torch.hub.download_url_to_file(url, weights)
37
+ assert os.path.exists(weights) and os.path.getsize(weights) > 1E6 # check
38
+ except Exception as e: # GCP
39
+ print('Download error: %s' % e)
40
+ url = 'https://storage.googleapis.com/ultralytics/yolov5/ckpt/' + file
41
+ print('Downloading %s to %s...' % (url, weights))
42
+ r = os.system('curl -L %s -o %s' % (url, weights)) # torch.hub.download_url_to_file(url, weights)
43
+ finally:
44
+ if not (os.path.exists(weights) and os.path.getsize(weights) > 1E6): # check
45
  os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
46
+ print('ERROR: Download failure: %s' % msg)
47
+ print('')
48
+ return
49
 
50
 
51
  def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'):
52
+ # Downloads a file from Google Drive. from utils.google_utils import *; gdrive_download()
 
53
  t = time.time()
54
 
55
  print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='')
 
63
  s = 'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm=%s&id=%s" -o %s' % (get_token(), id, name)
64
  else: # small file
65
  s = 'curl -s -L -o %s "drive.google.com/uc?export=download&id=%s"' % (name, id)
66
+ r = os.system(s) # execute, capture return
67
  os.remove('cookie') if os.path.exists('cookie') else None
68
 
69
  # Error check