You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.8KB

  1. # Google utils: https://cloud.google.com/storage/docs/reference/libraries
  2. import os
  3. import platform
  4. import subprocess
  5. import time
  6. from pathlib import Path
  7. import torch
  8. def gsutil_getsize(url=''):
  9. # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
  10. s = subprocess.check_output('gsutil du %s' % url, shell=True).decode('utf-8')
  11. return eval(s.split(' ')[0]) if len(s) else 0 # bytes
  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. msg = weights + ' missing, try downloading from https://github.com/ultralytics/yolov5/releases/'
  17. models = ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt'] # available models
  18. if file in models and not os.path.isfile(weights):
  19. # Google Drive
  20. # d = {'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO',
  21. # 'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr',
  22. # 'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV',
  23. # 'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS'}
  24. # r = gdrive_download(id=d[file], name=weights) if file in d else 1
  25. # if r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6: # check
  26. # return
  27. try: # GitHub
  28. url = 'https://github.com/ultralytics/yolov5/releases/download/v3.1/' + file
  29. print('Downloading %s to %s...' % (url, weights))
  30. torch.hub.download_url_to_file(url, weights)
  31. assert os.path.exists(weights) and os.path.getsize(weights) > 1E6 # check
  32. except Exception as e: # GCP
  33. print('Download error: %s' % e)
  34. url = 'https://storage.googleapis.com/ultralytics/yolov5/ckpt/' + file
  35. print('Downloading %s to %s...' % (url, weights))
  36. r = os.system('curl -L %s -o %s' % (url, weights)) # torch.hub.download_url_to_file(url, weights)
  37. finally:
  38. if not (os.path.exists(weights) and os.path.getsize(weights) > 1E6): # check
  39. os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
  40. print('ERROR: Download failure: %s' % msg)
  41. print('')
  42. return
  43. def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'):
  44. # Downloads a file from Google Drive. from utils.google_utils import *; gdrive_download()
  45. t = time.time()
  46. print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='')
  47. os.remove(name) if os.path.exists(name) else None # remove existing
  48. os.remove('cookie') if os.path.exists('cookie') else None
  49. # Attempt file download
  50. out = "NUL" if platform.system() == "Windows" else "/dev/null"
  51. os.system('curl -c ./cookie -s -L "drive.google.com/uc?export=download&id=%s" > %s ' % (id, out))
  52. if os.path.exists('cookie'): # large file
  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
  57. os.remove('cookie') if os.path.exists('cookie') else None
  58. # Error check
  59. if r != 0:
  60. os.remove(name) if os.path.exists(name) else None # remove partial
  61. print('Download error ') # raise Exception('Download error')
  62. return r
  63. # Unzip if archive
  64. if name.endswith('.zip'):
  65. print('unzipping... ', end='')
  66. os.system('unzip -q %s' % name) # unzip
  67. os.remove(name) # remove zip to free space
  68. print('Done (%.1fs)' % (time.time() - t))
  69. return r
  70. def get_token(cookie="./cookie"):
  71. with open(cookie) as f:
  72. for line in f:
  73. if "download" in line:
  74. return line.split()[-1]
  75. return ""
  76. # def upload_blob(bucket_name, source_file_name, destination_blob_name):
  77. # # Uploads a file to a bucket
  78. # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
  79. #
  80. # storage_client = storage.Client()
  81. # bucket = storage_client.get_bucket(bucket_name)
  82. # blob = bucket.blob(destination_blob_name)
  83. #
  84. # blob.upload_from_filename(source_file_name)
  85. #
  86. # print('File {} uploaded to {}.'.format(
  87. # source_file_name,
  88. # destination_blob_name))
  89. #
  90. #
  91. # def download_blob(bucket_name, source_blob_name, destination_file_name):
  92. # # Uploads a blob from a bucket
  93. # storage_client = storage.Client()
  94. # bucket = storage_client.get_bucket(bucket_name)
  95. # blob = bucket.blob(source_blob_name)
  96. #
  97. # blob.download_to_filename(destination_file_name)
  98. #
  99. # print('Blob {} downloaded to {}.'.format(
  100. # source_blob_name,
  101. # destination_file_name))