-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizations.py
195 lines (179 loc) · 6.18 KB
/
visualizations.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
from itertools import chain
from pathlib import Path
from typing import Iterable, List
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from utils import *
def translate_dataset(dataset_loader: DataLoader,
translations_dirs: List[str],
generator: nn.Module,
device: torch.device,
include_attention: bool = False,
attention_position: str = 'horizontal'
):
generator = generator.to(device)
with torch.inference_mode():
for n, (real_A, _) in enumerate(dataset_loader):
real_A = real_A.to(device)
img_path, _ = dataset_loader.dataset.samples[n]
img_name = Path(img_path).name.split('.')[0]
if not include_attention:
fake_B = generator(real_A, cam=False)
fake_B_RGB = RGB2BGR(tensor2numpy(denorm(fake_B[0]))) * 255.0
else:
_, _, H, W = real_A.shape
assert (H == W)
fake_B, _, fake_B_heatmap = generator(real_A, cam=True)
if attention_position == 'horizontal':
fake_B_RGB = np.hstack([
cam(tensor2numpy(fake_B_heatmap[0]), H) * 255.0,
RGB2BGR(tensor2numpy(denorm(fake_B[0]))) * 255.0,
])
else:
fake_B_RGB = np.vstack([
cam(tensor2numpy(fake_B_heatmap[0]), H) * 255.0,
RGB2BGR(tensor2numpy(denorm(fake_B[0]))) * 255.0,
])
for translations_dir in translations_dirs:
cv2.imwrite(
os.path.join(
translations_dir,
f'{img_name}_fake_B.jpg'
),
fake_B_RGB
)
def generate_translation_example(
real: torch.Tensor,
generator: nn.Module,
device: torch.device,
include_cam_heatmap: bool = False
):
generator = generator.to(device)
real = real.to(device)
if include_cam_heatmap:
_, _, H, W = real.shape
assert (H == W)
with torch.inference_mode():
fake, _, heatmap = generator(real, True)
return np.vstack([
RGB2BGR(tensor2numpy(denorm(real[0]))) * 255.0,
cam(tensor2numpy(heatmap[0]), H) * 255.0,
RGB2BGR(tensor2numpy(denorm(fake[0]))) * 255.0,
])
else:
with torch.inference_mode():
fake = generator(real, False)
return np.vstack([
RGB2BGR(tensor2numpy(denorm(real[0]))) * 255.0,
RGB2BGR(tensor2numpy(denorm(fake[0]))) * 255.0,
])
def plot_translation_examples(
generator: nn.Module,
patch_sampler: nn.Module,
trainA_iter: Iterable,
trainA_loader: DataLoader,
trainB_iter: Iterable,
trainB_loader: DataLoader,
valA_iter: Iterable,
valA_loader: DataLoader,
valB_iter: Iterable,
valB_loader: DataLoader,
testA_iter: Iterable,
testA_loader: DataLoader,
testB_iter: Iterable,
testB_loader: DataLoader,
device: torch.device,
A2B_results_filename: str,
B2B_results_filename: str,
train_examples_num: int = 4,
val_examples_num: int = 4,
test_examples_num: int = 4,
img_size: int = 256,
patch_sampling_type: str = 'random'
):
def generate_translation_examples_matrix(
A_examples_iter: Iterable,
A_examples_loader: DataLoader,
B_examples_iter: Iterable,
B_examples_loader: DataLoader,
examples_num: int
):
with torch.no_grad():
A2B, B2B = [], []
for _ in range(examples_num):
try:
real_A, _ = next(A_examples_iter)
except:
A_examples_iter = iter(A_examples_loader)
real_A, _ = next(A_examples_iter)
try:
real_B, _ = next(B_examples_iter)
except:
B_examples_iter = iter(B_examples_loader)
real_B, _ = next(B_examples_iter)
real_A = real_A.to(device)
real_B = real_B.to(device)
if patch_sampling_type == 'random':
A2B.append(generate_translation_example(real_A, generator, include_cam_heatmap=True, device=device))
B2B.append(generate_translation_example(real_B, generator, include_cam_heatmap=True, device=device))
else:
loss_attn_A2B = patch_sampler(
generator.encode(real_A),
return_only_full_attn_maps=True,
)
loss_attn_B2B = patch_sampler(
generator.encode(real_B),
return_only_full_attn_maps=True,
)
A2B.append(
np.vstack(
[generate_translation_example(
real_A,
generator,
include_cam_heatmap=True,
device=device
)] + [
cam(tensor2numpy(attn), img_size) * 255
for attn in loss_attn_A2B
])
)
B2B.append(
np.vstack(
[generate_translation_example(
real_B,
generator,
include_cam_heatmap=True,
device=device
)] + [
cam(tensor2numpy(attn), img_size) * 255
for attn in loss_attn_B2B
])
)
return A2B, B2B
train_A2B, train_B2B = generate_translation_examples_matrix(
A_examples_iter=trainA_iter,
A_examples_loader=trainA_loader,
B_examples_iter=trainB_iter,
B_examples_loader=trainB_loader,
examples_num=train_examples_num
)
val_A2B, val_B2B = generate_translation_examples_matrix(
A_examples_iter=valA_iter,
A_examples_loader=valA_loader,
B_examples_iter=valB_iter,
B_examples_loader=valB_loader,
examples_num=val_examples_num
)
test_A2B, test_B2B = generate_translation_examples_matrix(
A_examples_iter=testA_iter,
A_examples_loader=testA_loader,
B_examples_iter=testB_iter,
B_examples_loader=testB_loader,
examples_num=test_examples_num
)
A2B = np.hstack(list(chain(train_A2B, val_A2B, test_A2B)))
B2B = np.hstack(list(chain(train_B2B, val_B2B, test_B2B)))
cv2.imwrite(A2B_results_filename, A2B)
cv2.imwrite(B2B_results_filename, B2B)