1. 程式人生 > >[MNIST數據集]輸入圖像的預處理

[MNIST數據集]輸入圖像的預處理

轉換 for mage 二值化 from ply rbo tput warn

因為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 preValue
else: 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()

[MNIST數據集]輸入圖像的預處理