Image Classification Using Tensorflow

This Story is about implementing image classification on Fashion MNIST datasets using TensorFlow and Keras.


image classification using tensorflow mnist digitsimage classification using tensorflow mnist digits


We will import the require a library. Then, download the dataset with the help of the TensorFlow-Keras API.

#code: 
import tensorflow as tffrom tensorflow import kerasfashion_mnist = keras.datasets.fashion_mnist(train_Images, train_Labels), (test_Images, test_Labels) = fashion_mnist.load_data()


In the next step, we describe all required class names for fashion-mnist.

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

MNIST provides 60000 train images and 1000 test images and the size of images is 28 * 28. we can check here and it is optionally.

#code: 
train_Images.shape

Output: (60000, 28, 28)
Before processed ahead we can check image here and you will see that the pixel values fall in the range of 0 to 255.

#code: 
import matplotlib.pyplot as pltplt.figure()plt.imshow(train_Images[0])plt.grid(False)plt.show()

Convert your image on a scale of 0 to 1 before parsing on a convolution neural network.

#code: 
train_Images = train_Images/255test_Images = test_Images/255

Reshape your image dataset which will be adjusted in the convolution layer.

#code: 
train_Images = np.array(train_Images).reshape(-1, 28, 28, 1)test_Images = np.array(test_Images).reshape(-1, 28, 28, 1)

Import the require library and create a model and define layer and argument.

#code: 
from tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flattenfrom tensorflow.keras.layers import Conv2D, MaxPooling2Dmodel = Sequential()model.add(Conv2D(32, kernel_size=(3,3), input_shape=(28, 28, 1)))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Flatten())model.add(Dense(100, activation=tf.nn.relu))model.add(Dropout(0.2))model.add(Dense(10,activation=tf.nn.softmax))

Compile your model here with help of model.compile() and this method requires 3 arguments.

#code: 
model.compile(optimizer=’adam’,loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’])

You can start training here with the help of the model.fit() method.

#code: 
model.fit(train_Images, train_Labels, epochs=10)

Accuracy of model is also important and here we evaluate model here with three argument train images, train labels and verbose and you can also check test accuracy with the help of test images.

#code: 
train_Loss, train_Acc = model.evaluate(train_Images,  train_Labels, verbose=2)print('\nTest accuracy:', train_Acc)model.save("MNIST-FASHIONDATASET.model")

And Finally, you can save your model and test it with the real world.
You can follow on Linkedin, Github, and Twitter.