有我的代码
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(48, 120, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(62*4, activation='softmax'))
# print(model.summary())
model.compile(
loss='categorical_crossentropy',
optimizer=RMSprop(lr=1e-4),
metrics=['accuracy']
)
history = model.fit_generator(
DataGenerator(train_dir, (48, 120, 1)),
steps_per_epoch=32,
epochs=30,
validation_data=DataGenerator(test_dir, (48, 120, 1)),
validation_steps=32
)
DataGenerator
class DataGenerator(Sequence):
def __init__(self, image_path, shape, to_fit=True, batch_size=32,):
self.image_path = image_path
self.shape = shape
self.to_fit = to_fit
self.batch_size = batch_size
self.all_images = self.get_all_images()
def __len__(self):
# return the number of batches per epoch
return len(os.listdir(self.image_path))
def __getitem__(self, index):
# return a batch of images and (labels) if we are predicting
if self.to_fit:
return self._genX_(index), self._genY_(index)
else:
return self._genX_(index)
def _genX_(self, index):
X = self.all_images[index*self.batch_size:(index+1)*self.batch_size]
X = [cv2.imread(img, 0) for img in X]
X = [img.reshape(self.shape) for img in X]
X = [img.astype('float32')/255 for img in X]
return X
def _genY_(self, index):
y = self.all_images[index*self.batch_size:(index+1)*self.batch_size]
return y
def on_epoch_end(self):
# do something after every epoch
return
def get_all_images(self):
return os.listdir(self.image_path)
我有什么
Epoch 1/30
Traceback (most recent call last):
File ".\test_1.py", line 42, in <module>
validation_steps=32
File "E:\Environments\Python\Python37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator
initial_epoch=initial_epoch)
File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training_generator.py", line 220, in fit_generator
reset_metrics=False)
File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training.py", line 1508, in train_on_batch
class_weight=class_weight)
File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training.py", line 579, in _standardize_user_data
exception_prefix='input')
File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training_utils.py", line 87, in standardize_input_data
if isinstance(data[0], list):
IndexError: list index out of range
我只是想编写自己的生成器来加载很多图像,但是效果并不好。
有人可以告诉我,我是怎么得到这个错误的?以及如何解决?