34

Cifar10(バイナリファイル、size = 96*96*3画像ごとのバイト数)に似た形式の画像がたくさんあります(STL-10 dataset)。開いているファイルのサイズは 138MB です。

画像を含む Tensor の内容を読み取って確認し、読み取りが正しく行われたことを確認しようとしましたが、2 つの質問があります。

  1. FixedLengthRecordReaderファイル全体をロードしますが、一度に 1 つずつ入力を提供するだけですか? size最初のバイトの読み取りは比較的高速であるためです。ただし、コードの実行には約 2 分かかります。
  2. 実際の画像コンテンツを表示可能な形式で取得する方法、またはそれらを内部的に表示して画像が適切に読み取られることを検証する方法は? しましたsess.run(uint8image)が、結果は空です。

コードは以下のとおりです。

import tensorflow as tf
def read_stl10(filename_queue):
  class STL10Record(object):
    pass
  result = STL10Record()

  result.height = 96
  result.width = 96
  result.depth = 3
  image_bytes = result.height * result.width * result.depth
  record_bytes = image_bytes

  reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  result.key, value = reader.read(filename_queue)
  print value
  record_bytes = tf.decode_raw(value, tf.uint8)

  depth_major = tf.reshape(tf.slice(record_bytes, [0], [image_bytes]),
                       [result.depth, result.height, result.width])
  result.uint8image = tf.transpose(depth_major, [1, 2, 0])
  return result
# probably a hack since I should've provided a string tensor

filename_queue = tf.train.string_input_producer(['./data/train_X'])
image = read_stl10(filename_queue)

print image.uint8image
with tf.Session() as sess:
  result = sess.run(image.uint8image)
  print result, type(result)

出力:

Tensor("ReaderRead:1", shape=TensorShape([]), dtype=string)
Tensor("transpose:0", shape=TensorShape([Dimension(96), Dimension(96), Dimension(3)]), dtype=uint8)
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
[empty line for last print]
Process finished with exit code 137

それが何かを追加する場合、私はこれを私のCPUで実行しています。

編集: Rosa のおかげで純粋な TensorFlow ソリューションを見つけました。どうやら、 を使用する場合string_input_producer、結果を表示するには、キュー ランナーを初期化する必要があります。上記のコードに追加する必要があるのは、下から 2 行目だけです。

...
with tf.Session() as sess:
    tf.train.start_queue_runners(sess=sess)
...

その後、 で画像をresult表示できますmatplotlib.pyplot.imshow(result)。これが誰かに役立つことを願っています。さらに質問がある場合は、お気軽に私に尋ねるか、Rosa の回答のリンクを確認してください。

4

8 に答える 8

45

完全な答えを出すために:

filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) #  list of files to read

reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)

my_img = tf.image.decode_png(value) # use png or jpg decoder based on your files.

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
  sess.run(init_op)

  # Start populating the filename queue.

  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  for i in range(1): #length of your filename list
    image = my_img.eval() #here is your image Tensor :) 

  print(image.shape)
  Image.fromarray(np.asarray(image)).show()

  coord.request_stop()
  coord.join(threads)

または、画像のディレクトリがある場合は、この Github ソース ファイルを介してそれらすべてを追加できます

@mttk と @salvador-dali: それがあなたが必要としているものであることを願っています

于 2015-11-23T01:39:14.943 に答える
3

tf.train.match_filenames_once で名前をロードし、tf.size オープン セッションで反復処理するファイルの数を取得して楽しんでください ;-)

import tensorflow as tf
import numpy as np
import matplotlib;
from PIL import Image

matplotlib.use('Agg')
import matplotlib.pyplot as plt


filenames = tf.train.match_filenames_once('./images/*.jpg')
count_num_files = tf.size(filenames)
filename_queue = tf.train.string_input_producer(filenames)

reader=tf.WholeFileReader()
key,value=reader.read(filename_queue)
img = tf.image.decode_jpeg(value)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    num_files = sess.run(count_num_files)
    for i in range(num_files):
        image=img.eval()
        print(image.shape)
        Image.fromarray(np.asarray(image)).save('te.jpeg')
于 2017-02-21T12:53:05.333 に答える
2

(コメントできません。十分な評判ではありませんが、これは私のために働いた修正版です)

このエラーを取り除く ためにNo default session is registered使用できるに関する@HamedMPエラーへ: https://www.tensorflow.org/versions/r0.8/api_docs/python/client.html#InteractiveSessionInteractiveSession

また、@NumesSanguis の問題に対しては、画像オブジェクトを返すため、通常の PIL メソッドImage.showを使用できます。.show()fromarray

以下の両方を行います(PNGの代わりにJPEGを使用していることに注意してください):

import tensorflow as tf
import numpy as np
from PIL import Image

filename_queue = tf.train.string_input_producer(['my_img.jpg']) #  list of files to read

reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)

my_img = tf.image.decode_jpeg(value) # use png or jpg decoder based on your files.

init_op = tf.initialize_all_variables()
sess = tf.InteractiveSession()
with sess.as_default():
    sess.run(init_op)

# Start populating the filename queue.

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

for i in range(1): #length of your filename list
  image = my_img.eval() #here is your image Tensor :) 

Image.fromarray(np.asarray(image)).show()

coord.request_stop()
coord.join(threads)
于 2016-06-01T16:28:45.967 に答える
2

まず、scipy.misc.imread と PIL は利用できなくなりました。代わりに imageio ライブラリを使用しますが、依存関係としてそのための Pillow をインストールする必要があります

pip install Pillow imageio

次に、次のコードを使用して画像を読み込み、詳細を取得します。

import imageio
import tensorflow as tf

path = 'your_path_to_image' # '~/Downloads/image.png'

img = imageio.imread(path)
print(img.shape) 

また

img_tf = tf.Variable(img)
print(img_tf.get_shape().as_list()) 

どちらも正常に動作します。

于 2020-04-29T07:53:00.183 に答える