当我运行tensorflow程序时,我遇到了一些麻烦。这是我的代码:
# -*- coding: utf-8 -*-
import os
import numpy as np
import tensorflow as tf
import input_train_val_split
import model
N_CLASSES = 2
IMG_W = 208 # resize the image, if the input image is too large, training will be very slow.
IMG_H = 208
RATIO = 0.2 # take 20% of dataset as validation data
BATCH_SIZE = 64
CAPACITY = 2000
MAX_STEP = 6000 # with current parameters, it is suggested to use MAX_STEP>10k
learning_rate = 0.0001 # with current parameters, it is suggested to use learning rate<0.0001
def run_training():
# you need to change the directories to yours.
train_dir = '/home/wchzh/Desktop/dataset/kaggle_cats_and_dogs/train/'
logs_train_dir = '/home/wchzh/Desktop/cnn/3/cats_vs_dogs_2/logs/train/'
logs_val_dir = '/home/wchzh/Desktop/cnn/3/cats_vs_dogs_2/logs/val/'
train, train_label, val, val_label = input_train_val_split.get_files(train_dir, RATIO)
train_batch, train_label_batch = input_train_val_split.get_batch(train,
train_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
val_batch, val_label_batch = input_train_val_split.get_batch(val,
val_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE])
logits = model.inference(x, BATCH_SIZE, N_CLASSES)
loss = model.losses(logits, y_)
acc = model.evaluation(logits, y_)
train_op = model.training(loss, learning_rate)
with tf.Session() as sess:
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(logs_train_dir, sess.graph)
val_writer = tf.summary.FileWriter(logs_val_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(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)
if __name__ == '__main__':
run_training()
错误显示在此处:
Step 0, train loss = 0.69, train accuracy = 54.69%
2018-01-29 15:15:51.342678: W tensorflow/core/kernels/queue_base.cc:295] _0_input_producer/input_producer: Skipping cancelled enqueue attempt with queue not closed
2018-01-29 15:15:51.342774: W tensorflow/core/kernels/queue_base.cc:295] _2_input_producer_1/input_producer: Skipping cancelled enqueue attempt with queue not closed
Traceback (most recent call last):
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1323, in _do_call
return fn(*args)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1302, in _run_fn
status, run_metadata)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [64,208,208,3]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[64,208,208,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: Placeholder_1/_535 = _HostRecv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_45_Placeholder_1", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/wchzh/Desktop/cnn/3/cats_vs_dogs_2/other/train_and_val.py", line 90, in <module>
run_training()
File "/home/wchzh/Desktop/cnn/3/cats_vs_dogs_2/other/train_and_val.py", line 68, in run_training
summary_str = sess.run(summary_op)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 889, in run
run_metadata_ptr)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1317, in _do_run
options, run_metadata)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [64,208,208,3]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[64,208,208,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: Placeholder_1/_535 = _HostRecv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_45_Placeholder_1", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'Placeholder', defined at:
File "/home/wchzh/Desktop/cnn/3/cats_vs_dogs_2/other/train_and_val.py", line 90, in <module>
run_training()
File "/home/wchzh/Desktop/cnn/3/cats_vs_dogs_2/other/train_and_val.py", line 41, in run_training
x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1599, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3091, in _placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
op_def=op_def)
File "/home/wchzh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [64,208,208,3]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[64,208,208,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: Placeholder_1/_535 = _HostRecv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_45_Placeholder_1", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Process finished with exit code 1
但是,我已将值提供给 x
和 y_
,而步骤0 是更正显示,但程序显示错误
**InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [64,208,208,3] .
我不知道如何处理错误,** 谢谢你回答这个问题!
答案 0 :(得分:0)
您正在使用merge-all摘要,而不会在
中传递占位符的值summary_str = sess.run(summary_op)
您需要在训练操作中传递feed_dict
内的值