我需要从它们转换成的张量流碎片记录中获取数据集中图像的文件名,以便我可以使用这些名称为神经网络提供其他功能。由于随机性,我无法及时列出清单。在将其输入网络之前,我需要立即输入名称。
我正在使用在tensorflow v1.14上运行的Inception v3。 每个序列化的示例都使用此代码进行解析,我对其进行了编辑以返回文件名:
def parse_example_proto(example_serialized):
"""Parses an Example proto containing a training example of an image.
The output of the build_image_data.py image preprocessing script is a dataset
containing serialized Example protocol buffers. Each Example proto contains
the following fields:
image/height: 462
image/width: 581
image/colorspace: 'RGB'
image/channels: 3
image/class/label: 615
image/class/synset: 'n03623198'
image/class/text: 'knee pad'
image/object/bbox/xmin: 0.1
image/object/bbox/xmax: 0.9
image/object/bbox/ymin: 0.2
image/object/bbox/ymax: 0.6
image/object/bbox/label: 615
image/format: 'JPEG'
image/filename: 'ILSVRC2012_val_00041207.JPEG'
image/encoded: <JPEG encoded string>
Args:
example_serialized: scalar Tensor tf.string containing a serialized
Example protocol buffer.
Returns:
image_buffer: Tensor tf.string containing the contents of a JPEG file.
label: Tensor tf.int32 containing the label.
bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords]
where each coordinate is [0, 1) and the coordinates are arranged as
[ymin, xmin, ymax, xmax].
text: Tensor tf.string containing the human-readable label.
"""
# Dense features in Example proto.
if FLAGS.mode == '0_softmax':
feature_map = {
'image/encoded': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
'image/class/label': tf.FixedLenFeature([1], dtype=tf.int64,
default_value=-1),
'image/class/text': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
'image/filename': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
}
elif FLAGS.mode == '1_sigmoid':
Vdefault = [0]
for kk in range(FLAGS.ClassNumber):
Vdefault.append(0)
feature_map = {
'image/encoded': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
'image/class/label': tf.FixedLenFeature([1], dtype=tf.int64,
default_value=-1),
'image/class/text': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
'image/filename': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
}
else:
raise ValueError("You must set the mode option (to 0_softmax or 1_sigmoid for example)")
sparse_float32 = tf.VarLenFeature(dtype=tf.float32)
# Sparse features in Example proto.
feature_map.update(
{k: sparse_float32 for k in ['image/object/bbox/xmin',
'image/object/bbox/ymin',
'image/object/bbox/xmax',
'image/object/bbox/ymax']})
features = tf.parse_single_example(example_serialized, feature_map)
label = tf.cast(features['image/class/label'], dtype=tf.int32)
xmin = tf.expand_dims(features['image/object/bbox/xmin'].values, 0)
ymin = tf.expand_dims(features['image/object/bbox/ymin'].values, 0)
xmax = tf.expand_dims(features['image/object/bbox/xmax'].values, 0)
ymax = tf.expand_dims(features['image/object/bbox/ymax'].values, 0)
# Note that we impose an ordering of (y, x) just to make life difficult.
bbox = tf.concat(axis=0, values=[ymin, xmin, ymax, xmax])
# Force the variable number of bounding boxes into the shape
# [1, num_boxes, coords].
bbox = tf.expand_dims(bbox, 0)
bbox = tf.transpose(bbox, [0, 2, 1])
return features['image/encoded'], label, bbox, features['image/filename']
此功能在代码段中调用。 Inceptionv3代码库包含许多我不理解的并行处理代码,但我认为我正在做。但是,我不知道如何解释tf.train.batch_join
的输出。查看vscode调试器中输出的文件名,它是一个<tf.Tensor 'batch_processing/batch_join:2' shape=(32,) dtype=string>
。
for thread_id in range(num_preprocess_threads):
# Parse a serialized Example proto to extract the image and metadata.
image_buffer, label_index, bbox, filename = parse_example_proto(
example_serialized)
image = image_preprocessing(image_buffer, bbox, train, thread_id)
images_and_labels.append([image, label_index, filename])
images, label_index_batch, filenames = tf.train.batch_join(
images_and_labels,
batch_size=batch_size,
capacity=3 * num_preprocess_threads * batch_size)
当返回时,我想查看文件名中的值(文件名)。看来它应该是一个列表,而且我应该能够对其进行索引,但是我似乎无法对其进行索引,更不用说获取每个文件名了。
当我使用parsed = _parse_function(raw_record)
,fname = parses['image/filename'].numpy()
之类的内容直接读取记录时,已经能够获取文件名,但是在这里不起作用。哈哈,我真的在裤子的接缝处飞行。