我保存了训练模型。
预测代码如下:
get_serializer
我使用星图多进程来获取预测值:
def predict(predict_filename):
with tf.Session() as sess:
# normalization image
...
im_norm = tf.image.per_image_standardization(rgb_image)
sess = tf.InteractiveSession()
with sess.as_default():
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord, sess=sess)
def get_ckpt():
ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
return ckpt
ckpt = get_ckpt()
def get_saver():
new_saver = tf.train.import_meta_graph(ckpt.model_checkpoint_path + '.meta')
return new_saver
new_saver = get_saver()
if ckpt and ckpt.model_checkpoint_path:
print(ckpt.model_checkpoint_path)
new_saver.restore(sess, ckpt.model_checkpoint_path)
try:
while not coord.should_stop():
result = sess.run("Linear_regression/Add:0", feed_dict={'conv1/Reshape:0': image_new.eval(), 'dropout2/dropout/keep_prob:0': 0.2})
...
except tf.errors.OutOfRangeError:
print('Done training after reading all data')
finally: # When done, ask the threads to stop.
coord.request_stop()
# Wait for threads to finish.
# tf.reset_default_graph()
coord.join(threads)
sess.close()
# os._exit(0)
else:
print('no checkpoint found')
这里我只使用了一个过程。运行项目," 1.jpg"的预测值。可以成功,然后得到错误:
def main(argv=None):
filename = []
filename.append('./1.jpg')
filename.append('./2.jpg')
filename.append('./3.jpg')
filename = np.array(filename).astype(str)
print(filename)
pool = ThreadPool(1)
pool.starmap(predict, zip(filename))
pool.join()
pool.close()
我真的不知道为什么第二张图片的预测得到错误,但第一张图片能得到正确的结果?
当我在"最后"中添加os._exit(0)时,一切顺利,三张图片都可以得到正确的结果,为什么?
如果添加,如果我添加sess.run(tf.global_variables_initializer()),在预测第一张图片后,再获取日志:Traceback(最近一次调用最后一次):
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value file_queue/limit_epochs/epochs
[[Node: file_queue/limit_epochs/CountUpTo = CountUpTo[T=DT_INT64, _class=["loc:@file_queue/limit_epochs/epochs"], limit=40, _device="/job:localhost/replica:0/task:0/cpu:0"](file_queue/limit_epochs/epochs)]]