Training and Deployment code for an object detection model to recognize cat and dog in an image
An Object Detection Application using YOLOv8 (PyTorch and Django Implementation).
Django implementation for Webpage as well as REST API
A web application that provides object detection using YOLOv8 and also generates REST API. It's implemented using Django framework and PyTorch (for YOLO model).
I have developed Django API which accepts an image as request. The input image is converted to float32 type NumPy array and passed on to the YOLOv8 object detection model. The model performs object detection for the image and generates a JSON object with predictions of all the cats and dogs. The Response of the API is the JSON object.
The libraries required along with thier version are mentioned below:
-
Python (3.10)
-
Django (4.2)
-
ultralytics (8.0.78)
- whitenoise (6.4.0)
For object detection using Pre-Trained model, we need one important files :
- best.pt - The weights file for the trained model. The file is provided in the GitHub repo.
This repository can do two things:
-
Implementation web-application
-
Generation of REST API (API testing is done using POSTMAN)
-
Clone the GitHub repository
-
Change the directory to the cloned Repository folder.
-
Install all the required libraries from the requirement.txt.
-
Change directory to the catdog_detection api
-
Execute the code below: (This command needs to be executed only once)
-
python manage.py collectstatic
-
This command initiates Django and collects all the static files.
-
6- Then, Execute:
-
python manage.py runserver
-
This command starts Django server. Now we are all set to run the application.
7- Click on the link. This will direct you to the web browser.
8- Select an image via Drag-&-Drop or Browse mode. and Click on ”Detect Object”
9- Input to Django Web-app is an image. This input image is converted to float32 type NumPy array and passed on to the YOLOv8 model. The model performs object detection for the image and generates a JSON object with names of all the objects and their respective bounding boxes in the image.
10- The Form Response is the JSON object. This JSON object is displayed as a result.
11- Now, click on “Show Predictions” to see the image with Bounding Boxes.
12- To try on other images, click on “Choose a New File”.
Postman is a scalable API testing tool. The steps to be followed are:
-
Follow the first 6 steps as mentioned above.
-
Make sure the server runs properly
-
Open POSTMAN and select POST option. Enter the server link and append /detect/api_request/ to it.
- For Example : 127.0.0.1:8000/detect/api_request/
4- Click on Body. Enter key value as “Image”. Choose the image file and click on “SEND”.
- The input image is converted to float32 type NumPy array and passed on to the YOLOv8 model. The model performs object detection for the image and generates a JSON object with names of all the objects and their respective bounding boxes in the image.
- The HttpResponse is the JSON object. This JSON object is returned.
For training the model we will use roboflow datasets. The datasets used for training are provided as a zip file in the Training folder. A python script is also provided to carry out the training by hand. During training the mAP50-95 metric was used to gain an understanding of the best model. The final is as follows:
Ultralytics YOLOv8.0.78 🚀 Python-3.9.16 torch-2.0.0+cu118 CUDA:0 (Tesla T4, 15102MiB)
Model summary (fused): 168 layers, 11126358 parameters, 0 gradients, 28.4 GFLOPs
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 6/6 [00:07<00:00, 1.31s/it]
all 163 294 0.755 0.757 0.797 0.439
cat 163 153 0.805 0.732 0.786 0.456
dog 163 141 0.705 0.781 0.808 0.422
Speed: 6.1ms preprocess, 7.5ms inference, 0.0ms loss, 5.4ms postprocess per image
the training was done for 500 epochs on the provided Dataset. To run the train.py file, please change directory to the Train
folder and execute python .\train.py -d './cat-and-dog-detection-1.zip' -k
.
Thank you.