-
Notifications
You must be signed in to change notification settings - Fork 0
/
full_sample.py
325 lines (272 loc) · 10.3 KB
/
full_sample.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import contextlib
import math
import os
import shutil
import sys
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path
from typing import List, Union
import PIL.Image as ImageModule
import torch
import torchvision
import yaml
from cleanfid import fid
from einops import rearrange, repeat
from matplotlib import pyplot as plt
from PIL import ImageOps
from PIL.Image import Image
from torch import Tensor
from torch.utils.data import DataLoader
from torchmetrics.image.inception import InceptionScore
from torchvision.utils import save_image
from configs.settings import CALCULATION_BASE_DIR, CONFIGS, MODELS, MODELS_SN
from data import utils
from data.dataset import IAMDataset, IAMonDataset
from metrics import FolderDataset, HWDScore
def cli_main():
"""
Command-line interface for initializing and parsing arguments.
Returns:
Namespace: A namespace object containing parsed arguments.
"""
parser = ArgumentParser()
parser.add_argument(
"-c",
"--config",
help="Type of model",
choices=["Diffusion", "LatentDiffusion"],
type=str,
required=True,
)
parser.add_argument(
"-cf",
"--config-file",
help="Filename for configs",
type=str,
default="base_gpu.yaml",
)
parser.add_argument(
"--strict",
help="Strict mode for a dataset that excludes OOV words",
action="store_true",
)
return parser.parse_args()
def full_sampling():
"""
Conducts full sampling for generated and real images to calculate
metrics like Inception Score (IS), Frechet Inception Distance (FID)
Kernel Inception Distance (KID) and Handwriting Distance (HWD).
This function iterates over a dataset, generates handwriting samples using a model,
and calculates IS, FID, KID and HWD for these samples compared to real images. It handles different
configurations for models like LatentDiffusion and others. The results are saved and printed.
"""
num_of_image = 1
isc_fake = InceptionScore(normalize=False)
isc_real = InceptionScore(normalize=False)
hwd = HWDScore(device=config.device)
kwargs_dataset = dict(
config=config,
img_height=config.img_height,
img_width=config.img_width,
max_text_len=config.max_text_len,
max_files=config.max_files,
dataset_type="train",
strict=args.strict,
)
device = torch.device(config.device)
if args.config == "LatentDiffusion":
normalize_undo = utils.NormalizeInverse((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
fid_dataset = IAMDataset(**kwargs_dataset)
fid_dataloader = DataLoader(
fid_dataset,
batch_size=config.batch_size,
shuffle=False,
pin_memory=True,
)
for i, (writer_ids, images, labels) in enumerate(fid_dataloader, start=1):
print(
f"""
===================================
Step {i}/{math.ceil(len(fid_dataset) / config.batch_size)}
===================================
"""
)
writer_ids, labels = writer_ids.to(device), labels.to(device)
fake_samples = generate_handwriting(labels, writer_ids)
fake_samples = [
rearrange(
torchvision.transforms.ToTensor()(fake_sample), "c w h -> 1 c w h"
)
for fake_sample in fake_samples
]
fake_samples = torch.cat(fake_samples, dim=0)
for real, fake in zip(images.unbind(0), fake_samples.unbind(0)):
save_image(
real,
fp=f"{CALCULATION_BASE_DIR}/real_samples/img-{num_of_image}.jpeg",
)
save_image(
fake,
fp=f"{CALCULATION_BASE_DIR}/fake_samples/img-{num_of_image}.jpeg",
)
num_of_image += 1
fake_samples = normalize_undo(fake_samples).type(torch.uint8)
real_samples = normalize_undo(images).type(torch.uint8)
isc_fake.update(fake_samples)
isc_real.update(real_samples)
else:
kwargs_dataset["max_seq_len"] = config.max_seq_len
fid_dataset = IAMonDataset(**kwargs_dataset)
fid_dataloader = DataLoader(
fid_dataset,
batch_size=config.batch_size,
shuffle=False,
pin_memory=True,
)
for i, (_, text, style, img) in enumerate(fid_dataloader, start=1):
print(
f"""
===================================
Step {i}/{math.ceil(len(fid_dataset) / config.batch_size)}
===================================
"""
)
text, style = text.to(device), style.to(device)
args.text, args.style_path = text, style
fake_samples = generate_handwriting(text, style)
fake_samples = [
ImageModule.frombytes(
"RGB", fig.canvas.get_width_height(), fig.canvas.tostring_rgb()
)
for fig in fake_samples
]
total_width, total_height = fake_samples[0].size
for sample in fake_samples[1:]:
total_width = max(total_width, sample.width)
total_height = max(total_height, sample.height)
fake_samples_fixed = []
for sample in fake_samples:
img_fix = sample.convert("L")
bbox = ImageOps.invert(img_fix).getbbox()
img_fix = img_fix.crop(bbox)
img_fix = ImageOps.pad(
image=img_fix,
size=(total_width, total_height),
method=ImageModule.LANCZOS,
color="white",
centering=(0.0, 0.5),
)
img_fix = img_fix.convert("RGB")
fake_samples_fixed.append(
rearrange(
torchvision.transforms.ToTensor()(img_fix), "c h w -> 1 c h w"
)
)
fake_samples = torch.cat(fake_samples_fixed, dim=0)
for real, fake in zip(img.unbind(0), fake_samples.unbind(0)):
save_image(
real,
fp=f"{CALCULATION_BASE_DIR}/real_samples/img-{num_of_image}.jpeg",
)
save_image(
fake,
fp=f"{CALCULATION_BASE_DIR}/fake_samples/img-{num_of_image}.jpeg",
)
num_of_image += 1
fake_samples = fake_samples.type(torch.uint8)
real_samples = repeat(
img.type(torch.uint8), "b c h w -> b (repeats c) h w", repeats=3
)
isc_fake.update(fake_samples)
isc_real.update(real_samples)
real_dataset = FolderDataset(
f"{CALCULATION_BASE_DIR}/real_samples", extension="jpeg"
)
fake_dataset = FolderDataset(
f"{CALCULATION_BASE_DIR}/fake_samples", extension="jpeg"
)
hwd_value = hwd(real_dataset, fake_dataset)
isc_value_fake = isc_fake.compute()
isc_value_real = isc_real.compute()
fid_value = fid.compute_fid(
fdir1=f"{CALCULATION_BASE_DIR}/real_samples",
fdir2=f"{CALCULATION_BASE_DIR}/fake_samples",
batch_size=config.batch_size // 2,
num_workers=os.cpu_count() // 4,
device=device,
)
kid_value = fid.compute_kid(
fdir1=f"{CALCULATION_BASE_DIR}/real_samples",
fdir2=f"{CALCULATION_BASE_DIR}/fake_samples",
batch_size=config.batch_size // 2,
num_workers=os.cpu_count() // 4,
device=device,
)
print(
f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} "
f"|| Model: {MODELS_SN[args.config]} "
f"|| FID value: {fid_value} "
f"|| KID value: {kid_value} "
f"|| HWD value: {hwd_value} "
f"|| IS value: {isc_value_fake[0]} +- {isc_value_fake[1]} "
f"|| (Dataset) IS value: {isc_value_real[0]} +- {isc_value_real[1]}\n"
)
with open(f"{config.checkpoint_path}/metrics.txt", mode="a+") as f:
f.write(
f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} "
f"|| Model: {MODELS_SN[args.config]} "
f"|| FID value: {fid_value} "
f"|| KID value: {kid_value} "
f"|| HWD value: {hwd_value} "
f"|| IS value: {isc_value_fake[0]} +- {isc_value_fake[1]} "
f"|| (Dataset) IS value: {isc_value_real[0]} +- {isc_value_real[1]}\n"
)
def generate_handwriting(
text: Tensor, style: Tensor
) -> Union[Image, plt.Figure, List[Image], List[plt.Figure]]:
"""
Generates handwriting samples based on given text and style using the specified model.
Args:
text (Tensor): Input tensor representing text.
style (Tensor): Style tensor representing handwriting style or writer ID.
Returns:
Union[Image, plt.Figure, List[Image], List[plt.Figure]]: Generated handwriting images or figures.
"""
if args.config == "LatentDiffusion":
return model.generate(
text,
vocab=config.vocab,
max_text_len=config.max_text_len,
writer_id=style,
save_path=None,
color="black",
is_fid=True,
)
return model.generate(
text,
save_path=None,
vocab=config.vocab,
max_text_len=config.max_text_len,
color="black",
style_path=style,
is_fid=True,
)
if __name__ == "__main__":
args = cli_main()
if sys.version_info < (3, 8):
raise SystemExit("Only Python 3.8 and above is supported")
config_file = f"configs/{args.config}/{args.config_file}"
config = CONFIGS[args.config].from_yaml_file(
file=config_file, decoder=yaml.load, Loader=yaml.Loader
)
model = MODELS[args.config].load_from_checkpoint(
checkpoint_path=f"{config.checkpoint_path}/model.ckpt",
map_location=torch.device(config.device),
)
model.eval()
with contextlib.suppress(FileNotFoundError):
shutil.rmtree(CALCULATION_BASE_DIR)
Path(f"{CALCULATION_BASE_DIR}/real_samples").mkdir(parents=True)
Path(f"{CALCULATION_BASE_DIR}/fake_samples").mkdir(parents=True)
full_sampling()