深度学习实战笔记三:编码器、解码器+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))

第四层编码层结束之后,如果想要输出的话会得到中间的比较模糊的那个图。
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

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