def main(img_dir, lpr_model, num_channels): files = get_images(img_dir) img = cv2.imread(files[1]) img = resize_image(img) images = img[np.newaxis, :] images = np.transpose(images, axes=[0, 2, 1, 3]) global_step = tf.Variable(0, trainable=False) logits, inputs, targets, seq_len = get_train_model(num_channels, label_len, 1, img_size) logits = tf.transpose(logits, (1, 0, 2)) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) saver = tf.train.Saver() config = tf.ConfigProto(device_count = {'GPU': 1}) with tf.Session(config=config) as session: session.run(tf.global_variables_initializer()) saver.restore(session, lpr_model) print("{} loaded!!!".format(lpr_model)) #test_inputs, test_targets, test_seq_len = test_gen.next_batch() test_feed = {inputs: images, seq_len: 24} #st = time.time() #dd = session.run(decoded[0], test_feed) lg = session.run(logits, test_feed)
def batch_eval(img_dir, label_file, out_dir, model_ckpt, num_channels): global_step = tf.Variable(0, trainable=False) logits, inputs, targets, seq_len = get_train_model(num_channels, label_len, BATCH_SIZE, img_size, False, False) logits = tf.transpose(logits, (1, 0, 2)) # tragets是一个稀疏矩阵 #decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) # tragets是一个稀疏矩阵 loss = tf.nn.ctc_loss(labels=targets, inputs=logits, sequence_length=seq_len) cost = tf.reduce_mean(loss) #decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False, beam_width=100, top_paths=3) score = tf.subtract(log_prob[:, 0], log_prob[:, 1], name='score_computation') acc = tf.reduce_mean( tf.edit_distance(tf.cast(decoded[0], tf.int32), targets)) init = tf.global_variables_initializer() with tf.Session() as session: session.run(init) saver = tf.train.Saver(tf.global_variables(), max_to_keep=100) saver.restore(session, model_ckpt) test_gen = TextImageGeneratorBM(img_dir=img_dir, label_file=label_file, batch_size=BATCH_SIZE, img_size=img_size, num_channels=num_channels, label_len=label_len) nbatches = test_gen._num_batches print('### Number of batches = {}'.format(nbatches)) for i in range(nbatches): test_inputs, test_targets, test_seq_len, img_names = test_gen.next_batch( ) test_feed = { inputs: test_inputs, #targets: test_targets, seq_len: test_seq_len } st = time.time() [dd, probs, scores] = session.run([decoded[0], log_prob, score], test_feed) tim = time.time() - st print('time:%s' % tim) #print(scores) detected_list = report_accuracy(dd, test_targets, scores) write_ocr(detected_list, scores, img_names, out_dir)
def detect(test_inputs, test_targets, test_seq_len): logits, inputs, targets, seq_len, W, b = model.get_train_model() decoded, log_prob = tf.contrib.ctc.ctc_beam_search_decoder( logits, seq_len, merge_repeated=False) saver = tf.train.Saver() with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, "models/ocr.model-0.5-56499") print("Model restored.") #feed_dict = {inputs: test_inputs, targets: test_targets, seq_len: test_seq_len} feed_dict = {inputs: test_inputs, seq_len: test_seq_len} dd = sess.run(decoded[0], feed_dict=feed_dict) return decode_sparse_tensor(dd)
def detect(test_inputs, test_targets, test_seq_len): logits, inputs, targets, seq_len, Wforward, Wbackward, b = model.get_train_model( ) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) saver = tf.train.Saver() with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, "models/ocr.model-0.64-17999") print("Model restored.") feed_dict = {inputs: test_inputs, seq_len: test_seq_len} dd = sess.run(decoded[0], feed_dict=feed_dict) original_list = decode_sparse_tensor(test_targets) detected_list = decode_sparse_tensor(dd) true_numer = 0 # print(detected_list) if len(original_list) != (len(detected_list)): print("len(original_list)", len(original_list), "len(detected_list)", len(detected_list), " test and detect length desn't match") return print("T/F: original(length) <-------> detectcted(length)") for idx, number in enumerate(original_list): #if(idx==999): # break detect_number = detected_list[idx] print(number, "(", len(number), ") <-------> ", detect_number, "(", len(detect_number), ")") if (len(number) == len(detect_number)): hit = True for idy, value in enumerate(number): detect_value = detect_number[idy] if (value != detect_value): hit = False break if hit: true_numer = true_numer + 1 accuraccy = true_numer * 1.0 / len(original_list) #print("Test Accuracy:", accuraccy) return accuraccy
def detect(test_inputs, test_targets, test_seq_len): logits, inputs, targets, seq_len, W, b = model.get_train_model() decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) saver = tf.train.Saver() with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, "models/ocr.model-0.95-94999") print("Model restored.") #feed_dict = {inputs: test_inputs, targets: test_targets, seq_len: test_seq_len} feed_dict = {inputs: test_inputs, seq_len: test_seq_len} dd = sess.run(decoded[0], feed_dict=feed_dict) #return decode_sparse_tensor(dd) original_list = decode_sparse_tensor(test_targets) detected_list = decode_sparse_tensor(dd) true_numer = 0 # print(detected_list) if len(original_list) != len(detected_list): print("len(original_list)", len(original_list), "len(detected_list)", len(detected_list), " test and detect length desn't match") return print("T/F: original(length) <-------> detectcted(length)") for idx, number in enumerate(original_list): detect_number = detected_list[idx] print(number, "(", len(number), ") <-------> ", detect_number, "(", len(detect_number), ")") if(len(number) == len(detect_number)): hit = True for idy, value in enumerate(number): detect_value = detect_number[idy] if(value != detect_value): hit = False break if hit: true_numer = true_numer + 1 accuraccy = true_numer * 1.0 / len(original_list) print("Test Accuracy:", accuraccy) return accuraccy
def detect(): model_restore = "models/ocr.model-0.959379192885-161999" #give the path of your final model file test_filename = "test.txt" #txt file containing the paths of all the test images output_folder = "test_outputs3/" #where the outputs will be stored factor1 = 35 #think of it as number of test_batches factor2 = 41 #think of it as the batch size ac = 0 logits, inputs, targets, seq_len, W, b = model.get_train_model() decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) saver = tf.train.Saver() with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, model_restore) #saver.restore(sess, "models2/ocr.model-0.929263617018-35999") print("Model restored.") a = 0 for x in range(0, factor1): test_names, test_inputs, test_targets, test_seq_len = utils.get_data_set( test_filename, x * factor2, (x + 1) * factor2) print test_inputs[0].shape feed_dict = {inputs: test_inputs, seq_len: test_seq_len} dd, lp = sess.run([decoded[0], log_prob], feed_dict=feed_dict) original_list = decode_sparse_tensor(test_targets) detected_list = decode_sparse_tensor(dd) names_list = test_names.tolist() print "lp", lp for x, fname_save in enumerate(names_list): result = detected_list[x] file = codecs.open( output_folder + os.path.basename(fname_save) + ".rnn.txt", "w", "utf-8") #file.write(''.join(result.tolist())) if result is numpy file.write(''.join(result)) file.close() if len(original_list) != (len(detected_list)): print("len(original_list)", len(original_list), "len(detected_list)", len(detected_list), " test and detect length desn't match") return print("T/F: original(length) <-------> detectcted(length)") total_ed = 0 total_len = 0 for idx, number in enumerate(original_list): detect_number = detected_list[idx] """if os.path.exists("output/"+names_list[idx] + ".out.txt"): append_write = 'a' # append if already exists else: append_write = 'w' # make a new file if not f = codecs.open("output/"+names_list[idx] + ".out.txt",append_write, 'utf-8') f.write("\nDetected: "+''.join(detect_number)+"\n"+"Original: ",''.join(number)) f.close()""" ed = editdistance.eval(number, detect_number) ln = len(number) edit_accuracy = (ln - ed) / ln """if (idx % 10 == 0): print("Edit: ", ed, "Edit accuracy: ", edit_accuracy,"\n", ''.join(number).encode('utf-8'), "(", len(number), ") <-------> ", ''.join(detect_number).encode('utf-8'), "(", len(detect_number), ")") """ total_ed += ed total_len += ln accuraccy = (total_len - total_ed) / total_len print("Test Accuracy:", accuraccy) ac += accuraccy return ac / factor1
def train(): global_step = tf.Variable( 0, trainable=False ) # 代表总共训练了多少批,每批训练64个样本(即64张图),每训练一批就是一个迭代,故也可表示位总共迭代了多少次了 learning_rate = tf.train.exponential_decay( common.INITIAL_LEARNING_RATE, global_step, common.DECAY_STEPS, common.LEARNING_RATE_DECAY_FACTOR, staircase=True) # 计算训练的学习率 logits, inputs, targets, seq_len, W, b = model.get_train_model() with tf.name_scope('loss'): loss = tf.nn.ctc_loss(targets, logits, seq_len) cost = tf.reduce_mean(loss) # 计算识别的损失率,即误差 tf.scalar_summary('loss', cost) # 可视化损失率变化 with tf.name_scope('train'): optimizer = tf.train.MomentumOptimizer( learning_rate=learning_rate, momentum=common.MOMENTUM).minimize(cost, global_step=global_step) # Option 2: tf.contrib.ctc.ctc_beam_search_decoder # (it's slower but you'll get better results) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) # Accuracy: label error rate acc = tf.reduce_mean( tf.edit_distance(tf.cast(decoded[0], tf.int32), targets)) # 计算识别的准确率,即精度 tf.scalar_summary('accuracy', acc) # 可视化准确率变化 # Initializate the weights and biases init = tf.global_variables_initializer() def do_report(): test_feed = { inputs: test_inputs, targets: test_targets, seq_len: test_seq_len } dd, log_probs, accuracy = session.run([decoded[0], log_prob, acc], test_feed) # 这行代码的意思:取出三个变量的值 report_accuracy(dd, test_targets) # decoded_list = decode_sparse_tensor(dd) def do_batch(): # 每训练一批数据(即每迭代一次,也即每训练64个图片)系统会更新一下神经网络模型中各层的weights和biases # 每批训练64张图组成的序列 feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } b_cost, steps, _ = session.run([cost, global_step, optimizer], feed) if steps % 50 == 0: result = session.run(merged, feed_dict=feed) # merged也是需要run的 writer.add_summary(result, steps) # result是summary类型的,需要放入writer中,i步数(x轴) if steps > 0 and steps % common.REPORT_STEPS == 0: # 每训练1000批数据(即迭代1000次,即训练10次整个数据集)存一次模型 do_report() # 每训练10次整个数据集用测试图片数据计算一次识别出字符个数的准确率 save_path = saver.save(session, "models/ocr.model", global_step=steps) # print(save_path) return b_cost, steps # 返回当前批次的损失率batch_cost和当前批次的编号 config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.get_default_graph()._kernel_label_map({"CTCLoss": "WarpCTC"}): with tf.Session(config=config) as session: merged = tf.summary.merge_all() writer = tf.summary.FileWriter("logs/", session.graph) session.run(init) saver = tf.train.Saver(tf.global_variables(), max_to_keep=100) for curr_epoch in range( num_epochs): # 对完整数据集(6400张图,即6400个样本)训练10000次 curr_epoch_start = time.time() # 当前Epoch训练开始的时间 # variables = tf.all_variables() # for i in variables: # print(i.name) print("Epoch(第几个完整数据集的训练).......", curr_epoch) # 当前是第几次对完整数据集进行训练 train_cost = train_ler = 0 for batch in range( common.BATCHES ): # BATCH_SIZE = 64 每批训练64个样本(即64张图),那么训练完一次整个数据集(一个Epoch)需要迭代6400/64=100次,即100个批次;迭代次数就是把整个数据集训练一遍需要几批 get_data_start = time.time() # 当前批次获取数据开始的时间 train_inputs, train_targets, train_seq_len = utils.get_data_set( 'train', batch * common.BATCH_SIZE, (batch + 1) * common.BATCH_SIZE) # 每批取出64个样本即64张图进行训练 get_data_time = time.time( ) - get_data_start # 当前批次获取数据花费的时间 start = time.time() # 当前批次训练开始的时间 c, steps = do_batch( ) # 每训练一批(或者叫每迭代一次,也可叫每训练64张图)会更新一下神经网络模型各层的weights和biases train_cost += c * common.BATCH_SIZE # 累加每批中所有样本的损失率(也即当前批次64张图乘以当批的平均损失率c)计算当前Epoch(一次整个数据的训练)总的损失率 seconds = time.time() - start # 当前批次训练花费的时间 print("Step(在10000个Epoch中的批次编号):", steps, ", batch seconds(当前批次训练花费的时间):", seconds, ", batch get data seconds(当前批次获取数据花费的时间):", get_data_time, ", batch cost(当前批次的损失率):", c) train_cost /= common.TRAIN_SIZE # 计算当前Epoch(即整个数据集的样本数,也即6400个样本,再即6400张图)的每个样本(也即每张图)的损失率 # train_ler /= common.TRAIN_SIZE val_feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } # 用当前Epoch的最后一批样本数据来取 # 总共对整个数据集训练10000遍,每遍训练100批,每批训练64个样本,每个样本是一张图片 # val_cost指计算cost操作的返回值,是当前的误差率; # val_ler指计算acc操作的返回值,是当前的准确率; # lr指计算learning_rate操作的返回值,是当前的学习率; # steps指计算global_step操作的返回值,是已经训练的总批数; val_cost, val_ler, lr, steps = session.run( [cost, acc, learning_rate, global_step], feed_dict=val_feed) log = "Epoch(对整个数据集进行的第几次训练){}/{}, (第几批训练)steps = {}, (当前Epoch中平均每张图的损失率)train_cost = {:.3f}, (当前Epoch中平均每张图的精确度)train_ler = {:.3f}, (当前的损失率)val_cost = {:.3f}, (当前的精确度)val_ler = {:.3f}, (当前Epoch花费的时间)time = {:.3f}s, (当前的学习率)learning_rate = {}" print( log.format(curr_epoch + 1, num_epochs, steps, train_cost, train_ler, val_cost, val_ler, time.time() - curr_epoch_start, lr)) writer.close() # 10000个Epoch训练完时关闭summary的FileWriter
def train(): global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay( common.INITIAL_LEARNING_RATE, global_step, common.DECAY_STEPS, common.LEARNING_RATE_DECAY_FACTOR, staircase=True) logits, inputs, targets, seq_len, W, b = model.get_train_model() loss = tf.nn.ctc_loss(targets, logits, seq_len) cost = tf.reduce_mean(loss) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=common.MOMENTUM).minimize( cost, global_step=global_step) # Option 2: tf.contrib.ctc.ctc_beam_search_decoder # (it's slower but you'll get better results) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) # Accuracy: label error rate acc = tf.reduce_mean( tf.edit_distance(tf.cast(decoded[0], tf.int32), targets)) # Initializate the weights and biases init = tf.global_variables_initializer() def do_report(): test_feed = { inputs: test_inputs, targets: test_targets, seq_len: test_seq_len } dd, log_probs, accuracy = session.run([decoded[0], log_prob, acc], test_feed) report_accuracy(dd, test_targets) # decoded_list = decode_sparse_tensor(dd) def do_batch(): feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } b_cost, steps, _ = session.run([cost, global_step, optimizer], feed) if steps > 0 and steps % common.REPORT_STEPS == 0: do_report() save_path = saver.save(session, "models/ocr.model", global_step=steps) # print(save_path) return b_cost, steps with tf.Session() as session: session.run(init) saver = tf.train.Saver(tf.global_variables(), max_to_keep=100) for curr_epoch in xrange(num_epochs): # variables = tf.all_variables() # for i in variables: # print(i.name) print("Epoch.......", curr_epoch) train_cost = train_ler = 0 for batch in xrange(common.BATCHES): start = time.time() train_inputs, train_targets, train_seq_len = utils.get_data_set( 'train', batch * common.BATCH_SIZE, (batch + 1) * common.BATCH_SIZE) print("get data time", time.time() - start) start = time.time() c, steps = do_batch() train_cost += c * common.BATCH_SIZE seconds = time.time() - start print("Step:", steps, ", batch seconds:", seconds) train_cost /= common.TRAIN_SIZE # train_ler /= common.TRAIN_SIZE val_feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } val_cost, val_ler, lr, steps = session.run( [cost, acc, learning_rate, global_step], feed_dict=val_feed) log = "Epoch {}/{}, steps = {}, train_cost = {:.3f}, train_ler = {:.3f}, val_cost = {:.3f}, val_ler = {:.3f}, time = {:.3f}s, learning_rate = {}" print( log.format(curr_epoch + 1, num_epochs, steps, train_cost, train_ler, val_cost, val_ler, time.time() - start, lr))
def train(a): train_gen = TextImageGenerator(img_dir=ti, label_file=tl, batch_size=BATCH_SIZE, img_size=img_size, num_channels=num_channels, label_len=label_len) val_gen = TextImageGenerator(img_dir=vi, label_file=vl, batch_size=BATCH_SIZE, img_size=img_size, num_channels=num_channels, label_len=label_len) global_step = tf.Variable(0, trainable=False) #is_training = tf.placeholder(tf.bool, name='is_training') learning_rate = tf.train.exponential_decay(INITIAL_LEARNING_RATE, global_step, DECAY_STEPS, LEARNING_RATE_DECAY_FACTOR, staircase=True) isTraining = tf.placeholder(tf.bool, name="is_train") logits, inputs, targets, seq_len = get_train_model(num_channels, label_len,BATCH_SIZE, img_size, isTraining, True) logits = tf.transpose(logits, (1, 0, 2)) # tragets是一个稀疏矩阵 loss = tf.nn.ctc_loss(labels=targets, inputs=logits, sequence_length=seq_len) cost = tf.reduce_mean(loss) # optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,momentum=MOMENTUM).minimize(cost, global_step=global_step) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, global_step=global_step) # 前面说的划分块之后找每块的类属概率分布,ctc_beam_search_decoder方法,是每次找最大的K个概率分布 # 还有一种贪心策略是只找概率最大那个,也就是K=1的情况ctc_ greedy_decoder decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False, top_paths=3) plate_predict = decode_tensor(decoded[0]) score = tf.subtract(log_prob[:, 0], log_prob[:, 1], name='confidence_score') acc = tf.reduce_mean(tf.edit_distance(tf.cast(decoded[0], tf.int32), targets)) init = tf.global_variables_initializer() def report_accuracy(decoded_list, test_targets): original_list = decode_sparse_tensor(test_targets) detected_list = decode_sparse_tensor(decoded_list) true_numer = 0 if len(original_list) != len(detected_list): print("len(original_list)", len(original_list), "len(detected_list)", len(detected_list), " test and detect length desn't match") return print("T/F: original(length) <-------> detectcted(length)") for idx, number in enumerate(original_list): detect_number = detected_list[idx] hit = (number == detect_number) #print(hit, number, "(", len(number), ") <-------> ", detect_number, "(", len(detect_number), ")") if hit: true_numer = true_numer + 1 print("Test Accuracy:", true_numer * 1.0 / len(original_list)) def do_report(val_gen,num): for i in range(num): test_inputs, test_targets, test_seq_len = val_gen.next_batch() test_feed = {inputs: test_inputs, targets: test_targets, seq_len: test_seq_len, isTraining: True} st =time.time() dd= session.run(decoded[0], test_feed) tim = time.time() -st print('time:%s'%tim) report_accuracy(dd, test_targets) def test_report(testi,files): true_numer = 0 num = files//BATCH_SIZE for i in range(num): test_inputs, test_targets, test_seq_len = val_gen.next_batch() test_feed = {inputs: test_inputs, targets: test_targets, seq_len: test_seq_len, isTraining: True} dd = session.run([decoded[0]], test_feed) original_list = decode_sparse_tensor(test_targets) detected_list = decode_sparse_tensor(dd) for idx, number in enumerate(original_list): detect_number = detected_list[idx] hit = (number == detect_number) if hit: true_numer = true_numer + 1 print("Test Accuracy:", true_numer * 1.0 / files) def do_batch(train_gen,val_gen): train_inputs, train_targets, train_seq_len = train_gen.next_batch() feed = {inputs: train_inputs, targets: train_targets, seq_len: train_seq_len, isTraining:True} b_loss, b_targets, b_logits, b_seq_len, b_cost, steps, _ = session.run( [loss, targets, logits, seq_len, cost, global_step, optimizer], feed) #print(b_cost, steps) if steps > 0 and steps % REPORT_STEPS == 0: do_report(val_gen,test_num) saver.save(session, "./modelk11/LPRChar69.ckpt", global_step=steps) return b_cost, steps with tf.Session() as session: session.run(init) saver = tf.train.Saver(tf.global_variables(), max_to_keep=100) if a=='train': #start_epoch = 0 checkpoint = './modelk11/LPRAug.ckpt-78000' saver.restore(session, checkpoint) checkpoint_id = 0 start_epoch = checkpoint_id // BATCHES for curr_epoch in range(start_epoch, start_epoch+num_epochs): print("Epoch.......", curr_epoch) train_cost = train_ler = 0 for batch in range(BATCHES): start = time.time() c, steps = do_batch(train_gen,val_gen) train_cost += c * BATCH_SIZE seconds = time.time() - start #print("Step:", steps, ", batch seconds:", seconds) train_cost /= TRAIN_SIZE val_cs=0 val_ls =0 for i in range(test_num): train_inputs, train_targets, train_seq_len = val_gen.next_batch() val_feed = {inputs: train_inputs, targets: train_targets, seq_len: train_seq_len, isTraining: True} val_cost, val_ler, lr, steps = session.run([cost, acc, learning_rate, global_step], feed_dict=val_feed) val_cs+=val_cost val_ls+=val_ler log = "Epoch {}/{}, steps = {}, train_cost = {:.3f}, train_ler = {:.3f}, val_cost = {:.3f}, val_ler = {:.3f}, time = {:.3f}s, learning_rate = {}" print(log.format(curr_epoch + 1, num_epochs, steps, train_cost, train_ler, val_cs/test_num, val_ls/test_num, time.time() - start, lr))
def train(): global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(common.INITIAL_LEARNING_RATE, global_step, common.DECAY_STEPS, common.LEARNING_RATE_DECAY_FACTOR, staircase=True) logits, inputs, targets, seq_len, W, b = model.get_train_model() loss = tf.nn.ctc_loss(targets, logits, seq_len) cost = tf.reduce_mean(loss) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=common.MOMENTUM).minimize(cost, global_step=global_step) # Option 2: tf.contrib.ctc.ctc_beam_search_decoder # (it's slower but you'll get better results) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) # Accuracy: label error rate acc = tf.reduce_mean(tf.edit_distance(tf.cast(decoded[0], tf.int32), targets)) # Initializate the weights and biases init = tf.global_variables_initializer() def do_report(): test_feed = {inputs: test_inputs, targets: test_targets, seq_len: test_seq_len} dd, log_probs, accuracy = session.run([decoded[0], log_prob, acc], test_feed) report_accuracy(dd, test_targets) # decoded_list = decode_sparse_tensor(dd) def do_batch(): feed = {inputs: train_inputs, targets: train_targets, seq_len: train_seq_len} b_cost, steps, _ = session.run([cost, global_step, optimizer], feed) if steps > 0 and steps % common.REPORT_STEPS == 0: do_report() save_path = saver.save(session, "models/ocr.model", global_step=steps) #print(save_path) return b_cost, steps config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as session: session.run(init) saver = tf.train.Saver(tf.global_variables(), max_to_keep=100) for curr_epoch in range(num_epochs): # variables = tf.all_variables() # for i in variables: # print(i.name) print("Epoch.......", curr_epoch) train_cost = train_ler = 0 for batch in range(common.BATCHES): start = time.time() train_inputs, train_targets, train_seq_len = utils.get_data_set('train', batch * common.BATCH_SIZE, (batch + 1) * common.BATCH_SIZE) #print("get data time", time.time() - start) start = time.time() c, steps = do_batch() train_cost += c * common.BATCH_SIZE seconds = time.time() - start print("Step:", steps, ", batch seconds:", seconds) train_cost /= common.TRAIN_SIZE # train_ler /= common.TRAIN_SIZE val_feed = {inputs: train_inputs, targets: train_targets, seq_len: train_seq_len} val_cost, val_ler, lr, steps = session.run([cost, acc, learning_rate, global_step], feed_dict=val_feed) log = "Epoch {}/{}, steps = {}, train_cost = {:.3f}, train_ler = {:.3f}, val_cost = {:.3f}, val_ler = {:.3f}, time = {:.3f}s, learning_rate = {}" print(log.format(curr_epoch + 1, num_epochs, steps, train_cost, train_ler, val_cost, val_ler, time.time() - start, lr))
def freeze_graph(model_dir, output_node_names): """Extract the sub graph defined by the output nodes and convert all its variables into constant Args: model_dir: the root folder containing the checkpoint state file output_node_names: a string, containing all the output node's names, comma separated """ if not tf.gfile.Exists(model_dir): raise AssertionError( "Export directory doesn't exists. Please specify an export " "directory: %s" % model_dir) if not output_node_names: print("You need to supply the name of a node to --output_node_names.") return -1 # We retrieve our checkpoint fullpath checkpoint = tf.train.get_checkpoint_state(model_dir) input_checkpoint = './modelk11/LPRChar69.ckpt-96000' input_checkpoint = './model_c1/LPRc1.ckpt-63000' # We precise the file fullname of our freezed graph absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1]) print absolute_model_dir output_graph = absolute_model_dir + "/frozen_model.pb" # We clear devices to allow TensorFlow to control on which device it will load operations clear_devices = True tf.reset_default_graph() eval_graph = tf.Graph() config = tf.ConfigProto() config.gpu_options.allow_growth = True # We start a session using a temporary fresh Graph #with tf.Session(graph=tf.Graph()) as sess: with tf.Session(config=config, graph=eval_graph) as sess: global_step = tf.Variable(0, trainable=False) # We import the meta graph in the current default Graph #saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices) # isTraining = tf.placeholder(tf.bool, name="is_train") logits, inputs, targets, seq_len = get_train_model(1, label_len, BATCH_SIZE, img_size, False, False) logits = tf.transpose(logits, (1, 0, 2), name='logits_transpose') # decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False, beam_width=100, top_paths=3) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False, top_paths=3) plate_predict = decode_tensor(decoded[0]) score = tf.subtract(log_prob[:, 0], log_prob[:, 1], name='confidence_score') # We restore the weights saver = tf.train.Saver(tf.global_variables(), max_to_keep=4) saver.restore(sess, input_checkpoint) # We use a built-in TF helper to export variables to constants output_graph_def = tf.graph_util.convert_variables_to_constants( sess, # The session is used to retrieve the weights tf.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes output_node_names.split(",") # The output node names are used to select the usefull nodes ) # Finally we serialize and dump the output graph to the filesystem with tf.gfile.GFile(output_graph, "wb") as f: f.write(output_graph_def.SerializeToString()) print("%d ops in the final graph." % len(output_graph_def.node)) return output_graph_def
def train(): test_names, test_inputs, test_targets, test_seq_len = utils.get_data_set( 'valid.txt') global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay( common.INITIAL_LEARNING_RATE, global_step, common.DECAY_STEPS, common.LEARNING_RATE_DECAY_FACTOR, staircase=True) logits, inputs, targets, seq_len, W, b = model.get_train_model() loss = tf.nn.ctc_loss(logits, targets, seq_len) cost = tf.reduce_mean(loss) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=common.MOMENTUM).minimize( cost, global_step=global_step) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) acc = tf.reduce_mean( tf.edit_distance(tf.cast(decoded[0], tf.int32), targets)) def do_report(): test_feed = { inputs: test_inputs, targets: test_targets, seq_len: test_seq_len } dd, log_probs, accuracy = session.run([decoded[0], log_prob, acc], test_feed) accuracy = report_accuracy(dd, test_targets, test_names) save_path = saver.save(session, "models/ocr.model-" + str(accuracy), global_step=steps) # decoded_list = decode_sparse_tensor(dd) def do_batch(): feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } b_cost, steps, _ = session.run([cost, global_step, optimizer], feed) if steps > 0 and steps % common.REPORT_STEPS == 0: do_report() return b_cost, steps gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.49) with tf.Session(config=tf.ConfigProto(log_device_placement=True, allow_soft_placement=True, gpu_options=gpu_options)) as session: ckpt = tf.train.get_checkpoint_state("models") if ckpt and ckpt.model_checkpoint_path: saver = tf.train.Saver() saver.restore(session, ckpt.model_checkpoint_path) else: print("no checkpoint found") # Initializate the weights and biases init = tf.initialize_all_variables() session.run(init) saver = tf.train.Saver(tf.all_variables(), max_to_keep=100) for curr_epoch in xrange(num_epochs): print("Epoch.......", curr_epoch) train_cost = train_ler = 0 for batch in xrange(common.BATCHES): start = time.time() train_names, train_inputs, train_targets, train_seq_len = utils.get_data_set( 'train.txt', batch * common.BATCH_SIZE, (batch + 1) * common.BATCH_SIZE) print("get data time", time.time() - start) start = time.time() c, steps = do_batch() train_cost += c * common.BATCH_SIZE seconds = time.time() - start print("Step: ", steps, ", batch seconds: ", seconds) train_cost /= common.TRAIN_SIZE val_feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } val_cost, val_ler, lr, steps = session.run( [cost, acc, learning_rate, global_step], feed_dict=val_feed) log = "Epoch {}/{}, steps = {}, train_cost = {:.3f}, train_ler = {:.3f}, val_cost = {:.3f}, val_ler = {:.3f}, time = {:.3f}s, learning_rate = {}" print( log.format(curr_epoch + 1, num_epochs, steps, train_cost, train_ler, val_cost, val_ler, time.time() - start, lr))
dataset = pd.read_csv(path_dataset, header=0, index_col=0) # split into train and test values = dataset.values n_train_hours = 365 * 24 train = values[:n_train_hours,:] test = values[n_train_hours:, :] # split into input variables and output variables x_train, y_train = train[:,:-1], train[:, -1] x_test, y_test = test[:, :-1], test[:,-1] # reshape input to be 3D [samples, timesteps, features] x_train = x_train.reshape((x_train.shape[0], 1, x_train.shape[1])) x_test = x_test.reshape((x_test.shape[0], 1, x_test.shape[1])) model = get_train_model(x_train) model.compile(loss='mse', optimizer='SGD') x_test, x_val, y_test, y_val = train_test_split(x_test, y_test, test_size=0.5, random_state=121) model.fit(x_train, y_train, epochs=100, batch_size=512, validation_data=(x_val, y_val), verbose=2, shuffle=False) model.save(os.path.join(experiment_name, "model.h5")) # save the dataset transformed np.savez_compressed("dataset", x_train=x_train, y_train=y_train, x_test=x_test, y_test=y_test)
def train(): test_names, test_inputs, test_targets, test_seq_len = utils.get_data_set( 'train.txt') global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay( common.INITIAL_LEARNING_RATE, global_step, common.DECAY_STEPS, common.LEARNING_RATE_DECAY_FACTOR, staircase=True) logits, inputs, targets, seq_len, W, b = model.get_train_model() loss = tf.nn.ctc_loss(logits, targets, seq_len) cost = tf.reduce_mean(loss) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=common.MOMENTUM).minimize( cost, global_step=global_step) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False) acc = tf.reduce_mean( tf.edit_distance(tf.cast(decoded[0], tf.int32), targets)) cost_summary = tf.summary.scalar('cost', cost) val_labelerror = tf.summary.scalar("Label Error on the Validation Set", acc) #cost_summary = tf.scalar_summary("cost", cost) #loss_summary = tf.summary.scalar("loss", loss) # merge all summaries into a single "operation" which we can execute in a session #summary_op = tf.merge_summary([cost_summary]) def do_report(): avg_acc = 0 for bt in xrange(test_batches): test_names, test_inputs, test_targets, test_seq_len = utils.get_data_set( 'valid.txt', bt * test_batch_size, (bt + 1) * test_batch_size) test_feed = { inputs: test_inputs, targets: test_targets, seq_len: test_seq_len } dd, log_probs, accuracy = session.run([decoded[0], log_prob, acc], test_feed) accuracy = report_accuracy(dd, test_targets, test_names) if accuracy is not None: avg_acc += accuracy avg_acc = avg_acc / test_batches save_path = saver.save(session, "models/ocr.model-" + str(avg_acc), global_step=steps) #avg_acc_summary = tf.summary.scalar("Average_Accuracy", avg_acc) #summary_op = tf.merge_summary([cost_summary, avg_acc_summary]) # decoded_list = decode_sparse_tensor(dd) def do_batch(): feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } b_cost, steps, _ = session.run([cost, global_step, optimizer], feed) if steps > 0 and steps % common.REPORT_STEPS == 0: do_report() return b_cost, steps ########################################################################################################### gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8) ########################################################################################################### with tf.Session(config=tf.ConfigProto(log_device_placement=False, allow_soft_placement=True, gpu_options=gpu_options)) as session: ckpt = tf.train.get_checkpoint_state("models") if ckpt and ckpt.model_checkpoint_path: saver = tf.train.Saver() saver.restore(session, ckpt.model_checkpoint_path) else: print("no checkpoint found") # Initializate the weights and biases init = tf.initialize_all_variables() session.run(init) saver = tf.train.Saver(tf.all_variables(), max_to_keep=100) #writer = tf.train.SummaryWriter(logs_path, graph=tf.get_default_graph()) writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph()) for curr_epoch in xrange(num_epochs): print("Epoch.......", curr_epoch) train_cost = train_ler = 0 for batch in xrange(common.BATCHES): start = time.time() train_names, train_inputs, train_targets, train_seq_len = utils.get_data_set( 'train.txt', batch * common.BATCH_SIZE, (batch + 1) * common.BATCH_SIZE) print("get data time", time.time() - start) start = time.time() c, steps = do_batch() train_cost += c * common.BATCH_SIZE seconds = time.time() - start print("Step:", steps, ", batch seconds:", seconds) train_cost /= common.TRAIN_SIZE summary_op = tf.summary.merge_all() val_feed = { inputs: train_inputs, targets: train_targets, seq_len: train_seq_len } #val_cost, val_ler, lr, steps, summary= session.run([cost, acc, learning_rate, global_step,summary_op], feed_dict=val_feed) val_cost, val_ler, lr, steps, summary = session.run( [cost, acc, learning_rate, global_step, summary_op], feed_dict=val_feed) writer.add_summary(summary, steps) log = "Epoch {}/{}, steps = {}, train_cost = {:.3f}, train_ler = {:.3f}, val_cost = {:.3f}, val_ler = {:.3f}, time = {:.3f}s, learning_rate = {}" print( log.format(curr_epoch + 1, num_epochs, steps, train_cost, train_ler, val_cost, val_ler, time.time() - start, lr))
MODEL_CKPT = './model_c1/LPRc1.ckpt-63000' args.num_channels=1 tf.reset_default_graph() eval_graph = tf.Graph() config = tf.ConfigProto() config.gpu_options.allow_growth = True exporter = tf.saved_model.builder.SavedModelBuilder(MODEL_DIR) with tf.Session(config=config, graph=eval_graph) as sess: global_step = tf.Variable(0, trainable=False) #isTraining = tf.placeholder(tf.bool, name="is_train") logits, inputs, targets, seq_len = get_train_model(args.num_channels, label_len, BATCH_SIZE, img_size, False, False) logits = tf.transpose(logits, (1, 0, 2), name='logits_transpose') #decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False, beam_width=100, top_paths=3) decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, seq_len, merge_repeated=False, top_paths=3) plate_predict = decode_tensor(decoded[0]) score = tf.subtract(log_prob[:, 0], log_prob[:, 1], name='confidence_score') # feed_dict = {"inputs": inputs, # "seq_len": test_seq_len} ipt = {"Placeholder": inputs, "Placeholder_4":seq_len} #"is_train": isTraining} outputs = { 'code2str_conversion/predicted':plate_predict, 'confidence_score': score}