简体   繁体   中英

Keras model giving very low training and validation accuracy for multi-label image classification

My code has 50 categories of images which are being passed into the following model. But the accuracies received are almost the same after any of the parameter tuning done by me. The training and validation data is correct.

Every category has 34 training images and 6 validation images.

import keras
from keras.layers import Activation, Dense, Dropout, Conv2D, Flatten, MaxPooling2D, BatchNormalization
from keras.models import Sequential
from keras.optimizers import Adam, SGD

model = Sequential()
input_shape=(256, 256, 3)
adam = Adam(lr=0.000001,decay=0.001)
#sgd = SGD(lr=0.1, decay=1e-2, momentum=0.9)
chanDim=-1
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization(axis=chanDim))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization(axis=chanDim))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization(axis=chanDim))

# model.add(Conv2D(64, (3, 3)))
# model.add(Activation('relu'))
# model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
# model.add(Dense(300))
# model.add(Dropout(rate=0.5))
# model.add(Activation('relu'))
model.add(Dense(512))
model.add(Dropout(rate=0.5))
model.add(Activation('relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(50))
model.add(Activation('softmax'))

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

import PIL
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator
train_data_dir = 'C:/Users/abhir/Desktop/Difference4/train'
validation_data_dir = 'C:/Users/abhir/Desktop/Difference4/validate'

epochs = 10
# adding more parameters to training generator did not affect much too
train_datagen = ImageDataGenerator(rescale=1./255)
validate_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=(256,256), batch_size=12, class_mode='categorical',seed=7)
validate_generator = validate_datagen.flow_from_directory(validation_data_dir, target_size=(256,256), batch_size=6, class_mode='categorical',seed=7)

#increasing the steps_per_epoch and batch size doesn't affect much as well..
model.fit_generator(train_generator, steps_per_epoch=100,epochs=5, validation_data=validate_generator, validation_steps=50)

The results are as follows : 100/100 [==============================] - 337s 3s/step - loss: 5.7115 - acc: 0.0308 - val_loss: 3.9834 - val_acc: 0.0367

You are traing a neural network with thousands of parameters with 34 images per category in 10 epochs (340 images). You can use a rule of thumb which says you should have more training examples than trainable parameters. The trainable parameters are printed with model.summary()

So you could try to use about 1000 epochs and see how your network overfits the training data but in the end it is not enough data. Take a look at the loss curves, and check the tensorboard histograms to see if your model is learning something.

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