def run_training(): # 模型参数保存路径 logs_train_dir = 'logs/train/' # 读取训练集与测试集数据 data_resource = input_data.DataProcessor('images', 'image_labels_dir', 'labels.txt') training_data_set = data_resource.get_dataset(IMG_W, IMG_H, BATCH_SIZE, 'training') training_iterator = training_data_set.make_initializable_iterator() testing_data_set = data_resource.get_dataset(IMG_W, IMG_H, BATCH_SIZE, 'testing') testing_iterator = testing_data_set.make_initializable_iterator() # 生成batch test_batch, test_label_batch = testing_iterator.get_next() train_batch, train_label_batch = training_iterator.get_next() train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.training(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) # log汇总 summary_op = tf.summary.merge_all() # 产生会话 sess = tf.Session() # 产生一个writer写log文件 train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) # 产生saver来存储训练好的模型 saver = tf.train.Saver() # 所有节点初始化 sess.run(tf.global_variables_initializer()) sess.run(training_iterator.initializer) for step in np.arange(MAX_STEP): # 执行MAX_STEP步训练,一步一个batch try: _, tra_loss, tra_acc, summary_str = sess.run( [train_op, train_loss, train__acc, summary_op]) except tf.errors.OutOfRangeError: # 训练完一个epoch会到这里 sess.run(training_iterator.initializer) _, tra_loss, tra_acc, summary_str = sess.run( [train_op, train_loss, train__acc, summary_op]) if step % 20 == 0: print('Step %d, train loss = %.6f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) # 验证模型,只测了一个batch test_logits = model.inference(test_batch, BATCH_SIZE, N_CLASSES, True) test_loss = model.losses(test_logits, test_label_batch) test__acc = model.evaluation(test_logits, test_label_batch) sess.run(testing_iterator.initializer) print(sess.run([test_loss, test__acc])) sess.close()
def train(): """ Train model :return: """ X = tf.placeholder(tf.float32, [ cfg.CFG['batch_size'], cfg.CFG['image_size'], cfg.CFG['image_size'], cfg.CFG['image_channel'] ], name='train-input') Y = tf.placeholder(tf.float32, [None], name='label-input') train, train_label = generator.get_files('./dataset/') train_batch, train_label_batch = generator.get_batches( train, train_label, cfg.CFG["image_size"], cfg.CFG["image_size"], cfg.CFG["batch_size"], 20) train_logits = model.model(train_batch, cfg.CFG["batch_size"], cfg.CFG["classes"]) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, cfg.CFG["learning_rate"]) train_acc = model.evaluation(train_logits, train_label_batch) sess = tf.Session() saver = tf.train.Saver(max_to_keep=5) sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: ptar = tqdm(range(50)) for iter in ptar: if coord.should_stop(): break image_data, label = sess.run([train_batch, train_label_batch]) # _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc]) _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc], feed_dict={ X: image_data, Y: label }) ptar.set_description( "iterative %d times,train loss=%.4f train accuracy = %.4f" % (iter, tra_loss, tra_acc)) checkpoint_path = cfg.CFG[ 'model_path'] + 'flame_dector_%.4f.ckpt' % tra_acc # print(checkpoint_path) saver.save(sess, checkpoint_path, global_step=iter) except tf.errors.OutOfRangeError: print('Done training') finally: coord.request_stop() coord.join(threads)
def run_training(): traindir = 'data/train/' logs_train_dir = 'logs/train/' train_image, train_label = input_data.get_files(traindir) train_batch, train_label_batch = input_data.get_batch( train_image, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.training(train_loss, learning_rate) train_acc = model.evaluation(train_logits, train_label_batch) sess = tf.Session() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in range(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc]) if step % 20 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def train(): train_batch, label_batch = id.read_and_save() train_batch = tf.cast(train_batch, dtype=tf.float32) label_batch = tf.cast(label_batch, dtype=tf.int64) keep_prob = tf.placeholder(tf.float32) logits = model.inference(train_batch, keep_prob) loss = model.losses(logits, label_batch) op = model.training(loss=loss) accuracy = model.evaluation(logits, label_batch) saver = tf.train.Saver() summary_op = tf.summary.merge_all() with tf.Session() as sess: train_writer = tf.summary.FileWriter(LOG_DIR, graph=sess.graph) sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runner(sess=sess, coord=coord) try: for step in range(MAX_STEP): _, train_loss, train_acc = sess.run([op, loss, accuracy], feed_dict={keep_prob: 0.75}) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f' % (step, train_loss, train_acc)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(LOG_DIR, "model.ckpt") saver.save(sess, checkpoint_path) except tf.errors.OutOfRangeError: print('An error occur') finally: coord.request_stop() coord.join(threads=threads)
def evaluate(tf_file, logs_test_dir, restore_dir, loss_dir, weight_write_dir): filenames = tf.placeholder(tf.string, shape=[None]) validation_filenames = tf_file iterator = data.read_and_decode(filenames, BATCH_SIZE, False) sess = tf.Session() sess.run(iterator.initializer, feed_dict={filenames: validation_filenames}) test_img, test_label = iterator.get_next() test_logits, test_weight = model.inference(test_img) test_loss = model.losses(test_logits, test_label) summary_op = tf.summary.merge_all() train_writer = tf.summary.FileWriter(logs_test_dir, sess.graph) saver = tf.train.Saver() saver.restore(sess, restore_dir) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in range(MAX_STEP): if coord.should_stop(): break test_logits_val, test_loss_val, test_label_val = sess.run([test_logits, test_loss, test_label]) print('label:', test_label_val) print('estimate:', test_logits_val) print('Step %d, test loss = %.2f' % (step + 1, test_loss_val)) list_loss.append(test_loss_val) with open(loss_dir, 'a') as f: f.write('%.6f' % (test_loss_val)) f.write("\n") tra_weight = sess.run(test_weight) print(tra_weight.shape) for j in range(tra_weight.shape[0]): tra_weight_draw = tra_weight[j, :, :, :] tra_weight_draw = (tra_weight_draw - tra_weight_draw.min()) / \ (tra_weight_draw.max() - tra_weight_draw.min()) tra_weight_draw *= 255.0 tra_weight_draw = cv.resize(tra_weight_draw, (600, 400)) cv.imwrite(weight_write_dir.format(step), tra_weight_draw.astype(np.uint8)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_test_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print('mean', sess.run(tf.reduce_sum(list_loss) / MAX_STEP)) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close() cv.waitKey(0)
def run_training(): # 数据集 train_dir = r'D:\Python\mnist\test_my\big_0_1_4/' # My dir--20170727-csq # logs_train_dir 存放训练模型的过程的数据,在tensorboard 中查看 logs_train_dir = r'D:\PyCharm_code\Ai\Tensorflow_mooc_note\6\MinstNew\logs\train/' # 获取图片和标签集 train, train_label = input_data.get_files(train_dir) # 生成批次 train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) # 进入模型 train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) # 获取 loss train_loss = model.losses(train_logits, train_label_batch) # 训练 train_op = model.trainning(train_loss, learning_rate) # 获取准确率 train__acc = model.evaluation(train_logits, train_label_batch) # 合并 summary summary_op = tf.summary.merge_all() sess = tf.Session() # 保存summary train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): ckpt = tf.train.get_checkpoint_state(logs_train_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: # 每隔2000步保存一下模型,模型保存在 checkpoint_path 中 checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): # dataset train_dir = '/Users/xcliang/PycharmProjects/cats_vs_dogs/data/train/' # My dir--20170727-csq # logs_train_dir store the data of the process of training model, view in tensorbpard logs_train_dir = '/Users/xcliang/PycharmProjects/cats_vs_dogs/data/saveNet' # Get images and tag sets train, train_label = input_data.get_files(train_dir) # Generate batch train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) # Entering the model train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) # Get loss train_loss = model.losses(train_logits, train_label_batch) # train train_op = model.trainning(train_loss, learning_rate) # Get accuracy train__acc = model.evaluation(train_logits, train_label_batch) # merge summary summary_op = tf.summary.merge_all() sess = tf.Session() # save summary train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: # Save the model every 2000 steps and save the model in checkpoint_path checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): # you need to change the directories to yours. s_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+YuanTu' T_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+X_mid' logs_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+' s_train, s_train_label = input_data.get_files(s_train_dir) s_train_batch, s_train_label_batch = input_data.get_batch( s_train, s_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) T_train, T_train_label = input_data.get_files(T_train_dir) T_train_batch, T_train_label_batch = input_data.get_batch( T_train, T_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(s_train_batch, T_train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, s_train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, s_train_label_batch) summary_op = tf.summary.merge_all() #汇总操作 sess = tf.Session() #定义sess train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) # saver = tf.train.Saver() #保存操作 sess.run(tf.global_variables_initializer()) #初始化所有变量 coord = tf.train.Coordinator() #设置多线程协调器 threads = tf.train.start_queue_runners( sess=sess, coord=coord) #开始Queue Runners(队列运行器) #开始训练过程 try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) #运行汇总操作,写入汇总 summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 800 == 0 or (step + 1) == MAX_STEP: #保存当前模型和权重到 logs_train_dir,global_step为当前迭代次数 checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): ''' ''' ''' tf.train.Coordinator和tf.train.start_queue_runners貌似都要在sess.run之前使用,不然会无法运行 try:这些语法貌似是模板,直接使用就好 ''' # you need to change the directories to yours. train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/' train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() #和下面的queue_runners配合使用,发生错误可以正确关闭线程 threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: # When done, ask the threads to stop. coord.request_stop() # Wait for threads to finish. coord.join(threads) sess.close()
def evaluate(): test_img, test_label = data.test_read_and_decode([tf_file3]) test_img_batch, test_label_batch = data.test_get_batch(test_img, test_label, BATCH_SIZE, CAPACITY) test_logits, test_weight = model.inference(test_img_batch) test_loss = model.losses(test_logits, test_label_batch) summary_op = tf.summary.merge_all() with tf.Session() as sess: # sess.run(tf.global_variables_initializer()) # load_parameters("Alexnet.npy", sess, 'Alexnet') train_writer = tf.summary.FileWriter(logs_test_dir, sess.graph) saver = tf.train.Saver() saver.restore(sess, './logs/train12/model.ckpt-9999') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in range(MAX_STEP): if coord.should_stop(): break test_logits_val, test_loss_val, test_label_val = sess.run([test_logits, test_loss, test_label]) print('label:', test_label_val) print('estimate:', test_logits_val) print('Step %d, test loss = %.2f' % (step+1, test_loss_val)) list_loss.append(test_loss_val) with open("./color31.txt", 'a') as f: f.write('%.6f' % (test_loss_val)) f.write("\n") tra_weight = sess.run(test_weight) print(tra_weight.shape) for j in range(tra_weight.shape[0]): tra_weight_draw = tra_weight[j, :, :, :] tra_weight_draw = (tra_weight_draw - tra_weight_draw.min()) / \ (tra_weight_draw.max() - tra_weight_draw.min()) tra_weight_draw *= 255.0 tra_weight_draw = cv.resize(tra_weight_draw, (600, 400)) cv.imwrite("/home/mvl/Dataset/ColorConstancy/Cube/test3_v/weight{}.jpg".format(step), tra_weight_draw.astype(np.uint8)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_test_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print('mean', sess.run(tf.reduce_sum(list_loss)/MAX_STEP)) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close() cv.waitKey(0)
def run_training(): #train set path train_dir = '/raidHDD/experimentData/Dev/Knife/hackthon/upup7/' #output model path logs_train_dir = '/raidHDD/experimentData/Dev/Knife/hackthon/modelX' if removeLogFIle: if os.path.exists(logs_train_dir): for logFile in os.listdir(logs_train_dir): os.remove("{0}/{1}".format(logs_train_dir, logFile)) print("Delete Log file success...") train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.8 #0.8 =GPU_memory usage sess = tf.Session(config=config) train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) train_writer.flush() #only save end model if (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): train_dir = "/home/sxy/PycharmProjects/defect2/data/train" logs_train_dir = "/home/sxy/PycharmProjects/defect2/logs/train-4" tf.reset_default_graph() train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train_acc = model.evaluation(train_logits, train_label_batch) #测试准确率 # test_dir="/home/sxy/PycharmProjects/defect2/data/test" # test,test_label=input_data.get_files(test_dir) # test_batch,test_label_batch=input_data.get_batch(test, # test_label, # IMG_W, # IMG_H, # BATCH_SIZE, # CAPACITY) # test_logits = model.inference(test_batch, BATCH_SIZE, N_CLASSES) # train_loss = model.losses(test_logits, test_label_batch) # train_op = model.trainning(train_loss, learning_rate) # train_acc = model.evaluation(test_logits, test_label_batch) summary_op = tf.merge_all_summaries() sess = tf.Session() train_writer = tf.train.SummaryWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.initialize_all_variables()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc]) if step % 50 == 0: print("Step %d, train loss = %.2f, train accuracy = %.2f%%" % (step, tra_loss, tra_acc)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, "model.ckpt") saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print("Done training -- epoch limit reached.") finally: coord.request_stop() coord.join(threads) sess.close()
def main(_): # train raw_data and label train_data, train_label = input_data.get_files(WORK_DIRECTORY) train_batch, train_label_batch = input_data.next_batch( train_data, train_label) # define train op train_logits = model.inference(train_batch, N_LABELS, BATCH_SIZE) train_loss = model.losses(train_logits, train_label_batch) train_op = model.optimization(train_loss, LEARNING_RATE) train_acc = model.evaluation(train_logits, train_label_batch) # start logs summary_op = tf.summary.merge_all() # Save logs saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) # writen to logs train_writer = tf.summary.FileWriter(LOGS_DIRECTORY, sess.graph) # queue monitor coord = tf.train.Coordinator() # threads = tf.train.start_queue_runners(sess=sess, coord=coord) # train try: for step in np.arange(MAX_STEP): if coord.should_stop(): break # start op node _, loss, accuracy = sess.run([train_op, train_loss, train_acc]) # print and write to logs. if step % 2 == 0: print( f"Step [{step}/{MAX_STEP}] Loss {loss} Accuracy {accuracy * 100.0:.2f}%" ) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step == MAX_STEP: # Save logs checkpoint_path = os.path.join(LOGS_DIRECTORY, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) break print(f"Model saved! Global step = {step}") except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() sess.close()
def run_training(): starttime = datetime.datetime.now() # you need to change the directories to yours. train_dir = 'D:\\Thesis\\sample_patch\\6110_4_0\\*\\*.tif' logs_train_dir = 'D:\\Thesis\\classify\\6110_4_0\\logs\\train' train_image, train_label= input_data.new_getfiles(train_dir) train_image = input_data.read_images(train_image,R_Size) train_batch, train_label_batch = input_data.get_batch(train_image, train_label, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() # sess = tf.Session() config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) # allowing dynamic memory growth as follows train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) test = coord.should_stop() try: for step in np.arange(MAX_STEP): if test: break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 1 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 100 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() # coord.join(threads) sess.close() endtime=datetime.datetime.now() print(endtime-starttime).seconds
def run_training(): # 调用input_data文件的get_files()函数获得image_list, label_list train, train_label = input_data.get_files(train_dir) # 获得image_batch, label_batch train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) # 进行前向训练,获得回归值 train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) # 计算获得损失值loss train_loss = model.losses(train_logits, train_label_batch) # 对损失值进行优化 train_op = model.trainning(train_loss, learning_rate) # 根据计算得到的损失值,计算出分类准确率 train__acc = model.evaluation(train_logits, train_label_batch) # 将图形、训练过程合并在一起 summary_op = tf.summary.merge_all() # 新建会话 sess = tf.Session() # 将训练日志写入到logs_train_dir的文件夹内 train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) # 保存变量 saver = tf.train.Saver() # 执行训练过程,初始化变量 sess.run(tf.global_variables_initializer()) # 创建一个线程协调器,用来管理之后在Session中启动的所有线程 coord = tf.train.Coordinator() # 启动入队的线程,一般情况下,系统有多少个核,就会启动多少个入队线程(入队具体使用多少个线程在tf.train.batch中定义); threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): # 使用 coord.should_stop()来查询是否应该终止所有线程,当文件队列(queue)中的所有文件都已经读取出列的时候, # 会抛出一个 OutofRangeError 的异常,这时候就应该停止Sesson中的所有线程了; if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) # 每50步打印一次损失值和准确率 if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) # 每2000步保存一次训练得到的模型 if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) # 如果读取到文件队列末尾会抛出此异常 except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() # 使用coord.request_stop()来发出终止所有线程的命令 coord.join(threads) # coord.join(threads)把线程加入主线程,等待threads结束
def run_training(): # you need to change the directories to yours. train_dir = './data/train/train/' logs_train_dir = './logs/' train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) with tf.name_scope("training"): train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run( [train_op, train_loss, train__acc]) if step % 20 == 0: print( 'Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) #保存模型和模型参数到logs_train_dir文件夹 except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def evaluate_all_image(): start_time = time.time() ''' Test all image against the saved models and parameters. Return global accuracy of test_image_set ############################################## ##Notice that test image must has label to compare the prediction and real ############################################## ''' # you need to change the directories to yours. test_dir = '/Users/sherry/Documents/Study/CS 6220/HW01/HW01_20190901/01_cats_vs_dogs/data/outlier_test/' N_CLASSES = 2 print('-------------------------') test, test_label = input_data.get_files(test_dir) BATCH_SIZE = len(test) print('There are %d test images totally..' % BATCH_SIZE) print('-------------------------') test_batch, test_label_batch = input_data.get_batch( test, test_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) logits = model.inference(test_batch, BATCH_SIZE, N_CLASSES) testloss = model.losses(logits, test_label_batch) testacc = model.evaluation(logits, test_label_batch) logs_train_dir = '/Users/sherry/Documents/Study/CS 6220/HW01/HW01_20190901/01_cats_vs_dogs/data/logs_dataset1/train/' saver = tf.train.Saver() with tf.Session() as sess: print("Reading checkpoints...") ckpt = tf.train.get_checkpoint_state(logs_train_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('No checkpoint file found') print('-------------------------') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) test_loss, test_acc = sess.run([testloss, testacc]) print('The model\'s loss is %.2f' % test_loss) correct = int(BATCH_SIZE * test_acc) print('Correct : %d' % correct) print('Wrong : %d' % (BATCH_SIZE - correct)) print('The accuracy in test images are %.2f%%' % (test_acc * 100.0)) print( "------------------------ Testing time is: %s seconds -----------------------" % (time.time() - start_time)) coord.request_stop() coord.join(threads) sess.close() print("--- Testing time is: %s seconds ---" % (time.time() - start_time))
def run_training(): train_dir = './train/' # 加载数据训练 logs_train_dir = './save_model/' # 储存训练好的位置 train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES,True) # forward pass train_loss = model.losses(train_logits, train_label_batch) # 设置损失函数 train_op = model.trainning(train_loss,learning_rate=) # training train__acc = model.evaluation(train_logits, train_label_batch) # 验证正确率 summary_op = tf.summary.merge_all() # 定义合并变量操作,一次性生成所有摘要数据 sess = tf.Session() # 初始化会话 train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) # TensorBoard的记录 saver = tf.train.Saver() # 储存模型 sess.run(tf.global_variables_initializer()) # 所有变量初始化 coord = tf.train.Coordinator() # 多线程 threads = tf.train.start_queue_runners(sess=sess, coord=coord) csvfile = open('csv.csv', 'w', newline='') writer = csv.writer(csvfile) writer.writerow(['name','label']) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training1(): # you need to change the directories to yours. # train_dir = '/home/hjxu/PycharmProjects/01_cats_vs_dogs/data/train/' logs_train_dir = '/home/scy/eclipse-workspace/PythonProject/src/12yue3ri/logs/' # # train, train_label = input_data.get_files(train_dir) tfrecords_file = '/home/scy/eclipse-workspace/PythonProject/src/12yue3ri/my_train2.tfrecords' train_batch, train_label_batch = cr.read_and_decode(tfrecords_file, batch_size=BATCH_SIZE) train_batch = tf.cast(train_batch, dtype=tf.float32) train_label_batch = tf.cast(train_label_batch, dtype=tf.int64) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 5 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) saver.save(sess, 'net/mydataset_net.ckpt') # if step % 4 == 0 or (step + 1) == MAX_STEP: # checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') # saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training1(): train_dir = 'G:/Python/dogorcat/data/train' tfrecords_dir = 'tfrecords/' tfrecords_file = 'test.tfrecords' logs_train_dir = 'logs/recordstrain/' # images, labels = cr.get_files(train_dir) # cr.convert_to_tfrecord(images, labels, tfrecords_dir, tfrecords_file) train_batch, train_label_batch = cr.read_and_decode(tfrecords_dir + tfrecords_file, batch_size=BATCH_SIZE) train_batch = tf.cast(train_batch, dtype=tf.float32) train_label_batch = tf.cast(train_label_batch, dtype=tf.int64) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.training(train_loss, learning_rate) train_acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def training_preload(model_name, labelNames, t_batch_train, t_batch_evalu, log_train_dir, max_step, learning_rate): # Getting all training required operations t_op_logits, t_op_pred, classes = model.classification_inference( t_batch_train[0], labelNames, model_name) t_op_loss = model.losses(t_op_logits, t_batch_train[1]) t_op_acc = model.evaluation(t_op_logits, t_batch_train[1]) t_op_train = model.training(t_op_loss, learning_rate) model.writeSummaries(t_op_acc, t_op_loss, scope='training') t_op_summary = tf.summary.merge_all() with tf.Session() as sess: # Summary for tensorboard summary_writer = tf.summary.FileWriter(logdir=log_train_dir, graph=sess.graph, filename_suffix='training') # Saver for saving model saver = tf.train.Saver() # Initialize variables sess.run(tf.global_variables_initializer()) # Tensorflow Thread control coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in range(1, max_step + 1): if coord.should_stop(): break _, tra_loss, tra_acc, summary_str = sess.run( [t_op_train, t_op_loss, t_op_acc, t_op_summary]) summary_writer.add_summary(summary_str, step) if step % 100 == 0 or step == 1: print('\n') print( 'Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) print('', end='', flush=True) if step % 200 == 0 or step == max_step: checkpoint_path = os.path.join(log_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print('.', end='', flush=True) except tf.errors.OutOfRangeError: print('Done training -- step limit reached') finally: coord.request_stop() coord.join(threads)
def train(): img, label = data.read_and_decode("train.tfrecords") img_batch, label_batch = data.get_batch(img, label, BATCH_SIZE, CAPACITY) train_logits = model.inference(img_batch) # s = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'ALexNet') # print(s) # for v in s: # print(v.name) # exit(0) train_loss = model.losses(train_logits, label_batch) train_op = model.trainning(train_loss, learning_rate) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() with tf.Session() as sess: coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) im, la = sess.run([img_batch, label_batch]) print(im.shape, la.shape) cv2.imshow("asdas", im[0, :, :, :]) cv2.waitKey(0) exit(0) try: img, label = sess.run([img_batch, label_batch]) for step in range(MAX_STEP): if coord.should_stop(): break _, tra_loss = sess.run([train_op, train_loss]) if step % 50 == 0: print('Step %d, train loss = %.2f' % (step, tra_loss)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_train(image_batch, label_batch, n_class, batch_size, checkpoint_dir, lr, MAX_STEP): print('{:*^70}'.format('【train starting!】')) global result if args.inference == '2D': result = inference(image_batch, batch_size, n_class) elif args.inference == '1D': result = inference1D(image_batch, batch_size, n_class) else: print('【Unknown error】') print('{:*^70}'.format('DEBUG')) train_loss = losses(result[-1], label_batch) train_op = trainning(train_loss, lr) train__acc = evaluation(result[-1], label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(checkpoint_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(checkpoint_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close() print('{:*^70}'.format(' train ending! '))
def run_training(): DIR_PRE = os.getcwd() + '/' train_dir = DIR_PRE + 'data/train/' logs_train_dir = DIR_PRE + 'logs/train/' os.makedirs(train_dir, exist_ok=True) os.makedirs(logs_train_dir, exist_ok=True) # 获取所有图片文件列表和对应的标签列表 train, train_label = input_data.get_files(train_dir) # train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): # you need to change the directories to yours. train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/' train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): train_dir = "./data/TRAIN/" logs_train_dir = "./logs/" train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch(train,train_label,IMG_W,IMG_H,BATCH_SIZE,CAPACITY) train_logits = model.inference(train_batch,BATCH_SIZE,N_CLASSES) train_loss = model.losses(train_logits,train_label_batch) train_op = model.trainning(train_loss, learning_rate) train_acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc= sess.run([train_op, train_loss, train_acc]) if step % 100 == 0: print('Step:', step, 'train loss:', tra_loss, 'train accuracy:', tra_acc) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if tra_acc > 0.95 and step>6000: checkpoint_path = os.path.join(logs_train_dir, "model") saver.save(sess, checkpoint_path, global_step=step) print("train success!") print('Step:', step, 'train loss:', tra_loss, 'train accuracy:', tra_acc) coord.request_stop() except tf.errors.OutOfRangeError: print("Done training -- epoch limit reached.") finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): # you need to change the directories to yours. # train_dir = '/home/kevin/tensorflow/hams_vs_hots/data/train/' train_dir = 'D:/workspace/uploadPicJudge3Class/train/' # logs_train_dir = '/home/kevin/tensorflow/hams_vs_hots/logs/train/' logs_train_dir = 'D:/workspace/uploadPicJudge3Class/logs/' train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 10 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0) + ' ' + datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S')) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 100 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def train(): with tf.device('/cpu:0'): img, label = data.read_and_decode([tf_file1, tf_file2]) img_batch, label_batch = data.get_batch(img, label, BATCH_SIZE, CAPACITY) train_logits, train_weight = model.inference(img_batch) # s = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'ALexNet') # print(s) # for v in s: # print(v.name) # exit(0) train_loss = model.losses(train_logits, label_batch) train_op = model.trainning(train_loss, learning_rate) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) load_parameters("Alexnet.npy", sess, 'Alexnet') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in range(MAX_STEP): if coord.should_stop(): break _, tra_loss = sess.run([train_op, train_loss]) if step % 50 == 0: # tra_weight.imshow() print('Step %d, train loss = %.2f, l2 loss = %.2f' % (step, tra_loss, tra_loss)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): train_dir = 'C://Users/Sizhe/Desktop/CatsvsDogs/data/train/' logs_train_dir = 'C://Users/Sizhe/Desktop/CatsvsDogs/data/logs/train/' train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch(train, train_label, image_width, image_height, batch_size, capacity) train_logits = model.inference(train_batch, batch_size, n_class) train_loss = model.losses(train_logits, train_label_batch) train_op = model.training(train_loss, learning_rate) train_acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir,sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess = sess, coord = coord) try: for step in np.arange(max_step): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc]) ### step%50 when training if step%50 == 0: print ('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.00)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step%2000 == 0 or step == max_step-1: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step= step) except tf.errors.OutOfRangeError: print ('Training finished -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(w1, b1, w2, b2, w3, b3, w4, b4, w5, b5): train, train_label = get_data.get_zacao_input(train_dir, label_dir) train_batch, train_label_batch = get_data.get_batch_jpg( train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES, w1, b1, w2, b2, w3, b3, w4, b4, w5, b5) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) step = 0 ckpt = tf.train.get_checkpoint_state(logs_train_dir) if ckpt and ckpt.model_checkpoint_path: step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %d' % step) step += 1 try: while step < MAX_STEP: if coord.should_stop(): break _, tra_loss, tra_acc, summary_str = sess.run( [train_op, train_loss, train__acc, summary_op]) train_writer.add_summary(summary_str, step) if step % 50 == 0 or step + 1 == MAX_STEP: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0)) if step % 1000 == 0 or step + 1 == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model') saver.save(sess, checkpoint_path, global_step=step) step += 1 except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): print 'let us begin....' train_dir = '../data/train/' logs_train_dir = './train/' train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch( train, train_label, IMG_H, IMG_W, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, lr) train_acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() #???? sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): print 'coord stop!' break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc]) if step % 50 == 0: print 'Step: %d, train_loss = %.2f, train_accuracy = %.2f\n' % ( step, tra_loss, tra_acc * 100.0) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2500 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def run_training(): # you need to change the directories to yours. s_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+YuanTu' T_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+X_mid' logs_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+' s_train, s_train_label = input_data.get_files(s_train_dir) s_train_batch, s_train_label_batch = input_data.get_batch(s_train, s_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) T_train, T_train_label = input_data.get_files(T_train_dir) T_train_batch, T_train_label_batch = input_data.get_batch(T_train, T_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(s_train_batch,T_train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, s_train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, s_train_label_batch) summary_op = tf.summary.merge_all() #汇总操作 sess = tf.Session() #定义sess train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) # saver = tf.train.Saver() #保存操作 sess.run(tf.global_variables_initializer())#初始化所有变量 coord = tf.train.Coordinator() #设置多线程协调器 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #开始Queue Runners(队列运行器) #开始训练过程 try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0)) #运行汇总操作,写入汇总 summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 800 == 0 or (step + 1) == MAX_STEP: #保存当前模型和权重到 logs_train_dir,global_step为当前迭代次数 checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def training(): train, train_label, val, val_label = input_data.get_files(train_dir, RATIO) train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) val_batch, val_label_batch = input_data.get_batch(val, val_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) loss = model.losses(logits, train_label_batch) train_op = model.trainning(loss, learning_rate) acc = model.evaluation(logits, train_label_batch) x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3]) y_ = tf.placeholder(tf.int16, shape=[BATCH_SIZE]) with tf.Session() as sess: saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess= sess, coord=coord) summary_op = tf.summary.merge_all() train_writer = tf.summary.FileWriter(train_logs_dir, sess.graph) val_writer = tf.summary.FileWriter(val_logs_dir, sess.graph) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break tra_images,tra_labels = sess.run([train_batch, train_label_batch]) _, tra_loss, tra_acc = sess.run([train_op, loss, acc], feed_dict={x:tra_images, y_:tra_labels}) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 200 == 0 or (step + 1) == MAX_STEP: val_images, val_labels = sess.run([val_batch, val_label_batch]) val_loss, val_acc = sess.run([loss, acc], feed_dict={x:val_images, y_:val_labels}) print('** Step %d, val loss = %.2f, val accuracy = %.2f%% **' %(step, val_loss, val_acc*100.0)) summary_str = sess.run(summary_op) val_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(train_logs_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads)
s_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+YuanTu' T_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+X_mid' logs_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+' s_train, s_train_label = input_data.get_files(s_train_dir) #print(s_train) s_train_batch, s_train_label_batch = input_data.get_batch(s_train, s_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) #print(s_train_label_batch) T_train, T_train_label = input_data.get_files(T_train_dir) T_train_batch, T_train_label_batch = input_data.get_batch(T_train, T_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) #print(len(s_train),len(s_train_label),len(T_train),len(T_train_label)) #print(s_train_batch,s_train_label_batch,T_train_batch,s_train_label_batch) train_logits = model.inference(s_train_batch,T_train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, s_train_label_batch) train_op = model.trainning(train_loss, learning_rate) #correct_prediction = tf.equal(tf.argmax(train_logits,1),tf.argmax(y_,1)) labels_max = tf.reduce_max(s_train_label_batch) sess = tf.Session() print(sess.run(labels_max))