EOFError: Ran out of input
See original GitHub issuespeechbrain.core - Exception:
Traceback (most recent call last):
File ".\train.py", line 396, in <module>
asr_brain.fit(
File "f:\codes\python apps\asr\speechbrain-released\speechbrain\core.py", line 1008, in fit
for batch in t:
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\site-packages\tqdm\std.py", line 1133, in __iter__
for obj in iterable:
File "f:\codes\python apps\asr\speechbrain-released\speechbrain\dataio\dataloader.py", line 175, in __iter__
iterator = super().__iter__()
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\site-packages\torch\utils\data\dataloader.py", line 355, in __iter__
return self._get_iterator()
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\site-packages\torch\utils\data\dataloader.py", line 301, in _get_iterator
return _MultiProcessingDataLoaderIter(self)
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\site-packages\torch\utils\data\dataloader.py", line 914, in __init__
w.start()
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\multiprocessing\process.py", line 121, in start
self._popen = self._Popen(self)
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\multiprocessing\context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\multiprocessing\context.py", line 327, in _Popen
return Popen(process_obj)
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
reduction.dump(process_obj, to_child)
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'dataio_prepare.<locals>.audio_pipeline'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\flozi\anaconda3\envs\wav2vec\lib\multiprocessing\spawn.py", line 126, in _main
self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
train.py
import sys
import torch
import logging
import speechbrain as sb
import torchaudio
from speechbrain.utils.distributed import run_on_main
from speechbrain.utils.data_utils import undo_padding
from speechbrain.tokenizers.SentencePiece import SentencePiece
from hyperpyyaml import load_hyperpyyaml
logger = logging.getLogger(__name__)
# Define training procedure
class ASR(sb.Brain):
def compute_forward(self, batch, stage):
"Given an input batch it computes the phoneme probabilities."
batch = batch.to(self.device)
wavs, wav_lens = batch.sig
phns_bos, _ = batch.phn_encoded_bos
if stage == sb.Stage.TRAIN:
if hasattr(self.hparams, "augmentation"):
wavs = self.hparams.augmentation(wavs, wav_lens)
feats = self.modules.wav2vec2(wavs)
x = self.modules.enc(feats)
# output layer for ctc log-probabilities
logits = self.modules.ctc_lin(x)
p_ctc = self.hparams.log_softmax(logits)
e_in = self.modules.emb(phns_bos)
h, _ = self.modules.dec(e_in, x, wav_lens)
# output layer for seq2seq log-probabilities
logits = self.modules.seq_lin(h)
p_seq = self.hparams.log_softmax(logits)
if stage == sb.Stage.VALID:
hyps, scores = self.hparams.greedy_searcher(x, wav_lens)
return p_ctc, p_seq, wav_lens, hyps
elif stage == sb.Stage.TEST:
hyps, scores = self.hparams.beam_searcher(x, wav_lens)
return p_ctc, p_seq, wav_lens, hyps
return p_ctc, p_seq, wav_lens
def compute_objectives(self, predictions, batch, stage):
"Given the network predictions and targets computed the NLL loss."
if stage == sb.Stage.TRAIN:
p_ctc, p_seq, wav_lens = predictions
else:
p_ctc, p_seq, wav_lens, hyps = predictions
ids = batch.id
phns_eos, phn_lens_eos = batch.phn_encoded_eos
phns, phn_lens = batch.phn_encoded
loss_ctc = self.hparams.ctc_cost(p_ctc, phns, wav_lens, phn_lens)
loss_seq = self.hparams.seq_cost(p_seq, phns_eos, phn_lens_eos)
loss = self.hparams.ctc_weight * loss_ctc
loss += (1 - self.hparams.ctc_weight) * loss_seq
# Record losses for posterity
if stage != sb.Stage.TRAIN:
self.ctc_metrics.append(ids, p_ctc, phns, wav_lens, phn_lens)
self.seq_metrics.append(ids, p_seq, phns_eos, phn_lens_eos)
self.per_metrics.append(
ids, hyps, phns, None, phn_lens, self.label_encoder.decode_ndim,
)
return loss
def evaluate_batch(self, batch, stage):
"""Computations needed for validation/test batches"""
predictions = self.compute_forward(batch, stage=stage)
loss = self.compute_objectives(predictions, batch, stage=stage)
return loss.detach()
def on_stage_start(self, stage, epoch):
"Gets called when a stage (either training, validation, test) starts."
self.ctc_metrics = self.hparams.ctc_stats()
self.seq_metrics = self.hparams.seq_stats()
if stage != sb.Stage.TRAIN:
self.per_metrics = self.hparams.per_stats()
def on_stage_end(self, stage, stage_loss, epoch):
"""Gets called at the end of a epoch."""
if stage == sb.Stage.TRAIN:
self.train_loss = stage_loss
else:
per = self.per_metrics.summarize("error_rate")
if stage == sb.Stage.VALID:
old_lr_adam, new_lr_adam = self.hparams.lr_annealing_adam(per)
old_lr_wav2vec, new_lr_wav2vec = self.hparams.lr_annealing_wav2vec(
per
)
sb.nnet.schedulers.update_learning_rate(
self.adam_optimizer, new_lr_adam
)
sb.nnet.schedulers.update_learning_rate(
self.wav2vec_optimizer, new_lr_wav2vec
)
self.hparams.train_logger.log_stats(
stats_meta={
"epoch": epoch,
"lr_adam": old_lr_adam,
"lr_wav2vec": old_lr_wav2vec,
},
train_stats={"loss": self.train_loss},
valid_stats={
"loss": stage_loss,
"ctc_loss": self.ctc_metrics.summarize("average"),
"seq_loss": self.seq_metrics.summarize("average"),
"PER": per,
},
)
self.checkpointer.save_and_keep_only(
meta={"PER": per}, min_keys=["PER"]
)
if stage == sb.Stage.TEST:
self.hparams.train_logger.log_stats(
stats_meta={"Epoch loaded": self.hparams.epoch_counter.current},
test_stats={"loss": stage_loss, "PER": per},
)
with open(self.hparams.wer_file, "w") as w:
w.write("CTC loss stats:\n")
self.ctc_metrics.write_stats(w)
w.write("\nseq2seq loss stats:\n")
self.seq_metrics.write_stats(w)
w.write("\nPER stats:\n")
self.per_metrics.write_stats(w)
print(
"CTC, seq2seq, and PER stats written to file",
self.hparams.wer_file,
)
def fit_batch(self, batch):
"""Fit one batch, override to do multiple updates.
The default implementation depends on a few methods being defined
with a particular behavior:
* ``compute_forward()``
* ``compute_objectives()``
Also depends on having optimizers passed at initialization.
Arguments
---------
batch : list of torch.Tensors
Batch of data to use for training. Default implementation assumes
this batch has two elements: inputs and targets.
Returns
-------
detached loss
"""
# Managing automatic mixed precision
if self.auto_mix_prec:
self.wav2vec_optimizer.zero_grad()
self.adam_optimizer.zero_grad()
with torch.cuda.amp.autocast():
outputs = self.compute_forward(batch, sb.Stage.TRAIN)
loss = self.compute_objectives(outputs, batch, sb.Stage.TRAIN)
self.scaler.scale(loss).backward()
self.scaler.unscale_(self.wav2vec_optimizer)
self.scaler.unscale_(self.adam_optimizer)
if self.check_gradients(loss):
self.scaler.step(self.wav2vec_optimizer)
self.scaler.step(self.adam_optimizer)
self.scaler.update()
else:
outputs = self.compute_forward(batch, sb.Stage.TRAIN)
loss = self.compute_objectives(outputs, batch, sb.Stage.TRAIN)
loss.backward()
if self.check_gradients(loss):
self.wav2vec_optimizer.step()
self.adam_optimizer.step()
self.wav2vec_optimizer.zero_grad()
self.adam_optimizer.zero_grad()
return loss.detach().cpu()
def init_optimizers(self):
"Initializes the wav2vec2 optimizer and model optimizer"
self.wav2vec_optimizer = self.hparams.wav2vec_opt_class(
self.modules.wav2vec2.parameters()
)
self.adam_optimizer = self.hparams.adam_opt_class(
self.hparams.model.parameters()
)
if self.checkpointer is not None:
self.checkpointer.add_recoverable(
"wav2vec_opt", self.wav2vec_optimizer
)
self.checkpointer.add_recoverable("adam_opt", self.adam_optimizer)
# Define custom data procedure
def dataio_prepare(hparams):
"""This function prepares the datasets to be used in the brain class.
It also defines the data processing pipeline through user-defined functions."""
# 1. Define datasets
data_folder = hparams["data_folder"]
train_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["train_csv"], replacements={"data_root": data_folder},
)
if hparams["sorting"] == "ascending":
# we sort training data to speed up training and get better results.
train_data = train_data.filtered_sorted(
sort_key="duration",
key_max_value={"duration": hparams["avoid_if_longer_than"]},
)
# when sorting do not shuffle in dataloader ! otherwise is pointless
hparams["train_dataloader_opts"]["shuffle"] = False
elif hparams["sorting"] == "descending":
train_data = train_data.filtered_sorted(
sort_key="duration",
reverse=True,
key_max_value={"duration": hparams["avoid_if_longer_than"]},
)
# when sorting do not shuffle in dataloader ! otherwise is pointless
hparams["train_dataloader_opts"]["shuffle"] = False
elif hparams["sorting"] == "random":
pass
else:
raise NotImplementedError(
"sorting must be random, ascending or descending"
)
valid_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["valid_csv"], replacements={"data_root": data_folder},
)
# We also sort the validation data so it is faster to validate
valid_data = valid_data.filtered_sorted(sort_key="duration")
test_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["test_csv"], replacements={"data_root": data_folder},
)
# We also sort the validation data so it is faster to validate
test_data = test_data.filtered_sorted(sort_key="duration")
datasets = [train_data, valid_data, test_data]
# defining tokenizer and loading it
tokenizer = SentencePiece(
model_dir=hparams["save_folder"],
vocab_size=hparams["output_neurons"],
annotation_train=hparams["train_csv"],
annotation_read="wrd",
model_type=hparams["token_type"],
character_coverage=hparams["character_coverage"],
)
# 2. Define audio pipeline:
@sb.utils.data_pipeline.takes("wav")
@sb.utils.data_pipeline.provides("sig")
def audio_pipeline(wav):
info = torchaudio.info(wav)
sig = sb.dataio.dataio.read_audio(wav)
resampled = torchaudio.transforms.Resample(
info.sample_rate, hparams["sample_rate"],
)(sig)
return resampled
sb.dataio.dataset.add_dynamic_item(datasets, audio_pipeline)
# 3. Define text pipeline:
@sb.utils.data_pipeline.takes("wrd")
@sb.utils.data_pipeline.provides(
"wrd", "tokens_list", "tokens_bos", "tokens_eos", "tokens"
)
def text_pipeline(wrd):
yield wrd
tokens_list = tokenizer.sp.encode_as_ids(wrd)
yield tokens_list
tokens_bos = torch.LongTensor([hparams["blank_index"]] + (tokens_list))
yield tokens_bos
tokens_eos = torch.LongTensor(tokens_list + [hparams["blank_index"]])
yield tokens_eos
tokens = torch.LongTensor(tokens_list)
yield tokens
sb.dataio.dataset.add_dynamic_item(datasets, text_pipeline)
# 4. Set output:
sb.dataio.dataset.set_output_keys(
datasets, ["id", "sig", "tokens_bos", "tokens_eos", "tokens"],
)
return train_data, valid_data, test_data, tokenizer
if __name__ == "__main__":
# CLI:
print("CLI")
hparams_file, run_opts, overrides = sb.parse_arguments(sys.argv[1:])
with open(hparams_file) as fin:
hparams = load_hyperpyyaml(fin, overrides)
# If distributed_launch=True then
# create ddp_group with the right communication protocol
print("distributed")
sb.utils.distributed.ddp_init_group(run_opts)
# Dataset preparation (parsing CommonVoice)
from common_voice_prepare import prepare_common_voice # noqa
# Create experiment directory
print("create folders")
sb.create_experiment_directory(
experiment_directory=hparams["output_folder"],
hyperparams_to_save=hparams_file,
overrides=overrides,
)
# Due to DDP, we do the preparation ONLY on the main python process
print("prepare dataset")
run_on_main(
prepare_common_voice,
kwargs={
"data_folder": hparams["data_folder"],
"save_folder": hparams["save_folder"],
"train_tsv_file": hparams["train_tsv_file"],
"dev_tsv_file": hparams["dev_tsv_file"],
"test_tsv_file": hparams["test_tsv_file"],
"accented_letters": hparams["accented_letters"],
"language": hparams["language"],
"skip_prep": hparams["skip_prep"],
},
)
# here we create the datasets objects as well as tokenization and encoding
train_data, valid_data, test_data, tokenizer = dataio_prepare(hparams)
# Trainer initialization
asr_brain = ASR(
modules=hparams["modules"],
hparams=hparams,
run_opts=run_opts,
checkpointer=hparams["checkpointer"],
)
# adding objects to trainer:
asr_brain.tokenizer = tokenizer
# Training
asr_brain.fit(
asr_brain.hparams.epoch_counter,
train_data,
valid_data,
train_loader_kwargs=hparams["train_dataloader_opts"],
valid_loader_kwargs=hparams["valid_dataloader_opts"],
)
# Test
asr_brain.hparams.wer_file = hparams["output_folder"] + "/wer_test.txt"
asr_brain.evaluate(
test_data,
min_key="WER",
test_loader_kwargs=hparams["test_dataloader_opts"],
)
train_de.yaml
# Seed needs to be set at top of yaml, before objects with parameters are made
seed: 1234
__set_seed: !!python/object/apply:torch.manual_seed [!ref <seed>]
output_folder: !ref results/cv_transducer/<seed>
wer_file: !ref <output_folder>/wer.txt
save_folder: !ref <output_folder>/save
train_log: !ref <output_folder>/train_log.txt
# Data files
data_folder: D:/huggingfacecache/downloads/extracted/43ad5573cce32e3f168ac28ab12c46d406cf4efe0d3112271eb1c7cd17956969/cv-corpus-6.1-2020-12-11/de # e.g, /localscratch/cv-corpus-5.1-2020-06-22/fr
train_tsv_file: !ref <data_folder>/train.tsv # Standard CommonVoice .tsv files
dev_tsv_file: !ref <data_folder>/dev.tsv # Standard CommonVoice .tsv files
test_tsv_file: !ref <data_folder>/test.tsv # Standard CommonVoice .tsv files
accented_letters: True
language: de # use 'it' for Italian, 'rw' for Kinyarwanda, 'en' for english
train_csv: !ref <save_folder>/train.csv
valid_csv: !ref <save_folder>/dev.csv
test_csv: !ref <save_folder>/test.csv
skip_prep: False # Skip data preparation
# We remove utterance slonger than 10s in the train/dev/test sets as
# longer sentences certainly correspond to "open microphones".
avoid_if_longer_than: 20.0
wav2vec2_hub: maxidl/wav2vec2-large-xlsr-german
# BPE parameters
token_type: char # ["unigram", "bpe", "char"]
character_coverage: 1
# Training parameters
number_of_epochs: 20
batch_size: 8
lr: 0.0003
lr_wav2vec: 0.0001
ctc_weight: 0.2
sorting: ascending
auto_mix_prec: False
sample_rate: 16000
# Model parameters
activation: !name:torch.nn.LeakyReLU
dnn_layers: 2
dnn_neurons: 1024
emb_size: 128
dec_neurons: 256
freeze_wav2vec: False
# Outputs
output_neurons: 42 # 39phs+blank+eos+bos
bos_index: 0
eos_index: 1
blank_index: 2
# Decoding parameters
min_decode_ratio: 0.0
max_decode_ratio: 1.0
beam_size: 16
eos_threshold: 1.5
# Dataloader options
train_dataloader_opts:
batch_size: !ref <batch_size>
num_workers: !ref <batch_size>
valid_dataloader_opts:
batch_size: !ref <batch_size>
num_workers: !ref <batch_size>
test_dataloader_opts:
batch_size: !ref <batch_size>
num_workers: !ref <batch_size>
augmentation: !new:speechbrain.lobes.augment.TimeDomainSpecAugment
sample_rate: !ref <sample_rate>
speeds: [95, 100, 105]
epoch_counter: !new:speechbrain.utils.epoch_loop.EpochCounter
limit: !ref <number_of_epochs>
enc: !new:speechbrain.lobes.models.VanillaNN.VanillaNN
input_shape: [null, null, 1024]
activation: !ref <activation>
dnn_blocks: !ref <dnn_layers>
dnn_neurons: !ref <dnn_neurons>
wav2vec2: !new:speechbrain.lobes.models.huggingface_wav2vec.HuggingFaceWav2Vec2
source: !ref <wav2vec2_hub>
output_norm: True
freeze: !ref <freeze_wav2vec>
save_path: !ref <save_folder>/wav2vec2_checkpoint
#####
# Uncomment this block if you prefer to use a Fairseq pretrained model instead
# of a HuggingFace one. Here, we provide an URL that is obtained from the
# Fairseq github for an equivalent model compared to HuggingFace.
#
#wav2vec2_url: https://dl.fbaipublicfiles.com/fairseq/wav2vec/wav2vec_vox_new.pt
#wav2vec2: !new:speechbrain.lobes.models.fairseq_wav2vec.FairseqWav2Vec2
# pretrained_path: !ref <wav2vec2_url>
# output_norm: True
# freeze: False
# save_path: !ref <save_folder>/wav2vec2_checkpoint/model.pt
#####
emb: !new:speechbrain.nnet.embedding.Embedding
num_embeddings: !ref <output_neurons>
embedding_dim: !ref <emb_size>
dec: !new:speechbrain.nnet.RNN.AttentionalRNNDecoder
enc_dim: !ref <dnn_neurons>
input_size: !ref <emb_size>
rnn_type: gru
attn_type: location
hidden_size: !ref <dec_neurons>
attn_dim: 256
num_layers: 1
scaling: 1.0
channels: 10
kernel_size: 100
re_init: True
dropout: 0.5
ctc_lin: !new:speechbrain.nnet.linear.Linear
input_size: !ref <dnn_neurons>
n_neurons: !ref <output_neurons> # 39 phonemes + 1 blank
seq_lin: !new:speechbrain.nnet.linear.Linear
input_size: !ref <dec_neurons>
n_neurons: !ref <output_neurons> # 39 phonemes + 1 eos
log_softmax: !new:speechbrain.nnet.activations.Softmax
apply_log: True
ctc_cost: !name:speechbrain.nnet.losses.ctc_loss
blank_index: !ref <blank_index>
seq_cost: !name:speechbrain.nnet.losses.nll_loss
label_smoothing: 0.1
greedy_searcher: !new:speechbrain.decoders.seq2seq.S2SRNNGreedySearcher
embedding: !ref <emb>
decoder: !ref <dec>
linear: !ref <seq_lin>
bos_index: !ref <bos_index>
eos_index: !ref <eos_index>
min_decode_ratio: !ref <min_decode_ratio>
max_decode_ratio: !ref <max_decode_ratio>
beam_searcher: !new:speechbrain.decoders.seq2seq.S2SRNNBeamSearcher
embedding: !ref <emb>
decoder: !ref <dec>
linear: !ref <seq_lin>
ctc_linear: !ref <ctc_lin>
bos_index: !ref <bos_index>
eos_index: !ref <eos_index>
blank_index: !ref <blank_index>
min_decode_ratio: !ref <min_decode_ratio>
max_decode_ratio: !ref <max_decode_ratio>
beam_size: !ref <beam_size>
model: !new:torch.nn.ModuleList
- [!ref <enc>, !ref <emb>, !ref <dec>, !ref <ctc_lin>, !ref <seq_lin>]
adam_opt_class: !name:torch.optim.Adam
lr: !ref <lr>
wav2vec_opt_class: !name:torch.optim.Adam
lr: !ref <lr_wav2vec>
lr_annealing_adam: !new:speechbrain.nnet.schedulers.NewBobScheduler
initial_value: !ref <lr>
improvement_threshold: 0.0025
annealing_factor: 0.8
patient: 0
lr_annealing_wav2vec: !new:speechbrain.nnet.schedulers.NewBobScheduler
initial_value: !ref <lr_wav2vec>
improvement_threshold: 0.0025
annealing_factor: 0.9
modules:
wav2vec2: !ref <wav2vec2>
enc: !ref <enc>
emb: !ref <emb>
dec: !ref <dec>
ctc_lin: !ref <ctc_lin>
seq_lin: !ref <seq_lin>
checkpointer: !new:speechbrain.utils.checkpoints.Checkpointer
checkpoints_dir: !ref <save_folder>
recoverables:
model: !ref <model>
wav2vec2: !ref <wav2vec2>
lr_annealing_adam: !ref <lr_annealing_adam>
lr_annealing_wav2vec: !ref <lr_annealing_wav2vec>
counter: !ref <epoch_counter>
train_logger: !new:speechbrain.utils.train_logger.FileTrainLogger
save_file: !ref <train_log>
ctc_stats: !name:speechbrain.utils.metric_stats.MetricStats
metric: !name:speechbrain.nnet.losses.ctc_loss
blank_index: !ref <blank_index>
reduction: batch
seq_stats: !name:speechbrain.utils.metric_stats.MetricStats
metric: !name:speechbrain.nnet.losses.nll_loss
label_smoothing: 0.1
reduction: batch
per_stats: !name:speechbrain.utils.metric_stats.ErrorRateStats
error_rate_computer: !name:speechbrain.utils.metric_stats.ErrorRateStats
cer_computer: !name:speechbrain.utils.metric_stats.ErrorRateStats
split_tokens: True
Issue Analytics
- State:
- Created 2 years ago
- Comments:18
Top Results From Across the Web
Why do I get "Pickle - EOFError: Ran out of input" reading an ...
This error comes when your pickle file is empty (0 Bytes). You need to check the size of your pickle file first. This...
Read more >Python :Why do I get "Pickle - EOFError: Ran out of ... - YouTube
Python :Why do I get "Pickle - EOFError : Ran out of input " reading an empty file?(5solution). 115 views 2 weeks ago....
Read more >EOFError: Ran out of input #93 - GitHub
I get the following error. Please help me. Traceback (most recent call last): File "I:\TEST\CUT\train.py", line 32, in <module> ...
Read more >Ran out of input" from Django file cache when file is empty
When using Django file cache, if a cache file ends up empty for some reason (yes, this actually happened in the wild), an...
Read more >[Python] [Beginner] Keep getting "EOFError: Ran out of input ...
When I run the code I get EOFError: Ran out of input which points to the loading of the pickle. I am not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Because modules are used in the Brain class (to put on GPU or the right device) while model is just an abstraction that allows me to init the optimiser with all the neural parameters of the blocks within model.
Thanks a lot, the training running now Why is wav2vec only in modules, not in model ?