def predict(desired_sample_rate, fragment_length, _log, seed, _seed, _config, predict_seconds, data_dir, batch_size, fragment_stride, nb_output_bins, learn_all_outputs, run_dir, predict_use_softmax_as_input, use_ulaw, predict_initial_input, **kwargs): checkpoint_dir = os.path.join(run_dir, 'checkpoints') last_checkpoint = sorted(os.listdir(checkpoint_dir))[-1] epoch = int(re.match(r'checkpoint\.(\d+?)-.*', last_checkpoint).group(1)) _log.info('Using checkpoint from epoch: %s' % epoch) sample_dir = os.path.join(run_dir, 'samples') if not os.path.exists(sample_dir): os.mkdir(sample_dir) sample_name = make_sample_name(epoch) sample_filename = os.path.join(sample_dir, sample_name) _log.info('Saving to "%s"' % sample_filename) sample_stream = make_sample_stream(desired_sample_rate, sample_filename) model = build_model() model.load_weights(os.path.join(checkpoint_dir, last_checkpoint)) if predict_initial_input is not '': _log.info('Taking first %d (%.2fs) from \'%s\' as initial input.' % (fragment_length, fragment_length / desired_sample_rate, predict_initial_input)) wav = dataset.process_wav(desired_sample_rate, predict_initial_input, use_ulaw) outputs = list(dataset.one_hot(wav[0:fragment_length])) else: _log.info('Taking sample from test dataset as initial input.' % (fragment_length, predict_initial_input)) data_generators, _ = dataset.generators(data_dir, desired_sample_rate, fragment_length, batch_size, fragment_stride, nb_output_bins, learn_all_outputs, use_ulaw) outputs = list(data_generators['test'].next()[0][-1]) # write_samples(sample_stream, outputs) for i in tqdm(xrange(int(desired_sample_rate * predict_seconds))): prediction_seed = np.expand_dims( np.array(outputs[i:i + fragment_length]), 0) output = model.predict(prediction_seed) output_dist = output[0][-1] output_val = draw_sample(output_dist) if predict_use_softmax_as_input: outputs.append(output_dist) else: outputs.append(output_val) write_samples(sample_stream, [output_val]) sample_stream.close() _log.info("Done!")
def predict(desired_sample_rate, fragment_length, _log, seed, _seed, _config, predict_seconds, data_dir, batch_size, fragment_stride, nb_output_bins, learn_all_outputs, run_dir, predict_use_softmax_as_input, use_ulaw, predict_initial_input, **kwargs): fragment_length = compute_receptive_field()[0] _config['fragment_length'] = fragment_length checkpoint_dir = os.path.join(run_dir, 'checkpoints') last_checkpoint = sorted(os.listdir(checkpoint_dir))[-1] epoch = int(re.match(r'checkpoint\.(\d+?)-.*', last_checkpoint).group(1)) _log.info('Using checkpoint from epoch: %s' % epoch) sample_dir = os.path.join(run_dir, 'samples') if not os.path.exists(sample_dir): os.makedirs(sample_dir, exist_ok=True) sample_name = make_sample_name(epoch) sample_filename = os.path.join(sample_dir, sample_name) _log.info('Saving to "%s"' % sample_filename) sample_stream = make_sample_stream(desired_sample_rate, sample_filename) model = build_model() model.load_weights(os.path.join(checkpoint_dir, last_checkpoint)) model.summary() if predict_initial_input is None: outputs = list(dataset.one_hot(np.zeros(fragment_length) + nb_output_bins / 2)) elif predict_initial_input != '': _log.info('Taking first %d (%.2fs) from \'%s\' as initial input.' % ( fragment_length, fragment_length / desired_sample_rate, predict_initial_input)) wav = dataset.process_wav(desired_sample_rate, predict_initial_input, use_ulaw) outputs = list(dataset.one_hot(wav[0:fragment_length])) else: _log.info('Taking sample from test dataset as initial input.') data_generators, _ = get_generators() outputs = list(data_generators['test'].next()[0][-1]) # write_samples(sample_stream, outputs) warned_repetition = False for i in tqdm(range(int(desired_sample_rate * predict_seconds))): if not warned_repetition: if np.argmax(outputs[-1]) == np.argmax(outputs[-2]) and np.argmax(outputs[-2]) == np.argmax(outputs[-3]): warned_repetition = True _log.warning('Last three predicted outputs where %d' % np.argmax(outputs[-1])) else: warned_repetition = False prediction_seed = np.expand_dims(np.array(outputs[i:i + fragment_length]), 0) output = model.predict(prediction_seed) output_dist = output[0][-1] output_val = draw_sample(output_dist) if predict_use_softmax_as_input: outputs.append(output_dist) else: outputs.append(output_val) write_samples(sample_stream, [output_val]) sample_stream.close() _log.info("Done!")
def predict(desired_sample_rate, fragment_length, _log, seed, _seed, _config, predict_seconds, data_dir, batch_size, fragment_stride, nb_output_bins, learn_all_outputs, run_dir, predict_use_softmax_as_input, use_ulaw, predict_initial_input, **kwargs): fragment_length = compute_receptive_field()[0] _config['fragment_length'] = fragment_length checkpoint_dir = os.path.join(run_dir, 'checkpoints') last_checkpoint = sorted(os.listdir(checkpoint_dir))[-1] epoch = int(re.match(r'checkpoint\.(\d+?)-.*', last_checkpoint).group(1)) _log.info('Using checkpoint from epoch: %s' % epoch) sample_dir = os.path.join(run_dir, 'samples') if not os.path.exists(sample_dir): os.mkdir(sample_dir) sample_name = make_sample_name(epoch) sample_filename = os.path.join(sample_dir, sample_name) _log.info('Saving to "%s"' % sample_filename) sample_stream = make_sample_stream(desired_sample_rate, sample_filename) model = build_model() model.load_weights(os.path.join(checkpoint_dir, last_checkpoint)) model.summary() if predict_initial_input is None: outputs = list(dataset.one_hot(np.zeros(fragment_length) + nb_output_bins / 2)) elif predict_initial_input != '': _log.info('Taking first %d (%.2fs) from \'%s\' as initial input.' % ( fragment_length, fragment_length / desired_sample_rate, predict_initial_input)) wav = dataset.process_wav(desired_sample_rate, predict_initial_input, use_ulaw) outputs = list(dataset.one_hot(wav[0:fragment_length])) else: _log.info('Taking sample from test dataset as initial input.') data_generators, _ = get_generators() outputs = list(data_generators['test'].next()[0][-1]) # write_samples(sample_stream, outputs) warned_repetition = False for i in tqdm(range(int(desired_sample_rate * predict_seconds))): if not warned_repetition: if np.argmax(outputs[-1]) == np.argmax(outputs[-2]) and np.argmax(outputs[-2]) == np.argmax(outputs[-3]): warned_repetition = True _log.warning('Last three predicted outputs where %d' % np.argmax(outputs[-1])) else: warned_repetition = False prediction_seed = np.expand_dims(np.array(outputs[i:i + fragment_length]), 0) output = model.predict(prediction_seed) output_dist = output[0][-1] output_val = draw_sample(output_dist) if predict_use_softmax_as_input: outputs.append(output_dist) else: outputs.append(output_val) write_samples(sample_stream, [output_val]) sample_stream.close() _log.info("Done!")