def validate(config): if hasattr(config.eval, 'random_seed'): np.random.seed(config.eval.random_seed) tf.set_random_seed(config.eval.random_seed) random.seed(config.eval.random_seed) if hasattr(config.eval.execution, 'CUDA_VISIBLE_DEVICES'): os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ[ "CUDA_VISIBLE_DEVICES"] = config.train.execution.CUDA_VISIBLE_DEVICES height, width, channels_num = config.input_shape rnn_cells_num = config.rnn_cells_num graph = tf.Graph() with graph.as_default(): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=False): inp_data, label_val, file_names = data_input( height, width, channels_num, config.eval.file_list_path, batch_size=config.eval.batch_size) prob = inference(rnn_cells_num, inp_data, config.num_classes) prob = tf.transpose(prob, (1, 0, 2)) # prepare for CTC data_length = tf.fill( [tf.shape(prob)[1]], tf.shape(prob)[0]) # input seq length, batch size result = tf.nn.ctc_greedy_decoder(prob, data_length, merge_repeated=True) predictions = tf.to_int32(result[0][0]) d_predictions = tf.sparse_to_dense(predictions.indices, [ tf.shape(inp_data, out_type=tf.int64)[0], config.max_lp_length ], predictions.values, default_value=-1, name='d_predictions') init = tf.initialize_all_variables() saver = tf.train.Saver(write_version=tf.train.SaverDef.V2) # session conf = tf.ConfigProto() if hasattr(config.eval.execution, 'per_process_gpu_memory_fraction'): conf.gpu_options.per_process_gpu_memory_fraction = config.train.execution.per_process_gpu_memory_fraction if hasattr(config.eval.execution, 'allow_growth'): conf.gpu_options.allow_growth = config.train.execution.allow_growth sess = tf.Session(graph=graph, config=conf) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) sess.run(init) checkpoints_dir = config.model_dir latest_checkpoint = None wait_iters = 0 if not os.path.exists(os.path.join(checkpoints_dir, 'eval')): os.mkdir(os.path.join(checkpoints_dir, 'eval')) writer = tf.summary.FileWriter(os.path.join(checkpoints_dir, 'eval'), sess.graph) while True: if config.eval.checkpoint != '': new_checkpoint = config.eval.checkpoint else: new_checkpoint = tf.train.latest_checkpoint(checkpoints_dir) if latest_checkpoint != new_checkpoint: latest_checkpoint = new_checkpoint saver.restore(sess, latest_checkpoint) current_step = tf.train.load_variable(latest_checkpoint, 'global_step') test_size = dataset_size(config.eval.file_list_path) time_start = time.time() mean_accuracy, mean_accuracy_minus_1 = 0.0, 0.0 steps = int(test_size / config.eval.batch_size) if int( test_size / config.eval.batch_size) else 1 num = 0 for _ in range(steps): val, slabel, _ = sess.run( [d_predictions, label_val, file_names]) acc, acc1, num_ = accuracy(slabel, val, config.vocab, config.r_vocab) mean_accuracy += acc mean_accuracy_minus_1 += acc1 num += num_ writer.add_summary( tf.Summary(value=[ tf.Summary.Value(tag='evaluation/acc', simple_value=float(mean_accuracy / num)), tf.Summary.Value(tag='evaluation/acc-1', simple_value=float(mean_accuracy_minus_1 / num)) ]), current_step) print('Test acc: {}'.format(mean_accuracy / num)) print('Test acc-1: {}'.format(mean_accuracy_minus_1 / num)) print('Time per step: {} for test size {}'.format( time.time() - time_start / steps, test_size)) else: if wait_iters % 12 == 0: sys.stdout.write('\r') for _ in range(11 + wait_iters // 12): sys.stdout.write(' ') sys.stdout.write('\r') for _ in range(1 + wait_iters // 12): sys.stdout.write('|') else: sys.stdout.write('.') sys.stdout.flush() time.sleep(5) wait_iters += 1 if config.eval.checkpoint != '': break coord.request_stop() coord.join(threads) sess.close()
def infer(config): if hasattr(config.infer, 'random_seed'): np.random.seed(config.infer.random_seed) tf.set_random_seed(config.infer.random_seed) random.seed(config.infer.random_seed) if hasattr(config.infer.execution, 'CUDA_VISIBLE_DEVICES'): os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ[ "CUDA_VISIBLE_DEVICES"] = config.train.execution.CUDA_VISIBLE_DEVICES height, width, channels_num = config.input_shape rnn_cells_num = config.rnn_cells_num graph = tf.Graph() with graph.as_default(): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=False): inp_data, filenames = data_input( height, width, channels_num, config.infer.file_list_path, batch_size=config.infer.batch_size) prob = inference(rnn_cells_num, inp_data, config.num_classes) prob = tf.transpose(prob, (1, 0, 2)) # prepare for CTC data_length = tf.fill( [tf.shape(prob)[1]], tf.shape(prob)[0]) # input seq length, batch size result = tf.nn.ctc_greedy_decoder(prob, data_length, merge_repeated=True) predictions = tf.to_int32(result[0][0]) d_predictions = tf.sparse_to_dense(predictions.indices, [ tf.shape(inp_data, out_type=tf.int64)[0], config.max_lp_length ], predictions.values, default_value=-1, name='d_predictions') init = tf.initialize_all_variables() saver = tf.train.Saver(write_version=tf.train.SaverDef.V2) # session conf = tf.ConfigProto() if hasattr(config.eval.execution, 'per_process_gpu_memory_fraction'): conf.gpu_options.per_process_gpu_memory_fraction = config.train.execution.per_process_gpu_memory_fraction if hasattr(config.eval.execution, 'allow_growth'): conf.gpu_options.allow_growth = config.train.execution.allow_growth sess = tf.Session(graph=graph, config=conf) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) sess.run(init) latest_checkpoint = config.infer.checkpoint if config.infer.checkpoint == '': latest_checkpoint = tf.train.latest_checkpoint(config.model_dir) saver.restore(sess, latest_checkpoint) infer_size = dataset_size(config.infer.file_list_path) steps = int(infer_size / config.infer.batch_size) if int(infer_size / config.infer.batch_size) else 1 ii = 0 for _ in range(steps): vals, batch_filenames = sess.run([d_predictions, filenames]) #print(batch_filenames) pred = decode_beams(vals, config.r_vocab) for i, filename in enumerate(batch_filenames): filename = filename.decode('utf-8') img = cv2.imread(filename) size = cv2.getTextSize(pred[i], cv2.FONT_HERSHEY_SIMPLEX, 0.55, 2) text_width = size[0][0] text_height = size[0][1] img_he, img_wi, _ = img.shape img = cv2.copyMakeBorder(img, 0, text_height + 10, 0, 0 if text_width < img_wi else text_width - img_wi, cv2.BORDER_CONSTANT, value=(255, 255, 255)) cv2.putText(img, pred[i], (0, img_he + text_height + 5), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 2) #cv2.imshow('License Plate', img) ii = ii + 1 nameImg = 'result_' + str(ii) + '.png' savePath = "results" pathImg = savePath + "/" + nameImg print("Result saved at: {}".format(pathImg)) cv2.imwrite(os.path.join(savePath, nameImg), img) key = cv2.waitKey(1) if key == 27: break coord.request_stop() coord.join(threads) sess.close()