Last active
January 10, 2020 05:57
-
-
Save hasithsura/182649b27d6b4cb4831f5e2d2e898188 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from torch.utils.data import Dataset, DataLoader | |
from tqdm import tqdm | |
class ESC50Data(Dataset): | |
def __init__(self, base, df, in_col, out_col): | |
self.df = df | |
self.data = [] | |
self.labels = [] | |
self.c2i={} | |
self.i2c={} | |
self.categories = sorted(df[out_col].unique()) | |
for i, category in enumerate(self.categories): | |
self.c2i[category]=i | |
self.i2c[i]=category | |
for ind in tqdm(range(len(df))): | |
row = df.iloc[ind] | |
file_path = os.path.join(base,row[in_col]) | |
self.data.append(spec_to_image(get_melspectrogram_db(file_path))[np.newaxis,...]) | |
self.labels.append(self.c2i[row['category']]) | |
def __len__(self): | |
return len(self.data) | |
def __getitem__(self, idx): | |
return self.data[idx], self.labels[idx] | |
train_data = ESC50Data('audio', train, 'filename', 'category') | |
valid_data = ESC50Data('audio', valid, 'filename', 'category') | |
train_loader = DataLoader(train_data, batch_size=16, shuffle=True) | |
valid_loader = DataLoader(valid_data, batch_size=16, shuffle=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment