Example #1
0
def get_dnn_cfg(dnn_fname):
    """ Construct a minimum required NetworkConfig given a model file """
    model_data = io.json_load(dnn_fname)
    cfg = NetworkConfig()
    cfg.hidden_layers_sizes = []
    i = 0
    while 'W{}'.format(i) in model_data:
        W_shape = string_2_array(model_data['W{}'.format(i)]).shape
        # Currently factored layer can only be the first hidden layer
        # TODO: change this!
        if i == 0:
            if 'side_W{}_0'.format(i) in model_data:
                factored_cfg = FactoredConfig()
                j = 0
                while 'side_b{}_{}'.format(i, j) in model_data:
                    assert 'side_W{}_{}'.format(i, j) in model_data
                    side_W_shape = string_2_array(model_data['side_W{}_{}'.format(i, j)]).shape
                    if j == 0:
                        factored_cfg.n_in_main = W_shape[0]
                        factored_cfg.n_in_side = side_W_shape[0]
                        # NOTE: this assumes that main and secondary features
                        # are disjoint, but this is not required by the model.
                        # TODO: find a way to relax this assumption.
                        cfg.n_ins = W_shape[0] + side_W_shape[0]
                    factored_cfg.side_layers.append(side_W_shape[1])
                    j += 1
                cfg.factored_cfg = factored_cfg
            else:
                cfg.n_ins = W_shape[0]
        if 'W{}'.format(i + 1) in model_data:
            cfg.hidden_layers_sizes.append(W_shape[1])
        else:
            cfg.n_outs = W_shape[1]
        i += 1
    return cfg
Example #2
0
 def _init_base_model(self):
     """ Initialize base model.
     """
     args = self.args
     io.log('Initializing base model, json: {}, weights: {}'.format(
         args.base_model_json, args.base_model_weights))
     self.base_model = model_from_json(io.json_load(args.base_model_json))
     self.base_model.load_weights(args.base_model_weights)
Example #3
0
def get_srbm_cfg(srbm_fname):
    """ Construct a minimum required RBMConfig given a model file """
    model_data = io.json_load(srbm_fname)
    cfg = RBMConfig()
    cfg.hidden_layers_sizes = []
    i = 0
    while 'W{}'.format(i) in model_data:
        W_shape = string_2_array(model_data['W{}'.format(i)]).shape
        if i == 0:
            cfg.n_ins = W_shape[0]
        cfg.hidden_layers_sizes.append(W_shape[1])
        i += 1
    return cfg
Example #4
0
 def _init_resume_json(self, model_dir):
     """ Load json for resuming training if possible.
     """
     resume_json = None
     resume_fname = os.path.join(model_dir, 'resume.json')
     if os.path.exists(resume_fname):
         resume_json = io.json_load(resume_fname)
         # Check that json contains enough information
         assert 'weights' in resume_json
         assert 'lrate' in resume_json
         assert 'epoch' in resume_json
         # Make path absolute
         resume_json['weights'] = os.path.join(model_dir,
                                               resume_json['weights'])
         io.log('Resuming training: {}'.format(resume_json))
     return resume_json
Example #5
0
def main():
    desc = 'Outputs Kaldi-compatible log-likelihood to stdout using a ' + \
           'Keras model. This mimics the design of Kaldi nnet-forward.'
    parser = common.init_argparse(desc)
    parser.add_argument('model_json', help='JSON description of the model')
    parser.add_argument('model_weights', help='File containing model weights')
    parser.add_argument('feats_scp', help='scp of input features')
    parser.add_argument('--context', type=int, default=8,
                        help='Number of context frames for splicing')
    parser.add_argument('--padding', default='replicate',
                        help='What to do with out-of-bound frames. Valid ' + \
                             'values: [replicate|zero]')
    parser.add_argument('--primary-task', type=int,
                        help='Set to enable multi-task model decoding')
    parser.add_argument('--nutts', type=int, default=10,
                        help='How many utterances to feed to the model at once')
    parser.add_argument('--delay', type=int, default=5,
                        help='Output delay in frames')
    parser.add_argument('--class-frame-counts', help='Kaldi vector with ' + \
                        'frame-counts of pdfs to compute log-priors')
    parser.add_argument('--prior-floor', type=float, default=1e-10,
                        help='Flooring constant for prior probability, ' + \
                             'i.e. pdfs with prior smaller than this ' + \
                             'value will be ignored during decoding.')
    parser.add_argument('--ivectors', help='Utterance i-vectors to append')
    args = parser.parse_args()

    io.log('Initializing dataset')
    ivectors = None if args.ivectors is None else \
            ivector_ark_read(args.ivectors, dtype=np.float32)
    buf_ds = init_dataset(
        args.feats_scp, args.context, args.padding,
        args.nutts, args.delay, ivectors
    )
    io.log('Initializing model')
    json_str = io.json_load(args.model_json)
    model = model_from_json(json_str)
    model.load_weights(args.model_weights)
    io.log('Initializing priors')
    log_priors = get_log_priors(args.class_frame_counts, args.prior_floor)
    if args.primary_task is not None:
        io.log('Multi-task decoding enabled, primary task {}'.format(args.primary_task))

    io.log('** Begin outputting **')
    while True:
        # Load data chunk
        chunk = buf_ds.read_next_chunk()
        if chunk is None:
            break
        Xs, _, eobs, utt_indices = chunk
        X = Xs[0]
        eob = eobs[0]
        utt_names = buf_ds.dataset().get_utt_names_by_utt_indices(utt_indices)
        y = model.predict(X, batch_size=len(utt_indices), verbose=0)
        if args.primary_task is not None:
            y = y[args.primary_task]
        y = np.log(y, y)
        if log_priors is not None:
            y -= log_priors
        for i in range(len(utt_indices)):
            print_matrix(utt_names[i], y[i][buf_ds.get_delay():eob[i]])