Casting surface defect detection method and device based on machine learning
By employing a machine learning-based method for detecting surface defects in castings, and utilizing image preprocessing and feature extraction, combined with an improved slime mold algorithm to optimize SVM parameters, the method solves the problems of low recognition rate and low efficiency in traditional detection methods, achieving efficient and accurate identification of surface defects in castings.
Patent Information
- Application Number
- CN202311393893.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-25
- Publication Date
- 2026-04-17
AI Technical Summary
Existing methods for detecting surface defects in castings have low recognition rates, high costs, and low efficiency. Traditional methods cannot effectively capture complex image patterns, which affects the quality and service life of castings.
A machine learning-based method for detecting surface defects in castings is adopted. By optimizing the regularization parameter and radial basis kernel function in SVM through image preprocessing, feature extraction, and an improved slime mold algorithm, the classification accuracy is improved.
It enables rapid and accurate identification of surface defects in castings, improves detection efficiency and accuracy, avoids human error, and enhances casting quality and service life.
Smart Images

Figure CN121883334A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image recognition technology, specifically to a method and apparatus for detecting surface defects in castings based on machine learning. Background Technology
[0002] Castings are widely used industrial products, and their quality directly affects the performance and safety of the finished product. During the casting manufacturing process, surface defects such as bubbles, cracks, and shrinkage cavities are unavoidable. These defects reduce the quality of the casting and may even cause damage during use. Therefore, accurately detecting and identifying surface defects in castings is crucial for ensuring casting quality and service life.
[0003] Currently, commonly used methods for detecting surface defects in castings include visual inspection and X-ray inspection. However, these methods suffer from problems such as low recognition rate, high cost, and low efficiency.
[0004] In the existing technology, Zheng Xiaoling, in her paper "Surface Defect Detection Method for Aluminum Die Castings Using Machine Vision" (Journal of Huaqiao University, 2016, 37(2): 139-144), disclosed a method for establishing fault identification rules based on four dimensions: area, average brightness, difference between average brightness and grayscale curve, to segment suspicious defect areas, and applied it to aluminum die castings. However, this method has too few feature dimensions to capture more complex and abstract patterns and information in the image, thus limiting the accuracy of surface defect identification.
[0005] Therefore, developing a new machine learning-based method and device for identifying surface defects in castings to improve the accuracy of surface defect identification is an urgent problem to be solved in the industrial manufacturing field. Summary of the Invention
[0006] The purpose of this invention is to provide a method and apparatus for detecting surface defects in castings based on machine learning. By image preprocessing, feature extraction, and improving the regularization parameters and radial basis kernel function in the SVM algorithm, the accuracy of classification is improved, thereby solving the problems mentioned in the background art.
[0007] To achieve the above objectives, the present invention provides the following technical solution:
[0008] In a first aspect, this disclosure provides a method for detecting surface defects in castings based on machine learning, characterized in that it includes:
[0009] S1, Obtain the first image of the casting;
[0010] S2, preprocess the first image set and generate the second image;
[0011] S21, perform grayscale transformation on the first image; the grayscale transformation is a non-linear grayscale transformation;
[0012]
[0013] Where r is the gray value of the input image, s is the gray value of the output image, and a, b, and c are constants, with a ranging from [0, 255], b ranging from [1, 10], and c ranging from [1, 10].
[0014] S22, The image after grayscale conversion is filtered; the filtering process is a bilateral filtering process.
[0015] S23, perform image segmentation on the filtered image to generate the second image;
[0016] S3, input the second image into the pre-trained SVM-RFE model for casting surface features to extract features and construct a feature set;
[0017] S4, input the feature set into the pre-trained ISMA-SVM model for classifying surface defects of castings;
[0018] S5 outputs the classification results.
[0019] Preferably, S23 includes:
[0020] S231, Smooth the image according to the H×L smoothing operator and output the result f(J(h,l));
[0021] S232, Calculate the threshold T based on the result f(J(h,l));
[0022] T=(1-r)f(J(h,l))
[0023] Where r is a random number, and its value ranges from [0.1, 0.2].
[0024] S233, The image is segmented according to the threshold T to generate the second image;
[0025]
[0026] J(x,y) represents the gray value at the point (x,y), and T represents the threshold to be found.
[0027] Preferably, step S5 further includes:
[0028] S51. Initialize ISMA parameters; the parameters include iteration number T, cell number N, sensing range, and maximum step size;
[0029] S52. Initialize SVM parameters, randomly generate c and γ;
[0030] S53. Use an SVM classifier to classify the training set and calculate the classifier's performance metric as the fitness function;
[0031] S54. Update the position of each bacterial cell;
[0032] S55. Calculate the new c and γ parameters based on the stated position;
[0033] S56. Stop condition judgment: Determine whether the number of iterations has reached the set value. If it has, end the algorithm; otherwise, return to step S53.
[0034] S57. Output the optimal solution.
[0035] Preferably, S51 further includes:
[0036] The total number of slime molds was initialized using Sobol sequences.
[0037] Preferably, S54 further includes:
[0038] The inertia weight value adopts a random inertia weight value, and the formula is:
[0039] ω=ω max -(ω max -ω min )r1+r2δ
[0040] Where, ω max It is the maximum value of the random inertia weight, ω min ω is the minimum value of the random inertia weight, r1 is a random number in [0,1], r2 is a random number in a normal distribution, and δ (standard deviation) is used to measure the degree of deviation between the random inertia weight ω and its mathematical expectation.
[0041] Preferably, S54 includes:
[0042] Position update formula:
[0043]
[0044] Among them, U B and L B These represent the upper and lower boundaries of the search, respectively; rand and r are random values in [0,1]; z is a set value; v b The range is [-a, a], v c To decrease linearly from 1 to 0, t represents the current iteration number, X b Let X(t) represent the current location of the slime mold, where the odor concentration is currently the highest. A and X BThis represents two randomly selected slime mold locations, and W represents the slime mold weight.
[0045] p = tanh|S(i) - D E |
[0046] Where S(i) represents the fitness of X(t), and DE represents the best fitness value obtained from all iterations;
[0047]
[0048] Where maxt is the maximum number of iterations.
[0049] Preferably, S55 includes:
[0050] The SVM classifier is trained, and cross-validation is performed on the training and test sets.
[0051] A second aspect of this disclosure provides a method and apparatus for detecting surface defects in castings based on machine learning, characterized in that it is used for:
[0052] The acquisition unit is used to acquire the first image of the casting;
[0053] A processing unit is configured to preprocess the first image set and generate a second image;
[0054] The extraction unit is used to input the second image into a pre-trained SVM-RFE model for casting surface features to extract features and construct a feature set;
[0055] A classification unit is used to input the feature set into a pre-trained ISMA-SVM model for classifying surface defects in castings.
[0056] The output unit is used to output the classification results.
[0057] Preferably, the classification unit is further used for:
[0058] S41. Initialize ISMA parameters; the parameters include iteration number T, cell number N, sensing range, and maximum step size;
[0059] S42. Initialize SVM parameters, randomly generate c and γ;
[0060] S43. Use an SVM classifier to classify the training set and calculate the classifier's performance metric as the fitness function;
[0061] S44. Update the position of each bacterial cell;
[0062] S45. Calculate the new c and γ parameters based on the stated position;
[0063] S46. Stop condition judgment: Determine whether the number of iterations has reached the set value. If it has, end the algorithm; otherwise, return to step S43.
[0064] S47. Output the optimal solution.
[0065] Preferably, the classification unit is used for:
[0066] The inertia weight value adopts a random inertia weight value, and the formula is:
[0067] ω=ω max -(ω max -ω min )r1+r2δ
[0068] Where, ω max It is the maximum value of the random inertia weight, ω min ω is the minimum value of the random inertia weight, r1 is a random number in [0,1], r2 is a random number in a normal distribution, and δ (standard deviation) is used to measure the degree of deviation between the random inertia weight ω and its mathematical expectation.
[0069] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0070] 1. Machine learning-based methods for detecting surface defects in castings utilize image processing, feature extraction, and classifier training to quickly and accurately identify surface defects. Compared to traditional manual inspection methods, machine learning algorithms improve inspection efficiency and accuracy, avoiding human error and inefficiency.
[0071] 2. The slime mold algorithm can optimize model parameters through global search, improving classifier performance; while the SVM model has efficient classification capabilities, effectively distinguishing between defective and normal castings in the feature space. The parameters in the SVM affect the classification accuracy. Therefore, the improved slime mold algorithm is used to optimize the regularization parameter c and the radial basis function γ of the SVM, obtaining the optimal values of c and γ. With these optimal parameters, the SVM model is constructed, improving the accuracy of SVM in identifying and classifying surface defects in castings.
[0072] 3. By introducing stochastic inertia weights and a new position update formula into ISMA, the algorithm exhibits better stability and reliability during the search process, avoiding getting trapped in local optima and improving its global search capability. It also increases the randomness and diversity of the search process, enhancing the algorithm's exploratory ability. The SVM is trained using accuracy as a fitness function, optimizing the SVM parameters and iteratively determining the regularization and radial basis function parameters corresponding to the highest output fitness value. ISMA-SVM is constructed by optimizing the regularization and radial basis function parameters in the SVM. Compared to BP neural networks and other SVM models, ISMA-SVM improves the accuracy of casting surface defect identification. Attached Figure Description
[0073] Figure 1 This is a schematic diagram of the casting surface defect detection method based on machine learning according to the present invention.
[0074] Figure 2 This is a schematic diagram of the structure of the casting surface defect detection device based on machine learning according to the present invention.
[0075] Figure 3 This is a schematic diagram of the process of optimizing SVM using the improved slime mold algorithm of the present invention.
[0076] Figure 4 This is a diagram showing the porosity defects on the surface of the casting of the present invention;
[0077] Figure 5 This is a diagram of surface crack defects in the casting of the present invention;
[0078] Figure 6 This is a diagram showing the surface porosity defects of the casting according to the present invention;
[0079] Figure 7 This is a comparison chart of the classifier recognition results of the present invention; Detailed Implementation
[0080] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0081] Please see Figures 1 to 7 This invention provides a method and apparatus for detecting surface defects in castings based on machine learning, the technical solution of which is as follows:
[0082] See Figure 1This invention provides a machine learning-based method for detecting surface defects in castings. This method can be executed by a device, which can be implemented using software and / or hardware. In a specific embodiment, it includes steps S1-S5.
[0083] Specifically, S1, acquire a first image of the casting; S2, preprocess the first image set and generate a second image; S3, input the second image into a pre-trained casting surface feature SVM-RFE model to extract features and construct a feature set; S4, input the feature set into a pre-trained casting surface defect ISMA-SVM model for classification; S5, output the classification result.
[0084] Step S1: Obtain the first image of the casting.
[0085] Castings refer to metal or non-metal products manufactured through casting processes, typically possessing complex shapes and internal structures. Castings usually consist of casting materials (such as aluminum, copper, iron, steel, brass, tin, etc.) and a mold. Molten casting material is poured into the mold, allowing it to cool and solidify to form the desired shape and size. Due to limitations in casting processes, castings are prone to surface defects, including porosity, looseness, and cracks. (See also...) Figure 4 , Figure 5 , Figure 6 .
[0086] The first image is a photograph of the casting surface. The imaging equipment is selected based on specific circumstances to ensure high-quality images of the casting. When capturing the image, ensure the casting surface is clean, smooth, and positioned at the appropriate angle. Cleaning the casting removes dust, grease, and other impurities, ensuring a clear and visible surface image. The parameters of the image acquisition equipment are set according to specific needs and the characteristics of the casting. This includes adjusting parameters such as exposure time, focus, and white balance to obtain images with high clarity and appropriate contrast. The casting sample is placed in the appropriate position, the image acquisition equipment is activated, and an image of the casting is captured. Ensure the image covers the entire casting surface and that details in the image are clearly visible.
[0087] It should be noted that in practical applications, it may be necessary to acquire multiple images of the casting from different angles or sides in order to capture more comprehensive information about defects on the casting surface.
[0088] Step S2: Preprocess the first image set and generate the second image;
[0089] The preprocessing of the first image includes grayscale transformation, filtering, and image segmentation.
[0090] Step S21: Perform grayscale transformation on the first image; the grayscale transformation is a non-linear grayscale transformation.
[0091]
[0092] Where r is the gray value of the input image, s is the gray value of the output image, and a, b, and c are constants, with a ranging from [0, 255], b ranging from [1, 10], and c ranging from [1, 10].
[0093] Grayscale transformation maps or alters the grayscale levels of an image to adjust visual effects such as brightness, contrast, and hue. Specifically, grayscale transformation operates on the grayscale value of each pixel in the image, converting the original grayscale level into a new grayscale level.
[0094] Nonlinear grayscale transformation refers to using nonlinear functions to map the grayscale values of an input image to the grayscale values of an output image. Nonlinear grayscale transformation can use various nonlinear functions to map the grayscale values of the input image, achieving more flexible and precise grayscale adjustments. This allows nonlinear grayscale transformation to better meet specific image processing needs. Simultaneously, nonlinear grayscale transformation can handle images with high dynamic range, enhance local image contrast, and reduce the loss of detail in situations with large brightness differences. Nonlinear grayscale transformation, especially logarithmic transformation, can enhance details at low grayscale levels. This is extremely helpful for the visualization and analysis of important low-brightness details in images (such as details in dark areas).
[0095] It should be specifically explained that for each pixel in the first image, its grayscale value r is obtained. According to the given nonlinear grayscale transformation formula, the input grayscale value r is converted into the output grayscale value s. Based on specific requirements and the desired transformation effect on the image, appropriate parameters a, b, and c are selected. In this embodiment, the value range of a is [0, 255], the value range of b is [1, 10], and the value range of c is [1, 10]. These parameters can be adjusted based on practical application experience to obtain the desired grayscale transformation effect. For example, increasing the value of a can enhance the contrast of the image, and adjusting the value of b can change the brightness distribution of the image.
[0096] Step S22: The image after grayscale transformation is filtered; the filtering process is a bilateral filtering process.
[0097] Filtering is a common image processing technique used to alter the spectral characteristics of an image or reduce noise. Bilateral filtering works by adjusting the filter weights based on the spatial distance between pixels and the differences in their grayscale values. This means the filter preserves edge information more effectively and protects texture details.
[0098] The formula for bilateral filtering is:
[0099]
[0100] Where Mx is the pixel value at coordinate point x in the image, My is the pixel value at the endpoint, Mx' is the pixel value at coordinate point x after bilateral filtering, and Wx represents the standardization factor.
[0101]
[0102] in, The factor represents a spatial distance weighting function. The factor represents the weighted function of gray-level difference.
[0103]
[0104]
[0105] Where d(x,y) represents the Euclidean distance between the center point x and its neighboring point y, δ(I(x), I(y)) represents the pixel value difference between points x and y, and δ s To control the standard deviation of the spatial neighborhood, δ r To control the standard deviation of the pixel domain's effective range.
[0106] It should be noted that bilateral filtering can effectively reduce noise in images, especially Gaussian noise and salt-and-pepper noise. It can preserve image details and edge information while reducing noise, avoiding a blurring effect.
[0107] Step S23: Perform image segmentation on the filtered image to generate the second image;
[0108] Image segmentation is the process of dividing an image into multiple regions with specific attributes or features. The second image is the new image obtained by segmenting the filtered image.
[0109] Step S231: Smooth the image according to the H×L smoothing operator and output the result f(J(h,l));
[0110] The image is smoothed using the H×L smoothing operator. This helps reduce noise and detail in the image and makes it smoother.
[0111] Step S232: Calculate the threshold T based on the result f(J(h,l));
[0112] T=(1-r)f(J(h,l))
[0113] Where r is a random number, and its value ranges from [0.1, 0.2].
[0114] Step S233: Segment the image according to the threshold T to generate the second image;
[0115]
[0116] Where J(x,y) represents the grayscale value at point (x,y), and T represents the threshold being sought. 255 means changing the grayscale value of this point to 255, which will be displayed as white; 0 means changing the grayscale value of this point to 0, which will be displayed as black.
[0117] The advantages of threshold segmentation are obvious: it is intuitive, easy to interpret, fast to calculate, and simple to operate.
[0118] Step S3: Input the second image into the pre-trained SVM-RFE model for casting surface features to extract features and construct a feature set;
[0119] Among them, the surface feature SVM-RFE of the casting is a support vector machine recursive feature elimination. The selection process of this algorithm is to first calculate a weight for each feature and sort them, then remove the feature corresponding to the lowest weight, then use the remaining features to train the learner again, and continue to remove the feature corresponding to the lowest weight in the sorting results, and keep iterating until the number of remaining features is the number specified by the user.
[0120] The extracted features include LBP features, Hu moment features, HOG features, and geometric shape features.
[0121] Among them, geometric features include area, perimeter-to-area ratio, length-to-width ratio, rectangularity, sharpness, and average brightness.
[0122] It needs to be explained in detail that:
[0123] (1) Area
[0124] Area refers to the number of pixels in the defective region of an image, and this value can be obtained by counting the total number of these pixels.
[0125] (2) Perimeter-to-area ratio
[0126] Both the area and perimeter of a defect depend only on its boundaries and are unrelated to the grayscale value of the defect portion. The perimeter is generally more effective at distinguishing simple shapes. The ratio of perimeter to area, however, can describe the shape characteristics of the defect; for example, this ratio is smaller for circular or oblong shapes.
[0127] (3) Aspect Ratio
[0128] The aspect ratio refers to the ratio of the length to the width of the smallest bounding rectangle of the defective portion of a casting. The calculation formula is as follows:
[0129]
[0130] Where l represents length and w represents width.
[0131] (4) Rectangularity
[0132] Rectangularity refers to the degree to which a defective area in a casting fills its circumscribed rectangle. It is generally expressed as the ratio of the area of the defective area to the area of the smallest circumscribed rectangle. The calculation formula is as follows:
[0133]
[0134] Where S0 represents the area of the defective portion of the casting, S represents the area of the minimum bounding rectangle of the defect, and when R = 1, it indicates that the shape is rectangular. At that time, the shape is circular.
[0135] (5) Sharpness
[0136] Sharpness refers to the sharpness of the edge endpoints of the defect area, and the calculation formula is as follows:
[0137]
[0138] Where P represents its sharpness, S represents the area of the defective part of the casting, and S1 and S2 represent the area of the part where the length of the two ends of the casting defect is 1 / 4 of the maximum distance between two points in the defect area.
[0139] (6) Average brightness
[0140] Average brightness refers to the average brightness of the defective area of a casting.
[0141] LBP (Latent Backpropagation) features are feature descriptors used in image processing and computer vision. They are local features used to describe the texture information of each pixel in an image. The basic idea of LBP features is to compare the gray value of each pixel with the gray values of its neighboring pixels and encode the comparison result into a binary number. In this way, the LBP value of each pixel can be obtained, and then the LBP value can be used to describe the texture features of the image.
[0142] Hu matrix features are a method for describing image features, primarily used for image recognition and image similarity comparison. Hu matrix features are invariant to rotation, translation, and scale. By calculating and comparing Hu matrix features, the similarity between images can be quantified.
[0143] The main purpose of HOG features is to calculate gradients in the image after grayscale conversion and normalization to obtain gradient information. Image normalization is then performed to eliminate the effects of lighting variations and shadows.
[0144] First, the image is normalized to eliminate the effects of lighting changes and shadows.
[0145] i(x,y)=i(x,y) ζ
[0146] The value of ζ is typically taken as 0.5, and the gradient magnitude G(x,y) and gradient direction θ(x,y) are solved.
[0147]
[0148]
[0149] Where Gx(x,y) and Gy(x,y) represent the horizontal and vertical gradients at pixel (x,y) in the image, respectively.
[0150] G x (x,y)=H(x+1,y)-H(x-1,y)
[0151] G y (x,y)=H(x,y+1)-H(x,y-1)
[0152] Where H(x,y) is the pixel value corresponding to pixel point (x,y) in the image.
[0153] It should be noted that the extracted HOG features have a high dimensionality. Directly using them in subsequent processes would negatively impact the efficiency of casting defect identification and place a significant burden on the computer. Therefore, dimensionality reduction of the HOG features is necessary. In this embodiment, PCA (Principal Component Analysis) is used for dimensionality reduction. PCA is a common dimensionality reduction method. The process involves finding a projection space and then projecting the dataset onto this space using a set of basis vectors. Because these basis vectors are pairwise orthogonal, projection eliminates redundancy and correlation between variables. The uncorrelated variables after projection are linear combinations of the original variables. This allows the dimensionality-reduced features to retain more original data information; these uncorrelated variables are the principal components of the dataset.
[0154] Specifically, the first step is to train an SVM-RFE model for casting surface features to obtain a labeled training dataset. The dataset consists of feature vectors and corresponding class labels. The RFE algorithm selects the most relevant features from the dataset. RFE ranks the features according to their importance based on the SVM model and progressively eliminates lower-ranked features. This process is iterative until a predefined number of features is reached. In each iteration of RFE, the SVM model is used to train the features, and the features are ranked according to their importance. Based on the feature ranking, the least important features are progressively eliminated, starting with the least important ones, until a predefined number of features is reached. Using the remaining most important features as input, the dataset is divided into training and validation sets. The SVM model is used to train the training set, optimizing model performance by adjusting hyperparameters and kernel function selection. The validation set is used to evaluate model performance, using various metrics such as accuracy, recall, and F1 score. The model is then adjusted based on the evaluation results. After completing model training and evaluation, the model can be used to predict and classify new, unknown data.
[0155] It should be noted that, in some specific embodiments, the original data sample set is assumed to be... y i ∈{1, 2, 3, ..., M}, x i ∈R D M represents the number of categories, D represents the number of features, and N represents the number of data samples.
[0156] (1) Initialize the original feature set H = {1,2,3,...,D};
[0157] (2) Combine samples with different defect types in the training sample set in pairs to form a total of M(M-1) / 2 sample subsets;
[0158] (3) Based on the above sample subset, construct M(M-1) / 2 learning classifiers;
[0159] (4) Iterate the following process until the feature set H is empty;
[0160] 1) Obtain subsamples xj, and then train an SVM model based on these subsamples to obtain the weights Wj corresponding to each subsample;
[0161] 2) Calculate the ranking criterion score gj for all features, and find the feature corresponding to the minimum value;
[0162] 3) Update the optimal feature set Best_t;
[0163] 4) Remove the feature corresponding to the minimum weight from the feature set H;
[0164] It's important to note that after removing a feature, the accuracy of the learned classifier needs to be evaluated again. This evaluation can result in three outcomes: increased accuracy, decreased accuracy, or no change in accuracy. Our goal is to increase accuracy. If the accuracy remains unchanged, the mutual information method can be used to reselect the optimal feature subset. The formula for calculating mutual information is listed below. If the accuracy decreases, it indicates that the removed feature was relatively important and should not be removed.
[0165] Mutual information can be used to measure the correlation between defect features and categories. The calculation formula is as follows:
[0166]
[0167] Where I(X,Y) represents the correlation between variables X and Y, H(Y) represents the entropy of variable Y, and H(YX) represents the uncertainty of Y given that X is known. The formula for calculating H is:
[0168]
[0169] The second image is input into the trained SVM-RFE model for casting surface features. The extracted feature vectors are processed accordingly, the importance of each feature is calculated, and the most relevant features are selected to construct a feature set. These feature sets are used for subsequent classification.
[0170] Step S4: Input the feature set into the pre-trained ISMA-SVM model for casting surface defects for classification; firstly, the ISMA-SVM model for casting surface defects must be constructed. (See also...) Figure 3 .
[0171] Step S41, initialize ISMA parameters; the parameters include iteration number T, cell number N, sensing range, and maximum step size;
[0172] The number of iterations (T) represents the total number of iterations the algorithm performs, determining its convergence speed and search capability. In practice, the number of iterations can be set appropriately based on the complexity of the problem and the limitations of computational resources.
[0173] Number of cells (N): Cells are the explorers of the search space in the ISMA algorithm. The number of cells affects the breadth and diversity of the search. Generally, a larger number of cells results in a wider search range, but also increases computational overhead. A reasonable number of cells can be chosen based on the problem size and computational resources. Perception range: The perception range determines the neighborhood that a cell can perceive, i.e., the search range around each cell. The perception range is usually a positive number, representing the radius of a circle or sphere centered on the cell. A larger perception range allows the cell to perceive more surrounding information, enhancing its search capability. Maximum step size: The maximum step size refers to the maximum distance each cell can move in each iteration. It controls the movement magnitude when updating its position. A larger maximum step size can increase the breadth of the search, but may also cause the search process to skip the optimal solution.
[0174] It should be noted that the Sobol sequence method was used to initialize the slime mold colony. The steps for initializing the slime mold colony are as follows:
[0175] (1) Determine relevant parameters: Determine the number of bacteria N and the dimension of the problem (D).
[0176] The number of cells determines the length of the generated Sobol sequence, and the dimension of the question determines the dimension of each specific question vector.
[0177] (2) Generate Sobol sequence: Use the Sobol sequence generation algorithm to generate an N×D matrix, where each row represents the position vector of a cell.
[0178] (3) Scaling and transformation: Scaling and transformation of the generated Sobol sequence according to the requirements of the problem.
[0179] In a specific embodiment, if the range of variables in the problem is within a specific interval, each element in the Sobol sequence can be linearly scaled to the target interval.
[0180] (4) Initialize the total number of slime molds: Based on the generated Sobol sequence and the converted values, use each row as a position vector of a bacterial cell to initialize the position of the total number of slime molds.
[0181] Sobol sequences are low-discrepancy sequences that, compared to random sampling, more uniformly cover the entire sampling space in multidimensional space. Therefore, the bacterial population is more spatially uniformly distributed, which is beneficial for searching a wider solution space. Furthermore, the generation process of Sobol sequences is based on the selection of integer grid points, resulting in low correlation between points in the sequence. The bacterial population is relatively independent in location, reducing interference from correlations and facilitating better exploration of the solution space. The generation process of Sobol sequences uses a numerical recursive algorithm, which can generate uniform sampling points in high-dimensional space with a small sample size. Compared to traditional random sampling, Sobol sequences provide better mixing in high-dimensional space, making them more efficient in solving high-dimensional problems.
[0182] Step S42: Initialize SVM parameters and randomly generate c and γ;
[0183] SVM is a supervised learning algorithm widely used for classification and regression problems. The radial basis function (RBF) kernel is chosen for SVM. The specific formula is:
[0184] K(x i ,x j )=exp(-γ||x i -x j || 2 )
[0185] It should be noted that radial basis function kernels have good generalization ability, few internal parameters, and fast speed.
[0186] The initialization of SVM parameters includes the regularization parameter c and the kernel function parameter γ. First, the range of these parameters is determined. c is the regularization parameter in SVM, used to control the degree of penalty for misclassified samples during classifier training. γ is the kernel function parameter, used to control the influence of sample points on the decision boundary. Values of c and γ are randomly generated based on the determined range. In this embodiment, the value range of c is [0.01, 30], and the value range of γ is [0.001, 30].
[0187] Step S43: Use an SVM classifier to classify the training set and calculate the classifier's performance metric as the fitness function.
[0188] This process involves training an SVM classifier model using the training set data. The trained classifier model is then used to make predictions on the training set, and the predicted labels are obtained. In some specific embodiments, common performance metrics include accuracy, precision, recall, and F1 score. One or more performance metrics are selected as the appropriateness function.
[0189] For SVM classification training, cross-validation is performed on the training and test sets.
[0190] It's important to note that cross-validation allows for a more accurate assessment of the generalization ability of SVM classification models. Traditional training and test set splits can lead to overfitting or underfitting on specific datasets. Cross-validation, however, allows training and evaluation to be run on multiple different combinations of training and test sets, providing insights into the model's performance under varying data distributions. Cross-validation allows for efficient use of limited data. While the dataset is divided into training and test sets, since each sample is used for training and testing in a different subset, all samples in the dataset are fully utilized. This is particularly beneficial for problems with relatively few samples, improving the model's generalization ability and robustness. Cross-validation is also widely used for parameter tuning in SVM models. By training and evaluating models with different parameter combinations on each subset, the optimal parameter configuration can be found. The results of cross-validation allow for the selection of the best parameter combination to achieve optimal model performance. Cross-validation also allows for the evaluation of performance differences across different data subsets, thus determining the model's stability. If the model performs consistently across different subsets, it indicates good stability. Traditional training and test set splits can lead to inconsistent model performance on the test set, especially with limited test data. Cross-validation provides a more reliable assessment, reduces randomness and uncertainty caused by dataset partitioning, and also reduces the risk of overfitting.
[0191] Step S44: Update the position of each bacterial cell;
[0192] The inertia weight value uses a random inertia weight value, and the formula is as follows:
[0193] ω=ω max -(ω max -ω min )r1+r2δ
[0194] Where, ω max It is the maximum value of the random inertia weight, ω min ω is the minimum value of the random inertia weight, r1 is a random number in [0,1], r2 is a normally distributed random number, and δ is the standard deviation, which is used to measure the degree of deviation between the random inertia weight ω and its mathematical expectation.
[0195] The introduction of random inertia weights increases the randomness of the algorithm, making it more exploratory. Inertia weights are typically fixed values or decrease linearly or non-linearly, which can lead to getting trapped in local optima. Random inertia weights can reduce the influence of local optima to some extent, helping the algorithm to better explore the search space. Using random inertia weights helps balance the trade-off between global and local search. In optimization problems combining SVM and slime mold algorithms, random inertia weights can improve the algorithm's local search capability and accelerate its convergence speed. By introducing randomness, random inertia weights allow the algorithm to better adapt to different environments during the search process, more easily escape local optima, and move closer to the global optimum. Random inertia weights help maintain diversity in the search process, preventing the algorithm from getting trapped in early convergence or premature convergence. By randomly changing the inertia weights in each iteration, the diversity in the algorithm's search space can be increased, preventing the algorithm from converging to the same or similar solutions. This helps improve the algorithm's global search capability and avoids over-reliance on local optima.
[0196] It should be specifically noted that using randomized inertia weights in the improved combination of the slime mold algorithm and SVM can enhance the algorithm's exploration ability, accelerate convergence speed, and maintain diversity. By introducing randomness, randomized inertia weights can help the algorithm better cope with complex optimization problems, improving its performance and robustness.
[0197] The position of the bacterial cell is updated based on the random inertia weight value, and the update formula is as follows:
[0198]
[0199] Among them, U B and L B These represent the upper and lower boundaries of the search, respectively; rand and r are random values in [0,1]; z is a set value; v b The range is [-a, a], v c The odor concentration decreases linearly from 1 to 0, where t represents the current iteration number, Xb is the single location with the highest odor concentration, and X(t) represents the current location of the slime mold. A and X B ω represents two randomly selected slime mold locations, and ω represents the slime mold weight.
[0200] p = tanh|S(i) - D E |
[0201] Where S(i) represents the fitness of X(t), and D E This represents the optimal fitness value obtained from all iterations;
[0202]
[0203] Where T is the maximum number of iterations.
[0204] Step S45: Calculate new c and γ parameters based on the position;
[0205] Based on the updated position, the current c and γ can be calculated. If the c and γ obtained in the current iteration are better than the previous c and γ, then the optimal parameter configuration is updated to the parameter configuration of the current iteration, i.e., the values of c and γ. Otherwise, the original optimal parameter configuration is retained.
[0206] Step S46, Stop condition judgment: Determine whether the number of iterations has reached the set value. If it has, end the algorithm; otherwise, return to step S43.
[0207] The condition for judgment is to execute the next step when t=T, that is, the iteration is completed. If the iteration is not completed, return to step S43 to continue the iteration until the iteration is completed.
[0208] Step S47: Output the optimal solution.
[0209] After the iteration is complete, the optimal c and γ are output based on the function value with the highest appropriate value.
[0210] It should be noted that the improved slime mold algorithm was optimized to obtain the optimal values of c and γ, and an SVM classifier was constructed using these parameters. The model construction is now complete.
[0211] In this process, the feature set obtained in step S3 is input into the ISMA-SVM model for casting surface defects, and the feature set is classified by the ISMA-SVM model for casting surface defects.
[0212] It should be noted that SVM is a binary classification model. When there are more than two types to be classified, multiple SVM models need to be built for classification.
[0213] Step S5: Output the classification results.
[0214] The classification result can be obtained based on the model's output. Specific Implementation Example 1:
[0216] The produced castings are iron castings, and surface defects are identified. Common surface defects in iron castings include porosity, cracks, and looseness. A dataset of 150 images each of cracked, loose, and defective samples, and 150 defect-free samples is compiled. Nonlinear grayscale transformation is used to enhance the images in the dataset, increasing the contrast between defects and the background. Then, bilateral filtering is used to denoise the grayscale transformed images. Adaptive threshold image segmentation is used to segment the denoised images, with a smoothing operator scale (H,W) = (15,15) and r = 0.15, generating a second image. Feature extraction is performed on the second image, extracting geometric shape features, LBP features, Hu moment features, and HOG features. PCA is used to reduce the dimensionality of the HOG features, and all features are combined into a complete feature set. SVM-RFE is used to evaluate all features in the feature set, and the top 60% of the features by importance are selected to form the final feature set. Construct an ISMA-SVM model. Since there are three types of defect features, three ISMA-SVM models need to be built. Set the maximum population size N=20, the maximum number of iterations T=200, the value range of c is [0.1, 30], and the value range of γ is [0.01, 30]. Update the slime mold population with Sobol sequences, randomly generate c and γ, input the feature set into the three constructed models, classify the image, use the accuracy as the moderation value and perform normalization, update the position of the bacterial community, calculate the current c and γ, and iterate. After iteration, output the c and γ corresponding to the highest moderation value. For cracks and looseness, c=1.82, γ=0.44; for cracks and stomata, c=28.4, γ=20.3; for looseness and stomata, c=2.33, γ=0.61. The ISMA-SVM model is now constructed. Cross-validate the training and test sets and train twice. Output the final classification results to verify the model's accuracy.
[0217]
[0218]
[0219] The overall accuracy of the two validations was 0.948.
[0220] Comparison with other classifiers:
[0221]
[0222] Please see Figure 7The comparison chart of classifier recognition results shows that the ISMA-SVM classification accuracy is higher than that of the BP neural network, GA-SVM, and PSO-SVM models. Therefore, the method implemented in this embodiment improves the accuracy of surface defect recognition in castings. In this embodiment, compared with the BP neural network model, GA-SVM model, and PSO-SVM model, the ISMA-SVM model improves the accuracy of crack defect recognition by 0.38, 0.29, and 0.18 respectively; the accuracy of porosity defect recognition by 0.19, 0.11, and 0.1 respectively; and the accuracy of loosening defect recognition by 0.22, 0.17, and 0.12 respectively.
[0223] In this embodiment, a first image of the casting is first acquired; then, the first image set is preprocessed to generate a second image; then, the second image is input into a pre-trained casting surface feature SVM-RFE model to extract features and construct a feature set; then, the feature set is input into a pre-trained casting surface defect ISMA-SVM model for classification; and finally, the classification result is output. This disclosure obtains a second image more suitable for feature extraction by performing nonlinear grayscale transformation, bilateral filtering, and threshold segmentation on the captured first image, reducing the impact of noise and irrelevant information on subsequent steps and improving the accuracy and robustness of feature extraction. Inputting the second image into the casting surface feature SVM-RFE model allows for the recursive removal of unimportant features, selecting the optimal feature subset, and obtaining a feature set after feature selection. The RFE selection reduces the feature dimensionality, improving the accuracy and stability of subsequent classification. The slime mold algorithm can optimize model parameters through global search, improving classifier performance; while the SVM model has efficient classification capabilities, effectively distinguishing between defective and normal castings in the feature space. Constructing a combined model can achieve more accurate classification results and improve the accuracy of defect detection. By introducing random inertia weights and a position update formula, the slime mold algorithm is improved, resulting in better stability and reliability during the search process. This also helps avoid getting trapped in local optima, enhancing the algorithm's global search capability. The position update formula ensures the randomness and diversity of the slime mold cells during the search, increasing the algorithm's exploration ability. The improved slime mold algorithm is used to optimize the regularization parameter c and the radial basis function γ of the SVM, obtaining optimal values for c and γ. Using these optimal parameters, an SVM model is constructed, improving the accuracy of SVM in identifying and classifying surface defects in castings.
[0224] Please see Figure 2 This invention provides a machine learning-based casting surface defect detection device 200, the technical solution of which is as follows:
[0225] Acquisition unit 210 is used to acquire a first image of the casting;
[0226] Processing unit 220 is used to preprocess the first image set and generate a second image;
[0227] Extraction unit 230 is used to input the second image into a pre-trained casting surface feature SVM-RFE model to extract features and construct a feature set;
[0228] The classification unit 240 is used to input the feature set into a pre-trained ISMA-SVM model for classifying surface defects of castings.
[0229] Output unit 250 is used to input classification results.
[0230] In this embodiment, a first image of the casting is first acquired; then, the first image set is preprocessed to generate a second image; then, the second image is input into a pre-trained casting surface feature SVM-RFE model to extract features and construct a feature set; then, the feature set is input into a pre-trained casting surface defect ISMA-SVM model for classification; and finally, the classification result is output. This disclosure obtains a second image more suitable for feature extraction by performing nonlinear grayscale transformation, bilateral filtering, and threshold segmentation on the captured first image, reducing the impact of noise and irrelevant information on subsequent steps and improving the accuracy and robustness of feature extraction. Inputting the second image into the casting surface feature SVM-RFE model allows for the recursive removal of unimportant features, selecting the optimal feature subset, and obtaining a feature set after feature selection. The RFE selection reduces the feature dimensionality, improving the accuracy and stability of subsequent classification. The slime mold algorithm can optimize model parameters through global search, improving classifier performance; while the SVM model has efficient classification capabilities, effectively distinguishing between defective and normal castings in the feature space. Constructing a combined model can achieve more accurate classification results and improve the accuracy of defect detection. By introducing random inertia weights and a position update formula, the slime mold algorithm is improved, resulting in better stability and reliability during the search process. This also helps avoid getting trapped in local optima, enhancing the algorithm's global search capability. The position update formula ensures the randomness and diversity of the slime mold cells during the search, increasing the algorithm's exploration ability. The improved slime mold algorithm is used to optimize the regularization parameter c and the radial basis function γ of the SVM, obtaining optimal values for c and γ. Using these optimal parameters, an SVM model is constructed, improving the accuracy of SVM in identifying and classifying surface defects in castings.
[0231] In the description of this specification, the references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of this disclosure. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.
[0232] Any process or method description in the flowchart or otherwise herein can be understood as representing a module, segment, or portion of code comprising one or more executable instructions for implementing custom logic functions or processes, and the scope of preferred embodiments of this disclosure includes additional implementations in which functions may be performed not in the order shown or discussed, including substantially simultaneously or in reverse order depending on the functions involved, as will be understood by those skilled in the art to which embodiments of this disclosure pertain.
[0233] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A method for detecting surface defects in castings based on machine learning, characterized in that, include: S1, Obtain the first image of the casting; S2, preprocess the first image set and generate the second image; S21, perform grayscale transformation on the first image; The grayscale transformation is a nonlinear grayscale transformation; Where r is the gray value of the input image, s is the gray value of the output image, and a, b, and c are constants, with a ranging from [0, 255], b ranging from [1, 10], and c ranging from [1, 10]. S22, The image after grayscale conversion is filtered; the filtering process is a bilateral filtering process. S23, perform image segmentation on the filtered image to generate the second image; S3, input the second image into the pre-trained SVM-RFE model for casting surface features to extract features and construct a feature set; S4, input the feature set into the pre-trained ISMA-SVM model for classifying surface defects of castings; S5 outputs the classification results.
2. The method according to claim 1, characterized in that, S23 includes: S231, Smooth the image according to the H×L smoothing operator and output the result f(J(h,l)); S232, calculate the threshold T based on the result f(J(h,l)); T=(1-r)f(J(h,l)) Where r is a random number, and its value ranges from [0.1, 0.2]. S233, The image is segmented according to the threshold T to generate the second image; J(x,y) represents the gray value at the point (x,y), and T represents the threshold to be found.
3. The method according to claim 1, characterized in that, The S4 further includes: S41. Initialize ISMA parameters; the parameters include iteration number T, cell number N, sensing range, and maximum step size; S42. Initialize SVM parameters, randomly generate c and γ; S43. Use an SVM classifier to classify the training set and calculate the classifier's performance metric as the fitness function; S44. Update the position of each bacterial cell; S45. Calculate the new c and γ parameters based on the stated position; S46. Stop condition judgment: Determine whether the number of iterations has reached the set value. If it has, end the algorithm; otherwise, return to step S43. S47. Output the optimal solution.
4. The method according to claim 3, characterized in that, S51 includes: The total number of slime molds was initialized using Sobol sequences.
5. The method according to claim 3, characterized in that, The S54 further includes: The inertia weight value adopts a random inertia weight mechanism, and the formula is as follows: oh = oh max -(oh max -oh min )r1+r2δ Where, ω max It is the maximum value of the random inertia weight, ω min ω is the minimum value of the random inertia weight, r1 is a random number in [0,1], r2 is a random number in a normal distribution, and δ (standard deviation) is used to measure the degree of deviation between the random inertia weight ω and its mathematical expectation.
6. The method according to claim 3, characterized in that, S54 includes: Position update formula: Among them, U B and L B These represent the upper and lower boundaries of the search, respectively; rand and r are random values in [0,1]; z is a set value; v b The range is [-a, a], v c To decrease linearly from 1 to 0, t represents the current iteration number, X b Let X(t) represent the current location of the slime mold, where the odor concentration is currently the highest. A and X B This represents two randomly selected slime mold locations, and W represents the slime mold weight. p=tanh|S(i)-D E | Where S(i) represents the fitness of X(t), and D E This represents the optimal fitness value obtained from all iterations; Where T is the maximum number of iterations.
7. The method according to claim 3, characterized in that, The S55 includes: The SVM classifier is trained, and cross-validation is performed on the training and test sets.
8. A machine learning-based casting surface defect detection device, characterized in that, Used for: The acquisition unit is used to acquire the first image of the casting; A processing unit is configured to preprocess the first image set and generate a second image; The extraction unit is used to input the second image into a pre-trained SVM-RFE model for casting surface features to extract features and construct a feature set; A classification unit is used to input the feature set into a pre-trained ISMA-SVM model for classifying surface defects in castings. The output unit is used to output the classification results.
9. The apparatus according to claim 8, characterized in that, The classification unit is also used for: S41. Initialize ISMA parameters; the parameters include iteration number T, cell number N, sensing range, and maximum step size; S42. Initialize SVM parameters, randomly generate c and γ; S43. Use an SVM classifier to classify the training set and calculate the classifier's performance metric as the fitness function; S44. Update the position of each bacterial cell; S45. Calculate the new c and γ parameters based on the stated position; S46. Stop condition judgment: Determine whether the number of iterations has reached the set value. If it has, end the algorithm; otherwise, return to step S43. S47. Output the optimal solution.
10. The apparatus according to claim 8, characterized in that, The classification unit is used for: The inertia weight value adopts a random inertia weight value, and the formula is: oh = oh max -(oh max -oh min )r1+r2δ Where, ω max It is the maximum value of the random inertia weight, ω min ω is the minimum value of the random inertia weight, r1 is a random number in [0,1], r2 is a random number in a normal distribution, and δ (standard deviation) is used to measure the degree of deviation between the random inertia weight ω and its mathematical expectation.