def main(argv=None): with tf.Graph().as_default(): image, width, label, length = _get_input() with tf.device(FLAGS.device): features, sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) loss, label_error, sequence_error = _get_testing( logits, sequence_length, label, length) global_step = tf.contrib.framework.get_or_create_global_step() session_config = _get_session_config() restore_model = _get_init_trained() summary_op = tf.summary.merge_all() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter( os.path.join(FLAGS.model, FLAGS.output)) step_ops = [global_step, loss, label_error, sequence_error] with tf.Session(config=session_config) as sess: sess.run(init_op) coord = tf.train.Coordinator() # Launch reader threads threads = tf.train.start_queue_runners(sess=sess, coord=coord) summary_writer.add_graph(sess.graph) try: while True: restore_model(sess, _get_checkpoint()) # Get latest checkpoint if not coord.should_stop(): step_vals = sess.run(step_ops) print step_vals summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step_vals[0]) else: break time.sleep(FLAGS.test_interval_secs) except tf.errors.OutOfRangeError: print('Done') finally: coord.request_stop() coord.join(threads)
def main(argv=None): with tf.Graph().as_default(): with tf.device(FLAGS.device): image, width, label, length, text, filename = _get_input() features, sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) test_ops = _get_testing(logits, sequence_length, label, length) [label_error,sequence_error,update_metrics,metrics,predictions] = \ test_ops session_config = _get_session_config() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) with tf.Session(config=session_config) as sess: sess.run(init_op) coord = tf.train.Coordinator() # Launch reader threads threads = tf.train.start_queue_runners(sess=sess, coord=coord) _restore_model(sess) # Get latest checkpoint try: while not coord.should_stop(): if FLAGS.verbose: [upd, fnames, gt_txts, pred] = sess.run( [update_metrics, filename, text, predictions]) strings = mjsynth.get_strings(pred[0]) for fname, gt_txt, string in zip( fnames, gt_txts, strings): print string, gt_txt, fname else: sess.run(update_metrics) except tf.errors.OutOfRangeError: # Indicates that the single epoch is complete. pass finally: coord.request_stop() metrics.extend([label_error, sequence_error]) final_vals = sess.run(metrics) print final_vals coord.join(threads)
def main(argv=None): with tf.Graph().as_default(): image, width = _get_input() # Placeholder tensors proc_image = _preprocess_image(image) proc_image = tf.reshape(proc_image, [1, 32, -1, 1]) # Make first dim batch with tf.device(FLAGS.device): features, sequence_length = model.convnet_layers( proc_image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) prediction = _get_output(logits, sequence_length) session_config = _get_session_config() restore_model = _get_init_trained() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) with tf.Session(config=session_config) as sess: sess.run(init_op) restore_model(sess, _get_checkpoint()) # Get latest checkpoint # Iterate over filenames given on lines of standard input print("[label , predicion , ratio]") f = open( "/home/chun/cnn_lstm_ctc_ocr-master/data/val_final/val_logs.txt", "a") localtime = time.asctime(time.localtime(time.time())) f.write(localtime + '\n[label , predicion , ratio]\n') for line in sys.stdin: # Eliminate any trailing newline from filename image_data = _get_image(line.rstrip()) # Get prediction for single image (isa SparseTensorValue) [output] = sess.run(prediction, { image: image_data, width: image_data.shape[1] }) name = str(line).split(".jpg")[0].split("-")[0] seq = difflib.SequenceMatcher(None, name, _get_string(output.values)) ratio = seq.ratio() output = name, _get_string(output.values), round(ratio, 2) print(output) # Save logs f.write(str(output) + '\n') f.write('===========================\n') f.close()
def main(argv=None): with tf.Graph().as_default(): image,width,label,length = _get_input() with tf.device(FLAGS.device): features,sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers( features, sequence_length, mjsynth.num_classes() ) loss,label_error,sequence_error = _get_testing( logits,sequence_length,label,length) global_step = tf.contrib.framework.get_or_create_global_step() session_config = _get_session_config() restore_model = _get_init_trained() summary_op = tf.summary.merge_all() init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter( os.path.join(FLAGS.model, FLAGS.output) ) step_ops = [global_step, loss, label_error, sequence_error] with tf.Session(config=session_config) as sess: sess.run(init_op) coord = tf.train.Coordinator() # Launch reader threads threads = tf.train.start_queue_runners(sess=sess,coord=coord) summary_writer.add_graph(sess.graph) try: while True: restore_model(sess, _get_checkpoint()) # Get latest checkpoint if not coord.should_stop(): step_vals = sess.run(step_ops) print step_vals summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str,step_vals[0]) else: break time.sleep(FLAGS.test_interval_secs) except tf.errors.OutOfRangeError: print('Done') finally: coord.request_stop() coord.join(threads)
def main(argv=None): with tf.Graph().as_default(): image, width = _get_input() # Placeholder tensors proc_image = _preprocess_image(image) proc_image = tf.reshape(proc_image, [1, 32, -1, 1]) # Make first dim batch with tf.device(FLAGS.device): features, sequence_length = model.convnet_layers( proc_image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) prediction = _get_output(logits, sequence_length) session_config = _get_session_config() restore_model = _get_init_trained() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) with tf.Session(config=session_config) as sess: sess.run(init_op) restore_model(sess, _get_checkpoint()) # Get latest checkpoint # Iterate over filenames given on lines of standard input print("[label , predicion , ratio]") for line in sys.stdin: # Eliminate any trailing newline from filename ratio = [] k = 0 k = _get_image(k, line.rstrip()) name = str(line).split(".jpg")[0].split("-")[0] for i in range(k): [output] = sess.run(prediction, { image: all_image[i][1], width: all_image[i][1].shape[1] }) seq = difflib.SequenceMatcher(None, name, _get_string(output.values)) ratio.append((i, seq.ratio(), _get_string(output.values))) maxprob = max(ratio, key=lambda x: x[1]) #print(sorted(ratio,key=lambda x:x[1],reverse=True)) print(name, maxprob[2], round(maxprob[1], 2)) del all_image[:]
def main(argv=None): with tf.Graph().as_default(): global_step = tf.train.get_or_create_global_step() image, width, label , text, filename= _get_input() alldata = [image, width, label , text, filename] with tf.device(FLAGS.train_device): features,sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers( features, sequence_length, mjsynth.num_classes() ) train_op = _get_training(logits,label,sequence_length) session_config = _get_session_config() summary_op = tf.summary.merge_all() init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer()) #Structure to create or gather pieces commonly needed to train a model init_scaffold = tf.train.Scaffold( init_op=init_op, init_fn=_get_init_pretrained() ) #saves summaries every N steps summary_hook = tf.train.SummarySaverHook( output_dir=FLAGS.output, save_secs=10, summary_op=summary_op ) #saves checkpoint every N steps saver_hook = tf.train.CheckpointSaverHook( checkpoint_dir=FLAGS.output, save_secs=150 ) #Creates a MonitoredSession for training monitor = tf.train.MonitoredTrainingSession( checkpoint_dir=FLAGS.output, # Necessary to restore hooks=[summary_hook,saver_hook], config=session_config, scaffold=init_scaffold # Scaffold initializes session ) with monitor as sess: step = sess.run(global_step) while step < FLAGS.max_num_steps: if monitor.should_stop(): break [step_loss,step,alldata2]=sess.run([train_op, global_step,alldata])
def main(argv=None): with tf.Graph().as_default(): global_step = tf.train.get_or_create_global_step() image, width, label = _get_input() with tf.device(FLAGS.train_device): features, sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) train_op = _get_training(logits, label, sequence_length) session_config = _get_session_config() summary_op = tf.summary.merge_all() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) init_scaffold = tf.train.Scaffold(init_op=init_op, init_fn=_get_init_pretrained()) summary_hook = tf.train.SummarySaverHook(output_dir=FLAGS.output, save_secs=30, summary_op=summary_op) saver_hook = tf.train.CheckpointSaverHook(checkpoint_dir=FLAGS.output, save_secs=150) monitor = tf.train.MonitoredTrainingSession( checkpoint_dir=FLAGS.output, # Necessary to restore hooks=[summary_hook, saver_hook], config=session_config, scaffold=init_scaffold # Scaffold initializes session ) with monitor as sess: step = sess.run(global_step) while step < FLAGS.max_num_steps: if monitor.should_stop(): break [step_loss, step] = sess.run([train_op, global_step]) monitor.saver.save(sess, os.path.join(FLAGS.output, 'model.ckpt'), global_step=global_step)
def main(argv=None): bs = 3 with tf.Graph().as_default(): with tf.device('/device:CPU:0'): image, width = _get_input(bs) # Placeholder tensors proc_image = _preprocess_image(image) with tf.device('/device:CPU:0'): features, sequence_length = model.convnet_layers( proc_image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) with tf.device('/device:CPU:0'): predictions = _get_output(logits, sequence_length) session_config = _get_session_config() restore_model = _get_init_trained() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) with tf.Session(config=session_config) as sess: sess.run(init_op) restore_model(sess, _get_checkpoint()) # Get latest checkpoint print image, width print predictions tt = time() sortedlist = sorted(os.listdir(FLAGS.imgsdir), key=lambda x: (len(x), x)) for filename in sortedlist: if filename[-3:].upper() != 'JPG': continue line = os.path.join(FLAGS.imgsdir, filename) print line # Eliminate any trailing newline from filename image_data = _get_image(line.rstrip()) image_data = np.array([image_data] * bs) w = image_data.shape[2] ws = np.array([w] * bs) # Get prediction for single image (isa SparseTensorValue) p = sess.run(predictions, {image: image_data, width: ws}) print p[0].shape print p[0]
def main(argv=None): file_path = "b.png" with tf.Graph().as_default(): image, width = _get_input() # Placeholder tensors proc_image = _preprocess_image(image) proc_image = tf.reshape(proc_image, [1, 32, -1, 1]) # Make first dim batch with tf.device(FLAGS.device): features, sequence_length = model.convnet_layers( proc_image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) prediction = _get_output(logits, sequence_length) saver = tf.train.Saver() session_config = _get_session_config() #restore_model = _get_init_trained() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) with tf.Session(config=session_config) as sess: sess.run(init_op) model_dir = "../data/model/model.ckpt-81950" saver.restore(sess, model_dir) #restore_model(sess, _get_checkpoint()) # Get latest checkpoint # Iterate over filenames given on lines of standard input # Eliminate any trailing newline from filename image_data = _get_image(file_path) image_data = align_image(image_data) #Get prediction for single image (isa SparseTensorValue) [output] = sess.run(prediction, { image: image_data, width: image_data.shape[1] }) print(_get_string(output.values))
def main(argv=None): with tf.Graph().as_default(): global_step = tf.contrib.framework.get_or_create_global_step() image, width, label = _get_input() with tf.device(FLAGS.train_device): features, sequence_length = model.convnet_layers( image, width, mode) # features,sequence_length = zf_mod_denseNet2.Dense_net( image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) with tf.variable_scope(tf.get_variable_scope(), reuse=False): train_op = _get_training(logits, label, sequence_length) session_config = _get_session_config() summary_op = tf.summary.merge_all() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sv = tf.train.Supervisor( logdir=FLAGS.output, init_op=init_op, summary_op=summary_op, save_summaries_secs=600, #30 init_fn=_get_init_pretrained(), save_model_secs=600) #150 with sv.managed_session(config=session_config) as sess: step = sess.run(global_step) while step < FLAGS.max_num_steps: if sv.should_stop(): break [step_loss, step] = sess.run([train_op, global_step]) # if step%1000==0: # print(step_loss) sv.saver.save(sess, os.path.join(FLAGS.output, 'model.ckpt'), global_step=global_step)
def _get_output(rnn_logits,sequence_length): """Create ops for validation predictions: Results of CTC beacm search decoding """ with tf.name_scope("test"): if FLAGS.lexicon: dict_tensor = _get_dictionary_tensor(FLAGS.lexicon, mjsynth.out_charset) predictions,_ = tf.nn.ctc_beam_search_decoder_trie(rnn_logits, sequence_length, alphabet_size=mjsynth.num_classes() , dictionary=dict_tensor, beam_width=128, top_paths=1, merge_repeated=True) else: predictions,_ = tf.nn.ctc_beam_search_decoder(rnn_logits, sequence_length, beam_width=128, top_paths=1, merge_repeated=True) return predictions
def main(argv=None): with tf.Graph().as_default(): global_step = tf.contrib.framework.get_or_create_global_step() image,width,label = _get_input() with tf.device(FLAGS.train_device): features,sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers( features, sequence_length, mjsynth.num_classes() ) train_op = _get_training(logits,label,sequence_length) session_config,gpu_config = _get_session_config() summary_op = tf.summary.merge_all() init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer()) ## 这里使用分布式代码设计,其实可以不需要的. sv = tf.train.Supervisor( logdir=FLAGS.output, init_op=init_op, summary_op=summary_op, save_summaries_secs=30, init_fn=_get_init_pretrained(), save_model_secs=150) with sv.managed_session(config=session_config) as sess: step = sess.run(global_step) while step < FLAGS.max_num_steps: if sv.should_stop(): break [step_loss,step]=sess.run([train_op,global_step]) if step_loss < 1e-3: print 'loss %s ,===> to end'%step_loss break # 增加验证代码 sv.saver.save( sess, os.path.join(FLAGS.output,'model.ckpt'), global_step=global_step)
def main(argv=None): with tf.Graph().as_default(): global_step = tf.contrib.framework.get_or_create_global_step( ) # 得到目前模型训练到达全局步数 image, width, label = _get_input() with tf.device(FLAGS.train_device): features, sequence_length = model.convnet_layers( image, width, mode) # 得到feature maps和特征向量序列长度 logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) # 得到每一帧xt对应的标签yt train_op = _get_training(logits, label, sequence_length) # 得到RNN网络的训练op节点 session_config = _get_session_config() summary_op = tf.summary.merge_all() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # 得到包含几个操作的op节点 sv = tf.train.Supervisor(logdir=FLAGS.output, init_op=init_op, summary_op=summary_op, save_summaries_secs=30, init_fn=_get_init_pretrained(), save_model_secs=150) with sv.managed_session(config=session_config) as sess: step = sess.run(global_step) while step < FLAGS.max_num_steps: if sv.should_stop(): break [step_loss, step] = sess.run([train_op, global_step]) sv.saver.save(sess, os.path.join(FLAGS.output, 'model.ckpt'), global_step=global_step)
def main(): with tf.Graph().as_default(): # image, width, label, length = _get_input() # 直接读取文件内容 imgs, widths = get_single_image() imgsPh = tf.placeholder(tf.float32, [1, 32, None, 1], name='images') ##不定长度 widthsPh = tf.placeholder(tf.float32, [1]) features, sequence_length = model.convnet_layers( imgsPh, widthsPh, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) hypothesis = get_testing(logits, sequence_length) session_config = _get_session_config() restore_model = _get_init_trained() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # TODO 最好将输出的结果summary结果中 step_ops = [hypothesis] with tf.Session(config=session_config) as sess: sess.run(init_op) # restore_model(sess, _get_checkpoint()) # Get latest checkpoint # for imgs ,widths,labels in get_single_image(): # 恢复模型数据 for i in range(5): # hypothesis = sess.run(step_ops,feed_dict={imgsPh:imgs,widthsPh:widths}) # print hypothesis imgs, widths = sess.run([imgs, widths]) print widths
def main(argv=None): with tf.Graph().as_default(): global_step = tf.contrib.framework.get_or_create_global_step() image,width,label = _get_input() with tf.device(FLAGS.train_device): features,sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers( features, sequence_length, mjsynth.num_classes() ) train_op = _get_training(logits,label,sequence_length) session_config = _get_session_config() summary_op = tf.summary.merge_all() init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer()) sv = tf.train.Supervisor( logdir=FLAGS.output, init_op=init_op, summary_op=summary_op, save_summaries_secs=30, init_fn=_get_init_pretrained(), save_model_secs=150) with sv.managed_session(config=session_config) as sess: step = sess.run(global_step) while step < FLAGS.max_num_steps: if sv.should_stop(): break [step_loss,step]=sess.run([train_op,global_step]) sv.saver.save( sess, os.path.join(FLAGS.output,'model.ckpt'), global_step=global_step)
def main(argv=None): with tf.Graph().as_default(): image, width, label, length, text, filename, number_of_images = _get_input() # извлечение выборки изображений для теста with tf.device(FLAGS.device): features,sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers( features, sequence_length, mjsynth.num_classes() ) loss,label_error,sequence_error, predict, prob = _get_testing( logits,sequence_length,label,length) global_step = tf.contrib.framework.get_or_create_global_step() session_config = _get_session_config() restore_model = _get_init_trained() summary_op = tf.summary.merge_all() init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter( os.path.join(FLAGS.model, FLAGS.output) ) step_ops = [global_step, loss, label_error, sequence_error, tf.sparse_tensor_to_dense(label), tf.sparse_tensor_to_dense(predict), text, filename, prob, logits] with tf.Session(config=session_config) as sess: sess.run(init_op) count_list = [] enlisted_images = 0 coord = tf.train.Coordinator() # Launch reader threads threads = tf.train.start_queue_runners(sess=sess,coord=coord) summary_writer.add_graph(sess.graph) loss_change = [] Levenshtein_change = [] accuracy_change = [] try: while True: restore_model(sess, _get_checkpoint()) # Get latest checkpoint if not coord.should_stop(): step_vals = sess.run(step_ops) # print(step_vals[7]) # вывод на экран батча # out_charset = "abcdefghijklmnopqrstuvwxyz0123456789./-%" # a = step_vals[-1] # a = np.reshape(a, (step_vals[-1].shape[0],40)) # max_a = 0 # max_a_j = 0 # sum_a = 0 # total_mult = 0 # total_add = 0 # with open('./result.txt', 'a') as f: # for q in range(step_vals[-1].shape[0]): # for j in range(40): # if a[q][j] >= 0: # sum_a += np.exp(a[q][j]) # if a[q][j] > max_a: # max_a = a[q][j] # max_a_j = j # if str(out_charset[max_a_j]) != '%' and sum_a > 0: # f.write(str(max_a) + ' = ' + str(out_charset[max_a_j]) +' (' + str(round(np.log(np.exp(max_a)/sum_a), 5)) +') ' + '\n') # total_mult = total_mult*np.log(np.exp(max_a)/sum_a) # total_add += np.log(np.exp(max_a)/sum_a) # max_a = 0 # max_a_j = 0 # sum_a = 0 # f.write(str(total_mult) + '\n') # f.write(str(total_add) + '\n') with open('./result.txt', 'a') as f: out_charset = "abcdefghijklmnopqrstuvwxyz0123456789./-" for pred in range(len(step_vals[5])): pred_txt = '' for symb in step_vals[5][pred].tolist(): pred_txt += str(out_charset[symb]) pred_txt_clear = '' stop_pass = False # в выходе модели пустые символы в конце стркои заполняются первым символом out_charset for symb in pred_txt[::-1]: if symb == out_charset[0] and stop_pass == False: pass else: pred_txt_clear = symb + pred_txt_clear stop_pass = True # в итоговом txt файле сохраняются только уникальные изображения # батчи формируются последовательно без повторений # однако последний тестовый батч может содержать часть изображений первого батча if step_vals[7][pred] not in count_list: enlisted_images += 1 f.write(pred_txt_clear + ' ') # прогноз модели f.write(step_vals[6][pred].decode('utf-8') + ' ') # ground truth f.write(str(step_vals[8][pred][0]) + ' ') # вероятности f.write(step_vals[7][pred].decode('utf-8') + '\n') # директории count_list.append(step_vals[7][pred]) # остановить тест, когда количество уникальных изображений равно или больше количества изображений в датасете if enlisted_images >= number_of_images: coord.request_stop() print(round(enlisted_images/number_of_images, 2)) # процент пройденного датасета loss_change.append(step_vals[1]) Levenshtein_change.append(step_vals[2]) accuracy_change.append(1-step_vals[3]) print('Batch done') # summary_str = sess.run(summary_op) # вызывает повторное извлечение батча, который не используется моделью # summary_writer.add_summary(summary_str,step_vals[0]) else: print('loss', np.mean(loss_change)) print('mean Levenshtein', np.mean(Levenshtein_change)) print('accuracy', np.mean(accuracy_change)) print('Test done') break time.sleep(FLAGS.test_interval_secs) except tf.errors.OutOfRangeError: print('Done') finally: coord.request_stop() coord.join(threads)
def main(argv=None): with tf.Graph().as_default(): image, width, label, length, filename = _get_input() with tf.device(FLAGS.device): features, sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) hypothesis = _get_testing(logits, sequence_length) global_step = tf.contrib.framework.get_or_create_global_step() session_config = _get_session_config() restore_model = _get_init_trained() summary_op = tf.summary.merge_all() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter( os.path.join(FLAGS.model, FLAGS.output)) step_ops = [global_step, hypothesis, filename] with tf.Session(config=session_config) as sess: sess.run(init_op) coord = tf.train.Coordinator() # Launch reader threads threads = tf.train.start_queue_runners(sess=sess, coord=coord) summary_writer.add_graph(sess.graph) try: while True: restore_model(sess, _get_checkpoint()) # Get latest checkpoint if not coord.should_stop(): step, vals, imgfiles = sess.run(step_ops) rightCount = 0 count = 0 rightset = set() wrongset = set() hyparr = arr2str(vals) for fname, pred in zip(imgfiles, hyparr): actual = extractValue(fname) count += 1 if actual == pred: if not actual in rightset: print 'R', fname, '--->', pred rightCount += 1 rightset.add(actual) else: if not actual in wrongset: print 'W', fname, '--->', pred wrongset.add(actual) print 'total count %d' % count, 'accury:%f' % ( len(rightset) * 1. / (len(rightset) + len(wrongset))) summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) ## 统计正确率 else: break time.sleep(FLAGS.test_interval_secs) except tf.errors.OutOfRangeError: print('Done') finally: coord.request_stop() coord.join(threads)
def main(argv=None): with tf.Graph().as_default( ): # формальная (если граф в программе всего один) конструкция для объединения операция в отдельный граф, https://stackoverflow.com/questions/39614938/why-do-we-need-tensorflow-tf-graph , https://danijar.com/what-is-a-tensorflow-session/ tf.set_random_seed( 1 ) # фиксация сида, на gpu в вычислениях писутствует случайная составляющся и результаты все равно могут немного отличаться global_step = tf.contrib.framework.get_or_create_global_step( ) # переменная для подсчета количество эпох image, width, label, length, text, filename, number_of_images = _get_input( ) # формирование выборки для обучения with tf.device(FLAGS.train_device): features, sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) train_op, label_error, sequence_error, predict, prob = _get_training( logits, label, sequence_length, length) session_config = _get_session_config() saver_reader = tf.train.Saver(max_to_keep=100) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) step_ops = [ global_step, train_op, label_error, sequence_error, tf.sparse_tensor_to_dense(label), tf.sparse_tensor_to_dense(predict), text, filename, prob ] try: loss_change = np.load('./train_loss.npy').item().get('loss_change') Levenshtein_change = np.load('./train_loss.npy').item().get( 'Levenshtein_change') accuracy_change = np.load('./train_loss.npy').item().get( 'accuracy_change') print('metrics and loss are loaded') except: loss_change = [] Levenshtein_change = [] accuracy_change = [] print('metrics and loss are created') with tf.Session(config=session_config) as sess: sess.run(init_op) coord = tf.train.Coordinator() # Launch reader threads threads = tf.train.start_queue_runners(sess=sess, coord=coord) # подгрузка весов if os.path.isdir(FLAGS.output): ckpt = tf.train.get_checkpoint_state(FLAGS.output) if ckpt and ckpt.model_checkpoint_path: ckpt_path = ckpt.model_checkpoint_path else: raise RuntimeError('No checkpoint file found') # init_fn = lambda sess, ckpt_path: saver_reader.restore(sess, ckpt_path) saver_reader.restore(sess, ckpt_path) step = sess.run(global_step) while step < FLAGS.max_num_steps: step_vals = sess.run(step_ops) step = step_vals[0] out_charset = 'abcdefghijklmnopqrstuvwxyz0123456789./-' for pred in range(len(step_vals[5])): pred_txt = '' for symb in step_vals[5][pred].tolist(): pred_txt += str(out_charset[symb]) pred_txt_clear = '' stop_pass = False # в выходе модели пустые символы в конце стркои заполняются первым символом out_charset for symb in pred_txt[::-1]: if symb == out_charset[0] and stop_pass == False: pass else: pred_txt_clear = symb + pred_txt_clear stop_pass = True # print(step_ops[7]) # вывод на экран собранного батча для обучения loss_change.append(step_vals[1]) Levenshtein_change.append(step_vals[2]) accuracy_change.append(1 - step_vals[3]) print(step_vals[1]) if step_vals[0] % FLAGS.save_and_print_frequency == 0: print( 'loss', np.mean(loss_change[-FLAGS.save_and_print_frequency:])) print( 'mean Levenshtein', np.mean(Levenshtein_change[-FLAGS. save_and_print_frequency:])) print( 'accuracy', np.mean( accuracy_change[-FLAGS.save_and_print_frequency:])) # сохранение лосса и других статистик np.save( './train_loss', { 'loss_change': loss_change, 'Levenshtein_change': Levenshtein_change, 'accuracy_change': accuracy_change }) saver_reader.save(sess, os.path.join(FLAGS.output, 'model.ckpt'), global_step=step) coord.join(threads)
def main(argv=None): with tf.Graph().as_default(): image, width, label, length = _get_input() with tf.device(FLAGS.device): features, sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) loss, label_error, sequence_error = _get_testing( logits, sequence_length, label, length) global_step = tf.train.get_or_create_global_step() session_config = _get_session_config() restore_model = _get_init_trained() summary_op = tf.summary.merge_all() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter( os.path.join(FLAGS.model, FLAGS.output)) step_ops = [global_step, loss, label_error, sequence_error] with tf.Session(config=session_config) as sess: sess.run(init_op) coord = tf.train.Coordinator() # Launch reader threads threads = tf.train.start_queue_runners(sess=sess, coord=coord) summary_writer.add_graph(sess.graph) try: #f = open("/home/chun/cnn_lstm_ctc_ocr-master/data/test_logs.txt","a") while True: restore_model(sess, _get_checkpoint()) # Get latest checkpoint localtime = time.asctime(time.localtime(time.time())) if not coord.should_stop(): step_vals = sess.run(step_ops) print( "[global_step, loss, label_acc, sequence_acc]:%d, %.4f, %.4f, %.4f" % (step_vals[0], step_vals[1], 1.0 - step_vals[2], 1.0 - step_vals[3])) #f.write("%s, %d , %.4f , %.4f\n" % (localtime[4:19],step_vals[0],1.0-step_vals[2],1.0-step_vals[3])) summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step_vals[0]) else: break time.sleep(FLAGS.test_interval_secs) #f.close() except tf.errors.OutOfRangeError: print('Done') finally: coord.request_stop() coord.join(threads)