parser = argparse.ArgumentParser(description='Compare to Tesseract OCR') parser.add_argument('-m', '--model', metavar='m', type=str, default='model.pth') parser.add_argument('input_dir', type=str, help='Input directory') args = parser.parse_args() device = torch.device('cpu') dataset = data.MyDataset(args.input_dir) net = model.MyNet().to(device) net.load_state_dict(torch.load(args.model, map_location='cpu')) net.eval() torch.no_grad() time_sum_tess = 0 time_sum_mynet = 0 accuracy_sum_tess = 0.0 accuracy_sum_mynet = 0.0 accuracy_worst_tess = float('inf') accuracy_worst_mynet = float('inf') num_total = 0
import pandas as pd import torch import model import processData if __name__ == "__main__": dp = processData.DataProcessing() train_x, train_y = dp.load_data('data/train.csv') test_x = dp.load_data('data/test.csv') model = model.MyNet() model.train(train_x, train_y, 100) model.write_result(test_x)
def train_on_cpu(): #merged = tf.summary.merge_all() #print(merged) p1 = tf.placeholder(tf.float32, [None, 20, 40, 3]) p2 = tf.placeholder(tf.float32, [None, 8, 10]) p3 = tf.placeholder(tf.float32, [None, 10]) label = tf.placeholder(tf.float32, [None, 1]) y_pred = m.MyNet(p1, p2, p3) #mse = tf.reduce_sum((label -y_pred)**2) mse = tf.losses.mean_squared_error(label, y_pred) mse_train_summary = tf.summary.scalar('train mse', mse) mse_test_summary = tf.summary.scalar('test mse', mse) mse_train_hist = tf.summary.histogram('train_mse_hist', mse) mse_test_hist = tf.summary.histogram('test_mse_hist', mse) #mse = tf.nn.l2_loss(label,y_pred) train_opti = tf.train.AdamOptimizer(1e-4).minimize(mse) saver = tf.train.Saver(max_to_keep=10) sess.run(tf.global_variables_initializer()) # Compute for epochs. all_train_steps = 0 all_test_steps = 0 if C.RESTORE_MODEL: saver.restore(sess, tf.train.latest_checkpoint(C.LOAD_MODEL_PATH)) # search for checkpoint file print("模型开始增量训练====>") graph = tf.get_default_graph() for i in range(C.EPOCHS): train_next_element = get_data(C.TRAIN_RECODER_PATH) test_next_element = get_data(C.TEST_RECODER_PATH) train_bacth_per_epoch = 0 while True: try: train_bacth_per_epoch += 1 all_train_steps += 1 train_mse_loss = get_loss(train_next_element, p1, p2, p3, label, mse, train_opti, all_train_steps, train_writer, mse_train_summary, mse_train_hist) print("Train Epoch {} Batch {} All steps {}: " "MSE ==> {}".format(i + 1, train_bacth_per_epoch, all_train_steps, train_mse_loss)) if all_train_steps % C.SAVE_FREQUENCY == 0: print("model is saved at all train step {}".format( all_train_steps)) saver.save(sess, C.REGTRSSION_MODEL_SAVE_PATH, global_step=C.MAX_STEP) except tf.errors.OutOfRangeError: break test_loss = 0 test_batchs = 0 while True: try: all_test_steps += 1 test_mse_loss = get_loss(test_next_element, p1, p2, p3, label, mse, train_opti, all_test_steps, test_writer, mse_test_summary, mse_test_hist) test_loss += test_mse_loss test_batchs += 1 print("Test batch {} : MSE ==> {}".format( test_batchs, test_mse_loss)) #tf.summary.histogram('test_mse_loss_steps', test_mse_loss) except tf.errors.OutOfRangeError: break test_loss_ave = test_loss / test_batchs print("Epoch {} : All Test Data Average MSE {}".format( i + 1, test_loss_ave)) test_writer.close() train_writer.close()
def train_on_gpu(): with tf.device("/cpu:0"): global_step = tf.train.get_or_create_global_step() tower_grads = [] p1 = tf.placeholder(tf.float32, [None, 20, 40, 3]) p2 = tf.placeholder(tf.float32, [None, 8, 10]) p3 = tf.placeholder(tf.float32, [None, 10]) label = tf.placeholder(tf.float32, [None, 1]) train_opti = tf.train.AdamOptimizer(1e-4) saver = tf.train.Saver(max_to_keep=10) with tf.variable_scope(tf.get_variable_scope()): for i in range(num_gpus): #with tf.device(assign_to_device('/gpu:{}'.format(i), ps_device='/cpu:0')): with tf.device('/gpu:{}'.format(i)): print( "assign_to_device====>>>>", assign_to_device('/gpu:{}'.format(i), ps_device='/cpu:0')) _x1 = p1[i * C.BATCH_SIZE:(i + 1) * C.BATCH_SIZE] _x2 = p2[i * C.BATCH_SIZE:(i + 1) * C.BATCH_SIZE] _x3 = p3[i * C.BATCH_SIZE:(i + 1) * C.BATCH_SIZE] _y = label[i * C.BATCH_SIZE:(i + 1) * C.BATCH_SIZE] y_pred = m.MyNet(_x1, _x2, _x3) tf.get_variable_scope().reuse_variables() mse = tf.losses.mean_squared_error(_y, y_pred) grads = train_opti.compute_gradients(mse) tower_grads.append(grads) grads = average_gradients(tower_grads) train_opti = train_opti.apply_gradients(grads) sess.run(tf.global_variables_initializer()) # Compute for epochs. all_train_steps = 0 all_test_steps = 0 if C.RESTORE_MODEL: saver.restore(sess, tf.train.latest_checkpoint(C.LOAD_MODEL_PATH)) #search for checkpoint file print("模型开始增量训练====>") graph = tf.get_default_graph() for i in range(C.EPOCHS): train_next_element = get_data_gpu(C.TRAIN_RECODER_PATH, num_gpus * C.BATCH_SIZE) test_next_element = get_data(C.TEST_RECODER_PATH) train_bacth_per_epoch = 0 while True: try: train_bacth_per_epoch += 1 all_train_steps += 1 mse_train_summary = tf.summary.scalar('train mse', mse) mse_test_summary = tf.summary.scalar('test mse', mse) mse_train_hist = tf.summary.histogram( 'train_mse_hist', mse) mse_test_hist = tf.summary.histogram('test_mse_hist', mse) train_mse_loss = get_loss(train_next_element, p1, p2, p3, label, mse, train_opti, all_train_steps, train_writer, mse_train_summary, mse_train_hist) if all_train_steps % 500 == 0: print("Train Epoch {} Batch {} All steps {}: " "MSE ==> {}".format(i + 1, train_bacth_per_epoch, all_train_steps, train_mse_loss)) if all_train_steps % C.SAVE_FREQUENCY == 0: # print("model is saved at all train step {}".format(all_train_steps)) saver.save(sess, C.REGTRSSION_MODEL_SAVE_PATH, global_step=C.MAX_STEP) except tf.errors.OutOfRangeError: break test_loss = 0 test_batchs = 0 while True: try: all_test_steps += 1 test_mse_loss = get_loss(test_next_element, p1, p2, p3, label, mse, train_opti, all_test_steps, test_writer, mse_test_summary, mse_test_hist) test_loss += test_mse_loss test_batchs += 1 # print("Test batch {} : MSE ==> {}".format(test_batchs, test_mse_loss)) # tf.summary.histogram('test_mse_loss_steps', test_mse_loss) except tf.errors.OutOfRangeError: break test_loss_ave = test_loss / test_batchs print("Epoch {} : All Test Data Average MSE {}".format( i + 1, test_loss_ave)) test_writer.close() train_writer.close()