Tuesday, 7 June 2016

Feeding your own data set into the CNN model in Keras

# The code for Feeding your own data set into the CNN model in Keras
# please refer to the you tube video for this lesson -

https://www.youtube.com/watch?v=2pQOXjpO_u0&index=18&list=PLd9i_xMMzZF7eIjnVuPxggYuGPg5CnA1l


####just copy and paste the below given code to your shell

#KERAS
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop,adam
from keras.utils import np_utils

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import os
import theano
from PIL import Image
from numpy import *
# SKLEARN
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split

# input image dimensions
img_rows, img_cols = 200, 200

# number of channels
img_channels = 1

#%%
#  data

path1 = 'C:\Users\Ripul\Documents\Python Scripts\keeras-cnn-tutorial\input_data'    #path of folder of images    
path2 = 'C:\Users\Ripul\Documents\Python Scripts\keeras-cnn-tutorial\input_data_resized'  #path of folder to save images    

listing = os.listdir(path1)
num_samples=size(listing)
print num_samples

for file in listing:
    im = Image.open(path1 + '\\' + file)  
    img = im.resize((img_rows,img_cols))
    gray = img.convert('L')
                #need to do some more processing here          
    gray.save(path2 +'\\' +  file, "JPEG")

imlist = os.listdir(path2)

im1 = array(Image.open('input_data_resized' + '\\'+ imlist[0])) # open one image to get size
m,n = im1.shape[0:2] # get the size of the images
imnbr = len(imlist) # get the number of images

# create matrix to store all flattened images
immatrix = array([array(Image.open('input_data_resized'+ '\\' + im2)).flatten()
              for im2 in imlist],'f')
               
label=np.ones((num_samples,),dtype = int)
label[0:89]=0
label[89:187]=1
label[187:]=2


data,Label = shuffle(immatrix,label, random_state=2)
train_data = [data,Label]

img=immatrix[167].reshape(img_rows,img_cols)
plt.imshow(img)
plt.imshow(img,cmap='gray')
print (train_data[0].shape)
print (train_data[1].shape)

#%%

#batch_size to train
batch_size = 32
# number of output classes
nb_classes = 3
# number of epochs to train
nb_epoch = 20


# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3

#%%
(X, y) = (train_data[0],train_data[1])


# STEP 1: split X and y into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4)


X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

X_train /= 255
X_test /= 255

print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

i = 100
plt.imshow(X_train[i, 0], interpolation='nearest')
print("label : ", Y_train[i,:])

#%%

model = Sequential()

model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
                        border_mode='valid',
                        input_shape=(1, img_rows, img_cols)))
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
convout2 = Activation('relu')
model.add(convout2)
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')

#%%

hist = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
              show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))
           
           
hist = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
              show_accuracy=True, verbose=1, validation_split=0.2)


# visualizing losses and accuracy

train_loss=hist.history['loss']
val_loss=hist.history['val_loss']
train_acc=hist.history['acc']
val_acc=hist.history['val_acc']
xc=range(nb_epoch)

plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])

plt.figure(2,figsize=(7,5))
plt.plot(xc,train_acc)
plt.plot(xc,val_acc)
plt.xlabel('num of Epochs')
plt.ylabel('accuracy')
plt.title('train_acc vs val_acc')
plt.grid(True)
plt.legend(['train','val'],loc=4)
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])




#%%      

score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
print(model.predict_classes(X_test[1:5]))
print(Y_test[1:5])



#%%

# visualizing intermediate layers

output_layer = model.layers[1].get_output()
output_fn = theano.function([model.layers[0].get_input()], output_layer)

# the input image

input_image=X_train[0:1,:,:,:]
print(input_image.shape)

plt.imshow(input_image[0,0,:,:],cmap ='gray')
plt.imshow(input_image[0,0,:,:])


output_image = output_fn(input_image)
print(output_image.shape)

# Rearrange dimension so we can plot the result 
output_image = np.rollaxis(np.rollaxis(output_image, 3, 1), 3, 1)
print(output_image.shape)


fig=plt.figure(figsize=(8,8))
for i in range(32):
    ax = fig.add_subplot(6, 6, i+1)
    #ax.imshow(output_image[0,:,:,i],interpolation='nearest' ) #to see the first filter
    ax.imshow(output_image[0,:,:,i],cmap=matplotlib.cm.gray)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))
    plt.tight_layout()
plt

# Confusion Matrix

from sklearn.metrics import classification_report,confusion_matrix

Y_pred = model.predict(X_test)
print(Y_pred)
y_pred = np.argmax(Y_pred, axis=1)
print(y_pred)
 
                       (or)

y_pred = model.predict_classes(X_test)
print(y_pred)

p=model.predict_proba(X_test) # to predict probability

target_names = ['class 0(BIKES)', 'class 1(CARS)', 'class 2(HORSES)']
print(classification_report(np.argmax(Y_test,axis=1), y_pred,target_names=target_names))
print(confusion_matrix(np.argmax(Y_test,axis=1), y_pred))

# saving weights

fname = "weights-Test-CNN.hdf5"
model.save_weights(fname,overwrite=True)



# Loading weights

fname = "weights-Test-CNN.hdf5"
model.load_weights(fname)


# please refer to the you tube video for this lesson -

https://www.youtube.com/watch?v=2pQOXjpO_u0&index=18&list=PLd9i_xMMzZF7eIjnVuPxggYuGPg5CnA1l




49 comments:

  1. Hello Anuj;
    Thank you for your great tutorial. Can you please tell what is the number 167 in this line

    img=immatrix[167].reshape(img_rows,img_cols)

    Thank you at advance

    ReplyDelete
    Replies
    1. I think it's just a random number to test out the img, if you watch the video you will know

      Delete
  2. Hello Anuj! Thank you for your really valuable tutorial!
    I have a question though, if each instance has many labels, how can i add more labels to each one? For example if a picture has a car and a horde too, how can i define that this picture has two classes?
    Thank you in advance!

    ReplyDelete
  3. Hi Anuj
    I was searching for something like that so long ago, and you have a complete tutorial of it and is awesome. I am working with CNN in keras for face detection, specifically facial gestures. I have been doing some test of your code with my own images and 5 classes: Happy, sad, angry, scream and surprised. But my accuracy value is about 50% or between 47.50 and 52.50. Can you help me to improve the accuracy of the CNN?

    Thanks a lot for your tutorial

    ReplyDelete
    Replies
    1. I have the similar problem, my classifier is for body parts (face, hand, arm, foot, leg ..) however the validation accuracy also remained at around 50%. I think it's related to our input data, say an image labeled as arm may also contain hand. But I don't want to shift my problem to multi-label one (I think it will be much more complex), let me know if you find a way to improve accuracy. thx

      Delete
  4. Hello Anuj,

    I am getting below error. Can you please advise.

    Traceback (most recent call last):
    File "CNN_Fingerprint_4.py", line 118, in
    verbose=1, validation_data=(X_test, Y_test))
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/models.py", line 620, in fit
    sample_weight=sample_weight)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1104, in fit
    callback_metrics=callback_metrics)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 822, in _fit_loop
    outs = f(ins_batch)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 672, in __call__
    return self.function(*inputs)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 871, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/theano/gof/link.py", line 314, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 859, in __call__
    outputs = self.fn()
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/theano/gof/op.py", line 912, in rval
    r = p(n, [x[0] for x in i], o)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/theano/tensor/subtensor.py", line 2286, in perform
    out[0][inputs[2:]] = inputs[1]
    IndexError: index 1 is out of bounds for axis 1 with size 1
    Apply node that caused the error: AdvancedIncSubtensor{inplace=False, set_instead_of_inc=True}(Alloc.0, TensorConstant{1}, ARange{dtype='int64'}.0, Elemwise{Cast{int32}}.0)
    Toposort index: 70
    Inputs types: [TensorType(float32, matrix), TensorType(int8, scalar), TensorType(int64, vector), TensorType(int32, vector)]
    Inputs shapes: [(20, 1), (), (20,), (20,)]
    Inputs strides: [(4, 4), (), (8,), (4,)]
    Inputs values: ['not shown', array(1, dtype=int8), 'not shown', 'not shown']
    Outputs clients: [[Reshape{2}(AdvancedIncSubtensor{inplace=False, set_instead_of_inc=True}.0, MakeVector{dtype='int64'}.0)]]

    Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
    File "CNN_Fingerprint_4.py", line 113, in
    model.compile(loss='sparse_categorical_crossentropy', optimizer='adadelta', metrics=["accuracy"])
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/models.py", line 547, in compile
    **kwargs)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 622, in compile
    sample_weight, mask)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 324, in weighted
    score_array = fn(y_true, y_pred)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/objectives.py", line 44, in sparse_categorical_crossentropy
    return K.sparse_categorical_crossentropy(y_pred, y_true)
    File "/Users/prapo/anaconda/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 925, in sparse_categorical_crossentropy
    target = T.extra_ops.to_one_hot(target, nb_class=output.shape[-1])

    HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

    ReplyDelete
  5. Hi Anuj ,
    I am getting the following error :
    "UserWarning: The "show_accuracy" argument is deprecated, instead you should pass the "accuracy" metric to the model at compile time:
    `model.compile(optimizer, loss, metrics=["accuracy"])`
    Can you tell me what modificatione needs to be changed because ultimately the program is not getting fully executed for an error at the following line :
    train_acc=hist.history['acc']

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Hi if you haven't found the answer aldready please use this instead:
      model.compile(loss='binary_crossentropy',
      optimizer = 'adam',
      metrics=["accuracy"])


      hist=model.fit(X_train,Y_train,batch_size=batch_size,epochs=nb_epoch,verbose=1, validation_split=0.2)

      It will work according to the new release :)

      Delete
    3. hii... I am getting same error.

      TypeError: Unrecognized keyword arguments: {'show_accuracy': True}

      Please suggest the changes ...
      because metrics=["accuracy"]) also gives the same error...

      TypeError: Unrecognized keyword arguments: {'metrics': ['accuracy']}

      Delete
  6. Hi Anuj
    Your work is simply brillinat. Such a simple to understand.

    I have able to feed the data (Gray Image Database). But I am unable to feed Gray Image data.
    Can you help me in this matter.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Very Nice presentation ********
    dear how can it be possible that i don't load all of images to array
    means i don't have much memory and i want to do it for RGB image

    ReplyDelete
  9. Thats what every convolution layer is doing. We know that convolution layer extracts features for every image you input. Basically your question is either incomplete or I have interpreted it in a wrong way. I hope it will help.

    ReplyDelete
  10. I have a question, what if I want to increase train data size to help improve accuracy then do I need to manually label the newly added train images? IS there another way to label the image?

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. I keep getting warning message saying:

    Method on_batch_end() is slow compared to the batch update. Check your callbacks.

    Can you help me? Thank you.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Hi Anuj, Where can i get all those images car,bikes,etc.

    ReplyDelete
  15. how can I solve this issue any suggestions?
    UserWarning: The "show_accuracy" argument is deprecated, instead you should pass the "accuracy" metric to the model at compile time:
    `model.compile(optimizer, loss, metrics=["accuracy"])`

    ReplyDelete
  16. i watched all your videos about deep learning. you are doing a great job.thanks for your effort.

    I created a vgg model as like "FEEDING YOUR OWN DATA SET INTO THE CNN MODEL'.
    how we can predict the an image from this model.

    thanks in advance

    ReplyDelete
  17. dense(128) says it exceeds boundaries what to do?

    ReplyDelete
  18. how to test this weights created for label retrieval

    ReplyDelete
  19. I'm getting the following error:
    'Convolution2D' object has no attribute 'get_input'

    output_fn = theano.function([model.layers[0].get_input()], output_layer)

    how can i fix it?

    ReplyDelete
  20. Hi Bro, how can we use different GPU using keras, as i am trying to add few more layer in your code, i got out of memory error, as my primary GPU is 4gb and second one is 11.

    ReplyDelete
  21. Hi,Anuj..this blog is very interesting and it opened my mind how to feed my own image to cnn with keras . I am following all as your explanation and copy all the codes from this blog and also downloaded all your images dataset and place it in my disk so i just change my image data directory with my own path but when i run this code i got error FileNotFoundError: [Errno 2] No such file or directory: '~/Documents/PythonProject/Keras/data. So could you help me please. Thank you.

    ReplyDelete
  22. model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
    show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))

    it gives error: File "", line 1, in
    model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,

    NameError: name 'model' is not defined

    please comment

    ReplyDelete
  23. FileNotFoundError: [Errno 2] No such file or directory: 'input_data\\image_0075.jpg'
    plz fix it

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  24. Hi, Anuj..
    What if the dataset has 3 channels color rgb?
    So, PIL image convert to what?
    Then, how is the flatten matrix in RGB?
    And the input shape in RGB?
    Thanks, your help will be so important :)

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. how to give random input and predict its class

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. im = Image.open(path1 + '\\' + file)
    this line is giving error
    my path1 is /home/tanmay/symbols
    IOError: [Errno 2] No such file or directory: '/home/tanmay/symbols\\Q3.jpg'

    ReplyDelete
  29. Hi! i want to create an image recognition system of potholes. How to create a dataset i have images and how to load for keras. it should predict whether it is a pothole or not.

    ReplyDelete
  30. Activation' object has no attribute 'get_output'

    ReplyDelete
  31. sir i am getting an array not found i am using anaconda 3.5 with spyder 3 kindly suggest

    ReplyDelete
  32. sir,
    i have extracted the features of each and individual layer but my question is, is it possible to visualize the features from extracted features in .mat file ?

    ReplyDelete
  33. Hey Anuj, really good tutorial. But I am trying to work with Functional API as I have to merge two models. Can you please tell me how it will work with Functional API? specially the input part only. Do you have any tutorial lying around which works with Functional API in keras?
    thanks very much.

    ReplyDelete
  34. Hello Anuj, thanks for the great tutorial. I am new to python and Image processing . I would like to understand what does line 45 does. and whats the role of 'f' in it..

    Thanks you

    ReplyDelete
  35. Hello Anuj, many thanks . I found your tuto very helpful for me I am a beginner and I wonder if you can share with us the DataSet in order to understand the whole tuto.

    ReplyDelete
  36. Hi, when I implement your code I receive this error:"ValueError: Error when checking input: expected conv2d_43_input to have 4 dimensions, but got array with shape (646, 128, 128)". what is the problem? how can I solve it?

    ReplyDelete
    Replies
    1. I have the same issue...
      How do you solve this?

      Delete
  37. hi anuj sir
    Can you tell me how to feed my own 1D(signal) to cnn??
    thanks in advance

    ReplyDelete
  38. FileNotFoundError: [Errno 2] No such file or directory: 'input file\\subject01.glasses'

    ReplyDelete
  39. Can you simply helps me for Vgg16 pretrained model for simple classificaton of images in keras step by step i need your help

    ReplyDelete
  40. Hi ,can we tell how to make an object detection model from scratch

    ReplyDelete
  41. HI,can you tell how to train a model for object detection

    ReplyDelete