美文网首页
自编码器和潜在空间插值2021-03-25

自编码器和潜在空间插值2021-03-25

作者: 一只大南瓜 | 来源:发表于2021-03-25 18:07 被阅读0次

剪纸生成因为样本很少,使用GAN等生成模型行不通,为了能够产生新的图像,想到使用编码器,然后在潜空间插值看能否出现新的特征,附一张mnist的效果图



能够看出来渐变emm,我也期望能从图1到图2渐变,中间出现新特征,

然而失败了emmm
图1
图2
同时为了比较不同编码器的效果,有大佬吧各种编码器都进行实验:http://www.360doc.com/content/21/0217/12/7673502_962403408.shtml
github:https://github.com/tilman151/ae_bakeoff

自编码器相关内容可以参看这个博客:https://blog.csdn.net/slx_share/article/details/93048696
博客的代码可以 运行。
在本文潜空间插值直接采用np来做的,比较简单。

import tensorflow as tf
import numpy as np
import os
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D
import cv2
import random


# Visualize decoder setting
# Parameters
learning_rate = 0.01
batch_size = 4
display_step = 10
examples_to_show = 10
training_epochs = 200
iteration =16 #iteration per epoch

# tf Graph input (only pictures)
input_img = tf.placeholder("float", [None, 200,600,1])
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)

encoded_shape = tf.shape(encoded)

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)

x = Conv2D(32, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
decoded = tf.image.resize_images(decoded, [200,600])

# Prediction
y_pred = decoded
# Targets (Labels) are the input data.
y_true = input_img

# Define loss and optimizer, minimize the squared error
# 比较原始数据与还原后的拥有 784 Features 的数据进行 cost 的对比,
# 根据 cost 来提升我的 Autoencoder 的准确率
loss = tf.reduce_mean(tf.pow(y_true - y_pred, 2))  # 进行最小二乘法的计算(y_true - y_pred)^2
# loss = tf.reduce_mean(tf.square(y_true - y_pred))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)


samplepath = './sample/'
image_path = './data/'
image_list =[]

test_imge= cv2.imread('./test/100.jpg',0)   #验证自编码的效果
test_imge = test_imge[np.newaxis,:,:,np.newaxis]
test_imge = test_imge/255


for item in os.listdir(image_path):
   image_name = os.path.join(image_path,item)
   image = cv2.imread(image_name,0)
   image = image[np.newaxis,:,:,np.newaxis]
   image = image/255
   image_list.append(image)

with tf.Session() as sess:
   init = tf.global_variables_initializer()
   sess.run(init)
   # Training cycle
   for epoch in range(training_epochs):  # 到好的的效果,我们应进行10 ~ 20个 Epoch 的训练
       # Loop over all batches
       random.shuffle(image_list)
       for i in range(iteration ):
           # Run optimization op (backprop) and cost op (to get loss value)
           _, c = sess.run([optimizer, loss],feed_dict={input_img: image_list[i] })
           if i % 100 == 0:
               img = sess.run(y_pred, feed_dict={input_img: image_list[i]})
               img1 = np.squeeze(img)
               img = img1*255
               img = np.clip(img,0,255)
               sample_name = samplepath + str(epoch) + '.png'
               cv2.imwrite(sample_name, img)
       # Display logs per epoch step
       if epoch % display_step == 0:
           print("Epoch:", '%04d' % (epoch + 1),"cost=", "{:.9f}".format(c))
   print("Optimization Finished!")
   #潜在空间插值
   latent1 = sess.run(encoded, feed_dict={input_img: image_list[3]})
   latent2 = sess.run(encoded, feed_dict={input_img: image_list[7]})
   lat1 = np.squeeze(latent1).flatten()
   lat2 = np.squeeze(latent2).flatten()
   latent = np.linspace(lat1,lat2,50)
   j = 1
   for arr in latent:

       arr = arr.reshape(1,25,75,8)
       img = sess.run(decoded,feed_dict={encoded:arr})
       img1 = np.squeeze(img)
       img = img1 * 255
       img = np.clip(img, 0, 255)
       sample_name = './result/' + str(j) + '.png'
       cv2.imwrite(sample_name, img)
       j+=1

图1
潜空间正中间插值
图2

可以看出来图1的特征在逐渐淡化,图2 的特征在逐渐显现。但是并没有出现新的纹样。

相关文章

网友评论

      本文标题:自编码器和潜在空间插值2021-03-25

      本文链接:https://www.haomeiwen.com/subject/qpfrhltx.html