Ticker

6/recent/ticker-posts

Image Recognition Using Tensorflow and Keras


 In this post, we are gonna make a machine learning program to identify cat and dog using tensorflow and keras. Firstly we have to train the model with several images of cats and dogs, so that our program would identify some key features of cats and dogs and would be able to differentiate between them.

 In this image classification program, we are gonna use libraries like tensorflow, keras, sklearn, numpy, pandas and matplotib to display the confusion matrix for displaying the predicted result of our program. You can install all these libraries in command prompt and use any code editor supporting python written programmes.

You can download the cats and dogs dataset from this link  :  dataset


Preview:

  import matplotlib.pyplot as plt
  from sklearn.metrics import confusion_matrix
  import numpy as np
  from numpy import random
  import sklearn 
  from sklearn.model_selection import train_test_split 
  from sklearn.utils import shuffle
  from sklearn.preprocessing import MinMaxScaler
  import tensorflow as tf
  from tensorflow import keras
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.optimizers import Adam
  from tensorflow.keras.layers import Dense, Activation, Flatten, BatchNormalization, Conv2D, MaxPool2D
  from tensorflow.keras.preprocessing.image import ImageDataGenerator
  from tensorflow.keras.metrics import categorical_crossentropy
  import itertools
  import os
  import glob
  import shutil
  import random




  # organise Data into train, valid and test data

  os.chdir("C:\data\cats_vs_dogs")
  if os.path.isdir("train/dog") is False:
    os.makedirs("train/dog")
    os.makedirs("train/cat")
    os.makedirs("valid/dog")
    os.makedirs("valid/cat")
    os.makedirs("test/dog")
    os.makedirs("test/cat")

  # set the train, valid and test path
  train_path = "C:/data/cats_vs_dogs/train"
  valid_path = "C:/data/cats_vs_dogs/valid"
  test_path = "C:/data/cats_vs_dogs/test"

  # make the train, valid and test batches giving image size and classes

  train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
     .flow_from_directory(directory=train_path, target_size=(224,224), classes=["cat","dog"], batch_size=10)
  valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
     .flow_from_directory(directory=valid_path, target_size=(224,224), classes=["cat","dog"], batch_size=10)
  test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
     .flow_from_directory(directory=test_path, target_size=(224,224), classes=["cat","dog"], batch_size=10,        shuffle=False)

  # in this we have given the classes and batch size and in case of test data, we have shuffle=false as
  # by shuffling the test images, our labels wont be shuffled so we have uniform test data


  # now we are taking images and labels from training data
  imgs, labels = next(train_batches)

  # this functon will plot images from row 1 with 1 images as columns, this function is copied from tensorflow.org
  def plotImages(images_arr):
      fig, axes = plt.subplots(1, 10, figsize= (20,20))
      axes= axes.flatten()
      for img, ax in zip(images_arr, axes):
           ax.imshow(img)
           ax.axis("off")
      plt.tight_layout()
      plt.show()

  # create the model
  model = Sequential([
         Conv2D(filters=32, kernel_size=(3,3), activation="relu", padding="same", input_shape=(224,224,3)),
         MaxPool2D(pool_size=(2,2), strides=2),
         Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"),
         MaxPool2D(pool_size=(2,2), strides=2),
         Flatten(),
         Dense(units=2, activation="softmax")
  ])

  model.compile(optimizer=Adam(learning_rate=0.0001), loss="categorical_crossentropy", metrics=["accuracy"])

  model.fit(x=train_batches, validation_data=valid_batches, epochs=1, verbose=2)

  # now predict the values and make a confusion matrix

  test_imgs, test_labels = next(test_batches)

  prediction = model.predict(x= test_batches, verbose=0)
  rounded_prediction = np.argmax(prediction, axis=-1)


  # make the confusion matrix and compare the actual and predicted value
  cm = confusion_matrix(y_true=test_batches.classes, y_pred=rounded_prediction)

  # this is the confusion matrix function from tensorflow website
  # we use confusion matrix to compare our actual values and our model's predictions

  def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

    # execute confusion matrix

  cm_plot_labels=["cat", "dog"]
  plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title="confusion matrix")
  plt.show()


Post a Comment

0 Comments