@@ -497,20 +497,32 @@ def url2file(url):
497
497
return file
498
498
499
499
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 ):
501
501
# Multi-threaded file download and unzip function, used in data.yaml for autodownload
502
502
def download_one (url , dir ):
503
503
# Download 1 file
504
+ success = True
504
505
f = dir / Path (url ).name # filename
505
506
if Path (url ).is_file (): # exists in current path
506
507
Path (url ).rename (f ) # move to dir
507
508
elif not f .exists ():
508
509
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' ):
514
526
LOGGER .info (f'Unzipping { f } ...' )
515
527
if f .suffix == '.zip' :
516
528
ZipFile (f ).extractall (path = dir ) # unzip
0 commit comments