2016-01-14 21 views
9

私はTensorflowを使い始めています。初心者の質問があります。Tensorflowで画像ファイルを保存する

私はTensorflowはすべて神経網に関するものだと知っていますが、私はそれの力学だけで始まります。私は2つの画像を読み込み、サイズ変更し、反転して保存するようにしています。簡単な操作でなければならない、そして、それは私が基本から始めることを可能にする。

はここで、これまでに私のコードです:

import tensorflow as tf 
import numpy as np 

print("resizing images") 

filenames = ['img1.png', 'img2.png' ] 
filename_queue = tf.train.string_input_producer(filenames, num_epochs=1) 

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

resized = tf.image.resize_images(images, 180,180, 1) 
resized.set_shape([180,180,3]) 

flipped_images = tf.image.flip_up_down(resized) 

resized_encoded = tf.image.encode_jpeg(flipped_images,name="save_me") 

init = tf.initialize_all_variables() 
sess = tf.Session() 

with sess.as_default(): 
    tf.train.start_queue_runners() 
    sess.run(init) 

    f = open("/tmp/foo1.jpeg", "wb+") 
    f.write(resized_encoded.eval()) 
    f.close() 

    f = open("/tmp/foo2.jpeg", "wb+") 
    f.write(resized_encoded.eval()) 
    f.close() 

それは正常に動作し、2枚の画像をリサイズし、それらを保存します。しかし、それは常にエラーで終了します:

W tensorflow/core/common_runtime/executor.cc:1076] 0x7f97240e7a40 
Compute status: Out of range: Reached limit of 1 

私は明らかに何か間違っています。私がnum_epochs = 1を離れると、エラーなしで終了します。

私はいくつか質問があります:

これはどうやって正しく行うのですか?

また、元のファイル名をfilename_queueから最後まで保存して元の名前で保存できるようにするにはどうすればよいですか?また、保存する必要のあるファイルの数を確認するにはどうすればよいですか?ディレクトリを読み込んでファイル名のリストを作っているとしましょう。私はいろいろなことを試みましたが、私が終わりに達したときに私がどのように知っているかは決して分かりません。

resized_encoded.eval()を2回呼び出すのは奇妙に思えます。

これは非常に基本的な質問ですが、私はこれがどのように機能するのか理解できません。

編集:これは、同じ警告を与える

import tensorflow as tf 
import numpy as np 

filenames = ['file1.png', 'file2.png' ] 

filename_queue = tf.train.string_input_producer(filenames, 
         num_epochs=1, name="my_file_q") 

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

sess = tf.Session() 

with sess.as_default(): 
    print("session started") 

    sess.run(init) 

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

    for i in range (2): 
    print(key.eval()) 

    coord.request_stop() 
    coord.join(threads) 

:私は行動のさらに簡単なデモを作成しました。なぜ私は理解できません。

答えて

5

この警告は完全に正常です。 TensorFlow API

num_epochs: An integer (optional). If specified, string_input_producer produces each string from string_tensor num_epochs times before generating an OutOfRange error. If not specified, string_input_producer can cycle through the strings in string_tensor an unlimited number of times.

なぜこれが重要なのかを尋ねることがあります。私はあなたのコードをおそらくもっと理解できるものにリファクタリングしました。私に説明させてください。上記のコードで

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

cur_dir = os.getcwd() 
print("resizing images") 
print("current directory:",cur_dir) 

def modify_image(image): 
    resized = tf.image.resize_images(image, 180, 180, 1) 
    resized.set_shape([180,180,3]) 
    flipped_images = tf.image.flip_up_down(resized) 
    return flipped_images 

def read_image(filename_queue): 
    reader = tf.WholeFileReader() 
    key,value = reader.read(filename_queue) 
    image = tf.image.decode_jpeg(value) 
    return image 

def inputs(): 
    filenames = ['img1.jpg', 'img2.jpg' ] 
    filename_queue = tf.train.string_input_producer(filenames,num_epochs=2) 
    read_input = read_image(filename_queue) 
    reshaped_image = modify_image(read_input) 
    return reshaped_image 

with tf.Graph().as_default(): 
    image = inputs() 
    init = tf.initialize_all_variables() 
    sess = tf.Session() 
    sess.run(init) 
    tf.train.start_queue_runners(sess=sess) 
    for i in xrange(2): 
     img = sess.run(image) 
     img = Image.fromarray(img, "RGB") 
     img.save(os.path.join(cur_dir,"foo"+str(i)+".jpeg")) 

は、明示的にAPIがstring_tensor 2倍の文字列によって、string_input_producerサイクルを提案し、その後として、= 2 num_epochsを置く場合。 string_tensorには2つのファイル名しかないので、キューには4つのファイル名が入ります。 forループを次のように変更した場合:

for i in xrange(5) 

これはバグです。しかし、私が4でそれを残すと、それはうまくいくでしょう。さらに別の例を考えてみましょう。私がnum_epochsを入れないと、示唆されているように、無制限の回数だけ循環することができます。パッティング:

for i in xrange(100) 

したがって、バグが発生しません。これがあなたの質問に答えることを願っています

EDIT:あなたにはさらに質問があることがわかりました。

Also, if I want to preserve the original file names all the way from the filename_queue through to the end so I can save them with the original names, how do I do that? And how do I know how many files I need to save? Let's say I'm making the list of file names by reading a directory. I tried many different things but I could never find out how I know when I reach the end.

あなたは、元のファイル名を保持したい場合は、あなたの方法は、ファイル名を返す必要があります。以下にコードを示します。これは、ディレクトリ内のすべてのファイルを一覧表示します

os.listdir(os.getcwd()) 

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

cur_dir = os.getcwd() 
print("resizing images") 
print("current directory:",cur_dir) 

def modify_image(image): 
    resized = tf.image.resize_images(image, 180, 180, 1) 
    resized.set_shape([180,180,3]) 
    flipped_images = tf.image.flip_up_down(resized) 
    return flipped_images 

def read_image(filename_queue): 
    reader = tf.WholeFileReader() 
    key,value = reader.read(filename_queue) 
    image = tf.image.decode_jpeg(value) 
    return key,image 

def inputs(): 
    filenames = ['img1.jpg', 'img2.jpg' ] 
    filename_queue = tf.train.string_input_producer(filenames) 
    filename,read_input = read_image(filename_queue) 
    reshaped_image = modify_image(read_input) 
    return filename,reshaped_image 

with tf.Graph().as_default(): 
    image = inputs() 
    init = tf.initialize_all_variables() 
    sess = tf.Session() 
    sess.run(init) 
    tf.train.start_queue_runners(sess=sess) 
    for i in xrange(10): 
     filename,img = sess.run(image) 
     print (filename) 
     img = Image.fromarray(img, "RGB") 
     img.save(os.path.join(cur_dir,"foo"+str(i)+".jpeg")) 

は、あなただけの線に沿って何かを呼び出すことができ、あなたが保存する必要がどのように多くのファイルを知るために。 os.listdirのAPIをチェックして、特にJPG、PNGファイルタイプをフィルタリングしてください。これを取得したら、簡単な長さの操作を呼び出して、次の操作を行うことができます。

for i in xrange(len(number_of_elements))