21个项目玩转深度学习 第二章 CIFAR10

  1. 首先介绍第一个文件infar10_input.py,用途:在tensorflow中读取人CIFAR-10训练图片。

这个IMAGE_SIZE=24并不是原始图片的大小,而是接下来要裁剪成的大小

IMAGE_SIZE = 24

NUM_CLASSES = 10
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 50000
NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = 10000

接下来介绍第一个自定义函数

ARGS:
     filename_queue:包含要读取的文件名的字符串队列。

返回:
      表示单个示例的对象,包含以下字段:
       height:结果中的行数(32)
       width:结果中的列数(32)
       depth:结果中的颜色通道数(3)
       key:标量字符串Tensor描述文件名和记录号这个例子。
       label:一个int32 Tensor,标签范围为0..9。
       uint8image:一个 [高度,宽度,深度] uint8张量图像数据

给我一个要读取的文件名队列,返回result对象,包含了很多属性

def read_cifar10(filename_queue):

首先创建一个继承于object的基类,创建一个result的对象,对象属性有height=32,width=32,depth=3,lable_bytes=1,

image_bytes=32*32*3,record_bytes = lable_bytes + image_bytes,创建一个分词式reader,每次分词record_bytes个,读出的结果是result.key和value,value是字符串类型,解码value到tf.uint8类型,并赋值给recor_bytes,把record_bytes分离出一个字节并转换为tf.int32类型,赋值给result.lable,分离出第二部分,并且转化为[result.depth,result.height,result.width],随后转换为【高,宽,深】,并赋值给result.unit8image,这个属性是unit8格式的。最后返回result这个对象!

需要传入上个函数所得到的image,lable,以及给定队列中事例最少个数和batch_size,返回打包的的images,和打包的lable

images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
labels: Labels. 1D tensor of [batch_size] size.

def _generate_image_and_label_batch(image, label, min_queue_examples,
                                                                      batch_size, shuffle):

总之:创建一个打乱的示例的队列,然后从示例队列中读取“batch_size”图像+标签。

 num_preprocess_threads = 16    

如果打乱:

              images, label_batch = tf.train.shuffle_batch( [image, label],

                                                                                            batch_size=batch_size,
                                                                                            num_threads=num_preprocess_threads,
                                                                                            capacity=min_queue_examples + 3 * batch_size,
                                                                                            min_after_dequeue=min_queue_examples)

如果没有打乱:

                    images, label_batch = tf.train.batch( [image, label],
                                                                                    batch_size=batch_size,
                                                                                    num_threads=num_preprocess_threads,
                                                                                    capacity=min_queue_examples + 3 * batch_size)

返回成批次的images,lable _batch,其中有一点不明白,tf.reshape(label_batch, [batch_size])???images 却没有reshape

 

def distorted_inputs(data_dir, batch_size):

 首先把data_batch1到data_batch5文件列表生成队列,调用read_cifar10(filename_queue),得到结果read_input,并把read_input的uint8image属性转换为tf.float32类型,赋值给reshaped_image,把reashped_image进行裁剪成24*24*3形状,并进行左右翻转,提升对比度等等一系列操作,  float_image.set_shape([height, width, 3],  read_input.label.set_shape([1]),调用上个函数,打包成batchimages和lable并返回


def inputs(eval_data, data_dir, batch_size):

跟上面一样做同样的操作,不同点1:上一个函数是打乱的,而这个是没有打乱的。不同点2:这个函数有eval_data,这个一点目前并不太明白

猜你喜欢

转载自blog.csdn.net/weixin_42127577/article/details/83542968
今日推荐