使用 Keras 对我的数据训练自动编码器



我有一个用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)

有什么想法吗?

虽然你遇到了形状问题,但我建议使用 Keras 的图像预处理功能,特别是 ImageDataGenerator 类:

keras.preprocessing.image.ImageDataGenerator:通过实时数据增强生成批量张量图像数据。数据将被循环(分批)。

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

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

它将从目录中获取图像,并在应用任何所需的转换后作为输入输出对返回。文档很好地介绍了这些转换,并且它们允许您执行。

相关内容

  • 没有找到相关文章

最新更新