Skip to content

Chaganti-Reddy/Monkey-Detection

Repository files navigation

Monkey Detection using Computer Vision


This is an Assignment in the process of Machine Learning Internship Recruitment by IIT Bombay.


NOTE: Due to errors in Credit/Debit cards or errors in AWS/Google cloud platforms i was not able to deploy the application. So, I just created API & Dockerized the Application. Further Implementation of project is below. Please go through it.


Table of Contents


About

Monkey Detection using Computer Vision using AI. It uses live video stream from camera feed or rtsp streaming from ip camera or cctv or input images and use object detection to detect Monkey in these feeds. This project is part of Recruitment as an Internship in IIT Bombay. In this project I used YOLOV7 model to detect Monkey in the video feed.


YOLOV7

Performance

MS COCO

Model Test Size APtest AP50test AP75test batch 1 fps batch 32 average time
YOLOv7 640 51.4% 69.7% 55.9% 161 fps 2.8 ms
YOLOv7-X 640 53.1% 71.2% 57.8% 114 fps 4.3 ms
YOLOv7-W6 1280 54.9% 72.6% 60.1% 84 fps 7.6 ms
YOLOv7-E6 1280 56.0% 73.5% 61.2% 56 fps 12.3 ms
YOLOv7-D6 1280 56.6% 74.0% 61.8% 44 fps 15.0 ms
YOLOv7-E6E 1280 56.8% 74.4% 62.1% 36 fps 18.7 ms

Installation

Docker environment (recommended)

Expand
# create the docker container, you can change the share memory size if you have more.
nvidia-docker run --name yolov7 -it -v your_coco_path/:/coco/ -v your_code_path/:/yolov7 --shm-size=64g nvcr.io/nvidia/pytorch:21.08-py3

# apt install required packages
apt update
apt install -y zip htop screen libgl1-mesa-glx

# pip install required packages
pip install seaborn thop

# go to code folder
cd /yolov7

Testing

yolov7.pt yolov7x.pt yolov7-w6.pt yolov7-e6.pt yolov7-d6.pt yolov7-e6e.pt

python test.py --data data/coco.yaml --img 640 --batch 32 --conf 0.001 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_640_val

You will get the results:

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.51206
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.69730
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.55521
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.35247
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.55937
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.66693
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.38453
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.63765
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.68772
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.53766
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.73549
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.83868

To measure accuracy, download COCO-annotations for Pycocotools to the ./coco/annotations/instances_val2017.json


Training

Data preparation

bash scripts/get_coco.sh
  • Download MS COCO dataset images (train, val, test) and labels. If you have previously used a different version of YOLO, we strongly recommend that you delete train2017.cache and val2017.cache files, and redownload labels

Single GPU training

# train p5 models
python train.py --workers 8 --device 0 --batch-size 32 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml

# train p6 models
python train_aux.py --workers 8 --device 0 --batch-size 16 --data data/coco.yaml --img 1280 1280 --cfg cfg/training/yolov7-w6.yaml --weights '' --name yolov7-w6 --hyp data/hyp.scratch.p6.yaml

Multiple GPU training

# train p5 models
python -m torch.distributed.launch --nproc_per_node 4 --master_port 9527 train.py --workers 8 --device 0,1,2,3 --sync-bn --batch-size 128 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml

# train p6 models
python -m torch.distributed.launch --nproc_per_node 8 --master_port 9527 train_aux.py --workers 8 --device 0,1,2,3,4,5,6,7 --sync-bn --batch-size 128 --data data/coco.yaml --img 1280 1280 --cfg cfg/training/yolov7-w6.yaml --weights '' --name yolov7-w6 --hyp data/hyp.scratch.p6.yaml

Transfer learning

yolov7_training.pt yolov7x_training.pt yolov7-w6_training.pt yolov7-e6_training.pt yolov7-d6_training.pt yolov7-e6e_training.pt


Single GPU finetuning for custom dataset

# finetune p5 models
python train.py --workers 8 --device 0 --batch-size 32 --data data/custom.yaml --img 640 640 --cfg cfg/training/yolov7-custom.yaml --weights 'yolov7_training.pt' --name yolov7-custom --hyp data/hyp.scratch.custom.yaml

# finetune p6 models
python train_aux.py --workers 8 --device 0 --batch-size 16 --data data/custom.yaml --img 1280 1280 --cfg cfg/training/yolov7-w6-custom.yaml --weights 'yolov7-w6_training.pt' --name yolov7-w6-custom --hyp data/hyp.scratch.custom.yaml

Inference

On video:

python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source yourvideo.mp4

On image:

python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg

📁 Datasets

🔄 Source

This dataset is downloaded from roboflow using YOLOV7 model. You can find the data Here.

📖 Data Preprocessing

Data pre-processing is an important step for the creation of a machine learning model. Initially, data may not be clean or in the required format for the model which can cause misleading outcomes. In pre-processing of data, we transform data into our required format. It is used to deal with noises, duplicates, and missing values of the dataset. Data pre-processing has the activities like importing datasets, splitting datasets, attribute scaling, etc. Preprocessing of data is required for improving the accuracy of the model.

Requirements

System

System requirements depends on complexity or size of object detection model, larger model will require more compute power and will be good at detection. I have used this in Google Colab with Tesla T4 GPU & Kaggle with Tesla p100 GPU which is a good device. With my Nvidia Geforce 330 MX GPU based system it is not sufficient to train a YOLOV7 model.

Minimum of 4GB NVIDIA Graphics Card is required to train YOLOV7 model.

Python 3

Python 3.6 or higher. Tested with Python 3.6, 3.7, 3.8, 3.9, 3.10 in Windows 11 and Linux.

Packages

  • Torch >= 1.7.0
  • numpy >= 1.18.5
  • opencv-python >= 4.1.1

This implementation is tested with PyTorch gpu in Windows 11 and Linux and Google Colab


Installation

🔑 Prerequisites

All the dependencies and required libraries are included in the file requirements.txt See here

Install Python

There are two ways to install python in windows using Python 3 installer or Anaconda. Installing python with anaconda or miniconda is recommended. In linux Python 3 is installed by default but we can also install miniconda or conda into linux.

Creating Virtual Environment

Create a new python virtual environment using conda or venv and activate it. If Anaconda or Miniconda is installed use conda else use venv to create virtual environments.

  • Using conda
conda create --name security_cam
conda activate security_cam
conda install pip
  • Using venv in linux
python3 -m venv path/to/create/env/security_cam
source path/to/create/env/security_cam/bin/activate
  • Using venv in windows
python -m venv path\to\create\env\security_cam
path\to\create\env\security_cam\Scripts\activate

Installing dependencies

The command below will install all the required dependencies from requirements.txt file.

pip install -r requirements.txt

🚀  Setup (On Production Machine)

Download the YOLOV7 model from here and place it in the root directory of the project. Then copy the dataset you downloaded from Roboflow to the YOLOV7-MAIN Folder and add the data.yaml file in main directory of the project. The data.yaml file should contain the following information: (The dataset folder name is monkey_data)

Note: Clone the YOLOV7 repository and do not download as a zip since .git folder is required for training and create a "static" and "results" named empty folder after copying templates folder to YOLOV7.

git clone https://github.com/WongKinYiu/yolov7.git

The data.yaml file should contain the following information:

train: monkey_data/train/images
val: monkey_data/valid/images
nc: 1
names: ["monkey"]
  1. Change your directory to the cloned repo
cd "yolov7-main"
  1. Also copy the files from this repository to the YOLO Folder same as the structured folder in the given drive link.

Download on Drive

  1. I used "Flask" as an API to connect the model with the web application.

  2. Check out the flask file here app.py

  3. Then I created a Docker image using Dockerfile and pushed it to the Docker Hub.

  4. We can also use Docker-compose to run the application.

  5. Due to errors in Credit/Debit cards or errors in AWS/Google cloud platforms i was not able to deploy the application.


💡 How to Run

  1. Open terminal. Go into the cloned project directory and type the following command:
python3 train.py --epochs 128 --workers 8 --device 0 --batch-size 32 --data data.yaml --img 416 416 --cfg cfg/training/yolov7.yaml --weights '' --name Monkey-Model --hyp data/hyp.scratch.p5.yaml
  1. After training is done, run the following command to test the model on a video/image:
py detect.py --weights best.pt --project results --name results --source static/img.jpg --save-txt --save-conf
  1. If you want to try the Project then run the flask file app.py
python app.py

Additional guides

If get stuck in installation part follow these additional resources


Documentation

Input Parameters

The Input Parameters used in this project are

  1. --epochs: Number of epochs to train the model.
  2. --workers: Number of workers to use for data loading.
  3. --device: Device to use for training.(0 for GPU and 1 for CPU)
  4. --batch-size: Batch size for training.
  5. --data: Path to data.yaml file.
  6. img: Image size.
  7. --cfg: Path to model.yaml file.
  8. --weights: Path to weights file. (Leave blank for training from scratch)
  9. --name: Name of the model.
  10. --hyp: Path to hyperparameters file.

Error Handling

Mostly you are supposed to get errors regarding cv2.imwrite() to a particular folder, At that time you need to create respected folders in the main directory.


📂 Directory Tree

├───cfg
│   ├───baseline
│   ├───deploy
│   └───training
├───data
├───deploy
│   └───triton-inference-server
│       └───data
├───models
│   └───__pycache__
├───monkey_data
│   ├───test
│   │   ├───images
│   │   └───labels
│   ├───train
│   │   ├───images
│   │   └───labels
│   └───valid
│       ├───images
│       └───labels
├───results
├───runs
│   └───train
│       └───Monkey-Model
│           └───weights
├───scripts
├───static
├───templates
├───tools
└───utils
    ├───aws
    ├───google_app_engine
    ├───wandb_logging
    └───__pycache__

🔑 Results

Our YOLOV7 Model gives the best accuracy and some good F1 Scores etc., & you can see below :

F1 Score:

P Curve:

PR Curve:

R Curve:

Results:


Test Cases

Test Case 1:

Test Case 2:

Test Case 3:


👏 And it's done!

Feel free to mail me for any doubts/query 📧 chagantivenkataramireddy1@gmail.com

🙋 Citation

You are allowed to cite any part of the code or our dataset. You can use it in your Research Work or Project. Remember to provide credit to the Maintainer Chaganti Reddy by mentioning a link to this repository and her GitHub Profile.

Follow this format:

  • Author's name - Chaganti Reddy
  • Date of publication or update in parentheses.
  • Title or description of document.
  • URL.

You can find the references I used in the references.md file.


❤️ Owner

Made with ❤️  by Chaganti Reddy

👀 License

MIT © Chaganti Reddy

Releases

No releases published

Packages

No packages published