InvalidArgumentError:您必须使用dtype float和shape [1,640,1024,3]

时间:2017-07-27 03:23:12

标签: python python-2.7 tensorflow

当我使用TensorFlow时,我将数据style_img提供给图表,并显示InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [1,640,1024,3]
但是,我将占位符定义为input_img = tf.placeholder(dtype=style_img.dtype, shape=style_img.shape)
怎么会这样!这是我的代码。

#!/usr/bin/python
# -*- coding:utf8 -*-
from scripts.get_style_feature import *
from scripts.utils_func import *
from keras import backend as K
import tensorflow as tf

K.set_learning_phase(1)  # set learning phase, or batch norm won't work
alpha = 0.5   # trade-off parameter that balancing the contribution of the content and style targets

# img path & preprocess
style_img_path = '../images/style/starry_night.jpg'
test_img_path = '../images/content/Taylor.jpg'
result_prefix = "../images/autoencoder/"
style_img = preprocess_image(style_img_path)
test_img = preprocess_image(test_img_path)


# create a placeholder for input image
input_img = tf.placeholder(dtype=style_img.dtype, shape=style_img.shape)

# get style image features dict
style_feature_dict = get_style(style_img)

# instantiate a model
model = gen_model(input_tensor=input_img)

# layers's names
outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
feature_layers_former = ['input_1', 'block1_pool', 'block2_pool', 'block3_pool', 'block4_pool',
                     'block5_conv3']
feature_layers_later = ['block1_conv1_T', 'block2_conv1_T', 'block3_conv1_T', 'block4_conv1_T', 'block5_conv1_T',
                    'block5_pool_T']

# calculate loss
loss = K.variable(0.)
for layer_f, layer_l in zip(feature_layers_former, feature_layers_later):
    loss_style = alpha * style_loss(style_feature_dict[layer_f][0], outputs_dict[layer_l][0])
    loss_content = content_loss(style_feature_dict[layer_f][0], outputs_dict[layer_l][0])
    loss += loss_style + loss_content

# optimization
train_op = tf.train.AdamOptimizer(learning_rate=0.001,
  beta1=0.9,
  beta2=0.999,
  epsilon=1e-08).minimize(loss)
tf.summary.scalar('loss', loss)

# Launch the graph in a session
with tf.Session() as sess:
    # tensorboard --logdir=../logs/autoencoder_train
    autoencoder_writer = tf.summary.FileWriter('../logs/autoencoder_train',
                                     sess.graph)
    merged = tf.summary.merge_all()

    # initialize all variables
    tf.global_variables_initializer().run()

    for i in range(2000):
        _, loss_val = sess.run([train_op, loss], feed_dict={input_img: style_img})
        try:
            print 'iteration', i, 'loss =', long(loss_val)
        except:
            print loss_val
        summary = sess.run(merged)
        autoencoder_writer.add_summary(summary, i)
    autoencoder_writer.close()

    # save model
    model.save('../models/autoencoder.h5')

追溯就在这里。

Traceback (most recent call last):
File "/home/gongxinyu/Deeplearning/CNN/source_code/generative_style_transfer/scripts/train_autoencoder.py", line 65, in <module>
summary = sess.run(merged)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 894, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1106, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1259, in _do_run
options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1278, 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 [1,640,1024,3]
 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[1,640,1024,3], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
 [[Node: add_11/_965 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_2423_add_11", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op u'Placeholder', defined at:
File "/home/gongxinyu/Deeplearning/CNN/source_code/generative_style_transfer/scripts/train_autoencoder.py", line 20, in <module>
input_img = tf.placeholder(dtype=style_img.dtype, shape=style_img.shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1532, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1975, in _placeholder
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2528, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1203, 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 [1,640,1024,3]
 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[1,640,1024,3], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
 [[Node: add_11/_965 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_2423_add_11", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

0 个答案:

没有答案