def __call__(self, sample, wav_file, name_set): feature, label = sample['feature'], sample['label'] save = self.save config_file = self.config_file out_dir = self.out_dir feats_gmm_dir = self.feats_gmm_dir # Reading params/config params = utils.read_conf_file(file_name=config_file, conf_section='DEFAULTS')
3, reuse=True, training=False), feed_dict={net.input_z: sample_z}) samples.append(gen_samples) _ = utils.view_samples(-1, samples, 6, 12, figsize=(FLAGS.h_figsize, FLAGS.v_figsize)) plt.show() saver.save(sess, './checkpoints/generator.ckpt') with open('samples.pkl', 'wb') as f: pkl.dump(samples, f) fig, ax = plt.subplot() losses = np.array(losses) plt.plot(losses.T[0], label='Discriminator', alpha=0.5) plt.plot(losses.T[1], label='Generator', alpha=0.5) plt.title('Training Losses') plt.legend() plt.show() if __name__ == '__main__': args = parse_args() FLAGS = utils.read_conf_file(args.conf) main(FLAGS)
(step, loss_t, elapsed)) # summary if step % _SUMMARY_EPS == 0: tf.logging.info('adding summary...') summary_str = sess.run(summary) writer.add_summary(summary_str, step) writer.flush() # checkpoint if step % _SAVE_EPS == 0: saver.save(sess, os.path.join(training_path, 'fast-style-model.ckpt'), global_step=step) except tf.errors.OutOfRangeError: saver.save( sess, os.path.join(training_path, 'fast-style-model.ckpt-done')) tf.logging.info('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) def main(flag): training(flag) if __name__ == '__main__': FLAGS = read_conf_file(_YML_PATH + _DEFAULT_YML) main(FLAGS)
def __call__(self, sample, wav_file, name_set): waveform, label = sample['wave'], sample['label'] save = self.save config_file = self.config_file deltas = self.deltas out_dir = self.out_dir # frame-level feats params/config params = utils.read_conf_file(file_name=config_file, conf_section='DEFAULTS') # check if features are already computed if features do not exist, then compute them wav_name = os.path.splitext(os.path.basename(wav_file))[0] file_name = '/{0}_{1}'.format(self.feat_type, wav_name) feat_file_path = out_dir + '/' + file_name if not os.path.isfile(feat_file_path): # Compute without derivatives if deltas == 0: # Compute features feat = execute_extraction_function(feat_type=self.feat_type, waveform=waveform, **params) # Save features if asked for out_dir = out_dir + '/{0}/{1}/'.format(self.feat_type, name_set) if save: utils.save_features(out_dir, self.feat_type, wav_file, feat) utils.copy_conf(config_file, out_dir, self.feat_type) feature = {'feature': feat, 'label': label} return feature # Compute derivatives if asked for if deltas == 1: # Compute features feat = execute_extraction_function(feat_type=self.feat_type, waveform=waveform, **params) delta1 = torchaudio.functional.compute_deltas( feat) # compute 1st order feat = torch.cat((feat, delta1), 1) # Save features if asked for out_dir = out_dir + '/{0}/{1}/'.format(self.feat_type, name_set) if save: utils.save_features(out_dir, self.feat_type, '{0}_{1}del'.format(wav_file, deltas), feat) utils.copy_conf(config_file, out_dir, self.feat_type) feature = {'feature': feat, 'label': label} return feature if deltas == 2: # Compute features feat = execute_extraction_function(feat_type=self.feat_type, waveform=waveform, **params) delta1 = torchaudio.functional.compute_deltas( feat) # compute 1st order delta2 = torchaudio.functional.compute_deltas( delta1) # compute 2nd order feat = torch.cat((feat, delta1, delta2), 1) # Save features if asked for out_dir = out_dir + '/{0}/{1}/'.format(self.feat_type, name_set) if save: utils.save_features(out_dir, self.feat_type, '{0}_{1}del'.format(wav_file, deltas), feat) utils.copy_conf(config_file, out_dir, self.feat_type) feature = {'feature': feat, 'label': label} return feature # if features exist, then LOAD them else: feat = np.load(feat_file_path) feature = {'feature': feat, 'label': label} return feature
def compute_flfeats_offline(source_path, out_dir, feat_type, deltas=None, config_file=None): """Function to calculate the frame-level features and save them to files. The function saves one file (containing features) per utterance Args: source_path (string): Path to the wavs. out_dir (string): Type of the frame-level feature to extract from the utterances. Choose from: 'mfcc', 'fbanks', 'melspec'. Default is: 'fbanks'. feat_type (string): Type of the frame-level feature to extract from the utterances. Choose from: 'mfcc', 'fbanks', 'melspec'. Default is: 'fbanks'. deltas (int, optional): Compute delta coefficients of a tensor. '1' for first order derivative, '2' for second order. None for not using deltas. Default: None. config_file (string): Path to the configuration file (ini). """ list_wavs = utils.get_files_abspaths(path=source_path, file_type='.wav') # frame-level feats params/config from the config file params = utils.read_conf_file(file_name=config_file, conf_section='DEFAULTS') print("Computing {} for {} utterances in {}...".format( feat_type, len(list_wavs), source_path)) for wav_file in list_wavs: # Load wav waveform = utils.load_wav_torch(wav_file, max_length_in_seconds=5, pad_and_truncate=True) # Compute without derivatives if deltas == 0: # Compute features feat = execute_extraction_function(feat_type=feat_type, waveform=waveform, **params) final_dir = out_dir + '/{0}/{1}/'.format( feat_type, os.path.basename(source_path)) utils.save_features(final_dir, feat_type, wav_file, feat) utils.copy_conf(config_file, final_dir, feat_type) # Compute derivatives if asked for if deltas == 1: # Compute features feat = execute_extraction_function(feat_type=feat_type, waveform=waveform, **params) delta1 = torchaudio.functional.compute_deltas( feat) # compute 1st order feat = torch.cat((feat, delta1), 1) final_dir = out_dir + '/{0}/{1}/'.format( feat_type, os.path.basename(source_path)) utils.save_features(final_dir, feat_type, wav_file, feat) utils.copy_conf(config_file, final_dir, feat_type) if deltas == 2: # Compute features feat = execute_extraction_function(feat_type=feat_type, waveform=waveform, **params) delta1 = torchaudio.functional.compute_deltas( feat) # compute 1st order delta2 = torchaudio.functional.compute_deltas(delta1) feat = torch.cat((feat, delta1, delta2), 1) final_dir = out_dir + '/{0}/{1}/'.format( feat_type, os.path.basename(source_path)) utils.save_features(final_dir, feat_type, wav_file, feat) utils.copy_conf(config_file, final_dir, feat_type)
writer.flush() """checkpoint""" if step % 1000 == 0: saver.save(sess,os.path.join(training_path,'fast-style-model.ckpt'),global_step=step)#保存variable_to_restore中的参数值 except tf.errors.OutOfRangeError: saver.save(sess,os.path.join(training_path,'fast-style-model.ckpt-done')) tf.logging.info('Done training -- epoch limit reached') finally: coord.request_stop()#要求停止所有线程 coord.join(threads)#将线程并入主线程,删除 if __name__ == '__main__': tf.logging.set_verbosity(tf.logging.INFO) #将INFO及其以上的警告信息显示在屏幕上 args = parse_args() #设定命令行参数,主要是configure document 存储位置 FLAGS = utils.read_conf_file(args.conf) #读取configure main(FLAGS) #注意一个区别,tf.app.run()一般会与tf.app.flags.DEFINE_string()(定义命令行,同argparse.add_argument() ; FLAGS = tf.app.FLASG(读取命令行参数,如FLAGS.conf) 联用 #而argparse设定命令行参数,返回args=parse_args(),将命令行参数args作为main()的形参; #总结一下train.py的思路 #step1:取出loss_model,not train #step2:将origin image进行input前的预处理 #step2:将preprocessed origin输入“图像生成网络”,形成generated,“图像生成网络”需要train #step3:将generated进行input loss model前的预处理 #step4:将preprocessed origin和preprocessed generated送入loss model,得到各层的输出endpoints_dict #step5:计算内容损失,和,风格损失,得到total_loss #step5:建立需要训练的参数的list,以及需要restore and save的参数list #step6:利用Adam梯度下降来优化total_loss,需要训练的参数放在variable_to_train list中 #step6:将全局变量和局部变量初始化
"""开始训练""" coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) start_time = time.time() try: while not coord.should_stop(): _, loss_t, step = sess.run([train_op, loss, global_step]) elapsed_time = time.time() - start_time start_time = time.time() if step % 10 == 0: tf.logging.info( 'step: %d, total Loss %f, secs/step: %f,%s' % (step, loss_t, elapsed_time, time.asctime())) """checkpoint""" if step % 50 == 0: tf.logging.info('saving check point...') saver.save(sess, os.path.join(training_path, FLAGS.naming + '.ckpt'), global_step=step) except tf.errors.OutOfRangeError: saver.save(sess, os.path.join(training_path, 'fast-style-model.ckpt-done')) tf.logging.info('Done training -- epoch limit reached') finally: coord.request_stop() tf.logging.info('coordinator stop') coord.join(threads) if __name__ == '__main__': tf.logging.set_verbosity(tf.logging.INFO) args = parse_args() FLAGS = utils.read_conf_file(args.conf) main(FLAGS)