我正在尝试使用TensorFlow干净的方式(tf.train.shuffle_batch)处理我的输入数据,我从教程中收集的大部分代码都经过了一些细微的修改,例如decode_jpeg函数。
size = 32,32
classes = 43
train_size = 12760
batch_size = 100
max_steps = 10000
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'image/encoded': tf.FixedLenFeature([], tf.string),
'image/class/label': tf.FixedLenFeature([], tf.int64),
'image/height': tf.FixedLenFeature([], tf.int64),
'image/width': tf.FixedLenFeature([], tf.int64),
})
label = tf.cast(features['image/class/label'], tf.int32)
reshaped_image = tf.image.decode_jpeg(features['image/encoded'])
reshaped_image = tf.image.resize_images(reshaped_image, size[0], size[1], method = 0)
reshaped_image = tf.image.per_image_whitening(reshaped_image)
return reshaped_image, label
def inputs(train, batch_size, num_epochs):
subset = "train"
tf_record_pattern = os.path.join(FLAGS.train_dir + '/GTSRB', '%s-*' % subset)
data_files = tf.gfile.Glob(tf_record_pattern)
filename_queue = tf.train.string_input_producer(
data_files, num_epochs=num_epochs)
# Even when reading in multiple threads, share the filename
# queue.
image, label = read_and_decode(filename_queue)
# Shuffle the examples and collect them into batch_size batches.
# (Internally uses a RandomShuffleQueue.)
# We run this in two threads to avoid being a bottleneck.
images, sparse_labels = tf.train.shuffle_batch(
[image, label], batch_size=batch_size, num_threads=2,
capacity=1000 + 3 * batch_size,
# Ensures a minimum amount of shuffling of examples.
min_after_dequeue=1000)
return images, sparse_labels
Run Code Online (Sandbox Code Playgroud)
当我尝试跑步时
batch_x, batch_y = inputs(True, 100,100)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-543290a0c903> in <module>()
----> 1 batch_x, batch_y = inputs(True, 100,100)
<ipython-input-5-a8c07c7fc263> in inputs(train, batch_size, num_epochs)
73 capacity=1000 + 3 * batch_size,
74 # Ensures a minimum amount of shuffling of examples.
---> 75 min_after_dequeue=1000)
76 #return image, label
77 return images, sparse_labels
/Users/Kevin/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/input.pyc in shuffle_batch(tensors, batch_size, capacity, min_after_dequeue, num_threads, seed, enqueue_many, shapes, allow_smaller_final_batch, shared_name, name)
800 queue = data_flow_ops.RandomShuffleQueue(
801 capacity=capacity, min_after_dequeue=min_after_dequeue, seed=seed,
--> 802 dtypes=types, shapes=shapes, shared_name=shared_name)
803 _enqueue(queue, tensor_list, num_threads, enqueue_many)
804 full = (math_ops.cast(math_ops.maximum(0, queue.size() - min_after_dequeue),
/Users/Kevin/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.pyc in __init__(self, capacity, min_after_dequeue, dtypes, shapes, names, seed, shared_name, name)
580 """
581 dtypes = _as_type_list(dtypes)
--> 582 shapes = _as_shape_list(shapes, dtypes)
583 names = _as_name_list(names, dtypes)
584 # If shared_name is provided and an op seed was not provided, we must ensure
/Users/Kevin/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.pyc in _as_shape_list(shapes, dtypes, unknown_dim_allowed, unknown_rank_allowed)
70 if not unknown_dim_allowed:
71 if any([not shape.is_fully_defined() for shape in shapes]):
---> 72 raise ValueError("All shapes must be fully defined: %s" % shapes)
73 if not unknown_rank_allowed:
74 if any([shape.dims is None for shape in shapes]):
ValueError: All shapes must be fully defined: [TensorShape([Dimension(32), Dimension(32), Dimension(None)]), TensorShape([])]
Run Code Online (Sandbox Code Playgroud)
我不确定是什么导致了此错误,我想这与我处理图像的方式有关,因为它表明当它们应具有3个通道(RGB)时它们没有尺寸。
的在TensorFlow配料方法(tf.train.batch(),tf.train.batch_join(),tf.train.shuffle_batch(),和tf.train.shuffle_batch_join())要求的批次的每个元件具有完全相同的形状*,以便它们可以被包装成致密张量。在您的代码中,似乎image传递给您的张量的三维尺寸tf.train.shuffle_batch()大小未知。这对应于每个图像中的通道数,对于具有alpha通道的单色图像,该数目为1,对于单色图像为3,对于彩色图像为3,对于彩色图像为4。如果您传递一个显式值channels=N(N适当时为1,3或4),这将为TensorFlow提供有关图像张量形状的足够信息以继续进行。
*有一个例外:当你通过dynamic_pad=True到tf.train.batch()或tf.train.batch_join()元素可以有不同的形状,但它们必须具有相同的等级。通常,它仅用于顺序数据,而不用于图像数据(在图像边缘会产生不良行为)。