import os
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
img_width, img_height = 64, 64
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = sum([len(files) for files in os.walk(train_data_dir)])
nb_validation_samples = sum([len(files) for files in os.walk(validation_data_dir)])
nb_epoch = 10
model = Sequential()
model.add(Dense(4096, input_dim = 4096, init='normal', activation='relu'))
model.add(Dense(4,init='normal',activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rescale=1./255,
)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
color_mode="grayscale",
target_size=(img_width, img_height),
batch_size=1,
class_mode=None)
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
color_mode="grayscale",
target_size=(img_width, img_height),
batch_size=1,
class_mode=None)
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)
一切都運行良好,直到上面的代碼中的model.fit_generator()。然後它會彈出如下所示的錯誤。model.fit_generator()形狀錯誤
Traceback (most recent call last):
File "C:/Users/Sam/PycharmProjects/MLP/Testing Code without CNN.py", line 55, in <module>
nb_val_samples=nb_validation_samples)
File "C:\Python27\lib\site-packages\keras\models.py", line 874, in fit_generator
pickle_safe=pickle_safe)
File "C:\Python27\lib\site-packages\keras\engine\training.py", line 1427, in fit_generator
'or (x, y). Found: ' + str(generator_output))
Exception: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: [[[[ 0.19215688]
這段代碼應該做什麼?你能提供一些例子嗎? –