def synthesize(self, args, text, idx): seq = text_to_sequence(text) dec_input = np.zeros((1, 200, mel_dim), dtype='float32') pred = [] for i in range(1, 201): mel_out, alignment = self.session.run( [self.mel_output, self.alignment], feed_dict={ self.model.enc_input: [np.asarray(seq, dtype=np.int32)], self.model.sequence_length: np.asarray([len(seq)], dtype=np.int32), self.model.dec_input: dec_input }) if i < 200: dec_input[:, i, :] = mel_out[5 * i - 1, :] pred.extend(mel_out[5 * (i - 1):5 * i, :]) np.save(os.path.join(args.save_dir, 'mel-{}'.format(idx)), pred, allow_pickle=False) input_seq = sequence_to_text(seq) alignment_dir = os.path.join(args.save_dir, 'align-{}.png'.format(idx)) plot_alignment(alignment, alignment_dir, input_seq)
def test_step(text, idx): seq = text_to_sequence(text) enc_input = np.asarray([seq], dtype=np.int32) sequence_length = np.asarray([len(seq)], dtype=np.int32) dec_input = np.zeros((1, max_iter, mel_dim), dtype=np.float32) pred = [] for i in range(1, max_iter + 1): mel_out, alignment = model(enc_input, sequence_length, dec_input, is_training=False) if i < max_iter: dec_input[:, i, :] = mel_out[:, reduction * i - 1, :] pred.extend(mel_out[:, reduction * (i - 1):reduction * i, :]) pred = np.reshape(np.asarray(pred), [-1, mel_dim]) alignment = np.squeeze(alignment, axis=0) np.save(os.path.join(save_dir, 'mel-{}'.format(idx)), pred, allow_pickle=False) input_seq = sequence_to_text(seq) alignment_dir = os.path.join(save_dir, 'align-{}.png'.format(idx)) plot_alignment(alignment, alignment_dir, input_seq)
def train(args): save_dir = './checkpoint/1' checkpoint_path = os.path.join(save_dir, 'model.ckpt') coord = tf.train.Coordinator() feeder = DataFeeder(coord, mode=1) model = Tacotron() model.initialize(feeder.enc_input, feeder.sequence_length, feeder.dec_input, feeder.mel_target) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=2) with tf.Session() as sess: try: sess.run(tf.global_variables_initializer()) if args.step: load_dir = '{}-{}'.format(checkpoint_path, args.step) saver.restore(sess, load_dir) feeder.start_in_session(sess) while not coord.should_stop(): step, loss, optimize = sess.run( [model.global_step, model.loss, model.optimize]) print('Step: {}, Loss: {:.5f}'.format(step, loss)) if step % checkpoint_step == 0: saver.save(sess, checkpoint_path, global_step=step) input_seq, alignment, pred, target = \ sess.run([model.enc_input[0], model.alignment[0], model.mel_output[0], model.mel_target[0]]) input_seq = sequence_to_text(input_seq) alignment_dir = os.path.join( save_dir, 'step-{}-align.png'.format(step)) plot_alignment(alignment, alignment_dir, input_seq) except Exception as e: traceback.print_exc() coord.request_stop(e)
step = tf.Variable(0) checkpoint_dir = './checkpoint/1' os.makedirs(checkpoint_dir, exist_ok=True) checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model, step=step) manager = tf.train.CheckpointManager(checkpoint, checkpoint_dir, max_to_keep=5) checkpoint.restore(manager.latest_checkpoint) if manager.latest_checkpoint: print('Restore checkpoint from {}'.format(manager.latest_checkpoint)) try: for text, dec, mel, text_length in dataset: loss, pred, alignment = train_step(text, dec, mel, text_length) checkpoint.step.assign_add(1) print("Step: {}, Loss: {:.5f}".format(int(checkpoint.step), loss)) if int(checkpoint.step) % checkpoint_step == 0: checkpoint.save(file_prefix=os.path.join( checkpoint_dir, 'step-{}'.format(int(checkpoint.step)))) input_seq = sequence_to_text(text[0].numpy()) input_seq = input_seq[:text_length[0].numpy()] alignment_dir = os.path.join( checkpoint_dir, 'step-{}-align.png'.format(int(checkpoint.step))) plot_alignment(alignment, alignment_dir, input_seq) except Exception: traceback.print_exc()