我正在尝试将6类时频域信号(STFT频谱图)分类为3601x217像素。假设每个课程有70个训练样本,20个验证样本和10个测试样本。每个样本都是尺寸为3601x217x1(灰度)的PNG图像,而将来我正在考虑将其扩展为3601x217x4(3个颜色通道+ alpha)。但是现在,我首先关注灰度。
尝试纯LSTM后,我发现该模型很快过拟合(训练精度> 90%,但是val_acc停留在20%)。 ow,我想尝试一下Time Distributed CNN + LSTM。
以下是代码:
img_width, img_height = 3601,217
train_data_dir = 'sensor1/training'
validation_data_dir = 'sensor1/validation'
num_classes = 6
nb_train_samples = 70
nb_validation_samples = 20
epochs = 50
batch_size = 5
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(TimeDistributed(Conv2D(16, (3,3), padding='same', strides=(2,2), activation='relu', input_shape = input_shape)))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Conv2D(32, (3,3), padding='same', strides=(2,2), activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Conv2D(64, (3,3), padding='same', strides=(2,2), activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(units=128, return_sequences=False))
model.add(LSTM(units=64, return_sequences=False))
model.add(Dense(32))
model.add(Dense(num_classes, 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, target_size=(img_width, img_height),
batch_size=batch_size, color_mode='grayscale')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir, target_size=(img_width, img_height),
batch_size=batch_size, color_mode='grayscale')
model.fit_generator(
train_generator, steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs, callbacks=[plot_losses],
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
代码给出以下错误:
_make_train_function中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py”,第496行,引发RuntimeError(“使用模型之前必须先对其进行编译。”) RuntimeError:您必须先编译模型,然后再使用它。
代码有什么问题?我实际上是在调用model.fit_generator之前编译了模型:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])