代码之家  ›  专栏  ›  技术社区  ›  bluesummers

TensorFlow读取和解码一批图像

  •  0
  • bluesummers  · 技术社区  · 6 年前

    使用 tf.train.string_input_producer tf.image.decode_jpeg 我设法从磁盘读取并解码 单一的 图像。

    这是代码:

    # -------- Graph
    filename_queue = tf.train.string_input_producer(
        [img_path, img_path])
    
    image_reader = tf.WholeFileReader()
    
    key, image_file = image_reader.read(filename_queue)
    
    image = tf.image.decode_jpeg(image_file, channels=3)
    
    # Run my network
    logits = network.get_logits(image)
    
    # -------- Session
    sess = tf.Session()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    
    logits_output = sess.run(logits)
    

    问题是,当我看到 logit_outputs 即使队列有2个图像,我也只得到1个值。

    如何读取和解码整个队列?

    1 回复  |  直到 6 年前
        1
  •  2
  •   benjaminplanche    6 年前

    tf.WholeFileReader() ,沿 tf.train.string_input_producer() 作为一个迭代器工作,因此没有一个简单的方法来评估它正在处理的完整数据集的大小。

    获取 N 样本出来,你可以用 image_reader.read_up_to(filename_queue, N) .

    tf.data 管道:

    def _parse_function(filename):
      image_string = tf.read_file(filename)
      image_decoded = tf.image.decode_image(image_string)
      return image_decoded 
    
    # A vector of filenames.
    filenames = tf.constant([img_path, img_path])
    
    dataset = tf.data.Dataset.from_tensor_slices((filenames))
    dataset = dataset.map(_parse_function).batch(N)
    iterator = dataset.make_one_shot_iterator()
    next_image_batch = iterator.get_next()
    
    logits = network.get_logits(next_image_batch)
    # ...