[MNIST資料集]輸入影象的預處理
阿新 • • 發佈:2019-01-06
因為MNIST資料是28*28的黑底白字影象,而且輸入時要將其拉直,也就是可以看成1*784的二維張量(張量的值在0~1之間),所以我們要對圖片進行預處理操作,是圖片能被網路識別。
以下是程式碼部分
import tensorflow as tf import numpy as np from PIL import Image import backward as bw import forward as fw def restore(testPicArr): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, fw.INPUT_NODES]) y_= tf.placeholder(tf.float32, [None, fw.OUTPUT_NODES]) y = fw.get_y(x, None) preValue = tf.arg_max(y, 1) ema = tf.train.ExponentialMovingAverage(bw.MOVING_ARVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) with tf.Session() as sess: tf.logging.set_verbosity(tf.logging.WARN)#降低警告等級 ckpt = tf.train.get_checkpoint_state("./model/") if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) preValue = sess.run(preValue, feed_dict = {x: testPicArr}) return preValueelse: print("NO!!!") return -1 def pre_pic(picName): img = Image.open(picName) reIm = img.resize((28, 28), Image.ANTIALIAS) im_arr = np.array(reIm.convert('L'))#變為灰度圖 threshold = 50#閾值,將圖片二值化操作 for i in range(28): for j in range(28): im_arr[i][j] = 255 - im_arr[i][j]#進行反色處理 if(im_arr[i][j] < threshold): im_arr[i][j] = 0 else: im_arr[i][j] = 255 nm_arr = im_arr.reshape([1,784]) nm_arr = nm_arr.astype(np.float32)#型別轉換 img_ready = np.multiply(nm_arr, 1.0/255.0)#把值變為0~1之間的數值 return img_ready def app(): testNum = input("Input the number of test pictutre:") for i in range(int(testNum)): testPic = input("the path of test picture:") testPicArr = pre_pic(testPic) preValue = restore(testPicArr) print("The prediction number is :" , preValue) def main(): app() if __name__ == '__main__': main()