-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
executable file
·43 lines (31 loc) · 1.12 KB
/
data_loader.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
import numpy as np
from torch.utils.data import Dataset
class GQA(Dataset):
def __init__(self, root, split='train', transform=None):
with open(f'dataset/gqa_{split}.pkl', 'rb') as f:
self.data = pickle.load(f)
self.root = root
self.split = split
def __getitem__(self, index):
imgFile, question, answer = self.data[index]
return imgFile, question, len(question), answer
def __len__(self):
return len(self.data)
'''
Load a batch
def collate_data(batch):
images, lengths, answers = [], [], []
batch_size = len(batch)
max_len = max(map(lambda x: len(x[1]), batch))
questions = np.zeros((batch_size, max_len), dtype=np.int64)
sort_by_len = sorted(batch, key=lambda x: len(x[1]), reverse=True)
for i, b in enumerate(sort_by_len):
image, question, length, answer = b
images.append(image)
length = len(question)
questions[i, :length] = question
lengths.append(length)
answers.append(answer)
return torch.stack(images), torch.from_numpy(questions), \
lengths, torch.LongTensor(answers)
'''