Wood defect identification method based on YOLOv8n
By using ADown convolution, dysample and MSDA attention modules in the YOLOv8n model, the problems of low efficiency and poor accuracy of traditional wood defect detection are solved, and efficient wood defect recognition on small equipment is realized, which improves detection accuracy and model Deployment flexibility.
Patent Information
- Application Number
- CN202411678575.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-22
- Publication Date
- 2025-01-07
AI Technical Summary
Traditional wood defect detection relies on artificial visual inspection, is inefficient and subjective, unable to meet the high speed and high standards of modern industry, and deep learning models perform poorly when identifying multiple defects.
Using the lightweight wood defect recognition method based on YOLOv8n, the Conv convolution is replaced by ADown convolution and dysample replacement upsampling, and the MSDA attention module is added before the detection head to reduce the model parameter quantity and complexity, while improving the detection accuracy and Capability to capture wood defect information.
The model deployed on small embedded devices is realized, which improves the accuracy and efficiency of wood defect detection, reduces the parameter quantity and calculation quantity of the model, and enhances the capture and aggregation of multi-scale semantic information, and reduces self-attention. redundancy of force mechanism.
Smart Images

Figure CN119273675A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of defect detection and deep learning technology, and in particular to a wood defect recognition method based on YOLOv8n. Background Art
[0002] Traditionally, the detection of wood defects relies on workers' visual inspection and empirical judgment, which is not only inefficient but also highly subjective, making it difficult to ensure the consistency and accuracy of detection. In addition, manual inspection is powerless in the face of large-scale production and cannot meet the high speed and high standards of modern industrial production. With the rapid development of artificial intelligence technology, breakthroughs in image processing and pattern recognition have provided new ways to solve the above problems. Traditional detection methods based on machine vision are based on artificially designed features for detection. With the continuous development of machine vision, more and more people are applying deep learning to defect detection.
[0003] Although some deep learning models have higher reliability in defect detection than manual methods, many methods have the problem of limited identification and positioning types and pay little attention to the actual defects in the raw wood production process.
[0004] How to solve the above technical problems is the subject faced by the present invention. Summary of the invention
[0005] The purpose of the present invention is to provide a wood defect recognition method based on YOLOv8n, which lightweights the model so that it can be deployed in small embedded devices. At the same time, it also improves the accuracy of the model and improves the detection capability of wood defects.
[0006] In order to achieve the above-mentioned invention object, the technical solution adopted by the present invention is specifically: a wood defect recognition method based on YOLOv8n, comprising the following steps:
[0007] Step 1: Use ADown convolution to replace the original Conv convolution, which reduces the number of model parameters while ensuring the model detection accuracy;
[0008] Step 2: Use dysample to replace the original neck upsampling, which improves the accuracy without increasing the model complexity;
[0009] Step 3: Add the MSDA attention module in front of the detection head to improve the ability to capture wood defect information;
[0010] As a further optimization scheme of a wood defect recognition method based on YOLOv8n provided by the present invention, in the step 1, the original Conv convolution is replaced by ADown convolution, which reduces the number of model parameters while ensuring the model detection accuracy, including the following:
[0011] Using ADown to replace the original downsampling reduces the spatial dimension, number of parameters, and amount of computation of the feature map. It can also help the model capture image features at a higher level. Its design idea is: reducing the complexity of the model by reducing parameters helps to improve the operating efficiency of the model, especially in a resource-constrained environment, which helps the model to be ultimately deployed on devices with less computing power; while retaining as much image information as possible, it reduces the spatial resolution of the feature map so that the model can detect targets more accurately. It mainly combines the operations of average pooling and maximum pooling, and uses convolution to further process the feature map.
[0012] As a further optimization scheme of the wood defect recognition method based on YOLOv8n provided by the present invention, in the step 2, dysample is used to replace the original neck upsampling, which improves the accuracy without increasing the model complexity. Specifically, the input features, upsampled features, generated offsets, original grids and sigmoid functions are represented by X, X', O, G and σ respectively. Given a feature map X with a grid sampling size of C×H1×W1 and a sampling set S with a size of 2×H2×W2, where the one-dimensional 2 represents the x and y coordinates, C represents the number of channels, and H represents the number of channels. i represents the height of the i-th feature map, W i Represents the width of the i-th feature map, and the grid_sample function resamples the assumed bilinear interpolation X using the positions in S to generate X' of size C×H2×W2. This process is defined as:
[0013] X' = grid_sample(X,S)
[0014] Given an upsampling factor s and a feature map X of size C×H×W, a linear layer is used, where the number of input and output channels is C and 2s respectively. 2 , to generate a size of 2s 2 The offset O of ×H×W is transformed into 2×sH×sW by pixel shuffling. Then the sampling set S is the sum of the offset O and the original sampling grid G. This process is defined as:
[0015] O=linear(X)
[0016] S=G+O
[0017] It is characterized by using two methods to generate sampling points, namely: static range factor and dynamic range factor. Due to the existence of the normalization layer, the value of a specific output feature is usually between [-1, 1], centered on 0. So the local s 2The range of sampling positions is likely to overlap, which can easily affect the prediction of the boundary. To solve this problem, multiply the offset by a factor of 0.25 and rewrite the formula as follows:
[0018] O=0.25Linear(X)
[0019] In order to increase the flexibility of the offset, the dynamic range factor is generated by linearly projecting the input features. The dynamic range is [0, 0.5], centered at 0.25, using the sigmoid function and a static factor of 0.5. The rewritten formula is as follows:
[0020] O=0.5sigmoid(linear1(X))·linear2(X)
[0021] As a further optimization scheme of a wood defect recognition method based on YOLOv8n provided by the present invention, in the step three, an MSDA attention module is added before the detection head to improve the ability to capture wood defect information, specifically: an MSDA attention module is added before the detection head to improve the ability to capture wood defect information. Specifically: MSDA attention takes into account the locality and sparsity of the self-attention mechanism in the shallow network. It effectively aggregates semantic multi-scale information and reduces the redundancy of the self-attention mechanism without requiring additional computational costs. The core of MSDA lies in the sliding window attention SWDA, which is described as:
[0022] X=SWDA(Q,K,V,r),
[0023] X represents the SWDA output, Q, K and V represent the query matrix, key matrix and value matrix respectively. Each row of this matrix represents a separate query, key and value feature vector. For the query at position (i, j) in the original feature map, SWDA sparsely selects keys and values for self-attention operation in a sliding window centered at (i, j). The expansion rate r is used to control the degree of sparsity. The corresponding query, key and value are obtained by linear projection, and then the channels of the feature map are divided into n different heads, and multi-scale SWDA operations are performed in these different heads with different expansion rates. The specific definitions are as follows:
[0024] h i =SWDA(Q i ,K i ,V i ,r i ),1≤i≤n,
[0025] X=Linear(Concat[h1,...,h n ]),
[0026] where h i is the output of the ith head, ri is the expansion rate of the ith head, Q i represents the query matrix of the i-th head, K i represents the key matrix of the ith head, V i Represents the value matrix of the i-th head. They are connected together and then sent to the linear layer for feature aggregation into X. By setting different expansion rates for different heads, MSDA can effectively aggregate semantic information of different scales in the participating receptive field and effectively reduce the redundancy of the self-attention mechanism without complex operations and excessive additional computational costs.
[0027] Compared with the prior art, the present invention has the following beneficial effects:
[0028] 1. The present invention uses lighter downsampling to replace the original redundant convolution with efficient calculation, thereby reducing the parameters and complexity of the model while ensuring accuracy.
[0029] 2. The present invention uses lightweight upsampling to replace the traditional upsampling operation in Neck, thereby improving the performance of the model without increasing the number of model parameters and the amount of calculation.
[0030] 3. The present invention adds multi-scale void attention MSDA in front of each detection head, utilizes the sparsity of sub-attention at different scales, aggregates semantic information of different scales, and effectively reduces the redundancy of the self-attention mechanism without the need for complex operations and increasing too much additional computing costs.
[0031] 4. The present invention combines multiple modules together, making the model lightweight while improving accuracy, making it easier to deploy in small embedded devices. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.
[0033] Figure 1 This is a model diagram of the wood defect recognition method based on YOLOv8n provided by the present invention.
[0034] Figure 2 for Figure 1 Structural diagram of ADown downsampling.
[0035] Figure 3 for Figure 1 Structure diagram of dysample upsampling.
[0036] Figure 4 for Figure 1Structural diagram of MSDA attention. DETAILED DESCRIPTION
[0037] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. Of course, the specific embodiments described here are only used to explain the present invention and are not used to limit the present invention.
[0038] Example 1
[0039] See also Figures 1 to 4 This embodiment proposes a wood defect recognition method based on YOLOv8n. The experiment of this embodiment is carried out on pytorch, and the network training and testing are carried out on NVIDIA RTX3060TiGPU. During the experiment, a part of the public data set is selected for model training and evaluation. And it is compared with other methods using the same data set.
[0040] 1. Data preprocessing
[0041] For the wood data set, preprocessing is performed, including data cleaning, denoising, normalization, etc.
[0042] 2. Feature extraction
[0043] Using ADown to extract image features reduces the model parameters and model complexity, which helps improve the model's operating efficiency, especially in resource-constrained environments, which helps the model to be deployed on devices with less computing power; while retaining as much image information as possible, the spatial resolution of the feature map is reduced so that the model can detect targets more accurately. The feature map is further processed using convolution, mainly by combining average pooling and maximum pooling operations.
[0044] 3. Restore feature map
[0045] Use dysample to upsample and improve the resolution of the restored image. Generate a dynamic range factor by linearly projecting the input features to increase the flexibility of the offset.
[0046] 4. Aggregate multi-scale information
[0047] MSDA is used to aggregate semantic multi-scale information, taking into account the locality and sparsity of the self-attention mechanism in shallow networks. By setting different expansion rates for different heads, MSDA can effectively aggregate semantic information of different scales within the participating receptive fields and effectively reduce the redundancy of the self-attention mechanism without the need for complex operations and excessive additional computational costs.
[0048] 5. Comparative experiment
[0049] The wood defect dataset provided in Zenodo selects 7 types of defects to evaluate the proposed method. This dataset has a total of 3482 images, and the defect types include dead knots, live knots, cracks, cracked knots, missing knots, pith, and resin. The method proposed in this embodiment is compared with the existing popular object detection methods, and the difference in the parameter size of each method is small.
[0050] Table 1 is a comparative experiment
[0051]
[0052] From Table 1, we can see the running results of this embodiment and other methods on the same data set. It can be seen that this embodiment is superior to other methods in terms of average precision, and at the same time, the number of parameters and model size are lower, and the amount of calculation is also smaller.
[0053] Example 2
[0054] On the basis of Example 1, in order to verify the generalization of this example, the public dataset NEU-DET, a strip steel surface defect dataset published by Northeastern University, was used. The dataset contains 1,800 images, and the defect types include cracks, pits, plaques, scratches, inclusions, and pressed iron scale.
[0055] Table 2 is a comparative experiment
[0056]
[0057] It can be seen from Table 2 that in the data set of the strip surface, the method proposed in this embodiment has improved various indicators compared with the original method, indicating that the method proposed in this embodiment has good generalization ability.
[0058] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A wood defect recognition method based on YOLOv8n, characterized in that: The following steps are involved: Step 1: Use ADown convolution to replace the original Conv convolution, which reduces the number of model parameters while ensuring the model detection accuracy; Step 2: Use dysample to replace the original neck upsampling, which improves the accuracy without increasing the model complexity; Step 3: Add the MSDA attention module in front of the detection head to improve the ability to capture wood defect information.
2. The wood defect recognition method based on YOLOv8n according to claim 1, characterized in that: In the step 1, the original Conv convolution is replaced by ADown convolution, which is formed by combining the average pooling and maximum pooling operations, and then the Adown convolution is used to replace the downsampling of the Backbone and Neck parts of the model.
3. The wood defect recognition method based on YOLOv8n according to claim 1, characterized in that: In step 2, dysample upsampling is used to replace the two layers of upsampling in the original Neck part, and the input features are resampled to improve the accuracy without increasing the model complexity. Specifically: New grid sampling process: The input features, upsampled features, generated offsets, original grids, and sigmoid functions are represented as X, X', O, G, and σ, respectively. Given a feature map X with a grid sampling size of C×H1×W1 and a sampling set S with a size of 2×H2×W2, where the one-dimensional 2 represents the x and y coordinates, C represents the number of channels, and H represents the number of channels. i represents the height of the i-th feature map, W i Represents the width of the i-th feature map. The grid_sample function resamples the assumed bilinear interpolation X using the positions in S to generate X' of size C×H2×W2. The process is defined as: X' = grid_sample(X,S). The implementation of the sampling set S is as follows: given an upsampling factor s and a feature map X of size C×H×W, a linear layer is used with C and 2s as the input and output channels respectively. 2 , the generated size is 2s 2 The offset O of ×H×W is transformed into 2×sH×sW by pixel shuffling. The sampling set S is the sum of the offset O and the original sampling grid G. The process is defined as: O=linear(X) S=G+O.
4. The wood defect recognition method based on YOLOv8n according to claim 1, characterized in that: In step 2, dysample uses two methods to generate sampling points, namely: static range factor and dynamic range factor; due to the existence of the normalization layer, the value of a specific output feature is usually between [-1, 1], centered at 0, so the local s 2 The range of sampling positions overlaps, which affects the prediction of the boundary. Multiply the offset by a factor of 0.25 and rewrite the formula as follows: O=0.25Linear(X). The dynamic range factor is generated by linearly projecting the input features. The dynamic range is [0, 0.5], centered at 0.25, using the sigmoid function and a static factor of 0.
5. The formula is as follows: O=0.5sigmoid(linear1(X))·linear2(X).
5. The wood defect recognition method based on YOLOv8n according to claim 1, characterized in that: In step 3, add the MSDA attention module before the detection head: The core of MSDA lies in the sliding window attention SWDA, which is described as: X=SWDA(Q,K,V,r), X represents the SWDA output, Q, K and V represent the query matrix, key matrix and value matrix respectively. Each row of the matrix represents a separate query, key and value feature vector. For the query at position (i, j) in the original feature map, SWDA sparsely selects keys and values for self-attention operation in a sliding window centered at (i, j). The expansion rate r is used to control the degree of sparsity.
6. The wood defect recognition method based on YOLOv8n according to claim 5, characterized in that: In step 3, MSDA attention obtains the corresponding query, key, and value through linear projection, and then divides the channels of the feature map into n different heads, and performs multi-scale SWDA operations with different dilation rates in these different heads, which are defined as follows: h i =SWDA(Q i ,K i ,V i ,r i ),1≤i≤n, X=Linear(Concat[h1,...,h n ]), where h i is the output of the ith head, r i is the expansion rate of the ith head, Q i represents the query matrix of the i-th head, K i represents the key matrix of the ith head, V i Represents the value matrix of the i-th head, and the output {h i } i n=1 are connected together and then sent to the linear layer for feature aggregation into X. By setting different dilation rates for different heads, MSDA aggregates semantic information of different scales within the participating receptive field.
Citation Information
Cited By
Wood surface defect lightweight detection method based on synergistic regularization pruning and knowledge distillation
CN122841230A