keras model.fit_generator返回生成器的输出应该是一个元组

时间:2016-10-20 08:57:54

标签: python deep-learning keras

我是新的keras,我无法弄清楚为什么model.fit_generator(...)会返回异常"异常:生成器的输出应该是一个元组(x,y) ,sample_weight)或(x,y)。发现:无"不应该在哪里? 我的数据是7个等级的48 * 48灰度图像。

我的model.py文件:

import os
from keras.preprocessing.image import ImageDataGenerator
from model import get_model


img_width, img_height = 48, 48

train_data_dir = 'resources/data/train'
validation_data_dir = 'resources/data/validation'
nb_train_samples = sum([len(files) for r, d, files in os.walk(train_data_dir)])
nb_validation_samples = sum([len(files) for r, d, files in os.walk(validation_data_dir)])
nb_epoch = 100

train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_data_dir, color_mode="grayscale",
target_size=(img_height, img_width), batch_size=128, class_mode='categorical')

validation_generator = validation_datagen.flow_from_directory(validation_data_dir, color_mode="grayscale",
target_size=(img_height, img_width), batch_size=128, class_mode='categorical')

model = get_model()

for i in range(nb_epoch):
    print('epoch: ' + str(i))
    model.fit_generator(train_generator, samples_per_epoch=nb_train_samples, nb_epoch=1,
    validation_data=validation_generator, nb_val_samples=nb_validation_samples)

    model.save_weights('resources/models/'+str(i))

和我的train.py:

Using Theano backend.
Found 28709 images belonging to 7 classes.
Found 7178 images belonging to 7 classes.
epoch: 0
Epoch 1/1
Traceback (most recent call last):
  File "train.py", line 28, in <module>
validation_data=validation_generator, nb_val_samples=nb_validation_samples)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 874, in fit_generator
pickle_safe=pickle_safe)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1417, 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: None

和追溯:

<!DOCTYPE html>

0 个答案:

没有答案