io.py 631 B

12345678910111213141516171819202122
  1. from .imports import *
  2. from .torch_imports import *
  3. import gzip
  4. from urllib.request import urlretrieve
  5. from tqdm import tqdm
  6. class TqdmUpTo(tqdm):
  7. def update_to(self, b=1, bsize=1, tsize=None):
  8. if tsize is not None: self.total = tsize
  9. self.update(b * bsize - self.n)
  10. def get_data(url, filename):
  11. if not os.path.exists(filename):
  12. dirname = os.path.dirname(filename)
  13. if not os.path.exists(dirname):
  14. os.makedirs(dirname)
  15. with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t:
  16. urlretrieve(url, filename, reporthook=t.update_to)