实现自编码器对MNIST图像重构

实现自编码器对MNIST图像重构,第1张

文章目录 前言一、导入相关库二、搭建模型三、可视化四、模型训练五、结果写在最后


前言

使用的百度的paddle框架,在AIstudio上面运行本次任务。

# 查看当前挂载的数据集目录, 该目录下的变更重启环境后会自动还原
# View dataset directory. 
# This directory will be recovered automatically after resetting environment. 
!ls /home/aistudio/data
# 查看工作区文件, 该目录下的变更将会持久保存. 请及时清理不必要的文件, 避免加载过慢.
# View personal work directory. 
# All changes under this directory will be kept even after reset. 
# Please clean unnecessary files in time to speed up environment loading. 
!ls /home/aistudio/work
# 如果需要进行持久化安装, 需要使用持久化路径, 如下方代码示例:
# If a persistence installation is required, 
# you need to use the persistence path as the following: 
!mkdir /home/aistudio/external-libraries
!pip install beautifulsoup4 -t /home/aistudio/external-libraries
# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可: 
# Also add the following code, 
# so that every time the environment (kernel) starts, 
# just run the following code: 
import sys 
sys.path.append('/home/aistudio/external-libraries')

一、导入相关库
import paddle
import paddle.fluid as fluid
import numpy as np
import sys,os,math
import paddle.fluid.layers as F
import matplotlib.pylab as plt
from PIL import Image
二、搭建模型
class AutoEncoder(fluid.dygraph.Layer):
    def __init__(self):
        super(AutoEncoder, self).__init__()
        self.encoder = fluid.dygraph.Linear(784,128,act='sigmoid')
        self.decoder = fluid.dygraph.Linear(128,784,act='sigmoid')
    def loss(self,x,label):
        '''
        Args:
            label:原始图像
            x∶解码之后的图像
        Return:
            原始图像和解码图像之间的欧式距离
        '''
        return F.square(x-label)

    def forward(self,x):
        self.input_imgs = x
        x = self.encoder(x)
        x = self.decoder(x)
        return x
三、可视化
def show_array2img(array,title):
    rebuilded_img = Image.fromarray(array.astype('uint8')).convert('RGB')
    plt.imshow(rebuilded_img)
    plt.title(title)
    plt.show()

def draw_train_process(iters,loss):
    '''
    训练可视化
    '''
    plt.title('training',fontsize=24)
    plt.xlabel('iters',fontsize=20)
    plt.ylabel('loss',fontsize=20)
    plt.plot(iters,loss,color='green',label='loss')
    plt.legend()
    plt.grid()
    plt.show()

def fix_value(img_pixs):#像素拉伸
        '''
        img_pixs:featuremap的像素矩阵
        '''
        pix_max=np.max(img_pixs)#取最大像素
        pix_min=np.min(img_pixs)#取最小像素
        pix_range=np.abs(pix_max)+np.abs(pix_min)#获取像素距离
        if(pix_range==0): #如果所有值都是零则直接返回(下面不能除以零)
            return img_pixs
        pix_rate = 255/pix_range#获取像素缩放倍率
        pix_left = pix_min*pix_rate#获取最小还原像素值
        img_pixs = img_pixs*pix_rate-pix_left#整体像素值平移
        img_pixs[np.where(img_pixs<0)]=0. #增加鲁棒性,检查超出区间的像素值,np.where(a
        img_pixs[np.where(img_pixs>255)]=255.
        return img_pixs
四、模型训练
train_params = {
    'save_model_name':'AutoEncoder',
    'epoch_num':40000,
    'batch_size':32,
}

with fluid.dygraph.guard():
    print('start training')
    train_reader = paddle.batch(reader=paddle.reader.shuffle(
        paddle.dataset.mnist.train(),buf_size=512),batch_size=train_params['batch_size'])

    #读取一组32x784的输入,每次迭代都用这一个输入
    for i,data in enumerate(train_reader()) :
        temp_images = []
        for i in range(32):
            temp_images.append(np.reshape(data[i][0],(784)))
        temp_images=fluid.dygraph.to_variable(np.asarray(temp_images).reshape((32,784)))
        temp_images= (temp_images+1)/2  #原来的数据区间是[-1,1]这里把它调整到[0,1]
        break
    model = AutoEncoder() # 实例化模型
    model.train()
    all_iter = 0
    all_loss = []
    all_iters = []
    all_accs = []
    opt=fluid.optimizer.AdamOptimizer(learning_rate=0.00001,parameter_list=model.parameters())
    for pass_num in range(train_params['epoch_num']):
        predict = model(temp_images)
        avg_loss= F.mean(model.loss(predict,temp_images))
        avg_loss.backward()
        opt.minimize(avg_loss)
        opt.clear_gradients()
        all_iter+=1
        if all_iter%5000==0:
            all_loss.append(avg_loss.numpy()[0])
            all_iters.append(all_iter)
            print('pass_epoch:{},iters:{},loss:{}'.format(pass_num,all_iter,avg_loss.numpy()[0]))
    fluid.save_dygraph(model.state_dict(),train_params ['save_model_name']) #保存模型参数
    draw_train_process(all_iters,all_loss)
    predict = fix_value(predict.numpy())
    print(type(model.input_imgs))
    input_imgs = fix_value(model.input_imgs.numpy ())
    print('finished training')
    #图像重构显示
    for i in range(10):
        show_array2img(np.reshape(input_imgs[i],(28,28)), 'input_img')
        show_array2img(np.asarray(np.reshape(predict[i],(28,28))), 'rebuild_img')
五、结果



写在最后

仅对个人的深度学习实验做一次记录,文中不足、错误之处欢迎指正;
创作不易,点个赞吧!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1498045.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-25

发表评论

登录后才能评论

评论列表(0条)

保存