def construct(cls, config_path: str, alphabet_path: str) -> 'DeepSpeech':
        """ Construct DeepSpeech object base on the configuration and the alphabet files. """
        config = Configuration(config_path)
        model_dir = os.path.dirname(config_path)
        gpus = get_available_gpus()

        model = cls.get_model(is_gpu=len(gpus) > 0, **config.model)
        loss = cls.get_loss()
        optimizer = cls.get_optimizer(**config.optimizer)
        callbacks = cls.get_callbacks(home_dir=model_dir, configurations=config.callbacks)

        alphabet = cls.get_alphabet(alphabet_path)
        features_extractor = cls.get_features_extractor(**config.features_extractor)
        decoder = cls.get_decoder(alphabet=alphabet, model=model, **config.decoder)
        return cls(model, loss, optimizer, callbacks, alphabet, decoder, features_extractor, gpus)
示例#2
0
    def train(self):
        """ Train model using train and dev data generators """
        config_dataset = self.configuration.dataset
        train_generator = DataGenerator(name='train',
                                        csv_path=config_dataset.train_csv_path,
                                        alphabet=self.alphabet,
                                        **config_dataset.parameters)
        dev_generator = DataGenerator(name='dev',
                                      csv_path=config_dataset.dev_csv_path,
                                      alphabet=self.alphabet,
                                      **config_dataset.parameters)

        gpus = get_available_gpus()
        if len(gpus) > 1:
            distributed_model = multi_gpu_model(self.model, len(gpus))
        else:
            distributed_model = self.model

        optimizer = self.__get_optimizer()
        objective = self.__get_objective()
        y = Input(name='y', shape=[None], dtype='int32')
        distributed_model.compile(optimizer=optimizer,
                                  loss=objective,
                                  target_tensors=[y])

        # The template model shares the same weights, but it is not distributed
        # along different devices.
        distributed_model.template_model = self.model

        callbacks = self.__get_callbacks()

        history = distributed_model.fit_generator(
            generator=train_generator,
            validation_data=dev_generator,
            callbacks=callbacks,
            shuffle=False,
            **self.configuration.fit_generator.parameters)
        self.__set_best_weights_to_model(history)