-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataloader.py
285 lines (214 loc) · 11.7 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
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
import torch
import random
import os
from glob import glob
import subprocess
import numpy as np
from torch import stack
from torch.utils.data import Dataset as torchData
from torch.utils.data import DataLoader
from torchvision import transforms
from subprocess import Popen, PIPE
from CANF_VC.util.vision import imgloader, rgb_transform
from PIL import Image
libbpg_path = os.getenv('BPG') # Put your libbpg path here
class VideoTestSequence(torchData):
def __init__(self, root, lmda, dataset='U', sequence='Beauty', seq_len=600, GOP=12):
super(VideoTestSequence, self).__init__()
self.root = root
self.lmda = lmda
self.qp = {256: 37, 512: 32, 1024: 27, 2048: 22, 1440: 22}[lmda]
assert os.path.exists(os.path.join(self.root, dataset, sequence)), \
f'FileNotFoundError: sequence is not found (path: {os.path.join(self.root, dataset, sequence)})'
if seq_len % seq_len:
print(f'Warning: GOP={GOP} cannot divide seq_len={seq_len} ; Only the first {seq_len - seq_len % GOP} will be coded')
self.seq_name = [sequence]
seq_len = [seq_len - seq_len % GOP]
gop_size = [GOP]
dataset_name_list = [dataset]
seq_len = dict(zip(self.seq_name, seq_len))
gop_size = dict(zip(self.seq_name, gop_size))
dataset_name_list = dict(zip(self.seq_name, dataset_name_list))
self.gop_list = []
for seq_name in self.seq_name:
for gop_idx in range(seq_len[seq_name] // gop_size[seq_name]):
self.gop_list.append([dataset_name_list[seq_name],
seq_name,
1 + gop_size[seq_name] * gop_idx,
1 + gop_size[seq_name] * (gop_idx + 1)])
def __len__(self):
return len(self.gop_list)
def __getitem__(self, idx):
dataset_name, seq_name, frame_start, frame_end = self.gop_list[idx]
seed = random.randint(0, 1e9)
imgs = []
# First image of `imgs` will be BPG-compressed frame
for frame_idx in range(frame_start, frame_end):
random.seed(seed)
raw_path = os.path.join(self.root, dataset_name, seq_name, 'frame_{:d}.png'.format(frame_idx))
if frame_idx == frame_start:
img_root = os.path.join(self.root, 'bpg', str(self.qp), 'decoded', seq_name)
os.makedirs(img_root, exist_ok=True)
img_path = os.path.join(img_root, f'frame_{frame_idx}.png')
if not os.path.exists(img_path):
# Compress data on-the-fly when they are not previously compressed.
bin_path = img_path.replace('decoded', 'bin').replace('png', 'bin')
os.makedirs(os.path.dirname(bin_path), exist_ok=True)
os.makedirs(os.path.dirname(img_path), exist_ok=True)
subprocess.call(f'{libbpg_path}/bpgenc -f 444 -q {self.qp} -o {bin_path} {raw_path}'.split(' '))
subprocess.call(f'{libbpg_path}/bpgdec -o {img_path} {bin_path}'.split(' '))
imgs.append(transforms.ToTensor()(imgloader(img_path)))
imgs.append(transforms.ToTensor()(imgloader(raw_path)))
return dataset_name, seq_name, stack(imgs), frame_start
class VideoTestData(torchData):
def __init__(self, root, lmda, sequence=('U', 'B'), GOP=12):
super(VideoTestData, self).__init__()
assert GOP in [12, 16, 32], ValueError
self.root = root
self.lmda = lmda
self.qp = {256: 37, 512: 32, 1024: 27, 2048: 22, 4096: 22}[lmda]
self.seq_name = []
seq_len = []
gop_size = []
dataset_name_list = []
if 'U' in sequence:
self.seq_name.extend(['Beauty', 'Bosphorus', 'HoneyBee', 'Jockey', 'ReadySteadyGo', 'ShakeNDry', 'YachtRide'])
if GOP in [12, 16]:
seq_len.extend([600, 600, 600, 600, 600, 300, 600])
else:
seq_len.extend([96]*7)
gop_size.extend([GOP]*7)
dataset_name_list.extend(['U']*7)
if 'B' in sequence:
self.seq_name.extend(['Kimono1', 'BQTerrace', 'Cactus', 'BasketballDrive', 'ParkScene'])
if GOP in [12, 16]:
seq_len.extend([100]*5)
#seq_len.extend([240, 600, 500, 500, 240])
else:
seq_len.extend([96]*5)
gop_size.extend([GOP]*5)
dataset_name_list.extend(['B']*5)
if 'C' in sequence:
self.seq_name.extend(['BasketballDrill', 'BQMall', 'PartyScene', 'RaceHorses'])
if GOP in [12, 16]:
seq_len.extend([100]*4)
else:
seq_len.extend([96]*4)
gop_size.extend([GOP]*4)
dataset_name_list.extend(['C']*4)
if 'D' in sequence:
self.seq_name.extend(['BasketballPass', 'BQSquare', 'BlowingBubbles', 'RaceHorses1']) # Rename "RaceHorses" to avoid repetition
if GOP in [12, 16]:
seq_len.extend([100]*4)
else:
seq_len.extend([96]*4)
gop_size.extend([GOP]*4)
dataset_name_list.extend(['D']*4)
if 'E' in sequence:
self.seq_name.extend(['vidyo1', 'vidyo3', 'vidyo4'])
if GOP in [12, 16]:
seq_len.extend([100]*3)
else:
seq_len.extend([96]*3)
gop_size.extend([GOP]*3)
dataset_name_list.extend(['E']*3)
if 'M' in sequence:
MCL_list = []
for i in range(1, 31):
MCL_list.append('videoSRC'+str(i).zfill(2))
self.seq_name.extend(MCL_list)
if GOP in [12, 16]:
seq_len.extend([150, 150, 150, 150, 125, 125, 125, 125, 125, 150,
150, 150, 150, 150, 150, 150, 120, 125, 150, 125,
120, 120, 120, 120, 120, 150, 150, 150, 120, 150])
else:
seq_len.extend([96]*30)
gop_size.extend([GOP]*30)
dataset_name_list.extend(['M']*30)
seq_len = dict(zip(self.seq_name, seq_len))
gop_size = dict(zip(self.seq_name, gop_size))
dataset_name_list = dict(zip(self.seq_name, dataset_name_list))
self.gop_list = []
for seq_name in self.seq_name:
gop_num = seq_len[seq_name] // gop_size[seq_name]
for gop_idx in range(gop_num):
self.gop_list.append([dataset_name_list[seq_name],
seq_name,
1 + gop_size[seq_name] * gop_idx,
1 + gop_size[seq_name] * (gop_idx + 1)])
def __len__(self):
return len(self.gop_list)
def __getitem__(self, idx):
dataset_name, seq_name, frame_start, frame_end = self.gop_list[idx]
seed = random.randint(0, 1e9)
imgs = []
# First image of `imgs` will be BPG-compressed frame
for frame_idx in range(frame_start, frame_end):
random.seed(seed)
raw_path = os.path.join(self.root, dataset_name, seq_name, 'frame_{:d}.png'.format(frame_idx))
if frame_idx == frame_start:
img_root = os.path.join(self.root, 'bpg', str(self.qp), 'decoded', seq_name)
os.makedirs(img_root, exist_ok=True)
img_path = os.path.join(img_root, f'frame_{frame_idx}.png')
if not os.path.exists(img_path):
# Compress data on-the-fly when they are not previously compressed.
bin_path = img_path.replace('decoded', 'bin').replace('png', 'bin')
os.makedirs(os.path.dirname(bin_path), exist_ok=True)
os.makedirs(os.path.dirname(img_path), exist_ok=True)
subprocess.call(f'{libbpg_path}/bpgenc -f 444 -q {self.qp} -o {bin_path} {raw_path}'.split(' '))
subprocess.call(f'{libbpg_path}/bpgdec -o {img_path} {bin_path}'.split(' '))
imgs.append(transforms.ToTensor()(imgloader(img_path)))
imgs.append(transforms.ToTensor()(imgloader(raw_path)))
return dataset_name, seq_name, stack(imgs), frame_start
class BitstreamData(VideoTestData):
def __init__(self, root, lmda, sequence=('U', 'B'), GOP=12, bin_root='./bin', load_Iframe=True):
super(BitstreamData, self).__init__(root, lmda, sequence, GOP)
self.bin_root = bin_root
self.load_Iframe = load_Iframe # If False, ignore I-frame. Design for BPG-compressed I-frame
self.lmda = lmda
def __getitem__(self, idx):
dataset_name, seq_name, frame_start, frame_end = self.gop_list[idx]
filenames = []
for frame_idx in range(frame_start, frame_end):
filename = os.path.join(self.bin_root, dataset_name, seq_name, str(self.lmda), f'{frame_idx}.bin')
if frame_idx == frame_start and self.load_Iframe:
img_root = os.path.join(self.root, 'bpg', str(self.qp), 'decoded', seq_name)
os.makedirs(img_root, exist_ok=True)
img_path = os.path.join(img_root, f'frame_{frame_idx}.png')
if not os.path.exists(img_path):
# Compress data on-the-fly when they are not previously compressed.
bin_path = img_path.replace('decoded', 'bin').replace('png', 'bin')
os.makedirs(os.path.dirname(bin_path), exist_ok=True)
os.makedirs(os.path.dirname(img_path), exist_ok=True)
subprocess.call(f'{libbpg_path}/bpgenc -f 444 -q {self.qp} -o {bin_path} {raw_path}'.split(' '))
subprocess.call(f'{libbpg_path}/bpgdec -o {img_path} {bin_path}'.split(' '))
filenames.append(transforms.ToTensor()(imgloader(img_path)))
else:
filenames.append(filename)
return dataset_name, seq_name, filenames, frame_start
class BitstreamSequence(VideoTestSequence):
def __init__(self, root, lmda, dataset='U', sequence='Beauty', seq_len=600, GOP=12, bin_root='./bin', load_Iframe=True):
super(BitstreamSequence, self).__init__(root, lmda, dataset, sequence, seq_len, GOP)
self.bin_root = bin_root
self.load_Iframe = load_Iframe # If False, ignore I-frame. Design for BPG-compressed I-frame
self.lmda = lmda
def __getitem__(self, idx):
dataset_name, seq_name, frame_start, frame_end = self.gop_list[idx]
filenames = []
for frame_idx in range(frame_start, frame_end):
filename = os.path.join(self.bin_root, dataset_name, seq_name, str(self.lmda), f'{frame_idx}.bin')
if frame_idx == frame_start and self.load_Iframe:
img_root = os.path.join(self.root, 'bpg', str(self.qp), 'decoded', seq_name)
os.makedirs(img_root, exist_ok=True)
img_path = os.path.join(img_root, f'frame_{frame_idx}.png')
if not os.path.exists(img_path):
# Compress data on-the-fly when they are not previously compressed.
bin_path = img_path.replace('decoded', 'bin').replace('png', 'bin')
os.makedirs(os.path.dirname(bin_path), exist_ok=True)
os.makedirs(os.path.dirname(img_path), exist_ok=True)
subprocess.call(f'{libbpg_path}/bpgenc -f 444 -q {self.qp} -o {bin_path} {raw_path}'.split(' '))
subprocess.call(f'{libbpg_path}/bpgdec -o {img_path} {bin_path}'.split(' '))
filenames.append(transforms.ToTensor()(imgloader(img_path)))
else:
filenames.append(filename)
return dataset_name, seq_name, filenames, frame_start