def test_fit(deepspeech: DeepSpeech, generator: DataGenerator, config_path: str, alphabet_path: str, test_dir: str): # Test save best weights (overwrite the best result) weights_path = os.path.join(test_dir, 'weights_copy.hdf5') deepspeech.save(weights_path) distributed_weights = deepspeech.compiled_model.get_weights() model_checkpoint = deepspeech.callbacks[1] model_checkpoint.best_result = 0 model_checkpoint.best_weights_path = weights_path history = deepspeech.fit(train_generator=generator, dev_generator=generator, epochs=1, shuffle=False) assert type(history) == History # Test the returned model has `test_weights` deepspeech_weights = deepspeech.model.get_weights() new_deepspeech = DeepSpeech.construct(config_path, alphabet_path) new_deepspeech.load(model_checkpoint.best_weights_path) new_deepspeech_weights = new_deepspeech.model.get_weights() assert is_same(deepspeech_weights, new_deepspeech_weights) # Test that distributed model appropriate update weights new_distributed_weights = deepspeech.compiled_model.get_weights() assert is_same(distributed_weights, new_distributed_weights) shutil.rmtree('tests/checkpoints') os.remove('tests/weights_copy.hdf5')
def metrics(deepspeech: DeepSpeech, layer_outputs: List[np.ndarray], batch: Tuple[np.ndarray, np.ndarray]) -> Iterable[Metric]: X, y = batch y_hat = layer_outputs[-1] predict_sentences = deepspeech.decode(y_hat) true_sentences = deepspeech.get_transcripts(y) return get_metrics(sources=predict_sentences, destinations=true_sentences)
def test_get_labels_and_get_transcripts(deepspeech: DeepSpeech): bad_transcripts = ['to jest je$st!', 'test'] correct_transcripts = ['to jest jest', 'test'] labels = deepspeech.get_labels(bad_transcripts) assert labels.dtype == np.int64 assert labels.shape == (2, 12) assert labels[1, 4] == deepspeech.alphabet.blank_token transformed_transcripts = deepspeech.get_transcripts(labels) assert transformed_transcripts == correct_transcripts
def evaluate_batch(deepspeech: DeepSpeech, X: np.ndarray, y: np.ndarray, store: h5py.File, references: pd.DataFrame, save_activations: bool, get_activations: Callable) -> List[Metric]: if save_activations: *activations, y_hat = get_activations([X, 0]) # Learning phase is `test=0` else: activations = [] y_hat = deepspeech.predict(X) predict_sentences = deepspeech.decode(y_hat) true_sentences = deepspeech.get_transcripts(y) metrics = list(get_metrics(sources=predict_sentences, destinations=true_sentences)) save_in(store, [X, *activations, y_hat], metrics, references) return metrics
def main(args): deepspeech = DeepSpeech.construct(config_path=CONFIG_PATH, alphabet_path=ALPHABET_PATH) if args.pretrained_weights: deepspeech.load(args.pretrained_weights) train_generator, dev_generator = create_generators(deepspeech, args) deepspeech.fit(train_generator, dev_generator, epochs=args.epochs, shuffle=False) deepspeech.save(WEIGHTS_PATH)
def setup_deepspeech(config_path: str, alphabet_path: str, pretrained_weights: str = '') -> DeepSpeech: deepspeech = DeepSpeech.construct(config_path, alphabet_path) if pretrained_weights: deepspeech.load(pretrained_weights) return deepspeech
def test_main(deepspeech: DeepSpeech, config: Configuration): freeze(deepspeech.model) gpus = get_available_gpus() extended_model = create_extended_model(deepspeech.model, config, is_gpu=len(gpus) > 0) assert len(deepspeech.model.layers) + len(config.data['extension']['layers']) == len(extended_model.layers) *_, first_lstm, second_lstm, softmax = extended_model.layers assert not first_lstm.trainable assert second_lstm.trainable and softmax.trainable optimizer = DeepSpeech.get_optimizer(**config.optimizer) loss = DeepSpeech.get_loss() gpus = get_available_gpus() deepspeech.compiled_model = DeepSpeech.compile_model(extended_model, optimizer, loss, gpus) assert len(deepspeech.compiled_model.layers) == len(extended_model.layers) *_, first_lstm, second_lstm, softmax = deepspeech.compiled_model.layers assert not first_lstm.trainable assert second_lstm.trainable and softmax.trainable
def main(args): deepspeech = DeepSpeech.construct(config_path=CONFIG_PATH, alphabet_path=ALPHABET_PATH) if args.pretrained_weights: deepspeech.load(args.pretrained_weights) freeze(deepspeech.model) gpus = get_available_gpus() config = Configuration(CONFIG_PATH) extended_model = create_extended_model(deepspeech.model, config, is_gpu=len(gpus) > 0) optimizer = DeepSpeech.get_optimizer(**config.optimizer) loss = DeepSpeech.get_loss() gpus = get_available_gpus() deepspeech.model = extended_model deepspeech.compiled_model = DeepSpeech.compile_model(extended_model, optimizer, loss, gpus) train_generator, dev_generator = create_generators(deepspeech, args) deepspeech.fit(train_generator, dev_generator, epochs=args.epochs, shuffle=False) deepspeech.save(WEIGHTS_PATH)
def load_extended_model(config_path, alphabet_path, pretrained_weights): deepspeech = DeepSpeech.construct(config_path=config_path, alphabet_path=alphabet_path) freeze(deepspeech.model) gpus = get_available_gpus() config = Configuration(config_path) extended_model = create_extended_model(deepspeech.model, config, is_gpu=len(gpus) > 0) optimizer = DeepSpeech.get_optimizer(**config.optimizer) loss = DeepSpeech.get_loss() gpus = get_available_gpus() deepspeech.model = extended_model deepspeech.compiled_model = DeepSpeech.compile_model( extended_model, optimizer, loss, gpus) deepspeech.load(pretrained_weights) return deepspeech
def __run_program(configuration_line): """ Run DeepSpeech - save log file. """ configuration_file_path, *parameters = configuration_line.split('|') configuration = Configuration(configuration_file_path) __update_parameters(configuration, parameters) __create_experiment_dir(configuration) deepspeech_output = os.path.join(configuration.exp_dir, 'program.out') with open(deepspeech_output, 'w') as f: with redirect_stdout(f): ds = DeepSpeech(configuration) ds.train() ds.save()
def main(args): deepspeech = DeepSpeech.construct(config_path=config_path, alphabet_path=alphabet_path) if args.pretrained_weights: deepspeech.load(args.pretrained_weights) train_generator = deepspeech.create_generator( args.train, batch_size=args.batch_size, source=args.source, shuffle_after_epoch=args.shuffle_after_epoch) dev_generator = deepspeech.create_generator(args.dev, batch_size=args.batch_size, source=args.source) deepspeech.fit(train_generator, dev_generator, epochs=args.epochs, shuffle=False) deepspeech.save(weights_path)
import argparse import os from source.deepspeech import DeepSpeech from source.configuration import Configuration abspath = os.path.abspath(__file__) ROOT_DIR = os.path.dirname(abspath) os.chdir(ROOT_DIR) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--configuration', help='Experiment configuration') args = parser.parse_args() # Read configuration file config = Configuration(file_path=args.configuration) # Set up DeepSpeech object ds = DeepSpeech(config) # Model optimization ds.train() # Save whole deepspeech model ds.save()
def test_get_features(deepspeech: DeepSpeech, audio_file_paths: List[str]): features = deepspeech.get_features(audio_file_paths) assert features.shape == (4, 1477, 80)
def alphabet(alphabet_path: str) -> Alphabet: return DeepSpeech.get_alphabet(alphabet_path)
def test_get_callbacks(test_dir: str, config: Configuration): model = DeepSpeech.get_model(**config.model, is_gpu=False) callbacks = DeepSpeech.get_callbacks(home_dir=test_dir, configurations=config.callbacks, model=model) assert len(callbacks) == 2
def test_compile_model(config: Configuration): model = DeepSpeech.get_model(**config.model, is_gpu=False) optimizer = DeepSpeech.get_optimizer(**config.optimizer) loss = DeepSpeech.get_loss() DeepSpeech.compile_model(model, optimizer, loss) assert model._is_compiled
def test_get_features_extractor(config: Configuration): features_extractor = DeepSpeech.get_features_extractor( **config.features_extractor) assert type(features_extractor) == FeaturesExtractor
def test_get_decoder(config: Configuration, alphabet: Alphabet): model = DeepSpeech.get_model(**config.model, is_gpu=False) decoder = DeepSpeech.get_decoder(alphabet=alphabet, model=model, **config.decoder) assert callable(decoder)
def test_get_model(config: Configuration): model = DeepSpeech.get_model(**config.model, is_gpu=False) assert type(model) == Model new_model = DeepSpeech.get_model(**config.model, is_gpu=False) assert is_same(model.get_weights(), new_model.get_weights()) # Test random seed
def deepspeech(config_path: str, alphabet_path: str) -> DeepSpeech: return DeepSpeech.construct(config_path, alphabet_path)
def generator(features_store_path: str, deepspeech: DeepSpeech) -> DataGenerator: return deepspeech.create_generator(features_store_path, source='from_prepared_features', batch_size=3)
def test_get_callbacks(test_dir: str, config: Configuration): callbacks = DeepSpeech.get_callbacks(home_dir=test_dir, configurations=config.callbacks) assert len(callbacks) == 2
def config(config_path) -> Configuration: return DeepSpeech.get_configuration(config_path)