2016-01-14 6 views
9

मैं अभी टेंसफोर्लो से शुरू कर रहा हूं और मेरे पास एक नौसिखिया प्रश्न है।टेन्सफोर्लो में छवि फ़ाइलों को सहेजना

मुझे पता है कि टेन्स्फोर्लो सभी तंत्रिका जाल के बारे में है लेकिन मैं बस इसके यांत्रिकी के साथ शुरू कर रहा हूं। मैं इसे दो छवियों को लोड, आकार बदलने, फ़्लिप करने और सहेजने की कोशिश कर रहा हूं। एक सरल ऑपरेशन होना चाहिए, ठीक है, और यह मुझे मूल बातें के साथ शुरू कर देता है।

यहाँ अब तक मेरी कोड है:

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() 

यह ठीक काम करता है, दो छवियों का आकार और उन्हें बचत। लेकिन यह हमेशा एक त्रुटि के साथ समाप्त होता है:

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() को दो बार कॉल कर रहा हूं।

धन्यवाद और मुझे यकीन है कि यह एक बहुत ही बुनियादी सवाल है लेकिन मुझे समझ में नहीं आता कि यह कैसे काम करता है।

संपादित करें:

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")) 

उपरोक्त कोड में, आप स्पष्ट रूप से num_epochs = 2 डाल अगर, एपीआई पता चलता है तो के रूप में, string_tensor में तार 2 बार के माध्यम से string_input_producer चक्र। चूंकि string_tensor में केवल 2 फ़ाइल नाम हैं, कतार 4 फ़ाइल नाम से भरी है। अगर मैं लूप को बदलता हूं:

for i in xrange(5) 

तो यह बग आउट हो जाएगा। हालांकि, अगर मैं इसे 4 पर छोड़ देता हूं, तो यह ठीक होगा। एक और उदाहरण ले लो। अगर मैं num_epochs नहीं डालता, तो सुझाव दिया गया है, यह असीमित संख्या के माध्यम से चक्र कर सकते हैं। डालना:

for i in xrange(100) 

इस प्रकार बग आउट नहीं होता है। हम उम्मीद करते है कि यह आपके सवाल का जवाब दे देगा।

संपादित करें: मुझे एहसास हुआ कि आपके पास और प्रश्न हैं।

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.

यदि आप मूल फ़ाइल नामों को संरक्षित करना चाहते हैं, तो आपकी विधि को फ़ाइल नाम वापस करने की आवश्यकता है। नीचे दिया गया कोड यहाँ है।

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(os.getcwd()) 

। विशेष रूप से जेपीजी, पीएनजी फ़ाइल प्रकारों को फ़िल्टर करने के लिए os.listdir के एपीआई की जांच करें। एक बार जब आप इसे प्राप्त कर लेंगे, तो आप एक साधारण लंबाई ऑपरेशन को कॉल कर सकते हैं और कर सकते हैं:

for i in xrange(len(number_of_elements)) 
संबंधित मुद्दे