开发者社区> 问答> 正文

如何从张量中提取值呢?

我需要从tensorflow shard记录中获取数据集中图像的文件名,以便我可以使用这些名称来满足我的神经网络的其他功能。由于随机化的原因,我无法在一段时间内形成一个列表。我需要这些名字在它们被传送到网络之前。 我使用的是运行在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。张量'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 =解析['image/filename'].numpy(),但这里不起作用。我真的是在飞在我的裤子缝与这个哈哈。 问题来源StackOverflow 地址:/questions/59383969/how-do-i-extract-the-values-from-these-tensors-in-tensorflow

展开
收起
kun坤 2019-12-26 15:47:17 1619 0
1 条回答
写回答
取消 提交回答
  • tf.gather和gather_nd从params中收集数值,tf.scatter_nd 和 tf.scatter_nd_update用updates更新某一张量。严格上说,tf.gather_nd和tf.scatter_nd_update互为逆操作。

    已知数值的位置,从张量中提取数值:tf.gather, tf.gather_nd

    tf.gather indices每个元素(标量)是params某个axis的索引,tf.gather_nd 中indices最后一个阶对应于索引值。

    tf.gather函数

    函数原型

    ? 1 2 3 4 5 6 7 gather( params, indices, validate_indices=None, name=None, axis=0 ) params是要查找的张量,indices是要查找值的索引(int32或int64),axis是查找轴,name是操作名。

    如果indices是标量

    浅谈tensorflow中张量的提取值和赋值

    如果indices是向量

    浅谈tensorflow中张量的提取值和赋值

    如果indices是高阶张量

    浅谈tensorflow中张量的提取值和赋值

    返回值:

    该函数返回值类型与params相同,具体值是从params中收集过来的,形状为

    浅谈tensorflow中张量的提取值和赋值

    tf.gather_nd函数

    函数原型

    ? 1 2 3 4 5 gather_nd( params, indices, name=None ) indices是K阶张量,包含K-1阶的索引值。它最后一阶是索引,最后一阶维度必须小于等于params的秩。indices最后一阶的维数等于params的秩时,我们得到params的某些元素;indices最后一阶的维数小于params的秩时,我们得到params的切片。

    浅谈tensorflow中张量的提取值和赋值

    输出张量的形状由indices的K-1阶和params索引到的形状拼接而成,如下面

    ? 1 indices.shape[:-1] + params.shape[indices.shape[-1]:] 参数:

    params:被收集的张量。

    indices:索引张量。必须是以下类型之一:int32,int64。

    name:操作的名称(可选)。

    返回值:

    该函数返回一个张量.与params具有相同的类型。张量值从indices所给定的索引中收集,并且具有这样的形状:

    浅谈tensorflow中张量的提取值和赋值

    已知赋值的位置,向张量赋值:tf.scatter_nd, tf.scatter_nd_update

    tf.scatter_nd对零张量进行赋值,tf.scatter_nd_update对已有可变的张量进行赋值。

    ? 1 2 3 4 5 6 7 tf.scatter_nd函数 scatter_nd( indices, updates, shape, name=None ) 创建一个形状为shape的零张量,将updates赋值到indices指定的位置。

    indices是整数张量,最内部维度对应于索引。

    ? 1 indices.shape[-1] <= shape.rank 如果indices.shape[-1] = shape.rank,那么indices直接对应到新张量的单个元素。如果indices.shape[-1] < shape.rank,那么indices中每个元素对新张量做切片操作。updates的形状应该如下所示

    ? 1 indices.shape[:-1] + shape[indices.shape[-1]:] 如果我们要把形状为(4,)的updates赋值给形状为(8,)的零张量,如下图所示。

    浅谈tensorflow中张量的提取值和赋值

    我们需要这样子做

    ? 1 2 3 4 5 6 indices = tf.constant([[4], [3], [1], [7]]) updates = tf.constant([9, 10, 11, 12]) shape = tf.constant([8]) scatter = tf.scatter_nd(indices, updates, shape) with tf.Session() as sess: print(sess.run(scatter)) 我们得到这样子的张量

    ? 1 [0, 11, 0, 10, 9, 0, 0, 12] 上面代码中,indices的形状是(4,1),updates的形状是(4,),shape的形状是(8,)。

    ? 1 indices.shape[:-1]+shape[indices.shape[-1]:] = (4,)+(,)=(4,) 如果我们要在三阶张量中插入两个切片,如下图所示,则应该像下面代码里所说的那样子做。

    2021-02-07 20:08:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载