def test_fit(deepspeech: DeepSpeech, generator: DataGenerator, config_path: str, alphabet_path: str, model_dir: str): # Test save best weights (overwrite the best result) weights_path = os.path.join(model_dir, 'weights_copy.hdf5') deepspeech.save(weights_path) distributed_weights = deepspeech.distributed_model.get_weights() model_checkpoint = deepspeech.callbacks[2] model_checkpoint.best_result = 0 model_checkpoint.best_weights_path = weights_path history = deepspeech.fit(train_generator=generator, dev_generator=generator, epochs=2, 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.distributed_model.get_weights() assert is_same(distributed_weights, new_distributed_weights)
def test_save_load(deepspeech: DeepSpeech, config: Configuration, config_path: str, alphabet_path: str, model_dir: str): weights_path = os.path.join(model_dir, 'weights.hdf5') model_weights = deepspeech.model.get_weights() deepspeech.save(weights_path) new_deepspeech = DeepSpeech.construct(config_path, alphabet_path) new_deepspeech.model = deepspeech.get_model(**config.model, is_gpu=False, random_state=123) new_model_weights = new_deepspeech.model.get_weights() assert not is_same(model_weights, new_model_weights) new_deepspeech.load(weights_path) new_model_weights = new_deepspeech.model.get_weights() assert is_same(model_weights, new_model_weights)
def load(name: str): from deepspeech import DeepSpeech if os.path.isdir(name): model_dir = name else: model_dir = get_pretrained_model_dir(name) config_path = os.path.join(model_dir, 'configuration.yaml') alphabet_path = os.path.join(model_dir, 'alphabet.txt') weights_path = os.path.join(model_dir, 'weights.hdf5') assert os.path.isfile(config_path), 'The config file required in the directory' assert os.path.isfile(alphabet_path), 'The alphabet file required in the directory' deepspeech = DeepSpeech.construct(config_path, alphabet_path) if os.path.isfile(weights_path): deepspeech.load(weights_path) return deepspeech
def deepspeech(config_path: str, alphabet_path: str) -> DeepSpeech: return DeepSpeech.construct(config_path, alphabet_path)