1. Research background and significance
Project ReferenceAAAI Association for the Advancement of Artificial Intelligence
The smooth flow of fire escapes is crucial to the safety of personnel and property. However, in real life, due to various reasons, fire exits are often illegally occupied, resulting in fire vehicles being unable to reach the fire scene in time, seriously threatening people’s lives and property. Therefore, it is of great practical significance to develop an efficient and accurate fire escape passage occupancy detection system.
Currently, computer vision technology has made significant progress in the field of target detection. YOLOv5 is a target detection algorithm based on deep learning, which is fast and accurate. However, the traditional YOLOv5 has some problems in fire channel occupancy detection, such as poor detection of small targets and vulnerability to background interference. Therefore, it is of great research significance to improve YOLOv5 and improve its performance in fire passage occupancy detection.
Based on the research background, this study proposes a fire passage occupancy detection system based on YOLOv5 based on GSConv + SlimNeck. The system mainly includes two key technologies: GSConv and SlimNeck.
First of all, GSConv is an improved convolution operation that effectively improves the performance of target detection by introducing the ideas of group sparse convolution and group dense convolution. Traditional convolution operations tend to lose detailed information when processing large-scale targets, while GSConv can better retain the detailed information of the target and improve the accuracy of target detection.
Secondly, SlimNeck is a lightweight feature fusion module that reduces the complexity of the model and improves the running speed of the model by reducing the number of feature channels. In fire channel occupancy detection, since the targets are usually small, too many feature channels will lead to model redundancy and affect the detection effect. SlimNeck can effectively reduce the number of feature channels and improve the detection speed and accuracy of the model.
2. Picture demonstration
3. Demonstration video
Fire channel occupancy detection system based on YOLOv5 based on GSConv + SlimNeck_bilibili_bilibili
4. System advantages
First, the system can improve the accuracy of target detection. By introducing the GSConv operation, the detailed information of the target can be better retained, the accuracy of target detection can be improved, and the false detection rate and missed detection rate can be reduced.
Secondly, the system has a high operating speed. By introducing the SlimNeck module, the number of feature channels can be reduced, the complexity of the model can be reduced, the running speed of the model can be improved, and it is suitable for real-time fire channel occupancy detection.
Finally, the system has good versatility and scalability. Based on the YOLOv5 architecture, this system can be applied to other target detection tasks, such as pedestrian detection, vehicle detection, etc., and has good versatility and scalability.
In summary, the fire channel occupancy detection system based on GSConv + SlimNeck’s YOLOv5 has important research significance and practical application value. Through the development and application of this system, the accuracy and efficiency of fire passage occupancy detection can be improved and the safety of people’s lives and property can be guaranteed. At the same time, the research ideas and methods of this system can also provide reference for the research of other target detection tasks.
5. Core code explanation
5.1 GSConv + Slim Neck.py
# GSConv layer definition class GSConv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0): super(GSConv, self).__init__() self.main = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding) self.side = nn.Conv2d(in_channels, out_channels, 1) def forward(self, x): return self.main(x) + self.side(x) #GSbottleneck definition class GSbottleneck(nn.Module): def __init__(self, in_channels, out_channels): super(GSbottleneck, self).__init__() mid_channels = out_channels // 2 self.conv1 = GSConv(in_channels, mid_channels, 1) self.conv2 = GSConv(mid_channels, out_channels, 3, padding=1) def forward(self, x): return self.conv2(self.conv1(x)) # VoV-GSCSP definition class VoV_GSCSP(nn.Module): def __init__(self, in_channels, out_channels): super(VoV_GSCSP, self).__init__() self.conv = GSbottleneck(in_channels, out_channels) def forward(self, x): return self.conv(x) #Slim-Neck class SlimNeck(nn.Module): def __init__(self, in_channels, out_channels): super(SlimNeck, self).__init__() self.layer = VoV_GSCSP(in_channels, out_channels) def forward(self, x): return self.layer(x)
This program file is a neural network model for object detection. It contains several custom modules and layers.
The first is the GSConv layer, which is a convolutional layer with primary and auxiliary convolutions. The primary convolution and the auxiliary convolution perform convolution operations on the input respectively, and then add their results as the output.
Next is the GSbottleneck module, which consists of two GSConv layers. The first GSConv layer halves the number of input channels and then outputs it after passing through the second GSConv layer.
The VoV_GSCSP module is a module that uses the GSbottleneck module.
The SlimNeck module is a module that uses the VoV_GSCSP module.
GhostModule is a module with convolution operations with main convolutional and generative features. The main convolution performs a convolution operation on the input, then the convolution operation that generates the features performs a convolution operation on the output of the main convolution, and finally their results are spliced together as the output.
Finally, there is the YOLOv5Optimized module, which is a module using GhostModule and SlimNeck. It defines several GhostModule and SlimNeck layers, uses them for forward propagation, and finally outputs the detection results.
The entire program file defines an optimized YOLOv5 model for target detection tasks.
5.2 check_img.py
The following is the code encapsulated as a class:
class ImageProcessor: def __init__(self, path, train_file): self.path = path self.train_file = train_file def process_images(self): result = os.listdir(self.path) num = 0 if not os.path.exists(self.train_file): os.mkdir(self.train_file) for i in result: try: image = cv2.imread(self.path + '/' + i) cv2.imwrite(self.train_file + '/' + 'Compressed' + i, image, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) num + = 1 except: pass print('Data validity verification completed, the number of valid pictures is %d' % num) if num == 0: print('Your picture name has Chinese characters, it is recommended to unify it as 1 (1).jpg/png')
This class encapsulates the file processing logic in the process_images
method, and the constructor __init__
receives the image folder path and output folder path that need to be processed. You can adjust the properties and methods of the class as needed.
The program file name is check_img.py. Its main function is to reduce noise and compress the pictures in the specified folder, and save the processed pictures to another folder.
The program first imported the cv2, numpy and os modules. cv2 is an OpenCV library for image processing; numpy is a numerical calculation library for Python; os is an operating system interface module for Python.
Next, a variable path is defined to store the path of the image folder that needs to be processed. Then use the os.listdir() function to get all the file names in the folder and save the results to the variable result.
A variable train_file is defined, which is used to store the folder path of the processed images. Then a variable num is defined to record the number of valid pictures.
Next, use the if statement to determine if the train_file folder does not exist, and then use the os.mkdir() function to create the folder.
Then use a for loop to iterate through each file name in the result. In the loop, use the cv2.imread() function to read the image data of each file and save the result to the variable image.
Then use the cv2.imwrite() function to save the processed image data to the train_file folder. The file name is ‘Compressed’ + i, that is, add ‘Compressed’ before the original file name.
In the try-except statement, if an exception occurs, the processing of the file is skipped.
Finally, print out the number of valid pictures.
If the number of valid pictures is 0, it is recommended to print out the picture naming as ‘1 (1).jpg/png’.
Summary: The function of this program is to reduce noise and compress the pictures in the specified folder, and save the processed pictures to another folder.
5.3 torch_utils.py
import datetime import logging import math import os import platform import subprocess import time from contextlib import contextmanager from copy import deepcopy from pathlib import Path import torch import torch.distributed as dist import torch.nn as nn import torch.nn.functional as F import torchvision LOGGER = logging.getLogger(__name__) @contextmanager def torch_distributed_zero_first(local_rank: int): """ Decorator to make all processes in distributed training wait for each local_master to do something. """ if local_rank not in [-1, 0]: dist.barrier(device_ids=[local_rank]) yield if local_rank == 0: dist.barrier(device_ids=[0]) def date_modified(path=__file__): # return human-readable file modification date, i.e. '2021-3-26' t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime) return f'{<!-- -->t.year}-{<!-- -->t.month}-{<!-- -->t.day}' def git_describe(path=Path(__file__).
This program file is a tool file for PyTorch and contains some commonly used functions and classes. Here are some important parts of the file:
-
torch_distributed_zero_first
: A context manager used in distributed training to make all processes wait for each local master process to perform certain operations. -
date_modified
: Returns the modified date of the file. -
git_describe
: Returns the description information of the git warehouse. -
select_device
: Select a device (CPU or GPU). -
time_sync
: Returns the accurate time. -
profile
: used to analyze the speed, memory and FLOPs of the model. -
is_parallel
: Determine whether the model is a parallel model. -
de_parallel
: Convert the model to a single GPU model. -
intersect_dicts
: Returns two words
6. Overall system structure
According to the analysis, overall, this project is a fire passage occupancy detection system based on YOLOv5 of GSConv + SlimNeck. It contains multiple program files, each with different functions, used to implement various parts of the entire system.
The following is a summary of the functions of each file:
File path | Function overview |
---|---|
check_img.py | Denoise and compress images in the specified folder |
demo.py | Use YOLOv5 model for target detection |
GSConv + Slim Neck.py | Defines modules and layers such as GSConv and SlimNeck |
Interface.py | Load the model and perform target detection, draw the bounding box and display the image |
location.py | Display the image and allow the user to pass Click the mouse to get the coordinates on the image |
torch_utils.py | Contains some PyTorch utility functions and classes |
train.py | Train YOLOv5 model |
ui.py | User interface related functions |
models\common.py | Contains some common model components |
models\experimental.py | Contains some experimental model components |
models\tf.py | Contains some TensorFlow-related models Component |
models\yolo.py | Contains the definition of YOLOv5 model |
models_< em>init_.py | Model-related initialization files |
tools\activations.py | Contains Contains some activation functions |
tools\augmentations.py | Contains some data enhancement methods |
tools\autoanchor.py | Contains the method of automatic anchor box generation |
tools\autobatch.py | Contains |
tools\callbacks.py | Contains some callback functions |
tools\datasets.py | Contains data set processing methods |
tools\downloads.py | Contains Provides some methods for downloading data sets |
tools\general.py | Contains some common tool functions |
tools\loss.py | Contains some loss functions |
tools\metrics.py | Contains some evaluation indicators |
tools\plots.py | Contains some plotting functions |
tools\torch_utils.py | Contains some PyTorch utility functions |
tools_init_.py | Tool-related initialization files |
tools\aws\resume.py | Contains the model recovery method on AWS |
tools\aws_init_.py | AWS related initialization files |
tools\flask_rest_api\example_request.py | Contains an example request for the Flask REST API |
tools\flask_rest_api\restapi.py | Contains the implementation of Flask REST API |
tools\loggers_init_.py | Log Logger related initialization file |
tools\loggers\wandb\log_dataset.py | Contains the data set log method of WandB logger |
tools\loggers\wandb\sweep.py | Contains the hyperparameter search method of WandB logger |
tools\loggers\wandb\wandb_utils.py | Contains the utility functions of WandB logger |
tools\loggers\wandb_init_.py | WandB logger related initialization file |
utils\ activations.py | Contains some activation functions |
utils\augmentations.py | Contains some data enhancement methods |
utils\autoanchor.py | Contains the method of automatic anchor box generation |
utils\ autobatch.py | Contains automatic batch processing methods |
utils\callbacks.py | Contains some callback functions |
utils\datasets.py | Contains data set processing methods |
utils\ downloads.py | Contains some methods for downloading data sets |
utils\general.py | Contains some common methods Utility functions |
utils\loss.py | Contains some loss functions |
utils\ \metrics.py | Contains some evaluation metrics |
utils\plots.py | Contains some plotting functions |
utils\torch_utils.py | Contains some PyTorch utility functions |
utils_ init_.py | Tool-related initialization files |
utils\aws\resume.py | Contains model recovery methods on AWS |
utils\aws_init_.py | AWS-related initialization files |
utils\flask_rest_api\example_request.py | Contains an example request for the Flask REST API |