zoukankan      html  css  js  c++  java
  • TensorFlow基础笔记(1) 数据读取与保存

    https://zhuanlan.zhihu.com/p/27238630

    WholeFileReader

    # 我们用一个具体的例子感受tensorflow中的数据读取。如图,
    # 假设我们在当前文件夹中已经有A.jpg、B.jpg、C.jpg三张图片,
    # 我们希望读取这三张图片5个epoch并且把读取的结果重新存到read文件夹中。
    
    # 导入tensorflow
    import tensorflow as tf
    
    # 新建一个Session
    with tf.Session() as sess:
        # 我们要读三幅图片A.jpg, B.jpg, C.jpg
        filename = ['./data/A.png', './data/B.png', './data/C.png']
        # string_input_producer会产生一个文件名队列
        filename_queue = tf.train.string_input_producer(filename, shuffle=True, num_epochs=5)
        # reader从文件名队列中读数据。对应的方法是reader.read
        reader = tf.WholeFileReader()
        key, value = reader.read(filename_queue)
        # tf.train.string_input_producer定义了一个epoch变量,要对它进行初始化
        tf.local_variables_initializer().run()
        # 使用start_queue_runners之后,才会开始填充队列
        threads = tf.train.start_queue_runners(sess=sess)
        i = 0
        while True:
            i += 1
            # 获取图片数据并保存
            image_data = sess.run(value)
            with open('data/test_%d.jpg' % i, 'wb') as f:
                f.write(image_data)

    http://blog.csdn.net/wayne2019/article/details/77884478

    import tensorflow as tf
    import os
    import matplotlib.pyplot as plt
    
    def file_name(file_dir):   #来自http://blog.csdn.net/lsq2902101015/article/details/51305825
        for root, dirs, files in os.walk(file_dir):  #模块os中的walk()函数遍历文件夹下所有的文件
            print(root) #当前目录路径  
            print(dirs) #当前路径下所有子目录  
            print(files) #当前路径下所有非目录子文件  
    
    def file_name2(file_dir):   #特定类型的文件
        L=[]   
        for root, dirs, files in os.walk(file_dir):  
            for file in files:  
                if os.path.splitext(file)[1] == '.png':   
                    L.append(os.path.join(root, file))  
        return L 
    
    file_name('data')
    path = file_name2('data')
    print(path)
    
    #以下参考http://blog.csdn.net/buptgshengod/article/details/72956846 (十图详解TensorFlow数据读取机制)
    #以及http://blog.csdn.net/uestc_c2_403/article/details/74435286
    
    file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #创建输入队列  
    image_reader = tf.WholeFileReader()  
    key, image = image_reader.read(file_queue)  
    image = tf.image.decode_jpeg(image)  
    
    with tf.Session() as sess:  
        tf.local_variables_initializer().run()
        threads = tf.train.start_queue_runners(sess=sess)
        for _ in path+path:
            plt.figure
            plt.imshow(image.eval())
            plt.show()

     read_file

    import tensorflow as tf
    import os
    import matplotlib.pyplot as pltimport numpy as np
    
    print(tf.__version__)
    
    image_value = tf.read_file('data/A.png')
    img = tf.image.decode_jpeg(image_value, channels=3)
    
    with tf.Session() as sess:
        print(type(image_value)) # bytes
        print(type(img)) # Tensor
        print(type(img.eval())) # ndarray !!!
        print(img.eval().shape)
        print(img.eval().dtype)
        plt.figure(1)
        plt.imshow(img.eval())
        plt.show()

     gfile.FastGFile

    import matplotlib.pyplot as plt
    import tensorflow as tf
    import numpy as np
    
    print(tf.__version__)
    
    image_raw = tf.gfile.FastGFile('data/A.png','rb').read()   #bytes
    img = tf.image.decode_jpeg(image_raw)  #Tensor
    #img2 = tf.image.convert_image_dtype(img, dtype = tf.uint8)
    
    with tf.Session() as sess:
        print(type(image_raw)) # bytes
        print(type(img)) # Tensor
        #print(type(img2))
    
        print(type(img.eval())) # ndarray !!!
        print(img.eval().shape)
        print(img.eval().dtype)
    
    #    print(type(img2.eval()))
    #    print(img2.eval().shape)
    #    print(img2.eval().dtype)
        plt.figure(1)
        plt.imshow(img.eval())
        plt.show()
  • 相关阅读:
    truncate table
    SSIS学习笔记
    Bing Developer Assistant开发随记
    数组中的逆序对
    第一个只出现一次的字符
    丑数
    把数组排成最小的数
    连续子数组的最大和
    最小的k个数
    数组中出现次数超过一半的数字
  • 原文地址:https://www.cnblogs.com/adong7639/p/7609741.html
Copyright © 2011-2022 走看看