ValueError:无法为Tensor' Placeholder_3:0'提供形状值(224,224,3),它具有形状'(?,224,224,3)'

时间:2017-07-20 10:34:18

标签: python tensorflow convolution

我是Tensorflow的新人,我尝试训练我的CNN模型,以便将来对面部进行分类。我有一个56人的图像数据集和他们的裁剪面作为numpy数组,形状为[-1,224,224,3]和float32类型。当我尝试将feed_dict变成tensorflow时 我只是附上我的train_X和train_Y如何进入tensorflow

我得到了典型的错误ValueError:无法为Tensor' Placeholder_3:0'提供形状值(224,224,3),它有形状'(?,224,224,3 )&#39 ;.它似乎很容易理解,但我不知道如何修改我的代码使其工作。

我的Tensorflow代码在这里

import tensorflow as tf

config = tf.ConfigProto()
config.gpu_options.allocator_type = 'BFC'
#config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.6


n_classes = 56
batch_size = 1
hm_epochs = 100


#x = tf.placeholder('float', [None, 150528])
x = tf.placeholder('float', [None, 224,224,3])
y = tf.placeholder('float')


keep_rate = 0.8
keep_prob = tf.placeholder(tf.float32)

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

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


def convolutional_neural_network(x):
    weights = {'W_conv1':tf.Variable(tf.random_normal([5,5,3,32])),
               'W_conv2':tf.Variable(tf.random_normal([5,5,32,64])),
               'W_fc':tf.Variable(tf.random_normal([224*224*3,1024])),
               'out':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])),
              'out':tf.Variable(tf.random_normal([n_classes]))}

    x = tf.reshape(x, shape=[-1, 224, 224, 3])
    #x = train_X

    #creating the first layer of CNN
    conv1 = tf.nn.relu(conv2d(x, weights['W_conv1']) + biases['b_conv1']) # activation function 1
    conv1 = maxpool2d(conv1)

    #creating the second layer of CNN
    conv2 = tf.nn.relu(conv2d(conv1, weights['W_conv2']) + biases['b_conv2']) # activation function 2
    conv2 = maxpool2d(conv2)

    fc = tf.reshape(conv2,[-1, 224*224*3])
    fc = tf.nn.relu(tf.matmul(fc, weights['W_fc'])+biases['b_fc'])
    fc = tf.nn.dropout(fc, keep_rate)
    output = tf.matmul(fc, weights['out'])+biases['out']

    return output



def train_neural_network(x):


    i = 0

    prediction = convolutional_neural_network(x)
    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y) )

    optimizer = tf.train.AdamOptimizer().minimize(cost)

    with tf.Session(config = config) as sess:         
        sess.run(tf.global_variables_initializer())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            for _ in range(int(len(train_X)/batch_size)):
                _, c = sess.run([optimizer, cost], feed_dict={x: train_X[i:i+batch_size], y: train_y[i:i+batch_size]}) #HERE IS THE ERROR
                epoch_loss += c
                i += 100


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



train_neural_network(x)
如果有人能够帮我解决所有问题,我会很高兴。在此先感谢您的帮助。 P.S再见,我需要以我的GPU赢得给我OOM的方式批量处理我的数据。因为我可以改变喂食方式以排除批处理,除了OOM错误之外它工作正常。有趣的故事,当我重新启动内核并再次运行它时。发生了另一个错误--InvalidArgumentError(参见上面的回溯):重塑的输入是一个带有200704值的张量,但是请求的形状需要15052的倍数.200704不能在这里,因为这是224 * 224 * 4当我只有224 * 224 * 3

1 个答案:

答案 0 :(得分:1)

fc图层的形状不正确。

#W_fc':tf.Variable(tf.random_normal([224*224*3,1024]))
W_fc':tf.Variable(tf.random_normal([56*56*64,1024]))
#fc = tf.reshape(conv2,[-1, 224*224*3])
fc = tf.reshape(conv2,[-1, 56*56*64])

在输入图像上应用卷积和最大化时,您将获得以下大小的要素贴图。

输入图片:224x224x3
|
(conv1)224x224x32
|
(maxpool)112x112x32
|
(conv2)112x112x64
|
(maxpool)56x56x64

我按照上面的说明修改了你的代码,但它确实有效 请试一试。