Today, I am going to discuss about image recognition in python using some of the python basic libraries. We will do image recognition on Fashion MNIST dataset which is quite popular. Let’s proceed.
Now, first we will talk about packages which we are going to use in our image recognizer. these are some libraries which are going to be helpful.
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import cv2
Now, here we need to import dataset from keras of Fashion MNIST and split it to training and testing sets by following piece of code
(train_images,train_labels),(test_images,test_labels) = keras.datasets.fashion_mnist.load_data()
Now, we need to train our model on the dataset using neural nets. Let’s first define the model and then compile it and finally we need to fit it on our testing data.
model = keras.Sequential([ keras.layers.Flatten(input_shape=(28,28)), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax) ]) model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy']) model.fit(train_images, train_labels, epochs=1)
After these steps, we have to predict values for every data point. In my model, I have taken the image input from user and resized it, then model predict the value for that image and gives the probability of each type from given 10 types of object.
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
In this way, your guys can proceed further and can improve your model and can give predictions for new images also using OpenCV library.
So, finally I am concluding here guys and see you soon with the new blog and we will be talking about some more exciting concept regarding Artificial Intelligence. You can see live working of my model by clicking on this link.
These are some sample images which i have taken for some testing.
For these set of images, output which we came across is also linked with this blog.
Every set contains two images, first one is the image drawn by model and second one is stats which is showing probability or surety about any prediction.