Fish detection method based on improved YOLOv8n
By improving the YOLOv8n model and optimizing fish detection methods with a number of technical means, the accuracy and efficiency of fish detection in complex water environments in the Yangtze River Basin has been solved, the demand for high-frequency real-time monitoring has been achieved, and the adaptability and detection accuracy of the model in complex environments has been improved.
Patent Information
- Application Number
- CN202510542518.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-28
- Publication Date
- 2025-08-15
AI Technical Summary
Existing fish detection methods have problems such as low detection accuracy, insufficient robustness, low computing efficiency and poor generalization capabilities in complex water environments such as the Yangtze River Basin. Especially in dense fish, small and medium-sized fish are prone to missed detection, and the light changes and occlusion factors have a great impact, making it difficult to achieve high-frequency real-time monitoring.
The improved YOLOv8n model is adopted, combining Mosaic data augmentation, random occlusion, GhostConv convolutional layer, bilayer link attention mechanism BiFormer and repeated weighted bidirectional feature pyramid network BiFPN, and improved loss function optimization model training to improve the adaptability and detection accuracy of the model in complex environments.
It significantly improves the model's detection ability of small targets, enhances its adaptability in complex water environments, reduces computing resource consumption, improves detection accuracy and robustness, reduces missed and missed detection, and meets the long-term and high-frequency real-time monitoring needs.
Smart Images

Figure CN120495834A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of fish identification, and in particular relates to a fish detection method based on improved YOLOv8n. Background Art
[0002] With the increasing demand for ecological protection and aquatic species diversity conservation in the Yangtze River Basin, fish monitoring and protection have become important topics in aquatic ecology research. Traditional fish monitoring methods rely on manual sampling and visual identification. These methods are not only labor-intensive and inefficient, but are also easily affected by factors such as weather and light, resulting in inaccurate and incomplete data. Furthermore, manual sampling in some remote areas is limited by time and resources, making it difficult to achieve large-scale, high-frequency monitoring.
[0003] In recent years, computer vision and deep learning technologies have made significant progress in object detection, particularly in target detection and image classification. Object detection methods based on convolutional neural networks (CNNs), particularly the YOLO (You Only Look Once) model family, have been widely used in various fields such as traffic monitoring, autonomous driving, and medical image analysis due to their excellent real-time detection capabilities and high accuracy. However, the YOLO model still faces challenges when detecting objects in complex natural environments, particularly fish in complex waters such as the Yangtze River Basin.
[0004] In the prior art, Chinese patent CN114782759B discloses a method for detecting densely occluded fish based on the YOLOv5 network, belonging to the field of fish image technology. The method comprises a dataset establishment and processing section, a model training section, and a model testing section. The dataset establishment and processing section includes the collection and annotation of fish image data, as well as data partitioning. The data partitioning involves dividing the data into a training set, a validation set, and a test set. The model training section uses a loss function to calculate the training budget output by the model, obtains the error of the training budget result, and updates the parameters in the model's neural network to improve the model's accuracy. The loss function is an improved loss function that introduces an improved repulsive force loss function into the YOLOv5 algorithm to enhance the model's ability to detect densely occluded fish. This method significantly improves the detection accuracy of densely occluded fish and greatly reduces the number of missed fish detections.
[0005] However, this method has low detection accuracy for small fish, especially in dense schools, where small targets are easily missed or misidentified. Secondly, factors such as illumination variations, reflections, and ripples in the aquatic environment still significantly impact the model's detection performance, resulting in insufficient robustness. Furthermore, YOLOv5's computational efficiency and real-time performance when processing large-scale data remain limited, making it unable to meet the needs of long-term, high-frequency real-time monitoring. Finally, existing methods have poor adaptability to diverse aquatic environments and unbalanced datasets, and the model's generalization ability is insufficient. Summary of the Invention
[0006] The purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a fish detection method based on improved YOLOv8n.
[0007] The purpose of the present invention can be achieved by the following technical solutions:
[0008] On the one hand, the present invention provides a fish detection method based on improved YOLOv8n, comprising the following steps:
[0009] Build a fish detection model, train the fish detection model, and use the trained fish detection model to detect fish images in the Yangtze River Basin;
[0010] Among them, the fish detection model is an improved YOLOv8n model, including an input layer, a backbone network, a neck network and a detection head connected in sequence. The input end uses the Mosaic method and the random occlusion method for data enhancement, and the input image is randomly scaled, cropped, brightness adjusted and spliced to form a new input image; the improvement includes replacing the Conv convolution layer with GhostConv phantom convolution, adding a two-layer link attention mechanism BiFormer and a repeatedly weighted bidirectional feature pyramid network BiFPN between the backbone network and the neck network.
[0011] Furthermore, the input end uses a mosaic method and a random occlusion method to perform data enhancement, and randomly scales, crops, adjusts brightness, and splices the input image to form a new input image, specifically including:
[0012] Randomly select 4 images from the original fish image set, read their sizes respectively, and obtain the original width W of each image i and height H i ;
[0013] According to the unified input size of the target image, the four images are scaled separately, and the scaling ratio is s i Calculated using the following formula:
[0014]
[0015] Among them, s i is the scaling factor of the i-th image, W i 、H i are the width and height of the original image, W T 、H T The target width and height of the target stitched image;
[0016] After scaling, each image is randomly cropped, and the four cropped images are stitched into a new image in a cross structure using Mosaic. During the stitching process, the horizontal and vertical split points are randomly selected, and the stitching area ratio is controlled between 0.3 and 0.7.
[0017] Perform brightness perturbation on the stitched new image to simulate different lighting conditions. The processing method is as follows:
[0018] I′(x,y)=α·I(x,y)+β
[0019] Where I(x,y) is the original pixel value, α∈[0.7,1.3] is the brightness adjustment coefficient, and β∈[-20,20] is the brightness offset;
[0020] A random occlusion operation is introduced into the brightness-adjusted image to generate 1 to 5 occlusion blocks. The pixel values in the occlusion blocks are set to 0 or Gaussian noise is added.
[0021] Normalize the image after the occlusion operation.
[0022] Furthermore, the backbone network includes a first phantom convolution module, a second phantom convolution module, a first C2F module, a third phantom convolution module, a second C2F module, a fourth phantom convolution module, a third C2F module, a fifth phantom convolution module, a fourth C2F module, and a first SPPF module connected in sequence.
[0023] Furthermore, the neck network includes a first upsampling module, a first connection module, a fifth C2F module, a second upsampling module, a second connection module, a sixth C2F module, a third connection module, a seventh C2F module, a sixth phantom convolution module, a fourth connection module, an eighth C2F module, and a seventh phantom convolution module, which are connected in sequence.
[0024] Furthermore, the backbone network detection head includes a first output module, a second output module, and a third output module.
[0025] Furthermore, the output end of the third phantom convolution module of the backbone network is connected to the input end of the second connection module of the neck network, the output end of the third C2F module of the backbone network is connected to the input end of the first connection module, the output end of the first SPPF module of the backbone network is connected to the first upsampling module of the neck network, and the fifth C2F module of the neck network is connected to the fourth connection module.
[0026] Furthermore, the input end of the repeated weighted bidirectional feature pyramid network BiFPN is respectively connected to the output end of the first SPPF module of the backbone network and the output end of the seventh phantom convolution module of the neck network. The neck network also includes a ninth C2F module. The output end of the repeated weighted bidirectional feature pyramid network BiFPN is connected to the input end of the ninth C2F module, and the output end of the ninth C2F module is connected to the input end of the dual-layer link attention mechanism BiFormer.
[0027] Furthermore, the output end of the dual-layer link attention mechanism BiFormer is connected to the input end of the third output module, the output end of the seventh C2F module of the neck network is connected to the first output module, and the output end of the eighth C2F module of the neck network is connected to the second output module.
[0028] Furthermore, the dual-layer link attention mechanism BiFormer includes four layers of sequentially connected BRA modules, and the BRA module includes the following data processing process:
[0029] The input image is embedded in blocks to obtain the initial feature map F0. The processing method is to input the image into the depth-separable convolution operation, use the convolution kernel size of 3×3, and perform the convolution operation on the original image I to obtain the implicitly encoded initial feature map F0:
[0030] F0=Conv 3×3 (I)
[0031] Among them, I represents the input image, Conv 3×3 It represents the depth-separable convolution operation using a 3×3 convolution kernel, and F0 represents the initial feature map of the output;
[0032] Input the initial feature map F0 into the relative position encoding module, extract the relative position information, and obtain the position embedding feature P0:
[0033] P0=F0+RelPos
[0034] Among them, RelPos represents the position encoding tensor constructed by the predefined relative position offset table, and P0 is the position embedding feature after adding position information;
[0035] Input the position embedding feature P0 into the attention module, calculate the query matrix Q, key matrix K, value matrix V, and calculate the attention weight matrix A according to the bidirectional relative attention mechanism to obtain the weighted feature map F1:
[0036] Q=W Q P0,K=W K P0,V=W V P0
[0037]
[0038] Among them, W Q 、W K 、W V is a learnable linear projection matrix, d k is the key vector dimension, B is the position offset matrix, A is the attention weight matrix, and F1 is the weighted feature map after weighting;
[0039] The weighted feature map F1 is input into the two-layer perceptron for nonlinear feature conversion. The first layer of perceptron performs linear transformation and activation function processing to obtain the intermediate representation H1:
[0040] H1=ReLU(W1F1+b1)
[0041] Among them, W1 is the first layer fully connected weight matrix, b1 is the bias vector, ReLU is the rectification activation function, and H1 is the output feature after activation;
[0042] Input the intermediate feature map H1 into the second layer of perceptron, perform linear transformation, and obtain the output feature map H2:
[0043] H2=W2H1+b2
[0044] Among them, W2 is the fully connected weight matrix of the second layer, b2 is the bias of the second layer, and H2 is the feature map output by the two-layer perceptron;
[0045] Perform element-wise addition of the perceptron output feature map H2 and the weighted feature map F1 to obtain the final output feature map F of the BRA module. BRA :
[0046] F BRA =F1+H2
[0047] Among them, F BRA Represents the output features after fusing attention information and nonlinear transformation.
[0048] Furthermore, the loss function of the fish detection model is:
[0049]
[0050] in, is the loss function of the fish detection model, λ bbox ,λ QFL is the weight coefficient, is the bounding box regression loss, is the quality-aware classification loss;
[0051] The bounding box regression loss is:
[0052]
[0053] Among them, B is the bounding box predicted by the model, B gt is the true target bounding box, CIoU(B,B gt ) is the intersection-over-union (IoU) of the predicted bounding box and the true target bounding box;
[0054] The quality-aware classification loss is:
[0055]
[0056] Among them, C is the number of fish species, p c is the model’s predicted probability for the cth class, q c is the quality label of category c. If the current sample is a positive sample of category c, that is, the current sample belongs to category c, q c =IoU(B,B gt ), if the current sample does not belong to the cth category, then q c =0, a is the adjustment factor.
[0057] Compared with the prior art, the present invention has the following advantages:
[0058] (1) In the YOLOv8 model architecture, this paper significantly optimizes the model's feature processing capabilities by integrating a dynamic sparse attention module called BiFormer between the backbone network and the neck network. This module introduces an adaptive query mechanism that enables the model to focus on extracting relevant features while ignoring interfering information, thereby improving the model's performance and computational efficiency. At the same time, this improvement significantly enhances the model's ability to detect small targets, especially in complex scenes.
[0059] (2) This paper combines Mosaic data augmentation, random occlusion, GhostConv convolutional layers, a two-layer link attention mechanism (BiFormer), and a repetitively weighted bidirectional feature pyramid network (BiFPN) to significantly improve the accuracy and robustness of fish detection. Mosaic data augmentation and random occlusion effectively simulate different environmental changes and occlusion conditions, enhancing the model's adaptability in complex water environments and reducing detection errors caused by factors such as illumination changes and occlusion.
[0060] (3) By introducing the GhostConv convolutional layer instead of traditional convolution, this invention not only reduces the computational complexity of the model but also improves computational efficiency, enabling faster detection tasks when processing complex water images. Compared to traditional convolutional methods, this innovative design significantly reduces computing resource consumption while preserving model accuracy, meeting the high demand for real-time detection.
[0061] (4) By introducing the dual-layer link attention mechanism BiFormer and the repetitively weighted bidirectional feature pyramid network BiFPN, the present invention effectively improves the feature extraction capability of fish targets, especially in scenarios with dense occlusion or small target detection. These technical means help the model more accurately identify fish targets in complex backgrounds, especially in cases of overlapping or partial occlusion, showing significant advantages.
[0062] (5) The present invention improves the accuracy of the model during training by combining improved loss functions (including bounding box regression loss and quality-aware classification loss). This loss function allows the model to focus more on the accuracy of target detection during optimization, effectively reducing missed detections and false detections, and improving fish detection performance in complex environments such as the Yangtze River Basin. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] Figure 1 This is a schematic diagram of Coilia ectenes data enhancement according to an embodiment of the present invention;
[0064] Figure 2 This is a diagram of the overall network structure of the improved YOLOv8 according to an embodiment of the present invention;
[0065] Figure 3 This is a flowchart of the BiFormer attention mechanism workflow of an embodiment of the present invention;
[0066] Figure 4 This is a diagram of the BiFormer architecture of an embodiment of the present invention;
[0067] Figure 5 GhostConv principle diagram of an embodiment of the present invention
[0068] Figure 6 This is a diagram of a network structure that integrates different features according to an embodiment of the present invention;
[0069] Figure 7 This is a PR curve diagram of the improved YOLOv8n model according to an embodiment of the present invention. DETAILED DESCRIPTION
[0070] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of the present invention.
[0071] Example 1:
[0072] This embodiment provides a fish detection method based on improved YOLOv8n, including the following steps:
[0073] Build a fish detection model, train the fish detection model, and use the trained fish detection model to detect fish images in the Yangtze River Basin;
[0074] Among them, the fish detection model is an improved YOLOv8n model, which includes an input layer, a backbone network, a neck network and a detection head connected in sequence. The input end uses the Mosaic method and the random occlusion method for data enhancement, and the input image is randomly scaled, cropped, brightness adjusted and spliced to form a new input map; the improvements include replacing the Conv convolution layer with GhostConv phantom convolution, adding a two-layer link attention mechanism (Bidirectional RelativeAttention) and a repeatedly weighted bidirectional feature pyramid network (BiFPN) between the backbone network and the neck network.
[0075] The input end uses the Mosaic method and random occlusion method for data enhancement. The input image is randomly scaled, cropped, brightness adjusted, and spliced to form a new input image. Specifically, it includes:
[0076] Randomly select 4 images from the original fish image set, read their sizes respectively, and obtain the original width W of each image i and height H i ;
[0077] According to the unified input size of the target image, the four images are scaled separately, and the scaling ratio is s i Calculated using the following formula:
[0078]
[0079] Among them, s i is the scaling factor of the i-th image, W i 、H i are the width and height of the original image, W T 、H T The target width and height of the target stitched image;
[0080] After scaling, each image is randomly cropped, and the four cropped images are stitched into a new image in a cross structure using Mosaic. During the stitching process, the horizontal and vertical split points are randomly selected, and the stitching area ratio is controlled between 0.3 and 0.7.
[0081] Perform brightness perturbation on the stitched new image to simulate different lighting conditions. The processing method is as follows:
[0082] I′(x,y)=α·I(x,y)+β
[0083] Where I(x,y) is the original pixel value, α∈[0.7,1.3] is the brightness adjustment coefficient, and β∈[-20,20] is the brightness offset;
[0084] A random occlusion operation is introduced into the brightness-adjusted image to generate 1 to 5 occlusion blocks. The pixel values in the occlusion blocks are set to 0 or Gaussian noise is added.
[0085] Normalize the image after the occlusion operation.
[0086] like Figure 1 As shown, this example collected images of nine important Yangtze River fish species from iNaturalist and other public databases: Chinese sturgeon, silver carp, bighead carp, razorfish, grass carp, black carp, crucian carp, mandarin fish, and yellow catfish. These images come from various environments and scenes, including fish swimming in the water, caught in fishing nets, and living in water tanks. To improve the generalization and robustness of the model, this example expanded the image dataset of Yangtze River fish, including black carp, grass carp, and razorfish. Image enhancement techniques such as rotation, translation, mirroring, Gaussian noise addition, brightness adjustment, random occlusion (cutout), and cropping were used. These methods increase the diversity of the dataset by transforming images from multiple angles, scales, and noise characteristics, helping the model better cope with changes in real-world applications. After data augmentation, the total number of images reached 4,427, which were divided into a training set (3,530 images), a validation set (442 images), and a test set (441 images) in an 8:1:1 ratio.
[0087] like Figure 2 As shown in Figure 1, the overall network architecture of YOLOv8n consists of four main components: the input layer (Input), the backbone network (Backbone), the neck network (Neck), and the detection head (Head). YOLOv8 inherits the classic strategies of the YOLO series and introduces several new technologies to further enhance model performance. These improvements comprehensively optimize YOLOv8 in terms of accuracy and speed, making it suitable for a variety of object detection tasks.
[0088] The backbone network includes a first ghost convolution module (GhostConv), a second ghost convolution module, a first C2F module, a third ghost convolution module, a second C2F module, a fourth ghost convolution module, a third C2F module, a fifth ghost convolution module, a fourth C2F module, and a first SPPF module, which are connected in sequence.
[0089] The neck network includes a first upsampling module (Upsample), a first connection module (Concat), a fifth C2F module, a second upsampling module, a second connection module, a sixth C2F module, a third connection module, a seventh C2F module, a sixth phantom convolution module, a fourth connection module, an eighth C2F module, and a seventh phantom convolution module, which are connected in sequence.
[0090] The backbone network detection head includes a first output module (Detect), a second output module, and a third output module.
[0091] The output end of the third phantom convolution module of the backbone network is connected to the input end of the second connection module of the neck network, the output end of the third C2F module of the backbone network is connected to the input end of the first connection module, the output end of the first SPPF module of the backbone network is connected to the first upsampling module of the neck network, and the fifth C2F module of the neck network is connected to the fourth connection module.
[0092] The input end of the repeated weighted bidirectional feature pyramid network BiFPN is connected to the output end of the first SPPF module of the backbone network and the output end of the seventh phantom convolution module of the neck network respectively. The neck network also includes a ninth C2F module. The output end of the repeated weighted bidirectional feature pyramid network BiFPN is connected to the input end of the ninth C2F module, and the output end of the ninth C2F module is connected to the input end of the two-layer link attention mechanism BiFormer.
[0093] The output end of the dual-layer link attention mechanism BiFormer is connected to the input end of the third output module, the output end of the seventh C2F module of the neck network is connected to the first output module, and the output end of the eighth C2F module of the neck network is connected to the second output module.
[0094] like Figure 3 As shown in Figure 2, the two-layer link attention mechanism BiFormer includes four layers of BRA modules connected in sequence. The BRA module includes the following data processing steps:
[0095] The input image is embedded in blocks to obtain the initial feature map F0. The processing method is to input the image into the depth-separable convolution operation, use the convolution kernel size of 3×3, and perform the convolution operation on the original image I to obtain the implicitly encoded initial feature map F0:
[0096] F0=Conv 3×3 (I)
[0097] Among them, I represents the input image, Conv 3×3 It represents the depth-separable convolution operation using a 3×3 convolution kernel, and F0 represents the initial feature map of the output;
[0098] Input the initial feature map F0 into the relative position encoding module, extract the relative position information, and obtain the position embedding feature P0:
[0099] P0=F0+RelPos
[0100] Among them, RelPos represents the position encoding tensor constructed by the predefined relative position offset table, and P0 is the position embedding feature after adding position information;
[0101] Input the position embedding feature P0 into the attention module, calculate the query matrix Q, key matrix K, value matrix V, and calculate the attention weight matrix A according to the bidirectional relative attention mechanism to obtain the weighted feature map F1:
[0102] Q=W Q P0,K=W K P0,V=W V P0
[0103]
[0104] Among them, W Q 、W K 、W V is a learnable linear projection matrix, d k is the key vector dimension, B is the position offset matrix, A is the attention weight matrix, and F1 is the weighted feature map after weighting;
[0105] The weighted feature map F1 is input into the two-layer perceptron for nonlinear feature conversion. The first layer of perceptron performs linear transformation and activation function processing to obtain the intermediate representation H1:
[0106] H1=ReLU(W1F1+b1)
[0107] Among them, W1 is the first layer fully connected weight matrix, b1 is the bias vector, ReLU is the rectification activation function, and H1 is the output feature after activation;
[0108] Input the intermediate feature map H1 into the second layer of perceptron, perform linear transformation, and obtain the output feature map H2:
[0109] H2=W2H1+b2
[0110] Among them, W2 is the fully connected weight matrix of the second layer, b2 is the bias of the second layer, and H2 is the feature map output by the two-layer perceptron;
[0111] Perform element-wise addition of the perceptron output feature map H2 and the weighted feature map F1 to obtain the final output feature map F of the BRA module. BRA :
[0112] F BRA =F1+H2
[0113] Among them, F BRA Represents the output features after fusing attention information and nonlinear transformation.
[0114] The loss function of the fish detection model is:
[0115]
[0116] in, is the loss function of the fish detection model, λ bbox ,λ QFL is the weight coefficient, is the bounding box regression loss, is the quality-aware classification loss;
[0117] The bounding box regression loss is:
[0118]
[0119] Among them, B is the bounding box predicted by the model, B gt is the true target bounding box, CIoU(B,B gt ) is the intersection-over-union (IoU) of the predicted bounding box and the true target bounding box;
[0120] The quality-aware classification loss is:
[0121]
[0122] Among them, C is the number of fish species, p c is the model’s predicted probability for the cth class, q c is the quality label of category c. If the current sample is a positive sample of category c, that is, the current sample belongs to category c, q c =IoU(B,B gt ), if the current sample does not belong to the cth category, then q c =0, a is the adjustment factor.
[0123] like Figure 4As shown in the figure, A is the overall BiFormer framework, and B is the BRA (Bidirectional Relative Attention) module diagram. BiFormer, with the BRA (Bidirectional Relative Attention) module at its core, adopts a four-layer pyramidal structure. In the first stage, the image is encoded using overlapping block embedding. In the following three stages, the image merging module gradually reduces the resolution and increases the number of channels, achieving multi-level feature representation. Finally, the BiFormer module performs deep feature processing. Each BiFormer block begins with a 3×3 depthwise separable convolution to implicitly encode position information, preserving spatial structure. The BRA module captures bidirectional relative attention, promoting efficient interaction between local and global features and improving attentional focusing. Finally, a two-layer perceptron (MLP) is used for relationship modeling, further enhancing position information and contextual understanding. This multi-stage feature processing, attention mechanism, and position information embedding improve the model's feature extraction and contextual representation, particularly for small object detection in complex scenes.
[0124] The BiFormer architecture is built around the BRA module (Bidirectional Relative Attention) as its core building block and features a four-layer pyramid structure. In the first stage, overlapping image block embedding is used to divide the input image into blocks and encode embedded features. In the second, second, third, and fourth stages, an image merging module is introduced to achieve multi-level feature representation by gradually reducing image resolution and increasing channel capacity. Finally, the cascaded BiFormer modules perform deep transformations and processing on the input data's features. This design further optimizes feature extraction and representation. Each BiFormer block begins by implicitly encoding the image's positional information through a 3×3 depthwise separable convolution. This encoding preserves spatial structure and provides foundational support for subsequent feature processing. Subsequently, the core BRA module captures bidirectional relative attention (Bidirectional Relative Attention) to achieve efficient interaction between local and global features, thereby enhancing the model's attentional focus. Finally, a two-layer perceptron (MLP) is used to model relationships and further embed positional information to optimize feature representation completeness and contextual understanding. This architecture, through multi-stage feature processing, an effective attention mechanism, and position information embedding, provides the model with more efficient feature extraction capabilities and richer contextual information representation, significantly improving performance in object detection tasks. BiFormer's modular design enables flexible adaptation to different task requirements, demonstrating exceptional performance in small object detection in complex scenarios.
[0125] like Figure 5 As shown in the figure, the convolution module in the YOLOv8 model is responsible for feature extraction and fusion. However, traditional convolution operations produce redundant feature maps and have high computational and memory requirements, which affect the model's detection speed. To this end, GhostConv was introduced to optimize computational efficiency. GhostConv significantly reduces computational complexity and memory usage by generating a small number of "ghost features" while maintaining good performance. After applying GhostConv to YOLOv8, the model's inference speed is improved, especially on embedded devices and mobile terminals, demonstrating stronger real-time detection capabilities. Its linear computation strategy reduces redundant computation while effectively maintaining feature expression capabilities. This improvement enables YOLOv8 to achieve a good balance between high performance and low resource consumption, enhancing the feasibility of real-time detection tasks.
[0126] In the detection of fish and aquatic products images, the scale change of the detection target is a key issue. Figure 6 As shown in the figure, A represents FPN, B represents PAN, and C represents BiFPN. BiFPN (Bidirectional Feature Pyramid Networks) is an efficient feature pyramid network (FPN). The YOLOv8 model uses an FPN+PAN structure for multi-layer feature fusion, fusing multi-scale features through lateral connections and a pyramid-like hierarchy. However, due to the varying resolutions of the input features, the contribution of the FPN+PAN structure to the fused output features is often uneven, and features across different scales are not fully utilized, resulting in increased model parameters and computational complexity. While preserving surface semantics, deeper semantic information may also be lost. To address this issue, our research introduces a Bidirectional Feature Pyramid Network (BiFPN) module. BiFPN performs well in multi-scale object detection tasks. By introducing a bidirectional feature fusion mechanism, BiFPN improves the expressive power of features at different scales. Compared to traditional FPN structures, it can better fuse cross-scale information. The weighted bidirectional feature pyramid significantly improves feature fusion efficiency. The initial step of BiFPN is to eliminate nodes with a single input edge and limited contribution. This process significantly reduces network complexity. In addition, BiFPN improves the feature fusion efficiency of multi-scale objects by increasing horizontal connections and repeated feature integration.
[0127] Example 2:
[0128] Experimental Analysis: This example comprehensively evaluates the recognition performance of the improved YOLOv8 model on different aquatic product categories, and analyzes the model's mAP50, Precision, and Recall values and the corresponding confusion matrix in detail to demonstrate its performance in practical applications. In order to comprehensively measure the detection capability of the model, three core indicators, Precision, Recall, and mAP50, were selected for analysis. The recognition performance of the model on different categories is summarized, including the mAP50, Precision, and Recall values of various aquatic product categories such as black carp, grass carp, and razorfish. Through this evaluation, it can be seen that the improved model shows good detection performance in all categories and can more accurately identify the main aquatic product categories.
[0129] Table 1. Evaluation indicators of each category of YOLOv8 improved model
[0130]
[0131] In this example, an image recognition system for important fish species in the Yangtze River basin, such as black carp, grass carp, and razorback cory, was constructed based on the YOLOv8 model. To verify the model's effectiveness in fish recognition tasks, evaluating its performance is crucial. This section details the model evaluation method and its performance metrics, including the definitions and calculations of commonly used evaluation metrics such as precision, recall, F1 score, and mean average precision (mAP). Table 2 shows the results of the ablation experiment presented in this paper.
[0132] Table 2 Ablation experiment results
[0133]
[0134] According to the experimental results in Table 2, ablation experiments show that the introduction of the BiFormer, GhostConv, and BiFPN modules has a significant impact on model performance. After incorporating the BiFormer attention mechanism, the model's average precision (mAP50) improved by 3.2%, precision increased by 0.1%, and recall increased by 3%, while maintaining the same model size. This demonstrates that integrating the BiFormer attention mechanism into the YOLOv8n network significantly improves the model's accuracy, recall, and average precision. However, due to the complex structure of the BiFormer module, the number of model parameters and GFLOPs increased slightly, indicating that the introduction of this module increases the computational resource requirements while improving performance.
[0135] By introducing GhostConv into the Conv module, the model parameters were reduced by 4,900, the model size was reduced by 0.9MB, and GFLOPs dropped by 0.6. However, mAP50 dropped by 0.6% compared to the baseline model, and recall decreased by 5.5%. This result shows that while GhostConv significantly reduces the model's computational complexity and resource consumption, it has a slightly negative impact on recall and average precision. Because GhostConv decomposes the conventional convolution operation into two parts to reduce feature redundancy, this characteristic somewhat reduces the model's ability to capture detailed features.
[0136] After adopting the BiFPN feature fusion network, the model's accuracy decreased by 2.1%, the model size was reduced by 0.4MB, the recall rate increased by 4.5%, and the mAP50 score increased by 1%. This shows that the BiFPN module effectively integrates multi-scale features from different levels through bidirectional cross-scale connections and weighted feature fusion, while reducing the number of parameters and model size, thereby improving the model's recall rate. The weighted feature fusion strategy used by BiFPN assigns higher weights to important features, further optimizing feature expression capabilities and improving object detection performance.
[0137] On this basis, the BiFPN and GhostConv modules were added to the YOLOv8n+BiFormer model, forming the final improved model: YOLOv8n+BiFormer+BiFPN+GhostConv. Compared to the baseline YOLOv8n model, the improved model achieved improvements in precision, recall, and mAP50 by 1.2%, 9.2%, and 4.8%, respectively. Meanwhile, the number of GFLOPs, model parameters, and model size were reduced by 0.7, 000, 0.4, and 0.8MB, respectively. The improved model demonstrated excellent performance and efficiency, validating the effectiveness of the improved modules in optimizing model computational efficiency and detection performance. These results demonstrate that the improved model can provide higher detection accuracy and improved robustness under limited computational resources, and holds great promise for future applications.
[0138] To verify the advantages of the proposed improved YOLOv8 model in lightweight and detection performance, it was compared with various first-order and second-order algorithms, and a comprehensive performance evaluation was performed using six indicators: accuracy, recall, mAP, number of parameters, model size, and GFLOPs.
[0139] Table 3 Performance comparison results of different models on the dataset
[0140]
[0141]
[0142] From the comparative experimental results in Table 3, it can be seen that the improved YOLOv8 model outperforms most other models in terms of prediction accuracy. While maintaining a relatively low number of parameters and model size, the model achieves significantly higher accuracy. The models selected in the comparative experiments in this study include the YOLO series (YOLOv3, YOLOv5, YOLOv6, YOLOv7 and YOLOv8s) and a lightweight model that replaces the default backbone network of YOLOv8 with GhostNet. The results show that the improved YOLOv8 model performs excellently in the mAP50 indicator, which is 1.1% and 5.1% higher than YOLOv8s and GhostNet-v8 respectively, and 16.1% higher than Faster R-CNN. This excellent performance shows that the improved model exhibits stronger adaptability in complex target detection tasks.
[0143] like Figure 7 The PR curve of the improved YOLOv8n model of this embodiment is shown. From the perspective of GFLOPs, the GFLOPs value of the improved model is slightly higher than that of lightweight models (such as YOLOv5s), but significantly lower than YOLOv3, Faster R-CNN, and YOLOv8s. These higher GFLOPs are generally attributed to the more complex network structure and advanced feature extraction modules in the model. However, the improved model achieves a good balance between GFLOPs and detection performance by optimizing module design.
[0144] The improved YOLOv8 model demonstrates a more streamlined parameter count. Compared to the baseline YOLOv8n model, it has 5,000 fewer parameters; compared to the GhostNet-v8 model, it has 3,000 fewer parameters; and its parameter count is comparable to that of YOLOv5s. While GhostNet-v8's extensive use of lightweight convolutions and shallower network depth results in slightly lower model size and GFLOPs, its insufficient network depth results in insufficient recognition accuracy, with mAP50 significantly lower than that of the improved YOLOv8 model. In comparison, the improved model achieves a better balance between performance, compactness, and parameter efficiency.
[0145] The improved model achieves 1.2% improvements in precision, 9.2% in recall, and 4.8% in mAP50 over the baseline YOLOv8n model, respectively. It also excels in computational efficiency, reducing GFLOPs, model parameters, and model size by 7,000, 4,000, and 0.8MB, respectively. Compared to other mainstream object detection models, the improved YOLOv8 model excels in both accuracy and efficiency. In particular, when compared to YOLOv8s, GhostNet-v8, and Faster RCNN, it not only achieves significant improvements in accuracy, but also maintains efficient computational performance while maintaining a low parameter count and model size.
[0146] The improved YOLOv8 model demonstrated balanced performance across different fish recognition tasks, with particularly impressive results for escargot (Eriophorus edulis), achieving a mAP50 of 95.5%. Herring (Black Carp) and grass carp achieved mAP50s of 94.9% and 91.3%, respectively, also demonstrating high recognition accuracy. This improved model demonstrated exceptional performance in fish image recognition tasks in the Yangtze River basin, significantly improving recognition accuracy while also achieving significant progress in model lightweighting and efficiency optimization. This provides strong technical support for fish image recognition and opens up new avenues for aquatic life conservation and resource management.
[0147] Overall, the improved YOLOv8 model performed best in terms of compactness and accuracy. This model achieved superior detection performance with a smaller size and fewer parameters, better meeting the needs of real-world applications. This demonstrates that while other models may have sacrificed accuracy while optimizing for lightweightness, the improved YOLOv8 model achieves significant advantages in performance and practicality, making it particularly valuable in real-world deployments and application scenarios.
[0148] If the above functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0149] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in the present invention, and such modifications or substitutions are intended to be within the scope of protection of the present invention. Therefore, the scope of protection of the present invention shall be subject to the scope of protection of the claims.
Claims
1. A fish detection method based on improved YOLOv8n, characterized in that: The following steps are involved: Build a fish detection model, train the fish detection model, and use the trained fish detection model to detect fish images in the Yangtze River Basin; Among them, the fish detection model is an improved YOLOv8n model, including an input layer, a backbone network, a neck network and a detection head connected in sequence. The input end uses the Mosaic method and the random occlusion method for data enhancement, and the input image is randomly scaled, cropped, brightness adjusted and spliced to form a new input image; the improvement includes replacing the Conv convolution layer with GhostConv phantom convolution, adding a two-layer link attention mechanism BiFormer and a repeatedly weighted bidirectional feature pyramid network BiFPN between the backbone network and the neck network.
2. A fish detection method based on improved YOLOv8n according to claim 1, characterized in that, The input end uses the Mosaic method and the random occlusion method to perform data enhancement, and randomly scales, crops, adjusts brightness, and splices the input image to form a new input image, specifically including: Randomly select 4 images from the original fish image set, read their sizes respectively, and obtain the original width W of each image i and height H i ; According to the unified input size of the target image, the four images are scaled separately, and the scaling ratio is s i Calculated using the following formula: Among them, s i is the scaling factor of the i-th image, W i 、H i are the width and height of the original image, W T 、H T The target width and height of the target stitched image; After scaling, each image is randomly cropped, and the four cropped images are stitched into a new image in a cross structure using Mosaic. During the stitching process, the horizontal and vertical split points are randomly selected, and the stitching area ratio is controlled between 0.3 and 0.
7. Perform brightness perturbation on the stitched new image to simulate different lighting conditions. The processing method is as follows: I′(x,y)=α·I(x,y)+β Where I(x,y) is the original pixel value, α∈[0.7,1.3] is the brightness adjustment coefficient, and β∈[-20,20] is the brightness offset; A random occlusion operation is introduced into the brightness-adjusted image to generate 1 to 5 occlusion blocks. The pixel values in the occlusion blocks are set to 0 or Gaussian noise is added. Normalize the image after the occlusion operation.
3. A fish detection method based on improved YOLOv8n according to claim 1, characterized in that, The backbone network includes a first phantom convolution module, a second phantom convolution module, a first C2F module, a third phantom convolution module, a second C2F module, a fourth phantom convolution module, a third C2F module, a fifth phantom convolution module, a fourth C2F module, and a first SPPF module, which are connected in sequence.
4. A fish detection method based on improved YOLOv8n according to claim 1, characterized in that, The neck network includes a first upsampling module, a first connection module, a fifth C2F module, a second upsampling module, a second connection module, a sixth C2F module, a third connection module, a seventh C2F module, a sixth phantom convolution module, a fourth connection module, an eighth C2F module, and a seventh phantom convolution module, which are connected in sequence.
5. A fish detection method based on improved YOLOv8n according to claim 1, characterized in that, The backbone network detection head includes a first output module, a second output module, and a third output module.
6. A fish detection method based on improved YOLOv8n according to claim 3, 4 or 5, characterized in that: The output end of the third phantom convolution module of the backbone network is connected to the input end of the second connection module of the neck network, the output end of the third C2F module of the backbone network is connected to the input end of the first connection module, the output end of the first SPPF module of the backbone network is connected to the first upsampling module of the neck network, and the fifth C2F module of the neck network is connected to the fourth connection module.
7. A fish detection method based on improved YOLOv8n according to claim 1, 3 or 4, characterized in that: The input end of the repeated weighted bidirectional feature pyramid network BiFPN is respectively connected to the output end of the first SPPF module of the backbone network and the output end of the seventh phantom convolution module of the neck network. The neck network also includes a ninth C2F module. The output end of the repeated weighted bidirectional feature pyramid network BiFPN is connected to the input end of the ninth C2F module, and the output end of the ninth C2F module is connected to the input end of the dual-layer link attention mechanism BiFormer.
8. A fish detection method based on improved YOLOv8n according to claim 1, 4 or 5, characterized in that: The output end of the dual-layer link attention mechanism BiFormer is connected to the input end of the third output module, the output end of the seventh C2F module of the neck network is connected to the first output module, and the output end of the eighth C2F module of the neck network is connected to the second output module.
9. A fish detection method based on improved YOLOv8n according to claim 1, characterized in that, The dual-layer link attention mechanism BiFormer includes four layers of sequentially connected BRA modules, and the BRA module includes the following data processing process: The input image is embedded in blocks to obtain the initial feature map F0. The processing method is to input the image into the depth-separable convolution operation, use the convolution kernel size of 3×3, and perform the convolution operation on the original image I to obtain the implicitly encoded initial feature map F0: F0=Conv 3×3 (I) Among them, I represents the input image, Conv 3×3 It represents the depth-separable convolution operation using a 3×3 convolution kernel, and F0 represents the initial feature map of the output; Input the initial feature map F0 into the relative position encoding module, extract the relative position information, and obtain the position embedding feature P0: P0=F0+RelPos Among them, RelPos represents the position encoding tensor constructed by the predefined relative position offset table, and P0 is the position embedding feature after adding position information; Input the position embedding feature P0 into the attention module, calculate the query matrix Q, key matrix K, value matrix V, and calculate the attention weight matrix A according to the bidirectional relative attention mechanism to obtain the weighted feature map F1: Q=W Q P0,K=W K P0,V=W V P0 Among them, W Q 、W K 、W V is a learnable linear projection matrix, d k is the key vector dimension, B is the position offset matrix, A is the attention weight matrix, and F1 is the weighted feature map after weighting; The weighted feature map F1 is input into the two-layer perceptron for nonlinear feature conversion. The first layer of perceptron performs linear transformation and activation function processing to obtain the intermediate representation H1: H1=ReLU(W1F1+b1) Among them, W1 is the first layer fully connected weight matrix, b1 is the bias vector, ReLU is the rectification activation function, and H1 is the output feature after activation; Input the intermediate feature map H1 into the second layer of perceptron, perform linear transformation, and obtain the output feature map H2: H2=W2H1+b2 Among them, W2 is the fully connected weight matrix of the second layer, b2 is the bias of the second layer, and H2 is the feature map output by the two-layer perceptron; Perform element-wise addition of the perceptron output feature map H2 and the weighted feature map F1 to obtain the final output feature map F of the BRA module. BRA : F BRA =F1+H2 Among them, F BRA Represents the output features after fusing attention information and nonlinear transformation.
10. A fish detection method based on improved YOLOv8n according to claim 1, characterized in that, The loss function of the fish detection model is: in, is the loss function of the fish detection model, λ bbox ,λ QFL is the weight coefficient, is the bounding box regression loss, is the quality-aware classification loss; The bounding box regression loss is: Among them, B is the bounding box predicted by the model, B gt is the true target bounding box, CIoU(B,B gt ) is the intersection-over-union (IoU) of the predicted bounding box and the true target bounding box; The quality-aware classification loss is: Among them, C is the number of fish species, p c is the model’s predicted probability for the cth class, q c is the quality label of category c. If the current sample is a positive sample of category c, that is, the current sample belongs to category c, q c =IoU(B,B gt ), if the current sample does not belong to the cth category, then q c =0, a is the adjustment factor.
Citation Information
Patent Citations
A method for detecting densely occluded fish based on YOLOv5 network
CN114782759B
Cited By
Classroom concentration detection method and system based on machine vision
CN121354210A