为什么我的tensorflow keras自动编码器返回所有给定输入图像的平均值?

时间:2018-07-26 10:00:30

标签: python tensorflow keras autoencoder

我最近尝试使用tensorflow和keras编写自己的自动编码器。它应该重新创建两张图片:一只狗和一只猫,但是在训练后,它会为输入的狗返回与返回给猫的相同的图片。有人知道我可以更改什么,以便我的网络返回狗和猫的单个图像作为输入吗?

输出猫或狗的图像作为输入:

enter image description here

import numpy as np
from tensorflow.keras import *
from tensorflow.layers import *
from tensorflow.train import *
import tensorflow as tf
from PIL import Image

IMAGE_FOLDER = ''
LAYER_COUNT = 3
COMPILED_SIZE = 2
LEARNING_RATE = 0.1
EPOCHS = 1000
image_shape = ()
image_size = 0

images = []

# load images into an (1, image size) shaped numpy array
for filename in os.listdir(IMAGE_FOLDER):
    img = Image.open(IMAGE_FOLDER+filename)
    np_img = np.multiply(1/255, np.array(img))
    image_shape = np_img.shape
    np_img = np_img.flatten()
    image_size = len(np_img)
    images.append(np.reshape(np_img, (1, len(np_img)))[0])

# create keras.Sequential
network = Sequential()

# add layers to get an hourglass shaped network
for i in range(LAYER_COUNT):
    if i == 0:
        network.add(Dense(image_size-int((image_size-
               COMPILED_SIZE)/LAYER_COUNT), activation=tf.sigmoid,
               input_dim=image_size))
    else:
        network.add(Dense(
            network.get_layer(index=i-1).units - 
            int((network.get_layer(index=i-1).units-
            COMPILED_SIZE)/(LAYER_COUNT-i)),
            activation=tf.sigmoid))

for i in range(LAYER_COUNT-1):
    network.add(Dense(network.get_layer(index=LAYER_COUNT-2-i).units, 
        activation=tf.sigmoid))
network.add(Dense(image_size))

"""
for i in range(20):
    print(network.get_layer(index=i).units)
"""

# compile network
network.compile(optimizer=GradientDescentOptimizer(
learning_rate=LEARNING_RATE), loss=tf.losses.mean_squared_error)

# train for EPOCHS
for i in range(int(EPOCHS/10)):
    response = network.fit(np.array(images), np.array(images), epochs=
        (i+1)*10, verbose=0, initial_epoch=i*10)
    print(i*10)
    if len(response.history) != 0:
        if 0.02 >= response.history['loss'][-1]:
            break

# return result of training
for i in range(len(images)):
    Image.fromarray(np.multiply(255, 
        np.reshape(network.predict(np.reshape(images[i], (1, image_size))), 
        image_shape)).astype('uint8')).show()

0 个答案:

没有答案