Building Image Classifier using Keras and TensorFlow
By: Vishnu Satheesh
Tags: TensorFlow, Image Classification, Deep Learning, Keras, Neural Networks

One of the simplest ways to get started with TensorFlow, a machine learning library by Google is to build an image classifier from scratch. TensorFlow is pre-dominantly used for neural networks and it is a open sourced library. Since tensorflow is a low level library it provides more flexibility to develop own functionalities and services for the users especially developers. And also, they support a good number of programming languages including Python. The recent update of TensorFlow to TensorFlow 2.x has made a lot of difference especially in its integration Keras which is a high-level deep learning API that helps to easily build, train, validate and execute neural networks. Done with the introduction. Let’s go build our Image Classifier :)

Photo by Dan Gold on Unsplash

Importing Libraries

I am working on the Google Colab and the following are the codes to import tensorflow and keras libraries.

import tensorflow as tf

from tensorflow import keras

tf.__version__

Loading Datasets

Keras has some utility function to get common datasets. This includes the MNIST, California dataset etc. For our purpose, we will load the Fashion MNIST data. We will be splitting the data into train and test set.

fashion_mnist=keras.datasets.fashion_mnist

(train_X,train_Y),(test_X,test_Y)=fashion_mnist.load_data()

print("train_X has shape of: ",train_X.shape)

print("train_Y has shape of: ",train_Y.shape)

print("test_X has shape of: ",train_X.shape)

print("test_Y has shape of: ",train_Y.shape)

Output:

train_X has shape of: (60000, 28, 28)

train_Y has shape of: (60000,)

test_X has shape of: (60000, 28, 28)

test_Y has shape of: (60000,)

Now we have to scale down the pixel intensities to 0–1 range by dividing them by 255. By normalizing our inputs to a standard scale, we’re allowing the network to more quickly learn the optimal parameters for each input node. Moreover, if your inputs and target outputs are on a completely different scale then the default parameters for your neural network (eg. learning rates) will likely be improper for your data. Also we will take some data from the train data to the validation set.

// taking first 5000 records for validation set

valid_X,train_X=train_X[:5000]/255.0,train_X[5000:]/255.0

valid_Y,train_Y=train_Y[:5000]/255.0,train_Y[5000:]/255.0

Sample of the images in the dataset:

class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress","Coat",

"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]

n_rows = 4

n_cols = 10

plt.figure(figsize=(n_cols * 1.2, n_rows * 1.2))

for row in range(n_rows):

for col in range(n_cols):

index = n_cols * row + col

plt.subplot(n_rows, n_cols, index + 1)

plt.imshow(X_train[index], cmap="binary", interpolation="nearest")

plt.axis('off')

plt.title(class_names[y_train[index]], fontsize=12)

plt.subplots_adjust(wspace=0.2, hspace=0.5)

plt.show()

Sequential API

We will build a neural network with two hidden layers. One hidden layer with 300 neurons and other with 100 neurons. The neural network is created using layers of neurons connected sequentially. So the Sequential API helps in connecting the layers of neurons together.

model=keras.models.Sequential()

You might have noticed the dimension of the input data while loading the data. (60000, 28, 28) means that there are 60000 images with dimension 28 x 28. So our next step is to flatten the data we have. The input data is then passed through a Flatten layer than converts the image into 1D array.

model.add(keras.layers.Flatten(input_shape=[28,28]))

Now we will create the hidden layers and intialize their respective activation functions.

An activation function is a function that is added into an artificial neural network in order to help the network learn complex patterns in the data. We use softmax at the last layer since this is a multi-label classiication.

model.add(keras.layers.Dense(300,activation=”relu”))

model.add(keras.layers.Dense(100,activation=”relu”))

model.add(keras.layers.Dense(10,activation=”softmax”))

The above is a good article if you want to know more about activation functions.

model.summary()

Each node in a hidden layer will have a weights and bias associated with it. When the 784 input connects to the each nodes in the hidden layer it creates 784 * 300+ 300 = 235500 parameters for dense layer. Similarly the denser layer will have 300*100+100=30100 parameters associated with it. The dense_2 layer will have 100*10+10=1010 parameters.

Model’s list of layers can be accessed by

model.layers

//assigning the values of weights and bias of first hidden layers

hidden1=model.layers[1]

weights,biases=hidden1.get_weights()

Compile the Model

Now we need to specify the loss function and optimizer we will be using. Below is the documentation of TensorFlow for the losses associated.

Since the output is multi-class we will be using sparse categorical cross entropy.

The type of entropies available in TensorFlow are provided in the documentation below.

Now the model created, lets define the loss function and optimizers to be used.

model.compile(loss=”sparse_categorical_crossentropy”,optimizer=”sgd”,metrics=[“accuracy”])

Training and Evaluating the Model

We will fit and the model and the evaluate the results against test data. We are initializing 30 epochs for the training. Epochs indicates the number of passes of the entire training dataset the machine learning algorithm has completed.

history=model.fit(train_X, train_Y,epochs=30,validation_data=(valid_X,valid_Y))

model.evaluate(test_X,test_Y)

X_new = X_test[:3]

y_proba = model.predict(X_new)

y_proba.round(2)

y_pred = model.predict_classes(X_new)

y_pred

np.array(class_names)[y_pred]

y_new = y_test[:3]

y_new

Model evaluation gives us an accuracy of 87% which is decent enough. Now we will try to see the output thus generated with the images in the dataset.

for index, image in enumerate(X_new):

plt.subplot(1, 3, index + 1)

plt.imshow(image, cmap="binary", interpolation="nearest")

plt.axis('off')

plt.title(class_names[y_test[index]], fontsize=12)

plt.subplots_adjust(wspace=0.2, hspace=0.5)

plt.show()

The image classifier built looks decently well enough for the dataset. This is a very simple implementation of neural networks in image classification. Hope you got some insights from the article. Follow for more :)