Custom DataLoader For Audio Classification

Dear All,
I am very new to PyTorch. I am working towards designing of data loader for my audio classification task.
Training and Test Data:
I have a set of audion files (.wav files). I extracted the spectrogram features from each file and saved them into a database created using .h5py file in python.
Design of data loader:
I want to create a custom data loader in such a way that the created mini-batch always contains 50% of data that belong to two predefined clPreformatted textasses and remaining samples in minibatch are from classes other than two predefined classes.
In my case I have 10 classes, I always want the minibatch such that It contains 50% of samples from class 4 and class 6 and other 50 % samples from any of the class from [1,2,3,5,7,8,910].

Here is my piece of code for a custom class

import os
import torch
import numpy as np
import h5py
from import dataset
import pandas as pd

class AEData_Test():
def init(self,transform=None):
def len(self):
return len(self.annotations)
def getitem(self,index):
with h5py.File(‘New_Devlopment.hdf5’, ‘r’) as f:
SG_Data = f[key][()]
SG_Label= torch.tensor(int(self.annotations.lioc[index,1]))
if self.transform:
return (SG_Data,SG_Label)
Please guide me how to modify my code to complete the desired task.


If the number of samples could be “approx. 50% class4 and class 6 vs. the rest”, you could use a WeigthedRandomSampler as described in this post.
The sampler would accept the sample weights and uses these to draw each sample.
Since this is a random process, the exact number of class samples is not exactly defined.

1 Like