-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_identity_blocks.py
64 lines (41 loc) · 1.78 KB
/
split_identity_blocks.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
import os
from typing import Any
import torch
import torchvision
from torchvision.utils import save_image, make_grid
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
import sys
def split_identity_grid(samples_dir, id_name, image_size: int):
with open(os.path.join(samples_dir, id_name), "rb") as f:
img = Image.open(f)
img = img.convert("RGB")
img = torchvision.transforms.functional.to_tensor(img)
nrows = img.shape[1] // image_size
ncols = img.shape[2] // image_size
id_images = []
for r in range(nrows):
for c in range(ncols):
tile = img[:, r * image_size: (r + 1) * image_size, c * image_size: (c + 1) * image_size]
id_images.append(tile)
return id_images
if __name__ == "__main__":
block_datasets_dir = "samples/aligned"
datasets_dir = "datasets"
model_names = [
#"unet-cond-ca-bs512-150K",
"unet-cond-ca-bs512-150K-cpd25",
#"unet-cond-ca-bs512-150K-cpd50"
]
for model_name in model_names:
model_data_path = os.path.join(block_datasets_dir, model_name)
for dataset_name in os.listdir(model_data_path):
dataset_path = os.path.join(model_data_path, dataset_name)
for id_block_file in os.listdir(dataset_path):
id_images = split_identity_grid(dataset_path, id_block_file, image_size=112)
id_name = id_block_file.split["."][0]
for i, img in enumerate(id_images):
save_dir = os.path.join(datasets_dir, model_name, dataset_name)
os.makedirs(save_dir, exist_ok=True)
save_image(img, os.path.join(save_dir, f"{id_name}_{i}.png"))