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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
base_model:
|
| 6 |
+
- ultralytics/yolov8n
|
| 7 |
+
pipeline_tag: object-detection
|
| 8 |
+
tags:
|
| 9 |
+
- surveillance
|
| 10 |
+
- Threat_detection
|
| 11 |
+
- ultralytics
|
| 12 |
+
- yolov8
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# YOLOv8n based Threat Detection Model
|
| 16 |
+
|
| 17 |
+
[](https://opensource.org/licenses/MIT)
|
| 18 |
+
[](https://github.com/ultralytics/ultralytics)
|
| 19 |
+
[](https://github.com/subh-775/Threat_Detection_YOLO-vs-RF-DETR)
|
| 20 |
+
[](#performance-metrics)
|
| 21 |
+
|
| 22 |
+
## CNNs for Object Detection
|
| 23 |
+
|
| 24 |
+
**YOLOv8**, developed by Ultralytics, continues the legacy of the highly popular YOLO (You Only Look Once) series. This version brings significant improvements in both speed and accuracy, making it a top choice for real-time object detection tasks. Its efficient CNN-based architecture is optimized for performance on both CPUs and GPUs.
|
| 25 |
+
|
| 26 |
+
This repository features a **fine-tuned YOLOv8 Nano model** specifically trained for **Threat Detection**, designed to identify four critical threat categories with high precision and speed.
|
| 27 |
+
|
| 28 |
+
## Predicted Results
|
| 29 |
+
*Add your prediction images and videos here.*
|
| 30 |
+
|
| 31 |
+
## Model Overview
|
| 32 |
+
|
| 33 |
+
**YOLOv8n Threat Detection** is a specialized computer vision model for security and surveillance. Leveraging the speed and efficiency of the YOLOv8 Nano architecture, this model accurately detects potential threats in real-time scenarios.
|
| 34 |
+
|
| 35 |
+
The threat categories are:
|
| 36 |
+
|
| 37 |
+
| Class ID | Threat Type | Description |
|
| 38 |
+
|----------|-------------|-------------|
|
| 39 |
+
| 1 | **Gun** | Any type of firearm weapon including pistols, rifles, and other firearms |
|
| 40 |
+
| 2 | **Explosive** | Fire, explosion scenarios, and explosive devices |
|
| 41 |
+
| 3 | **Grenade** | Hand grenades and similar explosive devices |
|
| 42 |
+
| 4 | **Knife** | Bladed weapons including knives, daggers, and sharp objects |
|
| 43 |
+
|
| 44 |
+
## Training Dataset
|
| 45 |
+
|
| 46 |
+
The model was trained on a custom threat detection dataset, meticulously curated and annotated for robust performance across various scenarios.
|
| 47 |
+
|
| 48 |
+
### Class Distribution
|
| 49 |
+

|
| 50 |
+
|
| 51 |
+
### Sample Annotations
|
| 52 |
+
[!sample annotation image](https://cdn-uploads.huggingface.co/production/uploads/66c6048d0bf40704e4159a23/Mf65kxTEwfq9HPMlzwO5y.png)
|
| 53 |
+
|
| 54 |
+
## Performance Metrics
|
| 55 |
+
|
| 56 |
+
## Training performance
|
| 57 |
+

|
| 58 |
+
|
| 59 |
+
## Confusion matrix
|
| 60 |
+

|
| 61 |
+
|
| 62 |
+
## Validation Results
|
| 63 |
+
|
| 64 |
+
| **Metric** | **Gun** | **Explosive** | **Grenade** | **Knife** | **Overall** |
|
| 65 |
+
|------------------|:-------:|:-------------:|:------------:|:----------:|:------------:|
|
| 66 |
+
| **mAP@50:95** | 47.8% | 48.5% | 76.6% | 48.2% | **55.3%** |
|
| 67 |
+
| **mAP@50** | 78.3% | 74.1% | 92.1% | 80.9% | **81.3%** |
|
| 68 |
+
| **Precision** | 83.3% | 77.8% | 96.5% | 79.7% | **84.3%** |
|
| 69 |
+
| **Recall** | 69.0% | 68.2% | 89.9% | 78.1% | **76.3%** |
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
## Test Results
|
| 73 |
+
|
| 74 |
+
| **Metric** | **Gun** | **Explosive** | **Grenade** | **Knife** | **Overall** |
|
| 75 |
+
|------------------|:-------:|:-------------:|:------------:|:----------:|:------------:|
|
| 76 |
+
| **mAP@50:95** | 65.3% | 35.7% | 83.2% | 49.8% | **58.5%** |
|
| 77 |
+
| **mAP@50** | 93.1% | 60.5% | 91.1% | 79.7% | **81.1%** |
|
| 78 |
+
| **Precision** | 96.7% | 49.7% | 93.1% | 86.5% | **81.5%** |
|
| 79 |
+
| **Recall** | 83.0% | 83.0% | 83.0% | 83.0% | **83.0%** |
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
### Key Performance Highlights
|
| 83 |
+
- High Overall Accuracy: Achieved a strong 81.3% mAP@50 on the validation set, showing the model is highly effective.
|
| 84 |
+
- Exceptional 'Grenade' Detection: The model excels at identifying grenades, with an outstanding 92.1% mAP@50 and an extremely high 96.5% precision. This indicates a very low rate of false positives for this class.
|
| 85 |
+
- Strong Generalization: Reached a peak mAP@50-95 of 55.3%, demonstrating a good ability to predict bounding boxes with high precision (IoU > 0.95).
|
| 86 |
+
- Balanced Learning: The steady decrease in box_loss, cls_loss, and dfl_loss over 50 epochs indicates stable and balanced learning across localization, classification, and distribution focal loss tasks.
|
| 87 |
+
|
| 88 |
+
### Model Architecture
|
| 89 |
+
- **Base Architecture**: YOLOv8 Nano (yolov8n.pt)
|
| 90 |
+
- **Parameters**: ~3 Million (3,006,428 fused)
|
| 91 |
+
- **Computational Cost**: ~8.1 GFLOPs
|
| 92 |
+
- **Layers**: The final architecture consists of 129 layers, with the final detection head (Detect layer #22) customized for 4 output classes.
|
| 93 |
+
|
| 94 |
+
### Training Details
|
| 95 |
+
|
| 96 |
+
### Training Configuration
|
| 97 |
+
- Epochs: 50
|
| 98 |
+
- Image Size: 640x640 pixels
|
| 99 |
+
- Optimizer: AdamW
|
| 100 |
+
- Learning Rate: 0.00125 (automatically determined by the Ultralytics framework)
|
| 101 |
+
- Momentum: 0.9 (automatically determined)
|
| 102 |
+
|
| 103 |
+
### Training Strategy
|
| 104 |
+
- Transfer Learning: The model was initialized with pre-trained weights from the COCO dataset, transferring knowledge from 319 of the 355 original layers. This significantly accelerated learning.
|
| 105 |
+
- Automatic Hyperparameter Optimization: The framework automatically selected the best optimizer (AdamW) and its corresponding learning rate and momentum, removing the need for manual tuning.
|
| 106 |
+
- Dynamic Augmentation Strategy: For the first 40 epochs, a mosaic augmentation was used to expose the model to a wide variety of object contexts. This was strategically turned off for the final 10 epochs to allow the model to refine its performance on whole, un-altered images, leading to a final performance boost.
|
| 107 |
+
|
| 108 |
+
### Key Performance Highlights
|
| 109 |
+
|
| 110 |
+
- **81.1% mAP@50** on the test set.
|
| 111 |
+
- **Fast inference** thanks to the optimized YOLOv8n architecture.
|
| 112 |
+
- **Excellent precision** for Gun (96.7%) and Grenade (93.1%) detection on the test set.
|
| 113 |
+
|
| 114 |
+
## Model Architecture
|
| 115 |
+
|
| 116 |
+
- **Base Architecture**: YOLOv8 Nano (YOLOv8n)
|
| 117 |
+
- **Input Resolution**: 640×640 pixels
|
| 118 |
+
- **Backbone**: Optimized CNN
|
| 119 |
+
- **Detection Head**: Custom 4-class threat detection
|
| 120 |
+
|
| 121 |
+
## Model Files
|
| 122 |
+
|
| 123 |
+
- `best.pt` - Main model weights
|
| 124 |
+
|
| 125 |
+
### Inference Instructions
|
| 126 |
+
|
| 127 |
+
```python
|
| 128 |
+
!pip install ultralytics
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
```python
|
| 132 |
+
# process video in batches
|
| 133 |
+
import cv2
|
| 134 |
+
from ultralytics import YOLO
|
| 135 |
+
from huggingface_hub import hf_hub_download
|
| 136 |
+
import torch
|
| 137 |
+
from tqdm import tqdm
|
| 138 |
+
|
| 139 |
+
# Configuration
|
| 140 |
+
MODEL_REPO = "Subh775/Threat-Detection-YOLOv8n"
|
| 141 |
+
INPUT_VIDEO = "input_video.mp4"
|
| 142 |
+
OUTPUT_VIDEO = "output_video.mp4"
|
| 143 |
+
CONFIDENCE_THRESHOLD = 0.4
|
| 144 |
+
BATCH_SIZE = 32 # Adjust based on GPU memory
|
| 145 |
+
|
| 146 |
+
# Setup device
|
| 147 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
| 148 |
+
print(f"Using device: {'GPU' if device == 0 else 'CPU'}")
|
| 149 |
+
|
| 150 |
+
# Load model
|
| 151 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename="weights/best.pt")
|
| 152 |
+
model = YOLO(model_path)
|
| 153 |
+
|
| 154 |
+
# Process video
|
| 155 |
+
cap = cv2.VideoCapture(INPUT_VIDEO)
|
| 156 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 157 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 158 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 159 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 160 |
+
|
| 161 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 162 |
+
out = cv2.VideoWriter(OUTPUT_VIDEO, fourcc, fps, (frame_width, frame_height))
|
| 163 |
+
|
| 164 |
+
frames_batch = []
|
| 165 |
+
with tqdm(total=total_frames, desc="Processing video") as pbar:
|
| 166 |
+
while cap.isOpened():
|
| 167 |
+
success, frame = cap.read()
|
| 168 |
+
if success:
|
| 169 |
+
frames_batch.append(frame)
|
| 170 |
+
|
| 171 |
+
if len(frames_batch) == BATCH_SIZE:
|
| 172 |
+
# Batch inference
|
| 173 |
+
results = model(frames_batch, conf=CONFIDENCE_THRESHOLD,
|
| 174 |
+
device=device, verbose=False)
|
| 175 |
+
|
| 176 |
+
# Write annotated frames
|
| 177 |
+
for result in results:
|
| 178 |
+
annotated_frame = result.plot()
|
| 179 |
+
out.write(annotated_frame)
|
| 180 |
+
|
| 181 |
+
pbar.update(len(frames_batch))
|
| 182 |
+
frames_batch = []
|
| 183 |
+
else:
|
| 184 |
+
break
|
| 185 |
+
|
| 186 |
+
# Process remaining frames
|
| 187 |
+
if frames_batch:
|
| 188 |
+
results = model(frames_batch, conf=CONFIDENCE_THRESHOLD,
|
| 189 |
+
device=device, verbose=False)
|
| 190 |
+
for result in results:
|
| 191 |
+
annotated_frame = result.plot()
|
| 192 |
+
out.write(annotated_frame)
|
| 193 |
+
pbar.update(len(frames_batch))
|
| 194 |
+
|
| 195 |
+
cap.release()
|
| 196 |
+
out.release()
|
| 197 |
+
print(f"Processed video saved to: {OUTPUT_VIDEO}")
|
| 198 |
+
|
| 199 |
+
```
|
| 200 |
+
|
| 201 |
+
### Acknowledgments
|
| 202 |
+
- **Ultralytics** for the YOLOv8 architecture and framework.
|
| 203 |
+
- **Hugging Face** for model hosting and community support.
|
| 204 |
+
- **Roboflow** for the dataset.
|
| 205 |
+
|
| 206 |
+
**Disclaimer:** This model is for research and educational purposes. It should not be used for deployment in real-world security applications without further extensive validation.
|