Tensorflow:实战Google深度学习框架(VGGNet16--实现mnist数据集分类)

参考:tensorflow实战  黄文坚

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

from datetime import datetime
import math
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

#卷积层函数
'''
kh、kw卷积核的高和宽,n_out卷积核数量即输出通道数,dh、dw步长的长和宽, p=参数列表
'''
def conv_op(input_op, name, kh, kw, n_out, dh, dw, p):
    #get_shape()[-1].value 获取输入input_op的通道数,比如224x224x3中最后那个3
    n_in = input_op.get_shape()[-1].value

    with tf.name_scope(name) as scope:
        '''tf.contrib.layers.xavier_initializer_conv2d根据某一层网络的输出输入节点数量自动调节最合适的分布'''
        kernel = tf.get_variable(scope + 'w', shape=[kh, kw, n_in, n_out], dtype=tf.float32,
                                 initializer=tf.contrib.layers.xavier_initializer_conv2d())
        conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1), padding='SAME')
        bias_init_val = tf.constant(0.0, shape=[n_out], dtype=tf.float32)
        biases = tf.Variable(bias_init_val, trainable=True, name='b')
        z = tf.nn.bias_add(conv, biases)
        activation = tf.nn.relu(z, name=scope)
        p += [kernel, biases]
        return activation

#全连接层函数
def fc_op(input_op, name, n_out, p):
    n_in = input_op.get_shape()[-1].value

    with tf.name_scope(name) as scope:
        kernal = tf.get_variable(scope + 'w', shape=[n_in, n_out], dtype=tf.float32,
                                 initializer=tf.contrib.layers.xavier_initializer())
        #这里biases不在初始化为0,而赋予一个较小的值0.1以避免dead neuron(神经死亡)
        biases = tf.Variable(tf.constant(0.1, shape=[n_out], dtype=tf.float32), name='b')
        activation = tf.nn.relu_layer(input_op, kernal, biases, name=scope)

        p += [kernal, biases]
        return activation

#创建最大池化层函数
def maxpool_op(input_op, name, kh, kw, dh, dw):
    return tf.nn.max_pool(input_op, ksize=[1, kh, kw, 1], strides=[1, dh, dw, 1], padding='SAME', name=name)

#前向传播函数
def inference_op(input_op, keep_prob):
    p = []

    conv1_1 = conv_op(input_op, name='conv1_1', kh=3, kw=3, n_out=64, dh=1, dw=1, p=p)
    conv1_2 = conv_op(conv1_1, name='conv1_2', kh=3, kw=3, n_out=64, dh=1, dw=1, p=p)
    pool1 = maxpool_op(conv1_2, name='pool1', kh=2, kw=2, dw=2, dh=2)

    conv2_1 = conv_op(pool1, name='conv2_1', kh=3, kw=3, n_out=128, dh=1, dw=1, p=p)
    conv2_2 = conv_op(conv2_1, name='conv2_2', kh=3, kw=3, n_out=128, dh=1, dw=1, p=p)
    pool2 = maxpool_op(conv2_2, name='pool2', kh=2, kw=2, dh=2, dw=2)

    conv3_1 = conv_op(pool2, name='conv3_1', kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)
    conv3_2 = conv_op(conv3_1, name='conv3_2', kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)
    conv3_3 = conv_op(conv3_2, name='conv3_3', kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)
    pool3 = maxpool_op(conv3_3, name='conv3_3' ,kh=2, kw=2, dh=2, dw=2)

    conv4_1 = conv_op(pool3, name='conv4_1', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv4_2 = conv_op(conv4_1, name='conv4_2', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv4_3 = conv_op(conv4_2, name='conv4_3', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    pool4 = maxpool_op(conv4_3, name='pool4', kh=2, kw=2, dh=2, dw=2)

    conv5_1 = conv_op(pool4, name='conv5_1', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv5_2 = conv_op(conv5_1, name='conv5_2', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv5_3 = conv_op(conv5_2, name='conv5_3', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    pool5 = maxpool_op(conv5_3, name='pool5', kh=2, kw=2, dh=2, dw=2)

    #tf.shape函数将每一个样本化成长度为7x7x512=25088的一维向量
    shp = pool5.get_shape()
    flattened_shape = shp[1].value * shp[2].value * shp[3].value
    # 自动分好batch数
    resh1 = tf.reshape(pool5, [-1, flattened_shape], name='resh1')

    fc6 = fc_op(resh1, name='fc6', n_out=4096, p=p)
    fc6_drop = tf.nn.dropout(fc6, keep_prob=keep_prob, name='fc6_drop')

    fc7 = fc_op(fc6_drop, name='fc7', n_out=4096, p=p)
    fc7_drop = tf.nn.dropout(fc7, keep_prob=keep_prob, name='fc7_drop')

    fc8 = fc_op(fc7_drop, name='fc8', n_out=10, p=p)
    #biases = tf.Variable(tf.constant(0.1, shape=[n_classes], dtype=tf.float32))
    #fc8 = tf.matmul(fc7_drop, [4096, n_classes]) + biases
    softmax = tf.nn.softmax(fc8)
    predictions = tf.argmax(softmax, 1)
    return predictions, softmax, fc8, p

'''
#评测函数
def time_tensorflow_run(session, target, feed, info_string):

    num_steps_burn_in = 10
    total_duration = 0.0
    total_duration_squared = 0.0

    for i in range(num_steps_burn_in + num_batches):
        start_time = time.time()
        _ = session.run(target, feed_dict = feed)
        duration = time.time() - start_time
        if i >= num_steps_burn_in:
            if not i % 10:
                print('%s: step %d, duration = %.3f' % (datetime.now(), i - num_steps_burn_in, duration))
            total_duration += duration
            total_duration_squared += duration * duration

    mm = total_duration / num_batches
    vr = total_duration_squared / num_batches - mm * mm
    sd = math.sqrt(vr)
    print('%s: %s across %d steps , %.3f +/- %.3f sec / batch'
          % (datetime.now(), info_string, num_batches, mm, sd))
'''
#定义 网络参数
learning_rate = 0.001
training_iters = 30000
batch_size = 64   #每个batch的大小
display_step = 20  #每20步展示一下结果

# 定义网络参数
n_input = 784 # 输入的维度
n_classes = 10 # 标签的维度
dropout = 0.8 # Dropout 的概率

#主函数
def run_benchmark():


    with tf.Graph().as_default():
        x = tf.placeholder(tf.float32, [None, n_input])
        y = tf.placeholder(tf.float32, [None, n_classes])
        keep_prob = tf.placeholder(tf.float32)

        images = tf.reshape(x, shape=[-1, 28, 28, 1])
        '''
        fc8 = fc_op(fc7_drop, name='fc8', n_out=1000, p=p)
        softmax = tf.nn.softmax(fc8)
        predictions = tf.argmax(softmax, 1)
        return predictions, softmax, fc8, p
        '''
        predictions, softmax, fc8, p = inference_op(images, keep_prob)

        #定义损失函数和学习步骤
        cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=fc8))
        #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

        # 测试网络
        correct_pred = tf.equal(tf.argmax(fc8, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

        #开启一个训练
        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            step = 1
            # 直到达到最大迭代次数,没考虑梯度!!!
            while step * batch_size < training_iters:
                batch_xs, batch_ys = mnist.train.next_batch(batch_size)

                _ = sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})

                if step % display_step == 0:
                    # 计算精度
                    acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.0})
                    # 计算损失值
                    loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.0})
                    print("Iter " + str(step * batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) +
                          ", Training Accuracy = " + "{:.5f}".format(1 - acc))

                step += 1

            print("Optimization Finished!")
            # 计算测试精度
            print("Testing Accuracy:",
                  sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256],
                                                keep_prob: 1.}))  # 拿前256个来测试
            #print("Testing Result:", sess.run(a, feed_dict={x: mnist.test.images[63:64], y: mnist.test.labels[63:64],
                                                            #keep_prob: 1.}))

        #time_tensorflow_run(sess, predictions, {keep_prob:1.0}, "Forward")


        #objective = tf.nn.l2_loss(fc8)
        #grad = tf.gradients(objective, p)
        #time_tensorflow_run(sess, grad, {keep_prob:0.5}, "Forward-backward")



#batch_size = 32
#num_batches = 100

if __name__ == '__main__':
    run_benchmark()
    pass


不怎么什么原因,精度特别低。

 

Iter 1280, Minibatch Loss= 2.308178, Training Accuracy = 0.87500
Iter 2560, Minibatch Loss= 2.308879, Training Accuracy = 0.90625
Iter 3840, Minibatch Loss= 2.323878, Training Accuracy = 0.90625
Iter 5120, Minibatch Loss= 2.299078, Training Accuracy = 0.90625
Iter 6400, Minibatch Loss= 2.292816, Training Accuracy = 0.90625
Iter 7680, Minibatch Loss= 2.302978, Training Accuracy = 0.87500
Iter 8960, Minibatch Loss= 2.291189, Training Accuracy = 0.82812
Iter 10240, Minibatch Loss= 2.305110, Training Accuracy = 0.90625
Iter 11520, Minibatch Loss= 2.297863, Training Accuracy = 0.89062
Iter 12800, Minibatch Loss= 2.294856, Training Accuracy = 0.90625
Iter 14080, Minibatch Loss= 2.294659, Training Accuracy = 0.85938
Iter 15360, Minibatch Loss= 2.308236, Training Accuracy = 0.95312
Iter 16640, Minibatch Loss= 2.306330, Training Accuracy = 0.92188
Iter 17920, Minibatch Loss= 2.300455, Training Accuracy = 0.87500
Iter 19200, Minibatch Loss= 2.309525, Training Accuracy = 0.92188
Iter 20480, Minibatch Loss= 2.299466, Training Accuracy = 0.87500
Iter 21760, Minibatch Loss= 2.292279, Training Accuracy = 0.82812
Iter 23040, Minibatch Loss= 2.311123, Training Accuracy = 0.92188
Iter 24320, Minibatch Loss= 2.309712, Training Accuracy = 0.93750
Iter 25600, Minibatch Loss= 2.292507, Training Accuracy = 0.84375
Iter 26880, Minibatch Loss= 2.303140, Training Accuracy = 0.90625
Iter 28160, Minibatch Loss= 2.300102, Training Accuracy = 0.85938
Iter 29440, Minibatch Loss= 2.297694, Training Accuracy = 0.87500
Optimization Finished!
Testing Accuracy: 0.13671875

Process finished with exit code 0

 

 

你可能感兴趣的:(Tensorflow:实战Google深度学习框架(VGGNet16--实现mnist数据集分类))