import tensorflow as tf
import numpy as np
import math
import time
import cifar10
import cifar10_input
"""
Created on Tue Nov 27 17:31:35 2018
@author: zhen
"""
max_steps = 1000
# 下载cifar10数据集的默认路径
batch_size = 128
data_dir = "C:/Users/zhen/.spyder-py3/cifar/cifar-10/cifar-10-batches/cifar-10-batches-bin" def variable_with_weight_losses(shape, stddev, wl):
# 定义初始化weights的函数
var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))
if wl is not None:
weight_loss = tf.multiply(tf.nn.l2_loss(var), wl, name='weight_loss')
tf.add_to_collection("losses", weight_loss)
return var # 下载数据
cifar10.maybe_download_and_extract()
# 加载训练数据
images_train, labels_train = cifar10_input.distorted_inputs(data_dir=data_dir, batch_size=batch_size)
# 生成测试数据
images_test, labels_test = cifar10_input.inputs(eval_data=True, data_dir=data_dir, batch_size=batch_size) image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
label_holder = tf.placeholder(tf.int32, [batch_size]) # 设置第一层卷积层
weight_1 = variable_with_weight_losses(shape=[5, 5, 3, 64], stddev=5e-2, wl=0.0)
kernel_1 = tf.nn.conv2d(image_holder, filter=weight_1, strides=[1, 1, 1, 1], padding='SAME')
bias_1 = tf.Variable(tf.constant(0.0, shape=[64]))
# 卷积
conv_1 = tf.nn.relu(tf.nn.bias_add(kernel_1, bias_1))
# 池化
pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
norm_1 = tf.nn.lrn(pool_1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75) # 设置第二层卷积层
weight_2 = variable_with_weight_losses(shape=[5, 5, 64, 64], stddev=5e-2, wl=0.0)
kernel_2 = tf.nn.conv2d(norm_1, weight_2, [1, 1, 1, 1], padding='SAME')
bias_2 = tf.Variable(tf.constant(0.1, shape=[64])) conv_2 = tf.nn.relu(tf.nn.bias_add(kernel_2, bias_2))
norm_2 = tf.nn.lrn(conv_2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
pool_2 = tf.nn.max_pool(norm_2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME') # 全连接层
reshape = tf.reshape(pool_2, [batch_size, -1])
dim = reshape.get_shape()[1].value weight_3 = variable_with_weight_losses(shape=[dim, 384], stddev=0.04, wl=0.004)
bias_3 = tf.Variable(tf.constant(0.1, shape=[384]))
local_3 = tf.nn.relu(tf.matmul(reshape, weight_3) + bias_3) # 第二层全连接层
weight_4 = variable_with_weight_losses(shape=[384, 192], stddev=0.04, wl=0.004)
bias_4 = tf.Variable(tf.constant(0.1, shape=[192]))
local_4 = tf.nn.relu(tf.matmul(local_3, weight_4) + bias_4) # 结果层
weight_5 = variable_with_weight_losses(shape=[192, 10], stddev=1/192.0, wl=0.0)
bias_5 = tf.Variable(tf.constant(0.0, shape=[10]))
logits = tf.add(tf.matmul(local_4, weight_5), bias_5) def loss(logits, labels):
labels = tf.cast(labels, tf.int64)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits,
labels=labels,
name="cross_entropy_per_example"
)
cross_entropy_mean = tf.reduce_mean(cross_entropy, name="cross_entropy")
tf.add_to_collection("losses", cross_entropy_mean)
return tf.add_n(tf.get_collection("losses"), name="total_loss") loss = loss(logits=logits, labels=label_holder)
train_op = tf.train.AdamOptimizer(1e-3).minimize(loss)
top_k_op = tf.nn.in_top_k(logits, label_holder, 1)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
tf.train.start_queue_runners() # 训练
for step in range(max_steps):
start_time = time.time()
image_batch, label_batch = sess.run([images_train, labels_train])
_, loss_value = sess.run([train_op, loss], feed_dict={image_holder: image_batch, label_holder: label_batch})
duration = time.time() - start_time if step % 10 == 0:
examples_per_sec = batch_size / duration
sec_per_batch = float(duration) format_str = "step %d, loss =%.2f (%.1f examples/sec; %.3f sec/batch"
print(format_str % (step, loss_value, examples_per_sec, sec_per_batch)) # 评估模型
num_examples = 10000
num_iter = int(math.ceil(num_examples / batch_size))
true_count = 0
total_sample_count = num_iter * batch_size
step = 0
while step < num_iter:
image_batch, label_batch = sess.run([images_test, labels_test])
predictions = sess.run([top_k_op], feed_dict={image_holder: image_batch, label_holder: label_batch})
true_count += np.sum(predictions)
step += 1 precision = true_count / total_sample_count
print("precision @ 1 = %.3f" % precision)
过程:
Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
step 0, loss =4.68 (19.0 examples/sec; 6.734 sec/batch
step 10, loss =3.58 (62.1 examples/sec; 2.062 sec/batch
step 20, loss =3.09 (62.5 examples/sec; 2.047 sec/batch
step 30, loss =2.77 (62.5 examples/sec; 2.047 sec/batch
step 40, loss =2.48 (62.5 examples/sec; 2.047 sec/batch
step 50, loss =2.36 (62.5 examples/sec; 2.047 sec/batch
step 60, loss =2.13 (60.2 examples/sec; 2.125 sec/batch
step 70, loss =1.95 (63.0 examples/sec; 2.031 sec/batch
step 80, loss =2.01 (62.1 examples/sec; 2.062 sec/batch
step 90, loss =1.90 (63.5 examples/sec; 2.016 sec/batch
step 100, loss =1.93 (62.5 examples/sec; 2.047 sec/batch
step 110, loss =1.96 (62.1 examples/sec; 2.062 sec/batch
step 120, loss =1.92 (62.3 examples/sec; 2.055 sec/batch
step 130, loss =1.81 (63.5 examples/sec; 2.016 sec/batch
step 140, loss =1.86 (59.8 examples/sec; 2.141 sec/batch
step 150, loss =1.88 (64.0 examples/sec; 2.000 sec/batch
step 160, loss =1.87 (62.5 examples/sec; 2.047 sec/batch
step 170, loss =1.73 (49.6 examples/sec; 2.578 sec/batch
step 180, loss =1.86 (62.1 examples/sec; 2.062 sec/batch
step 190, loss =1.71 (62.5 examples/sec; 2.047 sec/batch
step 200, loss =1.63 (63.0 examples/sec; 2.031 sec/batch
step 210, loss =1.63 (63.5 examples/sec; 2.016 sec/batch
step 220, loss =1.67 (62.1 examples/sec; 2.063 sec/batch
step 230, loss =1.72 (62.5 examples/sec; 2.047 sec/batch
step 240, loss =1.76 (62.1 examples/sec; 2.062 sec/batch
step 250, loss =1.67 (61.6 examples/sec; 2.078 sec/batch
step 260, loss =1.67 (62.5 examples/sec; 2.047 sec/batch
step 270, loss =1.59 (63.0 examples/sec; 2.031 sec/batch
step 280, loss =1.55 (62.5 examples/sec; 2.047 sec/batch
step 290, loss =1.64 (62.5 examples/sec; 2.047 sec/batch
step 300, loss =1.63 (62.1 examples/sec; 2.062 sec/batch
step 310, loss =1.49 (62.1 examples/sec; 2.062 sec/batch
step 320, loss =1.49 (62.5 examples/sec; 2.047 sec/batch
step 330, loss =1.61 (62.1 examples/sec; 2.062 sec/batch
step 340, loss =1.55 (61.1 examples/sec; 2.094 sec/batch
step 350, loss =1.63 (62.5 examples/sec; 2.047 sec/batch
step 360, loss =1.75 (61.6 examples/sec; 2.078 sec/batch
step 370, loss =1.54 (61.1 examples/sec; 2.094 sec/batch
step 380, loss =1.66 (61.6 examples/sec; 2.078 sec/batch
step 390, loss =1.66 (62.1 examples/sec; 2.062 sec/batch
step 400, loss =1.74 (62.1 examples/sec; 2.062 sec/batch
step 410, loss =1.60 (61.6 examples/sec; 2.078 sec/batch
step 420, loss =1.64 (62.5 examples/sec; 2.047 sec/batch
step 430, loss =1.59 (61.1 examples/sec; 2.094 sec/batch
step 440, loss =1.64 (59.8 examples/sec; 2.141 sec/batch
step 450, loss =1.67 (62.5 examples/sec; 2.047 sec/batch
step 460, loss =1.35 (60.7 examples/sec; 2.109 sec/batch
step 470, loss =1.45 (63.5 examples/sec; 2.016 sec/batch
step 480, loss =1.47 (62.5 examples/sec; 2.047 sec/batch
step 490, loss =1.37 (61.6 examples/sec; 2.078 sec/batch
step 500, loss =1.64 (63.0 examples/sec; 2.031 sec/batch
step 510, loss =1.58 (64.0 examples/sec; 2.000 sec/batch
step 520, loss =1.36 (63.5 examples/sec; 2.016 sec/batch
step 530, loss =1.30 (61.6 examples/sec; 2.078 sec/batch
step 540, loss =1.49 (62.5 examples/sec; 2.047 sec/batch
step 550, loss =1.46 (62.5 examples/sec; 2.047 sec/batch
step 560, loss =1.58 (63.0 examples/sec; 2.031 sec/batch
step 570, loss =1.46 (63.5 examples/sec; 2.016 sec/batch
step 580, loss =1.49 (64.5 examples/sec; 1.984 sec/batch
step 590, loss =1.30 (64.0 examples/sec; 2.000 sec/batch
step 600, loss =1.39 (64.5 examples/sec; 1.984 sec/batch
step 610, loss =1.62 (63.0 examples/sec; 2.031 sec/batch
step 620, loss =1.41 (62.1 examples/sec; 2.062 sec/batch
step 630, loss =1.29 (62.5 examples/sec; 2.047 sec/batch
step 640, loss =1.42 (63.5 examples/sec; 2.016 sec/batch
step 650, loss =1.36 (63.0 examples/sec; 2.031 sec/batch
step 660, loss =1.46 (63.5 examples/sec; 2.016 sec/batch
step 670, loss =1.26 (63.0 examples/sec; 2.031 sec/batch
step 680, loss =1.64 (62.1 examples/sec; 2.062 sec/batch
step 690, loss =1.39 (63.0 examples/sec; 2.031 sec/batch
step 700, loss =1.32 (61.6 examples/sec; 2.078 sec/batch
step 710, loss =1.36 (61.6 examples/sec; 2.078 sec/batch
step 720, loss =1.51 (62.1 examples/sec; 2.062 sec/batch
step 730, loss =1.48 (63.5 examples/sec; 2.016 sec/batch
step 740, loss =1.34 (61.1 examples/sec; 2.094 sec/batch
step 750, loss =1.44 (61.1 examples/sec; 2.094 sec/batch
step 760, loss =1.34 (60.7 examples/sec; 2.109 sec/batch
step 770, loss =1.46 (61.1 examples/sec; 2.094 sec/batch
step 780, loss =1.46 (60.7 examples/sec; 2.109 sec/batch
step 790, loss =1.42 (61.1 examples/sec; 2.094 sec/batch
step 800, loss =1.40 (63.0 examples/sec; 2.031 sec/batch
step 810, loss =1.46 (61.6 examples/sec; 2.078 sec/batch
step 820, loss =1.32 (62.1 examples/sec; 2.062 sec/batch
step 830, loss =1.46 (62.5 examples/sec; 2.047 sec/batch
step 840, loss =1.27 (64.0 examples/sec; 2.000 sec/batch
step 850, loss =1.38 (62.5 examples/sec; 2.047 sec/batch
step 860, loss =1.30 (63.0 examples/sec; 2.031 sec/batch
step 870, loss =1.18 (63.0 examples/sec; 2.031 sec/batch
step 880, loss =1.39 (62.5 examples/sec; 2.047 sec/batch
step 890, loss =1.17 (63.5 examples/sec; 2.016 sec/batch
step 900, loss =1.27 (62.1 examples/sec; 2.062 sec/batch
step 910, loss =1.38 (60.7 examples/sec; 2.109 sec/batch
step 920, loss =1.64 (60.2 examples/sec; 2.125 sec/batch
step 930, loss =1.45 (60.7 examples/sec; 2.109 sec/batch
step 940, loss =1.39 (61.6 examples/sec; 2.078 sec/batch
step 950, loss =1.40 (63.5 examples/sec; 2.016 sec/batch
step 960, loss =1.32 (62.1 examples/sec; 2.063 sec/batch
step 970, loss =1.32 (63.0 examples/sec; 2.031 sec/batch
step 980, loss =1.28 (61.6 examples/sec; 2.078 sec/batch
step 990, loss =1.20 (63.5 examples/sec; 2.016 sec/batch
结果:
分析:
cifar10数据集比mnist数据集更完整也更复杂,基于cifar数据集进行10分类比mnist有更高的难度,整体的准确率和召回率都普遍偏低,但适当的增加迭代次数和卷积核的大小有助于提升准确度,大概能到80%,要想获得更高的准确度可以增加训练集的数量!