-
Notifications
You must be signed in to change notification settings - Fork 4
/
corpus.py
173 lines (134 loc) · 6.09 KB
/
corpus.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
import pandas as pd
import numpy as np
import torch
import random
from torch.utils.data import Dataset
class Dictionary(object):
def __init__(self):
self.word2idx = {}
self.idx2word = []
self.idx2count = {}
def add_word(self, word):
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1
self.idx2count[len(self.idx2word) - 1] = 0
return self.word2idx[word]
def add_count(self, idx):
self.idx2count[idx] += 1
def __len__(self):
return len(self.idx2word)
class EpicCorpus(Dataset):
def __init__(self, pickle_file, csvfiles, num_class, num_gram, train=True):
self.verb_dict, self.noun_dict = Dictionary(), Dictionary()
verb_csv, noun_csv = csvfiles[0], csvfiles[1]
self.num_class = num_class
self.num_gram = num_gram
self.train = train
assert num_gram >= 2
# Update verb & noun dictionary, note that last token is '<mask>' token
with open(verb_csv, 'r') as f:
lines = f.readlines()
for line in lines[1:]:
idx, word = int(line.split(',')[0]), line.split(',')[1]
self.verb_dict.add_word(word)
self.verb_dict.add_word('<mask>')
with open(noun_csv, 'r') as f:
lines = f.readlines()
for line in lines[1:]:
idx, word = int(line.split(',')[0]), line.split(',')[1]
self.noun_dict.add_word(word)
self.noun_dict.add_word('<mask>')
self.verbs, self.nouns = self.tokenize(pd.read_pickle(pickle_file))
def tokenize(self, df_labels):
"""Tokenizes a epic-kitchens file."""
# Parse the pandas file
video_ids = sorted(list(set(df_labels['video_id'])))
verb_idss, noun_idss = [], []
for video_id in video_ids:
df_video = df_labels[df_labels['video_id'] == video_id]
df_video = df_video.sort_values(by='start_frame')
verb_class = list(df_video['verb_class'])
noun_class = list(df_video['noun_class'])
for verbidx in verb_class:
self.verb_dict.add_count(verbidx)
for nounidx in noun_class:
self.noun_dict.add_count(nounidx)
assert len(verb_class) == len(noun_class)
for ii in range(len(verb_class) - self.num_gram + 1):
verb_temp = []
noun_temp = []
for j in range(self.num_gram):
verb_temp.append(verb_class[ii + j])
noun_temp.append(noun_class[ii + j])
verb_idss.append(torch.tensor(verb_temp).type(torch.int64))
noun_idss.append(torch.tensor(noun_temp).type(torch.int64))
verb_ids = torch.stack(verb_idss, dim=0)
noun_ids = torch.stack(noun_idss, dim=0)
assert verb_ids.shape[0] == noun_ids.shape[0]
return verb_ids, noun_ids
def __len__(self):
return len(self.verbs)
def __getitem__(self, index):
verb, noun = self.verbs[index], self.nouns[index]
verb_input, noun_input = verb.clone().detach(), noun.clone().detach()
if self.train:
verb_mask_pos = np.random.choice(list(range(self.num_gram)))
noun_mask_pos = verb_mask_pos
verb_input[verb_mask_pos] = self.verb_dict.word2idx['<mask>']
noun_input[noun_mask_pos] = self.noun_dict.word2idx['<mask>']
else:
# For evaluating, test only the centre action
mask_pos = self.num_gram // 2
verb_input[mask_pos] = self.verb_dict.word2idx['<mask>']
noun_input[mask_pos] = self.noun_dict.word2idx['<mask>']
data = {'verb_input': verb_input, 'verb_target': verb, 'noun_input': noun_input, 'noun_target' : noun}
return data
class EgteaCorpus(Dataset):
def __init__(self, pickle_file, csvfiles, num_class, num_gram, train=True):
self.action_dict = Dictionary()
self.num_class = int(num_class)
self.num_gram = num_gram
self.train = train
action_csv = csvfiles[0]
assert num_gram >= 2
# Update action dictionary, note that last token is '<mask>' token
with open(action_csv, 'r') as f:
lines = f.readlines()
for line in lines:
idx, word = int(line.split(',')[0]), line.split(',')[1]
self.action_dict.add_word(word)
self.action_dict.add_word('<mask>')
self.actions = self.tokenize(pd.read_pickle(pickle_file))
def tokenize(self, df_labels):
"""Tokenizes a epic-kitchens file."""
# Parse the pandas file
video_ids = sorted(list(set(df_labels['video_name'])))
action_idss = []
for video_id in video_ids:
df_video = df_labels[df_labels['video_name'] == video_id]
df_video = df_video.sort_values(by='start_frame')
action_class = list(df_video['action_idx'])
for actionidx in action_class:
self.action_dict.add_count(actionidx)
for ii in range(len(action_class) - self.num_gram + 1):
action_temp = []
for j in range(self.num_gram):
action_temp.append(action_class[ii + j])
action_idss.append(torch.tensor(action_temp).type(torch.int64))
action_ids = torch.stack(action_idss, dim=0)
return action_ids
def __len__(self):
return len(self.actions)
def __getitem__(self, index):
action = self.actions[index]
action_input = action.clone().detach()
if self.train:
mask_pos = np.random.choice(list(range(self.num_gram)))
action_input[mask_pos] = self.action_dict.word2idx['<mask>']
else:
# For evaluating, test only the centre action
mask_pos = self.num_gram // 2
action_input[mask_pos] = self.action_dict.word2idx['<mask>']
data = {'input': action_input, 'target': action}
return data