Pytorch笔记:自编码

使用自编码生成和训练图片相近的图片,代码如下:

import torch
import cv2 as cv
import torch.nn as nn

def gettensor(s):#读取图片,转换成张量
    scr = cv.imread(s)
    #print(scr.shape)
    c = torch.from_numpy(scr)/1.00
    c=c.reshape(100*148*3)
    return c
#print(gettensor().shape)

def getimg(x,i):#神经网络得到的结果张量转换成图片并显示
    x=x.reshape(148,100,3)#原始图片100*148,RGB三色
    b = x.numpy()#*255
    cv.imwrite('F:\jin\jin'+str(100)+'.png', b)
    scr = cv.imread('F:\jin\jin'+str(100)+'.png')
    ds=cv.resize(scr,(100*6,148*6),interpolation=cv.INTER_LINEAR)
    cv.imwrite('F:\jin\jin' + str(100) + '.png', ds)
    cv.imshow("jin", ds)
    cv.waitKey(100)


class AutoEncoder(nn.Module):
    def __init__(self):
        super(AutoEncoder, self).__init__()

        self.encoder = nn.Sequential(
            nn.Linear(100*148*3, 28 * 28),
            nn.Tanh(),
            nn.Linear(28*28, 128),
            nn.Tanh(),
            nn.Linear(128, 64),
            nn.Tanh(),
            nn.Linear(64, 12),
            nn.Tanh(),
            nn.Linear(12, 10),   # compress to 3 features which can be visualized in plt
        )
        self.decoder = nn.Sequential(
            nn.Linear(10, 12),
            nn.Tanh(),
            nn.Linear(12, 64),
            nn.Tanh(),
            nn.Linear(64, 128),
            nn.Tanh(),
            nn.Linear(128, 28*28),
            nn.Tanh(),
            nn.Linear(28*28, 100*148*3),
            #nn.Sigmoid(),       # compress to a range (0, 1)
        )

    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return encoded, decoded

    def geti(self,inx):
        img=self.decoder(inx)
        return img
human=['F:\jin\sample1\li.jpg','F:\jin\sample1\gu.jpg','F:\jin\sample1\jin.jpg','F:\jin\sample1\liu.jpg']
autoencoder = AutoEncoder()
#print(human)
x1=gettensor(human[0])
x2=gettensor(human[1])
x3=gettensor(human[2])
x4=gettensor(human[3])
#input0=torch.stack([x1,x2,x3,x4],dim=0)
#input=torch.stack([x1,x2,x3,x4],dim=0) 多组数据同时喂进去
#训练过程
input0=x1
input=x1#输入输出相同
optimizer = torch.optim.Adam(autoencoder.parameters(), lr=0.005)
loss_func = nn.MSELoss()
for epoch in range(10000):
    #input = gettensor(human[epoch%4])
    #input0 = gettensor(human[epoch%4])
    encoded, decoded = autoencoder(input)
    loss = loss_func(decoded, input0)      # mean square error
    optimizer.zero_grad()               # clear gradients for this training step
    loss.backward()                     # backpropagation, compute gradients
    optimizer.step()                    # apply gradients
    print(epoch)
    print ("loss is %f"%loss.data.numpy())
    getimg(decoded.detach(),epoch)
    torch.save(autoencoder,'autoencoder.pkl')
#训练过程
#显示生成图
net1=torch.load('autoencoder.pkl')
for k in range(100):
    a=torch.randn(10)*2
    b=net1.geti(a).detach()
    print(b.shape)
    getimg(b,102)
#显示生成图

训练用的图片:
gu.jpg
gu.jpg
li.jpg
li.jpg
liu.jpg
liu.jpg
jin.jpg
jin.jpg
最终生成结果如下,由于要趋近于四张图片,导致生成的图片像四张脸叠加在一起。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值