Stay Ahead of the Flames: A Comprehensive Guide to Wildfire Prevention with YOLOv8

Alim Tleuliyev
Institute of Smart Systems and AI
12 min readAug 15, 2023

--

Forest fire and smoke detection using YOLOv8 (original video)

1. Introduction

In the Abay Region of Kazakhstan, a heroic battle against a relentless wildfire claimed the lives of 15 individuals. Over the course of just six days, the blaze, ignited by two lightning strikes, ravaged an astonishing 60 thousand hectares of land (see Fig. 1). Sadly, this devastating incident is one among the 700 wildfires that plague Kazakhstan each year, contributing to the staggering global count of 60 thousand wildfires annually [1].

Fig. 1: Worldview on Abay region showing how the area was burnt to ashes

Wildfires are notoriously unpredictable and known for their rapid spread, driven by natural factors like temperature, dryness, and wind. Detecting fires at their incipient stages is crucial, yet it remains a daunting task for human intervention, particularly when faced with deep forest fires in remote areas. This article sheds light on the remarkable potential of computer vision technology in revolutionizing wildfire prevention through early fire and smoke detection.

2. Methodology

As we embark on our journey to explore wildfire prevention, the integration of computer vision technology presents an array of compelling advantages when compared to traditional fire detection methods. Unlike human-centric approaches or limited sensor-based systems, computer vision’s prowess lies in its ability to analyze vast visual datasets swiftly and accurately. With YOLOv8 at the helm, we unlock the potential for early fire and smoke detection, transcending the limitations of manual surveillance and paving the way for a proactive and efficient firefighting strategy.

2.1 Object Detection

Object detection is a fundamental task in computer vision, aiming to identify and localize objects in images or videos. Among various object detection models, the YOLO (You Only Look Once) [2] family stands out for its exceptional speed and real-time detection capabilities. In this article, our focus is on harnessing the power of YOLOv8, a prominent member of the YOLO family developed by Ultralytics.

Compared to its predecessors, YOLOv8 achieves higher accuracy on the benchmark COCO dataset with fewer parameters, resulting in improved efficiency (see Fig. 2). It offers different model sizes, ranging from nano to extra large, each tailored to specific requirements. As we delve deeper into our exploration of wildfire detection, we will analyze and compare the performance of different YOLOv8 sizes to determine the optimal solution for our specific problem.

Fig. 2: YOLOv8 performance compared to previous versions of YOLO (source)

2.2 Forest Fire and Smoke Detection Dataset

To accelerate the model training process, we utilize transfer learning by leveraging a pre-trained model on the widely-used COCO (Common Objects in Context) dataset. For fine-tuning the model specifically for fire and smoke detection, we rely on a specialized dataset called D-Fire.

D-Fire dataset consists of 21,000 labeled images. Each image is annotated with bounding boxes in the YOLO format (x_center, y_center, width, height), where class 0 represents smoke and class 1 denotes fire. While the dataset primarily focuses on fire and smoke instances, it also contains images that depict clouds and other scenes that may resemble fire but are not actual fire instances. This helps enhance the model’s robustness and accuracy when deployed in real-world scenarios.

Here are a few examples showcasing the diversity of images and their corresponding annotations from the D-Fire dataset:

Fig. 3: Examples from the D-Fire dataset

Furthermore, the dataset statistics provide valuable insights into the scale of the dataset, including the number of images and bounding boxes for smoke and fire instances:

Fig. 4: Statistics of the D-Fire dataset

2.3 Training YOLOv8

2.3.1 Download the dataset

To train our model, we first need to download the dataset. You can find the dataset on the GitHub repository. Simply follow the provided link to access the dataset on Google Drive, and download it to your local device. Once downloaded, unzip the archive to extract the dataset. If you are using Linux or need to install the dataset on a remote server, you can use the gdown library to download the dataset from Google Drive. First, install the library by running the following command:

pip install gdown

Next, obtain the file ID of the dataset on Google Drive by visiting the dataset’s link on GitHub and clicking the “Share” button. The file link will be in the following format:

https://drive.google.com/file/d/19LSrZHYQqJSdKgH8Mtlgg7-i-L3eRhbh/view?usp=sharing

The file ID in this link is 19LSrZHYQqJSdKgH8Mtlgg7-i-L3eRhbh. To download the dataset using gdown, navigate to the directory where you want to store the dataset and execute the following command:

gdown 19LSrZHYQqJSdKgH8Mtlgg7-i-L3eRhbh

To unzip the downloaded archive, use the following command:

unzip D-Fire.zip -d path/to/where/you/want/to/unzip

2.3.2 Preprocess the dataset

Once the archive is unzipped, the dataset structure in YOLO format will appear as follows:

D-Fire
- | train
- - -| images
- - -| labels
- | test
- - -| images
- - -| labels

The training set contains 17,211 images, while the test set contains 4,306 images. To create a validation set for model tuning, we can use 10% of the training set. You can use your preferred method to split the data. Here is an example of how you can accomplish this using Python:

! pip install shutil

import os
import random
import shutil

train_images_folder = "D-Fire/train/images"
train_labels_folder = "D-Fire/train/labels"
val_images_folder = "D-Fire/val/images"
val_labels_folder = "D-Fire/val/labels"

# Create the validation folders if they don't exist
os.makedirs(val_images_folder, exist_ok=True)
os.makedirs(val_labels_folder, exist_ok=True)

# Get the list of image files in the train set
image_files = os.listdir(train_images_folder)

# Calculate the number of images to move to the validation set
num_val_images = int(0.1 * len(image_files))

# Randomly select the images to move
val_image_files = random.sample(image_files, num_val_images)

# Move the selected images and their corresponding labels to the validation set
for image_file in val_image_files:
# Move image file
image_src = os.path.join(train_images_folder, image_file)
image_dst = os.path.join(val_images_folder, image_file)
shutil.move(image_src, image_dst)

# Move label file
label_file = image_file.replace(".jpg", ".txt")
label_src = os.path.join(train_labels_folder, label_file)
label_dst = os.path.join(val_labels_folder, label_file)
shutil.move(label_src, label_dst)

After executing this code, the train, val, and test sets will contain 15,499, 1,722, and 4,306 images, respectively. After that manipulation, the directory tree should look as follows:

D-Fire
- | train
- - -| images
- - -| labels
- | val
- - -| images
- - -| labels
- | test
- - -| images
- - -| labels

2.3.3 Create a configuration file

Before we start training, we need to create a configuration file that provides information about the dataset. Create an empty file named “data.yaml” and include the following content:

path: /D-Fire
train: train/images # relative to path
val: val/images # relative to path
test: test/images # relative to path

names:
0: smoke
1: fire

The path specifies the root directory of the dataset, and the train, val, and test paths indicate the relative paths to the corresponding image directories. The names section maps the class IDs to their respective names.

During training, if the Ultralytics library encounters any issues locating your dataset, it will provide informative error messages to help you troubleshoot the problem (see Fig. 5). In some cases, you might need to adjust the path parameter in the configuration file to ensure the library can find your dataset successfully.

Fig. 5: Path error example

You might be wondering why we are not explicitly specifying the path to the label files. The reason is that the Ultralytics library automatically replaces the ‘images’ keyword in the provided paths with ‘labels’ in the training step. Therefore, it is essential to structure your directory as described earlier to ensure the library can locate the corresponding label files correctly. For more information, please refer to Ultralytics documentation.

2.3.4 Start training

To install the necessary packages for training, you can use either pip or conda:

pip install ultralytics

or

conda install ultralytics

Training using Ultralytics is straightforward. We will use a Python script for more flexibility in adjusting hyperparameters. More details can be found here. Here is an example of how to train the YOLOv8 model:

from ultralytics import YOLO
# Load a model
model = YOLO('yolov8n.pt') # load an official model
PROJECT = 'project_name' # project name
NAME = 'experiment_name' # run name

model.train(
data = 'data.yaml',
task = 'detect',
epochs = 200,
verbose = True,
batch = 64,
imgsz = 640,
patience = 20,
save = True,
device = 0,
workers = 8,
project = PROJECT,
name = NAME,
cos_lr = True,
lr0 = 0.0001,
lrf = 0.00001,
warmup_epochs = 3,
warmup_bias_lr = 0.000001,
optimizer = 'Adam',
seed = 42,
)

The data parameter specifies the path to the configuration file we created earlier. You can adjust the hyperparameters to suit your specific requirements. The Ultralytics documentation provides further details on available hyperparameters (link).

One important note is that Ultralytics does not provide a parameter to change the metric used to determine the best model during training. By default, it uses precision as the metric. If the precision does not improve within the defined patience value (set to 20 in our example), the model training will stop.

Fig. 6: Example of training logs

3. Results

The selected hyperparameters for training proved to be highly effective, leading to smooth convergence and remarkable results. The model training process completed in approximately 130 epochs, demonstrating its efficiency. The training progress can be visualized through the graphs automatically generated by Ultralytics (see Fig. 7).

Fig. 7: Nano model training graphs

The training phase yielded two checkpoints: the last one for resuming training and the best one, representing the model with the highest precision. These checkpoints are stored in the “project_name/experiment_name/weights” directory in PyTorch format. Evaluating the best model on the test set can be accomplished using the following Python code:

from ultralytics import YOLO
model = YOLO('project_name/experiment_name/weights/best.pt')
model.val(split='test', batch=48, imgsz=640, verbose=True, conf = 0.1, iou = 0.5)

As evident in the code snippet, we can specify the split for evaluation. By default, it refers to the data.yaml file created earlier, which contains the dataset information. However, if needed, you can change the dataset used for evaluation by specifying the “data” parameter. You can explore all the available arguments for the evaluation function here.

Fig. 8: Nano model evaluation logs

Even the smallest YOLOv8 model nano could reach an outstanding mAP50 of 0.79 on the test set (see Fig. 9). We can see the PR curve and other graphs in the folder that is automatically created at “runs/detect/val” directory.

Fig. 9: Precision-Recall curve of Nano model

Notably, the evaluation process extends to other model sizes, such as small, medium, large, and extra-large models. Despite the extra-large model being approximately 21 times larger than the nano model in terms of parameters, it only demonstrates a marginal improvement of 0.03 in mAP50 (see Fig. 10). This observation highlights the need to strike a balance between model size and performance based on the specific problem at hand. In production, it may be unnecessary to use larger models unless significant accuracy gains outweigh the resource and time costs associated with their deployment.

Fig. 10: Performances of YOLOv8 models of different sizes

4. Model Deployment

As a tangible outcome of our research, we’ve developed the Wildfire Detection App, a user-friendly tool accessible via Streamlit. This interactive platform allows users to directly experience the capabilities of our YOLOv8 detection model (see Fig. 11).

Fig. 11: Wildfire Detection App deployed on Streamlit

Users can effortlessly upload images or URLs for analysis through the app. They can choose between the Fire Detection and General models, gaining insights into the model’s proficiency in identifying fire and smoke. The app’s adjustable settings provide adaptability for different contexts (see Fig. 12).

Fig. 12: App’s Interface

Discover our research in action by trying out the Wildfire Detection App and delve into the GitHub repository for implementation details. This app provides a glimpse into the potential of computer vision for wildfire prevention.

5. Future Implications

5.1 Drones Equipped with Fire Detection

An innovative approach to wildfire prevention involves the use of drones equipped with advanced fire detection models. Drones offer the advantage of accessing high altitudes and remote locations that are often inaccessible to humans during active wildfires. By leveraging powerful fire detection algorithms like YOLOv8, these drones can quickly identify even small fires with remarkable precision, mitigating the risk of human oversight. The autonomous flight capabilities and specialized sensors of drones enable them to navigate challenging terrains and provide real-time data to firefighting teams, facilitating prompt and targeted responses to suppress wildfires effectively. This technology holds great promise in enhancing wildfire prevention strategies and safeguarding communities from the devastating impact of wildfires.

Fig. 13: Forest fire detection using YOLOv8 on a drone recorded video (original video)

5.2 Smart Cameras in Endangered Areas

Another compelling idea is to deploy smart cameras in endangered forests to enable continuous monitoring and early wildfire detection. These smart cameras, integrated with YOLOv8 fire detection capabilities, can be strategically placed in remote and hard-to-reach areas, where human presence is limited or impractical. The cameras can perform regular inference, analyzing the captured visuals for signs of smoke or fire. Upon detecting any potential fire incidents, the system can automatically trigger alerts to relevant authorities, facilitating rapid response and minimizing the time between detection and intervention.

The implementation of smart camera networks in endangered forests presents a proactive approach to wildfire prevention. With the ability to operate 24/7 and monitor vast areas simultaneously, these intelligent systems can act as a first line of defense, promptly identifying wildfires and providing valuable early warnings. This not only reduces the risk of wildfires spiraling out of control but also allows for more efficient resource allocation and strategic firefighting efforts.

Fig. 14: Smoke detection using YOLOv8 on a video recorded by a static camera (original video)

6. Conclusion

In conclusion, this comprehensive guide has explored the remarkable potential of YOLOv8, a powerful computer vision model, in revolutionizing wildfire prevention. By leveraging object detection and transfer learning on the D-Fire dataset, we achieved impressive results in early fire and smoke detection. The article highlighted the benefits of using drones equipped with fire detection models and deploying smart cameras in endangered forests, envisioning a future where proactive measures can mitigate the risks of devastating wildfires.

Through the synergy of cutting-edge technology and environmental awareness, we can stay ahead of the flames and protect our communities and natural ecosystems from the destructive impact of wildfires. By embracing these advancements and implementing them in real-world scenarios, we take significant strides toward a safer and more resilient future, fostering a world where we are better prepared to face the challenges of wildfires with confidence and determination.

7. Acknowledgement

I would like to acknowledge the Institute of Smart Systems and Artificial Intelligence (ISSAI) at Nazarbayev University for fostering an environment of innovation and research excellence. The support I received from ISSAI has been integral to the successful completion of this endeavor.

I extend my heartfelt appreciation to my supervisor, Askat Kuzdeuov, at ISSAI, whose guidance and mentorship were indispensable to the success of this research. His expertise and support have been invaluable in shaping the direction and quality of this work.

I would also like to extend my thanks to the creators of the D-Fire dataset for providing a valuable resource that underpins the foundation of this research. Additionally, the Ultralytics team’s contribution to the YOLOv8 model has been instrumental in enabling accurate and efficient fire detection.

8. About the author

I am Alim Tleuliyev, a junior at Nazarbayev University, where I am pursuing a degree in Computer Science. My passion lies in the field of Artificial Intelligence, specifically Deep Learning and Machine Learning. I have had the privilege of immersing myself in these areas while conducting research as a Research Assistant at the Institute of Smart Systems and Artificial Intelligence (ISSAI).

Connect with me on LinkedIn to stay updated on my research endeavors and explore more of my projects. Feel free to reach out to me via email at alim.tleuliyev@nu.edu.kz.

References

  1. Shokan Alkhabayev, “Statistics of Forest and Steppe Fires for 3 Years Released by MES,” Tengrinews, 2022.
  2. Redmon, J., Divvala, S., Girshick, R., & Farhadi, A. (2016). You Only Look Once: Unified, Real-Time Object Detection. In 2016 IEEE Conference on Computer Vision and Pattern Recognition (pp. 779–788). https://doi.org/10.1109/CVPR.2016.91
  3. Ultralytics
  4. YOLOv8
  5. COCO Dataset
  6. D-Fire Dataset

--

--

Alim Tleuliyev
Institute of Smart Systems and AI

Nazarbayev University junior in Computer Science, researching Deep Learning and Machine Learning at the Institute of Smart Systems and Artificial Intelligence.