如何验证Tensorflow slim数据集中的实际数据

时间:2017-04-20 22:34:16

标签: tensorflow dataset

我已将数据编码为tfrecord文件。对于每个图像,我使用多个标签编码多个边界框。现在,我想验证我的数据是否已被Tensorflow / slim数据集类型正确解码。我写下了一个测试:

def test2(sess):
  labels_to_class = read_label_file(label_fname)
  reader = tf.TFRecordReader
  keys_to_features = {
    'image/encoded': tf.FixedLenFeature(
      (), tf.string, default_value=''),
    'image/format': tf.FixedLenFeature((), tf.string, default_value='jpg'),
    'image/object/labels': tf.VarLenFeature(dtype=tf.int64),
    'image/object/truns': tf.VarLenFeature(dtype=tf.int64),
    'image/object/occluds': tf.VarLenFeature(dtype=tf.int64),
    'image/object/bbox/xmin': tf.VarLenFeature(dtype=tf.int64),
    'image/object/bbox/xmax': tf.VarLenFeature(dtype=tf.int64),
    'image/object/bbox/ymin': tf.VarLenFeature(dtype=tf.int64),
    'image/object/bbox/ymax': tf.VarLenFeature(dtype=tf.int64),
  }
  items_to_handlers = {
    'image': slim.tfexample_decoder.Image('image/encoded', 'image/format'),
    'object/label': slim.tfexample_decoder.Tensor('image/object/labels'),
    'object/truncated': slim.tfexample_decoder.Tensor('image/object/truns'),
    'object/occluded': slim.tfexample_decoder.Tensor('image/object/occluds'),
    'object/bbox': slim.tfexample_decoder.BoundingBox(
        ['ymin', 'xmin', 'ymax', 'xmax'], 'image/object/bbox/'),
  }

  decoder = slim.tfexample_decoder.TFExampleDecoder(
    keys_to_features, items_to_handlers)

  dataset = slim.dataset.Dataset(
    data_sources=filename_queue,
    reader=reader,
    decoder=decoder,
    num_samples=sample_num,
    items_to_descriptions=_ITEMS_TO_DESCRIPTIONS,
    num_classes=_NUM_CLASSES,
    labels_to_names=labels_to_class)

  provider = slim.dataset_data_provider.DatasetDataProvider(dataset)

  keys = provider._items_to_tensors.keys()

  print(provider._num_samples)
  for item in provider._items_to_tensors:
    print(item, provider._items_to_tensors[item])

  [image, label] = provider.get(['image', 'object/label'])

  print('AAA')

  sess.run([image, label])

  print('BBB')

当我运行上述代码时,它会显示:

6
image Tensor("case/If_2/Merge:0", shape=(?, ?, 3), dtype=uint8)
object/label Tensor("SparseToDense:0", shape=(?,), dtype=int64)
object/occluded Tensor("SparseToDense_1:0", shape=(?,), dtype=int64)
record_key Tensor("parallel_read/common_queue_Dequeue:0", dtype=string)
object/bbox Tensor("transpose:0", shape=(?, 4), dtype=int64)
object/truncated Tensor("SparseToDense_2:0", shape=(?,), dtype=int64)
AAA

然后程序永远停在那里而不提供任何错误消息。 程序显示了正确的示例编号(6)和我编码的张量的正确类型,但我仍然想检查张量中的值。 无论如何我能检查他们的价值观吗?

感谢您的帮助。

-----------------更新--------------------

我添加的代码是:

tf.train.start_queue_runners()

print('Start verification process..')

for i in range(provider._num_samples):
  [image, labelList, truncList, occList,
       boxList] = provider.get([
         'image', 'object/label', 'object/truncated',
         'object/occluded', 'object/bbox'])
  enc_image = tf.image.encode_jpeg(image)
  img, labels, truns, occluds, boxes = sess.run(
      [enc_image, labelList, truncList, occList, boxList])

  f = tf.gfile.FastGFile('out_%.2d.jpg' % i, 'wb')
  f.write(img)
  f.close()

  for j in range(labels.shape[0]):
    print('label=%d (%s), truc=%d, occluded=%d at [%d, %d, %d, %d]' % (
        labels[j], labels_to_class[labels[j]], truns[j], 
        occluds[j], boxes[j][0], boxes[j][1],
        boxes[j][2],  boxes[j][3]))

1 个答案:

答案 0 :(得分:1)

您可能需要为要评估的图像和标签启动队列运行器。