简体   繁体   中英

Keras model accuracy is not improving - Image Classification

I have 4 classes and building a Keras model for image classification problem. I have tried a couple of adjustments but accuracy is not going beyond 75% and still loss is 64%.

I have 90,400 images as a training set and 20,000 images for testing.

Here is my model.

model = Sequential()
model.add(Conv2D(32, kernel_size = (3, 3),input_shape=(100,100,3),activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

batch_size = 64

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

training_set = train_datagen.flow_from_directory('/dir/training_set', target_size=(100,100),batch_size=batch_size,class_mode='binary')

test_set = test_datagen.flow_from_directory('/dir/test_set',target_size=(100,100), batch_size=batch_size, class_mode='binary')

# 90,400 images I have under the training_set directory and 20,000 under the test directory.

model.fit(training_set, steps_per_epoch=90400//batch_size, epochs=1,validation_data=test_set, validation_steps= 20000//batch_size)

I tried adjusting layers and dropouts but no luck. Any ideas?

Consider

  • Adding multiple convolutional layers (with Max pooling in between) enables the model to learn "low level" and "higher level" features
  • Adding more epochs to enable the model to learn from the input pictures. Neural Networks only learn "a little bit" along the gradient each time, it often takes multiple up to many epochs to have a sufficiently trained model.

Maybe start with less pictures but increase the epochs (and add a second conv/max pool pair) to keep calculation time under control!

If I encounter something like this, I would do following:

  1. Split my data into training-validation and test. Improve model by validation and use test to see final result.

  2. Removing Dropout layers since I don't have a proof that model is overfitted.

  3. If model is underfitted (your case),

    3.a. Try different / bigger architecture and searching better hyperparameters

    3.b. Training longer and try different optimization algorithms

  4. If model is overfitted,

    4.a. Try to get more data

    4.b. Regularization (L2, dropout etc.)

    4.c. Data augmentation

    4.d. Searching better hyperparameters

Note: You can always consider transfer learning. Basically, transfer leaning is using gained information from a successful model for your model.

You could try using one of the existing models in Keras and train it from scratch. I have used MobileNetV2 in the past and have gotten very good results.

When you initialize the model you can load pre-trained weights or None , and start traning from scratch with your images.

I was able to achieve accuracy with transfer learning using the pre-trained MobileNet model.

Attaching my code and confusion metrix here so it may be helpful to someone.

import pandas as pd
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense,GlobalAveragePooling2D
from keras.applications import MobileNet
from keras.preprocessing import image
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.optimizers import Adam

base_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(3,activation='softmax')(x) #final layer with softmax activation

model=Model(inputs=base_model.input,outputs=preds)

for layer in model.layers[:20]:
    layer.trainable=False
for layer in model.layers[20:]:
    layer.trainable=True

train_data_path = '../train_dataset_path'

train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input, validation_split=0.2) #included in our dependencies

train_generator=train_datagen.flow_from_directory(train_data_path,
                                                 target_size=(224,224),
                                                 color_mode='rgb',
                                                 batch_size=32,
                                                 class_mode='categorical',
                                                 shuffle=True,
                                                 subset='training')

test_generator=train_datagen.flow_from_directory(train_data_path, 
                                                 target_size=(224,224),
                                                 color_mode='rgb',
                                                 batch_size=32,
                                                 class_mode='categorical',
                                                 shuffle=False,
                                                 subset='validation')

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

step_size_train = train_generator.n//train_generator.batch_size
step_size_test  = test_generator.n//test_generator.batch_size


model_history = model.fit(train_generator,
                          steps_per_epoch=step_size_train,
                          epochs=5,
                          validation_data=test_generator,
                          validation_steps=step_size_test)


model.save('tl_interior_model_2')

#Load the model
model = keras.models.load_model('tl_interior_model_2')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM