我正在尝试检测交通信号灯。我有3个标签的图像,所以我将训练数据和验证数据放入3个子文件夹中进行训练和验证。当我运行程序时,我在model.fit_generator()时遇到错误。其他的事情都很好。
`
import os
from keras.layers import Dense, Dropout, Flatten, MaxPooling2D,
Convolution2D
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
import keras
from keras.optimizers import Adam
NUM_CHANNELS = 3
IMAGE_WIDTH = 224 # Original: 455
IMAGE_HEIGHT = 224 # Original: 256
NUM_CLASSES = 3
base_script_name = os.path.splitext(__file__)[0]
filepath=base_script_name + "-{epoch:02d}-val_acc-{val_acc:.2f}.hdf5"
def get_generator(directory, train):
if train:
datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
else:
datagen = ImageDataGenerator(rescale=1./255)
return datagen.flow_from_directory(
directory=directory,
target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
batch_size=8,
class_mode='categorical')
model = Sequential([
Convolution2D(16, 3, 3, border_mode='same', subsample=(2, 2), input_shape=
(IMAGE_WIDTH, IMAGE_HEIGHT, NUM_CLASSES), activation='relu'),
MaxPooling2D(pool_size=(3, 3)),
Dropout(0.2),
Convolution2D(32, 3, 3, border_mode='same', activation='relu'),
MaxPooling2D(pool_size=(3, 3)),
Dropout(0.2),
Convolution2D(64, 3, 3, border_mode='same', activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Dropout(0.2),
Flatten(),
Dense(128, activation='tanh'),
Dropout(0.3),
Dense(NUM_CLASSES, activation='softmax'),
])
model.summary()
directory = '/home/nishat/traffic_light/nexar/try/data/'
train_generator = get_generator(directory+'/train', True)
validation_generator = get_generator(directory+'valid', False)
model.compile(optimizer=Adam(lr=0.0003), loss='categorical_crossentropy',
metrics=['accuracy'])
# Callbacks
checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=False, mode='auto')
tensorboard = keras.callbacks.TensorBoard(log_dir='./tensorboar',
histogram_freq=0, write_graph=True, write_images=True)
callbacks = [checkpoint, tensorboard]
model.fit_generator(
train_generator,
samples_per_epoch=train_generator.shape,
nb_epoch=200,
validation_data=validation_generator,
nb_val_samples=validation_generator.shape,
callbacks=callbacks,
)
model.evaluate_generator(validation_generator,
val_samples=len(validation_generator.filenames))`
我收到如下错误:
Found 16791 images belonging to 3 classes.
Found 1868 images belonging to 3 classes.
Traceback (most recent call last):
File "<ipython-input-1-303a170ea444>", line 1, in <module>
runfile('/home/nishat/traffic_light/nexar/try/train.py', wdir='/home/nishat/traffic_light/nexar/try')
File "/home/nishat/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/home/nishat/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/nishat/traffic_light/nexar/try/train.py", line 69, in <module>
samples_per_epoch=train_generator.shape,
AttributeError: 'DirectoryIterator' object has no attribute 'shape'
如果有人可以建议我需要更改我的代码,我将非常感激。感谢
答案 0 :(得分:0)
两点:
1- train_generator和validation_generator没有'shape'属性
model.fit_generator( train_generator,
samples_per_epoch=train_generator,
nb_epoch=200,
validation_data=validation_generator,
nb_val_samples=validation_generator,
callbacks=callbacks)
2-最新的Keras版本'samples_per_epoch'重命名为'steps_per_epoch','nb_epoch'重命名为'epochs'
每个时期的步数可以按如下方式计算:
steps_per_epoch = int(math.ceil(float(train_ds_len) / batch_size))