kawser-nerd
kawser-nerd

Reputation: 117

FailedPreconditionError: Attempting to use uninitialized value conv2d_1/kernel with Tensorflow/Python

I am trying to create a CNN model with help of the following code:

import tensorflow as tf

class Create_CNN:
  def _conv(self, input, nChannels, kernelSize, kernelStride):
    conv = tf.layers.conv2d(inputs=input,
                            filters=nChannels,
                            kernel_size=kernelSize,
                            strides=(kernelStride, kernelStride),
                            padding='same',
                            activation=tf.nn.relu
                            )
    return conv

  def create_cnn(self, input, nChannels, kernelSize, kernelStride):
    input = tf.reshape(input, shape=[-1, 500, 530, 3])
    layer1 = Create_CNN()._conv(input, nChannels, kernelSize, kernelStride)
    layer2 = Create_CNN()._conv(layer1, nChannels, kernelSize, kernelStride)
    layer3 = Create_CNN()._conv(layer2, nChannels, kernelSize, kernelStride)
    layer4 = Create_CNN()._conv(layer3, nChannels, kernelSize, kernelStride)
    layer5 = Create_CNN()._conv(layer4, nChannels, kernelSize, kernelStride)
    return layer5

Here, my input is a 500 * 530 * 3 dimension image. I am trying pass input and other parameters using following code:

with tf.Session().as_default():
      tf.global_variables_initializer().run()
      i = PlantUtils().create_instance('ara2013_plant001_rgb.png', 'ara2013_plant001_label.png', 500, 530, 100, 106, 1, 1,
                                 1)
      input_image = i[0] # It is a 500 * 530 * 3 tensor
      b = Create_CNN().create_cnn(input=input_image, kernelSize=3, kernelStride=1, nChannels=30)
      x = tf.argmax(input=b, axis=1)
      print x.eval()

When I am trying to print logits(means value of x) I am getting the following error:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value conv2d_1/kernel

I am not sure what wrong I did. I need to see the logits generated from my CNN model. I really need help on this.

Upvotes: 1

Views: 4137

Answers (1)

Saurabh Saxena
Saurabh Saxena

Reputation: 479

This is happening because you are running the initializer before building the graph. Ideally you should build the Graph before creating a Session. Try this

with tf.Graph().as_default():
  i = PlantUtils().create_instance('ara2013_plant001_rgb.png', 'ara2013_plant001_label.png', 500, 530, 100, 106, 1, 1, 1)
  input_image = i[0] # It is a 500 * 530 * 3 tensor
  b = Create_CNN().create_cnn(input=input_image, kernelSize=3, kernelStride=1, nChannels=30)
  x = tf.argmax(input=b, axis=1)
  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(x)

Upvotes: 3

Related Questions