【问题标题】:Training an autoencoder on my data using Keras使用 Keras 对我的数据训练自动编码器
【发布时间】:2019-01-23 17:38:57
【问题描述】:

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

有什么想法吗?

【问题讨论】:

  • 我认为问题取决于 traning_images 列表的形状,link 希望对您有所帮助

标签: python keras autoencoder


【解决方案1】:

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

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

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

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

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

【讨论】:

猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 2021-04-03
相关资源
最近更新 更多