資源簡介
基于卷積神經網絡的手寫數字識別,工具使用Google的人工智能TensorFlow庫,識別準確率高,代碼使用python3.0以上版本

代碼片段和文件信息
import?tensorflow?as?tf
import?numpy?as?np
from?PIL?import?Image
import?forward?as?fw
import?backward?as?bw
import?time
import?os
os.environ[‘TF_CPP_MIN_LOG_LEVEL‘]=‘2‘
def?restore_model(testPivArr):
????with?tf.Graph().as_default()?as?g:
????????x=?tf.placeholder(tf.float32[1fw.IMAGE_SIZE?fw.IMAGE_SIZE?1])
????????#?x?=?tf.placeholder(tf.float32?[1784])
????????y=?fw.forward(xFalseNone)
????????preValue=?tf.argmax(y1)
????????variable_averages=?tf.train.ExponentialMovingAverage(bw.MOVING_AVERAGE_DECAY)
????????variable_to_restore=?variable_averages.variables_to_restore()
????????saver=?tf.train.Saver(variable_to_restore)
????????with?tf.Session()?as?sess:
????????????ckpt?=?tf.train.get_checkpoint_state(bw.MODEL_SAVE_PATH)
????????????if?ckpt?and?ckpt.model_checkpoint_path:
????????????????saver.restore(sess?ckpt.model_checkpoint_path)
????????????????ypreValue=?sess.run([ypreValue]feed_dict={x:testPivArr})
????????????????return?preValue
????????????else:
????????????????print(‘沒有找到訓練節點‘)
????????????????return?-1
def?pre_pic(picName):
????img=?Image.open(picName)
????im_arr=?np.array(img.convert(‘L‘))
????threshold=50
????for?i?in?range(fw.IMAGE_SIZE):
????????for?j?in?range(fw.IMAGE_SIZE):
????????????im_arr[i][j]=255-?im_arr[i][j]
????????????if?im_arr[i][j] ????????????????im_arr[i][j]=0
????????????else:
????????????????im_arr[i][j]?=?255
????nm_arr=?im_arr.reshape([1784])
????nm_arr=?nm_arr.astype(np.float32)
????img_ready=?np.dot(nm_arr1.0/255.0)
????img_ready=np.reshape(img_ready[-1fw.IMAGE_SIZE?fw.IMAGE_SIZE?1])
????return?img_ready
def?application():
????testPath?=?input(“input?the?folder?of?test?pictures:“)
????start?=?time.clock()
????for?filename?in?os.listdir(testPath):??#?listdir的參數是文件夾的路徑
????????print(“the?name?of?test?picture:“filename)
????????testPicArr?=?pre_pic(testPath+“/“+filename)
????????preValue?=?restore_model(testPicArr)
????????print(“預測數字可能是:“preValue)
????????print(‘###############################################‘)
????elapsed?=?(time.clock()?-?start)
????print(‘預測所用時間:‘elapsed)
def?main():
????#?mnist?=?input_data.read_data_sets(‘MNIST_data‘?one_hot=True)
????application()
if?__name__==‘__main__‘:
????main()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????2649??2018-05-05?17:31??backward.py
?????文件????????2218??2018-05-05?17:31??forward.py
?????文件????????1435??2018-04-17?13:57??handWriteNumber.py
?????文件????????1881??2018-05-05?17:31??test.py
?????文件????????2343??2018-05-05?17:31??app.py
評論
共有 條評論