if args == 'true' : TRAIN_FLAGS = True elif args == 'false' : TRAIN_FLAGS = False elif op == '--input' : input_data = load_english_dataset.read_inference_data(args) else : print 'invalid program usage. check the usage again.' sys.exit() if TRAIN_FLAGS == True: input_data, ground_truth, test_data, test_ground_truth = load_english_dataset.read_dataset() assert len(input_data) == len(ground_truth), 'len of input_data : %d, len of gt : %d' % (len(input_data), len(ground_truth)) assert len(test_data) == len(test_ground_truth) print '# of training dataset : %d, # of testing dataset : %d' %(len(input_data), len(test_data)) def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) initial /= 10.0 return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial)
def inference(image, bounding_box, bbox_type, bbox_character): input_data, ground_truth, test_data, test_ground_truth = [], [], [], [] if TRAIN_FLAGS == True: input_data, ground_truth, test_data, test_ground_truth = load_english_dataset.read_dataset() assert len(input_data) == len(ground_truth), 'len of input_data : %d, len of gt : %d' % (len(input_data), len(ground_truth)) assert len(test_data) == len(test_ground_truth) print '# of training dataset : %d, # of testing dataset : %d' %(len(input_data), len(test_data)) else: for row in bounding_box: input_row = [] for x1,y1,x2,y2 in row: input_row.append(load_english_dataset.read_inference_data(image[x1:x2,y1:y2])) input_data.append(input_row) def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) initial /= 10.0 return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') x = tf.placeholder("float", shape=[1,32*32*1]) #[32*32*1] y_ = tf.placeholder("float", shape=[1,class_size]) #[class_size] x_image = tf.reshape(x, [-1,32,32,1]) W_conv0 = weight_variable([3,3,1,16]) b_conv0 = bias_variable([16]) h_conv0 = tf.nn.relu(conv2d(x_image, W_conv0) + b_conv0) # h_conv0=[1,32*32*16] W_conv1 = weight_variable([3,3,16,32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(h_conv0, W_conv1) + b_conv1) #h_conv1=[1,32*32*32] h_pool1 = max_pool_2x2(h_conv1) #h_pool1=[1,16*16*32] W_conv1_5 = weight_variable([3,3,32,32]) b_conv1_5 = bias_variable([32]) h_conv1_5 = tf.nn.relu(conv2d(h_pool1, W_conv1_5) + b_conv1_5) # h_conv1.5=[32*32*32] W_conv2 = weight_variable([3,3,32,64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_conv1_5, W_conv2) + b_conv2) #h_conv2=[16*16*64] h_pool2 = max_pool_2x2(h_conv2) #h_pool2=[8*8*64] W_conv3 = weight_variable([3,3,64,128]) b_conv3 = bias_variable([128]) h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) W_fc1 = weight_variable([8*8*128, 1024]) b_fc1 = bias_variable([1024]) h_conv3_flat = tf.reshape(h_conv3, [-1, 8*8*128]) h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder("float") h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # is number of Hangul W_fc2 = weight_variable([1024,1024]) b_fc2 = bias_variable([1024]) h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) W_fc3 = weight_variable([1024,class_size]) b_fc3 = bias_variable([class_size]) y_conv = tf.nn.softmax(tf.matmul(h_fc2_drop, W_fc3) + b_fc3) cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv + 1e-9)) train_step = tf.train.AdamOptimizer(1e-6).minimize(cross_entropy) #train_step_2 = tf.train.AdamOptimizer(1e-7).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(y_conv,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) sess.run(tf.initialize_all_variables()) saver = tf.train.Saver() start_time = time.time() idx = range(len(ground_truth)) random.shuffle(idx) LR_CHANGE_FLAG = False # during training phase, we will train the model. if TRAIN_FLAGS == True: for i in range(EPOCH): #at every 100 iterations, we will compute loss and display it. if i%20 == 0: acc = 0.0 loss = 0.0 # we need batch size of 50 for calculate loss. for k in xrange(len(test_data)): acc += accuracy.eval( feed_dict={x:test_data[k], y_:test_ground_truth[k].reshape(1,class_size), keep_prob: 1.0}) loss += sess.run(cross_entropy, {x:test_data[k], y_:test_ground_truth[k].reshape(1, class_size), keep_prob: 1.0}) acc /= float(len(test_data)) loss /= float(len(test_data)) current_time = time.time() print '%dth iteration...%d secs passed over... accuracy : %lf, loss : %lf'% (i, current_time - start_time, acc, loss) #we need save checkpoint at every 1000 iterations. if i>0 and i%200 == 0: chk_file = checkpoint_dir + 'iteration_' + str(i) + '.ckpt' saver.save(sess, chk_file) print 'model saved in files : %s' % chk_file # every entry in training data is used at training. not a batch # formulation. for t in idx: train_step.run(feed_dict={x:input_data[t], y_:ground_truth[t], keep_prob: 0.5}) # during evaluation phase, we restore model by checkpoint file. else: check_file = './checkpoint_english/english_shortcut.ckpt' saver.restore(sess, check_file) #label = sess.run(tf.argmax(y_conv,1), feed_dict={x:input_data[0], keep_prob:1.0}) label_interface_file = open(pickle_path + 'english_label_information.txt') label_interface = pickle.load(label_interface_file) i = 0 for row in input_data: j = -1 for input_box in row: """ only takes english character. """ j += 1 if bbox_type[i][j] != 8 : continue label_index = sess.run(tf.argmax(y_conv,1), feed_dict = {x:input_box, keep_prob:1.0}) bbox_character[i][j] = str(label_interface[label_index]) i += 1 bbox_character_file = open(pickle_path + 'bbox_character.txt', 'w') pickle.dump(bbox_character, bbox_character_file) bbox_character_file.close() sess.close()