-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
243 lines (211 loc) · 6.61 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
import os
import torch
import h5py
import json
import glob
from torch.utils import data
from utils import get_filename
import pandas as pd
import numpy as np
import albumentations as A
from albumentations.pytorch.transforms import ToTensorV2
class LstmDataset(data.Dataset):
def __init__(self, paths, cnn_feat="resnet50", dataset="tvsum", use_augs=False):
super().__init__()
self.paths = paths
self.cnn_feat = cnn_feat
self.use_augs = use_augs
if dataset == "tvsum":
self.dataset = h5py.File(
"datasets/eccv16_dataset_tvsum_google_pool5.h5", "r"
)
else:
self.dataset = h5py.File(
"datasets/eccv16_dataset_summe_google_pool5.h5", "r"
)
f = open(f"id_to_key_map_{dataset}.json")
self.id_key_map = json.load(f)
f.close()
self.change_points = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["change_points"][...]
for key in list(self.dataset.keys())
],
)
)
self.nfps = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["n_frame_per_seg"][...]
for key in list(self.dataset.keys())
],
)
)
self.picks = dict(
zip(
list(self.dataset.keys()),
[self.dataset[key]["picks"][...] for key in list(self.dataset.keys())],
)
)
self.features = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["features"][...]
for key in list(self.dataset.keys())
],
)
)
self.user_summary = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["user_summary"][...]
for key in list(self.dataset.keys())
],
)
)
self.n_frames = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["n_frames"][()]
for key in list(self.dataset.keys())
],
)
)
def __getitem__(self, i):
path = self.paths[i]
id = get_filename(path)
key = self.id_key_map[id]
user_summary = self.user_summary[key]
change_points = self.change_points[key]
nfps = self.nfps[key]
picks = torch.LongTensor(self.picks[key])
n_frames = self.n_frames[key]
if self.cnn_feat == "resnet50":
if self.use_augs:
files = f"aug_cnn_feats/{id}/*"
files = sorted(glob.glob(files))
index = np.random.randint(8)
path = files[index]
feature = torch.load(path)
else:
feature = torch.load(path)
feature = feature[picks, :]
else:
feature = self.features[key]
return {
"feature": feature,
"user_summary": user_summary,
"id": id,
"change_points": change_points,
"nfps": nfps,
"picks": picks,
"n_frames": n_frames,
}
def __len__(self):
return len(self.paths)
class VideoDataset(data.Dataset):
def __init__(self, paths, dataset="tvsum"):
self.paths = list(
map(
lambda x: "videos_npy/" + x.split("/")[-1].split(".")[0] + ".npy", paths
)
)
f = open(f"id_to_key_map_{dataset}.json")
self.id_key_map = json.load(f)
f.close()
self.d_name = dataset
if dataset == "tvsum":
self.dataset = h5py.File(
"datasets/eccv16_dataset_tvsum_google_pool5.h5", "r"
)
else:
self.dataset = h5py.File(
"datasets/eccv16_dataset_summe_google_pool5.h5", "r"
)
self.change_points = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["change_points"][...]
for key in list(self.dataset.keys())
],
)
)
self.nfps = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["n_frame_per_seg"][...]
for key in list(self.dataset.keys())
],
)
)
self.picks = dict(
zip(
list(self.dataset.keys()),
[self.dataset[key]["picks"][...] for key in list(self.dataset.keys())],
)
)
self.user_summary = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["user_summary"][...]
for key in list(self.dataset.keys())
],
)
)
self.n_frames = dict(
zip(
list(self.dataset.keys()),
[
self.dataset[key]["n_frames"][()]
for key in list(self.dataset.keys())
],
)
)
self.transforms = self.get_transforms()
def get_transforms(self):
return A.Compose(
[
A.Resize(224, 224, p=1.0),
A.Normalize(
mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), p=1.0
),
ToTensorV2(),
]
)
def __getitem__(self, i):
path = self.paths[i]
id = get_filename(path)
key = self.id_key_map[id]
user_summary = self.user_summary[key]
change_points = self.change_points[key]
nfps = self.nfps[key]
picks = self.picks[key]
n_frames = self.n_frames[key]
video = np.load(path)
video = video[picks, :, :, :]
if self.transforms:
transformed_images = []
for image in video:
image = self.transforms(image=image)["image"]
transformed_images.append(image)
video = torch.stack(transformed_images)
return {
"feature": video,
"user_summary": user_summary,
"id": id,
"change_points": change_points,
"nfps": nfps,
"picks": picks,
"n_frames": n_frames,
}
def __len__(self):
return len(self.paths)