def transformer_test(hp, test_data): model = Transformer(hp) with model.graph.as_default(): saver = tf.train.Saver() num_data = len(test_data.pny_lst) length = test_data.data_length if length == None: length = num_data num = hp.count ran_num = random.randint(0, length - 1) han_num = 0 han_error_num = 0 data = '' with tf.Session(graph=model.graph) as sess: latest = tf.train.latest_checkpoint(Const.TransformerFolder) saver.restore(sess, latest) for i in range(num): try: print('\nthe ', i + 1, 'th example.') index = (ran_num + i) % num_data X, Y = test_data.get_transformer_data(index) hanzi = test_data.han_lst[index] tar_get = han2id(hanzi, hanzi_vocab) preds = sess.run(model.preds, feed_dict={ model.x: X, model.y: Y }) print(preds) han_pred = get_pred_han(preds) except ValueError: continue print('原文汉字结果:', ''.join(hanzi)) print('预测汉字结果:', han_pred) data += '原文汉字结果:' + ''.join(hanzi) + '\n' data += '预测汉字结果:' + han_pred + '\n' # 汉字距离 words_n = np.array(preds).shape[0] han_num += words_n # 把句子的总字数加上 han_edit_distance = GetEditDistance(np.array(tar_get), preds[0]) if (han_edit_distance <= words_n): # 当编辑距离小于等于句子字数时 han_error_num += han_edit_distance # 使用编辑距离作为错误字数 else: # 否则肯定是增加了一堆乱七八糟的奇奇怪怪的字 han_error_num += words_n # 就直接加句子本来的总字数就好了 data += '*[Test Result] Speech Recognition ' + test_data.data_type + ' set word accuracy ratio: ' + str( (1 - han_error_num / han_num) * 100) + '%' filename = str(datetime.datetime.now()) + '_' + str(num) with open(os.path.join(Const.PredResultFolder, filename), 'w') as f: f.writelines(data) print( '*[Test Result] Speech Recognition ' + test_data.data_type + ' set 汉字 word accuracy ratio: ', (1 - han_error_num / han_num) * 100, '%') pass
def recognition(type='dfcnn'): file = '../wav_file/input1.wav' # 现场输入识别 receive_wav(file) if type == 'dfcnn': # 1.声学模型----------------------------------- hparams = AmHparams() parser = hparams.parser hp = parser.parse_args() am_model = CNNCTCModel(hp) print('loading acoustic model...') select_model_step = 'model_04-14.91' am_model.load_model(select_model_step) # 2.语言模型----------------------------------- hparams = LmHparams() parser = hparams.parser hp = parser.parse_args() hp.is_training = False print('loading language model...') lm_model = Language_Model(hp) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.1) sess = tf.Session(graph=lm_model.graph, config=tf.ConfigProto(gpu_options=gpu_options)) with lm_model.graph.as_default(): saver = tf.train.Saver() with sess.as_default(): latest = tf.train.latest_checkpoint(Const.LmModelFolder) saver.restore(sess, latest) while (True): dfcnn_speech(sess, am_model, lm_model, file) if type == 'transformer': hparams = TransformerHparams() parser = hparams.parser hp = parser.parse_args() hp.is_training = False train_data = prepare_data('train', hp, shuffle=True, length=None) model = Transformer(hp) with model.graph.as_default(): saver = tf.train.Saver() with tf.Session(graph=model.graph) as sess: latest = tf.train.latest_checkpoint(Const.TransformerFolder) saver.restore(sess, latest) while (True): transformer_speech(sess, model, train_data, file)
def transformer_train(args, train_data): model = Transformer(args) epochs = args.epochs batch_num = len(train_data.path_lst) // train_data.batch_size with model.graph.as_default(): saver = tf.train.Saver(max_to_keep=50) with tf.Session(graph=model.graph) as sess: merged = tf.summary.merge_all() sess.run(tf.global_variables_initializer()) add_num = 0 if os.path.exists(Const.TransformerFolder): print('loading transformer model...') latest = tf.train.latest_checkpoint(Const.TransformerFolder) if latest != None: add_num = int(latest.split('_')[-2]) saver.restore(sess, latest) writer = tf.summary.FileWriter(Const.TransformerTensorboard, tf.get_default_graph()) generator = train_data.get_transformer_batch() for k in range(epochs): total_loss = 0 # 输入的数据是拼音数据 # 输出的是文字数据 for i in range(batch_num): data = next(generator) X = data["the_inputs"] Y = data["the_labels"] y_target = data["ground_truth"] feed = {model.x: X, model.y: Y, model.target: y_target} cost, _ = sess.run([model.mean_loss, model.train_op], feed_dict=feed) total_loss += cost if i % 10 == 0: print("epoch: %d step: %d/%d train loss=6%f" % (k + 1, i, batch_num, cost)) rs = sess.run(merged, feed_dict=feed) writer.add_summary(rs, k * batch_num + i) print('epochs', k + 1, ': average loss = ', total_loss / batch_num) saver.save( sess, Const.TransformerFolder + 'transformer_model_%d_%.3f.ckpt' % (k + 1 + add_num, total_loss / batch_num)) writer.close() pass
if __name__ == "__main__": parser = get_parser() data_params = parser.parse_args() check_all_data_params(data_params) exp_name = data_params.exp_name is_variational = data_params.variational > 0 use_distance_loss = data_params.use_distance_loss > 0 load_from_checkpoint = data_params.load_from_checkpoint > 0 logging.basicConfig(filename="logs/" + exp_name + "_eval" + ".log", level=logging.DEBUG) model = Transformer(data_params=data_params, logger=logging, init_emb=True, embd_file="corpora/mono/all.en-fr.60000.vec", is_variational=is_variational) if torch.cuda.is_available(): print("Using cuda") device = torch.device("cuda:0") model = nn.DataParallel(model) # self.logger.debug("transformer ", self.transformer) model.to(device) else: print("Not using cuda") device = torch.device('cpu') filename = exp_name+".pth" state = torch.load(filename, map_location=device)
except Exception as e: self.logger.debug("Exception in training loop") self.logger.debug(e.message) def test(self, n_tests): self.transformer.eval() lang = 0 get_iterator = self.get_lm_iterator(lang=lang, train=True, add_noise=True) train_iterator = get_iterator() for i in range(n_tests): batch_dict = next(train_iterator) #self.greedy_decoding(batch_dict, lang) self.output_samples(batch_dict, lang, lang) if __name__ == "__main__": logger = create_logger("logs/en_language_model.log") parser = get_parser() data_params = parser.parse_args() check_all_data_params(data_params) model = Transformer(data_params=data_params, logger=logger, init_emb=False) trainer = LanguageModeling(model) trainer.train(3000) trainer.save_model("en_language_model.pth") logger.info("testing trained model") trainer.test(10) logger.info("testing loaded model") trainer.load_model("en_language_model.pth") trainer.test(10)
loss = self.translation_loss(batch_dict, lang1, lang2) logging.info("translation loss", loss) if __name__ == "__main__": parser = get_parser() data_params = parser.parse_args() check_all_data_params(data_params) exp_name = data_params.exp_name is_variational = data_params.variational > 0 use_distance_loss = data_params.use_distance_loss > 0 load_from_checkpoint = data_params.load_from_checkpoint > 0 logging.basicConfig(filename="logs/" + exp_name + ".log", level=logging.DEBUG) model = Transformer(data_params=data_params, logger=logging, init_emb=True, embd_file="corpora/mono/all.en-fr.60000.vec", is_variational=is_variational) trainer = UnsupervisedTrainer(model, exp_name, use_distance_loss=use_distance_loss, load_from_checkpoint=load_from_checkpoint) trainer.train(2 * 50000) trainer.checkpoint(exp_name + ".pth")
output_types=(tf.int32, tf.int32)).shuffle(config.buffer_size).batch( config.inference_size, drop_remainder=True).repeat().prefetch(PREFETCH)) dataset_test_iterator = iter(dataset_test) # Tokenizer logger.info("Getting Tokenizer") tokenizer = data_loader.tokenizer tokenizer_ori: tf.keras.preprocessing.text.Tokenizer = tokenizer tokenizer_tar: tf.keras.preprocessing.text.Tokenizer = tokenizer # Model # encoder = Encoder(vocab_size_ori, **config.encoder['args']) # decoder = Decoder(vocab_size_tar, **config.decoder['args']) transformer = Transformer(vocab_size=data_loader.vocab_size, **config.transformer["args"]) # Tensorboard log_writer = namedtuple("logWriter", ["train", "test"]) log_writer.train = tf.summary.create_file_writer(logdir=config.logdir + "_train") log_writer.test = tf.summary.create_file_writer(logdir=config.logdir + "_test") # Optimizer optimizer = Adam(**config.optimizer["args"]) # Checkpoint & Manager Checkpoint = tf.train.Checkpoint CheckpointManager = tf.train.CheckpointManager ckpt = Checkpoint( step=tf.Variable(initial_value=0, dtype=tf.int64),
train_iterator = get_iterator() for i in range(n_tests): batch_dict = next(train_iterator) #self.greedy_decoding(batch_dict, lang1, lang2) self.output_samples(batch_dict, lang1, lang2) loss = self.translation_loss(batch_dict, lang1, lang2) self.logger.info("translation loss", loss) if __name__ == "__main__": logger = create_logger("logs/para_trainer.log") parser = get_parser() data_params = parser.parse_args() check_all_data_params(data_params) model = Transformer(data_params=data_params, logger=logger, init_emb=True, embd_file="corpora/mono/all.en-fr.60000.vec") trainer = ParallelTrainer(model) # test iterator # get_iter = trainer.get_para_iterator(lang1=0, lang2=1, train=False, add_noise=False) # iter = get_iter() # batch_dict = next(iter) # prev_output = batch_dict["prev_output"] # tgt_mask = batch_dict["tgt_mask"] # tgt_batch = batch_dict["tgt_batch"] # # print("prev_output", prev_output) # print("tgt_mask", tgt_mask) # print("tgt_batch", tgt_batch)