【问题标题】:Shape of placeholder in TensorflowTensorflow 中占位符的形状
【发布时间】:2017-03-05 23:28:27
【问题描述】:

我使用 Tensorflow 的时间很短。这是我的问题: 我加载 AlexNet 权重以对其进行微调,因此我给出了 50 个大小的批次。 所以我定义了:

# Graph input
x = tf.placeholder(tf.float32, [50, 227, 227, 3])
y = tf.placeholder(tf.float32, [None, 40])

我给一批50张图片,想得到40个输出类。

然后我定义了我的模型

class Model:
@staticmethod 
def alexnet(_X, _dropout):
    # Layer 1 (conv-relu-pool-lrn)
    conv1 = conv(_X, 11, 11, 96, 4, 4, padding='VALID', name='conv1')
    conv1 = max_pool(conv1, 3, 3, 2, 2, padding='VALID', name='pool1')
    norm1 = lrn(conv1, 2, 2e-05, 0.75, name='norm1')
    # Layer 2 (conv-relu-pool-lrn)
    conv2 = conv(norm1, 5, 5, 256, 1, 1, group=2, name='conv2')
    conv2 = max_pool(conv2, 3, 3, 2, 2, padding='VALID', name='pool2')
    norm2 = lrn(conv2, 2, 2e-05, 0.75, name='norm2')
    # Layer 3 (conv-relu)
    conv3 = conv(norm2, 3, 3, 384, 1, 1, name='conv3')
    # Layer 4 (conv-relu)
    conv4 = conv(conv3, 3, 3, 384, 1, 1, group=2, name='conv4')
    # Layer 5 (conv-relu-pool)
    conv5 = conv(conv4, 3, 3, 256, 1, 1, group=2, name='conv5')
    pool5 = max_pool(conv5, 3, 3, 2, 2, padding='VALID', name='pool5')
    # Layer 6 (fc-relu-drop)
    fc6 = tf.reshape(pool5, [-1, 6*6*256])
    fc6 = fc(fc6, 6*6*256, 4096, name='fc6')
    fc6 = dropout(fc6, _dropout)
    # Layer 7 (fc-relu-drop)
    fc7 = fc(fc6, 4096, 4096, name='fc7')
    fc7 = dropout(fc7, _dropout)
    # Layer 8 (fc-prob)
    fc8 = fc(fc7, 4096, 40, relu=False, name='fc8')
    return fc8 # fc8 and fc7 (for transfer-learning)

并创建它

keep_var = tf.placeholder(tf.float32)

# Model
pred = Model.alexnet(x, keep_var)  

我可以进行训练,效果很好,但最后,我只想提供一张图像,但 x 占位符和 y 占位符是为 50 张图像定义的,因此会引发错误。 这是我训练后的代码,只给出一张图片:

    x_test = tf.placeholder(tf.float32, [1, 227, 227, 3])
    y_test = tf.placeholder(tf.float32, [None, 40])
    img = loaded_img_train[0][:][:][:] # Only one image
    label = loaded_lab_train[0][:] # Only one label
    prediction = sess.run(pred, feed_dict={x_test: [img],     y_test: [label], keep_var: 1.})

它引发了我这个错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [50,227,227,3]
 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[50,227,227,3], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

我不知道如何输入我想要的输入大小。

我的练习直接受到cnn识别花朵的启发

非常感谢您的帮助! 纪尧姆

【问题讨论】:

    标签: input tensorflow conv-neural-network


    【解决方案1】:

    除了将形状的第一个维度设置为固定大小外,您还可以通过设置 None 而不是数字来为形状的第一个维度使用可变大小。 Tensorflow 能够通过输入大小和形状其他维度的固定大小来计算批量大小。

    对于占位符 y,您已正确:

    y = tf.placeholder(tf.float32, [None, 40])
    

    对于占位符 x,你必须设置:

    x = tf.placeholder(tf.float32, [None, 227, 227, 3])
    

    【讨论】:

    • 我做到了,但是当我定义我的卷积层时出现另一个错误:bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape().as_list()) 据说:TypeError: Expected binary or unicode string, got None 在 conv.get_shape() 中我有问题。
    • Conv 定义为:convolve = lambda i, k: tf.nn.conv2d( i, k, [1, s_h, s_w, 1], padding=padding) with tf.variable_scope(name) as scope: # Create tf variables for the weights and biases of the conv layer kernel = make_var('weights', shape=[k_h, k_w, int(c_i) / group, c_o]) biases = make_var('biases', [c_o]) if group == 1: conv = convolve(input, kernel)
    • 我知道我有点迷路了!我需要练习!
    • 检查 github.com/tensorflow/tensorflow/issues/2118,在另一个 stackoverflow 问题 (stackoverflow.com/questions/40628929/…) 中引用了这个。
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多