使用自编码生成和训练图片相近的图片,代码如下:
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
li.jpg
liu.jpg
jin.jpg
最终生成结果如下,由于要趋近于四张图片,导致生成的图片像四张脸叠加在一起。