-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataloader.py
165 lines (124 loc) · 5.41 KB
/
dataloader.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
import numpy as np
import torch
import os
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import pandas as pd
import ast
import json
from PIL import Image
from munch import munchify
import time
import random
def ids_to_multinomial(id, categories):
""" label encoding
Returns:
1d array, multimonial representation, e.g. [1,0,1,0,0,...]
"""
id_to_idx = {id: index for index, id in enumerate(categories)}
return id_to_idx[id]
class AVQA_dataset(Dataset):
def __init__(self, args, label,
audios_feat_dir, visual_feat_dir,
audios_patch_dir, visual_patch_dir,
qst_prompt_dir, qst_feat_dir,
transform=None, mode_flag='train'):
self.args = args
samples = json.load(open('../dataset/split_que_id/music_avqa_train.json', 'r'))
# Question
ques_vocab = ['<pad>']
ans_vocab = []
i = 0
for sample in samples:
i += 1
question = sample['question_content'].rstrip().split(' ')
question[-1] = question[-1][:-1]
p = 0
for pos in range(len(question)):
if '<' in question[pos]:
question[pos] = ast.literal_eval(sample['templ_values'])[p]
p += 1
for wd in question:
if wd not in ques_vocab:
ques_vocab.append(wd)
if sample['anser'] not in ans_vocab:
ans_vocab.append(sample['anser'])
self.ques_vocab = ques_vocab
self.ans_vocab = ans_vocab
self.word_to_ix = {word: i for i, word in enumerate(self.ques_vocab)}
self.samples = json.load(open(label, 'r'))
self.max_len = 14 # question length
self.audios_feat_dir = audios_feat_dir
self.visual_feat_dir = visual_feat_dir
self.audios_patch_dir = audios_patch_dir
self.visual_patch_dir = visual_patch_dir
self.qst_prompt_dir = qst_prompt_dir
self.qst_feat_dir = qst_feat_dir
self.transform = transform
def __len__(self):
return len(self.samples)
def get_lstm_embeddings(self, question_input, sample):
question = sample['question_content'].rstrip().split(' ')
question[-1] = question[-1][:-1]
p = 0
for pos in range(len(question)):
if '<' in question[pos]:
question[pos] = ast.literal_eval(sample['templ_values'])[p]
p += 1
if len(question) < self.max_len:
n = self.max_len - len(question)
for i in range(n):
question.append('<pad>')
idxs = [self.word_to_ix[w] for w in question]
ques = torch.tensor(idxs, dtype=torch.long)
return ques
def get_frames_spatial(self, video_name):
frames_path = os.path.join(self.frames_dir, video_name)
frames_spatial = image_info(frames_path) # [T, 3, 224, 224]
return frames_spatial
def __getitem__(self, idx):
sample = self.samples[idx]
name = sample['video_id']
question_id = sample['question_id']
audios_feat = np.load(os.path.join(self.audios_feat_dir, name + '.npy'))
visual_feat = np.load(os.path.join(self.visual_feat_dir, name + '.npy'))
question_feat = np.load(os.path.join(self.qst_feat_dir, str(question_id) + '.npy'))
question_prompt = np.load(os.path.join(self.qst_prompt_dir, str(question_id) + '.npy'))
audios_patch_feat = np.load(os.path.join(self.audios_patch_dir, name + '.npy'))
visual_patch_feat = np.load(os.path.join(self.visual_patch_dir, name + '.npy'))
### answer
answer = sample['anser']
answer_label = ids_to_multinomial(answer, self.ans_vocab)
answer_label = torch.from_numpy(np.array(answer_label)).long()
sample = {'video_name': name,
'audios_feat': audios_feat,
'visual_feat': visual_feat,
'audios_patch_feat': audios_patch_feat,
'visual_patch_feat': visual_patch_feat,
'question_feat': question_feat,
'question_prompt': question_prompt,
'answer_label': answer_label,
'question_id': question_id}
if self.transform:
sample = self.transform(sample)
return sample
class ToTensor(object):
def __call__(self, sample):
video_name = sample['video_name']
audios_feat = sample['audios_feat']
visual_feat = sample['visual_feat']
audios_patch_feat = sample['audios_patch_feat']
visual_patch_feat = sample['visual_patch_feat']
question_feat = sample['question_feat']
question_prompt = sample['question_prompt']
answer_label = sample['answer_label']
question_id = sample['question_id']
return {'video_name': video_name,
'audios_feat': torch.from_numpy(audios_feat),
'visual_feat': torch.from_numpy(visual_feat),
'audios_patch_feat': torch.from_numpy(audios_patch_feat),
'visual_patch_feat': torch.from_numpy(visual_patch_feat).to(torch.float32),
'question_feat': sample['question_feat'],
'question_prompt': sample['question_prompt'],
'answer_label': answer_label,
'question_id':question_id}