|
| 1 | +""" |
| 2 | +Modification of https://github.com/stanfordnlp/treelstm/blob/master/scripts/download.py |
| 3 | +
|
| 4 | +Downloads the following: |
| 5 | +- Celeb-A dataset |
| 6 | +- LSUN dataset |
| 7 | +- MNIST dataset |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import print_function |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import gzip |
| 14 | +import json |
| 15 | +import shutil |
| 16 | +import zipfile |
| 17 | +import argparse |
| 18 | +import subprocess |
| 19 | +from six.moves import urllib |
| 20 | + |
| 21 | +parser = argparse.ArgumentParser(description='Download dataset for DCGAN.') |
| 22 | +parser.add_argument('datasets', metavar='N', type=str, nargs='+', choices=['celebA', 'lsun', 'mnist'], |
| 23 | + help='name of dataset to download [celebA, lsun, mnist]') |
| 24 | + |
| 25 | +def download(url, dirpath): |
| 26 | + filename = url.split('/')[-1] |
| 27 | + filepath = os.path.join(dirpath, filename) |
| 28 | + u = urllib.request.urlopen(url) |
| 29 | + f = open(filepath, 'wb') |
| 30 | + filesize = int(u.headers["Content-Length"]) |
| 31 | + print("Downloading: %s Bytes: %s" % (filename, filesize)) |
| 32 | + |
| 33 | + downloaded = 0 |
| 34 | + block_sz = 8192 |
| 35 | + status_width = 70 |
| 36 | + while True: |
| 37 | + buf = u.read(block_sz) |
| 38 | + if not buf: |
| 39 | + print('') |
| 40 | + break |
| 41 | + else: |
| 42 | + print('', end='\r') |
| 43 | + downloaded += len(buf) |
| 44 | + f.write(buf) |
| 45 | + status = (("[%-" + str(status_width + 1) + "s] %3.2f%%") % |
| 46 | + ('=' * int(float(downloaded) / filesize * status_width) + '>', downloaded * 100. / filesize)) |
| 47 | + print(status, end='') |
| 48 | + sys.stdout.flush() |
| 49 | + f.close() |
| 50 | + return filepath |
| 51 | + |
| 52 | +def unzip(filepath): |
| 53 | + print("Extracting: " + filepath) |
| 54 | + dirpath = os.path.dirname(filepath) |
| 55 | + with zipfile.ZipFile(filepath) as zf: |
| 56 | + zf.extractall(dirpath) |
| 57 | + os.remove(filepath) |
| 58 | + |
| 59 | +def download_celeb_a(dirpath): |
| 60 | + data_dir = 'celebA' |
| 61 | + if os.path.exists(os.path.join(dirpath, data_dir)): |
| 62 | + print('Found Celeb-A - skip') |
| 63 | + return |
| 64 | + url = 'https://www.dropbox.com/sh/8oqt9vytwxb3s4r/AADIKlz8PR9zr6Y20qbkunrba/Img/img_align_celeba.zip?dl=1&pv=1' |
| 65 | + filepath = download(url, dirpath) |
| 66 | + zip_dir = '' |
| 67 | + with zipfile.ZipFile(filepath) as zf: |
| 68 | + zip_dir = zf.namelist()[0] |
| 69 | + zf.extractall(dirpath) |
| 70 | + os.remove(filepath) |
| 71 | + os.rename(os.path.join(dirpath, zip_dir), os.path.join(dirpath, data_dir)) |
| 72 | + |
| 73 | +def _list_categories(tag): |
| 74 | + url = 'http://lsun.cs.princeton.edu/htbin/list.cgi?tag=' + tag |
| 75 | + f = urllib.request.urlopen(url) |
| 76 | + return json.loads(f.read()) |
| 77 | + |
| 78 | +def _download_lsun(out_dir, category, set_name, tag): |
| 79 | + url = 'http://lsun.cs.princeton.edu/htbin/download.cgi?tag={tag}' \ |
| 80 | + '&category={category}&set={set_name}'.format(**locals()) |
| 81 | + print(url) |
| 82 | + if set_name == 'test': |
| 83 | + out_name = 'test_lmdb.zip' |
| 84 | + else: |
| 85 | + out_name = '{category}_{set_name}_lmdb.zip'.format(**locals()) |
| 86 | + out_path = os.path.join(out_dir, out_name) |
| 87 | + cmd = ['curl', url, '-o', out_path] |
| 88 | + print('Downloading', category, set_name, 'set') |
| 89 | + subprocess.call(cmd) |
| 90 | + |
| 91 | +def download_lsun(dirpath): |
| 92 | + data_dir = os.path.join(dirpath, 'lsun') |
| 93 | + if os.path.exists(data_dir): |
| 94 | + print('Found LSUN - skip') |
| 95 | + return |
| 96 | + else: |
| 97 | + os.mkdir(data_dir) |
| 98 | + |
| 99 | + tag = 'latest' |
| 100 | + #categories = _list_categories(tag) |
| 101 | + categories = ['bedroom'] |
| 102 | + |
| 103 | + for category in categories: |
| 104 | + _download_lsun(data_dir, category, 'train', tag) |
| 105 | + _download_lsun(data_dir, category, 'val', tag) |
| 106 | + _download_lsun(data_dir, '', 'test', tag) |
| 107 | + |
| 108 | +def download_mnist(dirpath): |
| 109 | + data_dir = os.path.join(dirpath, 'mnist') |
| 110 | + if os.path.exists(data_dir): |
| 111 | + print('Found MNIST - skip') |
| 112 | + return |
| 113 | + else: |
| 114 | + os.mkdir(data_dir) |
| 115 | + url_base = 'http://yann.lecun.com/exdb/mnist/' |
| 116 | + file_names = ['train-images-idx3-ubyte.gz','train-labels-idx1-ubyte.gz','t10k-images-idx3-ubyte.gz','t10k-labels-idx1-ubyte.gz'] |
| 117 | + for file_name in file_names: |
| 118 | + url = (url_base+file_name).format(**locals()) |
| 119 | + print(url) |
| 120 | + out_path = os.path.join(data_dir,file_name) |
| 121 | + cmd = ['curl', url, '-o', out_path] |
| 122 | + print('Downloading ', file_name) |
| 123 | + subprocess.call(cmd) |
| 124 | + cmd = ['gzip', '-d', out_path] |
| 125 | + print('Decompressing ', file_name) |
| 126 | + subprocess.call(cmd) |
| 127 | + |
| 128 | +def prepare_data_dir(path = './data'): |
| 129 | + if not os.path.exists(path): |
| 130 | + os.mkdir(path) |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + args = parser.parse_args() |
| 134 | + prepare_data_dir() |
| 135 | + |
| 136 | + if 'celebA' in args.datasets: |
| 137 | + download_celeb_a('./data') |
| 138 | + if 'lsun' in args.datasets: |
| 139 | + download_lsun('./data') |
| 140 | + if 'mnist' in args.datasets: |
| 141 | + download_mnist('./data') |
0 commit comments