Image Classification with Artificial Neural Network(ANN)

# import TensorFlow and keras API
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
2.8.2
#import numpy and matplotlib
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
fashion_data = keras.datasets.fashion_mnist
(X_train, y_train), (X_test, y_test) = fashion_data.load_data()
list1 = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
X_train.shape
(60000, 28, 28)
X_test.shape
(10000, 28, 28)
len(y_train)
60000
len(y_test)
10000

#To display an image
plt.figure()
plt.imshow(X_test[3])
plt.show()

img
#Scale or normalize train_images(X_train) and test_images(X_test)
X_train = X_train / 255.0
X_test = X_test / 255.0

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(X_train[i], cmap=plt.cm.binary)
plt.xlabel(list1[y_test[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)])

#Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])



#Trained the model
model.fit(train_images, train_labels, epochs=10)

#Trained the model
model.fit(X_train, X_test, epochs=10)
Epoch 1/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.5006 - accuracy: 0.8242
Epoch 2/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.3751 - accuracy: 0.8654
Epoch 3/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.3351 - accuracy: 0.8784
Epoch 4/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3125 - accuracy: 0.8864
Epoch 5/10
1875/1875 [==============================] - 5s 2ms/step - loss: 0.2958 - accuracy: 0.8904
Epoch 6/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2794 - accuracy: 0.8972
Epoch 7/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2680 - accuracy: 0.9000
Epoch 8/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2562 - accuracy: 0.9042
Epoch 9/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2479 - accuracy: 0.9076
Epoch 10/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2385 - accuracy: 0.9101


test_loss, test_acc = model.evaluate(y_train, y_test, verbose=2)

print('\nTest accuracy:', test_acc)

    313/313 - 1s - loss: 0.3417 - accuracy: 0.8838 - 513ms/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([[1.57365072e-08, 1.50676221e-10, 5.14108137e-07, ...,
        5.30719152e-03, 1.54653222e-07, 9.85224128e-01],
        [2.37710319e-05, 2.77888362e-13, 9.98255670e-01, ...,
        1.03614722e-21, 4.83171038e-11, 4.48418003e-20],
        [2.33083298e-07, 9.99999762e-01, 1.19365314e-12, ...,
         1.09194450e-28, 6.78241778e-14, 1.80723813e-24],

        ...,
        [6.42917075e-06, 1.04333625e-11, 1.23493805e-07, ...,
         6.19666402e-11, 9.99984860e-01, 2.22474157e-15],
        [5.79630978e-06, 9.99795377e-01, 3.28447349e-08, ...,
         1.08522259e-14, 4.80095741e-10, 5.09045930e-12],
        [3.08760536e-06, 4.15983603e-09, 2.75665047e-06, ...,
         7.17349816e-04, 4.99409398e-05, 9.72766088e-07]], dtype=float32)

y_prediction[7]

    array([1.7200844e-05, 1.3468861e-10, 6.4119446e-04, 1.1564042e-04,
          1.7157765e-02, 7.5309385e-09, 9.8206800e-01, 3.9398278e-13,
          1.3642875e-07, 5.6909447e-12], dtype=float32)

np.argmax(y_prediction[7])
    6

test_labels[7]
    6

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