X-ray detection method for new energy vehicle aluminum alloy die casting defects based on transunet model

CN123032607APending Publication Date: 2026-11-03GUANGZHOU CITY UNIV OF TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611110962.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-24
Publication Date
2026-11-03

AI Technical Summary

Technical Problem

In existing technologies, the internal defect detection efficiency of aluminum alloy die-cast parts for new energy vehicles is low and the accuracy is insufficient. Automated quantitative analysis is not possible, resulting in high rates of missed and false detections, which cannot meet the needs of high-efficiency production.

Method used

A detection method based on the TransUNet model is adopted. It extracts local details and global features by combining a two-layer encoder structure with CNN and Transformer architecture. It uses a multi-channel initialization mask module for defect identification and calculates the defect area and regional porosity through gridded analysis.

Benefits of technology

It enables high-precision identification and automated quantitative analysis of internal defects in aluminum alloy die castings, improving detection efficiency, reducing the missed detection rate, providing reliable quality assessment data support, and optimizing the die casting process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123032607A_ABST
    Figure CN123032607A_ABST
Patent Text Reader

Abstract

The application relates to a new energy vehicle aluminum alloy die casting defect X-ray detection method based on a TransUNet model, which firstly performs size normalization processing on an acquired X-ray original image; then inputs the image into an improved TransUNet model adopting a double-layer encoder structure, the model extracts local detail features by using a bottom-layer CNN encoder, and extracts global spatial features by using a high-layer Transformer encoder; at the decoder end, the local and global features are fused, and a multi-channel initialization mask module is used to parallelly output the segmentation areas of each defect type and the corresponding types; finally, according to the segmentation result, the defect area is automatically calculated, and a grid analysis method is used to calculate the area porosity of the target area. By combining the local perception ability of CNN and the global modeling advantage of Transformer, the recognition accuracy of complex defects is improved, the automation and digital quantitative analysis of the defect area and the area porosity are realized, and an efficient and reliable technical means is provided for quality control and process optimization of die castings.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of intelligent inspection technology for automotive die-cast parts, and in particular to an X-ray inspection method for defects in aluminum alloy die-casting of new energy vehicles based on the TransUNet model. Background Technology

[0002] With the rapid development of the new energy vehicle industry, lightweighting has become a core technological path to improve driving range and optimize vehicle performance. Aluminum alloys, due to their low density, high strength, and good thermal conductivity, are widely used in the manufacture of key components such as battery trays, motor housings, and body structural parts. High-pressure die casting is the main process for achieving large-scale, high-efficiency production of these complex structural parts. However, during high-pressure die casting, due to the complexity of the molten metal filling and solidification process, various defects such as porosity, shrinkage cavities, and segregation are easily generated inside the casting. These internal defects are fatal hidden dangers affecting the mechanical properties, airtightness, and service life of components. Therefore, accurate and reliable non-destructive testing of internal defects in die castings is a crucial link in ensuring the safety of new energy vehicles.

[0003] Currently, industrial X-ray inspection is the most widely used non-destructive testing technology in this field. Traditional inspection methods primarily rely on experienced inspectors manually interpreting X-ray images. This approach is not only inefficient and costly, but the results are also highly susceptible to subjective factors (such as visual fatigue and experience differences), leading to high rates of missed and false positives. Furthermore, to match production schedules, companies often employ sampling inspections, which exposes defective products to the risk of flowing into subsequent processes, hindering comprehensive traceability and control of product quality.

[0004] To overcome the drawbacks of manual inspection, researchers have attempted to introduce automated inspection techniques based on traditional image processing algorithms. These techniques identify defects through threshold segmentation and edge detection, but their segmentation effectiveness and robustness are insufficient for defects with indistinct grayscale differences, blurred edges, and irregular shapes (such as cloud-like shrinkage). In recent years, with the rise of artificial intelligence, deep learning methods, represented by convolutional neural networks (CNNs), such as the U-Net model, have been introduced into defect detection. CNN models, with their powerful local feature extraction capabilities, have improved the accuracy of defect recognition to some extent. However, the inherent limitations of convolutional operations make it difficult to effectively capture the global contextual information of an image. When faced with complex defects with large size ranges and varied shapes, their insufficient global modeling capabilities become apparent. More importantly, both traditional methods and conventional CNN methods mostly remain at the level of defect "detection," generally lacking the ability to perform automated, digital quantitative analysis of key physical quantities such as defect area and regional porosity, thus failing to provide direct data support for the optimization of die-casting processes and the digital control of product quality. Therefore, developing an intelligent detection method that can achieve both high-precision identification and automated quantitative analysis has become a pressing technical challenge in this field. Summary of the Invention

[0005] To address the technical problems existing in the prior art, this invention provides an X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model. The method aims to solve the problems of low detection efficiency, insufficient accuracy, and inability to perform automated quantitative analysis in the prior art, thereby achieving high-precision identification, type differentiation, and automated and digital evaluation of defect area and regional porosity of die castings.

[0006] This invention discloses an X-ray detection method for die-casting defects in aluminum alloys for new energy vehicles based on the TransUNet model, comprising the following steps:

[0007] The raw X-ray image of the aluminum alloy die-cast part of the new energy vehicle to be tested is acquired, and the size of the raw X-ray image of the aluminum alloy die-cast part of the new energy vehicle to be tested is normalized to obtain the image to be tested; The image to be detected is input into a pre-trained improved TransUNet model for feature extraction. The improved TransUNet model adopts a two-layer encoder structure, which uses the continuous convolutional layers in the bottom encoder to extract local detail features of the image to be detected, and uses the sliding window multi-head self-attention module in the high-level encoder to extract global spatial features of the image to be detected. At the decoder end of the improved TransUNet model, the extracted local detail features and global spatial features are fused, and an independent mask channel is assigned to each preset die casting defect type through a multi-channel initialization mask module, outputting the segmented regions of various die casting defects in the image to be detected and the corresponding defect types. Based on the output segmented regions and the mapping relationship between image pixels and actual physical dimensions, the defect area of ​​each segmented region is calculated. Simultaneously, a meshing analysis method is used to mesh the target region containing the segmented regions, and the proportion of defect pixels in the target region to the total number of pixels in the mesh is calculated. Combined with the physical density parameters of the aluminum alloy die casting, the regional porosity of the target region is calculated.

[0008] It is understood that the core of the technical solution of the present invention lies in constructing and applying a deeply improved TransUNet neural network model to achieve high-precision, automated detection and quantitative analysis of internal defects in X-ray images of aluminum alloy die-cast parts for new energy vehicles.

[0009] First, at the initial stage of the method, the system acquires raw X-ray images of the aluminum alloy die-casting part to be inspected. Considering that images acquired on actual production lines may vary in resolution due to factors such as shooting distance and equipment model, the first step of the method is to normalize the size of these raw images to ensure consistency and stability in subsequent model processing. This step uniformly adjusts images of different sizes to a fixed standard size (e.g., 640x640 pixels), thus forming standardized "images to be inspected," providing the model with input data in a uniform format.

[0010] Next, we move to the most crucial feature extraction stage of this invention. The standardized image to be detected is fed into a pre-trained improved TransUNet model. This improved TransUNet model is the key technology of this invention, embodied in an innovative two-layer encoder structure that cleverly combines the advantages of two mainstream neural network architectures: First, it utilizes a low-level encoder that employs the classic continuous convolution and downsampling operations of Convolutional Neural Networks (CNNs). This design can very effectively capture the local dependencies between pixels in the image, thereby accurately extracting local details such as the edges and textures of defects. Second, it introduces a high-level encoder that adopts the Transformer architecture, which currently excels in global information modeling, particularly utilizing its sliding window multi-head self-attention module (SW-MSA). After the low-level encoder compresses the image into a smaller feature map, the high-level encoder models the global relationships of the features based on this. It is no longer limited to neighboring pixels but can capture long-distance dependencies between any two locations in the image. This is crucial for identifying irregularly shaped and widely distributed defects (such as cloud-like contractions), thus efficiently extracting global spatial features.

[0011] After feature extraction, the model enters the decoding and output stage. At the decoder, a crucial feature fusion step is performed. High-resolution feature maps rich in detail from the lower-level encoder are merged with low-resolution feature maps containing global context information from the higher-level encoder at different levels of the decoder through skip connections and other methods. This high-low fusion approach ensures that the final generated features retain both precise defect contour details and an accurate understanding of the overall morphology. Next, a multi-channel initialization mask module begins operation, assigning an independent output channel to each predefined die-casting defect type (such as porosity, shrinkage cavities, etc.). This parallel processing mechanism avoids interference between different types of defect features in a single channel, with each channel focusing on identifying a specific defect. Ultimately, this module can simultaneously output precise segmentation maps of each defect region in the detected image, along with the corresponding specific defect type.

[0012] Finally, this invention elevates the process from simple identification to quantitative analysis. Based on the defect segmentation regions output by the model, the system first uses a preset conversion relationship between image pixels and actual physical dimensions to accurately calculate the defect area represented by each segmented region, providing quantitative data on the size of individual defects. Simultaneously, to assess the density of local areas in the casting, mesh analysis is introduced. The target region containing defects is virtually meshed, and then the proportion of defect pixels within each mesh is statistically analyzed. This proportion essentially reflects the porosity of the region due to the presence of defects. Further combining known physical density parameters of aluminum alloy materials (such as matrix density), the system can calculate the regional porosity of the target region using a specific physical formula. This porosity index provides a direct and reliable digital basis for evaluating the internal quality of castings and guiding and optimizing die-casting processes.

[0013] The X-ray detection method for die-casting defects in aluminum alloys for new energy vehicles based on the TransUNet model of the present invention has the following technical advantages: First, detection accuracy and recognition capability are significantly improved. This invention organically combines the powerful local detail capture capability of CNNs with the excellent global context modeling capability of Transformers through an innovative dual-layer encoder structure. This enables the model to not only clearly delineate defects with distinct boundaries (such as pores), but also accurately identify and segment complex defects with blurred, irregular, and widely distributed shapes (such as cloud-like shrinkage), overcoming the shortcomings of traditional image processing methods and simple CNN models in such problems, and significantly improving detection accuracy and recall.

[0014] Secondly, it achieves automated, multi-dimensional quantitative analysis of defects. This invention goes beyond simply marking defects as "present" or "absent," taking a crucial step by automatically calculating the precise area of ​​each defect and the regional porosity of a specified area. These two core quantitative indicators provide objective and repeatable digital evidence for assessing the quality grade of die-cast parts, eliminating the subjective judgment based on experience in traditional manual inspection. More importantly, this data can be directly fed back to the production end, providing scientific guidance for optimizing mold design and adjusting die-casting process parameters (such as pressure, speed, and temperature), thereby improving product quality from the source.

[0015] Furthermore, the detection efficiency and automation level are significantly improved, achieving full inspection capability. The entire process, from image input to final output of quantitative analysis results, achieves end-to-end automated processing, greatly reducing manual intervention and avoiding misjudgments and omissions caused by human fatigue and experience differences. Its efficient feature extraction and calculation process makes it possible to perform 100% through-line inspection of every die-casting part, completely avoiding the risk of missed inspections of non-conforming products that may arise from traditional sampling methods, and providing comprehensive assurance for the quality and safety of key components of new energy vehicles.

[0016] Finally, the differentiation of defect types is more accurate and reliable. By employing a multi-channel initialization mask module, independent recognition and segmentation channels are assigned to each defect type, effectively avoiding mutual interference between different types of defect features. This allows the model to more accurately determine the specific type (porosity or shrinkage) while outputting the defect location and size, providing more refined information for subsequent defect cause analysis and process improvement. Attached Figure Description

[0017] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0018] Figure 1 This is a schematic diagram of the overall process of the detection method described in this invention; Figure 2 This is an example image of X-ray images and their labeled masks of some die-casting defects in the dataset of this invention; Figure 3 This is a schematic diagram of the overall network architecture of the improved TransUNet model described in this invention; Figure 4 This is an example of the visualization results of the model described in this invention segmenting and classifying different defects; Figure 5 This is a schematic diagram of the visualization results of the quantitative analysis of defect area and regional porosity described in this invention; Figure 6 This is a flowchart illustrating the model training process of the present invention; Figure 7 Table 1 shows the results of ablation experiments conducted to verify the effectiveness of each improved module of the present invention. Figure 8 Table 2 shows a quantitative comparison of the performance indicators of the method of this invention with other models; Figure 9This is a graph showing the changes in loss and accuracy during the training process of the model of this invention.

[0019] Figure label: none. Detailed Implementation

[0020] The embodiments of the present invention are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention. In the description of the present invention, it should be understood that the terms "center," "longitudinal," "lateral," "length," "width," "thickness," "upper," "lower," "front," "rear," "left," "right," "vertical," "horizontal," "top," "bottom," "inner," "outer," "clockwise," "counterclockwise," "axial," "radial," "circumferential," etc., indicating orientation or positional relationships, are based on the orientation or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation, and therefore should not be construed as limiting the present invention.

[0021] This embodiment discloses an X-ray detection method for defects in aluminum alloy die castings for new energy vehicles based on the TransUNet model. This method aims to achieve automated, high-precision identification and quantitative analysis of internal defects in die castings. (Reference) Figure 1 The detection method of this embodiment is implemented in its entirety through a specially designed and trained improved TransUNet model. The core steps and implementation details are as follows: Step 1: Image Acquisition and Preprocessing. This step is detailed below: First, raw X-ray images of the aluminum alloy die-cast parts for new energy vehicles to be inspected are acquired using industrial X-ray flaw detection equipment. These raw images accurately reflect the internal microstructure of the castings. Due to differences in shooting conditions in actual production environments, the acquired raw images may vary in size and resolution. To provide a stable and uniform input for the subsequent neural network model, these raw images need to be normalized. Specifically, this embodiment uses image scaling algorithms such as bilinear interpolation to uniformly adjust all raw images of different resolutions to a fixed size of 640×640 pixels. The resulting image is the image to be inspected. This standardized preprocessing step is a crucial prerequisite for ensuring that the model can perform stable and efficient feature extraction subsequently.

[0022] The second step: Feature extraction based on the improved TransUNet model. This step is detailed as follows: The preprocessed image to be detected is input into a pre-trained improved TransUNet model to perform the core feature extraction task. The specific architecture of the model is as follows: Figure 3 As shown, it innovatively employs a dual-layer encoder structure to collaboratively capture multi-level feature information in images.

[0023] In this structure, the input image to be detected is first processed by a low-level encoder. This low-level encoder follows the classic U-Net convolutional neural network (CNN) encoding path, performing progressive downsampling of the image through successive 3×3 convolutional layers and max pooling layers. During the downsampling process, the convolution operation can effectively capture the local spatial relationships within the pixel neighborhood, thereby accurately extracting local detail features such as the edge contours and texture changes of defects.

[0024] As the lower-level encoder delves deeper, the size of the feature maps gradually decreases, while the semantic information becomes increasingly rich. When the feature maps are compressed to their minimum size, they are passed to the higher-level encoder. This higher-level encoder introduces a core component of the Transformer architecture—the Sliding Window Multi-Head Self-Attention (SW-MSA) module. The higher-level encoder first flattens the received minimum-size feature map, treating it as a sequence of feature vectors. Subsequently, the SW-MSA module processes this image sequence, no longer limited to the receptive field of local convolutional kernels, but capable of calculating the correlation between any two feature vectors in the sequence globally, thus effectively capturing large-scale, long-distance spatial dependencies in the image. This is crucial for identifying diffuse, irregular defects (such as cloud-like condensation). In this way, the higher-level encoder efficiently extracts the global spatial features of the image to be detected.

[0025] The third step: Feature fusion and multi-channel output. This step is detailed below: At the decoder end of the model, in order to generate high-precision segmentation results, the local details extracted by the encoder must be effectively fused with global spatial information. For example... Figure 3 As shown, in each upsampling path of the decoder, feature maps rich in global semantic information from higher-level encoders are concatenated with feature maps from lower-level encoders at corresponding levels that retain high-resolution detail information through skip connections. This design enables the model to gradually restore the spatial resolution of the image while ensuring that the final prediction has both macroscopic correctness and microscopic accuracy.

[0026] After feature fusion, a multi-channel initialization mask module is responsible for generating the final output. This module assigns an independent output channel to each predefined die-casting defect type (e.g., porosity, shrinkage cavities, segregation, etc.). Each channel focuses on identifying a specific defect and outputs a binary segmentation mask in which pixels belonging to that defect type are marked as 1, while the background is marked as 0. This parallel output mechanism enables the model to clearly distinguish different types of defects and ultimately output a composite image containing accurately segmented regions of all defects and their corresponding defect types.

[0027] Step 4: Quantitative Defect Analysis. This step is detailed below: The ultimate goal of this embodiment is to achieve automated, digital, and quantitative assessment of defects. After obtaining a segmentation map containing the location and type of defects, the system will perform the following quantitative analysis steps: (1) Defect area calculation: The system traverses each identified independent defect region in the segmentation image. Based on the pre-calibrated mapping relationship between image pixels and actual physical size (e.g., millimeters), the system automatically calculates the total number of pixels covered by each segmentation region and converts it into the actual defect area (e.g., in mm²).

[0028] (2) Calculation of regional porosity: To evaluate the overall quality of a specific region of the casting, this embodiment introduces a mesh analysis method. The operator or system can select a target region of interest (e.g., a critical stress-bearing part of the casting). Then, this target region is virtually meshed. Subsequently, the system calculates the total number of defect pixels within the target region and divides it by the total number of pixels in the mesh of that region, thereby obtaining a key parameter—the proportion of defect pixels to the total number of pixels in the mesh. This proportion can intuitively reflect the porosity of the region. Finally, combined with the pre-input physical density parameters of the aluminum alloy die casting of this grade (such as the physical density of the matrix when there are no defects), the system can calculate the regional porosity of the target region using the algorithm formula further described in this embodiment.

[0029] Therefore, through the above steps, the method disclosed in this invention can not only identify the location and type of defects, but also perform precise quantitative analysis of defects, providing accurate and reliable digital basis for quality rating, process optimization, and full life-cycle quality traceability of die-cast parts. For example, as Figure 5 As shown, this method can independently calculate the area of ​​the three selected defect areas (1#, 2#, and 4#), and quantitatively calculate and visualize the porosity of six key areas, thus providing engineering technicians with an intuitive analysis tool.

[0030] Furthermore, regarding the feature extraction step based on the improved TransUNet model described in the second step above, as follows: Figure 3 The improved TransUNet model architecture shown here, with its core component for feature extraction being the aforementioned two-layer encoder structure, has the following internal structure and workflow: First, the dual-layer encoder structure includes a bottom-layer encoder and a top-layer encoder, which comprehensively capture local details and global contextual information of the image.

[0031] Regarding the low-level encoder, in this embodiment, it adopts the classic Convolutional Neural Network (CNN) encoding path, which is consistent with the left half of the encoding path of the standard U-Net model. When the image to be detected is input, the low-level encoder processes it through a series of consecutive 3×3 convolutional layers and max pooling downsampling layers. During the progressive downsampling process, the spatial resolution of the feature map gradually decreases, while the receptive field increases accordingly, enabling the network to abstract increasingly higher-level semantic information from the original pixels. This process is particularly adept at capturing the close dependencies between pixels in the image, thus effectively extracting local contextual information and detailed features, including defect edges, textures, and shapes.

[0032] The high-level encoder is responsible for processing the feature map output by the low-level encoder, which has the smallest spatial size but the richest semantic information. To overcome the limitations of traditional convolution in long-distance dependency modeling, the high-level encoder in this embodiment also introduces a Transformer architecture. Its specific workflow is as follows: First, the high-level encoder receives the feature map from the deepest layer of the low-level encoder and flattens this two-dimensional feature map, converting it into a one-dimensional image sequence, where each element is a feature vector.

[0033] Subsequently, in this embodiment, the aforementioned high-level encoder performs a series of computational steps to extract global spatial features from the image sequence using the sliding window multi-head self-attention (SW-MSA) module. This process first uses a multi-head segmentation method to segment the input global query matrix Q, key matrix K, and value matrix V to obtain h groups (e.g., h can be set to 4, 8, or 12, etc.) of independent head vectors. This multi-head mechanism allows the model to learn information in parallel across different representation subspaces, thereby capturing richer feature relationships. Specifically, the segmentation method is implemented using the following formula:

[0034] Where i is the attention head number and h is the total number of multi-head attention heads; q, k, and v are the original input query matrix, key matrix, and value matrix, respectively; , , The following are, in order, the query projection weight matrix, the key projection weight matrix, and the value projection weight matrix corresponding to the i-th attention head; , , , , and , respectively, are the single-head query matrix, single-head key matrix, and single-head value matrix obtained after projection of the i-th head weight.

[0035] After completing the multi-head segmentation, the model performs attention calculation. This step aims to calculate the similarity of each set of Q, K, and V vectors (i.e., each "head" after segmentation) to obtain attention weights. The formula for this attention calculation is:

[0036] Where Q is the query matrix, which corresponds to the defect features that the model needs to focus on and retrieve; K is the key matrix, and V is the value matrix; This is the transpose of the key matrix; d is the dimension of the key vector; The softmax function is a normalization exponential function that converts the calculated similarity scores into a probability distribution with a sum of 1, i.e., attention weights. The final This refers to the attention-weighted output features that incorporate global contextual information. Through this mechanism, the model can assign different attention weights to each location in the image, thereby focusing on key defect areas. The effect can be seen in [reference needed]. Figure 4 As shown, different defect types (such as pores and shrinkage cavities) are assigned different labels, indicating that the model achieves effective feature differentiation through the attention mechanism.

[0037] Furthermore, to achieve information interaction between windows while maintaining low computational cost, the sliding window mechanism employed in this embodiment includes a crucial step: cyclically shifting the window partitions before the next multi-head self-attention computation. This means that after a computation is completed, the windows on the feature map undergo a controlled, cyclical translation, allowing them to cross the boundaries of the previous step in new computation rounds, thus enabling features originally located in different windows to exchange information. This design cleverly realizes the global receptive field, which is key to the efficient extraction of global spatial features in this invention.

[0038] Furthermore, at the decoder end of the improved TransUNet model, in order to effectively combine the high-level semantic information and low-level detail information extracted by the encoder to generate a high-precision segmentation map, this embodiment further refines the step described in the second step above of fusing the extracted local detail features and global spatial features at the decoder end. Specifically, as follows: like Figure 3 As shown, in each upsampling path of the decoder, the feature map from the higher-level encoder, after upsampling (e.g., through transposed convolution or bilinear interpolation) and thus having improved spatial resolution, is merged with the feature map from the lower-level encoder, which is directly passed through skip connections (skip topology) at the corresponding coding level and retains rich spatial details. In this embodiment, this merging operation specifically involves feature concatenation along the channel dimension. Through this step, the model can effectively fuse deep semantic information containing global context with shallow detail information containing high resolution at different scales while restoring the image's spatial resolution. This ensures that the final output segmentation mask can accurately identify the macroscopic category of defects and precisely delineate their subtle boundaries.

[0039] Furthermore, to achieve simultaneous identification and differentiation of multiple types of die-casting defects, this embodiment configures an independent, parallel binary segmentation channel for each defect type to be identified through the multi-channel initialization mask module. At the model's output, for the preset types of die-casting defects, such as porosity, shrinkage cavities, and segregation commonly found in aluminum alloy die-castings for new energy vehicles, this module configures a dedicated segmentation channel for each defect type. This means that if four types of defects need to be detected, the model will output the segmentation results of four channels in parallel. Each binary segmentation channel is used to output a binary segmentation mask, which is the same size as the input image. A pixel with a value of 1 indicates that the model determines that the location belongs to the defect type corresponding to the current channel, while a value of 0 represents the background or other types, thus highlighting the defect area corresponding to that channel.

[0040] Meanwhile, to determine the specific defect type of each segmented region, this embodiment combines a classification head to perform final defect type prediction on the extracted features. At the end of the model decoding path, the fused deep features x are fed into a classification head module, which is typically composed of fully connected layers. The output prediction probability of this classification head is defined by the following formula:

[0041] Where y is a vector containing the predicted probabilities of various defects, and each element in the vector represents the confidence that the image (or a specific region) belongs to a certain defect type; x is the high-dimensional feature extracted by the encoder and fused by the decoder; W is the weight matrix of the classification head; and b is the bias vector. The function transforms the linear output of the classification head into a probability distribution such that the sum of the probabilities of all categories is 1.

[0042] Finally, to generate a clear and comprehensive detection result image, this method integrates the outputs of the two parallel tasks mentioned above. Specifically, the system combines the segmentation mask output by the segmentation channel with the predicted probability output by the classification head to generate a final segmentation image containing all defect regions and their corresponding defect types. For example, the system can first determine the main defect type (e.g., "pores") of a region based on the highest probability y output by the classification head, and then call the binary segmentation mask output by the "pores" channel, using a specific color (e.g., ...). Figure 4 The area is marked with a red box or fill color as shown. This process is repeated for all identified defect areas to ultimately obtain an image like... Figure 4 The visualization results shown clearly indicate the location, shape, and specific type of all defects, thus greatly facilitating subsequent quantitative analysis and manual verification.

[0043] In some embodiments, to achieve accurate and digital assessment of the internal quality of die-cast parts, this embodiment provides a more specific implementation of the calculation step for the regional porosity of the target area described in the fourth step above. Porosity is a key physical indicator for measuring the density of the internal structure of a material, defined as the proportion of the volume of pores within the material to its total volume. This embodiment cleverly applies this physical concept to the analysis of X-ray images, specifically through any of the following formulas: The first method is based on volume or area calculations. This method directly addresses the issue from a geometric measurement perspective, and its calculation formula is as follows:

[0044] In this formula, P represents the final calculated material porosity. (Parameter) A and B have dual interpretations to accommodate different measurement dimensions. In the three-dimensional physical world, The volume of a material in its natural state, usually expressed in mm. 3 A represents the absolutely dense volume of the material, that is, the volume of the pure material portion without any pores, also in mm. 3 When we apply this formula to two-dimensional X-ray image analysis... This then represents the total pixel area of ​​the target region (corresponding to a certain physical area, in units such as mm). 2 A represents the pixel area of ​​the non-defective portion within the target area (corresponding to a certain physical area, in units such as mm). 2 Under this correspondence, ( The difference between the values ​​of the two values ​​corresponds precisely to the sum of the areas of all defective pixels within the target region. In this way, the present invention transforms the macroscopic concept of physical porosity into a pixel area ratio that can be directly calculated on a digital image, thus achieving the quantification of porosity.

[0045] The second method is based on density calculation, which calculates from the perspective of material physical properties. The formula is as follows:

[0046] Similarly, P represents the porosity of the material. However, in this formula, the calculation is based on density. The physical density of the aluminum alloy die-casting matrix is ​​a known, preset value. This is an ideal value representing the density of a fully dense material, expressed in g / mm². 3 ; This refers to the apparent density of the target region, i.e., the average density of the region including the defect, in g / mm². 3 Due to the presence of defects (whose density can be approximated as 0), the apparent density... It must be less than the matrix density A key innovation of this invention lies in the apparent density. The apparent density is characterized by the proportion of defective pixels to the total number of pixels in the grid within the target area. Specifically, the system can establish a mapping relationship to convert the calculated proportion of defective pixels into the corresponding apparent density value through a preset calibration model or lookup table. In this way, the present invention directly links visual features on an image with the macroscopic physical properties (density) of a material, providing another effective way to calculate porosity.

[0047] Furthermore, to conduct a more detailed and intuitive analysis of the distribution of internal defects in the casting, the meshing analysis method described in this embodiment is also used to generate a visual map of porosity distribution. Specifically: like Figure 5As shown in Figure (b), the method of this embodiment first divides a target region (e.g., a critical stress node) containing multiple identified defect segmentation regions into a grid array composed of multiple sub-grids at the software level. These sub-grids can be rectangular cells of any size and number; for example, a 100x100 pixel region can be divided into 10x10 sub-grids, for a total of 100 sub-grids.

[0048] After the subgrid is divided, the system will independently calculate the porosity of each subgrid. This means that for each subgrid, the system will repeat the aforementioned porosity calculation steps, namely, counting the number of defective pixels in the subgrid, calculating its proportion of the total pixels in the subgrid, and finally converting it into the porosity value of the subgrid itself.

[0049] After obtaining the porosity values ​​for all sub-grids, the system generates a porosity distribution cloud map of the target area based on the porosity calculation results for each sub-grid. In this cloud map, the system assigns a specific color or grayscale value to each sub-grid according to its porosity. For example, areas with high porosity can be displayed in red (warm colors), and areas with low porosity in blue (cool colors), thus visually presenting the distribution density of defects at different locations within the target area. This porosity cloud map is extremely valuable to engineers, as it not only reveals the total amount of defects but also clearly shows areas where defects accumulate (i.e., potential stress concentration points and structural weak points), providing highly accurate and intuitive data support for optimizing the design of the gating and venting systems of die-casting molds, as well as adjusting injection parameters.

[0050] Furthermore, in order to obtain a detection model capable of accurately identifying various die-casting defects, this embodiment provides a more detailed implementation plan for the training steps of the pre-trained improved TransUNet model described in the second step above. The entire training process aims to minimize the difference between the prediction results and the true annotations by optimizing the model parameters. The specific implementation method is as follows: In this embodiment, the training process is performed on a computing device configured with 64GB of memory and a high-performance graphics processing unit (GPU). The programming environment uses Python 3.8, and the model is built and trained using the mainstream deep learning framework PyTorch. First, X-ray images containing die-casting defects and their corresponding labeled masks are used as the training dataset. These datasets have been carefully prepared before training, covering a variety of typical defects such as porosity, shrinkage porosity, shrinkage cavities, and segregation, to ensure the comprehensiveness of the model's learning.

[0051] In the core training phase, the Adam optimizer is used in this embodiment to efficiently guide the convergence direction of the model parameters. Simultaneously, to balance the model's fitting and generalization abilities, the initial learning rate is set to 0.01, and the weight decay is 0.0005. Training is performed in batches; in this embodiment, each batch contains 8 images, and a total of 200 epochs are conducted. The model state is saved every 10 epochs to facilitate subsequent selection of the optimal model.

[0052] To address the severe imbalance in the number of foreground (defect) and background pixels, as well as the significant differences in pixel ratios between different defect categories, in die-casting defect segmentation tasks, this embodiment employs a weighted combined loss function, consisting of cross-entropy loss and Dice loss, to optimize the model. Cross-entropy loss is stable in pixel-level classification tasks, while Dice loss is particularly effective in resolving class imbalance by maximizing the overlap between the predicted and ground truth regions. The formula for this combined loss function is as follows:

[0053] in, This is the total loss used for backpropagation; Cross-entropy loss; For the loss of Des; and These are preset weighting coefficients used to balance the importance of the two types of losses.

[0054] In this embodiment, the experimental results can be used to determine the appropriate experimental parameters. and All values ​​are set to 0.5, or dynamically adjusted based on the segmentation difficulty of different defects. (Refer to...) Figure 9 The training loss curve shown in Figure (a) shows that the total loss increases with the number of training epochs. The model shows a continuous downward trend and tends to converge, indicating that the combined loss function and optimization strategy adopted are effective and the model is learning in the right direction.

[0055] Furthermore, considering that the number of samples of different defect types in the training dataset may vary (e.g., more pore samples and fewer segregation samples), to avoid the model biasing towards learning the most frequent majority class defects during training while ignoring the features of rare defects, the training steps in this embodiment include: calculating the cross-entropy loss... Category balancing weights are introduced at this time. The specific implementation method is as follows: First, the frequency of each defect type in the training dataset is statistically analyzed. Then, a higher weight is assigned to defect types with lower frequencies in the training dataset, and vice versa. For example, inverse frequency or inverse square root frequency can be used to determine the weights. When the model calculates the loss, the loss of pixels belonging to that rare defect type is multiplied by its corresponding higher weight. In this way, the gradient signal from rare defect samples is amplified during backpropagation, thereby forcing the model to focus more on and learn the features of these rare defects during training.

[0056] The effectiveness of the above training strategy can be determined by... Figure 9 This has been verified. Please refer to [link / reference]. Figure 9 The figure shows the performance change of the model over 200 training iterations: (1) Figure 9 (a) shows the curves of the training loss. As can be seen from the figure, the training loss and validation loss decrease rapidly in the initial stage, then remain at a low level and eventually converge. This indicates that the combined loss function and optimization strategy are effective and the model is learning in the right direction.

[0057] (2) Figure 9 (b) shows the change curve of Top-1 accuracy (acc). Top-1 accuracy refers to the proportion of the class with the highest probability predicted by the model that matches the true class. As can be seen from the figure, both training accuracy (train Top-1) and validation accuracy (val Top-1) steadily increase and tend to stabilize with the increase of training rounds. This proves that the model's classification and recognition ability has been significantly enhanced through training, and that it has effectively learned the characteristics of various defects through strategies such as class balancing weights.

[0058] To clearly demonstrate the effectiveness of the various improved modules introduced in this invention (including multi-head attention optimization, multi-scale feature fusion, and loss weighting strategy), this embodiment also conducted a series of ablation experiments, the results of which are as follows: Figure 7 As shown in Table 1. Please refer to Table 1. Figure 7 Table 1 records the changes in the average pixel accuracy (MPA) of the model after gradually adding various improvement modules to the original TransUNet model: (1) The “native TransUNet” in sequence 1 is used as the benchmark, and its MPA is 84.3%.

[0059] (2) As shown in Serial No. 2, after adding "multi-head attention optimization", the MPA increased to 85.2%, proving that the module enhanced the feature extraction capability of the model.

[0060] (3) As shown in number 3, after adding "multi-scale feature fusion", MPA reached 84.8%, which also brought performance gains.

[0061] (4) As shown in number 4, after adding the "loss weighting strategy", the MPA increased to 85.0%, proving that the strategy effectively improved the model's learning effect on imbalanced data.

[0062] (5) The “complete improvement model” in number 5 integrates all optimization strategies, and its MPA finally reaches 86.1%.

[0063] The results of this ablation experiment strongly demonstrate that the various technical improvements adopted in this invention are not simply a matter of piling them up, but rather that they work together in a coordinated manner to ultimately achieve an effective improvement in the overall performance of the model.

[0064] In some embodiments, to improve the generalization ability and reliability of the pre-trained model, enabling it to accurately identify defects under various poses, lighting conditions, and noise levels, this embodiment performs a series of preprocessing steps on the acquired raw X-ray images before inputting the image to be detected into the pre-trained improved TransUNet model. One key step is data augmentation of the acquired raw X-ray images. The purpose of data augmentation is to expand the diversity of the training dataset by performing a series of random transformations on the limited original training samples, thereby simulating various image variations that may occur in real production environments. This effectively prevents the model from overfitting during training and makes it more adaptable to previously unseen image data.

[0065] In this embodiment, the data augmentation processing operation further includes at least one of the following operations: (1) Rotate the image at random angles: For example, rotate the image at random angles within the range of -30° to +30°. This operation enables the model to learn defect features that are independent of orientation.

[0066] (2) Random horizontal or vertical flip: The image is flipped horizontally or vertically with a 50% probability. This operation helps the model understand the symmetry of the defect.

[0067] (3) Random scaling: For example, the image can be randomly scaled in the range of 0.8x to 1.2x. This operation allows the model to adapt to defects of different sizes.

[0068] (4) Add Gaussian noise to the image: Add random noise that follows a Gaussian distribution to the image. This operation can simulate the electronic noise that may be introduced during X-ray imaging, thereby improving the model's detection performance on low-quality images.

[0069] By analyzing images in the training set, such as Figure 2 By applying one or more of the above enhancement operations to the various labeled defect samples shown, a large number of virtual defect images with different perspectives and shapes can be generated, which greatly enriches the learning materials of the model and thus significantly improves the detection accuracy and generalization ability of the model.

[0070] In some embodiments, as another fundamental and important step in image preprocessing, this embodiment provides a more specific implementation of the operation step described in the first step above, which involves size normalization processing of the original X-ray image of the aluminum alloy die-casting part of the new energy vehicle under test, as follows: In actual detection processes, raw X-ray images acquired from different devices or under different shooting conditions may vary significantly in resolution and size. Directly inputting these images of varying sizes into a neural network model could lead to errors due to size mismatch in the model's input layer, or affect the consistency of feature extraction due to size differences. Therefore, in this embodiment, this step is specifically set as follows: raw X-ray images of various resolutions are uniformly adjusted to a fixed size of 640×640 pixels using image resampling algorithms such as bilinear interpolation, to serve as the standardized input to the improved TransUNet model.

[0071] Choosing a 640×640 pixel size was a trade-off made after comprehensively considering computational resource consumption and the degree of defect detail preservation. This size retains enough pixel information to identify minor defects without overburdening the model's training and inference processes due to excessive size, thus ensuring the efficiency and accuracy of the entire detection process. This standardized input is the foundation for ensuring that the model can work stably and reliably on images from different sources.

[0072] More specifically, regarding the steps for pre-training the improved TransUNet model, the clearer model training process in this embodiment is as follows: Figure 6 As shown, the entire training process is executed within one training epoch. First, the model is set to training mode (model.train), and a variable (running_loss) is initialized to accumulate the training loss for the current epoch. Then, the training dataset (train_loader) that has been divided into multiple batches is traversed.

[0073] For each batch in the dataset, perform the following loop operation: (a) Obtain the image data (inputs) and corresponding real defect labeling masks (labels) for the current batch.

[0074] (b) To take advantage of hardware acceleration, image data and labels are deployed to a graphics processing unit (GPU).

[0075] (c) Before calculating new gradients, the optimizer’s historical gradients are cleared to zero (optimizer.zero_grad()) to avoid gradient accumulation.

[0076] (d) Input the image data into the improved TransUNet model for forward propagation to obtain the model's predicted outputs of defects.

[0077] (e) Using the aforementioned combined loss function (criterion), calculate the loss value between the model's predicted output and the true label.

[0078] (f) Perform backpropagation (loss.backward()) based on the loss value to calculate the gradient of each parameter in the model.

[0079] (g) The optimizer updates the network parameters of the model once based on the calculated gradient (optimizer.step()).

[0080] (h) The loss value of the current batch is accumulated into the running_loss variable for subsequent calculation of the average loss.

[0081] After traversing all batches of data, the current training epoch ends. At this point, the average loss (epoch_loss) for that epoch is calculated by dividing the cumulative total loss by the number of batches in the training set, and this loss can be recorded to monitor the training status. Repeat this process for multiple training epochs until the model converges.

[0082] Example Effect Verification To further verify the advanced nature and practical application effect of the method disclosed in this invention, this embodiment will also include the method of this invention (in... Figure 8 The performance of TransUNet (referred to as "Improved TransUNet") was compared with several mainstream segmentation models in the current field, including U-Net, SegNet, DeepLabV3+, and the original TransUNet, on the same test dataset.

[0083] The quantitative performance index comparison results are as follows: Figure 8 As shown in Table 2. Please refer to Table 2. Figure 8 As shown, the table clearly lists the performance of each model on three core evaluation metrics: average pixel accuracy (MPA), average intersection-over-union (MIoU), and single-frame inference time. The following conclusions can be drawn from the table data: (1) In terms of detection accuracy, the "improved TransUNet" model of the present invention achieved the highest values ​​in both MPA and MIoU, which were 86.1% and 85.2% respectively, surpassing all the comparison models such as U-Net, SegNet, DeepLabV3+ and native TransUNet, demonstrating the significant superiority of the method of the present invention in recognition accuracy.

[0084] (2) In terms of detection efficiency, the single-frame inference time of the method of the present invention is 86ms, which is basically the same as that of the native TransUNet (88ms), far superior to DeepLabV3+ (95ms), and on the same order of magnitude as the lightweight U-Net (72ms) and SegNet (68ms).

[0085] In summary, Figure 8 The quantitative data shown fully demonstrates that the detection method proposed in this invention, while significantly improving detection accuracy, still maintains high detection efficiency, enabling rapid and accurate detection of die-cast parts on industrial production lines, and has significant technical advantages and practical value.

[0086] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for X-ray inspection of die-cast defects in aluminum alloys for new energy vehicles based on the TransUNet model, characterized in that, Includes the following steps: The raw X-ray image of the aluminum alloy die-cast part of the new energy vehicle to be tested is acquired, and the size of the raw X-ray image of the aluminum alloy die-cast part of the new energy vehicle to be tested is normalized to obtain the image to be tested; The image to be detected is input into a pre-trained improved TransUNet model for feature extraction. The improved TransUNet model adopts a two-layer encoder structure, which uses the continuous convolutional layers in the bottom encoder to extract local detail features of the image to be detected, and uses the sliding window multi-head self-attention module in the high-level encoder to extract global spatial features of the image to be detected. At the decoder end of the improved TransUNet model, the extracted local detail features and global spatial features are fused, and an independent mask channel is assigned to each preset die casting defect type through a multi-channel initialization mask module, outputting the segmented regions of various die casting defects in the image to be detected and the corresponding defect types. Based on the output segmented regions and the mapping relationship between image pixels and actual physical dimensions, the defect area of ​​each segmented region is calculated. Simultaneously, a meshing analysis method is used to mesh the target region containing the segmented regions, and the proportion of defect pixels in the target region to the total number of pixels in the mesh is calculated. Combined with the physical density parameters of the aluminum alloy die casting, the regional porosity of the target region is calculated.

2. The X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model according to claim 1, characterized in that, Feature extraction is achieved through the dual-layer encoder structure, which includes: A low-level encoder, which uses a convolutional neural network encoding path, extracts detailed features containing local contextual information during the process of downsampling the image to be detected. A high-level encoder receives the minimum-size feature map output by the low-level encoder, flattens the feature map, and converts it into an image sequence; the high-level encoder extracts global spatial features using the sliding window multi-head self-attention module by performing the following steps: First, the input query matrix Q, key matrix K, and value matrix V are split using a multi-head splitting method to obtain h independent head vectors. The splitting method is implemented by the following formula: Where i is the attention head number and h is the total number of multi-head attention heads; q, k, and v are the original input query matrix, key matrix, and value matrix, respectively; , , The following are, in order, the query projection weight matrix, the key projection weight matrix, and the value projection weight matrix corresponding to the i-th attention head; , , , , and , respectively, are the single-head query matrix, single-head key matrix, and single-head value matrix obtained after projection of the i-th head weight; Then, attention calculation is performed to obtain weights by calculating the similarity of each set of Q, K, and V vectors. The attention calculation formula is as follows: Where Q is the query matrix, corresponding to the defect features to be retrieved; K is the key matrix, and V is the value matrix; This is the transpose of the key matrix; d is the dimension of the key vector; softmax is a normalization exponential function used to convert the calculation results into a probability distribution; The output is the fused feature after attention weighting; Furthermore, before the next multi-head self-attention calculation, the window partition is cyclically shifted to enable information exchange between adjacent windows.

3. The X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model according to claim 2, characterized in that, The step of fusing the extracted local detail features and global spatial features at the decoder end of the improved TransUNet model is specifically implemented in the following way: In each upsampling path of the decoder, the feature map after upsampling from the higher-level encoder is concatenated with the feature map from the lower-level encoder through skip connections at the corresponding coding level, in the channel dimension.

4. The X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model according to claim 2, characterized in that, The multi-channel initialization mask module configures an independent, parallel binarization segmentation channel for each defect type to be identified. Each binarization segmentation channel is used to output a binarization segmentation mask to highlight the defect area corresponding to that channel. The extracted features are then combined with a classification head to perform a final defect type prediction. The prediction probability of the classification head is defined by the formula: Where y is the predicted probability of various defects, x is the feature extracted by the encoder, W is the weight matrix, and b is the bias vector. By combining the segmentation mask output by the segmentation channel with the prediction probability output by the classification head, a final segmentation map containing all defect regions and their corresponding defect types is generated.

5. The X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model according to claim 1, characterized in that, The calculation of the regional porosity of the target region is specifically achieved through any of the following formulas: The calculation method based on volume or area uses the following formula: Where P is the porosity of the material; The volume of the material in its natural state, in mm. 3 ,or The total pixel area of ​​the target region, in mm. 2 ; A represents the absolute dense volume of the material, in mm. 3 Or, A represents the pixel area of ​​the non-defective portion within the target area, in mm. 2 ; or, The formula for density-based calculations is as follows: Where P is the porosity of the material; The preset physical density of the aluminum alloy die-casting matrix, in g / mm³. 3 ; The apparent density of the target region is expressed in g / mm². 3 Furthermore, the apparent density ρ is characterized by the proportion of defective pixels to the total number of pixels in the grid within the target area.

6. The X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model according to claim 1, characterized in that, The gridded analysis method is also used for: The target region containing the segmented region is divided into a grid array consisting of multiple sub-grids, and the porosity within each sub-grid is calculated independently; Based on the porosity calculation results of each subgrid, a porosity distribution cloud map of the target area is generated, thereby visually presenting the distribution density of defects at different locations within the target area.

7. The X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model according to claim 1, characterized in that, The training steps for the pre-trained improved TransUNet model include: X-ray images containing die-casting defects and their corresponding labeled masks were used as the training dataset. The model was optimized using a combined loss function weighted by cross-entropy loss and Dessian loss. The formula for this combined loss function is as follows: in, Total loss; Cross-entropy loss; For the loss of Des; and These are the preset weighting coefficients.

8. The X-ray detection method for defects in aluminum alloy die casting of new energy vehicles based on the TransUNet model according to claim 7, characterized in that, The training steps for the improved TransUNet model also include: In calculating the cross-entropy loss The class balancing weight is introduced, which involves assigning a higher weight value to defect types that occur less frequently in the training dataset. When calculating the loss, the loss of the pixel belonging to the rare defect type is multiplied by its corresponding higher weight value, so as to guide the model to pay more attention to and learn the features of these rare defects during the training process.

9. The X-ray detection method for die-casting defects in aluminum alloys for new energy vehicles based on the TransUNet model according to claim 1, characterized in that: Before inputting the image to be detected into the pre-trained improved TransUNet model, the data augmentation process is also performed on the acquired raw X-ray image to expand the diversity of the training dataset. Furthermore, the data augmentation processing operation also includes at least one of the following operations: The image can be rotated at random angles, flipped randomly horizontally or vertically, scaled at random ratios, and Gaussian noise can be added to the image.

10. The X-ray detection method for die-casting defects in aluminum alloys for new energy vehicles based on the TransUNet model according to claim 1, characterized in that: The specific steps for normalizing the dimensions of the raw X-ray images of the aluminum alloy die-cast parts for new energy vehicles are as follows: The original X-ray images of various resolutions were uniformly adjusted to a fixed size of 640x640 pixels to serve as the standardized input for the improved TransUNet model.