Tensorflow错误:FailedPeconditionError:尝试使用未初始化的变量 [英] Tensorflow error: FailedPeconditionError: attempting to use uninitialized variable

查看:94
本文介绍了Tensorflow错误:FailedPeconditionError:尝试使用未初始化的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将立体图像放入优化器中.这是我的代码:

I want to put a stereo image into an optimizer. This is my code:

tf.reset_default_graph()

# config
learning_rate = 0.5
training_epochs = 5

# init
init = tf.global_variables_initializer()

def conv2d(input_layer):
    conv1 = tf.layers.conv2d(
        inputs=input_layer,
        filters=32,
        kernel_size=[3, 3],
        padding='same',
        activation=tf.tanh,
        use_bias=False
    )

    conv2 = tf.layers.conv2d(
        inputs=conv1,
        filters=32,
        kernel_size=[3, 3],
        padding='same',
        activation=tf.tanh,
        use_bias=False
    )

    conv3 = tf.layers.conv2d(
        inputs=conv2,
        filters=32,
        kernel_size=[3, 3],
        padding='same',
        activation=tf.tanh,
        use_bias=False
    )

    conv4 = tf.layers.conv2d(
        inputs=conv3,
        filters=32,
        kernel_size=[3, 3],
        padding='same',
        activation=tf.tanh,
        use_bias=False
    )

    logits = tf.layers.conv2d(
        inputs=conv4,
        filters=32,
        kernel_size=[3, 3],
        padding='same',
        activation=tf.sigmoid,
        use_bias=False
    )

    return logits


if __name__ == '__main__':
    # read images
    # preprocessing: rgb converted to float, zero_mean, uni_variance
    images = reading_images()
    mask_tensor = images["mask"][1]

    # reshape images
    img0 = images["img0"][1]
    img1 = images["img1"][1]
    img0_rs = tf.reshape(img0, [1, int(1988 / 2), int(2880 / 2), 3])
    img1_rs = tf.reshape(img1, [1, int(1988 / 2), int(2880 / 2), 3])

    # define symbolic placeholders
    t_im0 = tf.placeholder(tf.float32, [1, None, None, 3])
    t_im1 = tf.placeholder(tf.float32, [1, None, None, 3])
    t_img = tf.concat([t_im0, t_im1], axis=3)

    input_layer = tf.reshape(t_img, [1, int(1988 / 2), int(2880 / 2), 6])
    logits = conv2d(input_layer)

    with tf.name_scope("cost_function") as scope:
        mask_tensor = tf.tile(mask_tensor, [1, 1, 3])
        cost_function = -tf.reduce_mean(mask_tensor * tf.log(logits) + (1. - mask_tensor) * tf.log(1. - logits))
    tf.summary.scalar("cost_function", cost_function)

    with tf.name_scope("train") as scope:
        optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)
    merged_summary_op = tf.summary.merge_all()

    with tf.Session() as sess:
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        sess.run(init)
        # summary_writer = tf.summary.FileWriter('/tmp/tensorflow_logs', graph=sess.graph)
        for epoch in range(training_epochs):
            print("epoch ", epoch)

            avg_cost = 0.0
            mask = sess.run(mask_tensor)
            np_img0_rs = sess.run(img0_rs)
            np_img1_rs = sess.run(img1_rs)

            # res = t_img.eval(feed_dict={t_im0: img0_rs_, t_im1: img1_rs_})

            sess.run([optimizer], feed_dict={t_im0: np_img0_rs, t_im1: np_img1_rs})
    coord.request_stop()
    coord.join(threads)

但我总是会收到此错误.我不知道那是我必须改变的地方.我可以尝试调试什么?我确实做了很多尝试来纠正此错误.

But I always get this error. I do not know what it can be what I have to change. What can I try to debug it? I really tried a lot to fix this error.

epoch  0
2017-07-17 10:26:03.719539: W tensorflow/core/kernels/queue_base.cc:294] _4_input_producer: Skipping cancelled enqueue attempt with queue not closed
2017-07-17 10:26:03.719610: W tensorflow/core/kernels/queue_base.cc:294] _5_input_producer_1: Skipping cancelled enqueue attempt with queue not closed
Traceback (most recent call last):
  File "/home/test/Dropbox/occlusion_thesis/occ_small/main.py", line 111, in <module>
    sess.run([optimizer], feed_dict={t_im0: np_img0_rs, t_im1: np_img1_rs})
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_4/kernel
     [[Node: conv2d_4/kernel/read = Identity[T=DT_FLOAT, _class=["loc:@conv2d_4/kernel"], _device="/job:localhost/replica:0/task:0/cpu:0"](conv2d_4/kernel)]]

Caused by op u'conv2d_4/kernel/read', defined at:
  File "/home/test/Dropbox/occlusion_thesis/occ_small/main.py", line 84, in <module>
    logits = conv2d(input_layer)
  File "/home/test/Dropbox/occlusion_thesis/occ_small/main.py", line 60, in conv2d
    use_bias=False
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/convolutional.py", line 551, in conv2d
    return layer.apply(inputs)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 492, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 434, in __call__
    self.build(input_shapes[0])
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/convolutional.py", line 137, in build
    dtype=self.dtype)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 374, in add_variable
    trainable=trainable and self.trainable)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 367, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 725, in _get_single_variable
    validate_shape=validate_shape)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 200, in __init__
    expected_shape=expected_shape)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 319, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1303, in identity
    result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value conv2d_4/kernel
     [[Node: conv2d_4/kernel/read = Identity[T=DT_FLOAT, _class=["loc:@conv2d_4/kernel"], _device="/job:localhost/replica:0/task:0/cpu:0"](conv2d_4/kernel)]]

推荐答案

我不确定您的代码是否完整,但错误消息对我来说似乎很清楚:

I'm not sure if your code is complete or not, but the error message seems rather clear to me:

FailedPreconditionError:尝试使用未初始化的值conv2d_4/kernel

FailedPreconditionError: Attempting to use uninitialized value conv2d_4/kernel

在看您的代码,我看到您有 sess.run(init),但是我在任何地方都找不到 init 的定义.尝试在之前添加 init = tf.global_variables_initializer(),并将tf.Session()作为sess:,这应该可以解决未初始化的值"错误.

Looking a your code, I see you have sess.run(init) but I can't find a definition for init anywhere. Try adding init = tf.global_variables_initializer() before with tf.Session() as sess:, this should fix the "uninitialized value" error.

编辑:有了完整的代码,我发现问题出在以下地方:

Edit: With the full code, I see the problem rising from:

# init
init = tf.global_variables_initializer() # <<<<<<<<< 1

def conv2d(input_layer):
    ## Bunch of code defining layers
    return logits

if __name__ == '__main__':
    ## bunch of other code

    logits = conv2d(input_layer) # <<<<<<<<< 2

我将定义初始化函数的点标记为 1 ,直到该点为止定义的所有变量,并且 2 标记为该点实际定义网络(以及其中的所有变量)的位置. init 的定义必须在所有变量定义完成之后 ,否则您将拥有未初始化的变量.

I marked as 1 the point where you define the initialization function for al the variables defined up to that point, and 2 the point where you actually define your network (and all the variables in it). The definition of init has to be after all the definitions of the variables are done, otherwise you'll have uninitialized variables.

我在这里复制我对答案所作的评论,因为它可能是放置它的更好地方. tf.global_variables_initializer()必须在定义图形后被调用 .如果在开始时定义它,然后将其添加到网络中,则添加的层的权重将不会被初始化,因为在创建初始化操作时未定义它们的权重.始终将int定义为您在之前使用tf.Session()... 的最后一个操作,以确保在初始化过程中不会遗漏任何东西.

I'm copying here the comment I made to the answer since it's probably a better place to put it. tf.global_variables_initializer() has to be called after your graph has been defined. If you defne it at the beginning and then add layers to the network, the weights of the layers added will NOT be initialized because they were not defined when you created the initializing operation. Always define init as your last operation before with tf.Session() ... to make sure you don't miss anything in the initialization.

这篇关于Tensorflow错误:FailedPeconditionError:尝试使用未初始化的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆