2017-06-07 40 views
1

我是tensorflow的新手,但我已经遵循并执行了他们推广的教程以及遍布网络的许多其他人。 我在MNIST图像上做了一个小的卷积神经网络。没什么特别的,但我想测试我自己的图像。 现在我的问题来了:我创建了几个文件夹;每个文件夹的名称是图像所属的类(标签)。加载tensorflow中的图像文件夹

图像有不同的形状;我的意思是他们没有固定的大小。

我如何加载它们以使用Tensorflow?

我遵循了许多教程,并在StackOverflow和其他Q/A站点上都回答了这两个问题。但是,我仍然没有弄清楚如何做到这一点。

非常感谢。 Silvio

回答

1

样本输入管道脚本从目录加载图像和标签。在此之后,您可以进行预处理(调整图像大小等)。

import tensorflow as tf 
filename_queue = tf.train.string_input_producer(
tf.train.match_filenames_once("/home/xxx/Desktop/stackoverflow/images/*/*.png")) 

image_reader = tf.WholeFileReader() 
key, image_file = image_reader.read(filename_queue) 
S = tf.string_split([key],'/') 
length = tf.cast(S.dense_shape[1],tf.int32) 
# adjust constant value corresponding to your paths if you face issues. It should work for above format. 
label = S.values[length-tf.constant(2,dtype=tf.int32)] 
label = tf.string_to_number(label,out_type=tf.int32) 
image = tf.image.decode_png(image_file) 

# Start a new session to show example output. 
with tf.Session() as sess: 
    # Required to get the filename matching to run. 
    tf.initialize_all_variables().run() 

    # Coordinate the loading of image files. 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 

    for i in xrange(6): 
     # Get an image tensor and print its value. 
     key_val,label_val,image_tensor = sess.run([key,label,image]) 
     print(image_tensor.shape) 
     print(key_val) 
     print(label_val) 


    # Finish off the filename queue coordinator. 
    coord.request_stop() 
    coord.join(threads) 

文件目录

./images/1/1.png 
./images/1/2.png 
./images/3/1.png 
./images/3/2.png 
./images/2/1.png 
./images/2/2.png 

输出:

所有的
(881, 2079, 3) 
/home/xxxx/Desktop/stackoverflow/images/3/1.png 
3 
(155, 2552, 3) 
/home/xxxx/Desktop/stackoverflow/images/2/1.png 
2 
(562, 1978, 3) 
/home/xxxx/Desktop/stackoverflow/images/3/2.png 
3 
(291, 2558, 3) 
/home/xxxx/Desktop/stackoverflow/images/1/1.png 
1 
(157, 2554, 3) 
/home/xxxx/Desktop/stackoverflow/images/1/2.png 
1 
(866, 936, 3) 
/home/xxxx/Desktop/stackoverflow/images/2/2.png 
2 
+0

首先,感谢您的快速回复。 我试过你的代码片段,它引发了以下错误。 tensorflow.python.framework.errors_impl.OutOfRangeError:FIFOQueue '_0_input_producer' 被关闭,没有足够的元件(1请求,当前大小0) \t [[节点:ReaderReadV2 = ReaderReadV2 [_device =“/作业:本地主机/复制品: 0 /任务:0/CPU:0“](WholeFileReaderV2,input_producer)]] – SilvioBarra

+0

我认为它无法找到图像。文件夹的路径是否正确?尝试几张图片。 – hars

+1

我能够通过以下两行代码修复不足的元素错误:'sess.run(tf.local_variables_initializer())'和'sess.run(tf.global_variables_initializer())' –

相关问题