forked from YannDubs/disentangling-vae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_embeddings.py
202 lines (149 loc) · 6.49 KB
/
compute_embeddings.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from argparse import ArgumentParser, Namespace
from pathlib import Path
from subprocess import check_call
from typing import Tuple, Union
import numpy as np
import torch
from numpy.lib.npyio import NpzFile
from numpy.random import Generator
from torch import Tensor
from torch.utils.data import DataLoader, Dataset
from torchvision.datasets import MNIST
from torchvision.transforms import Compose, Resize, ToTensor
from src.models.encoders import EncoderBurgess
from src.utils.modelIO import load_model
def get_config() -> Namespace:
parser = ArgumentParser()
parser.add_argument("--use_gpu", type=bool, default=True)
parser.add_argument("--data_dir", type=str, default="data")
parser.add_argument("--model_dir", type=str, default="models")
parser.add_argument("--dataset", type=str, default="mnist")
parser.add_argument("--encoder", type=str, default="vae")
parser.add_argument("--batch_size", type=int, default=4_096)
return parser.parse_args()
def get_device(use_gpu: bool = True, use_deterministic_ops: bool = False) -> str:
"""
References:
https://pytorch.org/docs/stable/notes/mps.html
https://pytorch.org/docs/stable/notes/randomness.html
"""
if use_gpu and torch.cuda.is_available():
device = "cuda"
elif use_gpu and torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
if use_deterministic_ops:
torch.use_deterministic_algorithms(True)
torch.backends.cudnn.benchmark = False
return device
@torch.inference_mode()
def encode(loader: DataLoader, encoder: EncoderBurgess, device: str) -> np.ndarray:
embeddings = []
for images_i, _ in loader:
images_i = images_i.to(device)
embeddings_i, _ = encoder(images_i)
embeddings += [embeddings_i.cpu().numpy()]
return np.concatenate(embeddings)
def split_indices_using_class_labels(
labels: np.ndarray,
test_size: Union[dict, float, int],
rng: Generator,
balance_test_set: bool = True,
) -> Tuple[np.ndarray, np.ndarray]:
"""
sklearn.model_selection.train_test_split() doesn't produce exact stratification.
"""
if isinstance(test_size, float) and balance_test_set:
class_counts = np.bincount(labels)
test_size = int(test_size * min(class_counts))
test_size = max(test_size, 1)
train_inds, test_inds = [], []
for _class in np.unique(labels):
class_inds = np.flatnonzero(labels == _class)
class_inds = rng.permutation(class_inds)
if isinstance(test_size, dict):
class_test_size = test_size[_class]
else:
class_test_size = test_size
if isinstance(class_test_size, float):
class_test_size = int(class_test_size * len(class_inds))
class_test_size = max(class_test_size, 1)
train_inds += [class_inds[:-class_test_size]]
test_inds += [class_inds[-class_test_size:]]
train_inds = np.concatenate(train_inds)
train_inds = rng.permutation(train_inds)
test_inds = np.concatenate(test_inds)
test_inds = rng.permutation(test_inds)
return train_inds, test_inds
class DSprites(Dataset):
"""
DSprites dataset with one of the six underlying latents as the class label. The dataset is split
into training and test sets. The split depends on three parameters: the latent used as the class
label, the fraction of the dataset used for testing, and the random seed.
References:
https://github.com/google-deepmind/dsprites-dataset
"""
filename = "dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz"
target_names = {"color": 0, "shape": 1, "scale": 2, "orientation": 3, "pos_x": 4, "pos_y": 5}
def __init__(
self,
data_dir: Union[Path, str],
train: bool = True,
test_fraction: float = 0.15,
target_latent: str = "shape",
seed: int = 0,
) -> None:
data_dir = Path(data_dir)
if not (data_dir / self.filename).exists():
self.download(data_dir)
npz_file = np.load(data_dir / self.filename)
inputs, labels = self.process_npz(npz_file, target_latent)
rng = np.random.default_rng(seed=seed)
train_inds, test_inds = split_indices_using_class_labels(labels, test_fraction, rng)
if train:
self.data = inputs[train_inds]
self.targets = labels[train_inds]
else:
self.data = inputs[test_inds]
self.targets = labels[test_inds]
self.data = torch.tensor(self.data)
self.targets = torch.tensor(self.targets)
def __getitem__(self, index: int) -> Tuple[Tensor, Tensor]:
return self.data[index], self.targets[index]
def __len__(self) -> int:
return len(self.data)
def download(self, data_dir: Path) -> None:
url = f"https://github.com/google-deepmind/dsprites-dataset/raw/master/{self.filename}"
data_dir.mkdir(parents=True, exist_ok=True)
check_call(["curl", "--location", "--output", data_dir / self.filename, url])
def process_npz(self, npz_file: NpzFile, target_latent: str) -> Tuple[np.ndarray, np.ndarray]:
inputs = npz_file["imgs"] # [N, 64, 64], np.uint8, values in {0, 1}
inputs = inputs[:, None, :, :] # [N, 1, 64, 64]
inputs = inputs.astype(np.float32) # [N, 1, 64, 64]
labels = npz_file["latents_classes"] # [N, 6], np.int64
labels = labels[:, self.target_names[target_latent]] # [N,]
return inputs, labels # [N, 1, 64, 64], [N,]
def main(cfg: Namespace) -> None:
device = get_device(cfg.use_gpu)
data_dir = Path(cfg.data_dir) / cfg.dataset
model_dir = Path(cfg.model_dir) / cfg.dataset / cfg.encoder
encoder = load_model(model_dir, is_gpu=(device in {"cuda", "mps"})).encoder
encoder.eval()
for subset in ("train", "test"):
if cfg.dataset == "mnist":
dataset = MNIST(
root=data_dir,
train=(subset == "train"),
download=True,
transform=Compose([Resize(32), ToTensor()]),
)
else:
dataset = DSprites(data_dir=data_dir, train=(subset == "train"))
loader = DataLoader(dataset, batch_size=cfg.batch_size, shuffle=False)
embeddings = encode(loader, encoder, device)
np.save(data_dir / f"embeddings_{cfg.encoder}_{subset}.npy", embeddings, allow_pickle=False)
np.save(data_dir / f"labels_{subset}.npy", dataset.targets, allow_pickle=False)
if __name__ == "__main__":
cfg = get_config()
main(cfg)