My custom Dataloader

username: Mohamed_Nabih
cooked: <p>import os<br>
import warnings<br>
import torchaudio<br>
from torch.utils.data import Dataset<br>
from torchaudio.datasets.utils import (download_url, extract_archive, unicode_csv_reader, walk_files)<br>
URL = “<a href="https://www.kaggle.com/mfekadu/darpa-timit-acousticphonetic-continuous-speech" rel="nofollow noopener">https://www.kaggle.com/mfekadu/darpa-timit-acousticphonetic-continuous-speech</a>”<br>
FOLDER_IN_ARCHIVE = “timitcorpus”<br>
def load_timit_item(fileid, path, ext_audio):</p>
<h1>Read lables</h1>
<p>labels = [int© for c in fileid.split("_")]</p>
<h1>Read wav</h1>
<p>file_audio = os.path.join(path, fileid + ext_audio)<br>
waveform, sample_rate, lables<br>
class timit(Dataset):<br>
_ext_audio = “.wav”<br>
def <strong>init</strong>(self, root, url=URL, folder_in_archive = FOLDER_IN_ARCHIVE,<br>
downlaod = False, transform = None,<br>
target_transform = None):<br>
if transform is not None or target_transform is not None:<br>
warning.warn(“Show warning”, DeprecationWarning)<br>
self.transform = transform<br>
self.target_transform = target_transform<br>
archive = os.path.basename(url)<br>
archive = os.path.join(root, archive)<br>
self.path._path = os.path.join (root, folder_in_archive)<br>
if not os.path.isdir(self.path):<br>
raise RuntimeError (“Dataset not found. Please use <code>download=True</code>, to download it.”)<br>
walker = walk_files(self._path, suffix=self._ext_audio, prefix=False,<br>
remove_suffix=True)<br>
self._walker = list(walker)<br>
def <strong>getitem</strong>(self, n):<br>
fileid = self.walker[n]<br>
item = load_timit_item(fileid, self._path, self._ext_audio)<br>
waveform, sample_rate, labels = item<br>
if self.transform is not None:<br>
waveform = self.transform(waveform)<br>
if self.target_transform is not None:<br>
labels = self.target_transform(labels)<br>
return waveform, sample_rate, labels<br>
def <strong>len</strong>(self):<br>
return len (self._walker)</p>
<p><strong>When I run this command</strong><br>
timit_data = torchaudio.datasets.timit(’.’, download=True)<br>
<strong>it gives me tourchaudio.datasets has no attribute timit</strong>  I am following Yeson dataloader</p>

username: ptrblck
cooked: <p><code>torchaudio.datasets</code> doesn’t seem to have the “timit” dataset as seen <a href="https://pytorch.org/audio/datasets.html">here</a>.<br>
Your code is currently a bit hard to read and you can format it by wrapping it into three backticks ```.</p>
<p>Are you defining this dataset somehow?<br>
E.g. <code>load_timit_item</code> seems to be undefined as well.</p>

username: Amin_Jun
cooked: <p><a class="mention" href="/u/mohamed_nabih">@Mohamed_Nabih</a> I suggest you use something like this:</p>
<pre><code class="lang-auto">import os

import torchaudio
from torch.utils.data import Dataset
from torchaudio.datasets.utils import walk_files


def main():
    timit = Timit('../TIMIT/')
    x = timit[0]


def load_timit(file: str):
    data_path = os.path.splitext(file)[0]
    with open(data_path + '.TXT', 'r') as txt_file:
        _, __, transcript = next(iter(txt_file)).strip().split(" ", 2)
    with open(data_path + '.WRD', 'r') as word_file:
        words = [l.strip().split(' ') for l in word_file]
        words = [(int(hd), int(tl), w) for (hd, tl, w) in words]
    with open(data_path + '.PHN', 'r') as phn_file:
        phonemes = [l.strip().split(' ') for l in phn_file]
        phonemes = [(int(hd), int(tl), w) for (hd, tl, w) in phonemes]
    wav, sr = torchaudio.load(data_path + '.WAV')
    return data_path, wav, transcript, words, phonemes


class Timit(Dataset):
    def __init__(self, root: str):
        self.root = root
        self.walker = list(walk_files(root, suffix='.WAV', prefix=True))

    def __getitem__(self, item):
        return load_timit(self.walker[item])

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


if __name__ == '__main__':
    main()
</code></pre>
<p>It simply loads all files (regardless of train test). You could do the train test split by simply changing root=’…/TIMIT/data/TEST’ or similar thing for train.</p>