gpt4 book ai didi

python - 错误 - AttributeError : 'DirectoryIterator' object has no attribute 'ndim in autoencoder design in keras

转载 作者:行者123 更新时间:2023-11-28 21:35:01 25 4
gpt4 key购买 nike

我是 Python 3.5 的新手。我正在尝试编写一个简单的自动编码器,它将在 60 张苹果图像的数据集上进行训练,并尝试重建根中给出的图像。我使用了以下代码:

from keras.layers import Input, Dense
from keras.models import Model
import numpy as np
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
image = Image.open('C:\Python35\Scripts\apple.jpg')
encoding_dim = 32
input_img = Input(shape=(65536,))
encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(65536, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
train_datagen=ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
directory=r"C:\Users\vlsi\Desktop\train",
batch_size=32,
class_mode="categorical",
shuffle=True,
seed=42
)
autoencoder.fit(train_generator,
epochs=2,
batch_size=256,
shuffle=True)
encoded_img = encoder.predict(np.array(image))
decoded_img = decoder.predict(encoded_img)
plt.imshow(decoded_img)

它给出了一个错误

AttributeError: 'DirectoryIterator' object has no attribute 'ndim'



知道出了什么问题吗?

最佳答案

Keras fit函数采用数据数组,numpy 数组,而不是生成器。您需要的功能是fit_generator .请注意 fit_generator采用略有不同的参数,例如 steps_per_epoch而不是 batch_size .

关于python - 错误 - AttributeError : 'DirectoryIterator' object has no attribute 'ndim in autoencoder design in keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52779602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com