File size: 29,774 Bytes
05d6e12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import os
import random
import sys
import time

import librosa
import numpy as np
import soundfile
import torch
from torch.utils.data import Dataset
from tqdm import tqdm

from onsets_and_frames import constants
from onsets_and_frames.constants import DEFAULT_DEVICE, N_KEYS, SAMPLE_RATE
from onsets_and_frames.mel import melspectrogram
from onsets_and_frames.midi_utils import (
    midi_to_frames,
    save_midi_alignments_and_predictions,
)
from onsets_and_frames.utils import (
    get_diff,
    get_logger,
    get_peaks,
    shift_label,
    smooth_labels,
)


class EMDATASET(Dataset):
    def __init__(
        self,
        audio_path="NoteEM_audio",
        tsv_path="NoteEM_tsv",
        labels_path="NoteEm_labels",
        groups=None,
        sequence_length=None,
        seed=42,
        device=DEFAULT_DEVICE,
        instrument_map=None,
        update_instruments=False,
        transcriber=None,
        conversion_map=None,
        pitch_shift=True,
        pitch_shift_limit=5,
        keep_eval_files=False,
        n_eval=1,
        evaluation_list=None,
        only_eval=False,
        save_to_memory=False,
        smooth_labels=False,
        use_onset_mask=False,
    ):
        # Get the dataset logger (logging system should already be initialized by train.py)
        self.logger = get_logger("dataset")

        self.audio_path = audio_path
        self.tsv_path = tsv_path
        self.labels_path = labels_path
        self.sequence_length = sequence_length
        self.device = device
        self.random = np.random.RandomState(seed)
        self.groups = groups
        self.conversion_map = conversion_map
        self.eval_file_list = []
        self.file_list = self.files(
            self.groups,
            pitch_shift=pitch_shift,
            keep_eval_files=keep_eval_files,
            n_eval=n_eval,
            evaluation_list=evaluation_list,
            pitch_shift_limit=pitch_shift_limit,
        )
        self.save_to_memory = save_to_memory
        self.smooth_labels = smooth_labels
        self.use_onset_mask = use_onset_mask
        self.pitch_shift_limit = pitch_shift_limit

        self.logger.debug("Save to memory is %s", self.save_to_memory)
        self.logger.info("len file list %d", len(self.file_list))
        self.logger.info("\n\n")

        if instrument_map is None:
            self.get_instruments(conversion_map=conversion_map)
        else:
            self.instruments = instrument_map
            if update_instruments:
                self.add_instruments()
        self.transcriber = transcriber
        if only_eval:
            return
        self.load_pts(self.file_list)
        self.data = []
        self.logger.info("Reading files...")
        for input_files in tqdm(self.file_list, desc="creating data list"):
            flac, _ = input_files
            audio_len = librosa.get_duration(path=flac)
            minutes = int(np.ceil(audio_len / 60))
            copies = minutes
            for _ in range(copies):
                self.data.append(input_files)
        random.shuffle(self.data)

    def flac_to_pt_path(self, flac):
        pt_fname = os.path.basename(flac).replace(".flac", ".pt")
        pt_path = os.path.join(self.labels_path, pt_fname)
        return pt_path

    def __len__(self):
        return len(self.data)

    def files(
        self,
        groups,
        pitch_shift=True,
        keep_eval_files=False,
        n_eval=1,
        evaluation_list=None,
        pitch_shift_limit=5,
    ):
        self.path = self.audio_path
        tsvs_path = self.tsv_path
        self.logger.info("tsv path: %s", tsvs_path)
        self.logger.info("Evaluation list: %s", evaluation_list)
        res = []
        self.logger.info("keep eval files: %s", keep_eval_files)
        self.logger.info("n eval: %d", n_eval)
        for group in groups:
            tsvs = os.listdir(tsvs_path + os.sep + group)
            tsvs = sorted(tsvs)
            if keep_eval_files and evaluation_list is None:
                eval_tsvs = tsvs[:n_eval]
                tsvs = tsvs[n_eval:]
            elif keep_eval_files and evaluation_list is not None:
                eval_tsvs_names = [
                    i.split("#")[0].split(".flac")[0].split(".tsv")[0]
                    for i in evaluation_list
                ]
                eval_tsvs = [
                    i
                    for i in tsvs
                    if i.split("#")[0].split(".tsv")[0] in eval_tsvs_names
                ]
                tsvs = [i for i in tsvs if i not in eval_tsvs]
            else:
                eval_tsvs = []
            self.logger.info("len tsvs: %d", len(tsvs))

            tsvs_names = [t.split(".tsv")[0].split("#")[0] for t in tsvs]
            eval_tsvs_names = [t.split(".tsv")[0].split("#")[0] for t in eval_tsvs]
            for shft in range(-5, 6):
                if shft != 0 and not pitch_shift or abs(shft) > pitch_shift_limit:
                    continue
                curr_fls_pth = self.path + os.sep + group + "#{}".format(shft)

                fls = os.listdir(curr_fls_pth)
                orig_files = fls
                # print(f"files names before\n {fls}")
                fls = [
                    i for i in fls if i.split("#")[0] in tsvs_names
                ]  # in case we dont have the corresponding midi
                missing_fls = [i for i in orig_files if i not in fls]
                if len(missing_fls) > 0:
                    self.logger.warning("missing files: %s", missing_fls)
                fls_names = [i.split("#")[0].split(".flac")[0] for i in fls]
                tsvs = [
                    i for i in tsvs if i.split(".tsv")[0].split("#")[0] in fls_names
                ]
                assert len(tsvs) == len(fls)
                # print(f"files names after\n {fls}")
                fls = sorted(fls)

                if shft == 0:
                    eval_fls = os.listdir(curr_fls_pth)
                    # print(f"files names\n {eval_fls}")
                    eval_fls = [
                        i for i in eval_fls if i.split("#")[0] in eval_tsvs_names
                    ]  # in case we dont have the corresponding midi
                    eval_fls_names = [i.split("#")[0] for i in eval_fls]
                    eval_tsvs = [
                        i
                        for i in eval_tsvs
                        if i.split(".tsv")[0].split("#")[0] in eval_fls_names
                    ]
                    assert len(eval_fls_names) == len(eval_tsvs_names)
                    # print(f"files names\n {eval_fls}")
                    eval_fls = sorted(eval_fls)
                    for f, t in zip(eval_fls, eval_tsvs):
                        self.eval_file_list.append(
                            (
                                curr_fls_pth + os.sep + f,
                                tsvs_path + os.sep + group + os.sep + t,
                            )
                        )

                for f, t in zip(fls, tsvs):
                    res.append(
                        (
                            curr_fls_pth + os.sep + f,
                            tsvs_path + os.sep + group + os.sep + t,
                        )
                    )

        for flac, tsv in res:
            if (
                os.path.basename(flac).split("#")[0].split(".flac")[0]
                != os.path.basename(tsv).split("#")[0].split(".tsv")[0]
            ):
                self.logger.warning("found mismatch in the files: ")
                self.logger.warning("flac: %s", os.path.basename(flac).split("#")[0])
                self.logger.warning("tsv: %s", os.path.basename(tsv).split("#")[0])
                self.logger.warning("please check the input files")
                exit(1)
        return res

    def get_instruments(self, conversion_map=None):
        instruments = set()
        for _, f in self.file_list:
            events = np.loadtxt(f, delimiter="\t", skiprows=1)
            curr_instruments = set(events[:, -1])
            if conversion_map is not None:
                curr_instruments = {
                    conversion_map[c] if c in conversion_map else c
                    for c in curr_instruments
                }
            instruments = instruments.union(curr_instruments)
        instruments = [int(elem) for elem in instruments if elem < 115]
        if conversion_map is not None:
            instruments = [i for i in instruments if i in conversion_map]
        instruments = list(set(instruments))
        if 0 in instruments:
            piano_ind = instruments.index(0)
            instruments.pop(piano_ind)
            instruments.insert(0, 0)
        self.instruments = instruments
        self.instruments = list(
            set(self.instruments) - set(range(88, 104)) - set(range(112, 150))
        )
        self.logger.info("Dataset instruments: %s", self.instruments)
        self.logger.info("Total: %d instruments", len(self.instruments))

    def add_instruments(self):
        for _, f in self.file_list:
            events = np.loadtxt(f, delimiter="\t", skiprows=1)
            curr_instruments = set(events[:, -1])
            new_instruments = curr_instruments - set(self.instruments)
            self.instruments += list(new_instruments)
        instruments = [int(elem) for elem in self.instruments if (elem < 115)]
        self.instruments = instruments

    def __getitem__(self, index):
        data = self.load(*self.data[index])
        # result = dict(path=data['path'])
        midi_length = len(data["label"])
        n_steps = self.sequence_length // constants.HOP_LENGTH
        if midi_length < n_steps:
            step_begin = 0
            step_end = midi_length
        else:
            step_begin = self.random.randint(max(midi_length - n_steps, 1))
            step_end = step_begin + n_steps
        begin = step_begin * constants.HOP_LENGTH
        end = begin + self.sequence_length

        audio = (
            data["audio"][begin:end].float().div_(32768.0)
        )  # torch.ShortTensor → float
        label = data["label"][step_begin:step_end].clone()  # torch.Tensor

        if audio.shape[0] < self.sequence_length:
            pad_amt = self.sequence_length - audio.shape[0]
            audio = torch.cat([audio, torch.zeros(pad_amt, dtype=audio.dtype)], dim=0)

        if label.shape[0] < n_steps:
            pad_amt = n_steps - label.shape[0]
            label = torch.cat(
                [label, torch.zeros((pad_amt, *label.shape[1:]), dtype=label.dtype)],
                dim=0,
            )

        audio = torch.clamp(audio, -1.0, 1.0)
        result = {"path": data["path"], "audio": audio, "label": label}
        if "velocity" in data:
            result["velocity"] = data["velocity"][step_begin:step_end, ...]
            result["velocity"] = result["velocity"].float() / 128.0

        if result["label"].max() < 3:
            result["onset"] = result["label"].float()
        else:
            result["onset"] = (result["label"] == 3).float()

        result["offset"] = (result["label"] == 1).float()
        result["frame"] = (result["label"] > 1).float()

        if self.smooth_labels:
            result["onset"] = smooth_labels(result["onset"])
        if self.use_onset_mask:
            if "onset_mask" in data:
                result["onset_mask"] = data["onset_mask"][
                    step_begin:step_end, ...
                ].float()
            else:
                result["onset_mask"] = torch.ones_like(result["onset"]).float()
            if "frame_mask" in data:
                result["frame_mask"] = data["frame_mask"][
                    step_begin:step_end, ...
                ].float()
            else:
                result["frame_mask"] = torch.ones_like(result["frame"]).float()

        shape = result["frame"].shape
        keys = N_KEYS
        new_shape = shape[:-1] + (shape[-1] // keys, keys)
        result["big_frame"] = result["frame"]
        result["frame"], _ = result["frame"].reshape(new_shape).max(axis=-2)

        # if 'frame_mask' not in data:
        #     result['frame_mask'] = torch.ones_like(result['frame']).to(self.device).float()

        result["big_offset"] = result["offset"]
        result["offset"], _ = result["offset"].reshape(new_shape).max(axis=-2)
        result["group"] = self.data[index][0].split(os.sep)[-2].split("#")[0]

        return result

    def load(self, audio_path, tsv_path):
        if self.save_to_memory:
            data = self.pts[audio_path]
        else:
            data = torch.load(self.flac_to_pt_path(audio_path))
        if len(data["audio"].shape) > 1:
            data["audio"] = (data["audio"].float().mean(dim=-1)).short()
        if "label" in data:
            return data
        else:
            piece, part = audio_path.split(os.sep)[-2:]
            piece_split = piece.split("#")
            if len(piece_split) == 2:
                piece, shift1 = piece_split
            else:
                piece, shift1 = "#".join(piece_split[:2]), piece_split[-1]
            part_split = part.split("#")
            if len(part_split) == 2:
                part, shift2 = part_split
            else:
                part, shift2 = "#".join(part_split[:2]), part_split[-1]
            shift2, _ = shift2.split(".")
            assert shift1 == shift2
            shift = shift1
            assert shift != 0
            orig = audio_path.replace("#{}".format(shift), "#0")
            if self.save_to_memory:
                orig_data = self.pts[orig]
            else:
                orig_data = torch.load(self.flac_to_pt_path(orig))
            res = {}
            res["label"] = shift_label(orig_data["label"], int(shift))
            res["path"] = audio_path
            res["audio"] = data["audio"]
            if "velocity" in orig_data:
                res["velocity"] = shift_label(orig_data["velocity"], int(shift))
            if "onset_mask" in orig_data:
                res["onset_mask"] = shift_label(orig_data["onset_mask"], int(shift))
            if "frame_mask" in orig_data:
                res["frame_mask"] = shift_label(orig_data["frame_mask"], int(shift))
            return res

    def load_pts(self, files):
        self.pts = {}
        self.logger.info("loading pts...")
        for flac, tsv in tqdm(files, desc="loading pts"):
            # print('flac, tsv', flac, tsv)
            if os.path.isfile(
                self.labels_path
                + os.sep
                + flac.split(os.sep)[-1].replace(".flac", ".pt")
            ):
                if self.save_to_memory:
                    self.pts[flac] = torch.load(
                        self.labels_path
                        + os.sep
                        + flac.split(os.sep)[-1].replace(".flac", ".pt")
                    )
            else:
                if flac.count("#") != 2:
                    self.logger.debug("two # in filename: %s", flac)
                audio, sr = soundfile.read(flac, dtype="int16")
                if len(audio.shape) == 2:
                    audio = audio.astype(float).mean(axis=1)
                else:
                    audio = audio.astype(float)
                audio = audio.astype(np.int16)
                self.logger.debug("audio len: %d", len(audio))
                assert sr == SAMPLE_RATE
                audio = torch.ShortTensor(audio)
                if "#0" not in flac:
                    assert "#" in flac
                    data = {"audio": audio}
                    if self.save_to_memory:
                        self.pts[flac] = data
                    torch.save(data, self.flac_to_pt_path(flac))
                    continue
                midi = np.loadtxt(tsv, delimiter="\t", skiprows=1)
                unaligned_label = midi_to_frames(
                    midi, self.instruments, conversion_map=self.conversion_map
                )
                if len(self.instruments) == 1:
                    unaligned_label = unaligned_label[:, -N_KEYS:]
                if len(unaligned_label) < self.sequence_length // constants.HOP_LENGTH:
                    diff = self.sequence_length // constants.HOP_LENGTH - len(
                        unaligned_label
                    )
                    pad = torch.zeros(
                        (diff, unaligned_label.shape[1]), dtype=unaligned_label.dtype
                    )
                    unaligned_label = torch.cat((unaligned_label, pad), dim=0)

                group = flac.split(os.sep)[-2].split("#")[0]
                data = dict(
                    path=self.labels_path + os.sep + flac.split(os.sep)[-1],
                    audio=audio,
                    unaligned_label=unaligned_label,
                    group=group,
                    BON=float("inf"),
                    BON_VEC=np.full(unaligned_label.shape[1], float("inf")),
                )

                torch.save(data, self.flac_to_pt_path(flac))
                if self.save_to_memory:
                    self.pts[flac] = data

    def update_pts_counting(
        self,
        transcriber,
        counting_window_length,
        POS=1.1,
        NEG=-0.001,
        FRAME_POS=0.5,
        to_save=None,
        first=False,
        update=True,
        BEST_DIST=False,
        peak_size=3,
        BEST_DIST_VEC=False,
        counting_window_hop=0,
    ):
        self.logger.info("Updating pts...")
        self.logger.info("First %s", first)
        total_counting_time = 0.0  # Initialize total time for counting-based alignment

        self.logger.info("POS, NEG: %s, %s", POS, NEG)
        if to_save is not None:
            os.makedirs(to_save, exist_ok=True)
        self.logger.info("There are %d pts", len(self.pts))
        update_count = 0
        sys.stdout.flush()
        onlt_pitch_0_files = [f for f in self.file_list if "#0" in f[0]]
        for input_files in tqdm(onlt_pitch_0_files, desc="updating pts"):
            flac, tsv = input_files
            data = torch.load(self.flac_to_pt_path(flac))
            if "unaligned_label" not in data:
                self.logger.warning("No unaligned labels for %s", flac)
                continue
            audio_inp = data["audio"].float() / 32768.0
            MAX_TIME = 5 * 60 * SAMPLE_RATE
            audio_inp_len = len(audio_inp)
            if audio_inp_len > MAX_TIME:
                n_segments = int(np.ceil(audio_inp_len / MAX_TIME))
                self.logger.debug("Long audio, splitting to %d segments", n_segments)
                seg_len = MAX_TIME
                onsets_preds = []
                offset_preds = []
                frame_preds = []
                for i_s in range(n_segments):
                    curr = (
                        audio_inp[i_s * seg_len : (i_s + 1) * seg_len]
                        .unsqueeze(0)
                        .cuda()
                    )
                    curr_mel = melspectrogram(
                        curr.reshape(-1, curr.shape[-1])[:, :-1]
                    ).transpose(-1, -2)
                    (
                        curr_onset_pred,
                        curr_offset_pred,
                        _,
                        curr_frame_pred,
                        curr_velocity_pred,
                    ) = transcriber(curr_mel)
                    onsets_preds.append(curr_onset_pred)
                    offset_preds.append(curr_offset_pred)
                    frame_preds.append(curr_frame_pred)
                onset_pred = torch.cat(onsets_preds, dim=1)
                offset_pred = torch.cat(offset_preds, dim=1)
                frame_pred = torch.cat(frame_preds, dim=1)
            else:
                audio_inp = audio_inp.unsqueeze(0).cuda()
                mel = melspectrogram(
                    audio_inp.reshape(-1, audio_inp.shape[-1])[:, :-1]
                ).transpose(-1, -2)
                onset_pred, offset_pred, _, frame_pred, _ = transcriber(mel)
            self.logger.debug("Done predicting.")

            # We assume onset predictions are of length N_KEYS * (len(instruments) + 1),
            # first N_KEYS classes are the first instrument, next N_KEYS classes are the next instrument, etc.,
            # and last N_KEYS classes are for pitch regardless of instrument
            # Currently, frame and offset predictions are only N_KEYS classes.
            onset_pred = onset_pred.detach().squeeze().cpu()
            frame_pred = frame_pred.detach().squeeze().cpu()

            PEAK_SIZE = peak_size
            self.logger.debug("PEAK_SIZE: %d", PEAK_SIZE)
            # we peak peak the onset prediction to only keep local maximum onsets
            if peak_size > 0:
                peaks = get_peaks(
                    onset_pred, PEAK_SIZE
                )  # we only want local peaks, in a 7-frame neighborhood, 3 to each side.
                onset_pred[~peaks] = 0

            unaligned_onsets = (data["unaligned_label"] == 3).float().numpy()

            onset_pred_np = onset_pred.numpy()
            frame_pred_np = frame_pred.numpy()

            pred_bag_of_notes = (onset_pred_np[:, -N_KEYS:] >= 0.5).sum(axis=0)
            gt_bag_of_notes = unaligned_onsets[:, -N_KEYS:].astype(bool).sum(axis=0)
            bon_dist = (((pred_bag_of_notes - gt_bag_of_notes) ** 2).sum()) ** 0.5

            pred_bag_of_notes_with_inst = (onset_pred_np >= 0.5).sum(axis=0)
            gt_bag_of_notes_with_inst = unaligned_onsets.astype(bool).sum(axis=0)
            bon_dist_vec = np.abs(
                pred_bag_of_notes_with_inst - gt_bag_of_notes_with_inst
            )

            bon_dist /= gt_bag_of_notes.sum()
            self.logger.debug("bag of notes dist: %f", bon_dist)
            ####

            aligned_onsets = np.zeros(onset_pred_np.shape, dtype=bool)
            aligned_frames = np.zeros(onset_pred_np.shape, dtype=bool)

            # This block is the main difference between the counting approach and the DTW approach.
            # In the counting approach we label the audio by counting note onsets: For each onset pitch class,
            # denote by K the number of times it occurs in the unaligned label. We simply take the K highest local
            # peaks predicted by the current model.
            # Split unaligned onsets into chunks of size counting_window_length
            self.logger.debug(
                "unaligned onsets shape: %s, counting window length: %d, counting window hop: %d",
                unaligned_onsets.shape,
                counting_window_length,
                counting_window_hop,
            )
            assert counting_window_hop <= counting_window_length
            if counting_window_hop == 0:
                counting_window_hop = counting_window_length

            num_chunks = (
                1
                if counting_window_length == 0
                else int(np.ceil(len(unaligned_onsets) / counting_window_hop))
            )

            self.logger.debug("number of chunks: %d", num_chunks)
            start_time = time.time()
            for chunk_idx in range(num_chunks):
                start_idx = chunk_idx * counting_window_hop
                if counting_window_length == 0:
                    end_idx = max(len(unaligned_onsets), len(onset_pred_np))
                else:
                    end_idx = min(
                        start_idx + counting_window_length, len(unaligned_onsets)
                    )
                chunk_onsets = unaligned_onsets[start_idx:end_idx]
                chunk_onsets_count = (
                    (data["unaligned_label"][start_idx:end_idx, :] == 3)
                    .sum(dim=0)
                    .numpy()
                )

                for f, f_count in enumerate(chunk_onsets_count):
                    if f_count == 0:
                        continue
                    f_most_likely = np.sort(
                        onset_pred_np[start_idx:end_idx, f].argsort()[::-1][:f_count]
                    )
                    f_most_likely += start_idx  # Adjust indices to the original size
                    aligned_onsets[f_most_likely, f] = 1

                    f_unaligned = chunk_onsets[:, f].nonzero()
                    assert len(f_unaligned) == 1
                    f_unaligned = f_unaligned[0]

            counting_duration = time.time() - start_time
            total_counting_time += counting_duration
            self.logger.debug(
                "Counting alignment for file '%s' took %.2f seconds.",
                flac,
                counting_duration,
            )

            # Pseudo labels, Pos bigger than 1 is equivalent to not using pseudo labels
            pseudo_onsets = (onset_pred_np >= POS) & (~aligned_onsets)

            onset_label = np.maximum(pseudo_onsets, aligned_onsets)

            # in this project we do not train frame stack but we calculate the labeels anyways
            pseudo_frames = np.zeros(pseudo_onsets.shape, dtype=pseudo_onsets.dtype)
            pseudo_offsets = np.zeros(pseudo_onsets.shape, dtype=pseudo_onsets.dtype)
            for t, f in zip(*onset_label.nonzero()):
                t_off = t
                while (
                    t_off < len(pseudo_frames)
                    and frame_pred[t_off, f % N_KEYS] >= FRAME_POS
                ):
                    t_off += 1
                pseudo_frames[t:t_off, f] = 1
                if t_off < len(pseudo_offsets):
                    pseudo_offsets[t_off, f] = 1
            frame_label = np.maximum(pseudo_frames, aligned_frames)
            offset_label = get_diff(frame_label, offset=True)

            label = np.maximum(2 * frame_label, offset_label)
            label = np.maximum(3 * onset_label, label).astype(np.uint8)

            if to_save is not None:
                save_midi_alignments_and_predictions(
                    to_save,
                    data["path"],
                    self.instruments,
                    aligned_onsets,
                    aligned_frames,
                    onset_pred_np,
                    frame_pred_np,
                    prefix="",
                    group=data["group"],
                )
            prev_bon_dist = data.get("BON", float("inf"))
            prev_bon_dist_vec = data.get("BON_VEC", None)
            if update:
                if BEST_DIST_VEC:
                    self.logger.debug("Updated Labels")
                    if prev_bon_dist_vec is None:
                        raise ValueError(
                            "BEST_DIST_VEC is True but no previous BON_VEC found"
                        )
                    prev_label = data["label"]
                    new_label = torch.from_numpy(label).byte()
                    if first:
                        prev_label = new_label
                        update_count += 1
                    else:
                        updated_flag = False
                        num_pitches_updated = 0
                        for k in range(prev_label.shape[1]):
                            if prev_bon_dist_vec[k] > bon_dist_vec[k]:
                                prev_label[:, k] = new_label[:, k]
                                prev_bon_dist_vec[k] = bon_dist_vec[k]
                                num_pitches_updated += 1
                                updated_flag = True
                        if updated_flag:
                            update_count += 1
                        self.logger.debug("Updated %d pitches", num_pitches_updated)
                    data["label"] = prev_label
                    data["BON_VEC"] = prev_bon_dist_vec
                    self.logger.debug("saved updated pt")
                    torch.save(
                        data,
                        self.labels_path
                        + os.sep
                        + flac.split(os.sep)[-1]
                        .replace(".flac", ".pt")
                        .replace(".mp3", ".pt"),
                    )

                elif not BEST_DIST or bon_dist < prev_bon_dist:
                    update_count += 1
                    self.logger.debug("Updated Labels")

                    data["label"] = torch.from_numpy(label).byte()

                    data["BON"] = bon_dist
                    self.logger.debug("saved updated pt")
                    torch.save(
                        data,
                        self.labels_path
                        + os.sep
                        + flac.split(os.sep)[-1]
                        .replace(".flac", ".pt")
                        .replace(".mp3", ".pt"),
                    )

            if bon_dist < prev_bon_dist:
                self.logger.debug(
                    "Bag of notes distance improved from %f to %f",
                    prev_bon_dist,
                    bon_dist,
                )
                data["BON"] = bon_dist

                if to_save is not None and BEST_DIST:
                    os.makedirs(to_save + "/BEST_BON", exist_ok=True)
                    save_midi_alignments_and_predictions(
                        to_save + "/BEST_BON",
                        data["path"],
                        self.instruments,
                        aligned_onsets,
                        aligned_frames,
                        onset_pred_np,
                        frame_pred_np,
                        prefix="BEST_BON",
                        group=data["group"],
                        use_time=False,
                    )

        self.logger.info(
            "Updated %d pts out of %d", update_count, len(onlt_pitch_0_files)
        )
        self.logger.info(
            "Total counting alignment time for all files: %.2f seconds.", total_counting_time
        )