深度學習實戰筆記三:編碼器、解碼器+mnist+kears

對matplotlib的解釋

import matplotlib.pyplot as plt
import numpy as np

a = [[1,2]]
a = np.array(a)

c = a[:,0]#‘:'表示:不管a中有幾組數據,打印所有組數據的下標爲0的數據 c=1
b = a[:,1] #b=2

print(c,b)#[1] [2]

print(a.shape)#(1,2),表示一行兩列

#畫散點圖
plt.scatter(c,b)#畫一個點,點的座標是(1,2)
plt.show()

keras 非監督Autoencoding學習 非序列化Sequential

#分類
import numpy as np
from keras.datasets import mnist
from keras.models import Model
from keras.layers import Dense,Input
import matplotlib.pyplot as plt
from PIL import Image

#加載數據集 mnist
(X_train,Y_train),(X_test,Y_test) = mnist.load_data()

#數據處理

#轉浮點、歸一化

#不需要處理標籤
#因爲是圖和圖之間相比
X_train = X_train.astype(‘float32’)/255.0 #set as type to
X_test = X_test.astype(‘float32’)/255.0

#reshape數據形狀 適用於dense層的input需要
X_train = X_train.reshape(X_train.shape[0],784)#X_train.shape[0]是訓練集的記錄數目
X_test = X_test.reshape(-1,784)#-1缺省表示測試集的所有記錄數目,等價於X_test.shape[0]

#定義encoding的終極維度

#畫散點圖的時候使用維度爲2
#encoding_dim = 2 #輸出的時候需要用座標來可視化

#打印編碼層的輸出時,2個維度不好打印成圖片,所以該寫成維度爲4
encoding_dim = 4

#定義輸入層Input可以接受的數據shape,類似tensorflow的 placeholder
input_img = Input(shape=(784,)) #此時的Input像一個‘層’

#定義編碼層 把數據從大維度降低到小維度 如28*28 -》 784

#第一層編碼
encoded = Dense(
units = 128, #第一層的輸出維度爲128
activation = ‘relu’
)(input_img)

#第二層編碼
encoded = Dense(
units = 64,
activation=‘relu’
)(encoded) #此處的輸入是來自於上一層的編碼器encoded,下面的也類似

#第三層編碼
encoded = Dense(
units = 32,
activation=‘relu’
)(encoded)

#第四層編碼 並輸出給 解碼層

encoded_output = Dense(
units = encoding_dim,
)(encoded)

#第四層結束之後可以輸出結果了,如果你想的話
#四層編碼層的另一種呈現方式
#encoded = Dense(units = 128,activation = ‘relu’)(input_img)
#encoded = Dense(units = 64,activation = ‘relu’)(encoded)
#encoded = Dense(units = 32,activation = ‘relu’)(encoded)
#encoded_output = Dense(units = encoding_dim)(encoded)

#定義解碼層

#第一層解碼
decoded = Dense(units = 32,activation=‘relu’)(encoded_output)

#第二層編碼
decoded = Dense(units= 64,activation=‘relu’)(decoded)

#第三層編碼
decoded = Dense(units = 128,activation=‘relu’)(decoded)

#第四層編碼
#還原到784
#改變了激活函數
#decoded=Dense(units = 784,activation=‘tanh’)(decoded)
decoded = Dense(units=784,activation=‘tanh’)(decoded)

#構建自動編碼模型結構
#誰給model數據,model處理完給誰
autoencoder = Model(inputs=input_img,output=decoded)

#構建編碼模型結構
encoder = Model(inputs=input_img,outputs=encoded_output)

#編譯模型
autoencoder.compile(optimizer=‘adam’,loss=‘mse’) #均差方

#訓練
#原圖經過編碼、解碼後得到一個復原的圖,這個復原圖和原圖來進行比對
autoencoder.fit(
x = X_train,
y = X_train,
epochs = 10,
batch_size = 512,
shuffle = True,#每個訓練epoch完成後,數據會打亂
)

autoencoder.save(’/content/drive/My Drive/app/autoencoder_mnist.h5’)
#讀取自己的圖片
img_test5 = Image.open(’/content/drive/My Drive/app/my_image/test5.jpg’)
img_5 = Image.open(’/content/drive/My Drive/app/my_image/5.jpg’)

img_test5 = np.array(img_test5)
img_5 = np.array(img_5)

encoded_img_test5 = encoder.predict(img_test5.reshape(1,784))
encoded_img_5 = encoder.predict(img_3.reshape(1,784))
#打印編碼層之後的結果,看看兩個數字相同的圖片在特徵提取上是否相近
print(encoded_img_test5)

print(encoded_img_5)

encoded_imgs = encoder.predict(X_test)
#打印第一組數據
print(encoded_imgs[0])
plt.scatter(x=encoded_imgs[:,0],y=encoded_imgs[:,1],c=Y_test,s=2)#c主要用來控制顏色,可以是一個數值數組;s是設置精度的
plt.show()

#打印一個 三個圖對比情況

#全網絡輸出
decoded_img = autoencoder.predict(X_test[1].reshape(1,784)) #此處必須要reshape一下,告訴系統,這是一個(784,)的樣本
#編碼層輸出
encoded_img = encoder.predict(X_test[1].reshape(1,784))

plt.figure(1)
plt.imshow(decoded_img[0].reshape(28,28))

plt.figure(2)
plt.imshow(X_test[1].reshape(28,28))

plt.figure(3)
#因爲2個維度沒法打印。所以將上文中的encoded_dim改爲4,然後編碼層的輸出結構就可以被reshape爲2*2
plt.imshow(encoded_img[0].reshape(2,2))

第四層編碼層結束之後,如果想要輸出的話會得到中間的比較模糊的那個圖。
在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章