前置条件

对数据集的处理和加工请参照博客:
[深度学习]基于TensorFlow的基本深度学习模型

卷积神经网络神经元的构建

x = tf.placeholder(tf.float32, [None, 3072])
y = tf.placeholder(tf.int64, [None])
# [None], eg: [0,5,6,3]
x_image = tf.reshape(x, [-1, 3, 32, 32])
# 32*32
x_image = tf.transpose(x_image, perm=[0, 2, 3, 1])

# conv1: 神经元图, feature_map, 输出图像
conv1 = tf.layers.conv2d(x_image,
                         32, # output channel number
                         (3,3), # kernel size
                         padding = 'same',
                         activation = tf.nn.relu,
                         name = 'conv1')

# 16 * 16
pooling1 = tf.layers.max_pooling2d(conv1,
                                   (2, 2), # kernel size
                                   (2, 2), # stride
                                   name = 'pool1')


conv2 = tf.layers.conv2d(pooling1,
                         32, # output channel number
                         (3,3), # kernel size
                         padding = 'same',
                         activation = tf.nn.relu,
                         name = 'conv2')

# 8 * 8
pooling2 = tf.layers.max_pooling2d(conv2,
                                   (2, 2), # kernel size
                                   (2, 2), # stride
                                   name = 'pool2')

conv3 = tf.layers.conv2d(pooling2,
                         32, # output channel number
                         (3,3), # kernel size
                         padding = 'same',
                         activation = tf.nn.relu,
                         name = 'conv3')

# 4 * 4 * 32
pooling3 = tf.layers.max_pooling2d(conv3,
                                   (2, 2), # kernel size
                                   (2, 2), # stride
                                   name = 'pool3')
# [None, 4 * 4 * 32]
flatten = tf.layers.flatten(pooling3)
y_ = tf.layers.dense(flatten, 10)

loss = tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_)
# y_ -> sofmax
# y -> one_hot
# loss = ylogy_

# indices
predict = tf.argmax(y_, 1)
# [1,0,1,1,1,0,0,0]
correct_prediction = tf.equal(predict, y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float64))

with tf.name_scope('train_op'):
    train_op = tf.train.AdamOptimizer(1e-3).minimize(loss)

训练和测试

init = tf.global_variables_initializer()
batch_size = 20 #每一批次训练数量
train_steps = 100000 #训练的次数
test_steps = 100 #测试的次数
#共执行100000轮训练,每次训练20张图片

with tf.Session() as sess: 
    sess.run(init) 
    for i in range(train_steps):#开始训练,训练次数100,000次
        batch_data, batch_labels = train_data.next_batch(batch_size)
        loss_val, acc_val, _ = sess.run(
            [loss, accuracy, train_op],
            feed_dict={
                x: batch_data,
                y: batch_labels})
        if (i+1) % 500 == 0:
            print('[Train] Step: %d, loss: %4.5f, acc: %4.5f' 
                  % (i+1, loss_val, acc_val)) 
        if (i+1) % 5000 == 0:
            test_data = CifarData(test_filenames, False)
            all_test_acc_val = [] #用于存储每次测试的成绩
            for j in range(test_steps): # 在当前状态下执行测试100次
                test_batch_data, test_batch_labels \
                    = test_data.next_batch(batch_size)                   
                test_acc_val = sess.run(
                    [accuracy],
                    feed_dict = {
                        x: test_batch_data, 
                        y: test_batch_labels
                    }) 
                all_test_acc_val.append(test_acc_val)
            test_acc = np.mean(all_test_acc_val)
            print('[Test ] Step: %d, acc: %4.5f' % (i+1, test_acc))

实验:训练与测试

训练测试结果如下:

从结果中可以看到,训练结果打印达到100000/500=200次,测试次数为100000/5000=20次

[Train] Step: 100, loss: 1.98442, acc: 0.30000
[Train] Step: 200, loss: 2.08371, acc: 0.25000
[Train] Step: 300, loss: 1.55206, acc: 0.40000
[Train] Step: 400, loss: 1.56185, acc: 0.50000
[Train] Step: 500, loss: 1.75197, acc: 0.45000
[Train] Step: 600, loss: 1.51176, acc: 0.45000
[Train] Step: 700, loss: 1.59780, acc: 0.40000
[Train] Step: 800, loss: 1.52536, acc: 0.40000
[Train] Step: 900, loss: 1.33289, acc: 0.45000
[Train] Step: 1000, loss: 1.35082, acc: 0.60000
(10000, 3072)
(10000,)
...
...
[Test ] Step: 99000, acc: 0.69150
[Train] Step: 99100, loss: 0.21312, acc: 0.95000
[Train] Step: 99200, loss: 0.82235, acc: 0.80000
[Train] Step: 99300, loss: 0.44828, acc: 0.85000
[Train] Step: 99400, loss: 0.79768, acc: 0.70000
[Train] Step: 99500, loss: 0.24061, acc: 0.95000
[Train] Step: 99600, loss: 0.28077, acc: 0.90000
[Train] Step: 99700, loss: 0.52106, acc: 0.90000
[Train] Step: 99800, loss: 0.17791, acc: 0.95000
[Train] Step: 99900, loss: 0.48319, acc: 0.80000
[Train] Step: 100000, loss: 0.35406, acc: 0.85000
(10000, 3072)
(10000,)
[Test ] Step: 100000, acc: 0.69800

Process finished with exit code 0

最后一次测试的正确率为69.8%,可见卷积神经网络对图片的正确处理能力比普通的神经网络好很多。不过更优秀的算法层出不穷,我们在后续对更优秀的神经网络进行测试和讲解

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐