網(wǎng)站開發(fā)咨詢百度推廣優(yōu)化師
文章目錄
- 0 前言
- 1 簡介
- 2 LeNet-5 模型的介紹
- 2.1 結(jié)構(gòu)解析
- 2.2 C1層
- 2.3 S2層
- S2層和C3層連接
- 2.4 F6與C5層
- 3 寫數(shù)字識別算法模型的構(gòu)建
- 3.1 輸入層設(shè)計(jì)
- 3.2 激活函數(shù)的選取
- 3.3 卷積層設(shè)計(jì)
- 3.4 降采樣層
- 3.5 輸出層設(shè)計(jì)
- 4 網(wǎng)絡(luò)模型的總體結(jié)構(gòu)
- 5 部分實(shí)現(xiàn)代碼
- 6 在線手寫識別
- 7 最后
0 前言
🔥 優(yōu)質(zhì)競賽項(xiàng)目系列,今天要分享的是
🚩 卷積神經(jīng)網(wǎng)絡(luò)手寫字符識別 - 深度學(xué)習(xí)
該項(xiàng)目較為新穎,適合作為競賽課題方向,學(xué)長非常推薦!
🥇學(xué)長這里給一個(gè)題目綜合評分(每項(xiàng)滿分5分)
- 難度系數(shù):3分
- 工作量:3分
- 創(chuàng)新點(diǎn):4分
🧿 更多資料, 項(xiàng)目分享:
https://gitee.com/dancheng-senior/postgraduate
1 簡介
該設(shè)計(jì)學(xué)長使用python基于TensorFlow設(shè)計(jì)手寫數(shù)字識別算法,并編程實(shí)現(xiàn)GUI界面,構(gòu)建手寫數(shù)字識別系統(tǒng)。
這是學(xué)長做的深度學(xué)習(xí)demo,大家可以用于畢業(yè)設(shè)計(jì)。
這里學(xué)長不會以論文的形式展現(xiàn),而是以編程實(shí)戰(zhàn)完成深度學(xué)習(xí)項(xiàng)目的角度去描述。
項(xiàng)目要求:主要解決的問題是手寫數(shù)字識別,最終要完成一個(gè)識別系統(tǒng)。
設(shè)計(jì)識別率高的算法,實(shí)現(xiàn)快速識別的系統(tǒng)。
2 LeNet-5 模型的介紹
學(xué)長實(shí)現(xiàn)手寫數(shù)字識別,使用的是卷積神經(jīng)網(wǎng)絡(luò),建模思想來自LeNet-5,如下圖所示:
2.1 結(jié)構(gòu)解析
這是原始的應(yīng)用于手寫數(shù)字識別的網(wǎng)絡(luò),我認(rèn)為這也是最簡單的深度網(wǎng)絡(luò)。
LeNet-5不包括輸入,一共7層,較低層由卷積層和最大池化層交替構(gòu)成,更高層則是全連接和高斯連接。
LeNet-5的輸入與BP神經(jīng)網(wǎng)路的不一樣。這里假設(shè)圖像是黑白的,那么LeNet-5的輸入是一個(gè)32*32的二維矩陣。同
時(shí),輸入與下一層并不是全連接的,而是進(jìn)行稀疏連接。本層每個(gè)神經(jīng)元的輸入來自于前一層神經(jīng)元的局部區(qū)域(5×5),卷積核對原始圖像卷積的結(jié)果加上相應(yīng)的閾值,得出的結(jié)果再經(jīng)過激活函數(shù)處理,輸出即形成卷積層(C層)。卷積層中的每個(gè)特征映射都各自共享權(quán)重和閾值,這樣能大大減少訓(xùn)練開銷。降采樣層(S層)為減少數(shù)據(jù)量同時(shí)保存有用信息,進(jìn)行亞抽樣。
2.2 C1層
第一個(gè)卷積層(C1層)由6個(gè)特征映射構(gòu)成,每個(gè)特征映射是一個(gè)28×28的神經(jīng)元陣列,其中每個(gè)神經(jīng)元負(fù)責(zé)從5×5的區(qū)域通過卷積濾波器提取局部特征。一般情況下,濾波器數(shù)量越多,就會得出越多的特征映射,反映越多的原始圖像的特征。本層訓(xùn)練參數(shù)共6×(5×5+1)=156個(gè),每個(gè)像素點(diǎn)都是由上層5×5=25個(gè)像素點(diǎn)和1個(gè)閾值連接計(jì)算所得,共28×28×156=122304個(gè)連接。
2.3 S2層
S2層是對應(yīng)上述6個(gè)特征映射的降采樣層(pooling層)。pooling層的實(shí)現(xiàn)方法有兩種,分別是max-pooling和mean-
pooling,LeNet-5采用的是mean-
pooling,即取n×n區(qū)域內(nèi)像素的均值。C1通過2×2的窗口區(qū)域像素求均值再加上本層的閾值,然后經(jīng)過激活函數(shù)的處理,得到S2層。pooling的實(shí)現(xiàn),在保存圖片信息的基礎(chǔ)上,減少了權(quán)重參數(shù),降低了計(jì)算成本,還能控制過擬合。本層學(xué)習(xí)參數(shù)共有1*6+6=12個(gè),S2中的每個(gè)像素都與C1層中的2×2個(gè)像素和1個(gè)閾值相連,共6×(2×2+1)×14×14=5880個(gè)連接。
S2層和C3層連接
S2層和C3層的連接比較復(fù)雜。C3卷積層是由16個(gè)大小為10×10的特征映射組成的,當(dāng)中的每個(gè)特征映射與S2層的若干個(gè)特征映射的局部感受野(大小為5×5)相連。其中,前6個(gè)特征映射與S2層連續(xù)3個(gè)特征映射相連,后面接著的6個(gè)映射與S2層的連續(xù)的4個(gè)特征映射相連,然后的3個(gè)特征映射與S2層不連續(xù)的4個(gè)特征映射相連,最后一個(gè)映射與S2層的所有特征映射相連。
此處卷積核大小為5×5,所以學(xué)習(xí)參數(shù)共有6×(3×5×5+1)+9×(4×5×5+1)+1×(6×5×5+1)=1516個(gè)參數(shù)。而圖像大小為28×28,因此共有151600個(gè)連接。
S4層是對C3層進(jìn)行的降采樣,與S2同理,學(xué)習(xí)參數(shù)有16×1+16=32個(gè),同時(shí)共有16×(2×2+1)×5×5=2000個(gè)連接。
C5層是由120個(gè)大小為1×1的特征映射組成的卷積層,而且S4層與C5層是全連接的,因此學(xué)習(xí)參數(shù)總個(gè)數(shù)為120×(16×25+1)=48120個(gè)。
2.4 F6與C5層
F6是與C5全連接的84個(gè)神經(jīng)元,所以共有84×(120+1)=10164個(gè)學(xué)習(xí)參數(shù)。
卷積神經(jīng)網(wǎng)絡(luò)通過通過稀疏連接和共享權(quán)重和閾值,大大減少了計(jì)算的開銷,同時(shí),pooling的實(shí)現(xiàn),一定程度上減少了過擬合問題的出現(xiàn),非常適合用于圖像的處理和識別。
3 寫數(shù)字識別算法模型的構(gòu)建
3.1 輸入層設(shè)計(jì)
輸入為28×28的矩陣,而不是向量。
3.2 激活函數(shù)的選取
Sigmoid函數(shù)具有光滑性、魯棒性和其導(dǎo)數(shù)可用自身表示的優(yōu)點(diǎn),但其運(yùn)算涉及指數(shù)運(yùn)算,反向傳播求誤差梯度時(shí),求導(dǎo)又涉及乘除運(yùn)算,計(jì)算量相對較大。同時(shí),針對本文構(gòu)建的含有兩層卷積層和降采樣層,由于sgmoid函數(shù)自身的特性,在反向傳播時(shí),很容易出現(xiàn)梯度消失的情況,從而難以完成網(wǎng)絡(luò)的訓(xùn)練。因此,本文設(shè)計(jì)的網(wǎng)絡(luò)使用ReLU函數(shù)作為激活函數(shù)。
3.3 卷積層設(shè)計(jì)
學(xué)長設(shè)計(jì)卷積神經(jīng)網(wǎng)絡(luò)采取的是離散卷積,卷積步長為1,即水平和垂直方向每次運(yùn)算完,移動(dòng)一個(gè)像素。卷積核大小為5×5。
3.4 降采樣層
學(xué)長設(shè)計(jì)的降采樣層的pooling方式是max-pooling,大小為2×2。
3.5 輸出層設(shè)計(jì)
輸出層設(shè)置為10個(gè)神經(jīng)網(wǎng)絡(luò)節(jié)點(diǎn)。數(shù)字0~9的目標(biāo)向量如下表所示:
4 網(wǎng)絡(luò)模型的總體結(jié)構(gòu)
5 部分實(shí)現(xiàn)代碼
使用Python,調(diào)用TensorFlow的api完成手寫數(shù)字識別的算法。
注:我的程序運(yùn)行環(huán)境是:Win10,python3.。
當(dāng)然,也可以在Linux下運(yùn)行,由于TensorFlow對py2和py3兼容得比較好,在Linux下可以在python2.7中運(yùn)行。
?
#!/usr/bin/env python2# -*- coding: utf-8 -*-"""@author: 丹成學(xué)長 Q746876041"""#import modulesimport numpy as npimport matplotlib.pyplot as plt#from sklearn.metrics import confusion_matriximport tensorflow as tfimport timefrom datetime import timedeltaimport mathfrom tensorflow.examples.tutorials.mnist import input_datadef new_weights(shape):return tf.Variable(tf.truncated_normal(shape,stddev=0.05))def new_biases(length):return tf.Variable(tf.constant(0.1,shape=length))def conv2d(x,W):return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')def max_pool_2x2(inputx):return tf.nn.max_pool(inputx,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')#import datadata = input_data.read_data_sets("./data", one_hot=True) # one_hot means [0 0 1 0 0 0 0 0 0 0] stands for 2print("Size of:")print("--Training-set:\t\t{}".format(len(data.train.labels)))print("--Testing-set:\t\t{}".format(len(data.test.labels)))print("--Validation-set:\t\t{}".format(len(data.validation.labels)))data.test.cls = np.argmax(data.test.labels,axis=1) # show the real test labels: [7 2 1 ..., 4 5 6], 10000valuesx = tf.placeholder("float",shape=[None,784],name='x')x_image = tf.reshape(x,[-1,28,28,1])y_true = tf.placeholder("float",shape=[None,10],name='y_true')y_true_cls = tf.argmax(y_true,dimension=1)# Conv 1layer_conv1 = {"weights":new_weights([5,5,1,32]),"biases":new_biases([32])}h_conv1 = tf.nn.relu(conv2d(x_image,layer_conv1["weights"])+layer_conv1["biases"])h_pool1 = max_pool_2x2(h_conv1)# Conv 2layer_conv2 = {"weights":new_weights([5,5,32,64]),"biases":new_biases([64])}h_conv2 = tf.nn.relu(conv2d(h_pool1,layer_conv2["weights"])+layer_conv2["biases"])h_pool2 = max_pool_2x2(h_conv2)# Full-connected layer 1fc1_layer = {"weights":new_weights([7*7*64,1024]),"biases":new_biases([1024])}h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,fc1_layer["weights"])+fc1_layer["biases"])# Droupout Layerkeep_prob = tf.placeholder("float")h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)# Full-connected layer 2fc2_layer = {"weights":new_weights([1024,10]),"biases":new_weights([10])}# Predicted classy_pred = tf.nn.softmax(tf.matmul(h_fc1_drop,fc2_layer["weights"])+fc2_layer["biases"]) # The output is like [0 0 1 0 0 0 0 0 0 0]y_pred_cls = tf.argmax(y_pred,dimension=1) # Show the real predict number like '2'# cost function to be optimizedcross_entropy = -tf.reduce_mean(y_true*tf.log(y_pred))optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cross_entropy)# Performance Measurescorrect_prediction = tf.equal(y_pred_cls,y_true_cls)accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)train_batch_size = 50def optimize(num_iterations):total_iterations=0start_time = time.time()for i in range(total_iterations,total_iterations+num_iterations):x_batch,y_true_batch = data.train.next_batch(train_batch_size)feed_dict_train_op = {x:x_batch,y_true:y_true_batch,keep_prob:0.5}feed_dict_train = {x:x_batch,y_true:y_true_batch,keep_prob:1.0}sess.run(optimizer,feed_dict=feed_dict_train_op)# Print status every 100 iterations.if i%100==0:# Calculate the accuracy on the training-set.acc = sess.run(accuracy,feed_dict=feed_dict_train)# Message for printing.msg = "Optimization Iteration:{0:>6}, Training Accuracy: {1:>6.1%}"# Print it.print(msg.format(i+1,acc))# Update the total number of iterations performedtotal_iterations += num_iterations# Ending timeend_time = time.time()# Difference between start and end_times.time_dif = end_time-start_time# Print the time-usageprint("Time usage:"+str(timedelta(seconds=int(round(time_dif)))))test_batch_size = 256def print_test_accuracy():# Number of images in the test-set.num_test = len(data.test.images)cls_pred = np.zeros(shape=num_test,dtype=np.int)i = 0while i < num_test:# The ending index for the next batch is denoted j.j = min(i+test_batch_size,num_test)# Get the images from the test-set between index i and jimages = data.test.images[i:j, :]# Get the associated labelslabels = data.test.labels[i:j, :]# Create a feed-dict with these images and labels.feed_dict={x:images,y_true:labels,keep_prob:1.0}# Calculate the predicted class using Tensorflow.cls_pred[i:j] = sess.run(y_pred_cls,feed_dict=feed_dict)# Set the start-index for the next batch to the# end-index of the current batchi = jcls_true = data.test.clscorrect = (cls_true==cls_pred)correct_sum = correct.sum()acc = float(correct_sum) / num_test# Print the accuracymsg = "Accuracy on Test-Set: {0:.1%} ({1}/{2})"print(msg.format(acc,correct_sum,num_test))# Performance after 10000 optimization iterationsoptimize(num_iterations=10000)print_test_accuracy()savew_hl1 = layer_conv1["weights"].eval()saveb_hl1 = layer_conv1["biases"].eval()savew_hl2 = layer_conv2["weights"].eval()saveb_hl2 = layer_conv2["biases"].eval()savew_fc1 = fc1_layer["weights"].eval()saveb_fc1 = fc1_layer["biases"].eval()savew_op = fc2_layer["weights"].eval()saveb_op = fc2_layer["biases"].eval()np.save("savew_hl1.npy", savew_hl1)np.save("saveb_hl1.npy", saveb_hl1)np.save("savew_hl2.npy", savew_hl2)np.save("saveb_hl2.npy", saveb_hl2)np.save("savew_hl3.npy", savew_fc1)np.save("saveb_hl3.npy", saveb_fc1)np.save("savew_op.npy", savew_op)np.save("saveb_op.npy", saveb_op)
運(yùn)行結(jié)果顯示:測試集中準(zhǔn)確率大概為99.2%。
查看混淆矩陣
6 在線手寫識別
7 最后
🧿 更多資料, 項(xiàng)目分享:
https://gitee.com/dancheng-senior/postgraduate