使用Keras对数据进行自动编码器训练

时间:2019-01-23 17:38:58

标签: python keras autoencoder

我有一个用Keras编写的自动编码器,如下所示。但是,我遇到以下错误:

ValueError: Error when checking model input: the list of Numpy arrays
that you are passing to your model is not the size the model expected. 
Expected to see 1 arrays but instead got the following list of 374 arrays

假设374是我的训练图像的数量。

在这种情况下,如何在数据上训练自动编码器?

from keras.layers import Input, Dense
from keras.models import Model
import os

training_directory = '/training'
testing_directory ='/validation'
results_directory = '/results'
training_images = []
validation_images = []

# the size of the encoded represenatation
encoding_dimension = 4096
# input placeholder
input_image = Input(shape=(262144,))
# the encoded representation of the input
encoded = Dense(encoding_dimension,activation='relu')(input_image)
# reconstruction of the input (lossy)
decoded = Dense(262144,activation='sigmoid')(encoded)
# map the input image to its reconstruction
autoencoder = Model(input_image,decoded)

# encoder model
# map an input image to its encoded representation
encoder = Model(input_image,encoded)

# decoder model

# place holder fpr an encoded input
encoded_input = Input(shape=(encoding_dimension,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input,decoder_layer(encoded_input))

for root, dirs, files in os.walk(training_directory):
    for file in files:
        image = cv2.imread(root + '/' + file)
        training_images.append(image)

for root, dirs, files in os.walk(testing_directory):
    for file in files:
        image = cv2.imread(root + '/' + file)
        validation_images.append(image)

autoencoder.compile(optimizer='adam',loss='binary_crossentropy')

autoencoder.fit(training_images,epochs=10,batch_size=20,shuffle=True,validation_data=validation_images)

encoded_images = encoder.predict(validation_images)
decoded_images = decoder.predict(encoded_images)

谢谢。

编辑

我添加了以下内容而不是for循环:

training_generator = ImageDataGenerator()
validation_generator = ImageDataGenerator()
training_images = training_generator.flow_from_directory(training_directory, class_mode='input')
validation_images = validation_generator.flow_from_directory(validation_directory, class_mode='input')

但是,得到了以下内容:

TypeError: Error when checking model input: data should be a Numpy
array, or list/dict of Numpy arrays. Found
<keras.preprocessing.image.DirectoryIterator object at 0x2aff3a806650>...

发生在此语句上的

autoencoder.fit(
    training_images, 
    epochs=10, 
    batch_size=20, 
    shuffle=True,
    validation_data=validation_images)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尽管您遇到形状问题,但我还是建议您使用Keras的图像预处理功能,尤其是ImageDataGenerator class

  

keras.preprocessing.image.ImageDataGenerator :使用实时数据增强生成一批张量图像数据。数据将分批循环。

它将使您能够使用转换,数据增强和其他有用的功能来利用您的数据。对于自动编码器,您需要:

img_gen.flow_from_directory(training_directory, ..., class_mode='input')

将从目录中获取图像,并在应用任何所需的转换后返回为输入-输出对。有关这些转换的文档写得很好,并且它们使您可以做到。