Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow error: FailedPeconditionError: attempting to use uninitialized variable

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)]]
like image 281
j35t3r Avatar asked Dec 06 '22 14:12

j35t3r


1 Answers

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

FailedPreconditionError: Attempting to use uninitialized value conv2d_4/kernel

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

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.

Update:

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.

like image 161
GPhilo Avatar answered Dec 10 '22 17:12

GPhilo