-
Notifications
You must be signed in to change notification settings - Fork 1
/
drone_detection_yolov8x.py
236 lines (152 loc) · 11.7 KB
/
drone_detection_yolov8x.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-
"""drone_detection_yolov8x.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1V3VpyVxpF5RXqc6x8D5qCI0X9pCF9Ani
# Drone Detection Using YOLOv8: A Comprehensive Dataset and Model Approach
<br>
<img src="https://images.pexels.com/photos/1852984/pexels-photo-1852984.jpeg" height=550 width=1000 alt="https://www.pexels.com/"/>
<small>Picture Source: <a href="https://www.pexels.com/tr-tr/@sulimansallehi/">Suliman Sallehi</a></small>
<br>
## Abstract
Unmanned aerial vehicles (UAVs), commonly known as drones, have become increasingly prevalent in various domains, including surveillance, photography, and delivery services. However, the rapid proliferation of drones raises concerns regarding security and privacy threats. To address these concerns, effective drone detection systems are crucial for identifying and tracking drones in real-time. In this research, we present a comprehensive dataset and propose a state-of-the-art drone detection model using the YOLOv8 architecture.
## Introduction
The widespread adoption of drones has led to an urgent need for reliable drone detection systems to ensure the safety and security of public spaces. Drone detection poses unique challenges due to the small size, fast movement, and diverse appearance of drones, making traditional object detection methods insufficient. Therefore, there is a growing demand for advanced detection models that can accurately identify drones in complex environments.
YOLO (You Only Look Once) is an object detection algorithm that was introduced in 2015 by **Joseph Redmon** et al. It revolutionized the field of computer vision by providing a real-time object detection solution with impressive accuracy.
## Dataset
To facilitate the development and evaluation of drone detection models, we introduce a novel and comprehensive dataset specifically curated for training and testing drone detection algorithms. The dataset, sourced from the publicly available [YOLO Drone Detection Dataset](https://www.kaggle.com/datasets/muki2003/yolo-drone-detection-dataset) on Kaggle, comprises a diverse set of annotated images captured in various environmental conditions and camera perspectives. The dataset includes instances of drones along with other common objects to enable robust detection and classification.
## Methodology
In this study, we employ the YOLOv8 architecture, a popular and highly efficient object detection framework, for drone detection. YOLOv8 stands for "You Only Look Once" version 7, which utilizes a single neural network to simultaneously predict bounding boxes and class probabilities for multiple objects in an image. This architecture offers real-time performance, making it ideal for drone detection applications.
## Experimental Setup
To train and evaluate our drone detection model, we utilize the Colab platform, a cloud-based environment that provides access to powerful computing resources and deep learning libraries. Leveraging Colab's GPU acceleration capabilities, we train the YOLOv8 model using our curated dataset and fine-tune its parameters to optimize detection accuracy and efficiency.
## YOLO
* **Single Pass Detection**: YOLO takes a different approach compared to traditional object detection methods that use region proposal techniques. Instead of dividing the image into regions and examining each region separately, YOLO performs detection in a single pass. It divides the input image into a grid and predicts bounding boxes and class probabilities for each grid cell.
* **Grid-based Prediction**: YOLO divides the input image into a fixed-size grid, typically, say, 7x7 or 13x13. Each grid cell is responsible for predicting objects that fall within it. For each grid cell, YOLO predicts multiple bounding boxes (each associated with a confidence score) and class probabilities.
* **Anchor Boxes**: To handle objects of different sizes and aspect ratios, YOLO uses anchor boxes. These anchor boxes are pre-defined boxes of different shapes and sizes. Each anchor box is associated with a specific grid cell. The network predicts offsets and dimensions for anchor boxes relative to the grid cell, along with the confidence scores and class probabilities.
* **Training**: YOLO is trained using a combination of labeled bounding box annotations and classification labels. The training process involves optimizing the network to minimize the localization loss (related to the accuracy of bounding box predictions) and the classification loss (related to the accuracy of class predictions).
* **Speed and Accuracy Trade-off**: YOLO achieves real-time object detection by sacrificing some localization accuracy compared to slower methods like Faster R-CNN. However, it still achieves competitive accuracy while providing significantly faster inference speeds, making it well-suited for real-time applications.
<br>
Since its introduction, YOLO has undergone several improvements and variations. Different versions such as YOLOv2, YOLOv3, and YOLOv4 have been developed, each introducing enhancements in terms of accuracy, speed, and additional features.
It's important to note that this is a high-level overview of YOLO, and the algorithm has many technical details and variations. For a more in-depth understanding, it's recommended to refer to the original YOLO papers and related resources.
## Keywords
* Drone detection
* YOLOv8
* Object detection
* Deep learning
* Surveillance
* Security
<br>
Make sure your runtime is **GPU** (_not_ CPU or TPU). And if it is an option, make sure you are using _Python 3_. You can select these settings by going to `Runtime -> Change runtime type -> Select the above mentioned settings and then press SAVE`.
## Importing Libraries
"""
!pip install ultralytics -q
import os
from pathlib import Path
import pandas as pd
from IPython.display import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as patches
from PIL import Image
from google.colab import files
from google.colab import drive
import warnings
warnings.filterwarnings("ignore")
from ultralytics import YOLO, checks
import torch
checks()
"""## Data Preprocessing"""
# Commented out IPython magic to ensure Python compatibility.
# %cd /content/
files.upload()
!unzip -q /content/archive.zip
# !rm -rf /content/archive.zip
#@markdown ---
#@markdown ### Enter image paths:
train_images_dir = "/content/drone_dataset/train/images" #@param {type:"string"}
val_images_dir = "/content/drone_dataset/valid/images" #@param {type:"string"}
train_image_count = len([f for f in os.listdir(train_images_dir) if f.endswith(".jpg")])
val_image_count = len([f for f in os.listdir(val_images_dir) if f.endswith(".jpg")])
print(f"Number of images in train folder: {train_image_count}")
print(f"Number of images in val folder: {val_image_count}")
#@markdown ---
#@markdown ---
#@markdown ### Enter label paths:
train_labels_dir = "/content/drone_dataset/train/labels" #@param {type:"string"}
val_labels_dir = "/content/drone_dataset/valid/labels" #@param {type:"string"}
train_txt_count = len([f for f in os.listdir(train_labels_dir) if f.endswith(".txt")])
val_txt_count = len([f for f in os.listdir(val_labels_dir) if f.endswith(".txt")])
print(f"Number of TXT files in train labels folder: {train_txt_count}")
print(f"Number of TXT files in val labels folder: {val_txt_count}")
#@markdown ---
"""## Training
YOLOv8 GitHub: [ultralytics](https://github.com/ultralytics/ultralytics)
"""
torch.__version__
"""Before training, you neet change `coco.yaml` and define your number of class, class names and train-val paths like that:
```
train: ../drone_dataset/train
val: ../drone_dataset/valid
# number of classes
nc: 1
# class names
names: ['drone']
```
"""
yaml_content = f'''
train: ../drone_dataset/train
val: ../drone_dataset/valid
# number of classes
nc: 1
# class names
names: ['drone']
'''
with Path('coco.yaml').open('w') as f:
f.write(yaml_content)
"""* `!yolo train`: This is the command to execute the yolo script for training the YOLOv8 model.
* `data=/content/coco.yaml`: This parameter specifies the path to the YAML file containing the dataset configuration. In this case, **the *coco.yaml* file is used, which provides information about the dataset, including the classes and paths to the training and validation data.**
* `imgsz=640`: This parameter sets the input image size for the model. The YOLOv8 model requires square input images, and here the dimensions are set to 640x640 pixels.
* `epochs=32`: This parameter defines the number of epochs, which represents the number of times the entire training dataset will be passed through the model during training. In this case, the model will be trained for **32 epochs**.
* `model = YOLO("yolov8x.pt")`: This parameter specifies the initial weights of the model. The *yolov8x.pt* file contains the pre-trained weights for the *YOLOv8* model, which will be used as the starting point for training.
"""
model = YOLO("yolov8x.pt")
results = model.train(
batch=16,
data="coco.yaml",
epochs=32,
imgsz=640,
)
image = mpimg.imread('/content/runs/detect/train2/results.png')
plt.figure(figsize=(16, 9), dpi=300)
plt.title('Results')
plt.imshow(image)
plt.axis('off')
plt.show()
"""To calculate *mAP @ 0.5*, the model's predicted bounding boxes are compared to the ground truth bounding boxes for various objects in the dataset. The average precision (AP) is computed for each class at an IoU threshold of 0.5, and then the mean of these average precision scores is calculated across all classes. *mAP @ 0.5* provides an overall measure of the model's detection performance, indicating how well it can identify objects with a reasonable overlap threshold.
## Tracking Drone with YOLOv8
Object tracking is a task that involves identifying the location and class of objects, then assigning a unique ID to that detection in video streams.
Video source: [@ninosouza](https://www.pexels.com/@ninosouza/)
Video link: [pexels](https://www.pexels.com/video/an-airborne-drone-machine-8459631/)
"""
model = YOLO("/content/runs/detect/train2/weights/best.pt")
result = model.track(source="/content/pexels-joseph-redfield-8459631(1080p).mp4", conf=0.3, iou=0.5, show=True)
"""## Save the Weights and Configuration File"""
# model.export(format='onnx')
files.download('/content/coco.yaml')
files.download('/content/runs/detect/train2/weights/best.pt')
!rm -rf /content/runs/detect/train2/weights
!zip -r '/content/results.zip' '/content/runs/detect/train2/'
files.download('/content/results.zip')
"""## Results and Discussion
We present comprehensive results of our drone detection model's performance on both the training and testing datasets. The evaluation metrics include precision, recall, and F1-score, which are standard measures to assess the model's detection accuracy. Additionally, we analyze the model's performance across various environmental conditions and discuss its strengths and limitations.
## Conclusion
Our research addresses the critical need for reliable drone detection systems by proposing a comprehensive dataset and a state-of-the-art detection model using the YOLOv8 architecture. The availability of our curated dataset and the promising performance of our model offer valuable contributions to the field of drone detection. The outcomes of this study can pave the way for enhanced security measures and privacy protection in areas where drones pose potential risks.
## Contact Me
<p>If you have something to say to me please contact me:</p>
<ul>
<li>Twitter: <a href="https://twitter.com/Doguilmak">Doguilmak</a></li>
<li>Mail address: [email protected]</li>
</ul>
"""
from datetime import datetime
print(f"Changes have been made to the project on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")