• AI学习---数据IO操作&神经网络基础


    image

    image

    数据IO操作

    TF支持3种文件读取:
        1.直接把数据保存到变量中
        2.占位符配合feed_dict使用
        3. QueueRunner(TF中特有的)

    image

    文件读取流程

    文件读取流程(多线程 + 队列)
    1)构造文件名队列(先读取文件名到队列,方便快速读取文件)
        file_queue = tf.train.string_input_producer(string_tensor,shuffle=True)
    2)读取与解码(读取器根据文件名读取数据,以一个样本为一个单位进行读取,返回一个实例reader)
        文本(一行文字):
            读取:tf.TextLineReader()
            解码:tf.decode_csv()
        图片(一张图片):
            读取:tf.WholeFileReader()
            解码:
                tf.image.decode_jpeg(contents)   # 将jpeg编码的图片解码为unit8
                tf.image.decode_png(contents)    # 将png编码的图片解码为unit8
        二进制(固定数量字节):
            读取:tf.FixedLengthRecordReader(record_bytes)
            解码:tf.decode_raw()
        TFRecords类型(TF自定义的)
                读取:tf.TFRecordReader()
        说明:读取器有一个通用的方法read(),返回一个元组

            key, value = 读取器.read(file_queue)
                  key:文件名(指明文件在哪个文件名中)
                  value:一个样本
        3)批处理队列
             tf.train.batch(tensors, batch_size, num_threads = 1, capacity = 32, name=None)
        4)手动开启线程
            队列操作对象:tf.train.QueueRunner()
            开启会话:
                tf.train.start_queue_runners(sess=None, coord=None)

    image

    1、构造文件名队列

    image

    2、读取与解码

    image

    image

    3、批处理

    image

    线程操作

    image

    图片数据

    图像基本知识
        0 文本特征处理
            文本  -->利用特征词(某个单词,可统计数量Count,可统计重要程度TF-IDF) -> 二维数组shape(n_samples行,m_features列)
            字典  –> one-hot编码  -> 二维数组
            图片  像素值(组成图片的最基本单位就是像素)
        1 图片三要素(长度+宽度+通道数)
            黑白图、灰度图
                一个通道
                    黑[0, 255]白
            彩色图
                三个通道
                    一个像素点 由三个通道值构成,每一个通道是一个灰度图,3个组合在一起就是一个彩色图的像素点
                    R [0, 255]
                    G [0, 255]
                    B [0, 255]
        2 TensorFlow中表示图片
            Tensor对象
                指令名称、形状、类型
                shape = [height, width, channel]
        3 图片特征值处理
            [samples, features]
            为什么要缩放图片到统一大小?
                1)每一个样本特征数量要一样多
                2)缩小图片的大小
            tf.image.resize_images(images, size)
        4 数据格式
            存储:uint8
            训练:float32

    1、图片三要素(长+宽+通道数)

    image

    点击查看图片的某一个像素点(左:灰度图, 右:彩色图)

    imageimage

    2、张量(类似ndarray)形状--》3D

    image

    3、图片特征值处理

          iris数据集: 150个样本 4个特征值 目标值

          图片数据集:1个样本有500*500*3个特征(像素点) + 每个图片的特征值不一样 –》所以需要进行大小转换

    image

    4、数据格式

    image

    狗图片读取Demo

    image

    import tensorflow as tf
    import os
    
    os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'
    
    
    def read_picture_dog(file_list):
        """
        读取狗图片案例
        :return:
        """
        # 1、构造文件名队列
        file_queue = tf.train.string_input_producer(file_list)
        # 2、读取与解码
        # 读取
        reader = tf.WholeFileReader()
        key, value = reader.read(file_queue)
        print("key:
    ", key)
        print("value:
    ", value)
    
        # 解码
        image_decoded = tf.image.decode_jpeg(value)
        print("image_decoded:
    ", image_decoded)
    
        # 3、图像形状、类型修改
        # 将图片缩放到同一个大小
        image_resized = tf.image.resize_images(image_decoded, [200, 200])  # 到此仅确定了长和宽,未确定通道
        print("image_resized_before:
    ", image_resized)
    
        # 更新静态形状
        image_resized.set_shape([200, 200, 3])   # 第三个的通道一定是3
        print("image_resized_after:
    ", image_resized)
    
        # 4、批处理队列
        image_batch = tf.train.batch([image_resized], batch_size=100, num_threads=2, capacity=100)
        print("image_batch:
    ", image_batch)
    
        # 5、开始会话
        with tf.Session() as sess:
            # key_new, value_new = sess.run([key, value])
            # print('key_new
    ', key_new)    # 开启单线程的情况下,会阻塞,因为只有一个queue现场,而前面已经运行中
            # print('value_new
    ', value_new)
    
            # 开启线程
            #  ->构造线程协调器
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    
            # 运行
            filename, sample, image, n_image = sess.run([key, value, image_resized, image_batch])
            print("filename:
    ", filename)
            print("sample:
    ", sample)
            print("image:
    ", image)
            print("n_image:
    ", n_image)
    
            coord.request_stop()
            coord.join(threads)  # 回收线程
    
    
    if __name__ == "__main__":
        # 0、构造文件路径 + 文件名列表
        filename = os.listdir('F:\linear')
        file_list = [
            os.path.join('F:\linear', x) for x in filename
        ]
        print('文件名列表:', filename)
        print('文件路径:', file_list)
        read_picture_dog(file_list)
    

    二进制数据

    • CIFAR-10数据集介绍

          CIFAR-10数据集由10个类中的60000个32x32彩色图像组成,每个类有6000个图像。有50000个训练图像和10000个测试图像。
    数据集分为五个训练批次和一个测试批次,每个批次有10000个图像。测试批次包含来自每个类别的1000个随机选择的图像。训练批次以随机顺序包含剩余图像,但是一些训练批次可能包含来自一个类别的更多图像而不是另一个类别。在他们之间,训练批次包含来自每个班级的5000个图像。

    http://www.cs.toronto.edu/~kriz/cifar.html

    image

    • 二进制版本

    每3072个字节构成一个样本 1个目标值 + 3072个字节

    image

    image

    • CIFAR10 二进制数据读取
              流程分析:
                  1)构造文件名队列
                  2)读取与解码
                  3)批处理队列
                  开启会话
                  手动开启线程

    image

    • 图片转置

    image

    image

    demo:

    import tensorflow as tf
    import os
    
    os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'
    
    
    class Cifar():
        def __init__(self):
            # 设置图像大小
            self.width = 32
            self.height = 32
            self.channel = 3
    
            # 设置图像字节数
            self.image = self.height * self.width * self.channel  # 一个图的字节大小
            self.label = 1  # 标签字节数(范围0-9)
            self.sample = self.image + self.label  # 一个样本的大小 = 一个标签 + 一个图
    
        def read_binary(self, file_list):
            # 1、 构建文件名队列
            file_queue = tf.train.string_input_producer(file_list)
            # 2、 文件读取与解码
            # 读取
            reader = tf.FixedLengthRecordReader(self.sample)
            key, value = reader.read(file_queue)
            print('key
    ', key)  # Tensor("ReaderReadV2:0", shape=(), dtype=string)
            print('value
    ', value)
            # 解码
            image_decoded = tf.decode_raw(value, tf.uint8)
            print("image_decoded:
    ", image_decoded)  # 一阶 Tensor("DecodeRaw:0", shape=(?,), dtype=uint8)
    
            # 切片操作
            label = tf.slice(image_decoded, [0], [self.label])
            image = tf.slice(image_decoded, [self.label], [self.image])
            print("label:
    ", label) # Tensor("Slice:0", shape=(1,), dtype=uint8) ,1维,
            print("image:
    ", image) # Tensor("Slice_1:0", shape=(3072,), dtype=uint8), 1维,3072字节
    
            # 调整图像的形状
            image_reshaped = tf.reshape(image, [self.channel, self.height, self.width])
            print("image_reshaped:
    ", image_reshaped)  # 改变阶数, Tensor("Reshape:0", shape=(3, 32, 32), dtype=uint8)
    
            # 三维数组的转置(原数组的顺序不符合 TF的要求,转换后维)
            image_transposed = tf.transpose(image_reshaped, [1, 2, 0])
            print("image_transposed:
    ", image_transposed)
    
            # 调整图像类型
            # image_cast = tf.cast(image_transposed, tf.float32)
    
            # 3、构造批处理队列
            image_batch, label_batch = tf.train.batch([image_transposed, label], batch_size=100, num_threads=2,
                                                      capacity=100)
    
            # 开启会话
            with tf.Session() as sess:
                # 开启线程
                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(sess=sess, coord=coord)
                key_new, value_new, image_decoded, label_new, image_new, image_reshaped_new,
                image_transposed_new,image_batch_new, label_batch_new = sess.run([key, value,
                        image_decoded, label, image, image_reshaped, image_transposed, image_batch, label_batch])
                print('key_new
    ', key_new)  # Tensor("ReaderReadV2:0", shape=(), dtype=string)
                print('value_new
    ', value_new)  # b'x03x9ex9fxa5xa6xa0
                print('image_decoded
    ', image_decoded)  # [3 158 159 ... 124 129 110] --》 3是类别,158,159是目标和特征值,需切片
                print('label_new
    ', label_new)  # [6]
                print('image_new
    ', image_new)  # [ 59  43  50 ... 140  84  72]
                print('image_reshaped_new
    ', image_reshaped_new)
                print('image_transposed_new
    ', image_transposed_new)
                print('image_batch_new
    ', image_batch_new)
                print('label_batch_new
    ', label_batch_new)
    
                coord.request_stop()
                coord.join(threads)  # 回收线程
            return None
    
    
    if __name__ == "__main__":
        filename = os.listdir('F:\linear')
        file_list = [
            os.path.join('F:\linear', x) for x in filename if x[-3:] == 'bin'
        ]
        print('filename
    ', filename)
        print('file_list
    ', file_list)
    
        cifar = Cifar()
        cifar.read_binary(file_list)
    

    TFRecords

    神经网络基础

    神经网络原理

    image

    Softmax回归

    image

    softmax特点:神经网络的输出变成了概率输出

    image

    image

     

    交叉熵损失

    image

    image

    image

    image

    image

    logits: 线性值+权重的线性值

    Mnist手写数字识别案例

    image

    1、数据集介绍

    image

    1、特征值:

        特征值就是我们的图片(28*28个像素),图片就是以28*28*1的形式存储,根据书写的轨迹进行编码(即把数字1用0.1~1.0的数字进行标识,完全笔直用1,稍微偏一点用0.5标识)。一个手写识别图片包含图片和它对应的标签,设置图片为xs,标签为ys。样本数共55000

    image

    2、目标值

        Minist的每个图片都有对应的标签,0~9之间的数字表示图像中绘制的数字,利用one-hot编码,共[55000,10]个样本

    image

    3、Mnist数据获取API

    image

    Mnist手写数字的识别:

    image

          图片数字1  --》 输入特征值28*28*1  --》每个特征都有权重值,进行加权求和(y=w1x1+w2x2+b)  -->获取logits  -->进行softmax进行分类

    image

    image

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    
    
    def full_connection():
        """
        用全连接对手写数字进行识别
        :return:
        """
        # 1)准备数据
        mnist = input_data.read_data_sets("F:mnist_data", one_hot=True)
        # 用占位符定义真实数据
        X = tf.placeholder(dtype=tf.float32, shape=[None, 784])
        y_true = tf.placeholder(dtype=tf.float32, shape=[None, 10])
    
        # 2)构造模型 - 全连接
        # [None, 784] * W[784, 10] + Bias = [None, 10]
        weights = tf.Variable(initial_value=tf.random_normal(shape=[784, 10], stddev=0.01))
        bias = tf.Variable(initial_value=tf.random_normal(shape=[10], stddev=0.1))
        y_predict = tf.matmul(X, weights) + bias
    
        # 3)构造损失函数
        loss_list = tf.nn.softmax_cross_entropy_with_logits(logits=y_predict, labels=y_true)
        loss = tf.reduce_mean(loss_list)
    
        # 4)优化损失
        # optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)
        optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)
    
        # 5)增加准确率计算
        bool_list = tf.equal(tf.argmax(y_true, axis=1), tf.argmax(y_predict, axis=1))
        accuracy = tf.reduce_mean(tf.cast(bool_list, tf.float32))
    
        # 初始化变量
        init = tf.global_variables_initializer()
    
        # 开启会话
        with tf.Session() as sess:
    
            # 初始化变量
            sess.run(init)
    
            # 开始训练
            for i in range(50):
                # 获取真实值
                image, label = mnist.train.next_batch(50)
    
                _, loss_value, accuracy_value = sess.run([optimizer, loss, accuracy], feed_dict={X: image, y_true: label})
    
                print("第%d次的损失为%f,准确率为%f" % (i+1, loss_value, accuracy_value))
    
    
        return None
    
    if __name__ == "__main__":
        full_connection()

    ===

  • 相关阅读:
    NOIP 模拟 $94; m 超级加倍$
    NOIP 模拟 $94; m 欢乐豆$
    NOIP 模拟 $94; m 叁仟柒佰万$
    NOIP 模拟 $94; m 开挂$
    NOIP 模拟 $92; m 优美的旋律$
    NOIP 模拟 $92; m 翻转游戏$
    NOIP 模拟 $92; m 石子合并$
    NOIP 模拟 $90; m 校门外歪脖树上的鸽子$
    NOIP 模拟 $90; m 混乱邪恶$
    NOIP 模拟 $90; m 快速排序$
  • 原文地址:https://www.cnblogs.com/ftl1012/p/10540933.html
Copyright © 2020-2023  润新知