Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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()
Ejemplo n.º 13
0
def test_get_features(deepspeech: DeepSpeech, audio_file_paths: List[str]):
    features = deepspeech.get_features(audio_file_paths)
    assert features.shape == (4, 1477, 80)
Ejemplo n.º 14
0
def alphabet(alphabet_path: str) -> Alphabet:
    return DeepSpeech.get_alphabet(alphabet_path)
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
def test_get_features_extractor(config: Configuration):
    features_extractor = DeepSpeech.get_features_extractor(
        **config.features_extractor)
    assert type(features_extractor) == FeaturesExtractor
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
def deepspeech(config_path: str, alphabet_path: str) -> DeepSpeech:
    return DeepSpeech.construct(config_path, alphabet_path)
Ejemplo n.º 21
0
def generator(features_store_path: str,
              deepspeech: DeepSpeech) -> DataGenerator:
    return deepspeech.create_generator(features_store_path,
                                       source='from_prepared_features',
                                       batch_size=3)
Ejemplo n.º 22
0
def test_get_callbacks(test_dir: str, config: Configuration):
    callbacks = DeepSpeech.get_callbacks(home_dir=test_dir,
                                         configurations=config.callbacks)
    assert len(callbacks) == 2
Ejemplo n.º 23
0
def config(config_path) -> Configuration:
    return DeepSpeech.get_configuration(config_path)