CIFAR_10处理数据——搭建模型——训练模型

本文详细介绍了如何使用TensorFlow处理CIFAR-10数据集,构建深度学习模型,并进行模型训练。通过3个主要步骤:数据预处理、模型搭建和模型训练,展示了整个流程。同时,还提供了TensorBoard的可视化截图,以便更好地理解模型的训练过程。
摘要由CSDN通过智能技术生成

概览

1.处理数据

# -*- coding:utf-8 -*-

"""
time:2017/9/22
version:0.1
"""

"""此脚本处理并加载CIFAR_10的数据:以下是函数之间调用关系:
    distorted_inputs函数:调用read_cifar_bindata;
    _generate_image_and_label_batch函数:调用distorted_inputs;

    给外部调用的方法是:
    distorted_inputs()和inputs()

"""
import os
import tensorflow as tf

# 原图像的尺度为32*32
# 这里定义了裁剪后图像的尺寸,在“数据增强”的函数中用到
# 更改该数据(尺寸信息)需要重新定义全连接层的参数
HEIGHT_SIZE = 24
WIDTH_SIZE = 24
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 50000
NUM_EXAMPLES_PER_EPOCH_FOR_TEST = 10000
NUM_CLASSES = 10


def read_cifar_bindata(filename_queue):
    """ 读取并解析cifar的数据文件
    参数:要读取的文件队列的文件名(String)
    返回:
    图像的height、width、depth值
    文件队列的key
    label:一维张量
    uint8imgmat:uint8类型的[height, width, depth]的矩阵
    """
    class read_cifar10(object):
    # pass就是一条空语句。定义函数的时候, 如果没有内容, 可以先写pass, 这样不会报错。
        pass

    result = read_cifar10() # 一个Img类的实例
    label_bytes = 1
    # 返回的图像数据的各种参数
    result.height = 32
    result.width = 32
    result.depth = 3
    image_bytes = result.height * result.width * result.depth
    # 每条记录都包含标签数据和图像数据.
    record_bytes = label_bytes + image_bytes

    # 定义一个Reader,FixedLengthRecordReader:它每次能从文件中读取固定字节数
    # CIFAR - 10的数据格式中没有header 和 footer,
    # 所以header_bytes与footer_bytes采用默认值None,
    reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
    # read函数:返回reader中的下一条数据(key, value pair)
    # 在读取新数据时,之前的文件名会出队
    result.key, value_str = reader.read(filename_queue)
    # decode_raw函数:读二进制文件,把字符串中的字节转换为数值向量,每一个数值
    # 占用一个字节,在[0, 255]区间内,因此out_type要取uint8类型
    tensor_bytes = tf.decode_raw(bytes=value_str, out_type=tf.uint8)

    # 因为value中包含了label和image,故要对向量类型tensor进行'slice'操作
    # 第一个字节是label,取出并转换为int32类型
    result.label = tf.cast(tf.strided_slice(tensor_bytes, [0], [label_bytes]), tf.int32)

    # label字节之后的字节,都是与图像有关的(二进制中的数据顺序是制作二进制文件时决定的)
    # 1. 先将slice(切片)出来的1—D 张量reshape为[depth, height, width]的矩阵
    slice_data_mat = tf.reshape(
        tf.strided_slice(tensor_bytes, [label_bytes],
                         [label_bytes + image_bytes]),
        [result.depth, result.height, result.width])
    # 2. 再将上一步返回的矩阵 变换为 :[height, width, depth],便于之后的卷积计算
    result.uint8imgmat = tf.transpose(slice_data_mat, [1, 2, 0])
    return result


def _generate_image_and_label_batch(image, label, min_queue_examples,
                                    batch_size, shuffle):
  """ 生成图像batch和标签batch
  参数:
    image: 3-D Tensor of [height, width, 3] of type.float32.
    label: 1-D Tensor of type.int32
    min_queue_examples: int32, 要在提供的batches中保留的最小样本数
    batch_size:略
    shuffle: 布尔类型  true——使用shuffle打乱队列,false则不适用
  返回:
    images: Images. 4D tensor of [batch_size, height, width, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  # 使用创建一个被打乱(或不打乱)的队列Create a queue that shuffles the examples, and then
  # read 'batch_size' images + labels from the example queue.

  """
  tf.train.shuffle_batch():
      随机地打乱队列中的tensors来创建batches(也即每次可以读取多个data文件中的
      样例构成一个batch)。
    * capacity参数:用于控制打乱queue的最大长度;
    * min_after_dequeue参数:进行一次dequeue操作后队列中剩余tensorflow的最小数量
     (这样就可以确保batch中元素的随机性);
    * num_threads参数:用于指定多少个threads负责压tensors到队列;
  tf.train.batch():
      与前者类似,只不过顺序地出队列(也即每次只能从一个data文件中读取batch),少了随机性。
  """
  # 强烈建议大家看下这两个函数的文档。应该可以解决你此时心中的疑惑
  if shuffle:
      images, label_batch = tf.train.shuffle_batch(
          [image, label],
          batch_size=batch_size,
          num_threads=6,
          capacity=min_queue_examples + 3 * batch_size,
          min_after_dequeue=min_queue_examples)
  else:
      images, label_batch = tf.train.batch(
          [image, label],
          batch_size=batch_size,
          num_threads=6,
          capacity=min_queue_examples + 3 * batch_size)
  <
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值