• 卷积神经网络CNN手写数字识别案例


    目录

    网络设计

    设计两个卷积大层

    具体参数

    代码:

    结果:


    网络设计

    设计两个卷积大层

    手写数字识别数据集下载:

    链接:https://pan.baidu.com/s/1nqhP4yPNcqefKYs91jp9ng?pwd=xe1h 
    提取码:xe1h

    我们知道mnist图像数据【None,784】28*28

    具体参数

    第一层

        卷积:32个filter、大小5*5、strides = 1、padding = "SAME"

               tf.nn.conv2d(input,filter,strides=,padding=)

               input:输入图像[None,28,28,1]

                       要求:形状[batch,heigth,width,channel]

                                类型为float32,64  

               filter:

                    weights = tf.Variable(initial_value=tf.random_normal(shape=[5,5,1,32]))

                    bias = tf.Variable(initial value=tf.random normal(shape=[32]))

                    变量initial value=random normal(shape=[F,F,3/1,K])

               strides:  步长 1    [1,1,1,1]

               padding:“SAME"
                             "SAME”:越过边缘取样
                             “VALID”:不越过边缘取样 

              输出形状:[None,28,28,32]   ----通道数变为32

        激活:Relu 上层结果直接输入进来

                 tf.nn.relu(fetures)

        池化:

              输入形状:[None,28,28,32]

              大小2*2 、strides2 

              根据公式计算输出形状:[None,14,14,32]

    第二层

        卷积:64个filter、大小5*5、strides = 1、padding = "SAME"

                 tf.nn.conv2d(input,filter,strides=,padding=)

                input:输入图像[None,14,14,32]

                          要求:形状[batch,heigth,width,channel]

                           类型为float32,64  

               filter:

                    weights = tf.Variable(initial_value=tf.random_normal(shape=[5,5,32,64]))

                    bias = tf.Variable(initial value=tf.random normal(shape=[64]))

                    变量initial value=random normal(shape=[F,F,3/1,K])

               strides:  步长 1    [1,1,1,1]

               padding:“SAME"
                             "SAME”:越过边缘取样
                             “VALID”:不越过边缘取样 

              输出形状:[None,14,14,64]   ----通道数变为64

       激活:Relu

                 tf.nn.relu(fetures)

       池化:输入[None,14,14,64] 

                  大小2*2 、strides2 

                  根据公式计算输出形状:[None,7,7,64]

    全连接层

         tf.shape()

         [None,7,7,64]  ---->[None,7*7*64]   

         [None,7*7*64] * [7*7*64,10] = [None , 10]

         y_predict = tf.matmul[pool2,weights] + bias

    代码

    1. import tensorflow as tf
    2. import os
    3. from tensorflow.examples.tutorials.mnist import input_data
    4. tf.compat.v1.disable_eager_execution()
    5. # 1、利用数据,在训练的时候实时提供数据
    6. # mnist手写数字数据在运行时候实时提供给给占位符
    7. tf.compat.v1.app.flags.DEFINE_integer("is_train", 1, "指定是否是训练模型,还是拿数据去预测")
    8. FLAGS = tf.compat.v1.app.flags.FLAGS
    9. def create_weights(shape):
    10. return tf.Variable(initial_value=tf.compat.v1.random_normal(shape=shape, stddev=0.01))
    11. def create_model(x):
    12. """
    13. 构建卷积神经网络
    14. :param x:
    15. :return:
    16. """
    17. # 1)第一个卷积大层
    18. with tf.compat.v1.variable_scope("conv1"):
    19. # 卷积层
    20. # 将x[None, 784]形状进行修改
    21. input_x = tf.reshape(x, shape=[-1, 28, 28, 1])
    22. # 定义filter和偏置
    23. conv1_weights = create_weights(shape=[5, 5, 1, 32])
    24. conv1_bias = create_weights(shape=[32])
    25. conv1_x = tf.nn.conv2d(input=input_x, filters=conv1_weights, strides=[1, 1, 1, 1], padding="SAME") + conv1_bias
    26. # 激活层
    27. relu1_x = tf.nn.relu(conv1_x)
    28. # 池化层
    29. pool1_x = tf.nn.max_pool(input=relu1_x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
    30. # 2)第二个卷积大层
    31. with tf.compat.v1.variable_scope("conv2"):
    32. # 卷积层
    33. # 定义filter和偏置
    34. conv2_weights = create_weights(shape=[5, 5, 32, 64])
    35. conv2_bias = create_weights(shape=[64])
    36. conv2_x = tf.nn.conv2d(input=pool1_x, filters=conv2_weights, strides=[1, 1, 1, 1], padding="SAME") + conv2_bias
    37. # 激活层
    38. relu2_x = tf.nn.relu(conv2_x)
    39. # 池化层
    40. pool2_x = tf.nn.max_pool(input=relu2_x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
    41. # 3)全连接层
    42. with tf.compat.v1.variable_scope("full_connection"):
    43. # [None, 7, 7, 64]->[None, 7 * 7 * 64]
    44. # [None, 7 * 7 * 64] * [7 * 7 * 64, 10] = [None, 10]
    45. x_fc = tf.reshape(pool2_x, shape=[-1, 7 * 7 * 64])
    46. weights_fc = create_weights(shape=[7 * 7 * 64, 10])
    47. bias_fc = create_weights(shape=[10])
    48. y_predict = tf.matmul(x_fc, weights_fc) + bias_fc
    49. return y_predict
    50. def full_connected_mnist():
    51. """
    52. 单层全连接神经网络识别手写数字图片
    53. 特征值:[None, 784]
    54. 目标值:one_hot编码 [None, 10]
    55. :return:
    56. """
    57. mnist = input_data.read_data_sets("./tmp/mnist_data",one_hot=True)
    58. # 1、准备数据
    59. # x [None, 784] y_true [None. 10]
    60. with tf.compat.v1.variable_scope("mnist_data"):
    61. x = tf.compat.v1.placeholder(tf.float32, [None, 784])
    62. y_true = tf.compat.v1.placeholder(tf.int32, [None, 10])
    63. y_predict = create_model(x)
    64. # 3、softmax回归以及交叉熵损失计算
    65. with tf.compat.v1.variable_scope("softmax_crossentropy"):
    66. # labels:真实值 [None, 10] one_hot
    67. # logits:全层的输出[None,10]
    68. # 返回每个样本的损失组成的列表
    69. loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
    70. # 4、梯度下降损失优化
    71. with tf.compat.v1.variable_scope("optimizer"):
    72. # 学习率
    73. optimizer = tf.compat.v1.train.AdamOptimizer(0.001).minimize(loss)
    74. # 5、得出每次训练的准确率(通过真实值和预测值进行位置比较,每个样本都比较)
    75. with tf.compat.v1.variable_scope("accuracy"):
    76. equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
    77. accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
    78. # 初始化变量op
    79. init_op = tf.compat.v1.global_variables_initializer()
    80. # 开启会话去训练
    81. with tf.compat.v1.Session() as sess:
    82. # 初始化变量
    83. sess.run(init_op)
    84. if FLAGS.is_train == 1:
    85. # 循环步数去训练
    86. for i in range(500):
    87. # 获取数据,实时提供
    88. # 每步提供50个样本训练
    89. mnist_x, mnist_y = mnist.train.next_batch(50)
    90. # 运行训练op
    91. _,accuracy_value,loss_value = sess.run([optimizer,accuracy,loss],feed_dict={x: mnist_x, y_true: mnist_y})
    92. print("训练第%d步的准确率为:%f, 损失为:%f " % (i +1,accuracy_value,loss_value
    93. )
    94. )
    95. else:
    96. # 如果不是训练,我们就去进行预测测试集数据
    97. for i in range(100):
    98. # 每次拿一个样本预测
    99. mnist_x, mnist_y = mnist.test.next_batch(1)
    100. print("第%d个样本的真实值为:%d, 模型预测结果为:%d" % (
    101. i+1,
    102. tf.argmax(sess.run(y_true, feed_dict={x: mnist_x, y_true: mnist_y}), 1).eval(),
    103. tf.argmax(sess.run(y_predict, feed_dict={x: mnist_x, y_true: mnist_y}), 1).eval()
    104. )
    105. )
    106. return None
    107. if __name__ == "__main__":
    108. full_connected_mnist()

    结果

  • 相关阅读:
    Composer交互文档如何在PPT当中使用
    Uniapp离线打包SDK-模块配置
    android中输入系统之内核到InputManagerService过程(源码)
    108.(前端)分类管理删除值实现——¶elementui可移除标签
    持续集成部署-k8s-服务发现-Service
    解析后人类时代类人机器人的优越性
    快速幂矩阵-python
    初识JAVA中的包装类,时间复杂度及空间复杂度
    提高视频性能的 5 种方法
    Centos7部署Python3环境
  • 原文地址:https://blog.csdn.net/weixin_47256446/article/details/138033867