1

Hello I am trying to build an image classifier using Keras and CNN

I already trained a model for Binary classification and it works really well.

I applied the same knowledge to build a Image classification using multiple categories(Which is failing miserably) I have 5 classes I have created 5 folders inside jpeg dir and the directoy structure is as follows

C:\Users\jpeg

1.train

2.test

Inside train folder I have 5 subfolders each folder corresponding to a class

C:\Users\jpeg\train

1.Auth_Docs
2.Certificates_Reports
3.Document
4.Title
5.communication

and i placed appropriate images in each folder

Followed the exact same structure in test folder as well

source code:

import matplotlib.pyplot as plt
import cv2
%matplotlib inline
from keras.preprocessing.image import ImageDataGenerator
image_gen.flow_from_directory('C://Users/Jpeg/train')
image_gen.flow_from_directory('C://Users/jpeg/test')
image_shape = (150,150,3)

from keras.models import Sequential
from keras.layers import Activation, Dropout, Flatten, Dense, Conv2D, MaxPooling2D
model = Sequential()

model.add(Conv2D(filters=32, kernel_size=(3,3),input_shape=(150,150,3), activation='relu',))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=(150,150,3), activation='relu',))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=(150,150,3), activation='relu',))
model.add(MaxPooling2D(pool_size=(2, 2)))

# 
model.add(Flatten())

#hidden layer number of neurons
model.add(Dense(256, activation='relu'))

# Here we say randomly turn off 30% of neurons.
model.add(Dropout(0.3))

# Last layer(add number of layers based on number of categories)
model.add(Dense(5, activation='softmax'))

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

#Training the model
batch_size = 16

train_image_gen = image_gen.flow_from_directory('C://Users/jpeg/train',
                                               target_size=image_shape[:2],
                                               batch_size=batch_size,
                                               class_mode='categorical'
                                               )
#Found 2434 images belonging to 5 classes.

test_image_gen = image_gen.flow_from_directory('C://Users/jpeg/test',
                                               target_size=image_shape[:2],
                                               batch_size=batch_size,
                                               class_mode='categorical'
                                              )
#Found 60 images belonging to 5 classes.

train_image_gen.class_indices
#o/p
{'Auth_Docs': 0,
 'Certificates_Reports': 1,
 'Document': 2,
 'Title': 3,
 'communication': 4}

#Fitting the model
from PIL import Image 
Image.MAX_IMAGE_PIXELS = None
results = model.fit_generator(train_image_gen,epochs=50,
                              steps_per_epoch=100,
                              validation_data=test_image_gen,
                             validation_steps=12)

#saving the model
model.save('Document_Classification.h5')
#results.accuracy for my model gives around 80% of accuracy

Now the issue with testing the model

from keras.models import load_model
new_model = load_model('Document_Classification.h5')
import numpy as np
from keras.preprocessing import image
import os,sys
from PIL import Image 
Image.MAX_IMAGE_PIXELS = None
for a,b,c in os.walk("C:/Users/jpeg/test/communication"):

   for i in c:
     doc_img = image.load_img(os.path.join(a,i), target_size=(150, 150)) 
     doc_img = image.img_to_array(doc_img)  
     doc_img = np.expand_dims(doc_img, axis=0)   
     doc_img = doc_img/255
     #print (a,i)

     prediction_prob = new_model.predict_classes(doc_img)
     print(prediction_prob )

only output I get is

[2]

[2]

[2]

[2]

no matter which folder i use to test the o/p is the same a i.e in above example i used the communication folder images and the o/p is 2

same o/p when i test images from Auth_Docs,Title etc.

I don not see anything wrong in my code as this code worked for binary classification. Please advise

Also, I want to find what is the associated label with the output I am getting.

Please advise.

Thanks.

1 Answer 1

0

There are so many things you could do to troubleshoot. The amount of samples really matters; you should know this. Well, if I thought I had enough samples I'd save the images from the generators to check if they are ok (flow_from_directory - save_to_dir argument).

https://keras.io/preprocessing/image/

Also, while you are trainning, you could check tensorboard using callbacks (if you are using tensorflow) to see how bad/good your learning is going. Have a look at this video. See that what matters the most is val_acc.

2
  • Yes, thanks for your inputs but model.predict_classes should yied the class label(some places it is mentioned that for more than 2 classes it returns a onehot encoded value) but i just get an array [2] i want to know what label value it is predicting.
    – kumarm
    Mar 18, 2019 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.