|
| 1 | +import argparse |
| 2 | +from itertools import repeat |
| 3 | +from multiprocessing.pool import ThreadPool |
| 4 | +from pathlib import Path |
| 5 | +from tarfile import TarFile |
| 6 | +from zipfile import ZipFile |
| 7 | + |
| 8 | +import torch |
| 9 | + |
| 10 | + |
| 11 | +def parse_args(): |
| 12 | + parser = argparse.ArgumentParser( |
| 13 | + description='Download datasets for training') |
| 14 | + parser.add_argument( |
| 15 | + '--dataset-name', type=str, help='dataset name', default='coco2017') |
| 16 | + parser.add_argument( |
| 17 | + '--save-dir', |
| 18 | + type=str, |
| 19 | + help='the dir to save dataset', |
| 20 | + default='data/coco') |
| 21 | + parser.add_argument( |
| 22 | + '--unzip', |
| 23 | + action='store_true', |
| 24 | + help='whether unzip dataset or not, zipped files will be saved') |
| 25 | + parser.add_argument( |
| 26 | + '--delete', |
| 27 | + action='store_true', |
| 28 | + help='delete the download zipped files') |
| 29 | + parser.add_argument( |
| 30 | + '--threads', type=int, help='number of threading', default=4) |
| 31 | + args = parser.parse_args() |
| 32 | + return args |
| 33 | + |
| 34 | + |
| 35 | +def download(url, dir, unzip=True, delete=False, threads=1): |
| 36 | + |
| 37 | + def download_one(url, dir): |
| 38 | + f = dir / Path(url).name |
| 39 | + if Path(url).is_file(): |
| 40 | + Path(url).rename(f) |
| 41 | + elif not f.exists(): |
| 42 | + print('Downloading {} to {}'.format(url, f)) |
| 43 | + torch.hub.download_url_to_file(url, f, progress=True) |
| 44 | + if unzip and f.suffix in ('.zip', '.tar'): |
| 45 | + print('Unzipping {}'.format(f.name)) |
| 46 | + if f.suffix == '.zip': |
| 47 | + ZipFile(f).extractall(path=dir) |
| 48 | + elif f.suffix == '.tar': |
| 49 | + TarFile(f).extractall(path=dir) |
| 50 | + if delete: |
| 51 | + f.unlink() |
| 52 | + print('Delete {}'.format(f)) |
| 53 | + |
| 54 | + dir = Path(dir) |
| 55 | + if threads > 1: |
| 56 | + pool = ThreadPool(threads) |
| 57 | + pool.imap(lambda x: download_one(*x), zip(url, repeat(dir))) |
| 58 | + pool.close() |
| 59 | + pool.join() |
| 60 | + else: |
| 61 | + for u in [url] if isinstance(url, (str, Path)) else url: |
| 62 | + download_one(u, dir) |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + args = parse_args() |
| 67 | + path = Path(args.save_dir) |
| 68 | + if not path.exists(): |
| 69 | + path.mkdir(parents=True, exist_ok=True) |
| 70 | + data2url = dict( |
| 71 | + # TODO: Support for downloading Panoptic Segmentation of COCO |
| 72 | + coco2017=[ |
| 73 | + 'http://images.cocodataset.org/zips/train2017.zip', |
| 74 | + 'http://images.cocodataset.org/zips/val2017.zip', |
| 75 | + 'http://images.cocodataset.org/zips/test2017.zip', |
| 76 | + 'http://images.cocodataset.org/annotations/' + |
| 77 | + 'annotations_trainval2017.zip' |
| 78 | + ], |
| 79 | + lvis=[ |
| 80 | + 'https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v1_train.json.zip', # noqa |
| 81 | + 'https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v1_train.json.zip', # noqa |
| 82 | + ], |
| 83 | + voc2007=[ |
| 84 | + 'http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar', # noqa |
| 85 | + 'http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar', # noqa |
| 86 | + 'http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar', # noqa |
| 87 | + ], |
| 88 | + ) |
| 89 | + url = data2url.get(args.dataset_name, None) |
| 90 | + if url is None: |
| 91 | + print('Only support COCO, VOC, and LVIS now!') |
| 92 | + return |
| 93 | + download( |
| 94 | + url, |
| 95 | + dir=path, |
| 96 | + unzip=args.unzip, |
| 97 | + delete=args.delete, |
| 98 | + threads=args.threads) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + main() |
0 commit comments