Ejemplo n.º 1
0
            def on_epoch_end(self_callback, epoch, logs=()):
                if epoch % callback_step == 0:
                    callback()

                if epoch % save_step == 0 and epoch > 0:
                    mkdir(net_directory)

                    self.predictive_net.save_weights(
                        str(net_directory / self.model_file_name(epoch)))
Ejemplo n.º 2
0
 def __call__(self):
     mkdir(self.results_directory)
     write_text(self.result_file, "")
     handler = logging.FileHandler(str(self.result_file))
     handler.setLevel(logging.INFO)
     logger.addHandler(handler)
     try:
         self.action()
     finally:
         logger.removeHandler(handler)
Ejemplo n.º 3
0
 def move_incorrect_cached_file_to_backup_location_and_save_error(
         self, error_text: str):
     parent_directory = Path(self.spectrogram_cache_file.parent)
     incorrect_cached_backup_directory = Path(
         parent_directory.parent / (parent_directory.name + "-incorrect"))
     mkdir(incorrect_cached_backup_directory)
     incorrect_backup_file = incorrect_cached_backup_directory / self.spectrogram_cache_file.name
     incorrect_backup_message_file = incorrect_cached_backup_directory / (
         name_without_extension(self.spectrogram_cache_file) + "-error.txt")
     write_text(incorrect_backup_message_file, error_text)
     self.spectrogram_cache_file.rename(incorrect_backup_file)
Ejemplo n.º 4
0
def record_plot_and_save(
    recorder: Recorder = Recorder(),
    recording_directory: Path = configuration.default_data_directories.
    recording_directory
) -> LabeledExample:
    from speechless.labeled_example_plotter import LabeledExamplePlotter

    mkdir(recording_directory)
    name = "recording-{}".format(timestamp())
    example = recorder.record_to_file(recording_directory /
                                      "{}.wav".format(name))
    LabeledExamplePlotter(example).save_spectrogram(recording_directory)

    return example
Ejemplo n.º 5
0
            def on_epoch_end(self_callback, epoch, logs=()):
                if epoch % callback_step == 0:
                    callback()

                    # validation
                    data_generator = LabeledSpectrogramBatchGenerator(
                        self.corpus, self.spectrogram_cache_directory,
                        self.batch_size)
                    test_batches = data_generator.test_batches()
                    result = self.test_and_predict_batches(test_batches)
                    print('WER:', result.average_letter_error_rate)
                    result_dict = {
                        'wer': result.average_word_error_rate,
                        'ler': result.average_letter_error_rate,
                        'loss': result.average_loss,
                        'epoch': epoch
                    }

                    def write_to_jsonl(result_dict, mode='w'):
                        with output_path.open(mode) as f:
                            f.write(json.dumps(result_dict))
                            f.write('\n')

                    output_path = tensor_board_log_directory / 'output_val.jsonl'
                    if epoch == 0:
                        write_to_jsonl(result_dict, 'w')
                    else:
                        write_to_jsonl(result_dict, 'a')

                if epoch % save_step == 0 and epoch > 0:
                    mkdir(net_directory)

                    self.predictive_net.save_weights(
                        str(net_directory / self.model_file_name(epoch)))

                    # delete weights so we save only
                    # keep_checkpoint number of weights
                    if epoch > self.keep_checkpoint:
                        to_be_deleted = net_directory / self.model_file_name(
                            epoch - self.keep_checkpoint)
                        to_be_deleted.unlink()
Ejemplo n.º 6
0
    def __init__(self,
                 corpus: Corpus,
                 spectrogram_cache_directory: Path,
                 batch_size: int = 64):
        mkdir(spectrogram_cache_directory)

        self.batch_size = batch_size
        self.spectrogram_cache_directory = spectrogram_cache_directory
        self.labeled_training_spectrograms = [
            CachedLabeledSpectrogram(
                example,
                spectrogram_cache_directory=spectrogram_cache_directory)
            for example in corpus.training_examples
        ]

        self.labeled_test_spectrograms = [
            CachedLabeledSpectrogram(
                example,
                spectrogram_cache_directory=spectrogram_cache_directory)
            for example in corpus.test_examples
        ]

        self.labeled_spectrograms = self.labeled_training_spectrograms + self.labeled_test_spectrograms
Ejemplo n.º 7
0
    def __init__(self,
                 base_directory: Path,
                 corpus_name: str,
                 base_source_url_or_directory:
                 str = "http://www.openslr.org/resources/12/",
                 tar_gz_extension: str = ".tar.gz",
                 mel_frequency_count: int = 128,
                 root_compressed_directory_name_to_skip: Optional[
                     str] = "LibriSpeech/",
                 subdirectory_depth: int = 3,
                 allowed_characters: List[chr] = english_frequent_characters,
                 tags_to_ignore: Iterable[str] = list(),
                 id_filter_regex=re.compile('[\s\S]*'),
                 training_test_split: Callable[[List[LabeledExample]], Tuple[
                     List[LabeledExample],
                     List[LabeledExample]]] = TrainingTestSplit.randomly(),
                 maximum_example_duration_in_s: Optional[int] = None,
                 minimum_duration_per_character: Optional[float] = None):
        self.minimum_duration_per_character_in_s = minimum_duration_per_character
        self.maximum_example_duration_in_s = maximum_example_duration_in_s
        self.training_test_split = training_test_split
        self.id_filter_regex = id_filter_regex
        self.tags_to_ignore = tags_to_ignore
        self.allowed_characters = allowed_characters
        self.subdirectory_depth = subdirectory_depth
        self.root_compressed_directory_name_to_skip = root_compressed_directory_name_to_skip
        self.base_directory = base_directory
        self.base_url_or_directory = base_source_url_or_directory
        self.tar_gz_extension = tar_gz_extension
        self.mel_frequency_count = mel_frequency_count
        self.corpus_name = corpus_name
        mkdir(base_directory)

        self.corpus_directory = self._download_and_unpack_if_not_yet_done(
            corpus_name=corpus_name)

        directories = [self.corpus_directory]
        for i in range(self.subdirectory_depth):
            directories = [
                subdirectory for directory in directories
                for subdirectory in directory.iterdir()
                if subdirectory.is_dir()
            ]

        self.files = [
            file for directory in directories for file in directory.iterdir()
            if file.is_file()
        ]

        self.unfiltered_audio_files = [
            file for file in self.files
            if (file.name.lower().endswith(".flac")
                or file.name.lower().endswith(".wav"))
        ]
        audio_files = [
            file for file in self.unfiltered_audio_files
            if self.id_filter_regex.match(name_without_extension(file))
        ]
        self.filtered_out_count = len(
            self.unfiltered_audio_files) - len(audio_files)

        positional_label_by_id = self._extract_positional_label_by_id(
            self.files)
        found_audio_ids = set(name_without_extension(f) for f in audio_files)
        found_label_ids = positional_label_by_id.keys()
        self.audio_ids_without_label = list(found_audio_ids - found_label_ids)
        self.label_ids_without_audio = list(found_label_ids - found_audio_ids)

        def example(audio_file: Path) -> LabeledExample:
            id = name_without_extension(audio_file)

            def correct_whitespace(text: str) -> str:
                return " ".join(text.split()).strip()

            def correct(label: str) -> str:
                return correct_whitespace(self._remove_tags_to_ignore(label))

            original_positional_label = positional_label_by_id[id]
            has_positions = isinstance(original_positional_label,
                                       PositionalLabel)
            positional_label = original_positional_label.with_corrected_labels(
                correct).convert_range_to_seconds(
                    LabeledExampleFromFile.file_sample_rate(
                        audio_file)) if has_positions else None
            return LabeledExampleFromFile(
                audio_file,
                mel_frequency_count=self.mel_frequency_count,
                label=positional_label.label
                if has_positions else correct(original_positional_label),
                label_with_tags=original_positional_label.label
                if has_positions else original_positional_label,
                positional_label=positional_label)

        self.examples_with_empty_and_too_long_or_short = [
            example(file) for file in audio_files
            if name_without_extension(file) in positional_label_by_id.keys()
        ]

        self.examples_with_too_long_or_short = [
            e for e in self.examples_with_empty_and_too_long_or_short
            if e.label
        ]

        self.examples_with_too_short = [
            e for e in self.examples_with_too_long_or_short
            if not self.is_too_long(e)
        ]

        examples = [
            e for e in self.examples_with_too_short if not self.is_too_short(e)
        ]

        training_examples, test_examples = self.training_test_split(
            sorted(examples, key=lambda x: x.id))

        super().__init__(training_examples=training_examples,
                         test_examples=test_examples)