File size: 6,181 Bytes
e6a6383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Torch dataset object for synthetically rendered spatial data.
"""

import os
import json
import random
from pathlib import Path
import logging

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scaper
import torch
import torchaudio
import torchaudio.transforms as AT
from random import randrange

class FSDSoundScapesDataset(torch.utils.data.Dataset):  # type: ignore
    """
    Base class for FSD Sound Scapes dataset
    """

    _labels = [
    "Acoustic_guitar", "Applause", "Bark", "Bass_drum",
    "Burping_or_eructation", "Bus", "Cello", "Chime", "Clarinet",
    "Computer_keyboard", "Cough", "Cowbell", "Double_bass",
    "Drawer_open_or_close", "Electric_piano", "Fart", "Finger_snapping",
    "Fireworks", "Flute", "Glockenspiel", "Gong", "Gunshot_or_gunfire",
    "Harmonica", "Hi-hat", "Keys_jangling", "Knock", "Laughter", "Meow",
    "Microwave_oven", "Oboe", "Saxophone", "Scissors", "Shatter",
    "Snare_drum", "Squeak", "Tambourine", "Tearing", "Telephone",
    "Trumpet", "Violin_or_fiddle", "Writing"]

    def __init__(self, input_dir, dset='', sr=None,
                 resample_rate=None, max_num_targets=1):
        assert dset in ['train', 'val', 'test'], \
            "`dset` must be one of ['train', 'val', 'test']"
        self.dset = dset
        self.max_num_targets = max_num_targets
        self.fg_dir = os.path.join(input_dir, 'FSDKaggle2018/%s' % dset)
        if dset in ['train', 'val']:
            self.bg_dir = os.path.join(
                input_dir,
                'TAU-acoustic-sounds/'
                'TAU-urban-acoustic-scenes-2019-development')
        else:
            self.bg_dir = os.path.join(
                input_dir,
                'TAU-acoustic-sounds/'
                'TAU-urban-acoustic-scenes-2019-evaluation')
        logging.info("Loading %s dataset: fg_dir=%s bg_dir=%s" %
                     (dset, self.fg_dir, self.bg_dir))

        self.samples = sorted(list(
            Path(os.path.join(input_dir, 'jams', dset)).glob('[0-9]*')))

        jamsfile = os.path.join(self.samples[0], 'mixture.jams')
        _, jams, _, _ = scaper.generate_from_jams(
            jamsfile, fg_path=self.fg_dir, bg_path=self.bg_dir)
        _sr = jams['annotations'][0]['sandbox']['scaper']['sr']
        assert _sr == sr, "Sampling rate provided does not match the data"

        if resample_rate is not None:
            self.resampler = AT.Resample(sr, resample_rate)
            self.sr = resample_rate
        else:
            self.resampler = lambda a: a
            self.sr = sr

    def _get_label_vector(self, labels):
        """
        Generates a multi-hot vector corresponding to `labels`.
        """
        vector = torch.zeros(len(FSDSoundScapesDataset._labels))

        for label in labels:
            idx = FSDSoundScapesDataset._labels.index(label)
            assert vector[idx] == 0, "Repeated labels"
            vector[idx] = 1 

        return vector

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

    def __getitem__(self, idx):
        sample_path = self.samples[idx]
        jamsfile = os.path.join(sample_path, 'mixture.jams')

        mixture, jams, ann_list, event_audio_list = scaper.generate_from_jams(
            jamsfile, fg_path=self.fg_dir, bg_path=self.bg_dir)
        isolated_events = {}
        for e, a in zip(ann_list, event_audio_list[1:]):
            # 0th event is background
            isolated_events[e[2]] = a
        gt_events = list(pd.read_csv(
            os.path.join(sample_path, 'gt_events.csv'), sep='\t')['label'])

        mixture = torch.from_numpy(mixture).permute(1, 0)
        mixture = self.resampler(mixture.to(torch.float))

        if self.dset == 'train':
            labels = random.sample(gt_events, randrange(1,self.max_num_targets+1))
        elif self.dset == 'val':
            labels = gt_events[:idx%self.max_num_targets+1]
        elif self.dset == 'test':
            labels = gt_events[:self.max_num_targets]
        label_vector = self._get_label_vector(labels)

        gt = torch.zeros_like(
            torch.from_numpy(event_audio_list[1]).permute(1, 0))
        for l in labels:
            gt = gt + torch.from_numpy(isolated_events[l]).permute(1, 0)
        gt = self.resampler(gt.to(torch.float))

        return mixture, label_vector, gt #, jams

def tensorboard_add_sample(writer, tag, sample, step, params):
    """
    Adds a sample of FSDSynthDataset to tensorboard.
    """
    if params['resample_rate'] is not None:
        sr = params['resample_rate']
    else:
        sr = params['sr']
    resample_rate = 16000 if sr > 16000 else sr

    m, l, gt, o = sample
    m, gt, o = (
        torchaudio.functional.resample(_, sr, resample_rate).cpu()
        for _ in (m, gt, o))

    def _add_audio(a, audio_tag, axis, plt_title):
        for i, ch in enumerate(a):
            axis.plot(ch, label='mic %d' % i)
            writer.add_audio(
                '%s/mic %d' % (audio_tag, i), ch.unsqueeze(0), step, resample_rate)
        axis.set_title(plt_title)
        axis.legend()

    for b in range(m.shape[0]):
        label = []
        for i in range(len(l[b, :])):
            if l[b, i] == 1:
                label.append(FSDSoundScapesDataset._labels[i])

        # Add waveforms
        rows = 3 # input, output, gt
        fig = plt.figure(figsize=(10, 2 * rows))
        axes = fig.subplots(rows, 1, sharex=True)
        _add_audio(m[b], '%s/sample_%d/0_input' % (tag, b), axes[0], "Mixed")
        _add_audio(o[b], '%s/sample_%d/1_output' % (tag, b), axes[1], "Output (%s)" % label)
        _add_audio(gt[b], '%s/sample_%d/2_gt' % (tag, b), axes[2], "GT (%s)" % label)
        writer.add_figure('%s/sample_%d/waveform' % (tag, b), fig, step)

def tensorboard_add_metrics(writer, tag, metrics, label, step):
    """
    Add metrics to tensorboard.
    """
    vals = np.asarray(metrics['scale_invariant_signal_noise_ratio'])

    writer.add_histogram('%s/%s' % (tag, 'SI-SNRi'), vals, step)

    label_names = [FSDSoundScapesDataset._labels[torch.argmax(_)] for _ in label]
    for l, v in zip(label_names, vals):
        writer.add_histogram('%s/%s' % (tag, l), v, step)