Tensorflow_2

概要

  • 文件IO操作
  • 神经网络

文件IO操作

  • 分类
    • 图像IO操作
    • 二进制文件IO操作
    • TFRecords文件IO操作
  • 文件IO操作流程
    • 1.读取 文件名到队列
    • 2.读取文件内容
    • 3.解码
    • 4.批处理
  • 图像数字化三要素
    • 长度,宽度,通道数
  • 示例:狗图片读取案例
import tensorflow as tf
import os


def read_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:\n", key)
    print("value:\n", value)

    # 3、解码
    image_decoded = tf.image.decode_jpeg(value)
    print("image_decode:\n", image_decoded)

    # 4、基本的数据处理
    # 缩小图片大小
    image_resized = tf.image.resize_images(image_decoded, [200, 200])
    print("image_resized:\n", image_resized)
    # 在批处理之前必须把形状固定下来
    image_resized.set_shape([200, 200, 3])
    print("image_resized:\n", image_resized)

    # 5、批处理
    # 一定要把批处理的内容放在列表当中
    image_batch = tf.train.batch([image_resized], batch_size=10, num_threads=1, capacity=10)

    return image_batch

if __name__ == "__main__":
    file_name = os.listdir("/Users/zwy/Desktop/lesson/class/dog/")
    print("file_name:\n", file_name)
    file_list = [os.path.join("/Users/zwy/Desktop/lesson/class/dog/", file) for file in file_name]
    print("file_list:\n", file_list)
    image = read_dog(file_list)

    # 开启会话
    with tf.Session() as sess:
        # 线程管理器
        coord = tf.train.Coordinator()
        # 创建线程
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        image_value = sess.run(image)
        # 图片值
        print("图片值:\n", image_value.shape)

        # 请求停止
        coord.request_stop()
        # 资源回收
        coord.join(threads)
  • 二进制文件读取案例
def read_and_decode(self, file_list):
        # 读取二进制文件
        # print("read_and_decode:\n", file_list)
        # 1、构造文件名队列
        file_queue = tf.train.string_input_producer(file_list)

        # 2、构造二进制文件阅读器
        reader = tf.FixedLengthRecordReader(self.bytes)
        key, value = reader.read(file_queue)

        print("key:\n", key)
        print("value:\n", value)
        # 3、解码
        decoded = tf.decode_raw(value, tf.uint8)
        print("decoded:\n", decoded)

        # 4、基本的数据处理
        # 切片处理,把标签值和特征值分开
        label = tf.slice(decoded, [0], [self.label_bytes])
        image = tf.slice(decoded, [self.label_bytes], [self.image_bytes])

        print("label:\n", label)
        print("image:\n", image)
        # 改变图像的形状
        image_reshaped = tf.reshape(image, [self.channels, self.height, self.width])
        # 转置
        image_transposed = tf.transpose(image_reshaped, [1, 2, 0])
        print("image_transposed:\n", image_transposed)

        # 类型转换
        label_cast = tf.cast(label, tf.float32)
        image_cast = tf.cast(image_transposed, tf.float32)

        # 5、批处理
        label_batch, image_batch = tf.train.batch([label_cast, image_cast], batch_size=10, num_threads=1, capacity=10)
        return label_batch, image_batch

  • TFRecords文件读写示例
def write_to_tfrecords(self, label_batch, image_batch):
        # 进行类型转换,转成tf.uint8
        # 为了节省空间
        label_batch = tf.cast(label_batch, tf.uint8)
        image_batch = tf.cast(image_batch, tf.uint8)
        # 构造tfrecords存储器
        with tf.python_io.TFRecordWriter("./cifar.tfrecords") as writer:
            for i in range(10):
                label = label_batch[i].eval()[0]
                image = image_batch[i].eval().tostring()
                print("tfrecords_label:\n", label)
                print("tfrecords_image:\n", image, type(image))
                # 构造example协议块
                example = tf.train.Example(features=tf.train.Features(feature={
                    "label": tf.train.Feature(int64_list=tf.train. Int64List(value=[label])),
                    "image": tf.train.Feature(bytes_list=tf.train. BytesList(value=[image]))
                }))
                # 写入序列化后的example
                writer.write(example.SerializeToString())


    def read_tfrecords(self):
        # 读取tfrecords文件
        # 1、构造文件名队列
        file_queue = tf.train.string_input_producer(["cifar.tfrecords"])

        # 2、构造tfrecords阅读器
        reader = tf.TFRecordReader()
        key, value = reader.read(file_queue)

        # 3、解析example协议块
        example = tf.parse_single_example(value, features={
            "label": tf.FixedLenFeature(shape=[], dtype=tf.int64),
            "image": tf.FixedLenFeature(shape=[], dtype=tf.string)
        })
        label = example["label"]
        image = example["image"]
        print("read_tfrecords_label:\n", label)
        print("read_tfrecords_image:\n", image)

        # 4、解码
        image_decoded = tf.decode_raw(image, tf.uint8)
        print("read_tfrecords_image_decoded:\n", image_decoded)

        # 5、基本的数据处理
        # 调整图像形状
        image_reshaped = tf.reshape(image_decoded, [self.height, self.width, self.channels])
        # 转换类型
        image_cast = tf.cast(image_reshaped, tf.float32)
        label_cast = tf.cast(label, tf.float32)
        print("read_records_image_cast:\n", image_cast)
        print("read_records_label_cast:\n", label_cast)

        # 6、批处理
        label_batch, image_batch = tf.train.batch([label_cast, image_cast], batch_size=10, num_threads=1, capacity=10)

        return label_batch, image_batch

if __name__ == "__main__":
    # 构造文件名列表
    file_name = os.listdir("./cifar-10-batches-bin")
    print("file_name:\n", file_name)
    file_list = [os.path.join("./cifar-10-batches-bin/", file) for file in file_name if file[-3:] == "bin"]
    print("file_list:\n", file_list)

    # 调用读取二进制文件的方法
    cf = Cifar()
    # label, image = cf.read_and_decode(file_list)
    label, image = cf.read_tfrecords()

    # 开启会话
    with tf.Session() as sess:
        # 创建线程协调器
        coord = tf.train.Coordinator()
        # 创建线程
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        # 打印结果
        print("label:\n", sess.run(label))
        print("image:\n", sess.run(image))

        # cf.write_to_tfrecords(label, image)
        # 回收资源
        coord.request_stop()
        coord.join(threads)

神经网络

  • 深度学习和机器学习的区别

    • 传统机器学习算法:输入-》人工特征提取-》权重学习-》预测结果
    • 深度学习算法:输入-》基础特征提取-》多层复杂特征提取-》权重学习-》预测结果
  • 感知机

    • 有n个输入数据,通过权重与各数据之间的计算和,比较激活函数结果,得出输出
    • 很容易解决与或问题
    • 就像人的神经元的作用机理
  • 可以利用tensorflow playground 来测试神经网络的作用效果

  • 神经网络

    • 在机器学习和认知科学领域,人工神经网络(artificial neural netwark,ANN),简称神经网络,或类神经网络,是一种模仿生物神经网络的结构和功能的计算模型,用于对函数进行估计或近似。
    • 神经网络的种类
      • 基础神经网络:单层感知器,线性神经网络,BP神经网络,Hopfield神经网络等
      • 进阶神经网络:玻尔兹曼机,受限玻尔兹曼机,递归神经网络等
      • 深度神经网络:深度置信网络,卷积神经网络,循环神经网络,LSTM网络等
  • 神经网络的特点:

    • 有输入层,隐层,输出层(全连接层)组成
    • 每个链接都有权值
    • 同一层神经元之间没有没有连接
    • 第N层与第N-1层的所有神经元连接,也叫全连接
  • 神经网络的组成

    • 结构
    • 激活函数
    • 损失函数
    • 学习规则
  • 示例:Mnist数据集识别

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data


# 准备数据
mnist = input_data.read_data_sets("./mnist_data", one_hot=True)


def full_connection():
    """
    用神经网络识别手写数字
    :return:
    """
    # 全连接层的矩阵运算的形状变化
    # [None, 784] * [784, 10] = [None, 10]
    # 1、准备数据的占位符
    X = tf.placeholder(dtype=tf.float32, shape=[None, 784])
    y_true = tf.placeholder(dtype=tf.float32, shape=[None, 10])

    # 2、构建神经网络模型
    # 定义权重和偏置,变量
    weights = tf.Variable(initial_value=tf.random_normal(shape=[784, 10]))
    bias = tf.Variable(initial_value=tf.random_normal(shape=[10]))
    # 构建全连接层
    y_predict = tf.matmul(X, weights) + bias

    # 3、构造损失
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))

    # 4、优化损失
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)

    # 5、准确率计算
    equal_list = tf.equal(tf.argmax(y_predict, 1), tf.argmax(y_true, 1))
    accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))

    # 初始化变量
    init = tf.global_variables_initializer()

    # 开启会话
    with tf.Session() as sess:
        # 运行初始化op
        sess.run(init)

        # 准备训练数据
        mnist_x, mnist_y = mnist.train.next_batch(50)

        # 循环运行优化器,进行训练
        for i in range(5000):
            sess.run(optimizer, feed_dict={X: mnist_x, y_true: mnist_y})
            print("第%d次训练后的误差为%f, 准确率为%f" % (i+1,
                                            sess.run(loss, feed_dict={X: mnist_x, y_true: mnist_y}),
                                            sess.run(accuracy, feed_dict={X: mnist_x, y_true: mnist_y})))

    return None




if __name__ == "__main__":
    full_connection()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值