I am a newbie in Python 3.5. I am trying to program a simple auto-encoder which will train on a data-set of 60 images of apple and try to reconstruct the image given in the root. I have used the following codes :
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)
It gives an error
AttributeError: 'DirectoryIterator' object has no attribute 'ndim'
Any idea what went wrong?
The Keras fit
function takes arrays of data, numpy arrays, not generators. The function you need is fit_generator
. Note that fit_generator
takes slightly different parameters, such as steps_per_epoch
instead of batch_size
.