Skip to content

Commit b8ad5b0

Browse files
authored
Add retry=3 to download() (ultralytics#7313)
* Add `retry=3` to `download()` * Update general.py * Update general.py * Update general.py * Update VOC.yaml * Update VisDrone.yaml
1 parent 0692e52 commit b8ad5b0

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

data/VOC.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ download: |
6262
urls = [url + 'VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images
6363
url + 'VOCtest_06-Nov-2007.zip', # 438MB, 4953 images
6464
url + 'VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images
65-
download(urls, dir=dir / 'images', delete=False, threads=3)
65+
download(urls, dir=dir / 'images', delete=False, curl=True, threads=3)
6666
6767
# Convert
6868
path = dir / f'images/VOCdevkit'

data/VisDrone.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ download: |
5454
'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-val.zip',
5555
'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-dev.zip',
5656
'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-challenge.zip']
57-
download(urls, dir=dir, threads=4)
57+
download(urls, dir=dir, curl=True, threads=4)
5858
5959
# Convert
6060
for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':

utils/general.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,20 +497,32 @@ def url2file(url):
497497
return file
498498

499499

500-
def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1):
500+
def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1, retry=3):
501501
# Multi-threaded file download and unzip function, used in data.yaml for autodownload
502502
def download_one(url, dir):
503503
# Download 1 file
504+
success = True
504505
f = dir / Path(url).name # filename
505506
if Path(url).is_file(): # exists in current path
506507
Path(url).rename(f) # move to dir
507508
elif not f.exists():
508509
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'):
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'):
514526
LOGGER.info(f'Unzipping {f}...')
515527
if f.suffix == '.zip':
516528
ZipFile(f).extractall(path=dir) # unzip

0 commit comments

Comments
 (0)