Artificial Neural Networks with Machine Learning

Classify Clothes using Python and Artificial Neural Networks

Artificial neural networks are one of the main tools used in machine learning. As the “neural” part of their name suggests, they are brain-inspired systems which are intended to replicate the way that we humans learn.

Neural networks consist of input and output layers, as well as (in most cases) a hidden layer consisting of units that transform the input into something that the output layer can use.

They are excellent tools for finding patterns which are far too complex or numerous for a human programmer to extract and teach the machine to recognize.

This Machine Learning Project Classifies Clothes from the Fashion MNIST Data set using Artificial Neural Networks and Python.

Let’s start by importing the libraries we need for this task

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

To load the data set

fashion = keras.datasets.fashion_mnist
(trainImages, trainLabels), (testImages, testLabels) = fashion.load_data()
imgIndex = 0
img = trainImages[imgIndex]
print("Image Label :",trainLabels[imgIndex])
Image Label : 9
<matplotlib.image.AxesImage at 0x7f1111a06d68>

To print the shape of the training and testing data

(60000, 28, 28)
(10000, 28, 28)

Now let’s create a Neural Network

model = keras.Sequential([
                          keras.layers.Dense(128, activation=tf.nn.relu),
                          keras.layers.Dense(10, activation=tf.nn.softmax)

To Compile the Model

model.compile(optimizer = 'adam',
           loss = 'sparse_categorical_crossentropy',

To Train the model, trainLabels, epochs=5, batch_size=32)
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 3.6150 - accuracy: 0.6802
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.7296 - accuracy: 0.7488
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.6374 - accuracy: 0.7725
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.5873 - accuracy: 0.7906
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.5579 - accuracy: 0.7993
<tensorflow.python.keras.callbacks.History at 0x7f1108dc3588>

To Evaluate the Model

model.evaluate(testImages, testLabels)
313/313 [==============================] - 0s 1ms/step - loss: 0.5916 - accuracy: 0.7981
[0.5915989279747009, 0.7980999946594238]

To Make a Prediction

predictions = model.predict(testImages[0:5])

# Print the predicted labels
[[1.74235439e-07 2.69071290e-08 6.66509115e-20 3.09463957e-07
  1.11526007e-20 1.34603798e-01 8.10060641e-08 7.74199590e-02
  3.87958280e-05 7.87936807e-01]
 [2.89689321e-02 1.06601091e-02 6.28736615e-01 2.77338717e-02
  1.61624148e-01 1.49910515e-02 8.56256112e-02 1.23378839e-02
  2.35275514e-02 5.79410419e-03]
 [6.75366528e-06 9.99993205e-01 4.27281517e-12 2.68350314e-10
  8.65088672e-16 1.05001736e-14 1.33745196e-12 0.00000000e+00
  1.84386378e-11 0.00000000e+00]
 [6.56618613e-06 9.99993443e-01 1.46741508e-11 1.80866895e-08
  7.95811239e-14 1.56570215e-16 5.96713607e-12 0.00000000e+00
  3.94146077e-10 0.00000000e+00]
 [2.19924763e-01 1.00887669e-02 1.99720263e-01 6.23517819e-02
  4.97664846e-02 3.40277069e-07 4.30076748e-01 7.25772731e-09
  2.80708820e-02 2.27675168e-09]]

To print the maximum labels

print(np.argmax(predictions, axis=1))
# Print the actual label values
[9 2 1 1 6]
[9 2 1 1 6]

To Print the first 5 images

for i in range(0,5):
  plt.imshow(testImages[i], cmap='gray')

Follow us on Instagram for all your Queries

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply