keras自动编码器实现系列之卷积自动编码器操作
作者:xuyang2233 时间:2023-12-31 18:33:15
图片的自动编码很容易就想到用卷积神经网络做为编码-解码器。在实际的操作中,
也经常使用卷积自动编码器去解决图像编码问题,而且非常有效。
下面通过**keras**完成简单的卷积自动编码。 编码器有堆叠的卷积层和池化层(max pooling用于空间降采样)组成。 对应的解码器由卷积层和上采样层组成。
@requires_authorization
# -*- coding:utf-8 -*-
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
import os
## 网络结构 ##
input_img = Input(shape=(28,28,1)) # Tensorflow后端, 注意要用channel_last
# 编码器部分
x = Conv2D(16, (3,3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2,2), padding='same')(x)
x = Conv2D(8,(3,3), activation='relu', padding='same')(x)
x = MaxPooling2D((2,2), padding='same')(x)
x = Conv2D(8, (3,3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2,2), padding='same')(x)
# 解码器部分
x = Conv2D(8, (3,3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3,3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
# 得到编码层的输出
encoder_model = Model(inputs=autoencoder.input, outputs=autoencoder.get_layer('encoder_out').output)
## 导入数据, 使用常用的手写识别数据集
def load_mnist(dataset_name):
'''
load the data
'''
data_dir = os.path.join("./data", dataset_name)
f = np.load(os.path.join(data_dir, 'mnist.npz'))
train_data = f['train'].T
trX = train_data.reshape((-1, 28, 28, 1)).astype(np.float32)
trY = f['train_labels'][-1].astype(np.float32)
test_data = f['test'].T
teX = test_data.reshape((-1, 28, 28, 1)).astype(np.float32)
teY = f['test_labels'][-1].astype(np.float32)
# one-hot
# y_vec = np.zeros((len(y), 10), dtype=np.float32)
# for i, label in enumerate(y):
# y_vec[i, y[i]] = 1
# keras.utils里带的有one-hot的函数, 就直接用那个了
return trX / 255., trY, teX/255., teY
# 开始导入数据
x_train, _ , x_test, _= load_mnist('mnist')
# 可视化训练结果, 我们打开终端, 使用tensorboard
# tensorboard --logdir=/tmp/autoencoder # 注意这里是打开一个终端, 在终端里运行
# 训练模型, 并且在callbacks中使用tensorBoard实例, 写入训练日志 http://0.0.0.0:6006
from keras.callbacks import TensorBoard
autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
# 重建图片
import matplotlib.pyplot as plt
decoded_imgs = autoencoder.predict(x_test)
encoded_imgs = encoder_model.predict(x_test)
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
k = i + 1
# 画原始图片
ax = plt.subplot(2, n, k)
plt.imshow(x_test[k].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
# 画重建图片
ax = plt.subplot(2, n, k + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
# 编码得到的特征
n = 10
plt.figure(figsize=(20, 8))
for i in range(n):
k = i + 1
ax = plt.subplot(1, n, k)
plt.imshow(encoded[k].reshape(4, 4 * 8).T)
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
补充知识:keras搬砖系列-单层卷积自编码器
考试成绩出来了,竟然有一门出奇的差,只是有点意外。
觉得应该不错的,竟然考差了,它估计写了个随机数吧。
头文件
from keras.layers import Input,Dense
from keras.models import Model
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
导入数据
(X_train,_),(X_test,_) = mnist.load_data()
X_train = X_train.astype('float32')/255.
X_test = X_test.astype('float32')/255.
X_train = X_train.reshape((len(X_train),-1))
X_test = X_test.reshape((len(X_test),-1))
这里的X_train和X_test的维度分别为(60000L,784L),(10000L,784L)
这里进行了归一化,将所有的数值除上255.
设定编码的维数与输入数据的维数
encoding_dim = 32
input_img = Input(shape=(784,))
构建模型
encoded = Dense(encoding_dim,activation='relu')(input_img)
decoded = Dense(784,activation='relu')(encoded)
autoencoder = Model(inputs = input_img,outputs=decoded)
encoder = Model(inputs=input_img,outputs=encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
deconder = Model(inputs=encoded_input,outputs = decoder_layer(encoded_input))
模型编译
autoencoder.compile(optimizer='adadelta',loss='binary_crossentropy')
模型训练
autoencoder.fit(X_train,X_train,epochs=50,batch_size=256,shuffle=True,validation_data=(X_test,X_test))
预测
encoded_imgs = encoder.predict(X_test)
decoded_imgs = deconder.predict(encoded_imgs)
数据可视化
n = 10
for i in range(n):
ax = plt.subplot(2,n,i+1)
plt.imshow(X_test[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(2,n,i+1+n)
plt.imshow(decoded_imgs[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
完成代码
from keras.layers import Input,Dense
from keras.models import Model
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
(X_train,_),(X_test,_) = mnist.load_data()
X_train = X_train.astype('float32')/255.
X_test = X_test.astype('float32')/255.
X_train = X_train.reshape((len(X_train),-1))
X_test = X_test.reshape((len(X_test),-1))
encoding_dim = 32
input_img = Input(shape=(784,))
encoded = Dense(encoding_dim,activation='relu')(input_img)
decoded = Dense(784,activation='relu')(encoded)
autoencoder = Model(inputs = input_img,outputs=decoded)
encoder = Model(inputs=input_img,outputs=encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
deconder = Model(inputs=encoded_input,outputs = decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta',loss='binary_crossentropy')
autoencoder.fit(X_train,X_train,epochs=50,batch_size=256,shuffle=True,validation_data=(X_test,X_test))
encoded_imgs = encoder.predict(X_test)
decoded_imgs = deconder.predict(encoded_imgs)
##via
n = 10
for i in range(n):
ax = plt.subplot(2,n,i+1)
plt.imshow(X_test[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(2,n,i+1+n)
plt.imshow(decoded_imgs[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
来源:https://blog.csdn.net/xuyang508/article/details/74276181
标签:keras,卷积,编码器
0
投稿
猜你喜欢
JavaScript详解使用Promise处理回调地狱与async await修饰符
2024-04-22 22:43:58
Oracle19c 创建表空间遇到的坑
2024-01-15 14:18:24
python渗透测试linux密码激活的示例
2023-06-25 16:57:41
安装PyTorch的详细过程记录
2023-05-12 07:50:12
Python实现上下班抢个顺风单脚本
2021-01-12 15:56:51
Python jieba库用法及实例解析
2021-04-15 22:11:44
利用Python和C语言分别实现哈夫曼编码
2021-08-12 09:59:49
Python环境配置实现pip加速过程解析
2022-09-23 12:42:37
python3学习笔记之多进程分布式小例子
2022-08-23 04:46:07
Python实现8个概率分布公式的方法详解
2022-05-14 08:14:37
mysql批量更新多条记录的同一个字段为不同值的方法
2024-01-27 20:21:49
PHP utf-8编码问题,utf8编码,数据库乱码,页面显示输出乱码
2024-04-30 09:57:47
Websocket IM聊天教程 教你用GoEasy快速实现IM聊天
2023-10-04 08:52:52
Python Socket多线程并发原理及实现
2022-09-05 08:21:07
sql 查询慢的原因分析
2024-01-16 13:11:29
Django的ListView超详细用法(含分页paginate)
2021-02-13 00:53:41
Python RuntimeError: thread.__init__() not called解决方法
2022-12-22 17:11:46
pygame游戏之旅 游戏中添加显示文字
2023-03-26 02:56:23
Python检测PE所启用保护方式详解
2022-03-11 12:36:08
Python HTML解析模块HTMLParser用法分析【爬虫工具】
2023-10-04 02:07:09