Image Classification with Artificial Neural Network(ANN)

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib.pyplot as plt

#Loading Fashion MNIST dataset and when load_data() is invoking then this dataset is loading and it is returning 4 numpy arrays
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
40960/29515 [=========================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
26435584/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
16384/5148 [===============================================================================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
4431872/4422102 [==============================] - 0s 0us/step

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

train_images.shape
(60000, 28, 28)

len(train_labels)
60000

train_labels
array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)

test_images.shape
(10000, 28, 28)

len(test_labels)
10000

print(test_labels)
[9 2 1 ... 8 1 5]

#To display an image
plt.figure()
plt.imshow(test_images[5])
plt.colorbar()
#plt.grid(True)
plt.show()

img
#Scale train_images and test_images
train_images = train_images / 255.0

test_images = test_images / 255.0
img

plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()

img
#Create ANN model.
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)])
img
#Compile the model
model.compile(optimizer='adam',

loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
img
#Trained the model
model.fit(train_images, train_labels, epochs=10)

Epoch 1/10
1875/1875 [==============================] - 5s 2ms/step - loss: 0.4992 - accuracy: 0.8239
Epoch 2/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3725 - accuracy: 0.8650
Epoch 3/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3347 - accuracy: 0.8763
Epoch 4/10
1875/1875 [==============================] - 5s 2ms/step - loss: 0.3121 - accuracy: 0.8853
Epoch 5/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2914 - accuracy: 0.8925
Epoch 6/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.2787 - accuracy: 0.8965
Epoch 7/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.2656 - accuracy: 0.9001
Epoch 8/10
1875/1875 [==============================] - 5s 2ms/step - loss: 0.2552 - accuracy: 0.9052
Epoch 9/10
1875/1875 [==============================] - 5s 2ms/step - loss: 0.2454 - accuracy: 0.9076
Epoch 10/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2376 - accuracy: 0.9108
<keras.callbacks.History at 0x7f0a43a9bdd0>

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)

print('\nTest accuracy:', test_acc)

    313/313 - 1s - loss: 0.3335 - accuracy: 0.8838 - 503ms/epoch - 2ms/step

    Test accuracy: 0.8838000297546387

probability_model = tf.keras.Sequential([model,
                                         tf.keras.layers.Softmax()])

y_prediction = probability_model.predict(test_images)
y_prediction

    array([[9.1632967e-07, 1.3603168e-08, 4.2180295e-09, ..., 1.5751410e-02,
        1.0110255e-07, 9.4907498e-01],
        [1.2820651e-06, 1.9898266e-13, 9.9843818e-01, ..., 2.0250637e-16,
        1.8951423e-09, 9.0496644e-18],
        [6.8526184e-08, 9.9999988e-01, 2.7750072e-10, ..., 2.1147807e-22,
        2.6917511e-12, 1.7522404e-20],
        ...,
        [9.1290085e-06, 2.1243448e-12, 7.7862474e-05, ..., 4.3482315e-10,
        9.9982649e-01, 2.4573460e-12],
        [7.0186230e-05, 9.9967217e-01, 4.4651040e-07, ..., 9.5057473e-14,

        1.3335298e-06, 1.3605530e-09],
        [7.2471870e-05, 1.0830753e-08, 7.8908706e-06, ..., 3.0554649e-03,
        1.1909327e-03, 9.4211731e-07]], dtype=float32)

y_prediction[11]

    array([1.7185373e-06, 1.8919807e-11, 2.2544960e-08, 1.6086881e-12,
        2.3156841e-07, 9.9989259e-01, 1.7224195e-06, 1.0066609e-04,
        1.0256390e-07, 2.8413447e-06], dtype=float32)

np.argmax(y_prediction[11])
    5

test_labels[11]
    5

def plot_image(i, predictions_array, true_label, img):
    true_label, img = true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])

    plt.imshow(img, cmap=plt.cm.binary)

    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'

    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
    true_label = true_label[i]
    plt.grid(False)
    plt.xticks(range(10))
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1])
    predicted_label = np.argmax(predictions_array)

    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')

i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], test_labels)
plt.show()
img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext