-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluate_auto_encoder.py
217 lines (161 loc) · 5.28 KB
/
evaluate_auto_encoder.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from core import give, rename_dict
from typing import Any, Callable, Optional
from networks.variational import (
VariationalBase,
)
import torch
from networks.auto_encoder_network import AutoEncoderNetwork
import fire # type:ignore
import matplotlib.pyplot as plt
from params import (
activation_params, activations, dataset_params, networks,
)
from torchvision import transforms
def display_as_images(
values,
get_image: Callable[[Any], Any],
get_caption: Callable[[Any], str],
line_size: int,
save_path: Optional[str] = None,
color_map: str = 'gray',
axis: bool = False,
size_multiplier: float = 2,
show=True,
):
count = len(values)
lines = count // line_size + (
0 if count % line_size == 0 else 1
)
fig = plt.figure(
figsize=(
int(line_size * size_multiplier),
int(lines * size_multiplier)
)
)
for i in range(count):
subplot = fig.add_subplot(lines, line_size, i+1)
if not axis:
plt.axis('off')
plt.imshow(get_image(values[i]), cmap=color_map)
subplot.set_title(get_caption(values[i]))
if save_path is not None:
plt.savefig(save_path)
if show:
plt.show()
plt.close(fig)
def run_evaluation(net: AutoEncoderNetwork, dataset, device, images_count=40):
print()
net.eval()
VariationalBase.GLOBAL_STD = 0
images = []
if dataset is None:
for i in range(images_count):
image = net.generate(device=device)
images.append(image)
print(
"eval s["
+ str(i + 1)
+ "/"
+ str(images_count)
+ "]",
end="\r",
)
else:
for i, (data, target) in enumerate(dataset):
data = data.to(device)
image = net.generate(net.encoder(data), device=device)
images.append(image)
images.append(data)
print(
"eval s["
+ str(i + 1)
+ "/"
+ str(images_count)
+ "]",
end="\r",
)
if i >= images_count:
break
return images
def create_train_validation_test(dataset_name: str):
params = dataset_params[dataset_name]
train_val = params["dataset"](
params["path"],
train=True,
download=True,
transform=transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(params["mean"], params["std"]),
]
),
)
test = params["dataset"](
params["path"],
train=False,
download=True,
transform=transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(params["mean"], params["std"]),
]
),
)
train, val = torch.utils.data.random_split( # type: ignore
train_val, [params["train_size"], params["validation_size"]]
)
return train, val, test
def generate(
network_name,
dataset_name,
model_path=None,
model_suffix="",
device="cuda:0",
split=None,
image_shape=(28, 28),
**kwargs,
):
if "activation" in kwargs:
current_activations = kwargs["activation"].split(" ")
activation_functions = []
for i, activation in enumerate(current_activations):
activation_kwargs, kwargs = give(
kwargs,
list(
map(
lambda a: activation + "_" + a,
activation_params[activation],
)
),
)
if activation in current_activations[i + 1:]:
kwargs = {**kwargs, **activation_kwargs}
func = activations[activation](
**rename_dict(
activation_kwargs,
lambda name: name.replace(activation + "_", ""),
)
)
activation_functions.append(func)
if len(activation_functions) == 1:
activation_functions = activation_functions[0]
kwargs["activation"] = activation_functions
if model_path is None:
full_network_name = network_name
if dataset_name not in network_name:
full_network_name = dataset_name + "_" + full_network_name
full_network_name += "" if model_suffix == "" else "_" + model_suffix
model_path = "./models/" + full_network_name
device = torch.device(device if torch.cuda.is_available() else "cpu")
net: AutoEncoderNetwork = networks[network_name](**kwargs)
train, val, test = create_train_validation_test(dataset_name)
current_dataset = None if split is None else {"train": train, "validation": val, "test": test}[split]
images = run_evaluation(net, current_dataset, device)
display_as_images(
images,
lambda img: img.view(image_shape).detach(),
lambda img: '',
6
)
if __name__ == "__main__":
fire.Fire()