Method and system for detecting the appearance of a pastry
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HENAN SANDWICH FOOD CO LTD
- Filing Date
- 2026-05-15
- Publication Date
- 2026-07-21
AI Technical Summary
Traditional manual inspection of pastry appearance is inefficient and highly subjective. Existing machine vision algorithms are easily affected by pastry background texture, cannot decouple high and low frequency features, have misaligned feature spaces, and have rigid fusion mechanisms, resulting in high rates of missed detections and false detections.
A U-shaped detection network is used for multi-scale feature extraction. High and low frequency components are decoupled by texture activity index, feature position is corrected by offset field, features are screened by gating unit, and detection threshold is dynamically adjusted to improve detection accuracy.
It improves the accuracy and stability of pastry appearance detection and reduces the rate of misjudgment and missed detection caused by texture differences.
Smart Images

Figure CN122434907A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of testing, and in particular relates to a method and system for testing the appearance of pastries. Background Technology
[0002] With the continuous improvement of the food industry, the appearance inspection of pastries has become a crucial step in ensuring their quality and brand image. Traditional manual visual inspection is costly, inefficient, and easily affected by subjective experience and fatigue, making it difficult to meet the demands of high-speed production lines. Normal surface textures and appearance defects in pastries often share high similarities in visual features and grayscale distribution. The inherently high-activity background textures can easily mask subtle defects or misjudge normal wrinkles as damage. This results in traditional machine vision algorithms exhibiting weak generalization ability, low detection accuracy, and susceptibility to interference from background textures when processing pastry images with high intra-class differences.
[0003] U-shaped networks, through skip connections, fuse low-level high-resolution spatial details extracted by the encoder with high-level semantic features generated by the decoder, offering advantages in pixel-level defect segmentation. Conventional skip connections directly transmit shallow features, but shallow features of pastries often contain high-frequency background texture noise. Existing methods lack decoupling and differential processing of high and low frequency components, making it easy for high-frequency noise to interfere with the decoder's feature recovery. At the same time, multiple downsampling can cause slight spatial offsets between the corresponding layers of features in the encoder and decoder, and direct splicing can easily lead to feature misalignment and edge blurring. In addition, existing networks are insufficiently perceptive of the texture complexity of individual pastries, the feature fusion gating mechanism is relatively rigid, and the defect response threshold is mostly a globally fixed value, failing to fully consider the differences in texture activity among different pastries, resulting in easy missed detection of simple texture samples and easy false detection of complex texture samples. Summary of the Invention
[0004] To address the problems of low efficiency and high subjectivity in traditional manual inspection, as well as the susceptibility of existing machine vision algorithms to interference from pastry background textures, inability to decouple high and low frequency features, misalignment of feature space, rigid fusion mechanisms, and fixed defect judgment thresholds, which lead to high rates of missed and false detections, this invention provides a method and system for detecting the appearance of pastries.
[0005] According to one aspect of the present invention, a method for detecting the appearance of pastries is provided, comprising the following steps: Images of pastries are acquired and fed into a U-shaped detection network for multi-scale feature extraction. The feature maps output by each level of the network encoder are normalized by channels and used as the initial features for Skip connections. The mean channel variance is calculated based on the corresponding level feature maps before normalization and recorded as a texture activity index. In Skip Connect, the initial Skip Connect features after channel normalization are decomposed into low-frequency and high-frequency components using discrete wavelet transform. The low-frequency components are processed by depthwise separable convolution, and the high-frequency components are weighted by second-order channel statistics. The high-frequency components are then scaled according to the texture activity index after being weighted by second-order channel statistics. The processed components are then reconstructed into a fused feature map using inverse wavelet transform. An offset field is generated by convolution between the corresponding layer features of the decoder and the fused feature map. The offset field is used to correct the position of the fused feature map to obtain the corrected features. The maximum allowable single-axis offset of the offset field is determined by the texture activity index of the current layer. The corrected features are concatenated with the decoder features and filtered by a gating unit. The gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function. When the maximum response value in the probability map output by the decoder is greater than the set threshold, it is determined that there is a defect at the corresponding position of the pastry and the detection result is output.
[0006] Optionally, the output feature map of each level of the network encoder is processed by channel normalization and used as the initial feature map for Skip connection. The mean channel variance is calculated based on the corresponding level feature map before normalization, and the mean channel variance is recorded as a texture activity index, including: Extract the two-dimensional slice features of the output feature map of the current encoder before normalization processing in the channel dimension; The average value of all pixel feature values in each slice feature is calculated as the mean of the channel; Calculate the square of the difference between the feature value of each pixel in each slice feature and the mean value, sum over all pixels and divide by the total number of pixels to obtain the variance of the channel; The mean variance of all channels in the current level feature map is obtained by summing the variances and dividing by the total number of channels. This mean variance is then recorded as the texture activity index of the current level.
[0007] Optionally, the high-frequency components are weighted by second-order channel statistics, including: Extract the feature vectors of high-frequency components along the channel dimension at each spatial location; Calculate the average of the squared values of each element in the feature vector to obtain the second-order energy statistic of the channel that characterizes the high-frequency response intensity at the spatial location; or calculate the weighted sum of the channel variance and mean square energy of the feature vector as the second-order statistic of the spatial location. The calculated second-order statistics for each spatial location are normalized and then multiplied element-wise with the feature vector of the high-frequency component at the corresponding spatial location to obtain the high-frequency features after weighting the second-order statistics. This process relatively preserves and highlights local regions with strong high-frequency responses while suppressing invalid responses in smooth regions.
[0008] Optionally, the high-frequency components are weighted by second-order channel statistics and then scaled according to the texture activity index, including: Set the lower limit reference value for the amplitude scaling factor; Obtain the texture activity index of the current level, normalize the index to the range of 0 to 1 as the adjustment weight, wherein the texture activity index is used to characterize the overall background texture complexity of the current level, and is not directly used as the defect detail intensity. The scaling factor is calculated based on the lower limit reference value and the adjustment weight. The scaling factor approaches the lower limit reference value as the adjustment weight increases and approaches 1 as the adjustment weight decreases, so as to suppress high-frequency noise generated by normal pastry texture when the overall background texture is complex, and retain the high-frequency response required for defect edges through the lower limit reference value. The amplitude scaling of the high-frequency features is completed by multiplying each element of the weighted high-frequency features by the calculated scaling factor, thus preserving the high-frequency details corresponding to weak defects when the overall background texture is low.
[0009] Optionally, the step of generating an offset field through convolution based on the corresponding layer features of the decoder and the fused feature map includes: The upsampled features of the corresponding layer of the decoder are concatenated with the fused feature map along the channel dimension to obtain the concatenated features; Two consecutive two-dimensional convolution operations are performed on the spliced features to extract the spatial position difference information between the decoder features and the fused feature map; The last inactive two-dimensional convolutional layer outputs a feature map with two channels. The two channels of the feature map correspond to the pixel offsets in the horizontal and vertical directions, respectively, forming the offset field.
[0010] Optionally, the maximum permissible single-axis offset of the offset field is determined by the texture activity index of the current level, including: The maximum absolute offset constant allowed by the preset offset field; Obtain the texture activity index of the current level record and map the index to a constraint coefficient between 0 and 1 through a linear transformation; The constraint coefficient is converted into an offset suppression coefficient, so that the higher the texture activity index of the current level, the smaller the maximum allowable single-axis offset; the lower the texture activity index of the current level, the larger the maximum allowable single-axis offset. The offset in the generated offset field is numerically truncated using the maximum permissible single-axis offset, so that the horizontal and vertical offsets of all pixels are limited to the range of the maximum permissible single-axis offset.
[0011] Optionally, the step of concatenating the corrected features with the decoder features and filtering them through a gating unit, wherein the gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function, includes: A one-dimensional channel feature vector is obtained by concatenating the corrected features with the decoder features and performing global average pooling on the feature map. Extract the texture activity index of the current level, perform square root processing on the index, and expand it into a bias vector with the same dimension as the one-dimensional channel feature vector after mapping through learnable parameters; The updated fusion vector is obtained by adding the one-dimensional channel feature vector to the bias vector element by element. The fusion vector is nonlinearly mapped to generate channel gating values, which are used to perform channel-dimensional attention-weighted filtering of the spliced features.
[0012] Optionally, the process of determining the set threshold specifically includes: Extract texture activity metrics recorded in all layers of the network encoder; After normalizing the extracted texture activity indices of each level according to the preset statistical benchmark of the corresponding level, the indices are summed and divided by the total number of levels to calculate the global mean of the overall texture activity index of the pastry image. A preset basic judgment threshold is set, and an adjustment factor is calculated based on the global mean. When the global mean is high, the adjustment factor is increased, and when the global mean is low, the adjustment factor is decreased. Multiply the basic judgment threshold by the adjustment factor to obtain the set threshold used for defect judgment.
[0013] According to another aspect of the present invention, a system for detecting the appearance of pastries is provided, comprising the following modules: The acquisition module is used to acquire pastry images and send them to the U-shaped detection network for multi-scale feature extraction. The feature map output by each level of the network encoder is normalized by the channel and used as the initial feature for Skip connection. The mean channel variance is calculated based on the corresponding level feature map before normalization and the mean channel variance is recorded as the texture activity index. The determination module is used to decompose the initial features of the Skip connection after channel normalization into low-frequency and high-frequency components through discrete wavelet transform in the Skip connection. The low-frequency components are processed by depthwise separable convolution, and the high-frequency components are weighted by second-order channel statistics. The high-frequency components are scaled according to the texture activity index after being weighted by second-order channel statistics. The processed components are reconstructed into a fused feature map through inverse wavelet transform. An offset field is generated by convolution between the corresponding layer features of the decoder and the fused feature map. The position of the fused feature map is corrected using the offset field to obtain the corrected features. The maximum allowable single-axis offset of the offset field is determined by the texture activity index of the current layer. The output module is used to concatenate the corrected features with the decoder features and filter them through a gating unit. The gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function. When the maximum response value in the probability map output by the decoder is greater than the set threshold, it is determined that there is a defect at the corresponding position of the pastry and the detection result is output.
[0014] Furthermore, the output feature map of each level of the network encoder, after channel normalization, is used as the initial feature for Skip connections. The mean channel variance is calculated based on the corresponding level feature map before normalization, and this mean channel variance is recorded as a texture activity index, including: Extract the two-dimensional slice features of the output feature map of the current encoder before normalization processing in the channel dimension; The average value of all pixel feature values in each slice feature is calculated as the mean of the channel; Calculate the square of the difference between the feature value of each pixel in each slice feature and the mean value, sum over all pixels and divide by the total number of pixels to obtain the variance of the channel; The mean variance of all channels in the current level feature map is obtained by summing the variances and dividing by the total number of channels. This mean variance is then recorded as the texture activity index of the current level.
[0015] Furthermore, the high-frequency components are weighted by second-order channel statistics, including: Extract the feature vectors of high-frequency components along the channel dimension at each spatial location; Calculate the average of the squared values of each element in the feature vector to obtain the second-order energy statistic of the channel that characterizes the high-frequency response intensity at the spatial location; or calculate the weighted sum of the channel variance and mean square energy of the feature vector as the second-order statistic of the spatial location. The calculated second-order statistics for each spatial location are normalized and then multiplied element-wise with the feature vector of the high-frequency component at the corresponding spatial location to obtain the high-frequency features after weighting the second-order statistics. This process relatively preserves and highlights local regions with strong high-frequency responses while suppressing invalid responses in smooth regions.
[0016] Furthermore, the high-frequency components are weighted by second-order channel statistics and then scaled according to the texture activity index, including: Set the lower limit reference value for the amplitude scaling factor; Obtain the texture activity index of the current level, normalize the index to the range of 0 to 1 as the adjustment weight, wherein the texture activity index is used to characterize the overall background texture complexity of the current level, and is not directly used as the defect detail intensity. The scaling factor is calculated based on the lower limit reference value and the adjustment weight. The scaling factor approaches the lower limit reference value as the adjustment weight increases and approaches 1 as the adjustment weight decreases, so as to suppress high-frequency noise generated by normal pastry texture when the overall background texture is complex, and retain the high-frequency response required for defect edges through the lower limit reference value. The amplitude scaling of the high-frequency features is completed by multiplying each element of the weighted high-frequency features by the calculated scaling factor, thus preserving the high-frequency details corresponding to weak defects when the overall background texture is low.
[0017] Furthermore, the step of generating an offset field through convolution based on the corresponding layer features of the decoder and the fused feature map includes: The upsampled features of the corresponding layer of the decoder are concatenated with the fused feature map along the channel dimension to obtain the concatenated features; Two consecutive two-dimensional convolution operations are performed on the spliced features to extract the spatial position difference information between the decoder features and the fused feature map; The last inactive two-dimensional convolutional layer outputs a feature map with two channels. The two channels of the feature map correspond to the pixel offsets in the horizontal and vertical directions, respectively, forming the offset field.
[0018] Furthermore, the maximum permissible single-axis offset of the offset field is determined by the texture activity index of the current level, including: The maximum absolute offset constant allowed by the preset offset field; Obtain the texture activity index of the current level record and map the index to a constraint coefficient between 0 and 1 through a linear transformation; The constraint coefficient is converted into an offset suppression coefficient, so that the higher the texture activity index of the current level, the smaller the maximum allowable single-axis offset; the lower the texture activity index of the current level, the larger the maximum allowable single-axis offset. The offset in the generated offset field is numerically truncated using the maximum permissible single-axis offset, so that the horizontal and vertical offsets of all pixels are limited to the range of the maximum permissible single-axis offset.
[0019] Furthermore, the step of concatenating the corrected features with the decoder features and filtering them through a gating unit, wherein the gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function, includes: A one-dimensional channel feature vector is obtained by concatenating the corrected features with the decoder features and performing global average pooling on the feature map. Extract the texture activity index of the current level, perform square root processing on the index, and expand it into a bias vector with the same dimension as the one-dimensional channel feature vector after mapping through learnable parameters; The updated fusion vector is obtained by adding the one-dimensional channel feature vector to the bias vector element by element. The fusion vector is nonlinearly mapped to generate channel gating values, which are used to perform channel-dimensional attention-weighted filtering of the spliced features.
[0020] Furthermore, the process of determining the set threshold is specifically as follows: Extract texture activity metrics recorded in all layers of the network encoder; After normalizing the extracted texture activity indices of each level according to the preset statistical benchmark of the corresponding level, the indices are summed and divided by the total number of levels to calculate the global mean of the overall texture activity index of the pastry image. A preset basic judgment threshold is set, and an adjustment factor is calculated based on the global mean. When the global mean is high, the adjustment factor is increased, and when the global mean is low, the adjustment factor is decreased. Multiply the basic judgment threshold by the adjustment factor to obtain the set threshold used for defect judgment.
[0021] This invention represents the textural complexity of pastry surfaces by extracting a texture activity index. Discrete wavelet transform is introduced into the Skip connection to achieve feature decoupling. Low-frequency components are processed by depthwise separable convolution, and high-frequency components are directionally scaled using the texture activity index. This suppresses background noise while highlighting subtle defect features. Offset fields are used to correct the position of the fused feature map, and the maximum single-axis offset is limited by the texture activity index, which helps improve the spatial alignment of multi-scale features and reduces the risk of defect morphology distortion due to over-correction. Feature filtering using a gating unit combined with the texture activity index removes redundant interference information. The detection threshold is reasonably adjusted based on the overall texture index mean, reducing false positives and false negatives caused by differences in pastry textures, and improving the overall accuracy and stability of quality inspection. Attached Figure Description
[0022] Figure 1 This is a flowchart of a method for detecting the appearance of pastries; Figure 2This is a schematic diagram illustrating the relationship between the scaling factor and the weights; Figure 3 Schematic diagram of threshold adjustment. Detailed Implementation
[0023] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0024] Example 1 In Embodiment 1 of the present invention, as Figure 1 As shown, a method for detecting the appearance of pastries includes the following steps: S1. Collect pastry images and send them to a U-shaped detection network for multi-scale feature extraction. The feature map output by each level of the network encoder is normalized by the channel and used as the initial feature for Skip connection. The mean channel variance is calculated based on the corresponding level feature map before normalization and the mean channel variance is recorded as the texture activity index.
[0025] An industrial area array camera is used to acquire RGB color images of the pastry surface on the production line. Image frames are acquired through the camera's acquisition interface or by reading locally cached image data. The spatial resolution of the image is adjusted to a preset model input size, such as 512×512 pixels or 256×256 pixels. After normalizing the pixel values by dividing by 255, the images are converted to floating-point tensor format and fed into a U-shaped detection network. This network uses a pre-trained ResNet50 as the encoder backbone to extract multi-scale features. The encoder consists of four stages, outputting feature maps at four levels downsampled by two to sixteen times. For each level's output feature map tensor, instance normalization is performed in the spatial dimension. The mean and variance of all pixel values in each channel of the current level's feature map are calculated. By subtracting the mean and dividing by the sum of the standard deviation and small stable terms, the data distribution of each channel is made to achieve zero mean and unit variance. The resulting tensor is used as the initial feature for the Skip connection of the current level. Calculate the variance of each channel pixel before normalization, then calculate the arithmetic mean of the variances of all channels to generate a scalar value representing the texture complexity, and record the value as a texture activity index tensor for that level and temporarily store it in video memory.
[0026] In some embodiments, the output feature map of each level of the network encoder is processed by channel normalization and used as the initial feature map for Skip connections. The mean channel variance is calculated based on the corresponding level feature map before normalization, and the mean channel variance is recorded as a texture activity index, including: Extract the two-dimensional slice features of the output feature map of the current encoder before normalization processing in the channel dimension; The average value of all pixel feature values in each slice feature is calculated as the mean of the channel; Calculate the square of the difference between the feature value of each pixel in each slice feature and the mean value, sum over all pixels and divide by the total number of pixels to obtain the variance of the channel; The mean variance of all channels in the current level feature map is obtained by summing the variances and dividing by the total number of channels. This mean variance is then recorded as the texture activity index of the current level.
[0027] The encoder, as a basic network model for feature extraction, takes the original pastry image to be detected as input and outputs a multi-level high-dimensional feature tensor containing rich local details and semantic information. Its structure includes multiple consecutively stacked two-dimensional convolutional layers and pooling layers for downsampling. The high-dimensional feature tensor before normalization of the encoder output is extracted as the processing benchmark. Taking the output of the second layer of the encoder as an example, the spatial resolution is set to 64 pixels in both height and width, with a total of 256 channels. For this feature tensor, 256 independent two-dimensional slice feature matrices are extracted sequentially along the channel dimension. For any single-channel slice matrix, the algebraic average of the feature values of 4096 pixels in the slice matrix is calculated using global mean pooling, thus obtaining the scalar mean of each of the 256 channels. For example, the mean of the extracted 15th channel slice is calculated to be 0.45. To represent the local texture complexity of each channel, the feature values of all pixels in each two-dimensional slice matrix are successively subtracted from the corresponding channel mean and squared. After summing the 4096 squared differences, divide by the total number of spatial pixels (4096) to obtain the spatial variance of each channel slice. Iterate through and accumulate the variance values of all 256 channels in the current level, and divide the sum by the total number of channels (256) to calculate the mean channel variance of the feature map at that level. Allocate this mean variance, for example, if the calculated result is 0.082, to the network storage area and record it as the texture activity index of the current encoder level.
[0028] S2, In Skip Connection, the initial Skip Connection features after channel normalization are decomposed into low-frequency components and high-frequency components by discrete wavelet transform. The low-frequency components are processed by depthwise separable convolution, and the high-frequency components are weighted by channel second-order statistics. The high-frequency components are scaled according to the texture activity index after being weighted by channel second-order statistics, and the processed components are reconstructed into a fused feature map by inverse wavelet transform.
[0029] Within the skip connection modules at each layer of the network, a Haar wavelet basis is used to perform a two-dimensional discrete wavelet transform on the initial features of the skip connection. The normalized output feature map tensor of the encoder at the corresponding layer is then subjected to a two-dimensional discrete wavelet transform in the spatial dimension, decomposing it into a low-frequency approximate component representing the main contour information, and three high-frequency detail components corresponding to edge details in the horizontal, vertical, and diagonal directions, respectively. For the low-frequency component, a depthwise separable convolution is constructed, sequentially passing it through a torch.nn.Conv2d spatial depthwise convolution with the number of groups equal to the number of channels, and a point convolution with a kernel size of 1×1 for smoothing, noise reduction, and feature mapping. The number of output channels and the spatial size of the depthwise separable convolution remain consistent with the input low-frequency component to meet the dimensionality matching requirements of subsequent inverse wavelet reconstruction.
[0030] For high-frequency components, feature vectors are extracted along the channel dimension at each spatial location. The average of the squared values of each channel element in the feature vector is calculated, or the weighted sum of the channel variance and mean square energy is calculated as the second-order statistic for that spatial location. The second-order statistics for each spatial location are normalized to form a spatial weighted coefficient map. This spatial weighted coefficient map is then broadcast to the channel dimension and multiplied element-wise with the high-frequency components to complete the high-frequency weighting processing based on the channel-dimensional second-order statistics. The scalar values of the corresponding level texture activity index temporarily stored in the previous steps are extracted. The texture activity index is normalized into adjustment weights, and an amplitude scaling factor is calculated based on a preset lower limit benchmark value. The amplitude scaling factor decreases as the adjustment weight increases and increases as the adjustment weight decreases. The amplitude scaling factor is multiplied by the channel-weighted high-frequency component tensor to suppress high-frequency noise generated by normal textures when the overall background texture is complex, and to retain high-frequency details corresponding to weak defects when the overall background texture is low. The processed low-frequency components and the amplitude-scaled high-frequency components are subjected to a two-dimensional inverse discrete wavelet transform operation to reconstruct the fused feature map tensor.
[0031] In some embodiments, the high-frequency components are weighted by second-order channel statistics, including: Extract the feature vectors of high-frequency components along the channel dimension at each spatial location; Calculate the average of the squared values of each element in the feature vector to obtain the second-order energy statistic of the channel that characterizes the high-frequency response intensity at the spatial location; or calculate the weighted sum of the channel variance and mean square energy of the feature vector as the second-order statistic of the spatial location. The calculated second-order statistics for each spatial location are normalized and then multiplied element-wise with the feature vector of the high-frequency component at the corresponding spatial location to obtain the high-frequency features after weighting the second-order statistics. This process relatively preserves and highlights local regions with strong high-frequency responses while suppressing invalid responses in smooth regions.
[0032] Taking the high-frequency feature map components extracted by discrete wavelet transform as an example, assume the tensor dimension is 32×32×128. Traverse the coordinates of each planar spatial location in the feature tensor, and extract a one-dimensional channel feature vector of length 128 along the depth direction of the feature tensor. To represent the high-frequency response intensity at each spatial location, calculate the average of the squared values of the above 128 elements to obtain the channel mean square energy at that spatial location; or further calculate the channel variance, and then sum the channel mean square energy and channel variance according to non-negative weights, as the second-order statistic for that spatial location, where the sum of each non-negative weight is 1. Reorganize the second-order statistics calculated for all coordinate points into a second-order statistic matrix of size 32×32×1, which is used to represent the high-frequency response strength in different regions. Apply an extremum normalization method to linearly scale the second-order statistic matrix. For example, if the values of the original second-order statistics are distributed between 0.05 and 1.2, all values can be constrained to the standard floating-point range of 0.0 to 1.0 through formula mapping, forming a two-dimensional spatial weighted coefficient matrix. Using a tensor broadcasting mechanism, the obtained spatial weighted coefficient matrix is copied and extended to 128 dimensions along the channel dimension, and element-wise Hadamard product operations are performed with the original 32×32×128 high-frequency component tensor. This process preserves and highlights detailed regions with strong high-frequency responses relative to smooth regions, while suppressing the responses of smooth, featureless regions.
[0033] In some embodiments, the high-frequency components are weighted by second-order channel statistics and then scaled according to the texture activity index, including: Set the lower limit reference value for the amplitude scaling factor; Obtain the texture activity index of the current level, normalize the index to the range of 0 to 1 as the adjustment weight, wherein the texture activity index is used to characterize the overall background texture complexity of the current level, and is not directly used as the defect detail intensity. The scaling factor is calculated based on the lower limit reference value and the adjustment weight. The scaling factor approaches the lower limit reference value as the adjustment weight increases and approaches 1 as the adjustment weight decreases, so as to suppress high-frequency noise generated by normal pastry texture when the overall background texture is complex, and retain the high-frequency response required for defect edges through the lower limit reference value. The amplitude scaling of the high-frequency features is completed by multiplying each element of the weighted high-frequency features by the calculated scaling factor, thus preserving the high-frequency details corresponding to weak defects when the overall background texture is low.
[0034] Before performing overall amplitude scaling on the weighted high-frequency features, a lower limit reference value is preset in the configuration file to limit the attenuation limit. The preferred value is set between 0.30 and 0.50, and the example reference value here is 0.40. The texture activity index value corresponding to the current encoder level is obtained from the feature queue. The index value is divided by the expected maximum activity constant, for example, 0.15, and the value obtained after division is linearly normalized to a closed interval of 0 to 1 using a numerical truncation function, thereby generating the adjustment weight for control. For example, when the detection target is oat cake with extremely high surface roughness, the calculated adjustment weight may reach 0.85. After obtaining the adjustment weight, the amplitude scaling factor of the current level is calculated according to the negative correlation linear interpolation formula, which is: scaling factor = 1 - adjustment weight × (1 - lower limit reference value), which is equivalent to scaling factor = lower limit reference value + (1 - lower limit reference value) × (1 - adjustment weight). Substituting the aforementioned data, the scaling factor is calculated to be 0.49, indicating that when the inherent texture of the pastry surface is too complex, a factor close to the lower limit benchmark is output to suppress high-frequency noise interference; while for extremely smooth mousse pastries with a weight of, for example, 0.10, the scaling factor is 0.94 to preserve as many subtle, real defect boundaries as possible. Figure 2 As shown, the scaling factor approaches the lower limit benchmark value as the adjustment weight increases, and approaches 1 as the weight decreases, thereby achieving differentiated enhancement of high-frequency details and noise suppression under different texture complexities. The calculated scalar scaling factor is multiplied by all floating-point elements in the weighted high-frequency feature tensor to achieve global adaptive amplitude adjustment of the high-frequency feature map.
[0035] S3, Based on the corresponding layer features of the decoder and the fused feature map, an offset field is generated through convolution. The offset field is used to correct the position of the fused feature map to obtain the corrected features. The maximum allowable single-axis offset of the offset field is determined by the texture activity index of the current layer.
[0036] The current-level fused feature map is concatenated with the feature map of corresponding spatial resolution obtained by the decoder after upsampling in the channel dimension. The concatenation result is input into an offset prediction convolutional layer with 2 output channels to calculate and generate an offset field tensor representing the deformation offset of each pixel in the fused feature map in the horizontal and vertical directions. The two channels of the offset field tensor correspond to the pixel offset in the horizontal and vertical directions, respectively. To prevent excessive distortion beyond the range of pastry deformation during the feature space alignment process, the texture activity index of the current level is normalized to a constraint coefficient between 0 and 1. The offset suppression coefficient is obtained by subtracting the constraint coefficient from 1. Alternatively, the maximum deformation constraint coefficient is calculated by negatively interpolating based on a preset minimum offset constant and a preset absolute maximum offset constant. The maximum allowable single-axis offset is smaller when the texture activity index is higher. The generated offset field tensor value is truncated and restricted to the range of negative to positive constraint coefficients to determine and constrain the maximum allowable single-axis offset of the offset field. Based on the offset field constrained by numerical truncation, the fused feature map of the current level is subjected to interpolation resampling or spatial sampling correction. The offset field tensor, which has been numerically truncated, is applied to the fused feature map tensor of the current level. Spatial resampling calculation is performed on the sampling grid on the feature map to output a corrected feature tensor that reduces the spatial semantic misalignment error between the encoder and decoder.
[0037] In some embodiments, the step of generating an offset field through convolution based on the decoder's corresponding layer features and the fused feature map includes: The upsampled features of the corresponding layer of the decoder are concatenated with the fused feature map along the channel dimension to obtain the concatenated features; Two consecutive two-dimensional convolution operations are performed on the spliced features to extract the spatial position difference information between the decoder features and the fused feature map; The last inactive two-dimensional convolutional layer outputs a feature map with two channels. The two channels of the feature map correspond to the pixel offsets in the horizontal and vertical directions, respectively, forming the offset field.
[0038] Assuming the spatial resolution of the upsampled decoder feature map corresponding to the current processing layer is 128×128 pixels and the number of channels is 64, the matching skip connection reconstructed fused feature map has the same resolution size and 64 channels. The network engine uses a concatenation operator in memory to fuse the two along the channel dimension, outputting a concatenated feature map tensor containing 128 mixed channels. This concatenated feature map containing 128 channels is input into an offset predictor network consisting of two consecutive 2D convolutional kernels. This offset predictor network acts as a neural network model for estimating pixel position variations. Its input is a concatenated feature tensor containing multi-channel mixed information, and its output is a continuous offset mapping field carrying horizontal and vertical deformation information. The structure includes a first dimensionality-reduced convolutional layer with a non-linear activation function and a second inactive linear convolutional layer. The first layer is a 3×3 2D convolutional layer followed by a non-linear activation layer with a negative slope of 0.1, compressing the number of channels to 32 dimensions while maintaining the 128×128 resolution, used to extract the hidden geometric deformation error between the two feature paths. The second layer connects to a 3×3 linear 2D convolutional network layer without any activation functions or normalization layers, setting the output channels to 2D and generating a continuous offset mapping field of size 128×128×2. The matrix with channel index 0 in this tensor feature map carries the sub-pixel offset of each pixel grid along the horizontal direction, and the matrix with channel index 1 carries the sub-pixel offset along the vertical direction. For example, a pixel located at coordinates 25,25 might output a horizontal offset of +1.34 and a vertical offset of -0.78.
[0039] In some embodiments, the maximum permissible single-axis offset of the offset field is determined by the texture activity index of the current level, including: The maximum absolute offset constant allowed by the preset offset field; Obtain the texture activity index of the current level record and map the index to a constraint coefficient between 0 and 1 through a linear transformation; The constraint coefficient is converted into an offset suppression coefficient, so that the higher the texture activity index of the current level, the smaller the maximum allowable single-axis offset; the lower the texture activity index of the current level, the larger the maximum allowable single-axis offset. The offset in the generated offset field is numerically truncated using the maximum permissible single-axis offset, so that the horizontal and vertical offsets of all pixels are limited to the range of the maximum permissible single-axis offset.
[0040] During the initialization phase of the offset field position correction module, a global absolute maximum offset constant is pre-defined for the network. To prevent severe divergence of sampling point coordinates during backpropagation, the offset constant parameter is preferably set to 5 pixels. During the model inference phase, the network extracts and calls the texture activity index pre-stored in the current cascaded layer in real time, and converts the activity index into constraint coefficients using a division formula with boundary constraints. The empirical mapping coefficient and the texture activity index use the same statistical caliber. For example, if the empirical mapping coefficient is set to 0.1, when the read texture activity index is 0.06, the index is divided by 0.1 to obtain 0.6, and this is limited to between 0 and 1, resulting in a constraint coefficient of 0.6. Subtracting the constraint coefficient from 1 yields an offset suppression coefficient of 0.4, which is then multiplied by the absolute maximum offset constant of 5 to obtain a floating-point offset upper limit of 2.0. This 2.0 pixel subpixel offset truncation upper limit can be directly used, or rounded down to 2 pixels as the maximum allowed single-axis offset for the current layer. A boundary threshold is constructed using the maximum permissible single-axis offset. A numerical truncation operator is then applied to perform element-wise verification of the two-dimensional offset field tensor generated in the previous step. If the predicted original horizontal offset at a certain coordinate position reaches +4.2, it will be reset and truncated to +2.0 because it exceeds the boundary. This ensures that pixel mesh deformation caused by subsequent sampling operations will not exceed the local neighborhood tolerance limit of 2.0 pixels centered on the current pixel, thereby maintaining the geometric stability of the continuous features on the pastry surface. Under the constraint of the absolute maximum offset constant, the local alignment tolerance is reduced when the texture is complex to reduce the risk of excessive deformation caused by normal texture wrinkles; when the texture is simple, the offset range is appropriately widened to compensate for the spatial misalignment between the encoder and decoder features to reduce unnecessary deformation risks.
[0041] S4. The corrected features are concatenated with the decoder features and filtered by a gating unit. The gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function.
[0042] The spatially corrected fusion features are concatenated with the corresponding level's decoder features along the channel dimension to generate a joint concatenated feature. This joint concatenated feature tensor is fed into a global average pooling layer to compress the spatial size of the feature tensor to 1×1, thereby extracting the global spatial context feature vector for each channel. This feature vector is then input into a fully connected layer without bias parameters for dimensionality reduction and dimensionality enhancement through interactive calculation of channel information. After this interactive calculation output, the scalar value of the texture activity index for the current level is extracted and, after mapping or broadcasting, is superimposed as an additional bias term onto the output feature vector of the fully connected layer. The calculation result with index bias adjustment is nonlinearly mapped to the real number interval of 0 to 1 to generate a channel gating value vector representing the importance of each feature channel for pastry defect detection. Using element-wise multiplication, the calculated channel gating value vector is multiplied layer by layer along the channel dimension by the original joint concatenated feature tensor. This gating mechanism helps suppress redundant pastry background texture features and enhances the channel responses related to abnormal defects. Output the filtered and fused feature tensor of the current level and pass it to the next level decoder module.
[0043] In some embodiments, the step of concatenating the corrected features with the decoder features and filtering them through a gating unit, wherein the gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function, includes: A one-dimensional channel feature vector is obtained by concatenating the corrected features with the decoder features and performing global average pooling on the feature map. Extract the texture activity index of the current level, perform square root processing on the index, and expand it into a bias vector with the same dimension as the one-dimensional channel feature vector after mapping through learnable parameters; The updated fusion vector is obtained by adding the one-dimensional channel feature vector to the bias vector element by element. The fusion vector is nonlinearly mapped to generate channel gating values, which are used to perform channel-dimensional attention-weighted filtering of the spliced features.
[0044] The input is a feature map with a spatial size of 64×64 and a total of 256 channels, output by the decoding network. This total number of channels is formed by merging 128-dimensional fused features after correction and 128-dimensional upsampled decoder features. The network calls a global average pooling layer to perform a mean operation on 4096 pixels in each channel, compressing and aggregating the height and width spatial dimensions to extract a one-dimensional channel feature vector of dimension 1×1×256, in order to detect the global response intensity of each channel. The texture activity scalar index stored in the current level is extracted, and the square root function is applied to perform a square root operation to compress outliers of the activity. The smoothed scalar data is input into a bias mapping network configured with two fully connected perceptron layers. This bias mapping network is a small neural network model for nonlinear expansion of feature dimensions. The input is a smoothed one-dimensional scalar texture index, and the output is a high-dimensional bias vector containing the intensity of texture fluctuations at a specific level. The structure includes a first fully connected hidden layer for feature amplification and a second fully connected output layer for dimension reshaping. The first layer expands the 1D scalar features to a 32D latent state. The second layer includes learnable weight parameters updated with the gradient, reshaping and amplifying the 32D latent state features into a 256D space. This generates a bias vector containing the intensity of texture fluctuations at specific levels. The values of each channel in this bias vector are determined during training and are used to enhance or suppress different channels, rather than simply amplifying them positively. An element-wise vector addition operator is executed, injecting the calculated 256D bias vector into the aforementioned 1D channel feature vector, forming an updated fusion vector that combines local content representation with global prior texture characteristics. This fusion vector is then fed into a standard activation function for mapping, outputting 256 floating-point values constrained to channel gating values between 0 and 1. This gating vector is then multiplied by the original feature map to filter channels containing defect responses such as cracks, damage, and scorch marks, and to reduce interference from the pure background information layer.
[0045] S5, when the maximum response value in the probability map output by the decoder is greater than the set threshold, it is determined that there is a defect at the corresponding position of the pastry and the detection result is output.
[0046] The feature map output from the top layer of the decoder is processed by a 1×1 convolutional layer with a kernel size of 1 and a torch.sigmoid activation function to generate a defect probability map tensor with the same spatial size as the original input pastry image. Each pixel value is located in the range of 0 to 1, representing the probability of defect presence. The maximum pixel response value representing the highest defect confidence is found and extracted across the spatial dimension of the entire probability map tensor. For threshold calculation, a basic system judgment threshold, such as 0.5, is initialized. The texture activity index values recorded at all four levels of the encoder are extracted and normalized according to the statistical benchmarks obtained for each level in the training set or calibration samples. The overall arithmetic mean of the normalized index is then calculated. A smoothing benchmark value with the same statistical caliber as the overall texture activity index is preset, and a positive adjustment weight coefficient is set. An adjustment factor is calculated based on the difference between the mean of the overall texture activity index and the smoothing benchmark value. The basic judgment threshold is multiplied by the adjustment factor to obtain the corrected threshold. A threshold is set, and the corrected threshold is limited to a preset minimum threshold and a preset maximum threshold, both between 0 and 1, to accommodate the differences in natural crack textures on the surface of pastries from different baking batches. The relationship between the maximum response value and the corrected threshold is compared. If the maximum response value is greater than the threshold, a binary thresholding method is used to segment the probability map into a binary mask image to extract the defect connected component contours. Based on the locations of connected components exceeding the threshold in the binary mask image, it is determined whether the corresponding area in the currently acquired pastry image has cracks, damage, or scorch marks. The detection result image with the red alarm box and defect mask superimposed is saved to the local industrial control computer hard drive, and the detection result is sent out via serial port output of an NG level signal.
[0047] In some embodiments, the threshold determination process specifically includes: Extract texture activity metrics recorded in all layers of the network encoder; After normalizing the extracted texture activity indices of each level according to the preset statistical benchmark of the corresponding level, the indices are summed and divided by the total number of levels to calculate the global mean of the overall texture activity index of the pastry image. A preset basic judgment threshold is set, and an adjustment factor is calculated based on the global mean. When the global mean is high, the adjustment factor is increased, and when the global mean is low, the adjustment factor is decreased. Multiply the basic judgment threshold by the adjustment factor to obtain the set threshold used for defect judgment.
[0048] The U-shaped detection network is a fully convolutional neural network model based on an encoder and decoder architecture. The input is the original image to be detected, and the output is a probability map of defect predictions with equivalent spatial resolution. The structure includes an encoder branch responsible for extracting features layer by layer and a decoder branch responsible for progressively restoring spatial resolution and fusing encoder features. After the entire forward inference phase of the U-shaped detection network, the feature evaluation module extracts four independent texture activity index values recorded by all four multi-scale downsampling layers in the encoder (assuming a standard level). An array traversal and accumulation operation is performed, and the four extracted texture activity indexes are normalized according to the statistical benchmark of the training set for their respective levels. The sum of the four normalized activity values is then divided by the constant total number of levels (4), resulting in a scalar parameter used to evaluate the overall texture complexity level of the surface of a single pastry image to be detected, namely the global mean. When detecting a puff pastry with a complex network of crusts on its surface, the global mean of the aggregated texture activity indexes for that puff pastry may be as high as 0.12. The model initialization deployment file pre-allocates a basic decision threshold for determining the confidence level of positive and negative sample classification. The preferred value for this threshold is typically set to 0.65. The detection module internally incorporates function calculation formulas; the calculation formula corresponding to the generated rules is as follows: ,in This is the difference between the global mean and the preset smoothing benchmark value, and the preset smoothing benchmark value and the global mean use the same texture activity statistical caliber. For example... Figure 3 As shown, as the overall texture complexity of the pastry surface increases, the judgment threshold is adjusted upwards to suppress false detections against high-texture backgrounds; conversely, the threshold is lowered to avoid missed detections. Assuming the preset smoothing baseline value is configured as 0.05 and the scaling factor as 2.0, when a previously calculated high-complexity sample is input with a global mean of 0.12, the adjustment factor will automatically increase to 1.14 according to the above calculation formula. The adjustment factor is 1 + a × (M - M0), where M is the global mean of the overall texture activity index, M0 is the smoothing baseline value, and a is the positive adjustment coefficient. The corrected setting threshold is limited between the preset minimum threshold and the preset maximum threshold. A floating-point multiplication is performed between the basic judgment threshold of 0.65 and the adjustment factor of 1.14 to obtain 0.741, which is used as the output setting threshold for the current image defect judgment. This setting threshold is further limited between the preset minimum threshold and the preset maximum threshold to ensure that it remains within the reasonable range required for probability judgment. By employing a global parameter tuning strategy, false alarms on highly rough surfaces can be reduced. When dealing with extremely smooth pastries, the detection sensitivity for weak-response defects such as tiny black spots and cracks can be improved by lowering the threshold.
[0049] The experiments used a unified dataset of pastry defect images containing various surface materials for both training and testing, with the spatial resolution of the test images uniformly adjusted to 256×256 pixels. During network training, an adaptive moment estimation optimizer was used with an initial learning rate of 0.001, employing a cosine annealing learning rate decay strategy and undergoing iterative training for two hundred epochs. All ablation models ran on the same hardware acceleration device and software framework to ensure consistency in the experimental evaluation environment; the performance metrics for the models were precision, recall, and mean precision.
[0050] The experiments quantitatively tested the baseline model of the basic feature network and variant models that sequentially incorporated a high-frequency component weighted scaling module, an offset field correction module, and a texture-aware gating and thresholding module. The results showed that the baseline model achieved an average precision of 82.5%, an accuracy of 84.1%, and a recall of 80.3% on the test set. Adding only the high-frequency component weighted scaling module to the baseline model improved the average precision to 85.2%. Introducing the offset field correction module, which constrains the maximum single-axis offset by the texture activity index, resulted in an average precision of 87.8%. When all proposed modules were fused to form a complete scheme, superior detection performance was achieved under the stated test conditions, with an average precision of 90.6%, an accuracy of 91.2%, and a recall of 89.4%.
[0051] The high-frequency component processing module improved the average accuracy by 2.7%, indicating that the weighted combination of second-order channel statistics and activity-based adjustment weights highlighted the subtle boundaries of true defects and suppressed interference from complex backgrounds. The offset field correction module contributed a 2.6% gain, demonstrating that the method of limiting the maximum uniaxial deformation tolerance based on local texture indices helps maintain surface geometric stability and reduces the risk of feature misalignment due to excessive deformation. Threshold adjustment based on the global activity mean and feature stitching gating mechanism further improved the overall detection accuracy of the model, indicating that this strategy can effectively balance false positives in rough material samples and false negatives in smooth material samples under test conditions, enhancing the network's ability to suppress background texture interference.
[0052] Example 2 Embodiment 2 of the present invention provides a system for detecting the appearance of pastries, comprising the following modules: The acquisition module is used to acquire pastry images and send them to the U-shaped detection network for multi-scale feature extraction. The feature map output by each level of the network encoder is normalized by the channel and used as the initial feature for Skip connection. The mean channel variance is calculated based on the corresponding level feature map before normalization and the mean channel variance is recorded as the texture activity index. The determination module is used to decompose the initial features of the Skip connection after channel normalization into low-frequency and high-frequency components through discrete wavelet transform in the Skip connection. The low-frequency components are processed by depthwise separable convolution, and the high-frequency components are weighted by second-order channel statistics. The high-frequency components are scaled according to the texture activity index after being weighted by second-order channel statistics. The processed components are reconstructed into a fused feature map through inverse wavelet transform. An offset field is generated by convolution between the corresponding layer features of the decoder and the fused feature map. The position of the fused feature map is corrected using the offset field to obtain the corrected features. The maximum allowable single-axis offset of the offset field is determined by the texture activity index of the current layer. The output module is used to concatenate the corrected features with the decoder features and filter them through a gating unit. The gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function. When the maximum response value in the probability map output by the decoder is greater than the set threshold, it is determined that there is a defect at the corresponding position of the pastry and the detection result is output.
[0053] In this specification, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise limited, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element. In this document, "a," "an," "the," "the," and "its" may also include plural forms unless the context clearly indicates otherwise. "Multiple" refers to at least two, such as 2, 3, 5, or 8, etc. "And / or" includes any and all combinations of the associated listed items.
[0054] The various embodiments in this specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can be referred to each other.
[0055] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for detecting the appearance of pastries, characterized in that, Includes the following steps: Images of pastries are acquired and fed into a U-shaped detection network for multi-scale feature extraction. The feature maps output by each level of the network encoder are normalized by channels and used as the initial features for Skip connections. The mean channel variance is calculated based on the corresponding level feature maps before normalization and recorded as a texture activity index. In Skip Connect, the initial Skip Connect features after channel normalization are decomposed into low-frequency and high-frequency components using discrete wavelet transform. The low-frequency components are processed by depthwise separable convolution, and the high-frequency components are weighted by second-order channel statistics. The high-frequency components are then scaled according to the texture activity index after being weighted by second-order channel statistics. The processed components are then reconstructed into a fused feature map using inverse wavelet transform. An offset field is generated by convolution between the corresponding layer features of the decoder and the fused feature map. The offset field is used to correct the position of the fused feature map to obtain the corrected features. The maximum allowable single-axis offset of the offset field is determined by the texture activity index of the current layer. The corrected features are concatenated with the decoder features and filtered by a gating unit. The gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function. When the maximum response value in the probability map output by the decoder is greater than the set threshold, it is determined that there is a defect at the corresponding position of the pastry and the detection result is output.
2. The method according to claim 1, characterized in that, The output feature map of each level of the network encoder is processed by channel normalization and used as the initial feature for Skip connections. The mean channel variance is calculated based on the corresponding level feature map before normalization, and this mean channel variance is recorded as a texture activity index, including: Extract the two-dimensional slice features of the output feature map of the current encoder before normalization processing in the channel dimension; The average value of all pixel feature values in each slice feature is calculated as the mean of the channel; Calculate the square of the difference between the feature value of each pixel in each slice feature and the mean value, sum over all pixels and divide by the total number of pixels to obtain the variance of the channel; The mean variance of all channels in the current level feature map is obtained by summing the variances and dividing by the total number of channels. This mean variance is then recorded as the texture activity index of the current level.
3. The method according to claim 2, characterized in that, The high-frequency components are weighted by second-order statistics of the channels, including: Extract the feature vectors of high-frequency components along the channel dimension at each spatial location; Calculate the average of the squared values of each element in the feature vector to obtain the second-order energy statistic of the channel that characterizes the high-frequency response intensity at the spatial location; or calculate the weighted sum of the channel variance and mean square energy of the feature vector as the second-order statistic of the spatial location. The calculated second-order statistics for each spatial location are normalized and then multiplied element-wise with the feature vector of the high-frequency component at the corresponding spatial location to obtain the high-frequency features after weighting the second-order statistics. This process relatively preserves and highlights local regions with strong high-frequency responses while suppressing invalid responses in smooth regions.
4. The method according to claim 1, characterized in that, The high-frequency components are weighted by second-order channel statistics and then scaled according to the texture activity index, including: Set the lower limit reference value for the amplitude scaling factor; Obtain the texture activity index of the current level, normalize the index to the range of 0 to 1 as the adjustment weight, wherein the texture activity index is used to characterize the overall background texture complexity of the current level, and is not directly used as the defect detail intensity. The scaling factor is calculated based on the lower limit reference value and the adjustment weight. The scaling factor approaches the lower limit reference value as the adjustment weight increases and approaches 1 as the adjustment weight decreases, so as to suppress high-frequency noise generated by normal pastry texture when the overall background texture is complex, and retain the high-frequency response required for defect edges through the lower limit reference value. The amplitude scaling of the high-frequency features is completed by multiplying each element of the weighted high-frequency features by the calculated scaling factor, thus preserving the high-frequency details corresponding to weak defects when the overall background texture is low.
5. The method according to claim 1 or 2, characterized in that, The process of generating an offset field based on the corresponding layer features of the decoder and the fused feature map through convolution includes: The upsampled features of the corresponding layer of the decoder are concatenated with the fused feature map along the channel dimension to obtain the concatenated features; Two consecutive two-dimensional convolution operations are performed on the spliced features to extract the spatial position difference information between the decoder features and the fused feature map; The last inactive two-dimensional convolutional layer outputs a feature map with two channels. The two channels of the feature map correspond to the pixel offsets in the horizontal and vertical directions, respectively, forming the offset field.
6. The method according to claim 1, characterized in that, The maximum permissible single-axis offset of the offset field is determined by the texture activity index of the current level, including: The maximum absolute offset constant allowed by the preset offset field; Obtain the texture activity index of the current level record and map the index to a constraint coefficient between 0 and 1 through a linear transformation; The constraint coefficient is converted into an offset suppression coefficient, so that the higher the texture activity index of the current level, the smaller the maximum allowable single-axis offset; the lower the texture activity index of the current level, the larger the maximum allowable single-axis offset. The offset in the generated offset field is numerically truncated using the maximum permissible single-axis offset, so that the horizontal and vertical offsets of all pixels are limited to the range of the maximum permissible single-axis offset.
7. The method according to claim 1, characterized in that, The process of concatenating the corrected features with the decoder features and filtering them through a gating unit, wherein the gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function, includes: A one-dimensional channel feature vector is obtained by concatenating the corrected features with the decoder features and performing global average pooling on the feature map. Extract the texture activity index of the current level, perform square root processing on the index, and expand it into a bias vector with the same dimension as the one-dimensional channel feature vector after mapping through learnable parameters; The updated fusion vector is obtained by adding the one-dimensional channel feature vector to the bias vector element by element. The fusion vector is nonlinearly mapped to generate channel gating values, which are used to perform channel-dimensional attention-weighted filtering of the spliced features.
8. The method according to claim 1, characterized in that, The process of determining the set threshold is as follows: Extract texture activity metrics recorded in all layers of the network encoder; After normalizing the extracted texture activity indices of each level according to the preset statistical benchmark of the corresponding level, the indices are summed and divided by the total number of levels to calculate the global mean of the overall texture activity index of the pastry image. A preset basic judgment threshold is set, and an adjustment factor is calculated based on the global mean. When the global mean is high, the adjustment factor is increased, and when the global mean is low, the adjustment factor is decreased. Multiply the basic judgment threshold by the adjustment factor to obtain the set threshold used for defect judgment.
9. A system for detecting the quality of pastries, characterized in that, Includes the following modules: The acquisition module is used to acquire pastry images and send them to the U-shaped detection network for multi-scale feature extraction. The feature map output by each level of the network encoder is normalized by the channel and used as the initial feature for Skip connection. The mean channel variance is calculated based on the corresponding level feature map before normalization and the mean channel variance is recorded as the texture activity index. The determination module is used to decompose the initial features of the Skip connection after channel normalization into low-frequency and high-frequency components through discrete wavelet transform in the Skip connection. The low-frequency components are processed by depthwise separable convolution, and the high-frequency components are weighted by second-order channel statistics. The high-frequency components are scaled according to the texture activity index after being weighted by second-order channel statistics. The processed components are reconstructed into a fused feature map through inverse wavelet transform. An offset field is generated by convolution between the corresponding layer features of the decoder and the fused feature map. The position of the fused feature map is corrected using the offset field to obtain the corrected features. The maximum allowable single-axis offset of the offset field is determined by the texture activity index of the current layer. The output module is used to concatenate the corrected features with the decoder features and filter them through a gating unit. The gating value of the gating unit is obtained by pooling the concatenated features and using the texture activity index as the bias input activation function. When the maximum response value in the probability map output by the decoder is greater than the set threshold, it is determined that there is a defect at the corresponding position of the pastry and the detection result is output.
10. The system according to claim 9, characterized in that, The output feature map of each level of the network encoder is processed by channel normalization and used as the initial feature for Skip connections. The mean channel variance is calculated based on the corresponding level feature map before normalization, and this mean channel variance is recorded as a texture activity index, including: Extract the two-dimensional slice features of the output feature map of the current encoder before normalization processing in the channel dimension; The average value of all pixel feature values in each slice feature is calculated as the mean of the channel; Calculate the square of the difference between the feature value of each pixel in each slice feature and the mean value, sum over all pixels and divide by the total number of pixels to obtain the variance of the channel; The mean variance of all channels in the current level feature map is obtained by summing the variances and dividing by the total number of channels. This mean variance is then recorded as the texture activity index of the current level.