我如何在张量流中加载png目录? [英] how can i load a directory of png in tensorflow?

查看:112
本文介绍了我如何在张量流中加载png目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个png文件目录。有一个火车文件夹和测试文件夹。在train文件夹中,我有10个文件夹作为10个标签[0 -9]。每个文件夹都包含该标签的png文件。我想将它们加载到张量流中进行训练。我是张量流的新手,我很难做到这一点

i have a directory of png files . there is a train folder and test folder . In the train folder i have 10 folders as 10 labels [ 0 -9 ] .Each folder contains png files of that label . I want to load them in tensor flow for training . I am new in tensor flow i am having a very hard time getting this done

我正在使用anaconda(py ver 3.5)

i am using anaconda ( py ver 3.5 )

import tensorflow as tf


filename_queue = tf.train.string_input_producer(
    tf.train.match_filenames_once("./images/*.jpg"))

image_reader = tf.WholeFileReader()

我尝试使用它,但是可以使它工作。只能加载1张图片

i have tried using this but can make it work . it only loads 1 image

推荐答案

尽管它对我有用。您可以运行此脚本吗? (也更新为获取标签)

It's working for me though. Can you run this script ? (Updated to get Labels as well)

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

这篇关于我如何在张量流中加载png目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆