Updated March 16, 2023
Introduction to Keras Object Detection
Keras object detection is a very important problem in the vision of computers. The model is tasked with the localizing object which was present in the image and at the same time, it will classify into different categories. The keras object classification model is classified into one and two-stage detectors. Object detection two-stage detectors are more accurate but at the cost, time is slower. We can use object detection using retinanet.
Key Takeaways
- It is challenging computer vision tasks which were involving predicting both images and objects detected.
- While using deep learning for the detection of objects will result in models which was highly accurate but we are facing multiple challenges in the training of deep learning.
What is Keras Object Detection?
The convolutional neural network of the mask region, which is based on the model and state-of-the-art, approaches the task of object recognition. Object detection is important in computer vision applications because it allows the computer to see what is inside the image. Intensive computation was present in the entire image for locating the objects.
With the advent of CNN, the detection time of an object has been reduced to a few seconds. Object detection includes image classification and localization of objects, and it is also used to locate the image object that was indicating the location of the bounding box, as well as predict the class of the image object.
How to Use Custom Dataset Keras Object Detection?
Below steps shows how we can use object detection in a custom dataset as follows:
1. In the first step we are installing the detecto module by using the import keyword.
Code:
python -m pip install detecto
Output:
2. After installing the detecto module now in this step we are importing the required packages.
Code:
from detecto import core, utils, visualize
import matplotlib.pyplot as plt
import numpy as np
from detecto.visualize import show_labeled_image, plot_prediction_grid
from torchvision import transforms
Output:
3. After importing the modules now in this step, we are defining the custom image augmentation. Image augmentation is a process to expand the data by creating modified versions of the images.
Code:
CT = transforms.Compose ([
transforms.ToPILImage (),
transforms.Resize (500),
transforms.RandomHorizontalFlip (0.5),
transforms.ColorJitter (saturation = 0.2),
transforms.ToTensor (),
utils.normalize_transform (),
])
Output:
4. After defining the custom image augmentation now in this step we are defining the model training.
Code:
CD = core.Dataset ('keras/', transform = CT)
TD = core.Dataset ('keras/')
ls = model.fit(TD, epochs = 15, lr_step_size = 3, learning_rate = 0.005, verbose = True)
Output:
5. After defining the custom image augmentation now in this step we are plotting the graph.
Code:
plt.plot(TD)
plt.show()
Output:
6. After plotting the model, now in this step we are showing the actual car image which we have loaded.
Code:
model.save ('model_weights.pth')
mod = core.Model.load ('model_weights.pth', ['Wheel', 'Head Light'])
img = utils.read_image (car.jpg')
show_labeled_image(img, boxes, labels)
Output:
Image source: https://blog.gaadikey.com/
Keras Object Detection SSD
The SSD is a CNN based object detection framework. It will combine the predictions from the different resolutions for handling the object in multiple sizes. We can say that it will run in real time because the network will not resample features or pixels. To define the object detection with SSD in the first step we need to import the tensorflow, keras, OpenCV, and numpy models.
Code:
from detecto import core, utils, visualize
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
Output:
After importing the module now in this step we are testing the image, also we are defining the model and image size.
Code:
img_path = 'car.jpg'
mod_name = 'VGG16'
t_path = 'final.h5'
img_size = (300,300)
Output:
After testing the image now in this step we are training and evaluating the dataset.
Code:
dset_name = 'VOC07'
v_path = 'VOCdevkit'
Output:
After training and evaluating the dataset now in this step we are defining the SSD.
Code:
image_size = (300,300)
batch_size = 32
base_lr = 0.001
total = mod_name * tf.cast(batch_size, tf.float32)
Output:
Object Detection Project
While developing the project into the image bounding box we need to extract the bounding box and need to put it into the corresponding folder class. The below code shows how we can implement the project. To implement the object detection project we have to import pandas, os, numpy, cv2, keras, and tensorflow libraries.
Code:
from keras.preprocessing import image
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from keras.utils import to_categorical
import pandas as pd
import os
import numpy as np
import cv2
from skimage.segmentation import mark_boundaries
def get_image_value(path, dim):
'''Keras object detection project'''
keras_img = image.load_img(path, target_size = dim)
keras_img = image.img_to_array(keras_img)
return keras_img/255
def get_img_array(img_paths, dim):
'''Keras object detection project'''
f_array = []
from tqdm import tqdm
for path in tqdm(img_paths):
keras_img = get_image_value(path, dim)
f_array.append(keras_img)
f_array = np.array(f_array)
return f_array
def get_tts():
'''Keras object detection project'''
DIM = (150,150)
np.random.seed(10)
p_path = [f'keras{i}' for i in os.listdir('keras')]
p_lab = [1 for i in range(len(p_path))]
r_path = [f'keras{i}' for i in os.listdir('keras')]
r_lab = [2 for i in range(len(r_path))]
neg_paths = [f'keras{i}' for i in os.listdir('keras/')]
np.random.shuffle(neg_paths)
neg_paths = neg_paths[:len(p_path)- 500]
neg_labels = [0 for i in range(len(neg_paths))]
np.random.shuffle(p_path)
p_path = p_path[:len(r_path)+150]
neg_paths = neg_paths[:len(r_path)+150]
p_lab = [1 for i in range(len(p_path))]
r_lab = [2 for i in range(len(r_path))]
neg_labels = [0 for i in range(len(neg_paths))]
paths = p_path + r_path + neg_paths
labels = p_lab + r_lab + neg_labels
x_train, x_test, y_train, y_test = train_test_split(paths, labels, stratify = labels, train_size = .90, random_state = 10)
new_x_train = get_img_array(x_train, DIM)
new_x_test = get_img_array(x_test, DIM)
print('Count val')
print(pd.Series(y_train).value_counts())
y_train = np.array(y_train)
y_test = np.array(y_test)
y_test = to_categorical(y_test)
y_train = to_categorical(y_train)
tts = (new_x_train, new_x_test, y_train, y_test)
return tts
Output:
Keras Object Detection Implementation
The object detection implementation is an important task in computer vision. The deep learning models will tune the hyperparameters in an easier way.
Below example shows how we can implement object detection as follows:
Code:
from detecto import core, utils, visualize
import matplotlib.pyplot as plt
import numpy as np
from detecto.visualize import show_labeled_image, plot_prediction_grid
from torchvision import transforms
CT = transforms.Compose ([
transforms.ToPILImage (),
transforms.Resize (500),
transforms.RandomHorizontalFlip (0.5),
transforms.ColorJitter (saturation = 0.2),
transforms.ToTensor (),
utils.normalize_transform (),
])
CD = core.Dataset ('keras', transform = CT)
TD = core.Dataset('keras')
model.save('model_weights.pth')
mod = core.Model.load('model_weights.pth', ['Wheel', 'Head Light'])
img = utils.read_image('car.jpg')
show_labeled_image(img, boxes, labels
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of keras object detection?
Answer: Object detection is used in computer vision. Object detection is basically used to classify objects into different categories.
Q2. Which libraries do we need to use at the time of using keras object detection?
Answer: We need to use the keras, tensorflow, numpy, os, and pandas libraries at the time of using it.
Q3. What is custom object detection in keras?
Answer: At the time of developing the project by using custom object detection we are using OpenCV segmentation of selective search.
Conclusion
Intensive computation was present in the entire image for locating the objects. It is a very important problem in the vision of the computer, the model is tasked with the localizing object which was present in the image, and at the same time, it will be classified into different categories.
Recommended Articles
This is a guide to Keras Object Detection. Here we discuss the introduction, and how to use custom dataset keras object detection. and implementation. You may also have a look at the following articles to learn more –