Exemplo n.º 1
0
def test_generator_fake_loader():

    dataset_path = 'data/UrbanSound8K/'
    urbansound8k.default_path = dataset_path
    data = urbansound8k.load_dataset()
    folds, test = urbansound8k.folds(data)

    data_length = 16
    batch_size = 8
    frames = 72
    bands = 32
    n_classes = 10

    def zero_loader(s):
        #assert
        return numpy.zeros((bands, frames, 1))

    fold = folds[0][0]
    X = fold[0:data_length]
    Y = fold.classID[0:data_length]

    g = train.dataframe_generator(X,
                                  Y,
                                  loader=zero_loader,
                                  batchsize=batch_size,
                                  n_classes=n_classes)

    n_batches = 3
    batches = list(itertools.islice(g, n_batches))
    assert len(batches) == n_batches
    assert len(batches[0]) == 2  # X,y
    assert batches[0][0].shape == (batch_size, bands, frames, 1)
    assert batches[0][1].shape == (batch_size, n_classes)
def test_precompute():

    settings = dict(
        feature='mels',
        samplerate=16000,
        n_mels=32,
        fmin=0,
        fmax=8000,
        n_fft=512,
        hop_length=256,
        augmentations=12,
    )

    dir = './pre2'
    if os.path.exists(dir):
        shutil.rmtree(dir)

    workdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/'))

    data = urbansound8k.load_dataset()
    urbansound8k.maybe_download_dataset(workdir)

    d = os.path.join(dir, features.settings_id(settings))
    expect_path = features.feature_path(data.iloc[0], d)
    assert not os.path.exists(expect_path), expect_path

    preprocess.precompute(data[0:4], settings, out_dir=d, verbose=0, force=True, n_jobs=2)

    assert os.path.exists(expect_path), expect_path
def loadfiles():
    dataset = urbansound8k.load_dataset()
    dataset['embeddings'] = None
    # """print(dataset)
    for index, sample in dataset.iterrows():
        path = embedding_path(sample,"/Users/giovannibirkelund/Documents/GitHub/ESC-CNN-microcontroller/data/embeddedSounds")
        data = np.load(path)
        emb = data['embedding']
        dataset.at[index,'embeddings'] = emb
    return dataset
def embedFiles(datasetPath, newDir):
    soundfiles = urbansound8k.load_dataset()

    model = openl3.models.load_embedding_model(input_repr="mel256", content_type="env",
                                           embedding_size=512)
    for index in range(len(soundfiles)):
        sample = soundfiles.iloc[index, :] #fetch each sample from the dataset
        path = urbansound8k.sample_path(sample,datasetPath) #find the path given the sample, and the directory the files lay in.
        fold = sample["fold"] #get the fold for the sound from the sample
        differentDir = newDir+str(fold) #assign directory to save file in by adding the respective fold to the path
        openl3.process_file(path, suffix=None, output_dir=differentDir,model=model, content_type="env", embedding_size=512, hop_size=0.5, verbose=True) #save the embedded file in its respective fold in the differentDir directory
def test_folds():
    data = urbansound8k.load_dataset()
    f = urbansound8k.folds(data)
    assert len(f) == 10
Exemplo n.º 6
0
import sys
from microesc import urbansound8k
import pandas
import numpy

data = urbansound8k.load_dataset()
by_class = data.groupby('class')  
foreground_ratio = by_class.apply(lambda r: numpy.mean(r['salience'] == 1))

table = pandas.DataFrame({
    'Samples': by_class.count()['classID'],
    'Duration (avg)': by_class.apply(lambda r: '%.2f s' % (r.end-r.start).mean()),
    'In foreground': [ "{} %".format(int(100*r)) for r in foreground_ratio ]
})
out = table.to_latex(header=True, index=True, column_format="lrrr")
print(out)

outpath = sys.argv[1] 
with open(outpath, 'w') as f:
    f.write(out)