我在keras中构建自动编码器时遇到了很多麻烦,这使我可以加载自定义数据集进行培训和测试。我能够找到一些应该起作用的代码,但是尝试运行它时却不断出错。这是我的代码,将在树莓派上运行:
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('/home/pi/Downloads/neural-network-master/data/train/class_a/test(2chunk0.wav).png.jpg')
encoding_dim = 28
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"/home/pi/Downloads/neural-network-master/data/train",
batch_size=32,
class_mode="categorical",
shuffle=True,
seed=42
)
autoencoder.fit_generator(train_generator,
epochs=2,
steps_per_epoch=256,
shuffle=True)
encoded_img = encoder.predict(np.array(image))
print (encoded_img)
decoded_img = decoder.predict(encoded_img)
plt.imshow(decoded_img)
plt.imshow(image)
我得到的错误是:
ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (262, 1)
我认为这是由于喀拉拉邦人不喜欢输入的形状引起的。任何帮助将非常感激。谢谢。另外,如果有人可以向我解释density_2的意思...