使用10个时代时,Keras不起作用

时间:2018-03-02 19:05:04

标签: python tensorflow keras conv-neural-network

这绝对是奇怪的,但是当我使用10个时代运行我的Keras CNN模型时,每个时代(包括第一个时期)给我一个.333准确度(我有3个分类类。)使用任何其他数量的时期似乎工作得很好,在第一个时代之后给我〜.55准确度并继续增长到〜.9准确度。该模型定义如下:

import os
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, Activation, GlobalMaxPooling2D, PReLU, LeakyReLU
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

input_shape = (405, 270, 3)
batch_size = 16
epochs = 15

# training_directory = os.getcwd() + '\DataSubset\TrainImages'
# test_directory = os.getcwd() + '\DataSubset\TestImages'
# num_classes = 70
# train_samples = 27992
# test_samples = 7000

training_directory = os.getcwd() + '\SmallDataSubset\TrainImages'
test_directory = os.getcwd() + '\SmallDataSubset\TestImages'
num_classes = 3
train_samples = 9578
test_samples = 2415
target_shape = (405, 270)

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

train_generator = train_datagen.flow_from_directory(training_directory, target_size=target_shape, batch_size=batch_size)

test_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(test_directory, target_size=target_shape,
                                                                        batch_size=batch_size)


def create_short_model():
    model = Sequential()
    model.add(Conv2D(128, (3, 3), padding='same', activation='relu', input_shape=input_shape))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(256, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(256, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='sigmoid'))

    return model


model = create_short_model()

print(model.summary())

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

history = model.fit_generator(train_generator,
                              steps_per_epoch=train_samples // batch_size,
                              epochs=epochs,
                              validation_data=test_generator,
                              validation_steps=test_samples // batch_size)

plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

有没有其他人遇到过这个问题/有没有人知道我可能会遗漏的东西?

0 个答案:

没有答案