代码: /tensorflow_models/tensorflow_models/tutorials/image/cifar10
CIFAR10数据集介绍:
CIFAR10
图片: 32x32x3
lable: 0~9
所以CIFAR10一条数据大小:
32*32*3+1
CIFAR10包下面有:
data_batch_1.bin
data_batch_2.bin
data_batch_3.bin
data_batch_4.bin
data_batch_5.bin
test_batch.bin
global_step = tf.get_variable(
'global_step', [],
initializer=tf.constant_initializer(0), trainable=False)
这个目的是获取global_step 存储节点,如果不存在,会创建。
initializer 初始化
trainable=False 表示该存储节点不参与训练
lr = tf.train.exponential_decay(
learning_rate,
global_step,
decay_steps,
decay_rate,
staircase=False, # True 以离散时间衰减学习率
name=None
)
这个函数主要处理学习率指数衰减,进过exponential_decay处理之后学习率为:
lr = learning_rate * decay_rate^(global_step/decay_steps)
opt = tf.train.GradientDescentOptimizer(lr)
这个是优化器,
tf.train.GradientDescentOptimizer
__init__(
learning_rate,
use_locking=False,
name='GradientDescent'
)
filenamequeue = tf.train.string_input_producer(["a", "b"])
这个函数是生成一个文件队列
reader = tf.FixedLengthRecordReader(record_bytes=32*32*3+1)
创建一个固定数据大小的解析器
解析文件队列
key , value = reader.read(filenamequeue)
key 是这种数据:
'data_batch_1.bin:2', value
如果想要查看可以使用一下方式:
sess =tf.InteractiveSession()
tf.train.start_queue_runners(sess=sess)
print "key:", sess.run(key)
print "value": sess.run(value)
value解析:
record_bytes = tf.decode_raw(value, tf.uint8)
#decode_raw 将字符串转化为向量(一维)
#接下来需要从向量中提取label和image,使用到strided_slice
tf.strided_slice(
input_,
begin,
end,
strides=None,
begin_mask=0,
end_mask=0,
ellipsis_mask=0,
new_axis_mask=0,
shrink_axis_mask=0,
var=None,
name=None
)
#strided_slice可以向量中获取label
label = tf.cast(tf.strided_slice(record_bytes, [0],[1]), tf.int32)
#获取image数据:
image = tf.strided_slice(record_bytes, [label_bytes],
[label_bytes + image_bytes])
#将image维度转化,depth, height, width
jormain = tf.reshape(image, [3, 32, 32])
# 将jormain转化成我们一般的图片[height, width, depth]:
tf.transpose(jormain, [1, 2, 0])
这样image就提取出来了。
newiamge = tf.random_crop(image, [height, width, 3])
这是一个随机裁剪函数,因为原image是 32 x 32 x 3的维度,而训练模型是使用 24 x 24 x 3的维度,而训练模型是使用24
所以需要对数据进行裁剪,这里使用的是随机裁剪
newiamge = tf.image.random_flip_left_right(newiamge)
这里是对图片以一定概率做水平方向的左右翻转
newiamge = tf.image.random_brightness(newiamge, max_delta=63)
随机调整亮度 调整范围[-63 ,63)
newiamge = tf.image.random_contrast(newiamge, 0.1, 0.6)
在0.1 ~ 0.6 范围内随机调整对比度
newimage = tf.image.per_image_standardization(newiamge)
对图像做归一化处理
newimage.set_shape([height, width, 3])
从新设置维度
batch_image, labels = tf.train.batch(
tensors,
batch_size,
num_threads=1,
capacity=32, 队列中元素的最大数量
enqueue_many=False,
shapes=None,
dynamic_pad=False,
allow_smaller_final_batch=False,
shared_name=None,
name=None
)
tf.train.shuffle_batch(
tensors,
batch_size,
capacity,
min_after_dequeue, 在出队列之后队列中最小的元素数量
num_threads=1,
seed=None,
enqueue_many=False,
shapes=None,
allow_smaller_final_batch=False,
shared_name=None,
name=None
)
variable_averages = tf.train.ExponentialMovingAverage(
cifar10.MOVING_AVERAGE_DECAY, global_step)
这是计算平均滑动
variables_averages_op = variable_averages.apply(tf.trainable_variables())
更新训练存储节点的平均滑动值
bacth_queue = tf.contrib.slim.prefetch_queue.prefetch_queue(
[images, labels], capacity=8)
预填充batch数据,使用最多8个队列
每次获取batch数据 使用
images , lables = batch_queue.dequeue() 出队列