精华内容
下载资源
问答
  • 卷积神经网络CNN手写数字识别,有详细的代码注释和讲解,以及流程介绍,有利于初学者理解,能完整运行,且准确率当10各epochs时为0.985
  • Python开发,基于TensorFlow+MNIST,含数据集与训练好的模型,已经打包为exe,带UI界面
  • 针对卷积神经网络对手写数字识别训练在卷积核随机...实验结果表明,与随机初始化卷积核的CNN手写数字识别算法相比,改进的算法在应用MNIST数据库训练时不仅收敛,而且在产生相同均方误差的情况下迭代次数少,识别率高。
  • 一份基于cnn手写数字识别的代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 加载数据集 mnist = input_data....
  • 本篇文章主要介绍了PyTorch CNN实战之MNIST手写数字识别示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • CNN识别手写数字,很好的机器学习初心者学习资料,里面包含了MATLAB代码和详细的注释,可以直接运行
  • 卷积神经网络CNN手写数字图像识别
  • CNN卷积神经网络实现手写数字识别,Matlab+minist数据集都有,在matlab上可直接运行,新手友好,亲测可用。
  • TensorFlow框架下利用用CNN进行MNIST手写字符识别
  • 包含了可直接运行的CNN的Lenet5模型实现手写数字识别Mnist以及介绍Lenet5模型的PPT
  • 一个简单的CNN程序,实现对十种手写数字识别
  • 注释很详细,这里不再赘述。 import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from tensorflow.examples.tutorials.mnist import input_data # 下载...输出一个长度为10的向量来表示每个数字的可能性 #...

    注释很详细,这里不再赘述。 

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    from tensorflow.examples.tutorials.mnist import input_data  # 下载数据集
    
    # 初始化参数
    def weight_variable(shape):
        initial = tf.truncated_normal(shape, stddev=0.1)
        # shape是必选项,输出张量维度,stddev=0.1用于设置正态分布被截断前的标准差
        return tf.Variable(initial)
    
    def bias_variable(shape):
        initial = tf.constant(0.1, shape=shape)
        # 开始时设置为0.1
        return tf.Variable(initial)
    
    def conv2d(x, W):
        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
    
    def max_pool_2x2(x):
        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        # ksize为卷积核大小  padding取valid时卷积之后图像大小为N=(imgSize-ksize)/strides
        # x 表示输入图像,要求是一个tensor具有[batch,in_height,in_width,in_channels]这样的shape,为float32或者64
        # 过滤器要求是一个tensor,具有[filter_height,filter_width,in_channels,out_channels]这样的shape
    
    # 获取数据
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    
    # tf.name_scope()和tf.variable_scope()是两个作用域,
    # 一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用,用于变量共享
    # input image:pixel 28*28 = 784
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 784])
        y_ = tf.placeholder('float', [None, 10])  # y_ 代表真实结果
        # 创建占位符是 tf 读取数据的一种方法,让python代码来提供数据
    
    with tf.name_scope('image'):
        x_image = tf.reshape(x, [-1, 28, 28, 1])  # -1表示由实际情况来定,图像数目*宽*高/28/28/1=第一维数
        tf.summary.image('input_image', x_image, 8)
        # 输出带图像的probuf,汇总数据的图像的的形式如下: ’ tag /image/0’, ’ tag /image/1’…,如:input/image/0等
    
    # 第一个卷积层
    with tf.name_scope('conv_layer1'):
        W_conv1 = weight_variable([5, 5, 1, 32])  # 卷积核(kernel): 5*5*1, kernel个数: 32
        b_conv1 = bias_variable([32])
        h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)  # 输出(output): 28*28*32
    # 池化
    with tf.name_scope('pooling_layer'):
        h_pool1 = max_pool_2x2(h_conv1)  # 最大池化, 输出(output): 14*14*32
    
    # 第二个卷积层
    with tf.name_scope('conv_layer2'):
        W_conv2 = weight_variable([5, 5, 32, 64])  # 卷积核(kernel): 5*5, 深度: 32, kernel个数: 64
        b_conv2 = bias_variable([64])
        h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)  # 输出(output): 14*14*64
    # 池化
    with tf.name_scope('pooling_layer'):
        h_pool2 = max_pool_2x2(h_conv2)  # 最大池化, 输出(output): 7*7*64
    
    
    # 第一个全连接层
    with tf.name_scope('fc_layer3'):
        W_fc1 = weight_variable([7 * 7 * 64, 1024])
        b_fc1 = bias_variable([1024])  # 大小 : 1*1024
        h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)  # 输出(output): 1*1024
    
    # dropout层  dropout层一般加在全连接层 防止过拟合,提升模型泛化能力
    with tf.name_scope('dropout'):
        keep_prob = tf.placeholder(tf.float32)
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
    
    
    # 第二个全连接层
    # 训练模型 : y = softmax(x * w + b)
    with tf.name_scope('output_fc_layer4'):
        W_fc2 = weight_variable([1024, 10])
        b_fc2 = bias_variable([10])  # 大小 : 1*10
    
    with tf.name_scope('softmax'):
        y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)  # 输出(output): 1*10
    
    with tf.name_scope('lost'):
        cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))   # 交叉熵计算损失函数
        tf.summary.scalar('lost', cross_entropy)
    
    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        # AdamOptimizer是tensorflow中的一种优化器,1e-4是学习率
        # 为了最小化损失函数,需要用到反向传播思想,随机梯度下降算法来最小化损失函数
    
    with tf.name_scope('accuracy'):
        correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        tf.summary.scalar('accuracy', accuracy)  # 显示标量信息
        '''
        tf.argmax(y_conv, 1) 是返回第二维(行方向)上最大值的索引,
        tf.equal() 是比较两个值是否相等,返回一个 bool 值(True or False),
        tf.cast() 是将bool 值转换为 1.0 or 0.0 的浮点类型,
        tf.reduce_mean() 是计算平均值。
        '''
    
    merged = tf.summary.merge_all()
    train_summary = tf.summary.FileWriter(r'C:\Users\12956\Anaconda3\Lib\site-packages', tf.get_default_graph())
    # 将这里的地址路径改成tensorboard文件夹的绝对路径地址
    
    init = tf.global_variables_initializer()
    # 进行初始化,之前只是定义variable
    
    # 执行
    with tf.Session() as sess:
        sess.run(init)
        # 构建一个session,在session中运行
        # 训练数据 : 得到参数 w 和 b
        for i in range(2000):  # 迭代2000次
            batch = mnist.train.next_batch(50)
            # 批量给网络提供数据
    
            result, _ = sess.run([merged, train_step], feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
            # 在mnist数据集中,0是输入的图片,1是输入的标签,这个batch是784*10的
            # train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
    
            if i % 100 == 0:
                # train_accuracy = sess.run(accuracy, feed_dict)
                train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})  # 无dropout
                print('step %d, training accuracy %g' % (i, train_accuracy))
    
                # result = sess.run(merged, feed_dict={x: batch[0], y_: batch[1]})
                train_summary.add_summary(result, i)
    
        train_summary.close()
    
        print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
    
    
    

    可以直接复制运行,记得将路径改一下~

    #import tensorflow as tf
    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    #from tensorflow.examples.tutorials.mnist import input_data
    #用下面这几句代码来代替上面这句
    #import tensorflow as tf
    
    tf.__version__
    mint = tf.keras.datasets.mnist
    (x_, y_), (x_1, y_1) = mint.load_data()
    import matplotlib.pyplot as plt
    plt.imshow(x_[0], cmap="binary")
    plt.show()
    
    import tensorflow.examples.tutorials.mnist.input_data as input_data
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    
    # import tensorflow as tf
    # (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
    
    # from tensorflow.examples.tutorials.mnist import input_data
    # mnist = input_data.read_data_sets ( ” MNIST_data/ ”, one_hot=True)
    
    # import tensorflow.examples.tutorials.mnist.input_data as input_data
    # mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    
    # 初始化权重函数
    def weight_variable(shape):
        initial = tf.truncated_normal(shape, stddev=0.1);  # 生成维度是shape标准差是0.1的正态分布数
        return tf.Variable(initial)
    
    
    # 初始化偏置项
    def bias_variable(shape):
        initial = tf.constant(0.1, shape=shape)  # 生成维度为shape的全为0.1的向量
        return tf.Variable(initial)
    
    
    # 定义卷积函数
    def conv2d(x, w):
        return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
        # strides: 卷积时在图像每一维的步长,这是一个一维的向量,
        # [ 1, strides, strides, 1],第一位和最后一位固定必须是1
        # padding参数是string类型,值为“SAME” 和 “VALID”,表示的是卷积的形式。
        # 设置为"SAME"时,会在原图像边缘外增加几圈0来使卷积后的矩阵和原图像矩阵的维度相同
        # 设置为"VALID"则不考虑这一点,卷积后的矩阵维度会相应减少,例如原图像如果是5*5,卷积核是3*3
        # 那么卷积过后的输出矩阵回是3*3的
    
    
    # 定义一个2*2的最大池化层
    def max_pool_2_2(x):
        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        # 第一个参数value:需要池化的输入,一般池化层接在卷积层后面,
        # 依然是[batch, height, width, channels]这样的shape
        # 第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],
        # 因为我们不想在batch和channels上做池化,所以这两个维度设为了1
        # 第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,
        # 一般也是[1, stride,stride, 1]
        # 第四个参数padding:和卷积类似,可以取'VALID' 或者'SAME'
    
    
    if __name__ == "__main__":
        # 定义输入变量
        x = tf.placeholder("float", shape=[None, 784])  # 占位
        # 浮点型变量,行数不定,列数为784(每个图像是一个长度为784(28*28)的向量)
    
        # 定义输出变量
        y_ = tf.placeholder("float", shape=[None, 10])  # 占位
        # 浮点型变量,行数不定,列数为10,输出一个长度为10的向量来表示每个数字的可能性
        # 初始化权重,第一层卷积,32的意思代表的是输出32个通道
        # 其实,也就是设置32个卷积,每一个卷积都会对图像进行卷积操作
    
        w_conv1 = weight_variable([5, 5, 1, 32])  # 生成了32个5*5的矩阵
        # 初始化偏置项
        b_conv1 = bias_variable([32])
    
        x_image = tf.reshape(x, [-1, 28, 28, 1])
        # 将输入的x转成一个4D向量,第2、3维对应图片的宽高,最后一维代表图片的颜色通道数
        # 输入的图像为灰度图,所以通道数为1,如果是RGB图,通道数为3
        # tf.reshape(x,[-1,28,28,1])的意思是将x自动转换成28*28*1的数组
        # -1的意思是代表不知道x的shape,它会按照后面的设置进行转换
    
        # 卷积并激活
        h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
        # 池化
        h_pool1 = max_pool_2_2(h_conv1)
        # 第二层卷积
        # 初始权重
        w_conv2 = weight_variable([5, 5, 32, 64])
        # 在32个第一层卷积层上每个再用一个5*5的卷积核在做特征提取,并输出到第二层卷积层,
        # 第二层设置了64个卷积层
    
        # 初始化偏置项
        b_conv2 = bias_variable([64])
        # 将第一层卷积池化后的结果作为第二层卷积的输入加权求和后激活
        h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
        # 池化
        h_pool2 = max_pool_2_2(h_conv2)
        # 设置全连接层的权重
        w_fc1 = weight_variable([7 * 7 * 64, 1024])
        # 28*28的原图像经过两次池化后变为7*7,设置了1024个输出单元
    
        # 设置全连接层的偏置
        b_fc1 = bias_variable([1024])
        # 将第二层卷积池化后的结果,转成一个7*7*64的数组
        h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
        # 通过全连接之后并激活
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
        # 防止过拟合
        keep_prob = tf.placeholder("float")  # 占位
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
        # 设置每个单元保留的概率来随机放弃一些单元来防止过拟合
    
        # 输出层
        w_fc2 = weight_variable([1024, 10])
        b_fc2 = bias_variable([10])
        # 加权求和并激活
        y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2)
    
        # 日志输出,每迭代100次输出一次日志
        # 定义交叉熵为损失函数
        cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
        # 最小化交叉熵
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        # 计算准确率
        correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        sess = tf.Session()
        sess.run(tf.initialize_all_variables())
        # 上面的两行是在为tf的输出变量做准备
        # 下载minist的手写数字的数据集
        mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
        for i in range(20000):  # 迭代20000次
            batch = mnist.train.next_batch(50)  # 设置batch,即每次用来训练模型的数据个数
            if i % 100 == 0:  # 每100次迭代输出一次精度
                train_accuracy = accuracy.eval(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
                # 给之前占位的x和y_本次训练的batch个数据中第一个数据的图像矩阵和标签,不考虑过拟合
                # 计算当前的精度
    
                print("step %d,training accuracy %g" % (i, train_accuracy))
            train_step.run(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
            # 当i不能被100整除时的训练过程,考虑过拟合,单元保留的概率为0.5
    
        print("test accuracy %g" % accuracy.eval(session=sess, feed_dict={
            x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
        # 输出测试集的精度

     两种代码实现的功能一样,只是写法不同,第一种是使用with方法编写的,第二种是C语言思想编写的,先定义函数,再使用。

    学无止境,一起学习~

     

    展开全文
  • CNN 手写数字识别.zip

    2020-05-05 13:29:54
    利用CNN来进行手写识别,通过卷积神经网络来识别数据集中的手写数字,可以很好的得到识别结果。实现平台为matlab,易于上手,很基础的代码来实现
  • 实验CNN数字手写识别基于python jupyter notebook android (可运行)可安装在手机.zip实验CNN数字手写识别基于python jupyter notebook android (可运行)可安装在手机.zip实验CNN数字手写识别基于python ...
  • 基于TensorFlow2.0 Keras的CNN手写数字识别

    千次阅读 多人点赞 2019-09-11 17:42:31
    import os import tensorflow as tf from tensorflow.keras.datasets import mnist from tensorflow.keras.utils import to_categorical import numpy as np E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\p...
    import os 
    import tensorflow as tf
    from tensorflow.keras.datasets import mnist
    from tensorflow.keras.utils import to_categorical
    import numpy as np
    
    E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint8 = np.dtype([("qint8", np.int8, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint16 = np.dtype([("qint16", np.int16, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint32 = np.dtype([("qint32", np.int32, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      np_resource = np.dtype([("resource", np.ubyte, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint8 = np.dtype([("qint8", np.int8, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint16 = np.dtype([("qint16", np.int16, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint32 = np.dtype([("qint32", np.int32, 1)])
    E:\Anaconda3\envs\dl\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      np_resource = np.dtype([("resource", np.ubyte, 1)])
    
    (X_tarin, y_train), (X_test, y_test) = mnist.load_data()
    
    X_train4D = X_tarin.reshape(X_tarin.shape[0], 28, 28, 1).astype('float32')
    X_test4D = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
    
    X_train4D_Normalize = X_train4D / 255 # 归一化
    X_test4D_Normalize = X_test4D / 255
    
    y_trainOnehot = to_categorical(y_train)
    y_testOnehot = to_categorical(y_test)
    
    # 导入相关包
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
    
    # 建立模型
    model = Sequential()
    
    # 一层卷积
    model.add(
        Conv2D(
            filters=16,
            kernel_size=(5, 5),
            padding='same',  # 保证卷积核大小,不够补零
            input_shape=(28, 28, 1),
            activation='relu'))
    # 池化层1
    model.add(MaxPool2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    
    # 二层卷积
    model.add(
        Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu'))
    # 池化层2
    model.add(MaxPool2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    
    model.add(
        Conv2D(filters=64, kernel_size=(5, 5), padding='same', activation='relu'))
    model.add(
        Conv2D(filters=128, kernel_size=(5, 5), padding='same', activation='relu'))
    model.add(MaxPool2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    
    model.add(Flatten())  # 平坦层
    model.add(Dense(128, activation='relu'))  # 全连接层
    model.add(Dropout(0.25)) 
    model.add(Dense(10, activation='softmax')) # 激活函数
    
    model.summary()
    
    Model: "sequential"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    conv2d (Conv2D)              (None, 28, 28, 16)        416       
    _________________________________________________________________
    max_pooling2d (MaxPooling2D) (None, 14, 14, 16)        0         
    _________________________________________________________________
    dropout (Dropout)            (None, 14, 14, 16)        0         
    _________________________________________________________________
    conv2d_1 (Conv2D)            (None, 14, 14, 32)        12832     
    _________________________________________________________________
    max_pooling2d_1 (MaxPooling2 (None, 7, 7, 32)          0         
    _________________________________________________________________
    dropout_1 (Dropout)          (None, 7, 7, 32)          0         
    _________________________________________________________________
    conv2d_2 (Conv2D)            (None, 7, 7, 64)          51264     
    _________________________________________________________________
    conv2d_3 (Conv2D)            (None, 7, 7, 128)         204928    
    _________________________________________________________________
    max_pooling2d_2 (MaxPooling2 (None, 3, 3, 128)         0         
    _________________________________________________________________
    dropout_2 (Dropout)          (None, 3, 3, 128)         0         
    _________________________________________________________________
    flatten (Flatten)            (None, 1152)              0         
    _________________________________________________________________
    dense (Dense)                (None, 128)               147584    
    _________________________________________________________________
    dropout_3 (Dropout)          (None, 128)               0         
    _________________________________________________________________
    dense_1 (Dense)              (None, 10)                1290      
    =================================================================
    Total params: 418,314
    Trainable params: 418,314
    Non-trainable params: 0
    _________________________________________________________________
    
    # 训练模型
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    train_history = model.fit(x=X_train4D_Normalize,
                              y=y_trainOnehot,
                              validation_split=0.2,
                              batch_size=300,
                              epochs=10,
                              verbose=2)
    
    WARNING:tensorflow:From E:\Anaconda3\envs\dl\lib\site-packages\tensorflow\python\ops\math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
    Instructions for updating:
    Use tf.where in 2.0, which has the same broadcast rule as np.where
    Train on 48000 samples, validate on 12000 samples
    Epoch 1/10
    48000/48000 - 81s - loss: 0.4865 - accuracy: 0.8395 - val_loss: 0.0870 - val_accuracy: 0.9719
    Epoch 2/10
    48000/48000 - 80s - loss: 0.1150 - accuracy: 0.9651 - val_loss: 0.0539 - val_accuracy: 0.9842
    Epoch 3/10
    48000/48000 - 92s - loss: 0.0841 - accuracy: 0.9746 - val_loss: 0.0442 - val_accuracy: 0.9857
    Epoch 4/10
    48000/48000 - 90s - loss: 0.0639 - accuracy: 0.9797 - val_loss: 0.0373 - val_accuracy: 0.9892
    Epoch 5/10
    48000/48000 - 89s - loss: 0.0532 - accuracy: 0.9835 - val_loss: 0.0341 - val_accuracy: 0.9889
    Epoch 6/10
    48000/48000 - 83s - loss: 0.0496 - accuracy: 0.9846 - val_loss: 0.0305 - val_accuracy: 0.9908
    Epoch 7/10
    48000/48000 - 85s - loss: 0.0415 - accuracy: 0.9869 - val_loss: 0.0298 - val_accuracy: 0.9912
    Epoch 8/10
    48000/48000 - 92s - loss: 0.0390 - accuracy: 0.9879 - val_loss: 0.0288 - val_accuracy: 0.9913
    Epoch 9/10
    48000/48000 - 85s - loss: 0.0322 - accuracy: 0.9901 - val_loss: 0.0265 - val_accuracy: 0.9925
    Epoch 10/10
    48000/48000 - 83s - loss: 0.0299 - accuracy: 0.9905 - val_loss: 0.0304 - val_accuracy: 0.9912
    
    # 定义训练参数可视化
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    
    def show_train_history(train_history, train, validation):
        plt.plot(train_history.history[train])
        plt.plot(train_history.history[validation])
        plt.title('Train History')
        plt.ylabel('train')
        plt.xlabel('epoch')
        plt.legend(['train', 'validation'], loc='upper left')
        plt.show()
    
    # 准确率
    show_train_history(train_history, 'accuracy', 'val_accuracy')
    

    在这里插入图片描述

    # 损失率
    show_train_history(train_history, 'loss', 'val_loss')
    

    在这里插入图片描述

    model.evaluate(X_test4D_Normalize, y_testOnehot)[1]
    
    10000/10000 [==============================] - 4s 423us/sample - loss: 0.0252 - accuracy: 0.9915
    
    
    
    
    
    0.9915
    
    # 预测值
    prediction = model.predict_classes(X_test4D_Normalize)
    
    # 预测前十
    prediction[:10]
    
    array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=int64)
    
    # 定义图片可视化
    def plot_labels_prediction(images, labels, prediction, idx, num=10):
        fig = plt.gcf()
        fig.set_size_inches(12, 14) 
        if num > 25: num = 25
        for i in range(0, num):
            ax = plt.subplot(5, 5, i + 1) 
            ax.imshow(images[idx], cmap='binary')
            if len(prediction) > 0:
                title = ',labels' + str(prediction[idx])
            ax.set_title(title, fontsize=10)
            ax.set_xticks([])
            ax.set_yticks([])
            idx += 1
        plt.show()
    
    plot_labels_prediction(X_test, y_test, prediction, idx=0)
    

    在这里插入图片描述

    # 交叉表查看预测数据与原数据对比
    import pandas as pd
    pd.crosstab(y_test, prediction, rownames=['lables'], colnames=['predict'])
    
    predict0123456789
    lables
    0977000001110
    10112030005160
    20010291000110
    30001000080020
    4001097300026
    5100208881000
    6510003947020
    71261000101611
    8001001009711
    9001034016994

    由上图显示对角线为预测准确的次数

    展开全文
  • CNN 手写数字识别 (pytorch代码实现)

    千次阅读 2020-05-23 17:51:02
    1、pytorch 手写数字识别步骤 (1)加载minist数据 (2)搭建网络 (3)模型训练 (4)结果可视化 2、代码实现 # library # standard library import os # third-party library import torch import torch.nn as nn...

    1、CNN 手写数字识别步骤

    (1)加载minist数据
    (2)搭建网络
    (3)模型训练
    (4)结果可视化

    2、pytorch代码实现

    # library
    # standard library
    import os
    
    # third-party library
    import torch
    import torch.nn as nn
    import torch.utils.data as Data
    import torchvision
    import matplotlib.pyplot as plt
    
    # torch.manual_seed(1)    # reproducible
    
    # Hyper Parameters
    EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
    BATCH_SIZE = 50
    LR = 0.001              # learning rate
    DOWNLOAD_MNIST = False
    
    
    # Mnist digits dataset
    if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
        # not mnist dir or mnist is empyt dir
        DOWNLOAD_MNIST = True
    
    train_data = torchvision.datasets.MNIST(
        root='./mnist/',
        train=True,                                     # this is training data
        transform=torchvision.transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
                                                        # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
        download=DOWNLOAD_MNIST,
    )
    
    # plot one example
    print(train_data.train_data.size())                 # (60000, 28, 28)
    print(train_data.train_labels.size())               # (60000)
    plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
    plt.title('%i' % train_data.train_labels[0])
    plt.show()
    
    # Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
    train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
    
    # pick 2000 samples to speed up testing
    test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
    test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255.   # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
    test_y = test_data.test_labels[:2000]
    
    
    class CNN(nn.Module):
        def __init__(self):
            super(CNN, self).__init__()
            self.conv1 = nn.Sequential(         # input shape (1, 28, 28)
                nn.Conv2d(
                    in_channels=1,              # input height
                    out_channels=16,            # n_filters
                    kernel_size=5,              # filter size
                    stride=1,                   # filter movement/step
                    padding=2,                  # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1
                ),                              # output shape (16, 28, 28)
                nn.ReLU(),                      # activation
                nn.MaxPool2d(kernel_size=2),    # choose max value in 2x2 area, output shape (16, 14, 14)
            )
            self.conv2 = nn.Sequential(         # input shape (16, 14, 14)
                nn.Conv2d(16, 32, 5, 1, 2),     # output shape (32, 14, 14)
                nn.ReLU(),                      # activation
                nn.MaxPool2d(2),                # output shape (32, 7, 7)
            )
            self.out = nn.Linear(32 * 7 * 7, 10)   # fully connected layer, output 10 classes
    
        def forward(self, x):
            x = self.conv1(x)
            x = self.conv2(x)
            x = x.view(x.size(0), -1)           # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
            output = self.out(x)
            return output, x    # return x for visualization
    
    
    cnn = CNN()
    print(cnn)  # net architecture
    
    optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters
    loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
    
    # following function (plot_with_labels) is for visualization, can be ignored if not interested
    
    
    from matplotlib import cm
    try: from sklearn.manifold import TSNE; HAS_SK = True
    except: HAS_SK = False; print('Please install sklearn for layer visualization')
    
    
    def plot_with_labels(lowDWeights, labels):
        plt.cla()
        X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
        for x, y, s in zip(X, Y, labels):
            c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
        plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
    
    plt.ion()
    # training and testing
    
    
    for epoch in range(EPOCH):
        for step, (b_x, b_y) in enumerate(train_loader):   # gives batch data, normalize x when iterate train_loader
    
            output = cnn(b_x)[0]               # cnn output
            loss = loss_func(output, b_y)   # cross entropy loss
            optimizer.zero_grad()           # clear gradients for this training step
            loss.backward()                 # backpropagation, compute gradients
            optimizer.step()                # apply gradients
    
            if step % 50 == 0:
                test_output, last_layer = cnn(test_x)
                pred_y = torch.max(test_output, 1)[1].data.numpy()
                accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
                print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
                if HAS_SK:
                    # Visualization of trained flatten layer (T-SNE)
                    tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
                    plot_only = 500
                    low_dim_embs = tsne.fit_transform(last_layer.data.numpy()[:plot_only, :])
                    labels = test_y.numpy()[:plot_only]
                    plot_with_labels(low_dim_embs, labels)
    plt.ioff()
    
    # print 10 predictions from test data
    test_output, _ = cnn(test_x[:10])
    pred_y = torch.max(test_output, 1)[1].data.numpy()
    print(pred_y, 'prediction number')
    print(test_y[:10].numpy(), 'real number')
    
    
    展开全文
  • FCN实现手写数字数据集MNIST的识别,代码为自己翻写,包含MNIST数据集,代码与大量注释,及24页原创报告
  • DT+CNN手写数字识别.rar

    2020-06-19 15:27:41
    DT+CNN手写数字识别.rar
  • pytorch实现CNN手写数字识别(MNIST)

    千次阅读 2019-04-18 21:54:15
    super(CNN,self).__init__() self.layer1 = nn.Sequential( nn.Conv2d(1,16,kernel_size=3), nn.BatchNorm2d(16), nn.ReLU(inplace=True)) self.layer2 = nn.Sequential( nn.Conv2d(16,32,kernel_size=3)...

    导入依赖包

    import torch 
    import numpy as np
    from torch.utils.data import DataLoader
    from torchvision.datasets import mnist
    from torch import nn
    from torch.autograd import Variable
    from torch import optim
    from torchvision import transforms
    

    定义CNN结构

    class CNN(nn.Module):
        def __init__(self):
            super(CNN,self).__init__()
            
            self.layer1 = nn.Sequential(
                nn.Conv2d(1,16,kernel_size=3),
                nn.BatchNorm2d(16),
                nn.ReLU(inplace=True))
            
            self.layer2 = nn.Sequential(
                nn.Conv2d(16,32,kernel_size=3),
                nn.BatchNorm2d(32),
                nn.ReLU(inplace=True),
                nn.MaxPool2d(kernel_size=2,stride=2)) 
            
            self.layer3 = nn.Sequential(
                nn.Conv2d(32,64,kernel_size=3), 
                nn.BatchNorm2d(64),
                nn.ReLU(inplace=True))
            
            self.layer4 = nn.Sequential(
                nn.Conv2d(64,128,kernel_size=3),  
                nn.BatchNorm2d(128),
                nn.ReLU(inplace=True),
                nn.MaxPool2d(kernel_size=2,stride=2))
            
            self.fc = nn.Sequential(
                    nn.Linear(128 * 4 * 4,1024),
                    nn.ReLU(inplace=True),
                    nn.Linear(1024,128),
                    nn.ReLU(inplace=True),
                    nn.Linear(128,10))
            
        def forward(self,x):
            x = self.layer1(x)
            x = self.layer2(x)
            x = self.layer3(x)
            x = self.layer4(x)
            x = x.view(x.size(0),-1)
            x = self.fc(x)
            return x
    

    下载并导入数据

    data_tf = transforms.Compose(
                    [transforms.ToTensor(),
                     transforms.Normalize([0.5],[0.5])])
     
    train_set = mnist.MNIST('./data',train=True,transform=data_tf,download=True)
    test_set = mnist.MNIST('./data',train=False,transform=data_tf,download=True)
     
    train_data = DataLoader(train_set,batch_size=64,shuffle=True)
    test_data = DataLoader(test_set,batch_size=128,shuffle=False)
    

    需要科学上网,否则无法下载,该步骤完成后效果图如下:
    在这里插入图片描述

    设置神经网络

    net = CNN()
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(),1e-1)
    nums_epoch = 20
    

    训练与测试

    losses =[]
    acces = []
    eval_losses = []
    eval_acces = []
    
    for epoch in range(nums_epoch):
        #TRAIN
        train_loss = 0
        train_acc = 0
        net = net.train()
        for img , label in train_data:
            #img = img.reshape(img.size(0),-1) 
            img = Variable(img)
            label = Variable(label)
            
            # forward
            out = net(img)
            loss = criterion(out,label)
            # backward
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            
            # loss
            train_loss += loss.item()
            # accuracy
            _,pred = out.max(1)
            num_correct = (pred == label).sum().item()
            acc = num_correct / img.shape[0]
            train_acc += acc
            
        losses.append(train_loss / len(train_data))
        acces.append(train_acc / len(train_data))
        
        
        #TEST
        eval_loss = 0
        eval_acc = 0
        for img , label in test_data:
            #img = img.reshape(img.size(0),-1)
            img = Variable(img)
            label = Variable(label)
            
            out = net(img)
            loss = criterion(out,label)
            eval_loss += loss.item()
            _ , pred = out.max(1)
            num_correct = (pred==label).sum().item()
            acc = num_correct / img.shape[0]
            eval_acc += acc
            
        eval_losses.append(eval_loss / len(test_data))
        eval_acces.append(eval_acc / len(test_data))
        
        #PRINT IN EVERYEPOCH
        print('Epoch {} Train Loss {} Train  Accuracy {} Teat Loss {} Test Accuracy {}'.format(
            epoch+1, train_loss / len(train_data),train_acc / len(train_data), eval_loss / len(test_data), eval_acc / len(test_data)))
    

    在这里仅显示前两次结果,可以看到准确率已经很高了。
    在这里插入图片描述

    展开全文
  • CNN手写数字识别.rar

    2021-01-07 17:08:59
    python语言编写的卷积神经网络代码示例,可直接在tensorflow运行,不懂的可以留言交流。
  • mnist.npz 适合新手的手写数字识别本地数据集,可以用来做简单的卷积神经网络,循环神经网络等深度学习模型
  • 第一步:构建用来训练数据的模型 #coding:utf-8 -*- #首先导入相关模块 import tensorflow as tf ...#开始搭建CNN模型 class CNNmodel(object): def __init__(self): model = models.Sequential() #创建...
  • 完整的MNIST CNN手写识别数据集加代码,深度学习入门好资源!
  • MNIST 是手写数字的数据集 root 是数据集的根目录 train为True 代表的是下载训练数据,为False代表下载的是测试数据 transform 是指定数据格式,ToTensor是将图片转化为0到1之间的Tensor类型的数据 download 表示...
  • MNIST手写字 Matlab程序,包含BP和CNN程序。不依赖任何库,包含MNIST数据,BP网络可达到98.3%的识别率,CNN可达到99%的识别率。CNN比较耗时,关于CNN的程序介绍:...
  • MNIST手写数据集的训练和测试代码,代码准确率高于98%,代码完善,下载即可使用,利用TensorFlow编译环境,适合新手自学。MNIST数据集是个通用数据集,很多新手都会用到。
  • 1、Mnist_cnn.py 该脚本文件 用TensorFlow框架 实现CNN卷积神经网络处理Mnist手写数字识别数据集,准确率达到99.21%; 2、Mnist_cnn_tensorboard.py 该脚本文件在Mnist_cnn.py的基础上实现可视化。
  • 由于pip安装的TensorFlow缺少一部分代码,以及TensorFlow2版本相对于TensorFlow1在语句上有变化。因此大部分网上代码不适用(主要问题在于读取mnist数据包和一些函数形式有问题)。这个代码可以运行。...
  • kaggle 实战 (2): CNN 手写数字识别

    千次阅读 2019-04-30 02:37:45
    文章目录Tensorflow 官方示例CNN提交结果 Tensorflow 官方示例 import tensorflow as tf mnist = tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train / ...
  • 简陋的CNN实现手写数字识别

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 10,264
精华内容 4,105
关键字:

cnn手写数字识别