我使用write_tfrecord()将我自己的60 * 60像素的灰度数据集转换为tfrecords,但是当我想要读取和解码它们时会导致错误。有什么问题?
train_tfrecord_addr = './data/train.tfrecords'
test_tfrecord_addr = './data/test.tfrecords'
n_train_samples = 43990
n_test_samples = 12500
batch_size = 32 # number of batches in each iteration
keep_prob = 0.5 # Dropout, probability to keep units
n_epochs = 25
tfrecords_filename = './data/test.tfrecords'
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _float32_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
def write_tfrecord(path):
images_addrs, images_labels = get_lable_and_image(path=path)
filename_pairs = list(zip(images_addrs, images_labels))
print(filename_pairs)
# to shuffle data
shuffle(filename_pairs)
writer = tf.python_io.TFRecordWriter(tfrecords_filename)
for img_path, label in filename_pairs:
# in this case all images are png with (32, 32) shape
img = np.array(Image.open(img_path)) # (32, 32) uint8
img_raw = img.tostring()
label_raw = label.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'image_raw': _bytes_feature(img_raw),
'label_raw': _bytes_feature(label_raw),
}))
writer.write(example.SerializeToString())
writer.close()
解码方法......
def read_and_decode(filename, batch_size, num_epochs, num_samples):
filename_queue = tf.train.string_input_producer([train_tfrecord_addr],
num_epochs=num_epochs)
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_raw': tf.FixedLenFeature([], tf.string),
'label_raw': tf.FixedLenFeature([], tf.string),
})
# Convert from a scalar string tensor to a uint8 tensor
image_raw = tf.decode_raw(features['image_raw'], tf.uint8)
image_resized = tf.reshape(image_raw, [60 * 60])
# Convert from [0, 255] -> [-0.5, 0.5] floats.
image_resized = tf.cast(image_resized, tf.float32) * (1. / 255) - 0.5
# Convert from a scalar string tensor to a uint8 tensor
label_raw = tf.decode_raw(features['label_raw'], tf.uint8)
label_resized = tf.reshape(label_raw, [2])
images, labels = tf.train.batch([image_resized, label_resized],
batch_size=batch_size,
capacity=num_samples,
num_threads=2, )
return images, labels
这是提供卷积数据的主要代码。
def run_training():
"""Train ShapeNet for a number of steps."""
# Tell TensorFlow that the model will be built into the default Graph.
with tf.Graph().as_default():
# Input train images and labels.
train_images, train_labels = read_and_decode(filename=train_tfrecord_addr,
batch_size=batch_size,
num_epochs=n_epochs,
num_samples=n_train_samples)
# Input test images and labels.
# define batch_size = all test samples
test_images, test_labels = read_and_decode(filename=test_tfrecord_addr,
batch_size=n_test_samples,
num_epochs=n_epochs,
num_samples=n_test_samples)
# define placeholder for input images and labels
X = tf.placeholder(tf.float32, [None, 60 * 60])
Y = tf.placeholder(tf.float32, [None, 2])
# Build a Graph that computes predictions from the inference model.
prediction = convolutional_network_model(X)
# Backpropagation
# measure of error of our model
# this needs to be minimised by adjusting W and b
cross_entropy = -tf.reduce_sum(Y * tf.log(prediction))
# define training step which minimises cross entropy
train_op =tf.train.GradientDescentOptimizer(
learning_rate=0.001)
.minimize(cross_entropy)
# argmax gives index of highest entry in vector (1st axis of 1D tensor)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
# get mean of all entries in correct prediction, the higher the better
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
# The op for initializing the variables.
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
st = time.time()
# Create a session for running operations in the Graph.
with tf.Session() as sess:
# Initialize the variables (the trained variables and the
# epoch counter).
sess.run(init_op)
# Start input enqueue threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for epoch in range(n_epochs):
for itr in range(n_train_samples // batch_size):
# fetch the batch train images and labels
batch_x, batch_y = sess.run([train_images, train_labels])
sess.run([train_op], feed_dict={X: batch_x, Y: batch_y})
# fetch whole test images and labels
batch_x, batch_y = sess.run([test_images, test_labels])
# feed the model with all test images and labels
acc, _ = sess.run([accuracy, train_op],
feed_dict={X: batch_x, Y: batch_y})
print('epoch %d/%d: , accuracy = %.3f'
% (epoch, n_epochs, acc))
# When done, ask the threads to stop.
coord.request_stop()
# Wait for threads to finish.
coord.join(threads)
et = time.time()
duration = et - st
print(duration)
这是错误。我也以二进制格式转换我的数据集,但我再次看到相同的错误
2018-02-13 13:53:02.401813: I C:\tf_jenkins\workspace\rel-
win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your
CPU supports instructions that this TensorFlow binary was not compiled to
use: AVX
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\client\session.py", line 1350, in _do_call
return fn(*args) File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\client\session.py", line 1329, in _run_fn
status, run_metadata)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.OutOfRangeError: FIFOQueue
'_1_batch/fifo_queue' is closed and has insufficient elements (requested 32,
current size 0)
[[Node: batch = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_UINT8],
timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"]
(batch/fifo_queue, batch/n)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:/project/python/MathNet/mathnet.py", line 232, in <module>
run_training()
File "F:/project/python/MathNet/mathnet.py", line 207, in run_training
batch_x, batch_y = sess.run([train_images, train_labels])
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\client\session.py", line 895, in run
run_metadata_ptr)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\client\session.py", line 1128, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\client\session.py", line 1344, in _do_run
options, run_metadata)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\client\session.py", line 1363, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.OutOfRangeError: FIFOQueue
'_1_batch/fifo_queue' is closed and has insufficient elements (requested
32, current size 0)
[[Node: batch = QueueDequeueManyV2[component_types=[DT_FLOAT,
DT_UINT8], timeout_ms=-1,
_device="/job:localhost/replica:0/task:0/device:CPU:0"](batch/fifo_queue,
batch/n)]]
Caused by op 'batch', defined at:
File "F:/project/python/MathNet/mathnet.py", line 232, in <module>
run_training()
File "F:/project/python/MathNet/mathnet.py", line 159, in run_training
num_samples=n_train_samples)
File "F:/project/python/MathNet/mathnet.py", line 105, in
read_and_decode
num_threads=2, )
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\training\input.py", line 979, in batch
name=name)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\training\input.py", line 754, in _batch
dequeued = queue.dequeue_many(batch_size, name=name)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\ops\data_flow_ops.py", line 475, in dequeue_many
self._queue_ref, n=n, component_types=self._dtypes, name=name)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\ops\gen_data_flow_ops.py", line 2764, in
_queue_dequeue_many_v2
component_types=component_types, timeout_ms=timeout_ms, name=name)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\framework\op_def_library.py", line 787, in
_apply_op_helper
op_def=op_def)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\framework\ops.py", line 3160, in create_op
op_def=op_def)
File "C:\Program Files\Python35\lib\site-
packages\tensorflow\python\framework\ops.py", line 1625, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-
access
OutOfRangeError (see above for traceback): FIFOQueue '_1_batch/fifo_queue'
is closed and has insufficient elements (requested 32, current size 0)
[[Node: batch = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_UINT8],
timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"]
(batch/fifo_queue, batch/n)]]