-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare.py
43 lines (31 loc) · 1023 Bytes
/
prepare.py
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
import argparse
from pathlib import Path
import shutil
import pandas as pd
import torchvision
from tqdm import tqdm
from vae import problem
CACHE_ROOT = Path('cache')
def image_path(directory, index):
return directory / f'{index}.png'
def save_images(dataset, directory):
original_size = (178, 218)
crop_size = 148
left = (original_size[0] - crop_size) // 2
top = (original_size[1] - crop_size) // 2
for index, (image, _) in enumerate(tqdm(dataset)):
(
image.crop((left, top, left + crop_size, top + crop_size))
.resize((problem.settings.WIDTH, problem.settings.HEIGHT))
.save(image_path(directory, index))
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
args = parser.parse_args()
dataset = torchvision.datasets.CelebA(
CACHE_ROOT, split='all', download=True
)
directory = Path('prepared')
directory.mkdir(parents=True)
save_images(dataset, directory)
shutil.rmtree(CACHE_ROOT)