checkpoint_hook = tf.train.CheckpointSaverHook(checkpoint_dir=summary_path, save_steps=saving_step, scaffold=scaffold) config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True, gpu_options=(tf.GPUOptions(allow_growth=True))) session_creator = tf.train.ChiefSessionCreator(scaffold=scaffold, config=config, checkpoint_dir=summary_path) # Create a session for running operations in the Graph with tf.train.MonitoredSession(session_creator=session_creator, hooks=[checkpoint_hook]) as sess: print("🤖 Start testing...") step = 0 avg_loss = 0 while not sess.should_stop(): step += 1 l, pred, color, gray = sess.run([loss, predict_rgb, color_image_rgb, gray_image], feed_dict={is_training: False}) # Print batch loss print("📖 Testing iter %d, Minibatch Loss = %f" % (step, l)) avg_loss += l # Save all testing image for i in range(batch_size): summary_image = concat_images(gray[i], pred[i]) summary_image = concat_images(summary_image, color[i]) plt.imsave("%s/images/%d_%d.png" % (testing_summary, step, i), summary_image) if step >= len(file_paths) / batch_size: break print("🎉 Testing finished!") print("👀 Total average loss: %f" % (avg_loss / len(file_paths)))
# 创建一个会话来运行计算图的操作 with tf.train.MonitoredSession(session_creator=session_creator, hooks=[checkpoint_hook]) as sess: print("🤖 Start testing...") step = 0 avg_loss = 0 while not sess.should_stop(): step += 1 l, pred, color, gray = sess.run( [loss, predict_rgb, color_image_rgb, gray_image], feed_dict={is_training: False}) # Print batch loss print("📖 Testing iter %d, Minibatch Loss = %f" % (step, l)) avg_loss += l # 保存所有的测试图片 for i in range(len(color)): summary_image = concat_images(gray[i], pred[i]) # summary_image = concat_images(summary_image, color[i]) plt.imsave("%s/images/%d_%d.png" % (testing_summary, step, i), summary_image) # plt.imsave("%s/images/gray/%d_%d.jpeg" % (testing_summary, step, i), gray[i]) if step >= len(file_paths) / batch_size: break print("🎉 Testing finished!") print("👀 Total average loss: %f" % (avg_loss / len(file_paths)))
[ cost, predict_rgb, color_image_rgb, gray_image_rgb, merged ], feed_dict={ is_training: False, uv: 3 }) print "Iter %d, Minibatch Loss = %f" % ( step, float(np.mean(loss))) train_writer.add_summary(summary, step) train_writer.flush() # Save test image if step % test_step == 0: summary_image = concat_images(gray[0], pred[0]) summary_image = concat_images(summary_image, color[0]) plt.imsave( "%s/images/%s.jpg" % (train_summary, str(step)), summary_image) # Save model if step % save_step == 0 and step != 0: save_path = saver.save(sess, "%s/model.ckpt" % model_path) print "Model saved in file: %s" % save_path # Stop training if step == training_iters: break print "Training Finished!"
avg_error = 0 while not coord.should_stop(): step += 1 # Print batch loss if step % display_step == 0: loss, pred, pred_rgb, color_rgb, gray_rgb, summary = \ sess.run([cost, predict, predict_rgb, color_image_rgb, gray_image_rgb, merged], feed_dict={is_training: False, uv: 3}) print "Iter %d, Minibatch Loss = %f" % ( step, float(np.mean(loss))) avg_error += float(np.mean(loss)) test_writer.add_summary(summary, step) test_writer.flush() # Save output image summary_image = concat_images(gray_rgb[0], pred_rgb[0]) summary_image = concat_images(summary_image, color_rgb[0]) plt.imsave("%s/images/%s.jpg" % (test_summary, str(step)), summary_image) if step == len(file_paths): break print "Testing Finished!" print "Average error: %f" % (avg_error / len(file_paths)) sys.stdout.flush() except tf.errors.OUT_OF_RANGE as e: # Handle exception print "Done training -- epoch limit reached" coord.request_stop(e)