A water surface target detection method and system
By improving the YOLOv5 model and Wise-IoU loss function, and combining the CBAM attention module and image enhancement technology, the problem of distinguishing between rigid debris and soft algae in water surface target detection was solved, improving recognition accuracy and robustness.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HEFEI UNIV OF TECH
- Filing Date
- 2026-01-14
- Publication Date
- 2026-06-05
Smart Images

Figure CN122156575A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of target detection technology, and specifically to a water surface target detection method based on the collaborative optimization of attention mechanism and loss function. Background Technology
[0002] With the continued acceleration of industrialization, global water pollution has become increasingly severe. The direct discharge of large amounts of untreated industrial and domestic wastewater into water bodies, along with people's indiscriminate littering, are the main factors contributing to the dramatic increase in floating debris. These improper practices not only indirectly cause eutrophication, promoting the reproduction of various microorganisms and leading to rampant algae growth, thus disrupting the original ecological balance of aquatic bodies; but also, plastic waste, when left in water for extended periods, is difficult to degrade and gradually breaks down into tiny particles. When ingested by aquatic organisms, these particles accumulate in their bodies, affecting their growth, reproduction, and even survival, thereby disrupting the balance of the entire food chain.
[0003] In existing technologies for detecting floating objects on the water surface, deep learning has become the mainstream method. Models such as YOLO and Faster R-CNN, trained on large-scale datasets, can effectively capture key features such as the shape and texture of floating objects, and have high detection accuracy and efficiency under normal lighting conditions. However, existing methods still have several limitations: on the one hand, most models fail to fully consider the significant differences in shape and scale between "rigid debris" and "soft algae" in water surface targets, affecting the ability to distinguish between multiple categories of targets; on the other hand, complex backgrounds such as water surface reflection and wave interference can easily lead to feature confusion, resulting in varying recognition accuracy for some floating objects, making it difficult to meet the accuracy requirements of actual water surface detection.
[0004] Therefore, there is an urgent need for a water surface target recognition algorithm that can adapt to complex water surface environments, has high discriminative power and robustness, in order to solve the above-mentioned technical problems. Summary of the Invention
[0005] The technical solution of this invention is used to solve the problem of unclear identification of targets on the water surface and improve the identification accuracy.
[0006] The present invention solves the above-mentioned technical problems through the following technical means: A method for detecting water surface targets, comprising: Collect water surface image data, divide the image data into small rigid body data and large soft body data, perform K-means clustering on each, and generate rigid anchor frame groups and flexible anchor frame groups. An improved YOLOv5 was constructed: the shallow detection head of YOLOv5 was embedded in the SA module of CBAM, and the deep detection head of YOLOv5 was embedded in the CA module of CBAM; the Wise-IoU loss function module was integrated into the loss calculation part of YOLOv5. The rigid anchor frame group is mapped to the shallow and medium layer detectors of YOLOv5, and the flexible anchor frame group is mapped to the deep layer detector of YOLOv5; the improved YOLOv5 is used to train small rigid body data and large soft body data to obtain the trained target model. The target model was used to detect surface debris and algae.
[0007] Furthermore, in the improved YOLOv5, the shallow detection head is embedded in the SA module after the C3 module; the deep detection head is embedded in the CA module after the C3 module; this asymmetric design is maintained in the FPN+PAN structure.
[0008] Furthermore, the specific process of integrating the Wise-IoU loss function module into the loss calculation part of YOLOv5 is as follows: define the Wise-IoU calculation module in the metric tool file; modify the original bounding box intersection-union function and intersection-union variable in the loss calculation file; set different focusing coefficients for different object levels to form a hierarchical cascaded loss function, and use the integrated loss function to train the model.
[0009] Furthermore, the process of detecting surface debris and algae based on the target model is as follows: In the target detection process, the K-means clustering algorithm is used to segment the input image, dividing the image pixels into reflection areas, water surface areas and background areas; The identified reflective areas are processed by mean shift filtering to smooth homogeneous regions while preserving edge information. Using the Laplace pyramid decomposition and reconstruction technique, multi-scale processing is performed on both reflective and non-reflective regions. The final de-reflection image is obtained by performing secondary segmentation based on OpenCV and combining it with morphological operations.
[0010] The present invention also improves a surface target detection system, comprising: Data acquisition and processing module: Acquires water surface image data, divides the image data into small rigid body data and large soft body data, performs K-means clustering on each, and generates rigid anchor frame groups and flexible anchor frame groups. Model building module: Building an improved YOLOv5: embedding the shallow detection head of YOLOv5 into the SA module of CBAM, and embedding the deep detection head of YOLOv5 into the CA module of CBAM; integrating the Wise-IoU loss function module into the loss calculation part of YOLOv5; Model training module: Map rigid anchor frame groups to the shallow and medium layer detectors of YOLOv5, and map flexible anchor frame groups to the deep layer detectors of YOLOv5; use the improved YOLOv5 to train small rigid body data and large soft body data to obtain the trained target model. Model application module: Detects surface debris and algae based on the target model.
[0011] Furthermore, in the improved YOLOv5, the shallow detection head is embedded in the SA module after the C3 module; the deep detection head is embedded in the CA module after the C3 module; this asymmetric design is maintained in the FPN+PAN structure.
[0012] Furthermore, the specific process of integrating the Wise-IoU loss function module into the loss calculation part of YOLOv5 is as follows: define the Wise-IoU calculation module in the metric tool file; modify the original bounding box intersection-union function and intersection-union variable in the loss calculation file; set different focusing coefficients for different object levels to form a hierarchical cascaded loss function, and use the integrated loss function to train the model.
[0013] Furthermore, the process of detecting surface debris and algae based on the target model is as follows: In the target detection process, the K-means clustering algorithm is used to segment the input image, dividing the image pixels into reflection areas, water surface areas and background areas; The identified reflective areas are processed by mean shift filtering to smooth homogeneous regions while preserving edge information. Using the Laplace pyramid decomposition and reconstruction technique, multi-scale processing is performed on both reflective and non-reflective regions. The final de-reflection image is obtained by performing secondary segmentation based on OpenCV and combining it with morphological operations.
[0014] The present invention also provides a processing device, including at least one processor and at least one memory communicatively connected to the processor, wherein: the memory stores program instructions executable by the processor, and the processor can execute the above-described method by calling the program instructions.
[0015] The present invention also provides a computer-readable storage medium storing computer instructions that cause the computer to perform the methods described above.
[0016] The advantages of this invention are: 1. This invention uses a differentiated multi-scale perception architecture for rigid and flexible targets. Through category-aware anchor box clustering, asymmetric attention feature fusion, and an adaptive Wise-IoU strategy, it establishes a linkage mapping relationship of "target scale - detection level - loss focus", thereby effectively improving the distinguishability between water surface debris and green algae and improving the recognition accuracy.
[0017] 2. The CA and SA attention modules and the Wise-IoU loss function module of this invention, by integrating them into the neural network architecture, allow CA and SA to adaptively enhance features important for water surface target recognition and suppress irrelevant features, thereby improving the model's feature representation ability and recognition accuracy for water surface targets. Wise-IoU automatically adjusts the loss weights based on the IoU (Intersection over Union) between the anchor box and the target box. The model trained by combining these two modules improves the accuracy of water surface target recognition compared to the original model, solving the problem of relatively inaccurate target detection and improving recognition precision.
[0018] 3. In the target detection process, this invention introduces image enhancement technology, using K-means clustering algorithm, mean shift filtering, Laplacian pyramid decomposition and recombination technology and morphological operations to enhance the input image, effectively reducing interference from water surface reflection and improving the accuracy and reliability of water surface target detection results. Attached Figure Description
[0019] Figure 1 This is a flowchart of the method in an embodiment of the present invention; Figure 2 This is a schematic diagram of the overall structure of the asymmetric feature network in an embodiment of the present invention; Figure 3 This is a comparative schematic diagram of the traditional CBAM structure and the asymmetric feature enhancement structure in the embodiment of the present invention; Figure 4 This is a schematic diagram illustrating the physical semantic differences of feature maps at different levels after identification using the method in this embodiment of the invention. Detailed Implementation
[0020] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below in conjunction with the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0021] This embodiment provides a water surface target detection method based on the collaborative optimization of attention mechanism and loss function, such as Figure 1As shown, it includes the following steps: S1. Acquire and label water surface image data containing debris and algae to construct a benchmark dataset for water surface target detection. Specifically: S1-1: Collect images that can be used to construct the dataset through methods such as screening and adapting to public datasets, on-site shooting and collection, and compliant online collection and screening.
[0022] S1-2: Use labeling tools, such as labelimg, to mark the targets that need to be identified, such as debris or algae on the water surface.
[0023] S1-3: Store files and data in a specific format to facilitate their use in neural network training and form a dataset.
[0024] S2 divides the dataset into two groups: "small rigid bodies" (bottles) and "large soft bodies" (algae). K-means clustering is performed on each group to generate two sets of specific anchors. An asymmetric attention mechanism is introduced, integrating a spatial attention module (SA) at a shallow layer and a channel attention module (CA) at a deep layer to achieve an attention mechanism based on the physical semantics of feature maps at different levels. For example... Figure 2 , Figure 3 As shown, specifically: S2-1: Traverse the dataset, distinguish the labels, and perform K-means clustering for each label.
[0025] S2-2: Establish an asymmetric feature enhancement network and introduce SA and CA modules. S2-3: In the FPN+PAN structure, maintaining this asymmetric design allows the network to exhibit the characteristics of "shallow layers emphasizing spatial information and deep layers emphasizing semantic information".
[0026] S2-4: Train the model using the modified configuration file.
[0027] It should be further explained that in step S2, the K-means clustering includes: The dataset is traversed, and the labels are divided into two groups: Sr (plastic bottles, aluminum cans) and Ss (green algae, floating plants). K-means clustering is performed on Sr and Ss. Based on the anchor frame area size, "rigid anchor frames" are forcibly mapped to the P3 and P4 layers (shallow and mid-level detectors) of YOLOv5, and "flexible anchor frames" are mapped to the P5 layer (deep detector).
[0028] It needs to be further explained that in step S2, such as Figure 4 As shown, the asymmetric attention mechanism includes: Shallow features (P3 layer - corresponding to small objects): An SA module is embedded after the C3 module. Spatial masks are used to suppress edge interference from surrounding water ripples.
[0029] Deep features (P5 layer - corresponding to green algae): After the C3 module, the CA module is embedded. Channels representing "green plants" are filtered out, ignoring shape information.
[0030] Subsequently, in the FPN+PAN structure, this asymmetric design is maintained, which makes the network exhibit the characteristics of "shallow layers emphasizing spatiality and deep layers emphasizing semantics".
[0031] The calculation process of the Channel Attention (CA) mechanism module is as follows: .
[0032] This represents the global average pooling along the channel direction. This represents global max pooling in the channel direction, and MLP represents multilayer sensing mechanism. This represents the weight matrix of the MLP. Sigmoid function For ReLU function, This represents the channel attention weight.
[0033] The computation process of the Spatial Attention Mechanism (SA) module is as follows: =σ( ([AvgPool(F);MaxPool(F)])) Where F represents the input feature map, and AvgPool() and MaxPool() represent average pooling and max pooling along the channel dimension, respectively. Represents convolution. For the Sigmoid function, Represents a spatial attention map.
[0034] S3 integrates the Wise-IoU loss function module into the model's loss calculation, employing an adaptive Wise-IoU strategy to improve the model's generalization ability and robustness by dynamically adjusting the gradient gain of non-ideal anchor boxes. More aggressive focusing parameters are used in layers P3 and P4 (small to medium targets), while smoother parameters are used in layer P5 (large targets), forming a hierarchical cascaded loss function. Specifically: S3-1: Define the Wise-IoU calculation module in the metric tool file.
[0035] S3-2: Modify the original bounding box cross-union function and cross-union variable in the loss calculation file.
[0036] S3-3: Set different focus coefficients for different object layers.
[0037] S3-4: Forming a hierarchical cascaded loss function S3-5: Train the model using the ensembled loss function.
[0038] It should be further explained that in step S3, the integration of the Wise-IoU loss function module includes: Define the Wise-IoU calculation module in the metric tool file; modify the original bounding box cross-union function and cross-union variable in the loss calculation file; train the model using the ensembled loss function.
[0039] Among them, wise-iou constructs a new boundary loss calculation framework, and the calculation process is as follows:
[0040]
[0041] in, Describes the loss function of Wise-IoU v1. Indicates the basic IoU loss. This represents the dynamic reweighting factor; exp() is the exponential function; x, y represent the coordinates of the center point of the prediction box. , This represents the coordinates of the center point of the true bounding box. The square of the Euclidean distance from the center point This represents the actual width and height of the bounding box. Represents the square of the actual frame size.
[0042] It should be further explained that in step S3, the adaptive Wise-IoU strategy includes: P3 detector head (small object): Set a higher focusing factor because the boundary is clear.
[0043] P5 detector head (green algae): Set a lower focusing coefficient because the boundaries of green algae are blurry (soft).
[0044] S4, based on the benchmark dataset constructed in S1, jointly trains the object detection model that includes a hierarchical attention mechanism and a hierarchical loss function to obtain the trained model parameters; specifically: S4-1: Import the previously integrated dataset in a specific format into the training file.
[0045] S4-2: Adjust various training parameters of the training file, such as batch_size, to improve training results.
[0046] S4-3: Run the training file, train the neural network, and finally generate weights.
[0047] S5 uses the target detection model trained in S4 to detect targets in water surface images, and performs image enhancement processing on the input image during detection, finally outputting the target detection results for water surface debris and algae; specifically: S5-1: Use the K-means clustering algorithm to segment the input image, dividing the image pixels into reflection areas, water surface areas, and background areas.
[0048] S5-2: For the reflection area obtained in step S5-1, perform mean shift filtering to smooth the homogeneous area while preserving edge information.
[0049] S5-3: Perform Laplace pyramid multi-scale processing on both the reflective and non-reflective regions.
[0050] S5-4: Use OpenCV for secondary segmentation and combine it with morphological operations to obtain a de-reflected image, and pass it to the model for detection to obtain the final detection result.
[0051] The objective of K-means is to minimize the sum of squared errors within a cluster, which is the sum of the squared distances from each point to the center of its cluster, as shown in the following formula:
[0052]
[0053] k is the number of clusters Let x be the point set of the i-th cluster, and x be a subset of the i-th cluster. Data points, It is the centroid of the i-th cluster. Represents the relationship between data point x and cluster center Squared Euclidean distance between Furthermore, the K-means clustering algorithm includes: The image is converted into a one-dimensional feature vector and normalized; K clusters with similar features are generated using a clustering algorithm; the clustering results are mapped back to the original image size to generate an initial segmentation mask.
[0054] The decomposition and reconstruction of the Laplace pyramid includes: The image is decomposed into a high-frequency detail layer and a low-frequency smoothing layer; the low-frequency layer is retained in the reflective region while the high-frequency layer is weakened; the high-frequency layer is retained in the non-reflective region while the low-frequency layer is strengthened. The resulting image is a preliminary segmented image with smooth reflective regions and clear non-reflective regions.
[0055] The purpose of Laplacian pyramid decomposition is to decompose the source image into different spatial frequency bands, which is equivalent to bandpass filtering. Let the first... Layer Image The calculation formula is as follows: ;
[0056] (0) <l≤N,0≤i< ,0≤j< ) This indicates a low-resolution image. This represents the image after upsampling and convolution, where i and j represent the pixel coordinates of the icon image. This represents high-frequency details, where m and n represent the kernel indices. This indicates the weights of the upsampling convolution kernel, and 4 represents the amplification factor. This indicates the low-resolution coordinates corresponding to the sample.
[0057] The secondary segmentation and morphological operations include: Based on brightness features, Canny edge detection is used to delineate reflective regions, and the mask shape is repaired through morphological operations. The Canny edge detection algorithm requires image grayscale conversion and Gaussian filtering; the calculation formula is as follows: Grayscale conversion: Gray = (R + G + B) / 3; Gaussian filtering:
[0058] in, These are the Gaussian normalization coefficients, and k represents the convolution kernel size parameter. , This represents the offset distance measured from the center of the kernel. This represents the squared Euclidean distance. This represents the standard deviation of a Gaussian distribution. It is a standardization factor.
[0059] The method provided in this embodiment targets a differentiated multi-scale perception architecture for rigid and flexible targets. Through category-aware anchor box clustering, asymmetric attention feature fusion, and an adaptive Wise-IoU strategy, it establishes a linked mapping relationship between "target scale - detection level - loss focus," effectively improving the distinguishability between surface debris and algae, and enhancing recognition accuracy. The CA and SA attention modules and the Wise-IoU loss function module are integrated into the neural network architecture. CA and SA adaptively strengthen features important for surface target recognition and suppress irrelevant features, thereby improving the model's feature representation ability and recognition accuracy for surface targets. Wise-IoU automatically adjusts the loss weights based on the IoU (Intersection over Union) between anchor boxes and target boxes. The model trained using these two methods improves the accuracy of surface target recognition compared to the original model, solving the problem of inaccurate target detection and improving recognition precision. This embodiment uses image enhancement processing technology, including a series of steps such as K-means clustering algorithm, mean shift filtering, Laplacian pyramid decomposition and recombination technology, and morphological operations, to preprocess the image to be identified. This effectively improves the accuracy of water surface target detection and solves the problem of water surface reflection, thereby improving the recognition accuracy.
[0060] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for detecting water surface targets, characterized in that, include: Collect water surface image data, divide the image data into small rigid body data and large soft body data, perform K-means clustering on each, and generate rigid anchor frame groups and flexible anchor frame groups. An improved YOLOv5 was constructed: the shallow detection head of YOLOv5 was embedded in the SA module of CBAM, and the deep detection head of YOLOv5 was embedded in the CA module of CBAM; the Wise-IoU loss function module was integrated into the loss calculation part of YOLOv5. The rigid anchor frame group is mapped to the shallow and medium layer detectors of YOLOv5, and the flexible anchor frame group is mapped to the deep layer detector of YOLOv5; the improved YOLOv5 is used to train small rigid body data and large soft body data to obtain the trained target model. The target model was used to detect surface debris and algae.
2. The water surface target detection method according to claim 1, characterized in that, In the improved YOLOv5, the shallow detection head is embedded in the SA module after the C3 module; the deep detection head is embedded in the CA module after the C3 module; this asymmetric design is maintained in the FPN+PAN structure.
3. The water surface target detection method according to claim 1 or 2, characterized in that, The specific process of integrating the Wise-IoU loss function module into the loss calculation part of YOLOv5 is as follows: define the Wise-IoU calculation module in the metric tool file; modify the original bounding box intersection-union function and intersection-union variable in the loss calculation file; set different focusing coefficients for different object levels to form a hierarchical cascaded loss function, and use the integrated loss function to train the model.
4. The water surface target detection method according to claim 1 or 2, characterized in that, The process of detecting surface debris and algae based on the target model is as follows: In the target detection process, the K-means clustering algorithm is used to segment the input image, dividing the image pixels into reflection areas, water surface areas and background areas; The identified reflective areas are processed by mean shift filtering to smooth homogeneous regions while preserving edge information. Using the Laplace pyramid decomposition and reconstruction technique, multi-scale processing is performed on both reflective and non-reflective regions. The final de-reflection image is obtained by performing secondary segmentation based on OpenCV and combining it with morphological operations.
5. A surface target detection system, characterized in that, include: Data acquisition and processing module: Acquires water surface image data, divides the image data into small rigid body data and large soft body data, performs K-means clustering on each, and generates rigid anchor frame groups and flexible anchor frame groups. Model building module: Building an improved YOLOv5: embedding the shallow detection head of YOLOv5 into the SA module of CBAM, and embedding the deep detection head of YOLOv5 into the CA module of CBAM; integrating the Wise-IoU loss function module into the loss calculation part of YOLOv5; Model training module: Map rigid anchor frame groups to the shallow and medium layer detectors of YOLOv5, and map flexible anchor frame groups to the deep layer detectors of YOLOv5; use the improved YOLOv5 to train small rigid body data and large soft body data to obtain the trained target model. Model application module: Detects surface debris and algae based on the target model.
6. The water surface target detection system according to claim 5, characterized in that, In the improved YOLOv5, the shallow detection head is embedded in the SA module after the C3 module; the deep detection head is embedded in the CA module after the C3 module; this asymmetric design is maintained in the FPN+PAN structure.
7. The water surface target detection system according to claim 5 or 6, characterized in that, The specific process of integrating the Wise-IoU loss function module into the loss calculation part of YOLOv5 is as follows: define the Wise-IoU calculation module in the metric tool file; modify the original bounding box intersection-union function and intersection-union variable in the loss calculation file; set different focusing coefficients for different object levels to form a hierarchical cascaded loss function, and use the integrated loss function to train the model.
8. The water surface target detection system according to claim 5 or 6, characterized in that, The process of detecting surface debris and algae based on the target model is as follows: In the target detection process, the K-means clustering algorithm is used to segment the input image, dividing the image pixels into reflection areas, water surface areas and background areas; The identified reflective areas are processed by mean shift filtering to smooth homogeneous regions while preserving edge information. Using the Laplace pyramid decomposition and reconstruction technique, multi-scale processing is performed on both reflective and non-reflective regions. The final de-reflection image is obtained by performing secondary segmentation based on OpenCV and combining it with morphological operations.
9. A processing device, characterized in that, It includes at least one processor and at least one memory communicatively connected to the processor, wherein: the memory stores program instructions executable by the processor, and the processor can execute the method as described in any one of claims 1 to 4 by invoking the program instructions.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that cause the computer to perform the method as described in any one of claims 1 to 4.