我正在尝试使用MLP对Keras中的图像进行分类。我的代码是:
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.preprocessing.image import ImageDataGenerator
# Prepare data generators for train and test
SIZE = 32
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.2)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'train',
target_size=(SIZE, SIZE),
class_mode='categorical',
batch_size=32,
subset='training')
val_generator = train_datagen.flow_from_directory(
'train',
target_size=(SIZE, SIZE),
class_mode='categorical',
batch_size=32,
subset='validation')
test_generator = test_datagen.flow_from_directory(
'test',
target_size=(SIZE, SIZE),
class_mode='categorical',
batch_size=32)
model_mlp = Sequential()
model_mlp.add(Flatten(input_shape=(32,32, 3)))
model_mlp.add(Dense(256, activation='relu'))
model_mlp.add(Dropout(0.5))
model_mlp.add(Dense(128, activation='relu'))
model_mlp.add(Dropout(0.5))
model_mlp.add(Dense(68, activation='softmax'))
# Compile
model_mlp.compile(optimizer='rmsprop', # other choices: adam or SGD
loss='categorical_crossentropy',
metrics=['accuracy'])
print(model_mlp.summary())
history = model_mlp.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=100,
validation_data=val_generator,
validation_steps=100)
# Showing
show(history, model_mlp)
我可以使用Flatten
层作为多层感知器模型的输入,以将32 * 32 * 3图像重塑为如上所述的矢量吗?谢谢
我的代码有问题吗?因为accuracy
小于5%,太低了。