0

I tried to build a model that would help me identify images of a multi label classification problem, for example if I had pictures of cats, dogs and cows. I ran a CNN model but it didnt catch at all (gave a precision of 33%). Can anyone please share a model that works (even if the accuracy is just reasonable)? Thanks in advance to everyone! [attached a my code mentioned above]

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, 
BatchNormalization
from keras.callbacks import LearningRateScheduler
from keras.optimizers import adam, SGD
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16


# 2 - Create network layers
image_width = 200
image_height = 200

model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(3,3), 
activation='relu',input_shape=( 
(image_width,image_height,3)))
model.add(BatchNormalization())
model.add(Conv2D(filters=16, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPool2D(strides=(2,2)))
model.add(Dropout(0.25))
# Stage II = make it more compex with 'filters = 32'
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPool2D(strides=(2,2)))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))


# We'll Randomize the training set (shuffle), to avoid overfitting 
(augmentation)
datagen = ImageDataGenerator(zoom_range = 0.1,
                        height_shift_range = 0.1,
                        width_shift_range = 0.1,
                        rotation_range = 10)

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics= 

['accuracy'])

# automatically retrieve images and their classes for train and validation 
train_generator = datagen.flow_from_directory(
    train_dataset,
    target_size=(image_width, image_height),
    batch_size=32,
    class_mode='categorical')


validation_generator = datagen.flow_from_directory(
    validation_dataset,
    target_size=(image_width, image_height),
    batch_size=32,
    class_mode='categorical')

# Now let's fit the model on the validation set

model.fit_generator(
    train_generator,
    steps_per_epoch=50,
    epochs=500,
    validation_data=validation_generator,
    validation_steps=15)
RonJ
  • 1
  • 2
  • Why don't you consider transfer learning? You can refer code that I have written [here](https://github.com/tlokeshkumar/Fast-image-classification). – Lokesh Kumar Sep 21 '18 at 19:00
  • Hey @LokeshKumar, I'd be glad to refer some of your code, however I got a bit lost reading it and trying to see what could assist my problem... – RonJ Sep 23 '18 at 08:41

1 Answers1

0

One of the problems I see in your code is that, flow_from_directory does not support multi-label classification. It will only return a single label based on the sub-directories. Link to the docs

This could be a huge problem as your model is not even performing multi-label classification.

DollarAkshay
  • 2,063
  • 1
  • 21
  • 39