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.

123 line
4.9KB

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