A method, device and storage device for detecting defects in bamboo strips
By constructing a ResNet detection model and optimizing hyperparameters using the Optuna algorithm, combined with image preprocessing and data augmentation, the problem of low detection accuracy in traditional bamboo chip detection methods is solved, and the accuracy of bamboo chip defect detection is significantly improved.
Patent Information
- Application Number
- CN202310286388.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-22
- Publication Date
- 2025-11-04
- Estimated Expiration
- 2043-03-22
AI Technical Summary
Traditional bamboo strip defect detection methods have low accuracy, cannot meet production needs, and consume a lot of labor costs.
We employ a ResNet detection model combined with the Optuna algorithm to optimize hyperparameters. Through image preprocessing and data augmentation, we construct adaptive threshold segmentation and affine transformation correction. The Optuna algorithm is then used to automatically search for the optimal hyperparameter combination, thereby improving model accuracy.
It significantly improved the accuracy of bamboo strip defect detection, increasing the training accuracy from 94.3% to 99.9% and the testing accuracy from 92.4% to 95.6%.
Smart Images

Figure CN116433613B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of defect detection, and more particularly to a method for detecting defects in bamboo strips. Background Technology
[0002] The processing of bamboo chopsticks involves multiple steps, including material selection, sorting, cleaning, straightening, drying, and cutting. Generally, when the chopsticks are initially formed into semi-finished blanks, screening is necessary to remove chopsticks with defects such as bending, wormholes, unevenness, cracks at the ends, and discoloration to ensure smooth subsequent processing and quality inspection. This process saves significant time for later stages of processing. Currently, sorting and organizing are typically done manually, which is inefficient and labor-intensive. Alternatively, traditional image processing methods are used, but their accuracy is low and cannot meet production demands. Summary of the Invention
[0003] To address the technical problem of low detection accuracy in traditional image processing methods, this application provides a method for detecting defects in bamboo strips, specifically including the following steps:
[0004] S1. Collect images of bamboo strips and perform preprocessing to obtain a processed sample set;
[0005] S2. Construct a ResNet detection model and use the Optuna algorithm to obtain the model hyperparameters;
[0006] S3. Optimize the detection model using hyperparameters to obtain the optimized model;
[0007] S4. Apply the optimized model to detect defects in bamboo strips.
[0008] A storage device that stores instructions and data for implementing a bamboo strip defect detection method.
[0009] A bamboo strip defect detection device includes: a processor and a storage device; the processor loads and executes instructions and data in the storage device to implement a bamboo strip defect detection method.
[0010] The beneficial effect provided by this invention is that it improves the detection accuracy of defects in bamboo strips. Attached Figure Description
[0011] Figure 1 This is a flowchart of the method of the present invention;
[0012] Figure 2 This is a schematic diagram of the model before optimization;
[0013] Figure 3 This is a schematic diagram of the optimized model;
[0014] Figure 4This is a schematic diagram of the hardware device operation according to an embodiment of the present invention. Detailed Implementation
[0015] To make the objectives, technical solutions, and advantages of the present invention clearer, the embodiments of the present invention will be further described below with reference to the accompanying drawings.
[0016] The following is a unified explanation of the terms used in this application:
[0017] (1) Optuna: is an open-source framework for hyperparameter optimization that can automatically search for the best combination of hyperparameters for a neural network model;
[0018] (2) ResNet (Residual Neural Network) is a deep convolutional neural network model. Compared with traditional convolutional neural networks, ResNet adopts the residual learning method, which stacks multiple residual blocks to build a deeper network, thus solving the problem of gradient vanishing and gradient explosion that are easy to occur in deep neural networks.
[0019] This invention provides a method for detecting defects in bamboo strips, specifically including the following steps:
[0020] S1. Collect images of bamboo strips and perform preprocessing to obtain a processed sample set;
[0021] It should be noted that the first step is to put the acquired images into a folder and then preprocess the images in this folder, including grayscale transformation, filtering, thresholding, and other operations. The purpose is to separate the target image from the background and extract the edge features of the image more clearly.
[0022] Specifically, in the preprocessing stage, a series of operations such as grayscale transformation and filtering are performed first. Then, an adaptive threshold is used to binarize the image. Adaptive threshold segmentation is an image segmentation method that determines the threshold based on local grayscale features. Its basic idea is to use different thresholds for different regions to adapt to changes in image grayscale values. The main process of this method is as follows:
[0023] 1. Define a sliding window of fixed size, and use the pixels centered on this window to form a local region;
[0024] 2. For each local region, calculate the average gray value of its pixels, and use it as the threshold for that region;
[0025] 3. Mark pixels with gray values greater than the threshold within the region as foreground, and pixels with gray values less than or equal to the threshold as background;
[0026] 4. Slide the window to the next pixel in the image and repeat steps 2 and 3 until the entire image has been processed.
[0027] Adaptive thresholding segmentation methods can effectively handle regions in an image with varying grayscale values without causing segmentation errors due to inappropriate global thresholds. This allows for the extraction of the Region of Interest (ROI) and the unification of the background color.
[0028] Secondly, the image preprocessing process also includes image correction; image correction is to maintain the consistency of the images, as some images are not uniform due to the rolling of the conveyor belt during shooting, thus creating a good image environment for subsequent training.
[0029] In this application, by finding the smallest bounding box, calculating the rotation angle and displacement, an affine transformation matrix is obtained. The affine transformation is then used to automatically rotate and correct the chopstick image to a horizontal position and move it to the image center. All specified images are then horizontally flipped, vertically flipped, and both horizontally and vertically flipped. Operations such as finding the smallest bounding box can be performed using the OpenCV open-source framework.
[0030] Finally, these four images are saved to the specified output path, completing the image data augmentation. The processed images are then placed in the corresponding positive and negative sample sets, completing the sample set creation. Image data augmentation (image enhancement) is performed from different perspectives, such as contrast and interpolation. Image enhancement expands the dataset, making it richer and covering various sample data, which is beneficial for model training to achieve the expected results.
[0031] S2. Construct a ResNet detection model and use the Optuna algorithm to obtain the model hyperparameters;
[0032] As one embodiment, the construction process of the ResNet detection model is as follows: feature extraction and normalization are performed through convolutional layers and batch normalization layers, then added to the original input features, and finally nonlinear transformation is performed through the ReLU activation function. Finally, multiple residual blocks are used to construct the ResNet detection model.
[0033] Optuna is an automatic hyperparameter optimization library based on the Bayesian optimization algorithm, which can help efficiently search for the optimal combination of hyperparameters. When optimizing ResNet models, Optuna can be used to automatically search for the optimal combination of hyperparameters to improve the model's performance and accuracy.
[0034] The following are the steps for optimizing a ResNet model using the Optuna algorithm:
[0035] 1) Define the objective function. The objective function should accept hyperparameters as input and return the model's accuracy on the validation set. The model is trained on the training set and tested on the validation set, ultimately returning the model's accuracy on the validation set. The hyperparameters include: number of hidden layers, number of neurons in the hidden layers, choice of activation function (ReLU, sigmoid, tanh), drop, batch size, epochs, learning rate, loss function, and optimizer (Adam, sgd, nadam), etc.
[0036] In practice, different distribution functions can be used to define the prior distribution of each hyperparameter; the initial hyperparameters can be sampled from a set of candidate hyperparameters; specifically, Optuna uses sampling methods (such as Gaussian processes or the TPE algorithm) to sample a set of candidate hyperparameters from the prior distribution. These hyperparameters are used to train the model and compute its performance.
[0037] 2) Define the hyperparameter search space. To use Optuna to search for the optimal combination of hyperparameters, you need to specify the search range and type for each hyperparameter. For example, for the learning rate hyperparameter, you can specify its search range as [0.0001, 0.1] and its type as loguniform.
[0038] 3) Run the Optuna optimizer. Using the defined objective function and hyperparameter search space, the Optuna optimizer can be run to search for the optimal combination of hyperparameters. The optimizer will adjust the hyperparameter combination for the next search based on the results of each iteration, ultimately obtaining the optimal hyperparameter combination.
[0039] Specifically, this invention uses the evaluation results to update the prior distribution. This helps to better explore the hyperparameter space. The evaluation results are used as observations of the objective function, and Bayes' theorem is used to compute the posterior distribution. The posterior distribution is used for the next round of sampling so that the hyperparameter space can be better explored; the above steps are repeated until the specified number of iterations or a stopping condition is reached.
[0040] As one example, consider a model f(x, θ), where x is the input and θ is the hyperparameter. The goal is to find the optimal hyperparameter θ* to minimize the error y_v on the validation set.
[0041] Define the hyperparameter space Θ as all possible values of θ, i.e., θ∈Θ.
[0042] Different distribution functions are used to define the prior distribution p(θ) for each hyperparameter. For example, if the hyperparameter is continuous, a Gaussian distribution can be used; if the hyperparameter is discrete, a multinomial distribution can be used.
[0043] The performance of the model is evaluated using the objective function L(θ). The objective function L(θ) is the error of the model on the validation set, i.e.:
[0044] L(θ) = y_v = f(x_v, θ)
[0045] The goal is to minimize L(θ), that is:
[0046] θ*=argminL(θ)(θ∈Θ)
[0047] In each iteration, Optuna uses a Bayesian optimization method to update the prior distribution. Specifically, Optuna constructs the posterior distribution p(θ|D) using previously observed hyperparameters and objective function values (θ1, L(θ1)), ..., (θn, L(θn)), where D = {(θ1, L(θ1)), ..., (θn, L(θn))} are the previously observed data. The posterior distribution p(θ|D) can be calculated using Bayes' theorem:
[0048] p(θ|D)=p(D|θ)*p(θ) / p(D)
[0049] Where p(D|θ) is the probability density function (PDF) of the objective function value given hyperparameter θ, p(θ) is the prior distribution of the hyperparameter, and p(D) is the distribution of all possible marginal probabilities, which can be calculated by integrating p(D)=∫p(D|θ)p(θ)dθ.
[0050] Optuna uses sampling methods to select the next hyperparameter to evaluate. Typically, Optuna uses either a Gaussian process or the TPE algorithm to sample hyperparameters. Specifically, the Gaussian process sampling method models the posterior distribution as a Gaussian distribution and samples from it; the TPE algorithm employs a binary search strategy to optimize the objective function.
[0051] Finally, by repeating the above steps, the optimal hyperparameter θ* can be found to minimize the error y_v on the validation set.
[0052] S3. Optimize the detection model using hyperparameters to obtain the optimized model;
[0053] S4. Apply the optimized model to detect defects in bamboo strips.
[0054] This invention uses the constructed ResNet model and a model with added optimization conditions for image training, and obtains the following changes in training set loss and accuracy with the number of iterations: Figure 2-3 As shown; Figure 2 This is a schematic diagram of the model before optimization;
[0055] Figure 3 This is a schematic diagram of the optimized model;
[0056] Using images for model training and analyzing the model optimization effect, it can be seen that after optimization, the training accuracy increased from 94.3% to 99.9%, and the test set accuracy increased from 92.4% to 95.6%. Adding optimization conditions further improved the model accuracy.
[0057] Please see Figure 4 , Figure 4 This is a schematic diagram of the hardware device in operation according to an embodiment of the present invention. The hardware device specifically includes: a bamboo strip defect detection device 401, a processor 402, and a storage device 403.
[0058] A bamboo strip defect detection device 401: The device 401 implements the bamboo strip defect detection method.
[0059] Processor 402: The processor 402 loads and executes the instructions and data in the storage device 403 to implement the bamboo strip defect detection method.
[0060] Storage device 403: The storage device 403 stores instructions and data; the storage device 403 is used to implement the bamboo strip defect detection method.
[0061] The key point of this invention is:
[0062] 1. In the image preprocessing process, in order to suppress salt-and-pepper noise and more clearly preserve image edge information, median filtering is more effective; in order to reduce the influence of factors such as uneven illumination or uneven gray value distribution, there is a corresponding threshold for each position of the input matrix, and adaptive threshold segmentation is adopted.
[0063] 2. In the image enhancement process, the image is first normalized to correct the tilt, and then the resulting image is expanded. By rotating the image at different angles, different placement conditions during conveyor belt transportation are simulated to achieve the image augmentation effect.
[0064] 3. In the process of building the model, in order to maximize the effect of the ResNet model, the Optuna algorithm is used to find the best combination of parameters to optimize the model, which will greatly improve the detection accuracy during training.
[0065] The beneficial effect of this invention is that it improves the detection accuracy of bamboo strip defects.
[0066] 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 principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A method for detecting defects in bamboo strips, characterized in that: Includes the following steps: S1. Collect images of bamboo strips and perform preprocessing to obtain a processed sample set; S2. Construct a ResNet detection model and use the Optuna algorithm to obtain the model hyperparameters; S3. Optimize the detection model using hyperparameters to obtain the optimized model; S4. Apply the optimized model to detect defects in bamboo strips; The construction process of the ResNet detection model is as follows: feature extraction and normalization are performed through convolutional layers and batch normalization layers, then added to the original input features, and finally nonlinear transformation is performed through the ReLU activation function. Finally, multiple residual blocks are used to construct the ResNet detection model. The process of obtaining the hyperparameters of the ResNet detection model using the Optuna algorithm is as follows: S21. Define an optimization objective function; the input of the optimization objective function is the initial hyperparameters of the ResNet detection model, and the output is the detection accuracy of the ResNet detection model; S22. Define the search space for hyperparameters of the ResNet detection model; S23. Based on the hyperparameter search space, use the Optuna optimizer to iteratively search for hyperparameters and obtain the optimal hyperparameters.
2. The method for detecting defects in bamboo strips as described in claim 1, characterized in that: The image preprocessing includes grayscale transformation, filtering, and adaptive threshold segmentation.
3. The method for detecting defects in bamboo strips as described in claim 1, characterized in that: The image preprocessing also includes image correction.
4. The method for detecting defects in bamboo strips as described in claim 1, characterized in that: The image preprocessing also includes image enhancement.
5. The method for detecting defects in bamboo strips as described in claim 2, characterized in that: The specific process of adaptive threshold segmentation is as follows: S11. Define a sliding window of fixed size, and use the pixels centered on this window to form a local region; S12. For each local region, calculate the average gray value of its pixels and use it as the threshold for that region. S13. Mark pixels with gray values greater than the threshold in the region as foreground, and pixels with gray values less than or equal to the threshold as background; S14. Slide the window to the next pixel of the image and repeat steps S12 and S13 until the entire image has been processed.
6. The method for detecting defects in bamboo strips as described in claim 3, characterized in that: The specific process of image correction is as follows: by finding the smallest rectangle of the outline, calculating the rotation angle and displacement, the affine transformation matrix is obtained. The bamboo strip image is automatically rotated and corrected to a horizontal position and moved to the center of the image using the affine transformation.
7. A storage device, characterized in that: The storage device stores instructions and data for implementing any one of the bamboo strip defect detection methods described in claims 1 to 6.
8. A bamboo strip defect detection device, characterized in that: include: A processor and a storage device; the processor loads and executes instructions and data in the storage device to implement any one of the bamboo strip defect detection methods according to claims 1 to 6.
Citation Information
Patent Citations
Wind power generation device blade defect detection method based on unmanned aerial vehicle image analysis
CN115187878A
Fabric defect detection method based on deep convolutional neural network and visual saliency
WO2019104767A1