-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_loading.py
568 lines (470 loc) · 16.4 KB
/
data_loading.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import math
import os.path
import pickle
import random
from collections.abc import Callable
from datetime import timedelta
from pathlib import Path, PurePosixPath, PureWindowsPath
from typing import Optional
import torch
from torch.utils.data import DataLoader, Dataset
from torch.utils.data import IterableDataset
import tqdm
from positional_embedding import offset_sequence_embedding
from positional_embedding import position_sequence_embedding
from positional_embedding import timestep_embedding
from slider import Position
from slider.beatmap import Beatmap
from slider.beatmap import HitObject
from slider.beatmap import Slider
from slider.beatmap import Spinner
from slider.curve import Catmull
from slider.curve import Linear
from slider.curve import MultiBezier
from slider.curve import Perfect
playfield_size = torch.tensor((512, 384))
feature_size = 19
def create_datapoint(time: timedelta, pos: Position, datatype: int) -> torch.Tensor:
features = torch.zeros(19)
features[0] = pos.x
features[1] = pos.y
features[2] = time.total_seconds() * 1000
features[datatype + 3] = 1
return features
def repeat_type(repeat: int) -> int:
if repeat < 4:
return repeat - 1
elif repeat % 2 == 0:
return 3
else:
return 4
def append_control_points(
datapoints: list[torch.Tensor],
slider: Slider,
datatype: int,
duration: timedelta,
):
control_point_count = len(slider.curve.points)
for i in range(1, control_point_count - 1):
time = slider.time + i / (control_point_count - 1) * duration
pos = slider.curve.points[i]
datapoints.append(create_datapoint(time, pos, datatype))
def get_data(hitobj: HitObject) -> torch.Tensor:
if isinstance(hitobj, Slider) and len(hitobj.curve.points) < 100:
datapoints = [
create_datapoint(
hitobj.time,
hitobj.position,
5 if hitobj.new_combo else 4,
),
]
assert hitobj.repeat >= 1
duration: timedelta = (hitobj.end_time - hitobj.time) / hitobj.repeat
if isinstance(hitobj.curve, Linear):
append_control_points(datapoints, hitobj, 9, duration)
elif isinstance(hitobj.curve, Catmull):
append_control_points(datapoints, hitobj, 8, duration)
elif isinstance(hitobj.curve, Perfect):
append_control_points(datapoints, hitobj, 7, duration)
elif isinstance(hitobj.curve, MultiBezier):
control_point_count = len(hitobj.curve.points)
for i in range(1, control_point_count - 1):
time = hitobj.time + i / (control_point_count - 1) * duration
pos = hitobj.curve.points[i]
if pos == hitobj.curve.points[i + 1]:
datapoints.append(create_datapoint(time, pos, 9))
elif pos != hitobj.curve.points[i - 1]:
datapoints.append(create_datapoint(time, pos, 6))
datapoints.append(
create_datapoint(hitobj.time + duration, hitobj.curve.points[-1], 10),
)
slider_end_pos = hitobj.curve(1)
datapoints.append(
create_datapoint(
hitobj.end_time,
slider_end_pos,
11 + repeat_type(hitobj.repeat),
),
)
return torch.stack(datapoints, 0)
if isinstance(hitobj, Spinner):
return torch.stack(
(
create_datapoint(hitobj.time, hitobj.position, 2),
create_datapoint(hitobj.end_time, hitobj.position, 3),
),
0,
)
return create_datapoint(
hitobj.time,
hitobj.position,
1 if hitobj.new_combo else 0,
).unsqueeze(0)
def beatmap_to_sequence(beatmap: Beatmap) -> torch.Tensor:
# Get the hit objects
hit_objects = beatmap.hit_objects(stacking=False)
data_chunks = [get_data(ho) for ho in hit_objects]
sequence = torch.concatenate(data_chunks, 0)
sequence = torch.swapaxes(sequence, 0, 1)
return sequence.float()
def random_flip(seq: torch.Tensor) -> torch.Tensor:
if random.random() < 0.5:
seq[0] = 512 - seq[0]
if random.random() < 0.5:
seq[1] = 384 - seq[1]
return seq
def calc_distances(seq: torch.Tensor) -> torch.Tensor:
offset = torch.roll(seq[:2, :], 1, 1)
offset[0, 0] = 256
offset[1, 0] = 192
seq_d = torch.linalg.vector_norm(seq[:2, :] - offset, ord=2, dim=0)
return seq_d
def split_and_process_sequence(
seq: torch.Tensor,
) -> tuple[tuple[torch.Tensor, torch.Tensor, torch.Tensor], int]:
seq_d = calc_distances(seq)
# Augment and normalize positions for diffusion
seq_x = random_flip(seq[:2, :]) / playfield_size.unsqueeze(1)
seq_o = seq[2, :]
seq_c = torch.concatenate(
[
timestep_embedding(seq_d, 128).T,
seq[3:, :],
],
0,
)
return (seq_x, seq_o, seq_c), seq.shape[1]
def split_and_process_sequence_no_augment(
seq: torch.Tensor,
) -> tuple[tuple[torch.Tensor, torch.Tensor, torch.Tensor], int]:
seq_d = calc_distances(seq)
# Augment and normalize positions for diffusion
seq_x = seq[:2, :] / playfield_size.to(seq.device).unsqueeze(1)
seq_o = seq[2, :]
seq_c = torch.concatenate(
[
timestep_embedding(seq_d, 128).T,
seq[3:, :],
],
0,
)
return (seq_x, seq_o, seq_c), seq.shape[1]
def load_and_process_beatmap(beatmap: Beatmap):
seq = beatmap_to_sequence(beatmap)
return split_and_process_sequence(seq)
def window_and_relative_time(seq, s, e):
seq_x, seq_o, seq_c = seq
x = seq_x[:, s:e]
# Obscure the absolute time by normalizing to zero and adding a random offset between zero and the max period
# We do this to make sure the offset embedding utilizes the full range of values, which is also the case when sampling the model
o = seq_o[s:e] - seq_o[s] + random.random() * 100000
c = seq_c[:, s:e]
return x, o, c
class BeatmapDatasetIterable:
__slots__ = (
"beatmap_files",
"beatmap_idx",
"seq_len",
"stride",
"index",
"current_idx",
"current_seq",
"current_seq_len",
"seq_index",
"seq_func",
"win_func",
)
def __init__(
self,
beatmap_files: list[str],
seq_len: int,
stride: int,
seq_func: Callable,
win_func: Callable,
):
self.beatmap_files = beatmap_files
self.seq_len = seq_len
self.stride = stride
self.index = 0
self.current_idx = 0
self.current_seq = None
self.current_seq_len = -1
self.seq_index = 0
self.seq_func = seq_func
self.win_func = win_func
def __iter__(self) -> "BeatmapDatasetIterable":
return self
def __next__(self) -> tuple[any, int]:
while (
self.current_seq is None
or self.seq_index + self.seq_len > self.current_seq_len
):
if self.index >= len(self.beatmap_files):
raise StopIteration
# Load the beatmap from file
beatmap_path = self.beatmap_files[self.index]
beatmap = Beatmap.from_path(beatmap_path)
self.current_idx = int(os.path.basename(beatmap_path)[:6])
self.current_seq, self.current_seq_len = self.seq_func(beatmap)
self.seq_index = random.randint(0, self.stride - 1)
self.index += 1
# Return the preprocessed hit objects as a sequence of overlapping windows
window = self.win_func(
self.current_seq,
self.seq_index,
self.seq_index + self.seq_len,
)
self.seq_index += self.stride
return window, self.current_idx
class InterleavingBeatmapDatasetIterable:
__slots__ = ("workers", "cycle_length", "index")
def __init__(
self,
beatmap_files: list[str],
iterable_factory: Callable,
cycle_length: int,
):
per_worker = int(math.ceil(len(beatmap_files) / float(cycle_length)))
self.workers = [
iterable_factory(
beatmap_files[
i * per_worker: min(len(beatmap_files), (i + 1) * per_worker)
]
)
for i in range(cycle_length)
]
self.cycle_length = cycle_length
self.index = 0
def __iter__(self) -> "InterleavingBeatmapDatasetIterable":
return self
def __next__(self) -> tuple[any, int]:
num = len(self.workers)
for _ in range(num):
try:
self.index = self.index % len(self.workers)
item = self.workers[self.index].__next__()
self.index += 1
return item
except StopIteration:
self.workers.remove(self.workers[self.index])
raise StopIteration
class BeatmapDataset(IterableDataset):
def __init__(
self,
dataset_path: str,
start: int,
end: int,
iterable_factory: Callable,
cycle_length: int = 1,
shuffle: bool = False,
beatmap_files: Optional[list[str]] = None,
):
super(BeatmapDataset).__init__()
self.dataset_path = dataset_path
self.start = start
self.end = end
self.iterable_factory = iterable_factory
self.cycle_length = cycle_length
self.shuffle = shuffle
self.beatmap_files = beatmap_files
def _get_beatmap_files(self) -> list[str]:
if self.beatmap_files is not None:
return self.beatmap_files
# Get a list of all beatmap files in the dataset path in the track index range between start and end
beatmap_files = []
track_names = ["Track" + str(i).zfill(5) for i in range(self.start, self.end)]
for track_name in track_names:
for beatmap_file in os.listdir(
os.path.join(self.dataset_path, track_name, "beatmaps"),
):
beatmap_files.append(
os.path.join(
self.dataset_path,
track_name,
"beatmaps",
beatmap_file,
),
)
return beatmap_files
def __iter__(self) -> InterleavingBeatmapDatasetIterable | BeatmapDatasetIterable:
beatmap_files = self._get_beatmap_files()
if self.shuffle:
random.shuffle(beatmap_files)
if self.cycle_length > 1:
return InterleavingBeatmapDatasetIterable(
beatmap_files,
self.iterable_factory,
self.cycle_length,
)
return self.iterable_factory(beatmap_files)
# Define a `worker_init_fn` that configures each dataset copy differently
def worker_init_fn(worker_id: int) -> None:
worker_info = torch.utils.data.get_worker_info()
dataset = worker_info.dataset # the dataset copy in this worker process
overall_start = dataset.start
overall_end = dataset.end
# configure the dataset to only process the split workload
per_worker = int(
math.ceil((overall_end - overall_start) / float(worker_info.num_workers)),
)
dataset.start = overall_start + worker_id * per_worker
dataset.end = min(dataset.start + per_worker, overall_end)
def get_beatmap_idx(name) -> dict[int, int]:
p = Path(__file__).with_name(name)
with p.open("rb") as f:
beatmap_idx = pickle.load(f)
return beatmap_idx
def get_beatmap_files(name: str, data_path: str) -> list[PurePosixPath]:
p = Path(name)
with p.open("rb") as f:
relative_beatmap_files = pickle.load(f)
beatmap_files = [PurePosixPath(data_path, *PureWindowsPath(f).parts) for f in relative_beatmap_files]
return beatmap_files
class BeatmapDatasetIterableFactory:
__slots__ = ("seq_len", "stride", "seq_func", "win_func")
def __init__(self, seq_len, stride, seq_func, win_func):
self.seq_len = seq_len
self.stride = stride
self.seq_func = seq_func
self.win_func = win_func
def __call__(self, *args, **kwargs):
beatmap_files = args[0]
return BeatmapDatasetIterable(
beatmap_files=beatmap_files,
seq_len=self.seq_len,
stride=self.stride,
seq_func=self.seq_func,
win_func=self.win_func,
)
class CachedDataset(Dataset):
__slots__ = "cached_data"
def __init__(self, cached_data):
self.cached_data = cached_data
def __getitem__(self, index):
return self.cached_data[index]
def __len__(self):
return len(self.cached_data)
def cache_dataset(
out_path: str,
dataset_path: str,
start: int,
end: int,
iterable_factory: Callable,
cycle_length=1,
beatmap_files: Optional[list[str]] = None,
):
dataset = BeatmapDataset(
dataset_path=dataset_path,
start=start,
end=end,
iterable_factory=iterable_factory,
cycle_length=cycle_length,
shuffle=False,
beatmap_files=beatmap_files,
)
print("Caching dataset...")
cached_data = []
for datum in tqdm.tqdm(dataset):
cached_data.append(datum)
torch.save(cached_data, out_path)
def get_cached_data_loader(
data_path: str,
batch_size: int = 1,
num_workers: int = 0,
shuffle: bool = False,
pin_memory: bool = False,
drop_last: bool = False,
):
cached_data = torch.load(data_path)
dataset = CachedDataset(cached_data)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=pin_memory,
drop_last=drop_last,
persistent_workers=num_workers > 0,
shuffle=shuffle,
)
return dataloader
def get_data_loader(
dataset_path: str,
start: int,
end: int,
iterable_factory: Callable,
cycle_length=1,
batch_size: int = 1,
num_workers: int = 0,
shuffle: bool = False,
pin_memory: bool = False,
drop_last: bool = False,
beatmap_files: Optional[list[str]] = None,
) -> DataLoader:
dataset = BeatmapDataset(
dataset_path=dataset_path,
start=start,
end=end,
iterable_factory=iterable_factory,
cycle_length=cycle_length,
shuffle=shuffle,
beatmap_files=beatmap_files,
)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
worker_init_fn=worker_init_fn,
num_workers=num_workers,
pin_memory=pin_memory,
drop_last=drop_last,
persistent_workers=num_workers > 0,
)
return dataloader
def main(args):
dataloader = get_data_loader(
dataset_path=args.data_path,
start=0,
end=16291,
iterable_factory=BeatmapDatasetIterableFactory(
128,
16,
load_and_process_beatmap,
window_and_relative_time,
),
cycle_length=1,
batch_size=args.batch_size,
num_workers=args.num_workers,
shuffle=False,
pin_memory=False,
drop_last=True,
)
if args.mode == "plotfirst":
import matplotlib.pyplot as plt
for (x, o, c), y in dataloader:
x = torch.swapaxes(x, 1, 2) # (N, T, C)
c = torch.swapaxes(c, 1, 2) # (N, T, E)
print(x.shape, o.shape, c.shape, y.shape)
batch_pos_emb = position_sequence_embedding(x * playfield_size, 128)
print(batch_pos_emb.shape)
batch_offset_emb = offset_sequence_embedding(o / 10, 128)
print(batch_offset_emb.shape)
print(y)
for j in range(args.batch_size):
fig, axs = plt.subplots(3, figsize=(5, 20))
axs[0].imshow(batch_pos_emb[j])
axs[1].imshow(batch_offset_emb[j])
axs[2].imshow(c[j])
print(y[j])
plt.show()
break
elif args.mode == "benchmark":
for _ in tqdm.tqdm(dataloader, total=7000, smoothing=0.01):
pass
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--data-path", type=str, required=True)
parser.add_argument("--mode", type=str, required=True)
parser.add_argument("--batch-size", type=int, default=1)
parser.add_argument("--num-workers", type=int, default=0)
args = parser.parse_args()
main(args)