一、MNIST數字識別資料集獲取及處理
通過學習林大貴老師的《TensorFlow+Keras深度學習人工智能實踐應用》,對影像處理的程序有了較淺薄的理解,在此與大家分享,同時由于上書中提供的代碼下載頁面失效,筆者按照書本中的內容手敲代碼,如果有紕漏敬請指正,
此第一節為手寫資料的獲取與預處理,不涉及演算法,
1.數字識別資料集獲取
1.1 匯入相關模塊
匯入keras.utils模塊是因為后續用到獨熱編碼:
import numpy as np
import pandas as pd
from keras.utils import np_utils
np.random.seed(10)
Keras已經提供了下載、讀取MNIST資料的模塊,可以直接匯入:
from keras.datasets import mnist
1.2 MNIST資料的下載和讀取
第一次執行mnist.load_data()方法時,程式檢查目錄是否有MNIST資料集檔案,若無,則下載,
#下載代碼、加載代碼相同,如下
(x_train_image,y_train_label),(x_test_image,y_test_label) = mnist.load_data()
#查看資料,此處筆者加注釋
# print('train data=',len(x_train_image))
# print(' test data=',len(x_test_image))
# 下載完成之后,顯示影像格式
print('x_train_image:',x_train_image.shape)
print('y_train_image:',y_train_label.shape)
1.3查看訓練資料
為了能夠顯示images數字影像,創建plot_image函式:
(1)首先匯入matplotlib.pyplot模塊;
(2)定義函式的輸入引數為image;
(3、4)設定顯示圖形大小為(2,2),單位為英寸;
(5)使用plt.imshow顯示影像,傳入引數為28*28的影像,cmap引數為binary,以灰度顯示
import matplotlib.pylot as plt
def plot_image(image):
fig = plt.gcf()
fig.set_size_inches(2,2)
plt.imshow(image,cmap='binary')
plt.show()
plot_image(x_train_image[0]) #畫一個數字
1.4查看資料images以及label
匯入pyplot模塊,后續用plt參考.
定義plot_images_labels_prediction()函式,各資料如下:
images(數字影像),labels(真實值),prediction(預測結果),idx(開始顯示的資料index),num=10(要顯示的資料項數,默認時10,不超過25).
import matplotlib.pyplot as plt
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
fig = plt.gcf()
fig.set_size_inches(12,14) #設定影像的大小
if num>25 : num =25 #設定顯示最大項數
for i in range(0,num): #for回圈畫出num個數字影像
ax=plt.subplot(5,5,i+1)
ax.imshow(images[idx],cmap='binary') #建立subgraph子圖形為五行五列
title = "label="+str(labels[idx]) #設定子圖形title,顯示標簽欄位
if len(prediction)>0: #如果傳入了預測結果
title+=",prediction="+str(prediction[idx]) #標題
ax.set_title(title,fontsize=10) #設定子圖形的標題
ax.set_xticks([]);ax.set_yticks([]) #設定不顯示刻度
idx+=1 #讀取下一項
plt.show()
開始畫圖,查看前十項資料.
plot_images_labels_prediction(x_train_image,y_train_label,[],0,10) #查看訓練集資料
plot_images_labels_prediction(x_test_image, y_test_label, [],0,10) #查看測驗集資料
部分訓練集結果如下:

2. 影像資料預處理
2.1 features資料預處理
(1)將原本28*28的數字影像reshape(整形)為一維向量,長度為784,資料型別為Float.
(1)數字影像image的數字標準化(我理解是歸一化).
#整形
x_Train = x_train_image.reshape(60000,784).astype('float32')
x_Test = x_test_image.reshape(10000,784).astype('float32')
#歸一化
x_Train_nomalize = x_Train/255
x_Test_nomalize = x_Test/255
2.2 label資料預處理
(1)lael標簽欄位進行獨熱編碼(One-Hot Encoding)
#One-Hot Encoding 轉換
y_TrainOneHot = np_utils.to_categorical(y_train_label)
y_TestOneHot = np_utils.to_categorical(y_test_label)
3.小結
在使用TensorFlow進行影像處理之前,首先要進行的一步是資料的預處理,代碼對處理的物件格式有一定的要求,輸入資料要整形成一維,對輸出結果進行獨熱編碼更有利于結果匹配.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/20060.html
標籤:其他
