-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_visualization.py
335 lines (289 loc) · 11.8 KB
/
eval_visualization.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
326
327
328
329
330
331
332
333
334
335
import numpy as np
from datasets import load_dataset, load_from_disk, interleave_datasets
from pixel_datasets.dataset_transformations import (
SyntheticDatasetTransform,
SimpleTorchTransform,
)
from pixel_datasets.pixel_dataset_generator import PretrainingDataset
from pixel_datasets.glue_dataset_generator import GlueDatasetForPixel
from pixel_datasets.utils.utils import (
mask_single_word_from_scan,
convert_torch_tensor_to_image,
plot_arrays,
merge_mask_with_image,
get_mask_edges,
convert_patch_mask_to_pixel_mask,
)
from pixel_datasets.utils.dataset_utils import generate_patch_mask
from pixel_datasets.utils.squad_utils import (
convert_pixel_mask_to_patch_mask,
)
import wandb
from tqdm.auto import tqdm
from pixel.utils.inference import (
load_model_for_pretraining,
predict,
parse_outputs,
get_inference_font,
parse_squad_outputs,
predict_squad,
load_model_for_squad,
)
from PIL import Image
import matplotlib.pyplot as plt
import torch
import pickle
import platform
def load_real_scans(args, n=100):
visualization_dataset = load_dataset("Nadav/CaribbeanScans", split="test", cache_dir=args.dataset_cache_dir)
visualization_dataset = visualization_dataset.shuffle().select(range(n))
visualization_dataset = visualization_dataset.filter(lambda x: x["image"].size == (368, 368))
rng = np.random.RandomState(42)
visualization_examples = []
for sample in tqdm(visualization_dataset):
sample["patch_mask"] = generate_patch_mask(args, rng, (368, 368))[1]
sample["pixel_values"] = np.array(sample["image"])
visualization_examples.append(sample)
return visualization_examples
def random_masking_real_samples(args, save_all_figs=False):
visualization_examples = load_real_scans(args, 16)
model = load_model_for_pretraining(
wandb.config,
"Nadav/Pixel-real-scans-v3",
)
predictions = []
for example in tqdm(visualization_examples):
outputs = predict(model, example["pixel_values"], example["patch_mask"])
prediction = parse_outputs(outputs, model, example["pixel_values"])
predictions.append(prediction)
for i in range(len(predictions)):
mask = visualization_examples[i]["patch_mask"]
pixel_mask = convert_patch_mask_to_pixel_mask(mask)
only_edges = get_mask_edges(pixel_mask, 3)
merged = merge_mask_with_image(
only_edges, np.array(predictions[i]), colour=(0, 0, 0), alpha=0.1
)
if save_all_figs:
original = Image.fromarray(visualization_examples[i]["pixel_values"])
pixel_mask = Image.fromarray((pixel_mask * 255).astype(np.uint8))
predicted = Image.fromarray(predictions[i])
merged_image = Image.fromarray(merged)
for name, im in zip(["original", "pixel_mask", "predicted", "merged"], [original, pixel_mask, predicted, merged_image]):
im.save(f"evaluations/sample_scans/real_scans_{i}_{name}.png")
predictions[i] = merged
final = plot_arrays(predictions)
final.save("evaluations/completions_results/real_validation_samples.png")
def random_masking(args):
visualization_dataset = load_dataset("wikipedia", "20220301.simple", split="train")
visualization_dataset = visualization_dataset.filter(
lambda x: len(x["text"].split()) > 200
)
# visualization_dataset = load_dataset("wikipedia", "20220301.simple")["train"]
print(visualization_dataset[0])
rng = np.random.RandomState(42)
transform = SyntheticDatasetTransform(wandb.config, rng=rng)
simple_transform = SimpleTorchTransform(wandb.config, rng=rng)
dataset = PretrainingDataset(
config=wandb.config,
text_dataset=visualization_dataset,
transform=transform,
rng=rng,
)
visualization_examples = []
counter = 9
for sample in tqdm(dataset):
if counter == 0:
break
counter -= 1
visualization_examples.append(sample)
model = load_model_for_pretraining(
wandb.config,
"Nadav/PretrainedPHD-v2",
)
predictions = []
for example in tqdm(visualization_examples):
outputs = predict(model, example["pixel_values"], example["patch_mask"])
prediction = parse_outputs(outputs, model, example["pixel_values"])
predictions.append(prediction)
for i in range(len(predictions)):
mask = visualization_examples[i]["patch_mask"]
mask = mask.numpy()
pixel_mask = convert_patch_mask_to_pixel_mask(mask)
only_edges = get_mask_edges(pixel_mask, 3)
merged = merge_mask_with_image(
only_edges, np.array(predictions[i]), colour=(0, 0, 0), alpha=0.1
)
predictions[i] = merged
final = plot_arrays(predictions)
final.save("evaluations/final.png")
def generate_single_mask_real_scans(args):
visualization_dataset = load_real_scans(args, n=64)
filtered_dataset = []
for sample in tqdm(visualization_dataset):
im = np.array(sample["pixel_values"])
try:
pixel_mask, ocred_image, random_word = mask_single_word_from_scan(im)
except ValueError:
continue
patch_mask = convert_pixel_mask_to_patch_mask(pixel_mask, 16, 0.3)
sample["patch_mask"] = (
torch.from_numpy(patch_mask).flatten().type(torch.float32)
)
sample["ocr"] = ocred_image
sample["random_word"] = random_word
filtered_dataset.append(sample)
pickle.dump(
filtered_dataset, open("evaluations/visualization_examples_real.pkl", "wb")
)
print("Saved visualization examples to disk")
def generate_single_mask_scans():
visualization_dataset = load_dataset("wikipedia", "20220301.simple", split="train")
visualization_dataset = visualization_dataset.filter(
lambda x: len(x["text"].split()) > 200
)
# visualization_dataset = load_dataset("wikipedia", "20220301.simple")["train"]
print(visualization_dataset[0])
rng = np.random.RandomState(42)
transform = SyntheticDatasetTransform(wandb.config, rng=rng)
simple_transform = SimpleTorchTransform(wandb.config, rng=rng)
dataset = PretrainingDataset(
config=wandb.config,
text_dataset=visualization_dataset,
transform=transform,
rng=rng,
)
font = get_inference_font()
font.font_size = 20
visualization_examples = []
for text_sample in tqdm(visualization_dataset["text"][:9]):
image = dataset.generate_inference_image(
text_sample, split_text=True, clean_text=True, font=font
)
image = simple_transform(image)
visualization_examples.append({"pixel_values": image})
for sample in visualization_examples:
im = convert_torch_tensor_to_image(sample["pixel_values"])
pixel_mask, ocred_image, random_word = mask_single_word_from_scan(im)
patch_mask = convert_pixel_mask_to_patch_mask(pixel_mask, 16, 0.3)
sample["patch_mask"] = (
torch.from_numpy(patch_mask).flatten().type(torch.float32)
)
sample["ocr"] = ocred_image
sample["random_word"] = random_word
pickle.dump(
visualization_examples, open("evaluations/visualization_examples.pkl", "wb")
)
print("Saved visualization examples to disk")
def mask_a_word(generate=False):
if generate:
generate_single_mask_scans()
else:
visualization_examples_all = pickle.load(
open("evaluations/visualization_examples_real.pkl", "rb")
)
model = load_model_for_pretraining(
wandb.config,
"Nadav/Pixel-real-scans-v3",
)
predictions = []
visualization_examples = []
for example in tqdm(visualization_examples_all):
try:
outputs = predict(model, example["pixel_values"], example["patch_mask"])
prediction = parse_outputs(outputs, model, example["pixel_values"])
predictions.append(prediction)
visualization_examples.append(example)
except ValueError:
continue
for i in range(len(predictions)):
mask = visualization_examples[i]["patch_mask"]
random_word = visualization_examples[i]["random_word"]
mask = mask.numpy()
pixel_mask = convert_patch_mask_to_pixel_mask(mask)
only_edges = get_mask_edges(pixel_mask, 3)
merged = merge_mask_with_image(
only_edges, np.array(predictions[i]), colour=(0, 0, 0), alpha=0.1
)
predictions[i] = merged
merged = Image.fromarray(merged)
merged.save(f"evaluations/completions_results/real_scans_{random_word}.png")
final = plot_arrays(
predictions,
titles=[example["random_word"] for example in visualization_examples],
)
final.save("evaluations/completions_results/random_words_real_scans.png")
def save_synthetic_dataset_samples(args):
"""
Saves a random sample of fake images from the dataset
"""
train_text_datasets = [
load_dataset(
d_name,
d_config,
split=d_split,
use_auth_token=args.use_auth_token,
cache_dir=args.dataset_cache_dir,
)
for d_name, d_config, d_split in zip(
args.train_dataset_names,
args.train_dataset_configs,
args.train_splits,
)
]
dataset_sizes = [ds._info.splits.total_num_examples for ds in train_text_datasets]
combined_size = sum(dataset_sizes)
dataset_sampling_probs = [d_size / combined_size for d_size in dataset_sizes]
train_text_dataset = interleave_datasets(
train_text_datasets,
probabilities=dataset_sampling_probs,
seed=args.seed,
)
rng = np.random.RandomState(112)
train_transform = SimpleTorchTransform(args, rng=rng)
train_dataset = PretrainingDataset(
config=args,
text_dataset=train_text_dataset,
transform=train_transform,
rng=rng,
)
figures = []
counter = 0
for batch in train_dataset:
if counter == 16:
break
im = batch["pixel_values"].numpy().transpose(1, 2, 0)
im = (im * 255).astype(np.uint8)
Image.fromarray(im).save(
f"/home/knf792/PycharmProjects/pixel-2/pixel_datasets/results/samples/synthetics_no_noise_{counter}.png"
)
figures.append(im)
counter += 1
im = plot_arrays(figures)
im.save(
"/home/knf792/PycharmProjects/pixel-2/pixel_datasets/results/samples/synthetics_no_noise_sample_grid.png"
)
def visualise_squad(args):
model = load_model_for_squad(args, "/projects/copenlu/data/nadav/pixel/pixel_squad_mixed_with_hist/checkpoint-2400/")
dataset = load_from_disk("/projects/copenlu/data/nadav/Datasets/runaways_visual/dataset")
dataset = dataset["test"]
dataset = dataset.shuffle()
dataset = dataset.filter(lambda x: np.max(x["label"]) == 1)
for i in tqdm(range(0, 32)):
instance = dataset[i]
image = np.asarray(instance["image"].copy().convert("RGB"))
label = np.array(instance["label"])
prediction = predict_squad(model, image)
parsed_predictions = parse_squad_outputs(prediction, image, label, method="saliency")
pixel_mask = convert_patch_mask_to_pixel_mask(label)
only_edges = get_mask_edges(pixel_mask, 3)
merged = merge_mask_with_image(
only_edges, parsed_predictions, colour=(0, 0, 0), alpha=0.1
)
merged = Image.fromarray(merged.astype(np.uint8))
merged.save(f"evaluations/runaways/saliency_{i}.png")
if __name__ == "__main__":
wandb.init(
config="/home/knf792/PycharmProjects/pixel-2/configs/inference_config.yaml",
mode="disabled",
)
random_masking_real_samples(wandb.config)