forked from LBH1024/CAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
602 lines (524 loc) · 27.7 KB
/
dataset.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import torch
import time
import pickle as pkl
import json
import re
from torch import Tensor
from torch.utils.data import DataLoader, Dataset, RandomSampler
from PIL import Image
from pathlib import Path
from torchvision.transforms.functional import to_tensor
def average_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
def mean_pooling(model_output: Tensor, attention_mask: Tensor) -> Tensor:
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
class HMERDataset(Dataset):
def __init__(self, params, image_path, label_path, words, is_train=True):
super(HMERDataset, self).__init__()
if image_path.endswith('.pkl'):
with open(image_path, 'rb') as f:
self.images = pkl.load(f)
elif image_path.endswith('.list'):
with open(image_path, 'r') as f:
lines = f.readlines()
self.images = {}
print(f'data files: {lines}')
for line in lines:
name = line.strip()
print(f'loading data file: {name}')
start = time.time()
with open(name, 'rb') as f:
images = pkl.load(f)
self.images.update(images)
print(f'loading {name} cost: {time.time() - start:.2f} seconds!')
with open(label_path, 'r') as f:
self.labels = f.readlines()
self.words = words
self.is_train = is_train
self.params = params
def __len__(self):
assert len(self.images) == len(self.labels)
return len(self.labels)
def __getitem__(self, idx):
name, *labels = self.labels[idx].strip().split()
name = name.split('.')[0] if name.endswith('jpg') else name
image = self.images[name]
image = torch.Tensor(255-image) / 255
image = image.unsqueeze(0)
labels.append('eos')
words = self.words.encode(labels)
words = torch.LongTensor(words)
return image, words
class MLHMEDataset(Dataset):
def __init__(self, params, labels_path, words):
self.params = params
self.words = words
self.image_paths = []
self.image_labels = []
self.image_root = Path(labels_path).parent / 'train_images'
with open(labels_path, 'r', encoding='utf8') as f:
lines = f.readlines()
for line in lines:
image_name, *labels = line.strip().split()
labels.append('eos')
self.image_paths.append(self.image_root / image_name)
self.image_labels.append(labels)
def __len__(self):
assert len(self.image_paths) == len(self.image_labels)
return len(self.image_paths)
def __getitem__(self, idx):
image_path, labels = self.image_paths[idx], self.image_labels[idx]
im = Image.open(image_path).convert('L')
img = to_tensor(im)
im.close()
img = 1 - img
words, _ = self.words.encode(labels)
words = torch.LongTensor(words)
return img, words
class TelevicDataset(Dataset):
def __init__(self, params, jsons_file, words, questions_file=None):
self.params = params
self.invert = params['invert_image']
self.words = words
self.image_paths = []
self.image_labels = []
self.question_ids = []
self.question_embeddings = []
self.image_root = Path(jsons_file).parent / 'jpg_files_resized'
self.json_root = Path(jsons_file).parent / 'mathpix_output_adjusted'
self.questions_file = questions_file
if questions_file is not None:
import os
from transformers import AutoTokenizer, AutoModel
import gc
os.environ["TOKENIZERS_PARALLELISM"] = "true"
tokenizer = AutoTokenizer.from_pretrained("witiko/mathberta")
model = AutoModel.from_pretrained("witiko/mathberta")
with open(questions_file, 'r', encoding='utf-8') as questions:
lines = questions.readlines()
for line in lines:
question_id, *content = line.strip().split()
self.question_ids.append(question_id)
question = ' '.join(content)
tokenized = tokenizer(question, max_length=512, padding=True, truncation=True, return_tensors='pt')
outputs = model(**tokenized)
embedding = average_pool(outputs.last_hidden_state, tokenized['attention_mask']) #average pool or mean pool?
self.question_embeddings.append(embedding.clone().detach())
del model, tokenizer
gc.collect()
torch.cuda.empty_cache()
with open(jsons_file, 'r', encoding='utf-8') as jsons:
lines = jsons.readlines()
for json_file in lines:
if json_file.endswith('.json\n'):
json_file = json_file.strip('\n')
with open(Path(self.json_root) / json_file, 'r', encoding='utf-8') as file:
data = json.load(file)
if 'latex_styled' in data:
self.image_labels.append(data['latex_styled'])
else:
print('not found')
image_name = json_file.replace('.json', '.jpg')
self.image_paths.append(self.image_root / image_name)
# combined_pattern = r'(\\{2})|(\\[{}&%|]))|(\\sqrt)|(\\leftrightarrow)|(\\left\\{)|(\\left[\(\)\{\}\[\]\|.])|(\\left(\\rvert)?(\\lvert)?)|(\\right[\(\)\{\}\[\]\|.]?(\\rvert)?(\\lvert)?)|(\\text ?{([\w .?!,\)\()]+) ?})|(\\frac)|((\\[a-zA-Z]+)({[a-zA-Z~]+})?({[a-zA-Z\|]+})?)|([+-=*^_{}])|([\d\w])|([\[\]\(\)\|\\&<>?!~%\"\'\&])'
combined_pattern = r'(\\ ?,)|(\\ ?{)|(\\ ?})|(\\ ?%)|(\\ ?\|)|(\\sqrt)|(\\leftrightarrow)|(\\left\\{)|(\\left[\(\)\{\}\[\]\|.]?(\\rvert)?(\\lvert)?)|(\\right[\(\)\{\}\[\]\|.]?(\\rvert)?(\\lvert)?)|(\\text ?{([\w .?!,\)\()]+) ?})|(\\frac)|((\\[a-zA-Z]+)({[a-zA-Z~]+})?({[a-zA-Z\|]+})?)|([+-=*^_{}])|([\d\w])|(\\{2})|([\[\]\(\)\|\\&<>?!~%\"\'\&])'
temp = []
for label in self.image_labels:
parts = re.findall(combined_pattern, label)
comb_parts = []
for tuple in parts:
nonzero_elements = list(x for x in tuple if x != '')
comb_parts.append(nonzero_elements[0])
temp.append(comb_parts)
for i in range(len(self.image_labels)):
one = ''.join(temp[i]).replace(' ', '')
two = self.image_labels[i].replace(' ', '')
assert one == two, f'index {i} : {self.image_paths[i]} : {one} is not equal to {two}'
temp[i].append('eos')
self.image_labels = temp
assert len(self.image_paths) == len(self.image_labels)
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
image_path, labels = self.image_paths[idx], self.image_labels[idx]
im = Image.open(image_path).convert('L')
img = to_tensor(im)
im.close()
if self.invert:
img = 1 - img
if self.questions_file is not None:
combined_pattern = r'canvas_(\d+)_(\d+)'
parts = re.findall(combined_pattern, image_path.__str__())
embedding = self.question_embeddings[self.question_ids.index(parts[0][1])].flatten()
words, _ = self.words.encode(labels)
words = torch.LongTensor(words)
return img, words, embedding
else:
words, _ = self.words.encode(labels)
words = torch.LongTensor(words)
return img, words
def get_crohme_dataset(params):
words = Words(params['word_path'])
params['word_num'] = len(words)
print(f"训练数据路径 images: {params['train_image_path']} labels: {params['train_label_path']}")
print(f"验证数据路径 images: {params['eval_image_path']} labels: {params['eval_label_path']}")
train_dataset = HMERDataset(params, params['train_image_path'], params['train_label_path'], words, is_train=True)
eval_dataset = HMERDataset(params, params['eval_image_path'], params['eval_label_path'], words, is_train=False)
train_sampler = RandomSampler(train_dataset)
eval_sampler = RandomSampler(eval_dataset)
train_loader = DataLoader(train_dataset, batch_size=params['batch_size'], sampler=train_sampler,
num_workers=params['workers'], collate_fn=collate_fn_dict[params['collate_fn']], pin_memory=True)
eval_loader = DataLoader(eval_dataset, batch_size=1, sampler=eval_sampler,
num_workers=params['workers'], collate_fn=collate_fn_dict[params['collate_fn']], pin_memory=True)
print(f'train dataset: {len(train_dataset)} train steps: {len(train_loader)} '
f'eval dataset: {len(eval_dataset)} eval steps: {len(eval_loader)} ')
return train_loader, eval_loader, params
def get_mlhme_dataset(params):
words = Words(params['word_path'])
params['word_num'] = len(words)
print(f"Train labels: {params['train_label_path']}")
print(f"Eval labels: {params['eval_label_path']}")
train_dataset = MLHMEDataset(params, params['train_label_path'], words)
eval_dataset = MLHMEDataset(params, params['eval_label_path'], words)
train_sampler = RandomSampler(train_dataset)
eval_sampler = RandomSampler(eval_dataset)
train_loader = DataLoader(train_dataset, batch_size=params['batch_size'], sampler=train_sampler,
num_workers=params['workers'], collate_fn=collate_fn_dict[params['collate_fn']], pin_memory=True)
eval_loader = DataLoader(eval_dataset, batch_size=1, sampler=eval_sampler,
num_workers=params['workers'], collate_fn=collate_fn_dict[params['collate_fn']], pin_memory=True)
print(f'Train dataset: {len(train_dataset)}, Train steps: {len(train_loader)} \n'
f'Eval dataset: {len(eval_dataset)}, Eval steps: {len(eval_loader)}')
return train_loader, eval_loader, params
def get_televic_dataset(params):
words = Words(params['word_path'])
params['word_num'] = len(words)
print(f"Train labels: {params['train_label_path']}")
print(f"Eval labels: {params['eval_label_path']}")
train_dataset = TelevicDataset(params, params['train_label_path'], words)
eval_dataset = TelevicDataset(params, params['eval_label_path'], words)
train_sampler = RandomSampler(train_dataset)
eval_sampler = RandomSampler(eval_dataset)
train_loader = DataLoader(train_dataset, batch_size=params['batch_size'], sampler=train_sampler,
num_workers=params['workers'], collate_fn=collate_fn_dict[params['collate_fn']], pin_memory=True)
eval_loader = DataLoader(eval_dataset, batch_size=1, sampler=eval_sampler,
num_workers=params['workers'], collate_fn=collate_fn_dict[params['collate_fn']], pin_memory=True)
print(f'Train dataset: {len(train_dataset)}, Train steps: {len(train_loader)} \n'
f'Eval dataset: {len(eval_dataset)}, Eval steps: {len(eval_loader)}')
return train_loader, eval_loader, params
def collate_fn(batch_images):
max_width, max_height, max_length = 0, 0, 0
_, channel = len(batch_images), batch_images[0][0].shape[0]
proper_items = []
for item in batch_images:
if item[0].shape[1] * max_width > 1600 * 320 or item[0].shape[2] * max_height > 1600 * 320:
continue
max_height = item[0].shape[1] if item[0].shape[1] > max_height else max_height
max_width = item[0].shape[2] if item[0].shape[2] > max_width else max_width
max_length = item[1].shape[0] if item[1].shape[0] > max_length else max_length
proper_items.append(item)
images, image_masks = torch.zeros((len(proper_items), channel, max_height, max_width)), torch.zeros((len(proper_items), 1, max_height, max_width))
labels, labels_masks = torch.zeros((len(proper_items), max_length)).long(), torch.zeros((len(proper_items), max_length))
for i in range(len(proper_items)):
_, h, w = proper_items[i][0].shape
images[i][:, :h, :w] = proper_items[i][0]
image_masks[i][:, :h, :w] = 1
l = proper_items[i][1].shape[0]
labels[i][:l] = proper_items[i][1]
labels_masks[i][:l] = 1
return images, image_masks, labels, labels_masks
def collate_fn_context(batch_images):
max_width, max_height, max_length = 0, 0, 0
context_len = len(batch_images[0][2])
_, channel = len(batch_images), batch_images[0][0].shape[0]
proper_items = []
for item in batch_images:
if item[0].shape[1] * max_width > 1600 * 320 or item[0].shape[2] * max_height > 1600 * 320:
continue
max_height = item[0].shape[1] if item[0].shape[1] > max_height else max_height
max_width = item[0].shape[2] if item[0].shape[2] > max_width else max_width
max_length = item[1].shape[0] if item[1].shape[0] > max_length else max_length
proper_items.append(item)
images, image_masks = torch.zeros((len(proper_items), channel, max_height, max_width)), torch.zeros((len(proper_items), 1, max_height, max_width))
labels, labels_masks = torch.zeros((len(proper_items), max_length)).long(), torch.zeros((len(proper_items), max_length))
context = torch.zeros((len(proper_items), context_len))
for i in range(len(proper_items)):
_, h, w = proper_items[i][0].shape
images[i][:, :h, :w] = proper_items[i][0]
image_masks[i][:, :h, :w] = 1
l = proper_items[i][1].shape[0]
labels[i][:l] = proper_items[i][1]
labels_masks[i][:l] = 1
context[i] = proper_items[i][2]
return images, image_masks, labels, labels_masks, context
class Words:
def __init__(self, words_path):
with open(words_path, encoding='utf8') as f:
words = f.readlines()
self.words_dict = {words[i].strip(): i for i in range(len(words))}
self.words_index_dict = {i: words[i].strip() for i in range(len(words))}
self.text_expression = r'\\text ?{ ?([\w .?!,\)\():;]+) ?}'
def __len__(self):
return len(self.words_dict)
def encode(self, labels):
label_index = []
altered = False
for item in labels:
try:
label_index.append(self.words_dict[item])
except KeyError:
altered = True
if item.startswith('\\begin{array}') or item == '\\begin{aligned}':
label_index.append(self.words_dict['\\begin{matrix}'])
elif item == '\\end{array}' or item == '\\end{aligned}':
label_index.append(self.words_dict['\\end{matrix}'])
elif item.startswith('\\text'):
text = re.findall(self.text_expression, item)
for character in text[0]:
try:
label_index.append(self.words_dict[character])
except KeyError:
if character == ' ':
pass
else:
raise KeyError
elif item.startswith('\\vec'):
expression = r'\\vec ?{ ?([\w]+) ?}'
text = re.findall(expression, item)
label_index.append(self.words_dict['\\overrightarrow'])
label_index.append(self.words_dict['{'])
for character in text:
try:
label_index.append(self.words_dict[character])
except KeyError:
print(character)
raise KeyError
label_index.append(self.words_dict['}'])
elif item =='\\top':
label_index.append(self.words_dict['T'])
elif item == '\\quad' or item == '\\qquad' or item == '\\':
pass
# label_index.append(self.words_dict['\'])
elif item == '\\Leftrightarrow':
label_index.append(self.words_dict['\\Rightarrow'])
elif item == '\\left(' or item == '\\right(':
label_index.append(self.words_dict['('])
elif item == '\\left)' or item == '\\right)':
label_index.append(self.words_dict[')'])
elif item == '\\left|' or item == '\\right|' or item == '\\left\\rvert' or item == '\\right\\rvert' or item == '\\left\\lvert' or item == '\\right\\lvert':
label_index.append(self.words_dict['|'])
elif item == '\\left.' or item == '\\right.':
label_index.append(self.words_dict['.'])
elif item.startswith('\\operatorname') or item.startswith('\\mathrm'):
expression = r'{ ?([\w~]+) ?}'
text = re.findall(expression, item)
for character in text[0]:
try:
label_index.append(self.words_dict[character])
except KeyError:
if character == '~':
pass
else:
raise KeyError
elif item == '&':
pass
elif item == '%':
label_index.append(self.words_dict['\\%'])
elif item == '\\underbrace':
pass
elif item == '\\hat{i}':
label_index.append(self.words_dict['\\uparrow'])
elif item == '\\hat{x}':
label_index.append(self.words_dict['X'])
elif item == '\\hat{y}':
label_index.append(self.words_dict['Y'])
elif item == '\\hat{z}':
label_index.append(self.words_dict['Z'])
elif item == '\\backslash':
label_index.append(self.words_dict['|'])
elif item == '\\mathrm{I}':
label_index.append(self.words_dict['\\left['])
elif item == '\\mathbb{Z}':
label_index.append(self.words_dict['Z'])
elif item == '\\mathbb{R}':
label_index.append(self.words_dict['R'])
elif item == '\\mathbb{k}':
label_index.append(self.words_dict['K'])
elif item == '\\mathbb{C}':
label_index.append(self.words_dict['C'])
elif item == '\\ldots':
label_index.append(self.words_dict['\\cdots'])
elif item == '\\right':
pass
elif item == '\\rangle':
label_index.append(self.words_dict['>'])
elif item == '\\langle':
label_index.append(self.words_dict['<'])
elif item == '\\simeq':
label_index.append(self.words_dict['='])
elif item == '\\hat':
pass
elif item == '\\longrightarrow' or item == '\\hookleftarrow' or item == '\\leftrightarrow' or item == '\\mapsto' or item == '\\longleftrightarrow' or '\\ll':
label_index.append(self.words_dict['\\rightarrow'])
elif item == '\\Downarrow':
label_index.append(self.words_dict['\\downarrow'])
elif item == '\\wedge':
pass
elif item == '\\vee':
pass
elif item == '\\Gamma':
label_index.append(self.words_dict['V'])
elif item == '\\searrow':
label_index.append(self.words_dict['\\downarrow'])
elif item == '\\hline':
pass
elif item == '\\Phi':
pass
elif item == '\\dot{B}':
label_index.append(self.words_dict['B'])
else:
raise KeyError(f'\'{item}\' is not a key, comes from {labels}')
return label_index, altered
def encode2(self, labels):
label_index = []
altered = False
for item in labels:
try:
label_index.append(self.words_dict[item])
except KeyError:
if item.startswith('\\begin{array}') or item == '\\begin{aligned}':
label_index.append(self.words_dict['\\begin{matrix}'])
elif item == '\\end{array}' or item == '\\end{aligned}':
label_index.append(self.words_dict['\\end{matrix}'])
elif item.startswith('\\text'):
text = re.findall(self.text_expression, item)
for character in text[0]:
try:
_ = self.words_dict[character]
label_index.append(self.words_dict[character])
except KeyError:
if character == ' ':
pass
else:
raise KeyError
elif item.startswith('\\operatorname'):
expression = r'{ ?([\w~]+) ?}'
text = re.findall(expression, item)
for character in text[0]:
try:
_ = self.words_dict[character]
label_index.append(self.words_dict[character])
except KeyError:
if character == '~':
pass
else:
raise KeyError
elif item.startswith('\\mathrm'):
expression = r'{ ?([\w~]+) ?}'
text = re.findall(expression, item)
for character in text[0]:
try:
label_index.append(self.words_dict[character])
except KeyError:
if character == '~':
pass
else:
raise KeyError
elif item.startswith('\\vec'):
expression = r'\\vec ?{ ?([\w]+) ?}'
text = re.findall(expression, item)
label_index.append(self.words_dict['\\vec'])
label_index.append(self.words_dict['{'])
for character in text:
try:
label_index.append(self.words_dict[character])
except KeyError:
raise KeyError
label_index.append(self.words_dict['}'])
elif item.startswith('\\hat'):
expression = r'\\hat ?{ ?([\w]+) ?}'
text = re.findall(expression, item)
label_index.append(self.words_dict['\\hat'])
label_index.append(self.words_dict['{'])
for character in text:
try:
label_index.append(self.words_dict[character])
except KeyError:
print(character)
raise KeyError
label_index.append(self.words_dict['}'])
elif item == '\\ldots':
label_index.append(self.words_dict['\\cdots'])
elif item == '\\left(' or item == '\\right(':
label_index.append(self.words_dict['('])
elif item == '\\left)' or item == '\\right)':
label_index.append(self.words_dict[')'])
elif item == '\\left|' or item == '\\right|' or item == '\\left\\rvert' or item == '\\right\\rvert' or item == '\\left\\lvert' or item == '\\right\\lvert':
label_index.append(self.words_dict['|'])
elif item == '\\left.' or item == '\\right.':
label_index.append(self.words_dict['.'])
elif item == '\\left[' or item == '\\right[':
label_index.append(self.words_dict['['])
elif item == '\\left]' or item == '\\right]':
label_index.append(self.words_dict[']'])
elif item == '\\left\\{':
label_index.append(self.words_dict['\\{'])
elif item == '\\right' or item == '\\left':
pass #formatting
elif item == '\\left\\lvert' or item == '\\right\\lvert':
label_index.append(self.words_dict['\\lvert'])
elif item == '\\left\\rvert' or item == '\\right\\rvert':
label_index.append(self.words_dict['\\rvert'])
elif item == '\\leqslant':
label_index.append(self.words_dict['\\leq'])
elif item == '\\geqslant':
label_index.append(self.words_dict['\\geq'])
else:
raise ValueError(item, 'is not recognized in the dictionary', labels)
return label_index, altered
def encode3(self, labels):
label_index = []
altered = False
for item in labels:
try:
_ = self.words_dict[item]
label_index.append(item)
except KeyError:
label_index.append(item)
return label_index, altered
def decode(self, label_index):
label = ' '.join([self.words_index_dict[int(item)] for item in label_index])
return label
def decode2(self, label_index):
label = [self.words_index_dict[int(item)] for item in label_index]
return label
collate_fn_dict = {
'collate_fn': collate_fn,
'collate_fn_context': collate_fn_context
}
def test1():
words = Words(r'/data/leuven/351/vsc35170/echmer/data/words.txt')
test1 = TelevicDataset(0, r'/data/leuven/351/vsc35170/echmer/data/Televic/mathpix_processed_data/train.txt', r'/data/leuven/351/vsc35170/echmer/data/Televic/formatted_questions.txt', words)
start_time = time.time()
altered = 0
for i in range(len(test1)):
print(i,'/',len(test1))
im, lab, emb = test1.__getitem__(i)
end_time = time.time()
elapsed_time = end_time - start_time
print(f'Altered: {altered}/{len(test1)}')
print(f"Elapsed time: {elapsed_time:.3f} seconds")
def test2():
from utils import load_config
config_file = r'/data/leuven/351/vsc35170/echmer/config_televic_desktop_inference.yaml'
params = load_config(config_file)
train_loader, _ = get_televic_dataset(params)
for i, batch in enumerate(train_loader):
print(str(i+1),'/',len(train_loader))
images, image_masks, labels, labels_masks, contexts = batch
print('Images:', images[0])
print('Context:', contexts[0])
if __name__ == '__main__':
test2()