download_model_urls.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import asyncio
  2. import sys
  3. from pathlib import Path
  4. from time import perf_counter
  5. from urllib.parse import urlsplit
  6. import aiofiles
  7. import aiohttp
  8. from torchvision import models
  9. from tqdm.asyncio import tqdm
  10. async def main(download_root):
  11. download_root.mkdir(parents=True, exist_ok=True)
  12. urls = {weight.url for name in models.list_models() for weight in iter(models.get_model_weights(name))}
  13. async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=None)) as session:
  14. await tqdm.gather(*[download(download_root, session, url) for url in urls])
  15. async def download(download_root, session, url):
  16. response = await session.get(url, params=dict(source="ci"))
  17. assert response.ok
  18. file_name = Path(urlsplit(url).path).name
  19. async with aiofiles.open(download_root / file_name, "wb") as f:
  20. async for data in response.content.iter_any():
  21. await f.write(data)
  22. if __name__ == "__main__":
  23. download_root = (
  24. (Path(sys.argv[1]) if len(sys.argv) > 1 else Path("~/.cache/torch/hub/checkpoints")).expanduser().resolve()
  25. )
  26. print(f"Downloading model weights to {download_root}")
  27. start = perf_counter()
  28. asyncio.get_event_loop().run_until_complete(main(download_root))
  29. stop = perf_counter()
  30. minutes, seconds = divmod(stop - start, 60)
  31. print(f"Download took {minutes:2.0f}m {seconds:2.0f}s")