glenn-jocher commited on
Commit
32661f7
1 Parent(s): f735458

Add `retry=3` to `download()` (#7313)

Browse files

* Add `retry=3` to `download()`

* Update general.py

* Update general.py

* Update general.py

* Update VOC.yaml

* Update VisDrone.yaml

Files changed (3) hide show
  1. data/VOC.yaml +1 -1
  2. data/VisDrone.yaml +1 -1
  3. utils/general.py +18 -6
data/VOC.yaml CHANGED
@@ -62,7 +62,7 @@ download: |
62
  urls = [url + 'VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images
63
  url + 'VOCtest_06-Nov-2007.zip', # 438MB, 4953 images
64
  url + 'VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images
65
- download(urls, dir=dir / 'images', delete=False, threads=3)
66
 
67
  # Convert
68
  path = dir / f'images/VOCdevkit'
 
62
  urls = [url + 'VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images
63
  url + 'VOCtest_06-Nov-2007.zip', # 438MB, 4953 images
64
  url + 'VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images
65
+ download(urls, dir=dir / 'images', delete=False, curl=True, threads=3)
66
 
67
  # Convert
68
  path = dir / f'images/VOCdevkit'
data/VisDrone.yaml CHANGED
@@ -54,7 +54,7 @@ download: |
54
  'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-val.zip',
55
  'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-dev.zip',
56
  'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-challenge.zip']
57
- download(urls, dir=dir, threads=4)
58
 
59
  # Convert
60
  for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':
 
54
  'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-val.zip',
55
  'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-dev.zip',
56
  'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-challenge.zip']
57
+ download(urls, dir=dir, curl=True, threads=4)
58
 
59
  # Convert
60
  for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':
utils/general.py CHANGED
@@ -497,20 +497,32 @@ def url2file(url):
497
  return file
498
 
499
 
500
- def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1):
501
  # Multi-threaded file download and unzip function, used in data.yaml for autodownload
502
  def download_one(url, dir):
503
  # Download 1 file
 
504
  f = dir / Path(url).name # filename
505
  if Path(url).is_file(): # exists in current path
506
  Path(url).rename(f) # move to dir
507
  elif not f.exists():
508
  LOGGER.info(f'Downloading {url} to {f}...')
509
- if curl:
510
- os.system(f"curl -L '{url}' -o '{f}' --retry 9 -C -") # curl download, retry and resume on fail
511
- else:
512
- torch.hub.download_url_to_file(url, f, progress=threads == 1) # torch download
513
- if unzip and f.suffix in ('.zip', '.gz'):
 
 
 
 
 
 
 
 
 
 
 
514
  LOGGER.info(f'Unzipping {f}...')
515
  if f.suffix == '.zip':
516
  ZipFile(f).extractall(path=dir) # unzip
 
497
  return file
498
 
499
 
500
+ def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1, retry=3):
501
  # Multi-threaded file download and unzip function, used in data.yaml for autodownload
502
  def download_one(url, dir):
503
  # Download 1 file
504
+ success = True
505
  f = dir / Path(url).name # filename
506
  if Path(url).is_file(): # exists in current path
507
  Path(url).rename(f) # move to dir
508
  elif not f.exists():
509
  LOGGER.info(f'Downloading {url} to {f}...')
510
+ for i in range(retry + 1):
511
+ if curl:
512
+ s = 'sS' if threads > 1 else '' # silent
513
+ r = os.system(f"curl -{s}L '{url}' -o '{f}' --retry 9 -C -") # curl download
514
+ success = r == 0
515
+ else:
516
+ torch.hub.download_url_to_file(url, f, progress=threads == 1) # torch download
517
+ success = f.is_file()
518
+ if success:
519
+ break
520
+ elif i < retry:
521
+ LOGGER.warning(f'Download failure, retrying {i + 1}/{retry} {url}...')
522
+ else:
523
+ LOGGER.warning(f'Failed to download {url}...')
524
+
525
+ if unzip and success and f.suffix in ('.zip', '.gz'):
526
  LOGGER.info(f'Unzipping {f}...')
527
  if f.suffix == '.zip':
528
  ZipFile(f).extractall(path=dir) # unzip