def main(prj_dir=None, model=None, mode=None): # Configuration Part # if mode is 'train': import path_setting as ps set_path = ps.PathSetting(prj_dir, model) logs_dir = initial_logs_dir = set_path.logs_dir input_dir = set_path.input_dir output_dir = set_path.output_dir norm_dir = set_path.norm_dir valid_file_dir = set_path.valid_file_dir sys.path.insert(0, prj_dir + '/configure/ACAM') import config as cg global initLr, dropout_rate, max_epoch, batch_size, valid_batch_size initLr = cg.lr dropout_rate = cg.dropout_rate max_epoch = cg.max_epoch batch_size = valid_batch_size = cg.batch_size global w, u w = cg.w u = cg.u global bdnn_winlen, bdnn_inputsize, bdnn_outputsize bdnn_winlen = (((w - 1) / u) * 2) + 3 bdnn_inputsize = int(bdnn_winlen * num_features) bdnn_outputsize = int(bdnn_winlen) global glimpse_hidden, bp_hidden, glimpse_out, bp_out, nGlimpses,\ lstm_cell_size, action_hidden_1, action_hidden_2 glimpse_hidden = cg.glimpse_hidden bp_hidden = cg.bp_hidden glimpse_out = bp_out = cg.glimpse_out nGlimpses = cg.nGlimpse # 7 lstm_cell_size = cg.lstm_cell_size action_hidden_1 = cg.action_hidden_1 # default : 256 action_hidden_2 = cg.action_hidden_2 # default : 256 # Graph Part # mean_acc_list = [] var_acc_list = [] print('Mode : ' + mode) print("Graph initialization...") with tf.device(device): with tf.variable_scope("model", reuse=None): m_train = Model(batch_size=batch_size, reuse=None, is_training=True) # m_train(batch_size) with tf.device(device): with tf.variable_scope("model", reuse=True): m_valid = Model(batch_size=valid_batch_size, reuse=True, is_training=False) print("Done") # Summary Part # print("Setting up summary op...") summary_ph = tf.placeholder(dtype=tf.float32) with tf.variable_scope("Training_procedure"): cost_summary_op = tf.summary.scalar("cost", summary_ph) accuracy_summary_op = tf.summary.scalar("accuracy", summary_ph) # train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', max_queue=4) # valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=4) # summary_dic = summary_generation(valid_file_dir) print("Done") # Model Save Part # print("Setting up Saver...") saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(initial_logs_dir) print("Done") # Session Part # sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess_config.gpu_options.allow_growth = True sess = tf.Session(config=sess_config) if mode is 'train': train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', sess.graph, max_queue=2) valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=2) if ckpt and ckpt.model_checkpoint_path: # model restore print("Model restored...") print(initial_logs_dir + ckpt_name) if mode is 'train': saver.restore(sess, ckpt.model_checkpoint_path) else: saver.restore(sess, initial_logs_dir + ckpt_name) saver.save(sess, initial_logs_dir + "/model_ACAM.ckpt", 0) # model save print("Done") else: sess.run(tf.global_variables_initializer() ) # if the checkpoint doesn't exist, do initialization if mode is 'train': train_data_set = dr.DataReader( input_dir, output_dir, norm_dir, w=w, u=u, name="train") # training data reader initialization if mode is 'train': for itr in range(max_epoch): start_time = time.time() train_inputs, train_labels = train_data_set.next_batch(batch_size) feed_dict = { m_train.inputs: train_inputs, m_train.labels: train_labels, m_train.keep_probability: dropout_rate } sess.run(m_train.train_op, feed_dict=feed_dict) if itr % 10 == 0 and itr >= 0: train_cost, train_reward, train_avg_b, train_rminusb, train_p_bps, train_lr \ = sess.run([m_train.cost, m_train.reward, m_train.avg_b, m_train.rminusb, m_train.p_bps, m_train.print_lr] , feed_dict=feed_dict) duration = time.time() - start_time print( "Step: %d, cost: %.4f, accuracy: %4.4f, b: %4.4f, R-b: %4.4f, p_bps: %4.4f, lr: %7.6f (%.3f sec)" % (itr, train_cost, train_reward, train_avg_b, train_rminusb, train_p_bps, train_lr, duration)) train_cost_summary_str = sess.run( cost_summary_op, feed_dict={summary_ph: train_cost}) train_accuracy_summary_str = sess.run( accuracy_summary_op, feed_dict={summary_ph: train_reward}) train_summary_writer.add_summary( train_cost_summary_str, itr) # write the train phase summary to event files train_summary_writer.add_summary(train_accuracy_summary_str, itr) # if train_data_set.eof_checker(): # if itr % val_freq == 0 and itr >= val_start_step: if itr % 50 == 0 and itr > 0: saver.save(sess, logs_dir + "/model.ckpt", itr) # model save print('validation start!') valid_accuracy, valid_cost = \ utils.do_validation(m_valid, sess, valid_file_dir, norm_dir, type='ACAM') print("valid_cost: %.4f, valid_accuracy=%4.4f" % (valid_cost, valid_accuracy * 100)) valid_cost_summary_str = sess.run( cost_summary_op, feed_dict={summary_ph: valid_cost}) valid_accuracy_summary_str = sess.run( accuracy_summary_op, feed_dict={summary_ph: valid_accuracy}) valid_summary_writer.add_summary( valid_cost_summary_str, itr) # write the train phase summary to event files valid_summary_writer.add_summary(valid_accuracy_summary_str, itr) # mean_accuracy, var_accuracy = full_evaluation(m_valid, sess, valid_batch_size, valid_file_dir, valid_summary_writer, summary_dic, itr) # if mean_accuracy >= 0.991: # # print('model was saved!') # model_name = '/model' + str(int(mean_accuracy * 1e4)) + 'and'\ # + str(int(var_accuracy * 1e5)) + '.ckpt' # saver.save(sess, save_dir + model_name, itr) # mean_acc_list.append(mean_accuracy) # var_acc_list.append(var_accuracy) # train_data_set.initialize() elif mode == 'test': final_softout, final_label = utils.vad_test(m_valid, sess, valid_batch_size, test_file_dir, norm_dir, data_len, eval_type) # if data_len is None: # return final_softout, final_label # else: # final_softout = final_softout[0:data_len, :] # final_label = final_label[0:data_len, :] # fpr, tpr, thresholds = metrics.roc_curve(final_label, final_softout, pos_label=1) # eval_auc = metrics.auc(fpr, tpr) # print(eval_auc) # full_evaluation(m_valid, sess, valid_batch_size, test_file_dir, valid_summary_writer, summary_dic, 0) # if visualization: # global attention # attention = np.asarray(attention) # sio.savemat('attention.mat', {'attention' : attention}) # subprocess.call(['./visualize.sh']) if data_len is None: return final_softout, final_label else: return final_softout[0:data_len, :], final_label[0:data_len, :]
def main(save_dir, prj_dir=None, model=None, mode=None, dev="/gpu:2"): # Configuration Part # # os.environ["CUDA_VISIBLE_DEVICES"] = '3' device = dev os.environ["CUDA_VISIBLE_DEVICES"] = device[-1] if mode is 'train': import path_setting as ps set_path = ps.PathSetting(prj_dir, model,save_dir) logs_dir = initial_logs_dir = set_path.logs_dir input_dir = set_path.input_dir output_dir = set_path.output_dir norm_dir = set_path.norm_dir valid_file_dir = set_path.valid_file_dir sys.path.insert(0, prj_dir+'/configure/LSTM') import config as cg global seq_size, batch_num seq_size = cg.seq_len batch_num = cg.num_batches global learning_rate, dropout_rate, max_epoch, batch_size, valid_batch_size learning_rate = cg.lr dropout_rate = cg.dropout_rate max_epoch = cg.max_epoch batch_size = valid_batch_size = batch_num*seq_size global target_delay target_delay = cg.target_delay global lstm_cell_size, num_layers lstm_cell_size = cg.cell_size num_layers = cg.num_layers # Graph Part # print("Graph initialization...") with tf.device(device): with tf.variable_scope("model", reuse=None): m_train = Model(is_training=True) with tf.variable_scope("model", reuse=True): m_valid = Model(is_training=False) print("Done") # Summary Part # print("Setting up summary op...") summary_ph = tf.placeholder(dtype=tf.float32) with tf.variable_scope("Training_procedure"): cost_summary_op = tf.summary.scalar("cost", summary_ph) accuracy_summary_op = tf.summary.scalar("accuracy", summary_ph) print("Done") # Model Save Part # print("Setting up Saver...") saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(logs_dir) print("Done") # Session Part # sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess_config.gpu_options.allow_growth = True sess = tf.Session(config=sess_config) if mode is 'train': train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', sess.graph, max_queue=2) valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=2) if ckpt and ckpt.model_checkpoint_path: # model restore print("Model restored...") if mode is 'train': saver.restore(sess, ckpt.model_checkpoint_path) else: saver.restore(sess, initial_logs_dir+ckpt_name) # saver.restore(sess, logs_dir+ckpt_name) saver.save(sess, logs_dir + "/model_LSTM.ckpt", 0) # model save print("Done") else: sess.run(tf.global_variables_initializer()) # if the checkpoint doesn't exist, do initialization if mode is 'train': train_data_set = dr.DataReader(input_dir, output_dir, norm_dir, target_delay=target_delay, u=u, name="train") # training data reader initialization if mode is 'train': file_len = train_data_set.get_file_len() MAX_STEP = max_epoch * file_len print(get_num_params()) for itr in range(MAX_STEP): train_inputs, train_labels = train_data_set.next_batch(seq_size) one_hot_labels = train_labels.reshape((-1, 1)) one_hot_labels = dense_to_one_hot(one_hot_labels, num_classes=2) feed_dict = {m_train.inputs: train_inputs, m_train.labels: one_hot_labels, m_train.keep_probability: dropout_rate} sess.run(m_train.train_op, feed_dict=feed_dict) if itr % 100 == 0 and itr >= 0: train_cost, train_accuracy, train_rate, train_pre = sess.run([m_train.cost, m_train.accuracy, m_train.lr, m_train.pred], feed_dict=feed_dict) # print(logits) print("Step: %d, train_cost: %.4f, train_accuracy=%4.4f lr=%.8f" % (itr, train_cost, train_accuracy*100, train_rate)) pass train_cost_summary_str = sess.run(cost_summary_op, feed_dict={summary_ph: train_cost}) train_accuracy_summary_str = sess.run(accuracy_summary_op, feed_dict={summary_ph: train_accuracy}) train_summary_writer.add_summary(train_cost_summary_str, itr) # write the train phase summary to event files train_summary_writer.add_summary(train_accuracy_summary_str, itr) if itr % file_len == 0 and itr > 0: saver.save(sess, logs_dir + "/model.ckpt", itr) # model save print('validation start!') valid_accuracy, valid_cost = \ utils.do_validation(m_valid, sess, valid_file_dir, norm_dir, type='LSTM') print("valid_cost: %.4f, valid_accuracy=%4.4f" % (valid_cost, valid_accuracy * 100)) valid_cost_summary_str = sess.run(cost_summary_op, feed_dict={summary_ph: valid_cost}) valid_accuracy_summary_str = sess.run(accuracy_summary_op, feed_dict={summary_ph: valid_accuracy}) valid_summary_writer.add_summary(valid_cost_summary_str, itr) # write the train phase summary to event files valid_summary_writer.add_summary(valid_accuracy_summary_str, itr) gs.freeze_graph(prj_dir + '/logs/LSTM', prj_dir + '/saved_model/graph/LSTM', 'model_1/soft_pred,model_1/raw_labels') elif mode is 'test': final_softout, final_label = utils.vad_test3(m_valid, sess, valid_batch_size, test_file_dir, norm_dir, data_len, eval_type) if data_len is None: return final_softout, final_label else: return final_softout[0:data_len, :], final_label[0:data_len, :]
os.system("mkdir " + save_dir + '/train') os.system("mkdir " + save_dir + '/valid') os.system( "matlab -r \"try acoustic_feat_ex(\'%s\',\'%s\'); catch; end; quit\"" % (train_data_dir, train_save_dir)) os.system( "matlab -r \"try acoustic_feat_ex(\'%s\',\'%s\'); catch; end; quit\"" % (valid_data_dir, valid_save_dir)) train_norm_dir = save_dir + '/train/global_normalize_factor.mat' test_norm_dir = prj_dir + '/norm_data/global_normalize_factor.mat' os.system("cp %s %s" % (train_norm_dir, test_norm_dir)) if mode == 0: set_path = ps.PathSetting(prj_dir, 'ACAM', save_dir) logs_dir = set_path.logs_dir os.system("rm -rf " + logs_dir + '/train') os.system("rm -rf " + logs_dir + '/valid') os.system("mkdir " + logs_dir + '/train') os.system("mkdir " + logs_dir + '/valid') Vp.main(save_dir, prj_dir, 'ACAM', 'train', dev='/gpu:' + gpu_no) gs.freeze_graph(prj_dir + '/logs/ACAM', prj_dir + '/saved_model/graph/ACAM', 'model_1/logits,model_1/raw_labels') if mode == 1: set_path = ps.PathSetting(prj_dir, 'bDNN', save_dir)
def main(save_dir, prj_dir=None, model=None, mode=None, dev="/gpu:2"): device = dev os.environ["CUDA_VISIBLE_DEVICES"] = device[-1] import path_setting as ps set_path = ps.PathSetting(prj_dir, model, save_dir) logs_dir = initial_logs_dir = set_path.logs_dir input_dir = set_path.input_dir output_dir = set_path.output_dir norm_dir = set_path.norm_dir valid_file_dir = set_path.valid_file_dir sys.path.insert(0, prj_dir + '/configure/LSTM') import config as cg global seq_size, batch_num seq_size = cg.seq_len batch_num = cg.num_batches global learning_rate, dropout_rate, max_epoch, batch_size, valid_batch_size learning_rate = cg.lr dropout_rate = cg.dropout_rate max_epoch = cg.max_epoch batch_size = valid_batch_size = batch_num * seq_size global target_delay target_delay = cg.target_delay global lstm_cell_size, num_layers lstm_cell_size = cg.cell_size num_layers = cg.num_layers print("Graph initialization...") with tf.device(device): with tf.variable_scope("model", reuse=None): m_train = Model(is_training=True) with tf.variable_scope("model", reuse=True): m_valid = Model(is_training=False) print("Done") print("Setting up Saver...") saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(logs_dir) print("Done") sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess_config.gpu_options.allow_growth = True sess = tf.Session(config=sess_config) if ckpt and ckpt.model_checkpoint_path: # model restore print("Model restored...") if mode is 'train': saver.restore(sess, ckpt.model_checkpoint_path) else: saver.restore(sess, initial_logs_dir + ckpt_name) saver.save(sess, logs_dir + "/model_LSTM.ckpt", 0) # model save print("Done") train_data_set = dr.DataReader(input_dir, output_dir, norm_dir, target_delay=target_delay, u=u, name="train") if mode is 'train': file_len = train_data_set.get_file_len() acc_sum = 0 tp_sum = 0 tn_sum = 0 fp_sum = 0 fn_sum = 0 frame_num = 0 for itr in range(file_len): train_inputs, train_labels = train_data_set.next_batch(seq_size) one_hot_labels = train_labels.reshape((-1, 1)) one_hot_labels = dense_to_one_hot(one_hot_labels, num_classes=2) feed_dict = { m_train.inputs: train_inputs, m_train.labels: one_hot_labels, m_train.keep_probability: dropout_rate } train_accuracy, train_pre, raw_label = sess.run( [m_train.accuracy, m_train.pre, m_train.raw_labels], feed_dict=feed_dict) frame_s = len(train_pre) frame_num += len(train_pre) tn = 0 tp = 0 fp = 0 fn = 0 for i in range(len(train_pre)): if train_pre[i] == 0 and raw_label[i] == 0: tn += 1 elif train_pre[i] == 0 and raw_label[i] == 1: fn += 1 elif train_pre[i] == 1 and raw_label[i] == 0: fp += 1 elif train_pre[i] == 1 and raw_label[i] == 1: tp += 1 acc_sum += train_accuracy tn_sum += tn tp_sum += tp fp_sum += fp fn_sum += fn # if train_accuracy <= 0.7: # file_name = train_data_set.get_cur_file_name().split('/')[-1] # obj_name = file_name.split('.')[0] # wav_path = "/mnt/E_DRIVE/TIMIT_echo_noisy/train/low" # shutil.copy("/mnt/E_DRIVE/TIMIT_echo_noisy/train/"+obj_name+'.wav', wav_path) # np.save(os.path.join("/mnt/E_DRIVE/TIMIT_echo_noisy/train/low", obj_name+'.label.npy'), original_label(train_pre,"/mnt/E_DRIVE/TIMIT_echo_noisy/train/"+obj_name+'.wav')) print("Step: %d/%d, train_accuracy=%4.4f" % (file_len, itr, train_accuracy * 100)) # print("path is "+train_data_set.get_cur_file_name()) print( "true_positive: %f, false positive: %f, true negative: %f, false negative: %f" % (tp / frame_s, fp / frame_s, tn / frame_s, fn / frame_s)) # valid_file_reader = dr.DataReader(valid_file_dir+'/feature_mrcg', valid_file_dir+'/label', norm_dir, target_delay = target_delay, u=u, name="train") # valid_len = valid_file_reader.get_file_len() # valid_accuracy, valid_cost = utils.do_validation(m_valid, sess, valid_file_dir, norm_dir, type='LSTM') # total_acc = (valid_accuracy*valid_len+acc_sum)/(valid_len+file_len) total_acc = acc_sum / file_len print("valid_accuracy=%4.4f" % (total_acc * 100)) print( "total: true_positive: %f, false positive: %f, true negative: %f, false negative: %f" % (tp_sum / frame_num, fp_sum / frame_num, tn_sum / frame_num, fn_sum / frame_num))
os.system("mkdir " + save_dir) os.system("mkdir " + save_dir + '/train') os.system("mkdir " + save_dir + '/valid') os.system( "matlab -r \"try acoustic_feat_ex(\'%s\',\'%s\'); catch; end; quit\"" % (train_data_dir, train_save_dir)) os.system( "matlab -r \"try acoustic_feat_ex(\'%s\',\'%s\'); catch; end; quit\"" % (valid_data_dir, valid_save_dir)) train_norm_dir = save_dir + '/train/global_normalize_factor.mat' test_norm_dir = prj_dir + '/norm_data/global_normalize_factor.mat' os.system("cp %s %s" % (train_norm_dir, test_norm_dir)) if mode == 0: set_path = ps.PathSetting(prj_dir, 'ACAM') logs_dir = set_path.logs_dir os.system("rm -rf " + logs_dir + '/train') os.system("rm -rf " + logs_dir + '/valid') os.system("mkdir " + logs_dir + '/train') os.system("mkdir " + logs_dir + '/valid') Vp.main(prj_dir, 'ACAM', 'train') # Vp.train_config(save_dir+'/train', save_dir+'/valid', prj_dir+'/logs', batch_size, # train_step, 'train') # # Vp.main() gs.freeze_graph(prj_dir + '/logs/ACAM', prj_dir + '/saved_model/graph/ACAM', 'model_1/logits,model_1/raw_labels')
def main(save_dir, prj_dir=None, model=None, mode=None, dev="/gpu:2"): # Configuration Part # # os.environ["CUDA_VISIBLE_DEVICES"] = '3' device = dev os.environ["CUDA_VISIBLE_DEVICES"] = device[-1] if mode is 'train': import path_setting as ps set_path = ps.PathSetting(prj_dir, model, save_dir) logs_dir = initial_logs_dir = set_path.logs_dir input_dir = set_path.input_dir output_dir = set_path.output_dir norm_dir = set_path.norm_dir valid_file_dir = set_path.valid_file_dir sys.path.insert(0, prj_dir + '/configure/ACAM') import config as cg global initLr, dropout_rate, max_epoch, batch_size, valid_batch_size initLr = cg.lr dropout_rate = cg.dropout_rate max_epoch = cg.max_epoch batch_size = valid_batch_size = cg.batch_size global w, u w = cg.w u = cg.u global bdnn_winlen, bdnn_inputsize, bdnn_outputsize bdnn_winlen = (((w - 1) / u) * 2) + 3 bdnn_inputsize = int(bdnn_winlen * num_features) bdnn_outputsize = int(bdnn_winlen) global glimpse_hidden, bp_hidden, glimpse_out, bp_out, nGlimpses,\ lstm_cell_size, action_hidden_1, action_hidden_2 glimpse_hidden = cg.glimpse_hidden bp_hidden = cg.bp_hidden glimpse_out = bp_out = cg.glimpse_out nGlimpses = cg.nGlimpse # 7 lstm_cell_size = cg.lstm_cell_size action_hidden_1 = cg.action_hidden_1 # default : 256 action_hidden_2 = cg.action_hidden_2 # default : 256 # Graph Part # mean_acc_list = [] var_acc_list = [] print('Mode : ' + mode) print("Graph initialization...") with tf.device(device): with tf.variable_scope("model", reuse=None): m_train = Model(batch_size=batch_size, reuse=None, is_training=True) # m_train(batch_size) with tf.device(device): with tf.variable_scope("model", reuse=True): m_valid = Model(batch_size=valid_batch_size, reuse=True, is_training=False) print("Done") # Summary Part # print("Setting up summary op...") summary_ph = tf.placeholder(dtype=tf.float32) with tf.variable_scope("Training_procedure"): cost_summary_op = tf.summary.scalar("cost", summary_ph) accuracy_summary_op = tf.summary.scalar("accuracy", summary_ph) # train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', max_queue=4) # valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=4) # summary_dic = summary_generation(valid_file_dir) print("Done") # Model Save Part # print("Setting up Saver...") saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(initial_logs_dir) print("Done") # Session Part # sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess_config.gpu_options.allow_growth = True sess = tf.Session(config=sess_config) if mode is 'train': train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', sess.graph, max_queue=2) valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=2) if ckpt and ckpt.model_checkpoint_path: # model restore print("Model restored...") print(initial_logs_dir + ckpt_name) if mode is 'train': saver.restore(sess, ckpt.model_checkpoint_path) else: saver.restore(sess, initial_logs_dir + ckpt_name) saver.save(sess, initial_logs_dir + "/model_ACAM.ckpt", 0) # model save print("Done") else: sess.run(tf.global_variables_initializer() ) # if the checkpoint doesn't exist, do initialization if mode is 'train': train_data_set = dr.DataReader( input_dir, output_dir, norm_dir, w=w, u=u, name="train") # training data reader initialization if mode is 'train': file_len = train_data_set.get_file_len() MAX_STEP = max_epoch * file_len print(get_num_params()) for itr in range(MAX_STEP): start_time = time.time() train_inputs, train_labels = train_data_set.next_batch(batch_size) feed_dict = { m_train.inputs: train_inputs, m_train.labels: train_labels, m_train.keep_probability: dropout_rate } sess.run(m_train.train_op, feed_dict=feed_dict) if itr % 100 == 0 and itr >= 0: train_cost, train_reward, train_avg_b, train_rminusb, train_p_bps, train_lr, train_res \ = sess.run([m_train.cost, m_train.reward, m_train.avg_b, m_train.rminusb, m_train.p_bps, m_train.print_lr,m_train.result] , feed_dict=feed_dict) duration = time.time() - start_time print( "Step: %d, cost: %.4f, accuracy: %4.4f, b: %4.4f, R-b: %4.4f, p_bps: %4.4f, lr: %7.6f (%.3f sec)" % (itr, train_cost, train_reward, train_avg_b, train_rminusb, train_p_bps, train_lr, duration)) # np.save('pre/'+train_data_set.get_cur_file_name().split('/')[-1], train_res) train_cost_summary_str = sess.run( cost_summary_op, feed_dict={summary_ph: train_cost}) train_accuracy_summary_str = sess.run( accuracy_summary_op, feed_dict={summary_ph: train_reward}) train_summary_writer.add_summary(train_cost_summary_str, itr) train_summary_writer.add_summary(train_accuracy_summary_str, itr) if itr % file_len == 0 and itr > 0: saver.save(sess, logs_dir + "/model.ckpt", itr) # model save print('validation start!') valid_accuracy, valid_cost = \ utils.do_validation(m_valid, sess, valid_file_dir, norm_dir, type='ACAM') print("valid_cost: %.4f, valid_accuracy=%4.4f" % (valid_cost, valid_accuracy * 100)) valid_cost_summary_str = sess.run( cost_summary_op, feed_dict={summary_ph: valid_cost}) valid_accuracy_summary_str = sess.run( accuracy_summary_op, feed_dict={summary_ph: valid_accuracy}) valid_summary_writer.add_summary( valid_cost_summary_str, itr) # write the train phase summary to event files valid_summary_writer.add_summary(valid_accuracy_summary_str, itr) gs.freeze_graph(prj_dir + '/logs/ACAM', prj_dir + '/saved_model/graph/ACAM', 'model_1/logits,model_1/raw_labels') elif mode == 'test': final_softout, final_label = utils.vad_test(m_valid, sess, valid_batch_size, test_file_dir, norm_dir, data_len, eval_type) if data_len is None: return final_softout, final_label else: return final_softout[0:data_len, :], final_label[0:data_len, :]
def main(prj_dir=None, model=None, mode=None): # Configuration Part # if mode is 'train': import path_setting as ps set_path = ps.PathSetting(prj_dir, model) logs_dir = initial_logs_dir = set_path.logs_dir input_dir = set_path.input_dir output_dir = set_path.output_dir norm_dir = set_path.norm_dir valid_file_dir = set_path.valid_file_dir sys.path.insert(0, prj_dir+'/configure/bDNN') import config as cg global initLr, dropout_rate, max_epoch, batch_size, valid_batch_size initLr = cg.lr dropout_rate = cg.dropout_rate max_epoch = cg.max_epoch batch_size = valid_batch_size = cg.batch_size global w, u w = cg.w u = cg.u global bdnn_winlen, bdnn_inputsize, bdnn_outputsize bdnn_winlen = (((w-1) / u) * 2) + 3 bdnn_inputsize = int(bdnn_winlen * num_features) bdnn_outputsize = int(bdnn_winlen) global num_hidden_1, num_hidden_2 num_hidden_1 = cg.num_hidden_1 num_hidden_2 = cg.num_hidden_2 # Graph Part # print("Graph initialization...") with tf.device(device): with tf.variable_scope("model", reuse=None): m_train = Model(is_training=True) with tf.variable_scope("model", reuse=True): m_valid = Model(is_training=False) print("Done") # Summary Part # print("Setting up summary op...") summary_ph = tf.placeholder(dtype=tf.float32) with tf.variable_scope("Training_procedure"): cost_summary_op = tf.summary.scalar("cost", summary_ph) accuracy_summary_op = tf.summary.scalar("accuracy", summary_ph) # train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', max_queue=4) # valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=4) # summary_dic = summary_generation(valid_file_dir) print("Done") # Model Save Part # print("Setting up Saver...") saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(initial_logs_dir) print("Done") # Session Part # sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess_config.gpu_options.allow_growth = True sess = tf.Session(config=sess_config) if mode is 'train': train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', sess.graph, max_queue=2) valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=2) if ckpt and ckpt.model_checkpoint_path: # model restore print("Model restored...") if mode is 'train': saver.restore(sess, ckpt.model_checkpoint_path) else: saver.restore(sess, initial_logs_dir+ckpt_name) # print(initial_logs_dir) # saver.save(sess, initial_logs_dir + "/model_bDNN.ckpt", 0) # model save print("Done") else: sess.run(tf.global_variables_initializer()) # if the checkpoint doesn't exist, do initialization if mode is 'train': train_data_set = dr.DataReader(input_dir, output_dir, norm_dir, w=w, u=u, name="train") # training data reader initialization # train_data_set = dr.DataReader(input_dir, output_dir, norm_dir, w=w, u=u, name="train") # training data reader initialization if mode is 'train': for itr in range(max_epoch): train_inputs, train_labels = train_data_set.next_batch(batch_size) feed_dict = {m_train.inputs: train_inputs, m_train.labels: train_labels, m_train.keep_probability: dropout_rate} sess.run(m_train.train_op, feed_dict=feed_dict) if itr % 10 == 0 and itr >= 0: train_cost, logits = sess.run([m_train.cost, m_train.logits], feed_dict=feed_dict) result = bdnn_prediction(batch_size, logits, threshold=th) raw_indx = int(np.floor(train_labels.shape[1] / 2)) raw_labels = train_labels[:, raw_indx] raw_labels = raw_labels.reshape((-1, 1)) train_accuracy = np.equal(result, raw_labels) train_accuracy = train_accuracy.astype(int) train_accuracy = np.sum(train_accuracy) / batch_size # change to mean... print("Step: %d, train_cost: %.4f, train_accuracy=%4.4f" % (itr, train_cost, train_accuracy*100)) train_cost_summary_str = sess.run(cost_summary_op, feed_dict={summary_ph: train_cost}) train_accuracy_summary_str = sess.run(accuracy_summary_op, feed_dict={summary_ph: train_accuracy}) train_summary_writer.add_summary(train_cost_summary_str, itr) # write the train phase summary to event files train_summary_writer.add_summary(train_accuracy_summary_str, itr) # if train_data_set.eof_checker(): if itr % 50 == 0 and itr > 0: saver.save(sess, logs_dir + "/model.ckpt", itr) # model save print('validation start!') valid_accuracy, valid_cost = \ utils.do_validation(m_valid, sess, valid_file_dir, norm_dir, type='bDNN') print("valid_cost: %.4f, valid_accuracy=%4.4f" % (valid_cost, valid_accuracy * 100)) valid_cost_summary_str = sess.run(cost_summary_op, feed_dict={summary_ph: valid_cost}) valid_accuracy_summary_str = sess.run(accuracy_summary_op, feed_dict={summary_ph: valid_accuracy}) valid_summary_writer.add_summary(valid_cost_summary_str, itr) # write the train phase summary to event files valid_summary_writer.add_summary(valid_accuracy_summary_str, itr) # full_evaluation(m_valid, sess, valid_batch_size, valid_file_dir, valid_summary_writer, summary_dic, itr) # train_data_set.reader_initialize() # print('Train data reader was initialized!') # initialize eof flag & num_file & start index elif mode is 'test': # full_evaluation(m_valid, sess, valid_batch_size, test_file_dir, valid_summary_writer, summary_dic, 0) final_softout, final_label = utils.vad_test(m_valid, sess, valid_batch_size, test_file_dir, norm_dir, data_len, eval_type) if data_len is None: return final_softout, final_label else: return final_softout[0:data_len, :], final_label[0:data_len, :]
def main(prj_dir=None, model=None, mode=None): # Configuration Part # if mode is 'train': import path_setting as ps set_path = ps.PathSetting(prj_dir, model) logs_dir = initial_logs_dir = set_path.logs_dir input_dir = set_path.input_dir output_dir = set_path.output_dir norm_dir = set_path.norm_dir valid_file_dir = set_path.valid_file_dir sys.path.insert(0, prj_dir+'/configure/LSTM') import config as cg global seq_size, batch_num seq_size = cg.seq_len batch_num = cg.num_batches global learning_rate, dropout_rate, max_epoch, batch_size, valid_batch_size learning_rate = cg.lr dropout_rate = cg.dropout_rate max_epoch = cg.max_epoch batch_size = valid_batch_size = batch_num*seq_size global target_delay target_delay = cg.target_delay global lstm_cell_size, num_layers lstm_cell_size = cg.cell_size num_layers = cg.num_layers # Graph Part # print("Graph initialization...") with tf.device(device): with tf.variable_scope("model", reuse=None): m_train = Model(is_training=True) with tf.variable_scope("model", reuse=True): m_valid = Model(is_training=False) print("Done") # Summary Part # print("Setting up summary op...") summary_ph = tf.placeholder(dtype=tf.float32) with tf.variable_scope("Training_procedure"): cost_summary_op = tf.summary.scalar("cost", summary_ph) accuracy_summary_op = tf.summary.scalar("accuracy", summary_ph) # train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', max_queue=4) # valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=4) # summary_dic = summary_generation(valid_file_dir) print("Done") # Model Save Part # print("Setting up Saver...") saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(logs_dir + '/LSTM') print("Done") # Session Part # sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess_config.gpu_options.allow_growth = True sess = tf.Session(config=sess_config) if mode is 'train': train_summary_writer = tf.summary.FileWriter(logs_dir + '/train/', sess.graph, max_queue=2) valid_summary_writer = tf.summary.FileWriter(logs_dir + '/valid/', max_queue=2) if ckpt and ckpt.model_checkpoint_path: # model restore print("Model restored...") if mode is 'train': saver.restore(sess, ckpt.model_checkpoint_path) else: saver.restore(sess, initial_logs_dir+ckpt_name) # saver.restore(sess, logs_dir+ckpt_name) saver.save(sess, logs_dir + "/model_LSTM.ckpt", 0) # model save print("Done") else: sess.run(tf.global_variables_initializer()) # if the checkpoint doesn't exist, do initialization if mode is 'train': train_data_set = dr.DataReader(input_dir, output_dir, norm_dir, target_delay=target_delay, u=u, name="train") # training data reader initialization # train_data_set = dr.DataReader(input_dir, output_dir, norm_dir, target_delay=target_delay, u=u, name="train") # training data reader initialization if mode is 'train': for itr in range(max_epoch): train_inputs, train_labels = train_data_set.next_batch(batch_size) one_hot_labels = train_labels.reshape((-1, 1)) one_hot_labels = dense_to_one_hot(one_hot_labels, num_classes=2) feed_dict = {m_train.inputs: train_inputs, m_train.labels: one_hot_labels, m_train.keep_probability: dropout_rate} sess.run(m_train.train_op, feed_dict=feed_dict) if itr % 10 == 0 and itr >= 0: train_cost, train_accuracy = sess.run([m_train.cost, m_train.accuracy], feed_dict=feed_dict) print("Step: %d, train_cost: %.4f, train_accuracy=%4.4f" % (itr, train_cost, train_accuracy*100)) train_cost_summary_str = sess.run(cost_summary_op, feed_dict={summary_ph: train_cost}) train_accuracy_summary_str = sess.run(accuracy_summary_op, feed_dict={summary_ph: train_accuracy}) train_summary_writer.add_summary(train_cost_summary_str, itr) # write the train phase summary to event files train_summary_writer.add_summary(train_accuracy_summary_str, itr) # if train_data_set.eof_checker(): if itr % 50 == 0 and itr > 0: saver.save(sess, logs_dir + "/model.ckpt", itr) # model save print('validation start!') valid_accuracy, valid_cost = \ utils.do_validation(m_valid, sess, valid_file_dir, norm_dir, type='LSTM') print("valid_cost: %.4f, valid_accuracy=%4.4f" % (valid_cost, valid_accuracy * 100)) valid_cost_summary_str = sess.run(cost_summary_op, feed_dict={summary_ph: valid_cost}) valid_accuracy_summary_str = sess.run(accuracy_summary_op, feed_dict={summary_ph: valid_accuracy}) valid_summary_writer.add_summary(valid_cost_summary_str, itr) # write the train phase summary to event files valid_summary_writer.add_summary(valid_accuracy_summary_str, itr) # mean_acc = full_evaluation(m_valid, sess, valid_batch_size, valid_file_dir, # valid_summary_writer, summary_dic, itr) # if mean_acc > 0.968: # print('finish!!') # break # train_data_set.reader_initialize() # print('Train data reader was initialized!') # initialize eof flag & num_file & start index elif mode is 'test': final_softout, final_label = utils.vad_test3(m_valid, sess, valid_batch_size, test_file_dir, norm_dir, data_len, eval_type) if data_len is None: return final_softout, final_label else: return final_softout[0:data_len, :], final_label[0:data_len, :]