def predict(path): im = io.imread(path, as_grey=True) * 255 width = im.shape[1] sequence_length = [len2chars[width]] * 32 im = np.expand_dims(im, 0) im = np.expand_dims(im, 3) im = np.repeat(im, 32, axis=0) im = tf.constant(im, dtype=tf.float32) sequence_length = tf.constant(sequence_length) features = model.convnet_layers(im, False) rnn_logits = model.rnn_layers(features, sequence_length, num_chars) predictions, _ = tf.nn.ctc_beam_search_decoder(rnn_logits, sequence_length, beam_width=128, top_paths=1, merge_repeated=True) hypothesis = tf.cast(predictions[0], tf.int32) label = tf.sparse_tensor_to_dense(hypothesis) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) with tf.Session() as sess: sess.run(init_op) restore_model(sess) # Get latest checkpoint label = sess.run(label) label = list(label[0]) result = [] for i in label: result.append(idx2vocab[i]) result = ''.join(result) return result
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 = _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 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]} ) print(mjsynth.get_string(output.values))
def build_model(args): #image,width,label = _get_input() image = tf.placeholder(tf.float32, [args.batch_size, 32, 426, 4]) x_tensor = image #tf.expand_dims(image,-1) print(x_tensor) width = tf.constant(value=426, shape=[args.batch_size]) label = tf.sparse_placeholder(tf.int32) if args.mode == 'train': mode = learn.ModeKeys.TRAIN # 'Configure' training mode for dropout layers else: mode = learn.ModeKeys.INFER features, seq_len = model.convnet_layers(x_tensor, width, mode) logits = model.rnn_layers(features, seq_len, num_classes=102) train_op, cost = _get_training(args, logits, label, seq_len) decoded, log_prob = tf.nn.ctc_beam_search_decoder(inputs=logits, sequence_length=seq_len, beam_width=5, top_paths=1, merge_repeated=False) print(features) print(logits) print(decoded) return train_op, cost, image, label, width, decoded, None
def build_lstm(): graph = tf.Graph() with 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() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess = tf.Session(config=session_config, graph=graph) sess.run(init_op) model_dir = "lstm/model/model.ckpt-81950" saver.restore(sess, model_dir) return image, width, sess, prediction
def main2(): with tf.Graph().as_default(): session_config = _get_session_config() 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) # Get latest checkpoint for imgs, widths, labels in get_single_image(): # 恢复模型数据 # hypothesis = sess.run(step_ops,feed_dict={imgsPh:imgs,widthsPh:widths}) # print hypothesis features, sequence_length = model.convnet_layers( imgs, widths, mode) logits = model.rnn_layers(features, sequence_length, mjsynth.num_classes()) hypothesis = get_testing(logits, sequence_length) restore_model = _get_init_trained() restore_model(sess, _get_checkpoint()) hypo = sess.run([hypothesis]) print hypo, labels
def build_model2(args): import gen_lineocr lineOCR = gen_lineocr.LineOCRGenerator(h_shape=64, limit_per_char=500) lineOCR.load_db(data_dir='../hwocr/data/kanji_hira.pkl') lineOCR.load_db(data_dir='../hwocr/data/full_katakana_quote.nice.pkl') lineOCR.finalize_all_db() lineOCR.equip_txt(max_len=1000, is_random=True, top_freq=False) num_classes = len(lineOCR.label2char) print(lineOCR.label2char) print(lineOCR.char2label) #image,width,label = _get_input() image = tf.placeholder(tf.float32, [args.batch_size, lineOCR.h_shape, None, 1]) x_tensor = image #tf.expand_dims(image,-1) print(x_tensor) width = tf.placeholder(tf.int32, shape=[args.batch_size]) label = tf.sparse_placeholder(tf.int32) if args.mode == 'train': mode = learn.ModeKeys.TRAIN # 'Configure' training mode for dropout layers else: mode = learn.ModeKeys.INFER features, seq_len = model.convnet_layers(x_tensor, width, mode) logits = model.rnn_layers(features, seq_len, num_classes=num_classes) train_op, cost = _get_training(args, logits, label, seq_len) decoded, log_prob = tf.nn.ctc_beam_search_decoder(inputs=logits, sequence_length=seq_len, beam_width=5, top_paths=1, merge_repeated=False) print(features) print(logits) print(decoded) return train_op, cost, image, label, width, decoded, lineOCR
def main(argv=None): with tf.Graph().as_default(): image = _get_input() with tf.device(FLAGS.device): pred_img = tf.placeholder(tf.float32, [1, 64, None, 1]) img_width = tf.placeholder(tf.int32) features, sequence_length = model.convnet_layers( pred_img, img_width, mode) logits = model.rnn_layers(features, sequence_length, textsyn.num_classes()) recognition_hypothesis = _get_predict(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()) step_ops = [recognition_hypothesis, logits, sequence_length] with tf.Session(config=session_config) as sess: sess.run(init_op) image_data = sess.run(image) image_data = image_data.reshape((64, 256)) restore_model(sess, _get_checkpoint()) # image_data = plt.imread(FLAGS.file_path) # image_data = rgb2gray(image_data) # img_height = image_data.shape[0] # img_width = image_data.shape[1] # new_width = int( (32./img_height) * img_width) # image_data.resize((32,int((32.0/img_height)*img_width))) # data_width = image_data.shape[1] print image_data.shape plt.imshow(image_data, cmap=plt.cm.gray) image_data = image_data.reshape((1, 64, 256, 1)) pred_output, l, sl = sess.run(step_ops, feed_dict={ pred_img: image_data, img_width: 256 }) print pred_output text_output = [ch_charset[c].encode('utf-8') for c in pred_output] text = "" for i in text_output: text += i.encode('utf-8') print text print sl #print l for j in l: print np.argmax(j), #print j, plt.title(text) plt.show()
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) # features,sequence_length = zf_mod_denseNet2.Dense_net( 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 # for line in sys.stdin: #命令行 # # Eliminate any trailing newline from filename # image_data = _get_image(line.rstrip()) #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)) # base_dir = "I:\ICPR_task1_test_20180514\icpr_mtwi_task1\\testimage" base_dir = "..\\data\\test_image" bld = os.listdir(base_dir) # base_dir = "I:\ICPR_task1_test_20180514\icpr_mtwi_task1\9999" # tem_result = "" for i in bld: # name = "line_"+str(i)+".jpg" name = i image_data = _get_image(os.path.join(base_dir,name)) if image_data.shape[0] > image_data.shape[1]: image_data = image_data.reshape(image_data.shape[1], image_data.shape[0], 1) try: [output] = sess.run(prediction, {image: image_data, width: image_data.shape[1]}) tem_result = name + " "+_get_string(output.values) + "\n" except: f1 = open("..\\data\\val\\error.txt","a",encoding="utf-8") tem_result = name + " 10" + "\n" f1.write(tem_result) f1.close() f = open("..\\data\\val\\result.txt", "a", encoding='utf-8') f.write(tem_result) f.close() print(name)
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) saver = tf.train.Saver() 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 saver.restore(sess, "../data/model/model.ckpt-81950") 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(): global_step = tf.train.get_or_create_global_step() input_stream = _get_input_stream() # Grab the next batch of data from input_stream image, width, label, _, _ = input_stream.get_next() with tf.device(FLAGS.train_device): features,sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers( features, sequence_length, dynmj.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): 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 _get_image_info(features, mode): """Calculates the logits and sequence length""" image = features['image'] width = features['width'] conv_features, sequence_length = model.convnet_layers(image, width, mode) logits = model.rnn_layers(conv_features, sequence_length, charset.num_classes()) return logits, sequence_length
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]") 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 = _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): 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): with tf.Graph().as_default(): input_stream = _get_input_stream() # Get the next batch image, width, label, length, _ = input_stream.get_next() with tf.device(FLAGS.device): features, sequence_length = model.convnet_layers( image, width, mode) logits = model.rnn_layers(features, sequence_length, dynmj.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) summary_writer.add_graph(sess.graph) try: while True: restore_model(sess, _get_checkpoint()) step_vals = sess.run(step_ops) print step_vals summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step_vals[0]) except tf.errors.OutOfRangeError: print('Done')
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( ): # формальная (если граф в программе всего один) конструкция для объединения операция в отдельный граф, 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)
def main(argv=None): global_steps = tf.Variable(0) image = tf.placeholder(shape=[None, None, None, 1], dtype=tf.float32) label = tf.sparse_placeholder(dtype=tf.int32) sequence_length = tf.placeholder(dtype=tf.int32) train_mode = tf.placeholder(dtype=tf.bool) ''' train ''' features = model.convnet_layers(image, train_mode) logits = model.rnn_layers(features, sequence_length, num_chars) train_op = _get_training(logits, label, sequence_length, global_steps) predictions, _ = tf.nn.ctc_beam_search_decoder(logits, sequence_length, beam_width=128, top_paths=1, merge_repeated=True) res = tf.cast(predictions[0], tf.int32) res = tf.sparse_tensor_to_dense(res) session_config = _get_session_config() summary_op = tf.summary.merge_all() writer = tf.summary.FileWriter('./logs/') saver = tf.train.Saver() with tf.Session(config=session_config) as sess: sess.run(tf.local_variables_initializer()) sess.run(tf.global_variables_initializer()) step = sess.run(global_steps) batch_data_train = {} batch_data_val = {} for i in range(4, 9): batch_data_train[i] = _read_data('txt/train_' + str(i) + '.txt', True) batch_data_val[i] = _read_data('txt/val_' + str(i) + '.txt', False) while step < FLAGS.max_num_steps: for l in range(4, 9): try: batch_data_train1 = sess.run(batch_data_train[l]) train_image1 = batch_data_train1[0] train_label1 = batch_data_train1[1] if train_label1.shape[0] != 32: continue train_label2 = _dense2sparse(train_label1) sequence_length1 = np.ones( (FLAGS.batch_size)) * len(train_label1[0]) step_loss, summary = sess.run( [train_op, summary_op], feed_dict={ image: train_image1, label: train_label2, sequence_length: sequence_length1, train_mode: True }) step = sess.run(global_steps) print("step %d loss is: %f" % (step, step_loss)) writer.add_summary(summary, step) except tf.errors.OutOfRangeError: saver.save(sess, 'models/im2str') print("data is finished!") break if step % 1000 == 0: batch_data_val1 = sess.run(batch_data_val[l]) val_image1 = batch_data_val1[0] val_label1 = batch_data_val1[1] if val_label1.shape[0] != 32: continue sequence_length1 = np.ones( (FLAGS.batch_size)) * len(train_label1[0]) res1 = sess.run(res, feed_dict={ image: val_image1, sequence_length: sequence_length1, train_mode: True }) sq_pred_labels = [] sq_true_labels = [] for i in range(res1.shape[0]): sq_label = [] for j in range(res1.shape[1]): sq_char = idx2vocab[res1[i][j]] sq_label.append(sq_char) sq_true_labels.append(val_label1[i].decode()) sq_pred_labels.append(''.join(sq_label)) print("#################################") print('True labels', sq_true_labels) print('Pred labels', sq_pred_labels) print("#################################") saver.save(sess, 'models/im2str_' + str(step)) print('%d step model saved!' % step) print("end!")
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)