Search code examples
numpytensorflowmachine-learningneural-networktraining-data

ValueError: Cannot feed value of shape (128, 28, 28) for Tensor 'Placeholder:0', which has shape '(?, 784)'


I am new to Tensorflow and Machine Learning and trying out CNN using Tensorflow with my custom input data. But I am getting the error attached below.

The Data or Image Size is 28x28 with 15 Labels. I am not getting the numpy reshape thing in this script or the error.

Help is highly appreciated.

enter image description here

import tensorflow as tf
import os
import skimage.data
import numpy as np
import random

def load_data(data_directory):
    directories = [d for d in os.listdir(data_directory) 
                   if os.path.isdir(os.path.join(data_directory, d))]
    labels = []
    images = []
    for d in directories:
        label_directory = os.path.join(data_directory, d)
        file_names = [os.path.join(label_directory, f) 
                      for f in os.listdir(label_directory) 
                      if f.endswith(".jpg")]
        for f in file_names:
            images.append(skimage.data.imread(f))
            labels.append(d)
        print(str(d)+' Completed')
    return images, labels

ROOT_PATH = "H:\Testing\TrainingData"
train_data_directory = os.path.join(ROOT_PATH, "Training")
test_data_directory = os.path.join(ROOT_PATH, "Testing")

print('Loading Data...')
images, labels = load_data(train_data_directory)
print('Data has been Loaded')

n_classes = 15
training_examples = 10500
test_examples = 4500
batch_size = 128

x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')

def maxpool2d(x):
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

def neural_network_model(x):
    weights = {'W_Conv1':tf.Variable(tf.random_normal([5,5,1,32])),
               'W_Conv2':tf.Variable(tf.random_normal([5,5,32,64])),
               'W_FC':tf.Variable(tf.random_normal([7*7*64, 1024])),
               'Output':tf.Variable(tf.random_normal([1024, n_classes]))}

    biases = {'B_Conv1':tf.Variable(tf.random_normal([32])),
               'B_Conv2':tf.Variable(tf.random_normal([64])),
               'B_FC':tf.Variable(tf.random_normal([1024])),
               'Output':tf.Variable(tf.random_normal([n_classes]))}   

    x = tf.reshape(x, shape=[-1,28,28,1])

    conv1 = conv2d(x, weights['W_Conv1'])
    conv1 = maxpool2d(conv1)

    conv2 = conv2d(conv1, weights['W_Conv2'])
    conv2 = maxpool2d(conv2)

    fc = tf.reshape(conv2, [-1, 7*7*64])
    fc = tf.nn.relu(tf.matmul(fc, weights['W_FC'])+biases['B_FC'])

    output = tf.matmul(fc, weights['Output'])+biases['Output']

    return output

def next_batch(num, data, labels):
    idx = np.arange(0 , len(data))
    np.random.shuffle(idx)
    idx = idx[:num]
    data_shuffle = [data[ i] for i in idx]
    labels_shuffle = [labels[ i] for i in idx]

    return np.asarray(data_shuffle), np.asarray(labels_shuffle)

def train_neural_network(x):
    prediction = neural_network_model(x)

    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10
    with tf.Session() as sess:
        # OLD:
        #sess.run(tf.initialize_all_variables())
        # NEW:
        sess.run(tf.global_variables_initializer())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            for _ in range(int(training_examples/batch_size)):
                epoch_x, epoch_y = next_batch(batch_size, images, labels)
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
                epoch_loss += c

            print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)

        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))

        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:',accuracy.eval({x: images, y: labels}))

print('Training Neural Network...')
train_neural_network(x)

What am I doing wrong? What is needed to be fixed and how do I fix the shape of numpy array?


Solution

  • If you look closely, you'll see that you have two x placeholders:

    x = tf.placeholder('float', [None, 784])  # global
    
    ...
    
    x = tf.reshape(x, shape=[-1,28,28,1])     # in neural_network_model
    

    One of them is in the function scope, hence not visible in train_neural_network, so tensorflow takes the one with [?, 784] shape. You should get rid of one of them.

    Also note that your training data has the rank 3, i.e. [batch_size, 28, 28], so it's not directly compatible with any of those placeholders.

    To feed it into the first x, take epoch_x.reshape([-1, 784]). For the second placeholder (once you make it visible), take epoch_x.reshape([-1, 28, 28, 1]).