UnboundLocalError: local variable 'data' referenced before assignment

username: Kuldeep_Rana
cooked: <p>I am facing a “local variable ‘data’ problem” when I am doing train our model; Error is like this</p>
<p>Traceback (most recent call last):<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/trainnew.py”, line 92, in <br>
run(args)<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/trainnew.py”, line 54, in run<br>
trainer.run(train_loader, dev_loader, num_epochs=args.epochs)<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/trainer.py”, line 221, in run<br>
cv = self.eval(dev_loader)<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/trainer.py”, line 208, in eval<br>
for egs in data_loader:<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/dataset.py”, line 143, in <strong>iter</strong><br>
for chunks in self.eg_loader:<br>
File “/home/speech70809/anaconda3/envs/speech_denoiser/lib/python3.9/site-packages/torch/utils/data/dataloader.py”, line 521, in <strong>next</strong><br>
data = self._next_data()<br>
File “/home/speech70809/anaconda3/envs/speech_denoiser/lib/python3.9/site-packages/torch/utils/data/dataloader.py”, line 561, in _next_data<br>
data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
File “/home/speech70809/anaconda3/envs/speech_denoiser/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/home/speech70809/anaconda3/envs/speech_denoiser/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/dataset.py”, line 42, in <strong>getitem</strong><br>
ref = [reader[key] for reader in self.ref]<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/dataset.py”, line 42, in <br>
ref = [reader[key] for reader in self.ref]<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/audio.py”, line 120, in <strong>getitem</strong><br>
return self._load(index)<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/audio.py”, line 138, in _load<br>
samp_rate, samps = read_wav(<br>
File “/media/speech70809/Data01/speech_donoiser_new/main/nnet/libs/audio.py”, line 38, in read_wav<br>
samp_rate, samps_int16 = wf.read(fname)<br>
File “/home/speech70809/anaconda3/envs/speech_denoiser/lib/python3.9/site-packages/scipy/io/wavfile.py”, line 707, in read<br>
return fs, data<br>
UnboundLocalError: local variable ‘data’ referenced before assignment</p>

username: tom
cooked: <p>The error message seems like a bug in scipy/io/wavfile.py, possibly triggered by a corrupted wav file (if you can read some).<br>
You could try to loop over your dataset to find the problematic indices and delete those from the dataset.</p>
<p>Best regards</p>
<p>Thomas</p>

username: Kuldeep_Rana
cooked: <p>Thank you so much for your reply. But I am a beginner so how can I track corrupted files.</p>

username: tom
cooked: <p>Personally, I’d aim for the low-tech solution of writing a short program that loops over the dataset and sees which indices fail, remove the file that causes it and repeat. If that is too much work (which it is if there are many corrupted files), an alternative could be to use Python’s <a href="https://docs.python.org/3/library/os.html#os.walk" rel="noopener nofollow ugc">os.walk</a> to find all wav files and try to read them to get a list of files where it didn’t work and remove those from the dataset.</p>
<p>Best tregards</p>
<p>Thomas</p>

username: sambutle
cooked: <aside class="quote no-group" data-username="Kuldeep_Rana" data-post="1" data-topic="141104">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kuldeep_rana/48/40861_2.png" class="avatar"> Kuldeep_Rana:</div>
<blockquote>
<p>UnboundLocalError: local variable ‘data’ referenced before assignment</p>
</blockquote>
</aside>
<p>An UnboundLocalError is raised when a local variable is referenced before it has been assigned. In most cases this will occur when trying to modify a local variable before it is actually assigned within the local scope. Python doesn’t have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.</p>
<p>Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they’re declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can’t modify the binding in the enclosing environment. UnboundLocalError happend because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. However, to modify a <a href="http://net-informations.com/python/iq/global.htm" rel="noopener nofollow ugc">global variable</a> inside a function, you must use the global keyword.</p>