CNN准确度增长太快 - Keras

时间:2017-10-09 04:34:27

标签: python keras classification image-recognition

我正在研究更多关于卷积神经网络的知识,所以我尝试创建一个简单的图像分类器,它应该将图像分类为猫或狗。当我训练模型时,问题出现了,它在第一秒就已经达到了94%的精度。我想由于某种原因,它过度拟合了。 可以在此处找到数据集:Dataset

import pandas as pd
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D
from scipy import misc
import numpy as np

def build_classifier():
    #Model based on 'https://www.researchgate.net/profile/Le_Lu/publication/277335071/figure/fig8/AS:294249976352779@1447166069905/Figure-8-The-proposed-CNN-model-architecture-is-composed-of-five-convolutional-layers.png'
    #It's smarter to add layer without creating variables because of the processing, but as a small dataset it doesn't matter a lot.
    classifier = Sequential()

    conv1 = Conv2D(filters=64, kernel_size=(2,2), activation='relu', input_shape=(64,64,3))
    conv2 = Conv2D(filters=192, kernel_size=(2,2), activation='relu')
    conv3 = Conv2D(filters=384, kernel_size=(2,2), activation='relu')
    conv4 = Conv2D(filters=256, kernel_size=(2,2), activation='relu')
    conv5 = Conv2D(filters=256, kernel_size=(2,2), activation='relu')
    pooling1 = MaxPooling2D(pool_size=(2,2))
    pooling2 = MaxPooling2D(pool_size=(2,2))
    pooling3 = MaxPooling2D(pool_size=(2,2))
    fcl1 = Dense(1024, activation='relu')
    fcl2 = Dense(1024, activation='relu')
    fcl3 = Dense(1, activation='sigmoid')
    dropout1= Dropout(0.5)
    dropout2 = Dropout(0.5)
    flatten = Flatten()

    layers = [conv1, pooling1, conv2, pooling2, conv3, conv4, conv5,
             pooling3, flatten, fcl1, dropout1, fcl2, dropout2, fcl3]

    for l in layers:
        classifier.add(l)

    return classifier

model = build_classifier()
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

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

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        'dataset/train',
        target_size=(64, 64),
        batch_size=32,
        class_mode='banery')

validation_generator = test_datagen.flow_from_directory(
        'dataset/test',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

model.fit_generator(
        train_generator,
        steps_per_epoch=2000,
        epochs=50,
        validation_data=validation_generator,
        validation_steps=800)`

0 个答案:

没有答案