FixedLengthRecordReader

FixedLengthRecordReader

我正在尝试使用FixedLengthRecordReader用TensorFlow创建一个CNN。

import tensorflow as tf
import numpy as np

BATCH_SIZE = 100
IMAGE_SIZE = 30
IMAGE_DEPTH = 3
image_data_len = IMAGE_SIZE * IMAGE_SIZE * 3

class Record(object):
    pass

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

def generate_batch(image, label, min_queue_examples, batch_size):
  num_preprocess_threads = 16
  images, label_batch = tf.train.shuffle_batch(
      [image, label],
      batch_size=batch_size,
      num_threads=num_preprocess_threads,
      capacity=min_queue_examples + 3 * batch_size,
      min_after_dequeue=min_queue_examples)
  return images, tf.reshape(label_batch, [batch_size])

def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):
    l1a = tf.nn.relu(tf.nn.conv2d(X, w, strides=[1, 1, 1, 1], padding='SAME'))
    l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    l1 = tf.nn.dropout(l1, p_keep_conv)

    l2a = tf.nn.relu(tf.nn.conv2d(l1, w2, strides=[1, 1, 1, 1], padding='SAME'))
    l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    l2 = tf.nn.dropout(l2, p_keep_conv)

    l3a = tf.nn.relu(tf.nn.conv2d(l2, w3, strides=[1, 1, 1, 1], padding='SAME'))
    l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])
    l3 = tf.nn.dropout(l3, p_keep_conv)

    l4 = tf.nn.relu(tf.matmul(l3, w4))
    l4 = tf.nn.dropout(l4, p_keep_hidden)

    pyx = tf.matmul(l4, w_o)
    return pyx

reader = tf.FixedLengthRecordReader(record_bytes=(1 + image_data_len))
filenames = ['train.bin']
filename_queue = tf.train.string_input_producer(filenames)
result = Record()
result.key, value = reader.read(filename_queue)
record_bytes = tf.decode_raw(value, tf.uint8)
result.label = tf.cast(tf.slice(record_bytes, [0], [1]), tf.int32)
depth_major = tf.reshape(tf.slice(record_bytes, [1], [image_data_len]), [IMAGE_DEPTH, IMAGE_SIZE, IMAGE_SIZE])
result.uint8image = tf.transpose(depth_major, [1, 2, 0])
float_image = tf.image.per_image_standardization(tf.cast(result.uint8image, tf.float32))

min_fraction_of_examples_in_queue = 0.4
min_queue_examples = int(10000 * min_fraction_of_examples_in_queue)
batch_images, batch_labels = generate_batch(float_image, result.label, min_queue_examples, BATCH_SIZE)

X = tf.placeholder("float", [None, IMAGE_SIZE, IMAGE_SIZE, IMAGE_DEPTH])
Y = tf.placeholder("float", [None, 1])

w = init_weights([3, 3, IMAGE_DEPTH, 32])
w2 = init_weights([3, 3, 32, 64])
w3 = init_weights([3, 3, 64, 128])
w4 = init_weights([128 * 4 * 4, 625])
w_o = init_weights([625, 1])

p_keep_conv = tf.placeholder("float")
p_keep_hidden = tf.placeholder("float")
py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
predict_op = tf.argmax(py_x, 1)

init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
for step in range(1000):
  _, loss_value = sess.run([train_op, cost])


在最后一行中,我得到以下内容:

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


有任何想法吗?

最佳答案

您需要将尺寸为[?, IMAGE_SIZE, IMAGE_SIZE, IMAGE_DEPTH]的图像(图像批次)和尺寸为[?, 1]的标签(标签批次)作为sess.run()传递给feed_dict调用。

由于您有一批名为batch_images的图像和一批名为batch_labels的标签,因此最后几行应为:

images, labels = sess.run([batch_images, batch_labels])
_, loss_value = sess.run([train_op, cost], feed_dict={X: images, Y: labels})

关于python - TensorFlow FixedLengthRecordReader占位符数据类型错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42476251/

10-12 22:23