예제 #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')
예제 #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)
예제 #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
예제 #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
예제 #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)
예제 #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
예제 #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)
예제 #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
예제 #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()
예제 #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)
예제 #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()
예제 #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)
예제 #14
0
def alphabet(alphabet_path: str) -> Alphabet:
    return DeepSpeech.get_alphabet(alphabet_path)
예제 #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
예제 #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
예제 #17
0
def test_get_features_extractor(config: Configuration):
    features_extractor = DeepSpeech.get_features_extractor(
        **config.features_extractor)
    assert type(features_extractor) == FeaturesExtractor
예제 #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)
예제 #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
예제 #20
0
def deepspeech(config_path: str, alphabet_path: str) -> DeepSpeech:
    return DeepSpeech.construct(config_path, alphabet_path)
예제 #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)
예제 #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
예제 #23
0
def config(config_path) -> Configuration:
    return DeepSpeech.get_configuration(config_path)