Deep learning convolutional neural network, tensorflow1.x version from entry installation environment to model code operation, tensorflow1.x uses LeNet to realize handwritten digit recognition

foreword

  1. Have a basic knowledge of python and be able to understand the syntax of python
  2. Understanding anaconda tools is smoother
  3. If the above two points are not clear, it can only guarantee that you can run the model, but it cannot guarantee that you understand the reason

anaconda download and installation

  1. It is recommended to use the Tsinghua mirror, because it is better to use the anaconda download module based on the domestic mirror
  2. Website Tsinghua University Open Source Software Mirror Station | Tsinghua Open Source Mirror Tsinghua Open Source Mirror Tsinghua University Open Source Software Mirror Station is committed to providing domestic and campus users with high-quality open source software mirrors and Linux mirror source services to help users obtain open source software more conveniently. This mirror station is operated and maintained by Tsinghua University TUNA Association. https://mirrors.tuna.tsinghua.edu.cn/
  3. Download anaconda: (Remarks: any version is acceptable) The benefits of installing anaconda: First, you don’t need to install python after installing anaconda, because anaconda can create the desired python version environment https://repo.anaconda.com/archive/Anaconda3- 5.2.0-Windows-x86_64.exe https://repo.anaconda.com/archive/Anaconda3-5.2.0-Windows-x86_64.exe
  4. After downloading, click Install directly (then go to the next step to select the path)

        

  1. Choose the installation path by yourself (the next step is to check both boxes, the first box is to configure the anaconda environment, and the second is to install the default python3.6 of anaconda)

        

  1. tick two

        

  1. Then anaconda is installed

Use of anaconda

  1. Select Anaconda prompt as shown in the figure

  1. Add Tsinghua mirror website (you can also add it without adding it, I will not add it in detail here, you can find other articles to add it)

        First enter the command: conda config --set show_channel_urls yes , then go to the path where anaconda is installed to find the hidden file .condarc and copy and paste the URL below to the hidden file .condarc, save it

        Note: If you cannot use conda to download the module after adding the mirror source, then delete it without configuring the mirror source, and use the default

#添加下面
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2

         Enter the command: conda config --show channels to see if the installation is successful

  1. Create a new environment, the environment name is tensorflow, the command: conda create -n tensorflow python==3.6.4, here I set the python version of the environment to 3.6.4
  2. Finally, you will be prompted to use the command conda activate tensorflow to activate the environment just created called tensorflow

  1. Enter conda activate tensorflow to activate the environment,
  2. Then use the command conda install tensorflow==1.14.0 to download tensorflow1.14.0 version, my code is tensorflow1.14.0, pay attention to the difference between tensorflow2 version and 1 version, enter y

 

  1. It’s the last step, install and use jupyter to write code training, a step that is easy to ignore, when installing anaconda, jupyter is already installed, but that is just jupyter in the default environment

 

 Then enter the command jupyter notebook to write code in the browser

Code for LeNet

  1. Download the dataset, the URL of the dataset is as follows

MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burgeshttp://yann.lecun.com/exdb/mnist/

  1. Next is the complete code for LeNet to realize mnist handwriting recognition
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data


#“/home/tensor/jupyter/MNIST_data/“改成你的数据集路径
mnist = input_data.read_data_sets("/home/tensor/jupyter/MNIST_data/",one_hot=True)

#确认是否能加载出来
print('训练集数量:',mnist.train.num_examples,
     ',验证集数量:',mnist.validation.num_examples,
     ',测试集数量:',mnist.test.num_examples)

#占位符
x=tf.placeholder(tf.float32,[None,784],name='X')
y=tf.placeholder(tf.float32,[None,10],name='Y')

#定义线性的斜率和截距变量
W=tf.Variable(tf.random_normal([784,10]),name='W')
b=tf.Variable(tf.zeros([10]),name='b')

#矩阵运算
forward=tf.matmul(x,W)+b

#创建模型
pred = tf.nn.softmax(forward)

#超参数
train_epochs = 100 #训练50次
batch_size = 100 #单次训练样本
total_batch = int(mnist.train.num_examples/batch_size) #一轮训练有多少批
display_step = 2 #显示粒度
learning_rate = 0.01 #学习率

#损失函数
loss_function = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))

#梯度下降优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)

#匹配情况
correct_prediction = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))

#准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

sess = tf.Session()#创建会话
init = tf.global_variables_initializer()#初始化变量
sess.run(init)

#开始训练
for epoch in range(train_epochs):
    for batch in range(total_batch):
        xs,ys = mnist.train.next_batch(batch_size)#读取批次数据
        sess.run(optimizer,feed_dict={x:xs,y:ys})#执行批次训练
    loss,acc = sess.run([loss_function,accuracy],feed_dict={x:mnist.validation.images,y:mnist.validation.labels})
    if (epoch+1) % display_step == 0:
        print("Train Epoch:",'%02d' % (epoch+1),"Loss=","{:.9}".format(loss)," Accuracy=","{:.4f}".format(acc))
print("finished!")

#接下来是图形化测试模型输出结果对比
prediction_result = sess.run(tf.argmax(pred,1),feed_dict={x:mnist.test.images})

import matplotlib.pyplot as plt
import numpy as np
def plot_show_img_label(img,lab,pred,index,num=10):
    fig = plt.gcf() #获取当前图表
    fig.set_size_inches(10,12) #图片列表大小
    if num > 25:
        num = 25
    for i in range(0,num):
        ax = plt.subplot(5,5,i+1)
        ax.imshow(np.reshape(img[index],(28,28)),cmap='binary') #显示图像
        title = "label="+str(np.argmax(lab[index])) #构建图像标签值
        if len(pred)>0:
            title+=",pred="+str(pred[index])
        ax.set_title(title,fontsize=10)
        ax.set_xticks([]);
        ax.set_yticks([])
        index+=1
    plt.show()

plot_show_img_label(mnist.test.images,mnist.test.labels,prediction_result,10,10)

The results obtained are as follows. It can be seen that the prediction in the second row and the fourth column is wrong, the correct result should be 3, and the model result is 5

Guess you like

Origin blog.csdn.net/m0_59799878/article/details/129671361