-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep.py
88 lines (73 loc) · 2.47 KB
/
prep.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
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
import torch
from torchvision import datasets, transforms
from torchvision import utils
import os
from tqdm.auto import tqdm
import shutil
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"datapath", help="path to data set. Eg './data/images'", type=str
)
parser.add_argument(
"start_size",
default=4,
help="first progression image size (default is 4)",
type=int,
)
parser.add_argument(
"end_size",
default=512,
help="last progression image size (default is 512)",
type=int,
)
args = parser.parse_args()
datapath = args.datapath
start_size = int(args.start_size)
end_size = int(args.end_size)
# Move OG images to new folder called 'original'.
dest_fold = os.path.join(datapath, "original", "images")
if not os.path.exists(dest_fold):
os.makedirs(dest_fold)
for file_name in os.listdir(datapath):
if file_name != "original":
shutil.move(os.path.join(datapath, file_name), dest_fold)
# Create dir for prepared datasets. If it exists, delete and overwrite.
prepared_path = out_path = os.path.join(datapath, "prepared")
if not os.path.exists(prepared_path):
os.mkdir(prepared_path)
index = 0
cur_size = start_size
while cur_size <= end_size:
image = 0
out_path = os.path.join(prepared_path, f"set_{index + 1}", "images")
if os.path.exists(out_path):
if input(f"set_{index + 1} exists. Delete? (y/N)") == "y":
shutil.rmtree(out_path)
else:
index += 1
cur_size = cur_size * 2
continue
os.makedirs(out_path)
transformation = transforms.Compose(
[
transforms.Resize((cur_size, cur_size)),
transforms.ToTensor(),
transforms.ConvertImageDtype(float),
]
)
images = datasets.ImageFolder(os.path.join(dest_fold, ".."), transformation)
dataset = torch.utils.data.DataLoader(
images,
batch_size=16,
shuffle=True,
num_workers=3,
)
for batch, _ in tqdm(dataset):
for im in batch:
image_name = f"image-{image}.png"
utils.save_image(im, os.path.join(out_path, image_name))
image += 1
cur_size = cur_size * 2
index += 1