【发布时间】: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