Image Recognition in Python using Machine Learning
Image Recognition is the process of providing a category to the image. We have to train our machine and build a model that can recognize the image for this purpose we use Keras and Tensorflow.
Image Recognition using Keras and TensorFlow
The objective of image recognition is to get the label or category of the input image by the neural network.
Creating the Basic Image Recognition Model:
- Importing Necessary Modules
import keras import tensorflow as tf import matplotlib.pyplot as plt import random import numpy as np import pydot
- Loading Dataset
Loads the Fashion-MNIST dataset.
Dataset : 60,000 28×28 grayscale images
Categories: 10
Test set images: 10,000
Label Description :
0- T-shirt/top
1- Trouser
2 -Pullover
3 -Dress
4 -Coat
5 -Sandal
6- Shirt
7 -Sneaker
8 -Bag
9- Ankle boot
fashion=keras.datasets.fashion_mnist (x_train,y_train),(x_test,y_test)=fashion.load_data()
- Providing Labels to the values(0,1,2….)
class_names=['T-shirt/Top','Trouser','Pullover','Dress','Coat', 'Sandal','Shirt','Sneaker','Bag','Ankle boot']
- Normalization of the data: Performing normalization to get the values in a confined range.
x_train_n=x_train/255. x_test_n=x_test/255.
- Splitting dataset into validation/train/test: We have taken 5000 rows for validation and the remaining for training.
x_valid,x_training=x_train_n[:5000],x_train_n[5000:] y_valid,y_training=y_train[:5000],y_train[5000:] x_test=x_test_n
np.random.seed(42) tf.random.set_random_seed(42)
Output:
1.28*28 pixel (convert in 1-d) 2.Input layer 3.Hidden layer 1 4.Hidden layer 2 5.Output layer 6.10 categories
- Training Model: We are training the model using Keras and we are building a sequential model having a dense layer with 300 neurons and relu activation function and an output layer with 10 categories..
model=keras.models.Sequential() #model object model.add(keras.layers.Flatten(input_shape=[28,28])) #input layer #dense layer with 300 neurons and relu activation function model.add(keras.layers.Dense(300,activation="relu")) model.add(keras.layers.Dense(100,activation="relu")) #output layer with 10 categories model.add(keras.layers.Dense(10,activation="softmax")) model.summary()
Output:
Model: "sequential_2" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten_2 (Flatten) (None, 784) 0 _________________________________________________________________ dense_4 (Dense) (None, 300) 235500 _________________________________________________________________ dense_5 (Dense) (None, 100) 30100 _________________________________________________________________ dense_6 (Dense) (None, 10) 1010 ================================================================= Total params: 266,620 Trainable params: 266,610 Non-trainable params: 0 ________________________________________________________________
- Compiling the model
model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"]) #sochastic gradient design model_history=model.fit(x_training,y_training,epochs=30, validation_data=(x_valid,y_valid))
Output:
Train on 55000 samples, validate on 5000 samples Epoch 1/30 55000/55000 [==============================] - 8s 145us/step - loss: 0.7326 - accuracy: 0.7609 - val_loss: 0.4999 - val_accuracy: 0.8366 Epoch 2/30 55000/55000 [==============================] - 6s 109us/step - loss: 0.4890 - accuracy: 0.8294 - val_loss: 0.4330 - val_accuracy: 0.8526 Epoch 3/30 55000/55000 [==============================] - 7s 128us/step - loss: 0.4419 - accuracy: 0.8457 - val_loss: 0.4077 - val_accuracy: 0.8602 Epoch 4/30 55000/55000 [==============================] - 7s 136us/step - loss: 0.4158 - accuracy: 0.8534 - val_loss: 0.4049 - val_accuracy: 0.8612 Epoch 5/30 55000/55000 [==============================] - 8s 145us/step - loss: 0.3949 - accuracy: 0.8621 - val_loss: 0.3932 - val_accuracy: 0.8646 Epoch 6/30 55000/55000 [==============================] - 11s 192us/step - loss: 0.3802 - accuracy: 0.8658 - val_loss: 0.3882 - val_accuracy: 0.8670 Epoch 7/30 55000/55000 [==============================] - 13s 233us/step - loss: 0.3664 - accuracy: 0.8695 - val_loss: 0.3616 - val_accuracy: 0.8726 Epoch 8/30 55000/55000 [==============================] - 11s 206us/step - loss: 0.3550 - accuracy: 0.8742 - val_loss: 0.3754 - val_accuracy: 0.8622 Epoch 9/30 55000/55000 [==============================] - 11s 197us/step - loss: 0.3452 - accuracy: 0.8776 - val_loss: 0.3569 - val_accuracy: 0.8770 Epoch 10/30 55000/55000 [==============================] - 13s 244us/step - loss: 0.3364 - accuracy: 0.8804 - val_loss: 0.3498 - val_accuracy: 0.8740 Epoch 11/30 55000/55000 [==============================] - 9s 162us/step - loss: 0.3275 - accuracy: 0.8826 - val_loss: 0.3582 - val_accuracy: 0.8746 Epoch 12/30 55000/55000 [==============================] - 11s 195us/step - loss: 0.3198 - accuracy: 0.8867 - val_loss: 0.3473 - val_accuracy: 0.8756 Epoch 13/30 27104/55000 [=============>................] - ETA: 4s - loss: 0.3112 - accuracy: 0.8878
model_history.params
Output:
{'batch_size': 32, 'epochs': 30, 'steps': None, 'samples': 55000, 'verbose': 1, 'do_validation': True, 'metrics': ['loss', 'accuracy', 'val_loss', 'val_accuracy']}
- Evaluation: Evaluating the accuracy of the model.
model.evaluate(x_test,y_test)
Output:
- Testing
y_proba=model.predict(x_new) y_proba.round(2)
Output:
array([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.01, 0. , 0.99], [0. , 0. , 0.97, 0. , 0.03, 0. , 0. , 0. , 0. , 0. ], [0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]], dtype=float32)
y_pred=model.predict_classes(x_new) y_pred np.array(class_names)[y_pred]
Output:
array([9, 2, 1], dtype=int64) array(['Ankle boot', 'Pullover', 'Trouser'], dtype='<U11')
To check the testing image print image.
print(plt.imshow(x_new[0]))
We are done with our basic training and testing part of the model, you can add GUI to this. We can change the number of units in the hidden layer, the optimizer, the number of epochs of training, the size of batches, and analyze the change in the accuracy of the model.
Leave a Reply