aze
aze

Reputation: 850

tensorflow : Analysing images gives Cannot feed value of shape (1296000,) for Tensor 'Placeholder:0', which has shape '(?, 1296000)'

I am using tensorflow to build a multilayer_perceptron network, based on the example given by Google

The point is to train images to recognize specific patterns I am using images that are 1440*900 with coordinates that point these patterns (that might not be the best way but it is just a test really).

When running my code I have the following error :

#python multilayer_perceptron.py

WARNING:tensorflow:From multilayer_perceptron.py:119: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See @{tf.nn.softmax_cross_entropy_with_logits_v2}.

2018-06-07 11:29:14.144489: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
current batch :  [195330 195330 195330 ... 155252 155252 155252] [ 90.5 312.5]
Traceback (most recent call last):
  File "multilayer_perceptron.py", line 141, in <module>
    Y: batch_y})
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 900, in run
    run_metadata_ptr)
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1111, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1296000,) for Tensor 'Placeholder:0', which has shape '(?, 1296000)'

Here is how I created the perceptrons:

# Network Parameters 
n_hidden_1 = 10 #256 # 1st layer number of neurons
n_hidden_2 = 10 #256 # 2nd layer number of neurons
#each image has been flattened and converted to a 1-D numpy array of 1440*900
n_input = INPUT_SIZE # img size
n_classes = 2 # coordinates of where to click

# tf Graph input
X = tf.placeholder("float", [None, n_input]) #n_input is size of input which is 784 pixels
Y = tf.placeholder("float", [None, n_classes])  # n_classes is size of output which is 10 digits here so why float and not bool ?

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}

biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}


# Create model
def multilayer_perceptron(x):
    # Hidden fully connected layer with 256 neurons
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    # Hidden fully connected layer with 256 neurons
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    # Output fully connected layer with a neuron for each class
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

# Construct model
logits = multilayer_perceptron(X)

# Define loss and optimizer
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
# Initializing the variables
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(len(train_data)/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            #batch_x, batch_y = mnist.train.next_batch(batch_size)
            batch_x = train_data[i] # numpy.array of 1-D image
            batch_y = train_labels[i] # numpy.array of coords of where to click
            print("current batch : ", batch_x, batch_y)
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([train_op, loss_op], feed_dict={X: batch_x,
                                                            Y: batch_y})
            # Compute average loss
            avg_cost += c / total_batch
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost={:.9f}".format(avg_cost))
    print("Optimization Finished!")
    # Test model
    pred = tf.nn.softmax(logits)  # Apply softmax to logits
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("Accuracy:", accuracy.eval({X: mnist.test.images, Y: mnist.test.labels}))

I am not sure about what the error means nor how to solve it really.

Upvotes: 0

Views: 41

Answers (1)

nessuno
nessuno

Reputation: 27042

You're feeding a single element with shape (1296000), that's a 1-D tensor. Your placeholder, (at in general every tensorflow input) instead, want's a batch of elements.

Hence you have to feed your network a (batch_size, X) tensor. If you want to feed one element at a time, you can use numpy to reshape your tensor to the required shape (the same reasoning holds for the labels tensor):

        _, c = sess.run([train_op, loss_op], feed_dict={
                         X: np.expand_dims(batch_x, axis=0),
                         Y: np.expand_dims(batch_y, axis=0)})

Upvotes: 1

Related Questions