Mechanical parts defect detection method and system based on improved YOLOv12 model

By improving the structure and feature extraction method of the YOLOv12 model, the problem of insufficient detection accuracy and recall in the visual defect detection of mechanical parts is solved, and efficient and accurate defect detection is achieved in complex industrial environments.

CN120279020BActive Publication Date: 2025-08-26JIANGXI SCI & TECH NORMAL UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510759132.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-09
Publication Date
2025-08-26
Estimated Expiration
2045-06-09

AI Technical Summary

Technical Problem

The existing YOLOv12 model has insufficient detection accuracy and recall in the detection of visual defects of mechanical parts, especially in the poor performance when dealing with small goals, complex backgrounds and multi-scale goals. Data imbalance and noise interference affect the generalization ability and stability of the model in industrial production environments.

Method used

By improving the YOLOv12 model, the C3k2_RCB feature extraction module is used to replace the original C3k2 module, the gated dynamic space aggregator GDSAFusion is introduced to replace the Concat module, and the A2C2f_STR module is used in the backbone network and the neck network to optimize the model structure to enhance feature extraction and anti-interference capabilities, and combine dynamic convolution kernel and context mixing mechanism to improve the adaptability of the model in complex industrial environments.

Benefits of technology

It significantly improves the accuracy and efficiency of mechanical parts defect detection, and can accurately identify various complex and subtle defects in complex industrial environments, meeting the high-quality inspection needs of modern manufacturing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120279020B_ABST
    Figure CN120279020B_ABST
Patent Text Reader

Abstract

This application belongs to the fields of image processing technology and industrial inspection, and discloses a method and system for mechanical part defect detection based on an improved YOLOv12 model. The method comprises: first, collecting a dataset of defective mechanical part images; then, using the preprocessed dataset to train the improved YOLOv12 mechanical part defect detection model to obtain an optimized model. The improvements include: replacing the original C3k2 module with the C3k2_RCB feature extraction module in the backbone network and the neck network; replacing the original Concat module with the gated dynamic spatial aggregator GDSAFusion in the neck network; replacing the original A2C2f module with the A2C2f_STR in the backbone network and the neck network; and finally, using the optimized model to perform real-time defect detection on mechanical parts. By optimizing the YOLOv12 model structure, this method significantly improves the accuracy and efficiency of the YOLOv12 model for mechanical part defect detection in complex industrial environments, especially improving the detection performance for small defects and complex backgrounds.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the fields of image processing technology and industrial detection, and in particular to a mechanical parts defect detection method and system based on an improved YOLOv12 model. Background Art

[0002] In modern manufacturing, the quality of mechanical parts directly impacts the performance and reliability of the entire product, making high-precision and efficient visual defect detection of mechanical parts crucial. Traditional visual defect detection methods for mechanical parts, such as image processing algorithms based on threshold segmentation and edge detection, often rely on manually set parameters. These methods are poorly adaptable to complex lighting conditions, surface texture variations, and the detection of minor defects, and exhibit low detection efficiency, making them difficult to meet the demands of large-scale industrial production.

[0003] In recent years, deep learning has demonstrated powerful performance in object detection. The YOLO (You Only Look Once) family of algorithms, owing to their fast and efficient detection capabilities, has been widely used in industrial inspection scenarios. As an advanced version of this family, the YOLOv12 model inherits the YOLO family's advantages for end-to-end, fast detection while further optimizing its network structure and detection algorithm, significantly improving detection accuracy and speed. However, in the practical application of visual defect detection in mechanical parts, the YOLOv12 model still faces some challenges. Mechanical parts vary in shape and size, and some surface defects have subtle features. The existing YOLOv12 model needs further improvement in detection accuracy and recall for small objects, feature extraction in complex backgrounds, and multi-scale object detection. Furthermore, data collection in industrial production environments can be subject to issues such as data imbalance and noise, which can affect the model's generalization and stability.

[0004] Therefore, it is urgent to propose a mechanical parts defect detection method based on the improved YOLOv12 model. By optimizing and improving the model structure, feature extraction method, training strategy, etc., the detection accuracy and efficiency of mechanical parts can be improved, the adaptability of the model in complex industrial environments can be enhanced, and the needs of modern manufacturing for high-quality inspection of mechanical parts can be met. Summary of the Invention

[0005] The purpose of the present invention is to provide a mechanical parts defect detection method and system based on an improved YOLOv12 model in order to solve the above problems, so as to improve the detection accuracy and efficiency of mechanical parts.

[0006] In a first aspect, the present invention provides a method for detecting defects in mechanical parts based on an improved YOLOv12 model, comprising the following steps:

[0007] Collecting a dataset of defective mechanical parts images and preprocessing the dataset of mechanical parts images, wherein the preprocessing includes marking defects in each mechanical parts image and performing image data expansion and enhancement;

[0008] The improved YOLOv12 mechanical part defect detection model is trained using the preprocessed mechanical part image dataset to obtain an optimized model. The improved YOLOv12 mechanical part defect detection model includes:

[0009] In the backbone network and neck network, the C3k2_RCB feature extraction module is used to replace the original C3k2 module;

[0010] In the neck network, the gated dynamic spatial aggregator GDSAFusion is used to replace the original Concat module;

[0011] Introduced OverLoCK, a visual foundation model based on dynamic convolution kernels and context mixing mechanism, into the backbone network;

[0012] The optimization model is used to perform real-time defect detection on mechanical parts.

[0013] As an optional implementation of the first aspect of the present application, in the step of replacing the original C3k2 module with the C3k2_RCB feature extraction module in the backbone network and the neck network, the implementation process of the C3k2_RCB feature extraction module includes: initializing the C3k2_RCB module, including receiving the number of input channels, the number of output channels, the number of repetitions n of the RepConvBlock module, the judgment flag, the channel scaling coefficient, the number of convolutional feature extraction layers, and the identification parameter of whether to use residual connection; inputting the feature map into the processing unit inside the C3k2_RCB module; the processing unit dynamically constructs a core processing structure according to the judgment flag, and the core processing structure is composed of n RepConvBlock modules, specifically including: passing the feature map through the convolutional feature extraction layer, and each time it passes through a convolutional feature extraction layer, it is divided into two feature maps using a feature splitting operation, and the feature map obtained by passing one of the feature maps through n RepConvBlock modules is combined with the other feature map separated by the feature splitting operation using a feature splicing operation, and finally integrating the features through a convolutional fusion layer to obtain the final output feature map.

[0014] As an optional implementation of the first aspect of the present application, the RepConvBlock module implements the following steps: extracting local features from the input feature map through a first 3×3 depthwise separable convolution to obtain a local feature map; processing the local feature map through a multi-branch projection module, wherein the projection module sequentially includes a normalization layer, a reparameterizable hole convolution, a batch normalization layer, a channel attention module, a first 1×1 convolution layer, a GELU activation function, a second 3×3 depthwise separable convolution, a global response normalization, and a second 1×1 convolution layer; if residual scaling is enabled, the input feature map is scaled and added to the output of the projection module to obtain the output feature; otherwise, a residual connection is used to directly use the output of the projection module as the output feature.

[0015] As an optional implementation of the first aspect of the present application, in the step of replacing the original Concat module with the gated dynamic spatial aggregator GDSAFusion in the neck network, the implementation process of the gated dynamic spatial aggregator GDSAFusion includes: channel-wise splicing of the input feature map and the contextual features to form a fused feature; extracting local features from the fused features through a 3×3 depthwise separable convolution, and then stabilizing the feature distribution through a normalization layer; calculating the spatial attention weight through a query-key mechanism, and the query-key mechanism uses a relative position bias; capturing multi-scale contextual information from the fused features through a reparameterizable dilated convolution, and then performing channel-adaptive calibration through a channel attention module; controlling the information flow through a gating mechanism; weightedly fusing the features processed by the gating mechanism with the features of the residual path; and processing the weighted fused features using double-level scaling combined with random depth dropout to obtain an enhanced feature map.

[0016] As an optional implementation of the first aspect of the present application, in the step of using the A2C2f_STR module to replace the original A2C2f module in the backbone network and the neck network, the implementation process of the A2C2f_STR module includes: connecting the input feature tensor to the initial convolution layer, and the initial convolution layer realizes channel dimension compression of the input feature tensor through low-rank mapping, and extracts feature information of the basic characterization capability to obtain a feature map after the initial convolution processing; temporarily storing the feature map after the initial convolution processing in a list data structure, and entering a loop processing link composed of a visual modeling module that integrates cross-index temporal interactions. In the loop processing link, the tail element of the list data structure is used as input and is passed to each visual modeling module that integrates cross-index temporal interactions in sequence for processing to obtain a feature vector generated after processing by the visual modeling module that integrates cross-index temporal interactions; the feature vector generated after processing by the visual modeling module that integrates cross-index temporal interactions is fused with the original input feature tensor through a residual mapping mechanism to obtain the final output fusion feature.

[0017] As an optional implementation of the first aspect of the present application, the implementation process of the visual modeling module that integrates cross-index temporal interaction includes: performing a first layer normalization processing on the feature map after initial convolution processing to obtain a first normalized feature; realizing adaptive weight fusion of the first normalized feature and the feature map after initial convolution processing through learnable parameters to obtain an adaptive weight fusion feature; inputting the adaptive weight fusion feature into the cross-index temporal interaction modeling component, and the cross-index temporal interaction modeling component uses the scan index and the inverse scan index to reorganize and model the temporal features across spatial dimensions to obtain the features processed by the cross-index temporal interaction modeling component; performing a second layer normalization processing on the features processed by the cross-index temporal interaction modeling component to obtain a second normalized feature; the second normalized feature enters the feedforward convolution block to complete the nonlinear transformation to obtain a feedforward output; and the features processed by the cross-index temporal interaction modeling component are fused with the feedforward output through a residual connection to obtain a feature vector generated after processing by the visual modeling module that integrates cross-index temporal interaction.

[0018] As an optional implementation of the first aspect of the present application, the implementation process of the cross-index temporal interaction modeling component includes: generating an index pair for the adaptive weight fusion features input to the cross-index temporal interaction modeling component through an index generation module, and the index pair includes a scan index and an inverse scan index; using the scan index to rearrange the adaptive weight fusion features input to the cross-index temporal interaction modeling component to obtain a rearranged one-dimensional temporal feature; mapping the rearranged one-dimensional temporal feature to a dynamic parameter through a temporal parameter projection layer; performing temporal modeling on the dynamic parameter based on a selective scanning engine of a state space model, and outputting a one-dimensional sequence feature after temporal modeling; restoring the one-dimensional sequence feature after temporal modeling to a two-dimensional spatial feature through the inverse scan index; generating a gating weight through an adaptive gating unit, and multiplying the gating weight by the two-dimensional spatial feature element by element, and outputting the feature finally processed by the cross-index temporal interaction modeling component.

[0019] In a second aspect, an embodiment of the present application provides a mechanical parts defect detection system based on an improved YOLOv12 model, comprising:

[0020] An image acquisition module is used to collect a dataset of defective mechanical parts images and preprocess the dataset of mechanical parts images, wherein the preprocessing includes marking defects in each mechanical parts image and performing image data expansion and enhancement;

[0021] The model training module is used to train the improved YOLOv12 mechanical part defect detection model using the preprocessed mechanical part image dataset to obtain an optimized model. The improved YOLOv12 mechanical part defect detection model includes:

[0022] In the backbone network and neck network, the C3k2_RCB feature extraction module is used to replace the original C3k2 module;

[0023] In the neck network, the gated dynamic spatial aggregator GDSAFusion is used to replace the original Concat module;

[0024] Use A2C2f_STR to replace the original A2C2f module in the backbone network and neck network;

[0025] A defect detection module is used to perform real-time defect detection on mechanical parts using the optimization model.

[0026] In a third aspect, an embodiment of the present application provides an electronic device comprising a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein when the program or instruction is executed by the processor, the steps of the method described in the first aspect are implemented.

[0027] In a fourth aspect, an embodiment of the present application provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.

[0028] Compared with the existing technology, the present invention provides a mechanical parts defect detection method based on an improved YOLOv12 model. The method first collects a dataset of defective mechanical parts images and performs preprocessing, including marking the defects in each image and expanding and enhancing the image data. By providing high-quality and diverse training data and simulating complex environments, the generalization ability of the subsequent training model and its adaptability to the complex environment of actual industrial scenarios are significantly improved. Then, the preprocessed dataset was used to train an improved YOLOv12 mechanical parts defect detection model. The key to the improved model lies in its structural optimization: the C3k2_RCB feature extraction module is used to replace the original C3k2 module in the backbone network and the neck network. This improvement enhances the model's detection capability for small targets, tiny and internal defects through heterogeneous convolution kernel topology, residual feature recalibration and internal stacking of RepConvBlock modules, improves the feature extraction accuracy in complex industrial environments (such as oil stains and reflections), and optimizes the inference performance on edge devices. At the same time, the gated dynamic spatial aggregator GDSAFusion is used to replace the original Concat module in the neck network. This improvement is achieved by combining dynamic weight generation and contextual mixer. The new algorithm utilizes a dynamic kernel optimization strategy based on a non-local attention operator to overcome the representation bottleneck of traditional methods in detecting irregular defects (such as casting pores and machining tool marks). This significantly enhances the model's anti-interference capabilities in complex industrial environments and effectively addresses feature degradation issues caused by harsh working conditions such as metal surface reflections and oil stains. Furthermore, the original A2C2f module is replaced with the A2C2f_STR module in the backbone and neck networks. By introducing the STR visual modeling module, which integrates cross-index temporal interactions, the model effectively correlates spatial and temporal information, enhancing detection performance for periodic defects such as surface cracks on rotating parts or continuous features of dynamic deformation. This also addresses the difficulty of traditional A2C2f modules, which rely on local convolution operations, in capturing long-range spatiotemporal dependencies. Through these targeted improvements to key model modules, the resulting optimized model, while inheriting the fast and efficient advantages of the YOLO series, significantly improves the accuracy, robustness, and efficiency of detecting various complex and subtle defects on mechanical parts in complex and harsh industrial environments. Finally, the trained optimization model is used to perform real-time defect detection on mechanical parts. With its high precision, high robustness and high efficiency, it can detect and locate defects in a timely and accurate manner, meeting the needs of modern manufacturing for high-quality detection. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Figure 1 A flowchart of a mechanical parts defect detection method based on an improved YOLOv12 model provided in an embodiment of the present invention;

[0030] Figure 2 This is a schematic diagram of the model structure of the mechanical parts defect detection method based on the improved YOLOv12 model;

[0031] Figure 3 This is a schematic diagram of the C3k2_RCB module structure;

[0032] Figure 4 This is a schematic diagram of the RepConvBlock module structure;

[0033] Figure 5 This is a schematic diagram of the GDSAFusion module structure;

[0034] Figure 6 This is a schematic diagram of the A2C2f_STR module structure;

[0035] Figure 7 Schematic diagram of the STR module structure;

[0036] Figure 8 This is a schematic diagram of the CITIM unit structure;

[0037] Figure 9 A schematic diagram of the structure of a mechanical parts defect detection system based on an improved YOLOv12 model provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0038] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.

[0039] The terms "first", "second", etc. in the specification and claims of this application are used to distinguish similar objects, and are not used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the application can be implemented in a sequence other than those illustrated or described here. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally represents that the objects associated before and after are in a kind of "or" relationship. In the description of the present invention, the meaning of "plurality" is two or more, unless otherwise clearly and specifically limited.

[0040] Example 1

[0041] See also Figure 1 , is a flowchart of an implementation method of a mechanical parts defect detection method based on an improved YOLOv12 model provided by an embodiment of the present invention. Figure 1 , a mechanical parts defect detection method based on an improved YOLOv12 model provided by an embodiment of the present invention includes the following steps:

[0042] S1: Collect a dataset of defective mechanical parts images and preprocess the dataset. The preprocessing includes marking the defects of each mechanical part image and performing image data expansion and enhancement.

[0043] First, we collected a dataset of defective mechanical parts images. For example, we captured 3,000 images of common mechanical part defects in a factory, including images of internal holes in cast iron, fatigue cracks on bearing surfaces, gear tooth wear, and deformation of precision shafts, among other typical industrial scenarios.

[0044] Secondly, a computer is used to annotate the defects in each photo, and data expansion and enhancement are performed. For example, a dataset of common mechanical parts defect images is imported into the X-AnyLabeling annotation tool, and the defects in these 3,000 images are labeled in YOLO format. The annotation file contains information such as the category number of each defect target. Then, data enhancement methods such as random contrast enhancement, noise, flipping, and scaling are used to expand the image data and labels to 30,000 images, simulating the images recognized by the camera in various extreme situations, thereby improving the generalization ability of the training model.

[0045] Finally, the computer data set is divided into validation, training, and test sets, and then processed into the YOLOv12 network model recognition format. For example, Python code is used to divide the mechanical parts defect data set into training, validation, and test sets at a set ratio of 8:1:1. The training set will be used to train the model, the validation set will be used for evaluation during the training process, and the test set will be used to evaluate the model's performance and processed into the YOLOv12 network model recognition format.

[0046] S2: Use the preprocessed mechanical parts image dataset to train the improved YOLOv12 mechanical parts defect detection model to obtain an optimized model.

[0047] like Figure 2As shown in the figure, the improved YOLOv12 mechanical part defect detection model includes: using the C3k2_RCB feature extraction module to replace the original C3k2 module in the backbone network and the neck network; using the gated dynamic spatial aggregator GDSAFusion to replace the original Concat module in the neck network; and using A2C2f_STR to replace the original A2C2f module in the backbone network and the neck network.

[0048] The C3k2_RCB module achieves cross-modal feature extraction of internal defects in mechanical parts while reducing the number of model parameters through heterogeneous convolution kernel topology optimization and residual feature recalibration mechanism. Its innovative deep separable convolution group architecture combined with dynamic receptive field reconstruction technology reduces the false detection rate of traditional algorithms in small defect detection. At the same time, through hardware-aware operator optimization, the inference latency on industrial-grade edge devices is more stable.

[0049] like Figure 3 As shown in the figure, in the steps of replacing the original C3k2 module with the C3k2_RCB feature extraction module in the backbone network and the neck network, the implementation process of the C3k2_RCB feature extraction module includes: initializing the C3k2_RCB module, including receiving the number of input channels, the number of output channels, the number of repetitions n of the RepConvBlock module, the judgment flag, the channel scaling coefficient, the number of convolutional feature extraction layers, and the identification parameter of whether to use residual connection; inputting the feature map into the processing unit inside the C3k2_RCB module; the core processing structure of the processing unit is composed of n RepConvBlock modules, specifically including: the feature map first passes through the convolutional feature extraction layer, and then each time it passes through a convolutional feature extraction layer, it is divided into two feature maps using a feature splitting operation, and the feature map obtained by passing one of the feature maps through n RepConvBlock modules is combined with the other feature map separated by the feature splitting operation using a feature splicing operation, and finally the features are integrated through a convolutional fusion layer to obtain the final output feature map.

[0050] In the visual inspection scenario of mechanical parts, the C3k2_RCB module plays an important role. Mechanical parts come in various shapes and sizes, and some defect features are subtle, making it difficult for traditional modules to balance the detection accuracy of large and small targets. In the C3k2_RCB module, when the C3k_RCB module is enabled, it combines the channel processing capabilities of the original C3k2 module with the feature extraction advantages of the introduced internal stacked dynamic convolution block RepConvBlock module, effectively enhancing the detection capability of small target defects. The heavy parameterization characteristics of the RepConvBlock module enable it to extract features more accurately in complex industrial environments such as pollution, reflections, and other interference, reducing false detections and missed detections. At the same time, the module can flexibly adjust its internal structure according to the distribution of defect categories in the data, alleviate the problem of missed detection of rare defects, improve the robustness and detection accuracy of the model in mechanical parts inspection, and meet the actual industrial production demand for high-quality inspection.

[0051] like Figure 4 As shown in the figure, the implementation process of the RepConvBlock module includes: extracting local features from the input feature map through a 3×3 depth-separable convolution to obtain a local feature map; processing the local feature map through a multi-branch projection module, which sequentially includes a normalization layer, a reparameterizable hole convolution, a batch normalization layer, a channel attention module, a 1×1 convolution layer, a GELU activation function, a 3×3 depth-separable convolution, a global response normalization, and a 1×1 convolution layer; if residual scaling is enabled, the input feature map is scaled and added to the output of the projection module to obtain the output feature; otherwise, a residual connection is used to directly use the output of the projection module as the output feature.

[0052] It can be understood that the input feature map first passes through a 3×3 depth-wise separable convolution (dwconv) for local feature extraction to capture spatial local information. Subsequently, the feature map passes through a multi-branch projection module (proj), which contains the following operations in sequence: first, the feature map is normalized by a normalization layer; then, a reparameterizable hole convolution is used to enhance the receptive field and fuse multi-scale information; then, the feature distribution is further stabilized by a batch normalization layer; then, the channels of the feature map are adaptively weighted by the channel attention module, and the channel weights are adaptively calibrated to improve the contribution of important features; then, the number of channels is expanded by a 1×1 convolution layer; nonlinearity is introduced by applying the GELU activation function; and then, a 3×3 depth-wise separable convolution (dwconv) is used to further extract features to obtain output features. ; Enhance the discriminability of features through global response normalization; Finally, through a The convolution layer restores the number of channels to the original dimension. Finally, residual scaling (ls) is enabled, which scales the input feature map and the output of the projection module. Xproj Add together to get X final Otherwise, the residual connection output is used directly X final . Its forward propagation mathematical expression is:

[0053]

[0054] in, Represents random depth.

[0055] Furthermore, depth-wise separable convolution is implemented by: transforming the input feature map into Feature extraction is performed through a 3×3 depthwise separable convolution with a grouping number equal to the number of input channels. This design reduces the number of parameters from the standard convolution to down to ( is the number of channels, is the kernel size), to improve computational efficiency; at the same time, in the forward propagation, the output of the depthwise separable convolution DepthwiseConv ( ) is directly added to the input feature map to form a residual connection to obtain the output feature This design not only preserves the original feature information, but also alleviates the gradient vanishing problem through skip connections. The forward propagation mathematical expression is:

[0056]

[0057] like Figure 5 As shown, in the step of replacing the original Concat module with the gated dynamic spatial aggregator GDSAFusion in the neck network, the implementation process of the gated dynamic spatial aggregator GDSAFusion includes: and contextual features Perform channel splicing to form fusion features ; The fused features are extracted through 3×3 depth-separable convolution (Dwconv) to extract local features , and then the feature distribution is stabilized by the normalization layer (Norm) ; The spatial attention weight is calculated through the query-key mechanism, which uses the relative position bias (RPB) to enhance the position perception ability; the fusion feature is obtained by capturing multi-scale context information through the reparameterizable hole convolution (LEPE) , and then the channel attention module performs channel adaptive calibration to obtain ; Information flow is controlled by gating mechanism The features processed by the gate mechanism (gate) are weighted and fused with the features of the residual path. The weighted fused features are processed using dual-level scaling (ls1 and ls2) combined with random depth dropout (drop_path) to obtain an enhanced feature map, which significantly improves training stability while maintaining feature expression capabilities. The forward propagation mathematical expression is:

[0058]

[0059] in, represents the concatenation of vector dimensions, represents the query Q weight, represents the key K weight, represents the attention weight, represents an activation function that normalizes the input into a probability distribution vector, d represents the dimension of the key K, Represents the attention channel layer.

[0060] The GDSAFusion module combines dynamic weight generation and contextual mixing mechanisms to construct a multimodal feature interaction matrix, breaking through the representation bottleneck of traditional visual algorithms in the detection of irregular defects such as casting pores and machining tool marks. Its dynamic kernel optimization strategy based on the non-local attention operator greatly improves the signal-to-noise ratio in complex industrial backgrounds, solving the feature degradation problem of traditional methods under harsh working conditions such as metal surface reflection and oil interference.

[0061] like Figure 6 As shown, the implementation of A2C2f_STR is as follows: the input feature tensor X is first fed into an initial convolutional layer constructed using the parent class initialization mechanism. This initial convolutional layer compresses the channel dimensions of the input feature tensor through low-rank mapping. Using a sliding window convolution operation on the feature map spatial domain, the convolution kernel performs point-by-point multiplication and accumulation between the weights and the corresponding elements, completing the dimensionality conversion from input channels to hidden channels. This extracts feature information with basic representational capabilities. The resulting feature map after the initial convolution is temporarily stored in a specific list data structure. The feature processing flow then enters a loop consisting of a visual modeling module (STR module) that integrates cross-index temporal interactions. In this loop, the last element of the list is input and sequentially passed to each STR module for processing. The feature vector X2 generated by the STR module is fused with the original input feature tensor X through a residual mapping mechanism, ultimately outputting the fused feature vector Y. The forward propagation process is mathematically expressed as:

[0062]

[0063] like Figure 7As shown in Figure 1, the STR module (Spatio-Temporal Reformer) is a visual modeling module that integrates cross-index temporal interaction. It is mainly composed of two-layer normalization, adaptive residual connection, cross-index temporal interaction modeling component (CITIM unit (Cross-Index Temporal Interaction Module)) and feed-forward convolution block. This module first performs a multi-layer normalization on the input features. Perform the first layer normalization (LayerNorm) processing to obtain the normalized features ; Through learnable parameters and Realize the adaptive weight fusion of normalized features and original input feature map (feature map after initial convolution processing); the adaptive weight fusion feature after fusion Input to the cross-index temporal interaction modeling component (CITIM unit), the scan index and inverse index (IDs) generated by the cross-index temporal interaction modeling component are reorganized and modeled for temporal features across spatial dimensions; then the second layer normalization process is performed to obtain , and enters the feedforward convolution block Mlp to complete the nonlinear transformation and output the feedforward output ;Finally, the adaptive weight fusion features are connected through residual With feedforward output Fusion, the final output feature vector X2 is obtained. Its forward propagation mathematical expression is as follows:

[0064]

[0065] IDs are index pairs consisting of a scan index and an inverse scan index. The scan index is a sequential index of the 2D feature graph traversal generated based on a preset scan length and offset strategy. It is used to convert spatial features into a one-dimensional sequence for time series modeling. The inverse scan index is the reverse mapping of the scan index and is used to restore the one-dimensional sequence after time series modeling to the original 2D feature graph structure, ensuring accurate restoration of feature spatial positions. Together, they form the core indexing mechanism for cross-dimensional feature interaction.

[0066] like Figure 8 As shown in the figure, the Cross-Index Temporal Interaction Modeling Component (CITIM unit) mainly consists of an index generation module, a feature rearrangement layer, a temporal parameter projection layer, a selective scanning engine, an inverse index restoration module, and an adaptive gating unit. The CITIM unit first performs an index generation on the input features. Output index pair IDs through the index generation module; use the scanning index to Rearrange into one-dimensional time series features ; Through the time series parameter projection layer Mapping to dynamic parameters (timing decay factor), (input mapping matrix), (output mapping matrix); selective scanning engine based on state space model (SSM) Perform time series modeling and output one-dimensional sequence features ; Then reverse scan the index Restore to two-dimensional space features ; Finally, the adaptive gating unit generates weights and Multiply element by element and output the features that are finally processed by the cross-index temporal interaction modeling component . Its forward propagation mathematical expression is as follows:

[0067]

[0068] Among them, index_scan is index scan; Represents the inverse index restoration function, which is responsible for restoring one-dimensional time series features to two-dimensional spatial features; Represents the skip connection parameters; Project is the parameter projection layer; inverse_ids represents the inverse scan index; Gating is the gating function; It is an operation to constrain the range of learnable parameters; A_logs is a learnable parameter in the model.

[0069] Furthermore, the training and validation sets are fed into the improved YOLOv12 mechanical part defect detection model, and the number of training cycles is set. As the number of training cycles increases, the model's loss function curve gradually converges. When the loss function curve converges and stabilizes, the mechanical part defect detection model is trained to its optimal state, and its optimal model weight file is saved. The images to be detected from the test set are fed into the trained mechanical part defect detection model, which outputs defect detection images of the corresponding parts. The detection images include the type of each detection target, and the location of each target in the target detection image is marked. The optimal weight file is downloaded and saved on a computer for later use.

[0070] S3: Real-time defect detection of mechanical parts using optimized models.

[0071] For example, the trained model weight file is deployed in the production environment, and images of mechanical parts are captured in real time through a camera or image acquisition device and input into the model for analysis. The model will quickly identify and mark the defect location and type.

[0072] In summary, in this embodiment, the improved YOLOv12 model proposed in the present invention can be applied to more complex mechanical parts defect recognition application scenarios. The A2C2f_STR module introduces the visual modeling module STR that integrates cross-index temporal interaction, so that the cross-index temporal interaction modeling space and time dimensions are associated, thereby enhancing the detection performance of periodic defects or dynamic deformations such as the continuity characteristics of surface cracks of rotating parts, and solving the problem that traditional A2C2f relies on local convolution operations and is difficult to capture long-distance spatiotemporal dependencies; the GDSAFusion module combines dynamic weight generation and context mixing mechanisms, and constructs a multimodal feature interaction matrix to detect irregular defects such as casting pores and machining tool marks. The test breaks through the representation bottleneck of traditional visual algorithms. Its dynamic kernel optimization strategy based on the non-local attention operator greatly improves the signal-to-noise ratio in complex industrial backgrounds, and solves the feature degradation problem of traditional methods under harsh working conditions such as metal surface reflection and oil interference; the C3k2_RCB module optimizes the heterogeneous convolution kernel topology structure and the residual feature recalibration mechanism. While keeping the number of model parameters reduced, it realizes cross-modal feature extraction of internal defects of mechanical parts. Its innovative deep separable convolution group architecture combined with dynamic receptive field reconstruction technology reduces the false detection rate of traditional algorithms in small defect detection. At the same time, through hardware-aware operator optimization, the inference delay on industrial-grade edge devices is more stable.

[0073] Example 2

[0074] See also Figure 9 , shown is a structural diagram of a mechanical parts defect detection system based on an improved YOLOv12 model proposed in the second embodiment of the present application. The system includes the following key modules:

[0075] An image acquisition module 100 is used to collect a dataset of defective mechanical part images and preprocess the dataset, including marking defects on each mechanical part image and performing image data expansion and enhancement.

[0076] The model training module 200 is used to train the improved YOLOv12 mechanical part defect detection model using the preprocessed mechanical part image dataset to obtain an optimized model. The improved YOLOv12 mechanical part defect detection model includes:

[0077] In the backbone network and neck network, the C3k2_RCB feature extraction module is used to replace the original C3k2 module;

[0078] In the neck network, the gated dynamic spatial aggregator GDSAFusion is used to replace the original Concat module;

[0079] Use A2C2f_STR to replace the original A2C2f module in the backbone network and neck network;

[0080] The defect detection module 300 is used to perform real-time defect detection on mechanical parts using the optimization model.

[0081] In the embodiments of the present application, a mechanical part defect detection system based on an improved YOLOv12 model can be a device, or a component, integrated circuit, or chip in a terminal. The device can be a mobile electronic device or a non-mobile electronic device. For example, the mobile electronic device can be a mobile phone, tablet computer, laptop computer, PDA, in-vehicle electronic device, wearable device, ultra-mobile personal computer (UMPC), netbook, or personal digital assistant (PDA), while the non-mobile electronic device can be a server, network attached storage (NAS), personal computer (PC), etc., which are not specifically limited in the embodiments of the present application.

[0082] In the embodiments of the present application, a mechanical part defect detection system based on an improved YOLOv12 model can be a device having an operating system. The operating system can be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiments of the present application.

[0083] The mechanical parts defect detection system based on the improved YOLOv12 model provided in the embodiment of the present application can achieve Figure 1 In the method embodiment, each process of implementing a mechanical parts defect detection method based on an improved YOLOv12 model is not described here to avoid repetition.

[0084] Optionally, an embodiment of the present application also provides an electronic device, including a processor, a memory, and a program or instruction stored in the memory and executable on the processor. When the program or instruction is executed by the processor, the various processes of the above-mentioned embodiment of the mechanical parts defect detection method based on the improved YOLOv12 model are implemented, and the same technical effect can be achieved. To avoid repetition, they will not be described here.

[0085] An embodiment of the present application also provides a readable storage medium, which stores a program or instruction. When the program or instruction is executed by the processor, the various processes of the embodiment of the above-mentioned mechanical parts defect detection method based on the improved YOLOv12 model are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.

[0086] The processor is the processor in the electronic device described in the above embodiment. The readable storage medium includes a computer-readable storage medium, such as a computer read-only memory (ROM), random access memory (RAM), a magnetic disk, or an optical disk.

[0087] It should be noted that, in this article, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device comprising the element. In addition, it should be noted that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in the opposite order according to the functions involved. For example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.

[0088] Through the description of the above embodiments, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus the necessary general hardware platform. Of course, they can also be implemented by hardware, but in many cases the former is a more preferred embodiment. Based on this understanding, the technical solution of this application, or the part that contributes to the existing technology, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for enabling a terminal (which can be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in each embodiment of this application.

[0089] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of this application, ordinary technicians in this field can also make many forms without departing from the purpose of this application and the scope of protection of the claims, all of which are within the protection of this application.

Claims

1. A mechanical parts defect detection method based on an improved YOLOv12 model, characterized in that: The following steps are involved: Collecting a dataset of defective mechanical parts images and preprocessing the dataset of mechanical parts images, wherein the preprocessing includes marking defects in each mechanical parts image and performing image data expansion and enhancement; The improved YOLOv12 mechanical part defect detection model is trained using the preprocessed mechanical part image dataset to obtain an optimized model. The improved YOLOv12 mechanical part defect detection model includes: In the backbone network and neck network, the C3k2_RCB feature extraction module is used to replace the original C3k2 module; The implementation process of the C3k2_RCB feature extraction module includes: initializing the C3k2_RCB feature extraction module, including receiving the number of input channels, the number of output channels, the number of repetitions n of the RepConvBlock module, the judgment flag, the channel scaling coefficient, the number of convolutional feature extraction layers, and the identification parameter of whether to use residual connection; inputting the feature map into the processing unit inside the C3k2_RCB feature extraction module; the processing unit dynamically constructs a core processing structure according to the judgment flag, and the core processing structure is composed of n RepConvBlock modules, specifically including: passing the feature map through the convolutional feature extraction layer, each time it passes through a convolutional feature extraction layer, it is divided into two feature maps using a feature splitting operation, and a feature map obtained by passing one of the feature maps through n RepConvBlock modules is combined with another feature map separated by the feature splitting operation using a feature splicing operation, and finally integrating the features through a convolutional fusion layer to obtain a final output feature map; The RepConvBlock module implements the following steps: extracting local features from the input feature map through a first 3×3 depthwise separable convolution to obtain a local feature map; processing the local feature map through a multi-branch projection module, wherein the projection module sequentially includes a normalization layer, a reparameterizable dilated convolution, a batch normalization layer, a channel attention module, a first 1×1 convolution layer, a GELU activation function, a second 3×3 depthwise separable convolution, a global response normalization, and a second 1×1 convolution layer; if residual scaling is enabled, scaling the input feature map and adding it to the output of the projection module to obtain the output feature; otherwise, using a residual connection to directly use the output of the projection module as the output feature; In the neck network, the gated dynamic spatial aggregator GDSAFusion is used to replace the original Concat module; The implementation process of the gated dynamic spatial aggregator GDSAFusion includes: channel-wise concatenation of input feature maps and contextual features to form fused features; extracting local features from the fused features through 3×3 depthwise separable convolution, and then stabilizing the feature distribution through a normalization layer; calculating spatial attention weights through a query-key mechanism that uses relative position bias; capturing multi-scale contextual information from the fused features through reparameterizable dilated convolution, and then performing channel-wise adaptive calibration through a channel attention module; controlling information flow through a gating mechanism; weighted fusion of the features processed by the gating mechanism with the features of the residual path; and processing the weighted fused features using double-level scaling combined with random depth dropout to obtain an enhanced feature map. Use A2C2f_STR to replace the original A2C2f module in the backbone network and neck network; The implementation process of the A2C2f_STR module includes: connecting the input feature tensor to the initial convolution layer, the initial convolution layer realizes channel dimension compression of the input feature tensor through low-rank mapping, and extracts feature information of basic characterization capability to obtain a feature map after initial convolution processing; temporarily storing the feature map after initial convolution processing in a list data structure, and entering a loop processing link composed of a visual modeling module that integrates cross-index temporal interactions, in which the tail element of the list data structure is used as input and sequentially passed into each visual modeling module that integrates cross-index temporal interactions for processing to obtain a feature vector generated after processing by the visual modeling module that integrates cross-index temporal interactions; the feature vector generated after processing by the visual modeling module that integrates cross-index temporal interactions is fused with the original input feature tensor through a residual mapping mechanism to obtain the final output fusion feature; The implementation process of the visual modeling module that integrates cross-index temporal interaction includes: performing a first layer normalization process on the feature map after the initial convolution process to obtain a first normalized feature; realizing adaptive weight fusion of the first normalized feature and the feature map after the initial convolution process through learnable parameters to obtain an adaptive weight fusion feature; inputting the adaptive weight fusion feature into the cross-index temporal interaction modeling component, and the cross-index temporal interaction modeling component uses the scan index and the inverse scan index to reorganize and model the temporal features across spatial dimensions to obtain the features processed by the cross-index temporal interaction modeling component; performing a second layer normalization process on the features processed by the cross-index temporal interaction modeling component to obtain a second normalized feature; the second normalized feature enters the feedforward convolution block to complete the nonlinear transformation to obtain a feedforward output; the features processed by the cross-index temporal interaction modeling component are fused with the feedforward output through a residual connection to obtain a feature vector generated after being processed by the visual modeling module that integrates cross-index temporal interaction; The implementation process of the cross-index temporal interaction modeling component includes: generating an index pair for the adaptive weight fusion features input to the cross-index temporal interaction modeling component through an index generation module, and the index pair includes a scan index and an inverse scan index; using the scan index to rearrange the adaptive weight fusion features input to the cross-index temporal interaction modeling component to obtain a rearranged one-dimensional temporal feature; mapping the rearranged one-dimensional temporal feature to a dynamic parameter through a temporal parameter projection layer; performing temporal modeling on the dynamic parameter based on a selective scanning engine of a state space model, and outputting a one-dimensional sequence feature after temporal modeling; restoring the one-dimensional sequence feature after temporal modeling to a two-dimensional space feature through the inverse scan index; generating a gating weight through an adaptive gating unit, and multiplying the gating weight by the two-dimensional space feature element by element, and outputting the feature finally processed by the cross-index temporal interaction modeling component; The optimization model is used to perform real-time defect detection on mechanical parts.

2. A mechanical parts defect detection system based on an improved YOLOv12 model, characterized in that: include: An image acquisition module is used to collect a dataset of defective mechanical parts images and preprocess the dataset of mechanical parts images, wherein the preprocessing includes marking defects in each mechanical parts image and performing image data expansion and enhancement; The model training module is used to train the improved YOLOv12 mechanical part defect detection model using the preprocessed mechanical part image dataset to obtain an optimized model. The improved YOLOv12 mechanical part defect detection model includes: The C3k2_RCB feature extraction module is used to replace the original C3k2 module in the backbone network and the neck network; the implementation process of the C3k2_RCB feature extraction module includes: initializing the C3k2_RCB feature extraction module, including receiving the number of input channels, the number of output channels, the number of repetitions n of the RepConvBlock module, the judgment flag, the channel scaling coefficient, the number of convolutional feature extraction layers, and the identification parameter of whether to use residual connection; inputting the feature map into the processing unit inside the C3k2_RCB feature extraction module; the processing unit dynamically constructs a core processing structure according to the judgment flag, and the core processing structure is composed of n RepConvBlock modules, specifically including: passing the feature map through the convolutional feature extraction layer, and each time it passes through a convolutional feature extraction layer, it is divided into two feature maps using a feature splitting operation, and one of the feature maps is passed through n RepConvBlock layers. The feature map obtained by the ck module is combined with another feature map separated by the feature splitting operation using a feature splicing operation, and finally the features are integrated through a convolutional fusion layer to obtain the final output feature map; the RepConvBlock module, the implementation process includes: extracting local features from the input feature map through a first 3×3 depth-separable convolution to obtain a local feature map; processing the local feature map through a multi-branch projection module, the projection module sequentially includes a normalization layer, a reparameterizable hole convolution, a batch normalization layer, a channel attention module, a first 1×1 convolution layer, a GELU activation function, a second 3×3 depth-separable convolution, a global response normalization and a second 1×1 convolution layer; if residual scaling is enabled, the input feature map is scaled and added to the output of the projection module to obtain the output feature; otherwise, a residual connection is used to directly use the output of the projection module as the output feature; The original Concat module is replaced by the gated dynamic spatial aggregator GDSAFusion in the neck network; the implementation process of the gated dynamic spatial aggregator GDSAFusion includes: channel-wise concatenation of the input feature map and the contextual features to form fused features; extracting local features from the fused features through 3×3 depthwise separable convolution, and then stabilizing the feature distribution through a normalization layer; calculating the spatial attention weight through a query-key mechanism that uses relative position bias; capturing multi-scale contextual information through a reparameterizable dilated convolution of the fused features, and then performing channel-wise adaptive calibration through a channel attention module; controlling the information flow through a gating mechanism; weighted fusion of the features processed by the gating mechanism and the features of the residual path; processing the weighted fused features using double-level scaling combined with random depth dropout to obtain an enhanced feature map; The A2C2f_STR module is used to replace the original A2C2f module in the backbone network and the neck network; the implementation process of the A2C2f_STR module includes: connecting the input feature tensor to the initial convolution layer, and the initial convolution layer realizes channel dimension compression of the input feature tensor through low-rank mapping, and extracts feature information of the basic characterization capability to obtain the feature map after the initial convolution processing; the feature map after the initial convolution processing is temporarily stored in a list data structure, and enters a loop processing link composed of a visual modeling module that integrates cross-index temporal interactions. In the loop processing link, the tail element of the list data structure is used as input, and it is sequentially passed to each visual modeling module that integrates cross-index temporal interactions for processing. , obtain the feature vector generated after processing by the visual modeling module of fusion cross-index temporal interaction; fuse the feature vector generated after processing by the visual modeling module of fusion cross-index temporal interaction with the original input feature tensor through the residual mapping mechanism to obtain the final output fusion feature; the implementation process of the visual modeling module of fusion cross-index temporal interaction includes: performing the first layer normalization processing on the feature map after the initial convolution processing to obtain the first normalized feature; realizing the adaptive weight fusion of the first normalized feature and the feature map after the initial convolution processing through the learnable parameters to obtain the adaptive weight fusion feature; inputting the adaptive weight fusion feature into the cross-index temporal interaction modeling component, and the cross-index temporal interaction modeling component The component uses the scan index and the inverse scan index to reorganize and model the temporal features across spatial dimensions to obtain the features processed by the cross-index temporal interaction modeling component; the features processed by the cross-index temporal interaction modeling component are subjected to a second-layer normalization process to obtain the second normalized features; the second normalized features are fed into the feedforward convolution block to complete the nonlinear transformation to obtain the feedforward output; the features processed by the cross-index temporal interaction modeling component are fused with the feedforward output through the residual connection to obtain the feature vector generated after being processed by the visual modeling module that fused the cross-index temporal interaction; the implementation process of the cross-index temporal interaction modeling component includes: the adaptive weight fusion feature generation module input to the cross-index temporal interaction modeling component is generated by the index generation module An index pair is formed, wherein the index pair includes a scan index and an inverse scan index; the scan index is used to rearrange the adaptive weight fusion features input to the cross-index temporal interaction modeling component to obtain a rearranged one-dimensional temporal feature; the rearranged one-dimensional temporal feature is mapped to a dynamic parameter through a temporal parameter projection layer; a selective scanning engine based on a state space model performs temporal modeling on the dynamic parameter, and outputs a one-dimensional sequence feature after temporal modeling; the one-dimensional sequence feature after temporal modeling is restored to a two-dimensional spatial feature through the inverse scan index; a gating weight is generated through an adaptive gating unit, and the gating weight is element-wise multiplied by the two-dimensional spatial feature to output the feature finally processed by the cross-index temporal interaction modeling component; A defect detection module is used to perform real-time defect detection on mechanical parts using the optimization model.

3. An electronic device, characterized in that: The invention comprises a processor, a memory, and a program or instruction stored in the memory and executable on the processor. When the program or instruction is executed by the processor, the steps of the mechanical part defect detection method based on the improved YOLOv12 model as claimed in claim 1 are implemented.

4. A readable storage medium, characterized in that: The readable storage medium stores a program or instruction, and when the program or instruction is executed by the processor, the steps of the mechanical part defect detection method based on the improved YOLOv12 model as claimed in claim 1 are implemented.

Citation Information

Patent Citations

  • PPY-YOLO-based steel surface defect detection method and system

    CN119672031A

  • Steel surface defect detection method and system

    CN119941724A