File size: 3,733 Bytes
8edefd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | """Download multiple datasets from Hugging Face and COCO."""
import argparse
import os
import sys
from huggingface_hub import snapshot_download
COCO_VAL_URL = "http://images.cocodataset.org/zips/val2014.zip"
def download_huggingface_dataset(repo_id: str, output_dir: str, use_auth_token: bool) -> str:
os.makedirs(output_dir, exist_ok=True)
print(f"Downloading Hugging Face dataset {repo_id} to '{output_dir}'...")
local_dir = snapshot_download(
repo_id=repo_id,
repo_type="dataset",
local_dir=output_dir,
use_auth_token=use_auth_token,
)
print(f"Finished: {repo_id} -> {local_dir}\n")
return local_dir
def download_file(url: str, output_path: str) -> None:
try:
import requests
except ImportError:
import urllib.request
print("requests not found, falling back to urllib.request")
urllib.request.urlretrieve(url, output_path)
return
print(f"Downloading file from {url} to '{output_path}'...")
with requests.get(url, stream=True) as response:
response.raise_for_status()
total_size = int(response.headers.get("content-length", 0))
downloaded = 0
chunk_size = 8192
with open(output_path, "wb") as f:
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
downloaded += len(chunk)
if total_size:
percent = downloaded * 100 / total_size
print(f"\rDownloaded {downloaded}/{total_size} bytes ({percent:.1f}%)", end="")
if total_size:
print()
print(f"Finished: {output_path}\n")
def main():
parser = argparse.ArgumentParser(description="Download VGMED, Medical Multimodal Evaluation Data, and COCO 2014 val images.")
parser.add_argument(
"--output-dir",
default="/home/jinhua/ExplainableVQA/benchmarks",
help="Base output directory for all downloads. Default: downloads",
)
parser.add_argument(
"--use-auth-token",
action="store_true",
help="Use HF authentication token from HUGGINGFACE_HUB_TOKEN environment variable if required.",
)
parser.add_argument(
"--skip-vgmed",
action="store_true",
help="Skip downloading guimeng-liu/VGMED.",
)
parser.add_argument(
"--skip-med-eval",
action="store_true",
help="Skip downloading FreedomIntelligence/Medical_Multimodal_Evaluation_Data.",
)
parser.add_argument(
"--skip-coco",
action="store_true",
help="Skip downloading COCO 2014 validation images.",
)
parser.add_argument(
"--coco-url",
default=COCO_VAL_URL,
help="COCO 2014 val images download URL. Default is the official val2014 zip URL.",
)
args = parser.parse_args()
base_dir = os.path.abspath(args.output_dir)
os.makedirs(base_dir, exist_ok=True)
if not args.skip_vgmed:
download_huggingface_dataset(
repo_id="guimeng-liu/VGMED",
output_dir=os.path.join(base_dir, "VGMED"),
use_auth_token=args.use_auth_token,
)
if not args.skip_med_eval:
download_huggingface_dataset(
repo_id="FreedomIntelligence/Medical_Multimodal_Evaluation_Data",
output_dir=os.path.join(base_dir, "Medical_Multimodal_Evaluation_Data"),
use_auth_token=args.use_auth_token,
)
if not args.skip_coco:
coco_target = os.path.join(base_dir, "val2014.zip")
download_file(args.coco_url, coco_target)
print("All requested downloads completed.")
if __name__ == "__main__":
main()
|