我正在尝试使用tf.TFRecordReader
和tf.python_io.TFRecordWriter
来编写和读取由相等长度的短视频序列组成的训练和测试数据。每个帧在一个tf.train.Example
中被编码为PNG图像作为特征。我按照以下方式编写训练集:
with tf.Graph().as_default():
image_placeholder = tf.placeholder(dtype=tf.uint8)
encoded_image = tf.image.encode_png(image_placeholder)
init_op = tf.initialize_all_variables()
filename = os.path.join(data_dir, 'train.tfrecords')
writer = tf.python_io.TFRecordWriter(filename)
sess = tf.Session()
for sequence in sequences:
for i, frame in enumerate(sequence):
png_string = sess.run(encoded_image, feed_dict={ \
image_placeholder: (frame * 255.).astype(np.uint8)})
example = tf.train.Example(features=tf.train.Features(feature={ \
'seq/frame' + str(i): \
tf.train.Feature(bytes_list=tf.train.BytesList(value=[png_string]))}))
writer.write(example.SerializeToString())
我正在使用这样的读者:
filenames = [os.path.join(data_dir, 'train.tfrecords')]
filename_queue = tf.train.string_input_producer(filenames, shuffle=True)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
image_seq, state_seq, action_seq = [], [], []
for i in range(seqence_length):
image_name = "seq/frame" + str(i)
features = {image_name: tf.FixedLenFeature((), tf.string)}
features = tf.parse_single_example(serialized_example, features=features)
image_buffer = tf.reshape(features[image_name], shape=[])
image = tf.image.decode_png(image_buffer, channels=COLOR_CHAN)
image.set_shape([ORIGINAL_HEIGHT, ORIGINAL_WIDTH, COLOR_CHAN])
image = tf.reshape(image, [1, ORIGINAL_HEIGHT, ORIGINAL_WIDTH, COLOR_CHAN])
seq.append(image)
seq = tf.concat(0, image)
image_batch = tf.train.batch(
[seq],
FLAGS.batch_size,
num_threads=FLAGS.batch_size,
capacity=100 * FLAGS.batch_size)
但是,无法为所有帧找到提供的键seq/frame + str(i)
的功能,例如:
ERROR:tensorflow:Exception in QueueRunner: Name: <unknown>, Feature: seq/frame18 is required but could not be found.
[[Node: val_model/ParseSingleExample_18/ParseExample/ParseExample = ParseExample[Ndense=1, Nsparse=0, Tdense=[DT_STRING], dense_shapes=[[]], sparse_types=[], _device="
/job:localhost/replica:0/task:0/cpu:0"](val_model/ParseSingleExample_18/ExpandDims, val_model/ParseSingleExample_18/ParseExample/ParseExample/names, val_model/ParseSingleExampl
e_18/ParseExample/ParseExample/dense_keys_0, val_model/ParseSingleExample_18/ParseExample/Const)]]