Transform interpretation method based on sequential three-branch mask and attention fusion
Through the sequential three-mask and attention fusion method, the Transformer model's poor interpretability and time-consuming data labeling problems in medical image analysis are solved, and the accuracy and trust of the model in medical image analysis are improved.
Patent Information
- Application Number
- CN202411133754.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-19
- Publication Date
- 2024-12-17
AI Technical Summary
The existing Transformer model has poor interpretability in medical imaging analysis, which makes it difficult for doctors to understand the model decision-making process, and the data labeling required for training takes time, resulting in waste of medical resources.
Using the Transformer interpretation method based on sequential three-branch masks and attention fusion, the mask types are divided and weighted fusion is generated by saving each layer's attention matrix, generating cross-layer relationship matrix, Hadamma product and KL divergence, etc. Visualize the interpretation of the results.
It improves the interpretability of the model, reduces the time for doctors to analyze MRI images, improves the accuracy of judgment, and enhances the trust in deep learning models.
Smart Images

Figure CN119152253A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of deep learning model interpretation methods, and in particular to a Transformer interpretation method based on sequential three-branch masking and attention fusion. Background Art
[0002] Brain tumors are abnormal proliferation of brain cells caused by cancerous changes in brain tissue. This pathological change poses a high risk to the patient's health and may cause headaches, cognitive impairment, motor impairment and other neurological symptoms. In severe cases, brain tumors may even be life-threatening. Therefore, accurately identifying the specific location and morphology of brain tumors is crucial in clinical diagnosis and treatment. Magnetic resonance imaging (MRI) is widely used in the diagnosis and treatment of brain tumors because it provides high-resolution images of soft tissue anatomical structures. By analyzing MRI images, doctors can effectively detect brain tumors and obtain important information about the location, volume and shape of the tumor. In recent years, the development of deep learning technology has provided new opportunities for the detection of brain tumors. Applying deep learning models to analyze medical images can not only significantly improve the accuracy of detection, but also improve the efficiency of detection, thereby improving the early detection and diagnosis process of brain tumors.
[0003] For example, Lin et al. proposed a hybrid Transformer model with modality-correlated cross-attention for brain tumor segmentation in CKD-TransBTS: Clinical Knowledge-Driven Hybrid Transformer With Modality-Correlated Cross-Attention for Brain Tumor Segmentation. However, since the Transformer model usually has more than 10 million parameters and its self-attention mechanism works in a complex way, it is usually regarded as a black box model, which makes the model unable to gain full trust from users and difficult to use in high-risk industries such as medical care. At the same time, the collection and annotation of the data set required for training deep learning models requires professional radiologists to spend a lot of time manually annotating, resulting in a large waste of medical resources. A new method is urgently needed to visualize the internal structure and learning process of the model to help humans understand the decision-making process of the model, while assisting radiologists in making medical judgments, reducing the consumption of expensive medical resources in other fields, and having certain significance for early screening of brain tumors. Summary of the invention
[0004] The purpose of the present invention is to provide a Transformer interpretation method based on sequential three-branch masking and attention fusion, which solves the problems of poor interpretability of existing Transformer and its derivative models, and long time consumption for radiologists to analyze MRI images, and effectively improves the accuracy of judgment.
[0005] In order to achieve the above-mentioned invention object, the present invention is implemented by the following measures: a Transformer interpretation method based on sequential three-branch masking and attention fusion comprises the following steps:
[0006] S1: Read image I from the dataset, reshape it to the preset size of 224×224, then cut it into 196 non-overlapping image blocks of size 16×16, flatten these image blocks and linearly map them to 768 dimensions, then concatenate the classification tensor, add the position information encoding to get the input tensor X0, input X0 into the Transformer encoder, repeat L rounds, and the output result of the i-th round is X i , the output of the last layer is X L ;
[0007] S2: During the operation of the Transformer encoder layer, save the attention matrix A of each Transformer encoder layer j , j = 1, 2, ..., L, concatenate all the saved attention matrices and discard the category tensor Where L is the number of Transformer encoder layers, H is the number of self-attention heads in each encoder layer, and N is the number of image blocks cut into. First, layer aggregation is performed on the dimensions of the Transformer encoder layer to obtain the cross-layer relationship matrix r, that is, in Then, the head is aggregated from the dimension of the head of the self-attention layer to obtain the final relationship matrix R, that is, in
[0008] S3: Take the output of the final encoder layer of the Transformer Discard the classified tensor and reshape it into a grid shape to get the reshaped tensor Then upsample it to the preset image size to get the mask set
[0009] S4: Set the mask set M set Put in a sequential three-branch mask module, in the first layer three-branch decision, for mask M i ∈M set , the mask M i and flip mask 1-M iAfter Hadamard product with the original input image I, the corresponding confidence is obtained after being put into the Transformer model f(·). After comparing with the set thresholds α, β and γ, the mask is divided into positive mask, negative mask and uncertain mask, and they are put into the positive domain POS, negative domain NEG and boundary domain BND respectively. In the second layer, the three-branch decision is made. For the mask M in the boundary domain i ∈BND, using the KL divergence information between the original image and the mask, KL(f(I)||f(M i ⊙I)), where KL(·||·) represents the KL divergence function. After comparing with the set threshold δ, the uncertain mask is divided into positive mask and negative mask again, and they are put into the positive domain POS and the negative domain NEG respectively. Finally, the positive mask M in the positive domain is POS The weighted sum of the confidence scores obtained after the corresponding Transformer is divided by the expected value of the mask set to obtain the preliminary explanation result S, and the preliminary explanation result is normalized;
[0010] S5: Downsample to Then flatten it to 1×N size and take out the aggregated Use in sequence i=1,2,...,N and S perform cosine similarity calculation, and concatenate all cosine similarity calculation results to get importance score Then reshape P into Then upsample to the size of H×W, and finally perform Hadamard product on P and S to get the final interpretation result V. Overlay it on the original image through heat map mapping to get the final visual interpretation result.
[0011] Furthermore, the specific steps of step S2 are as follows:
[0012] Step S2.1: Input tensor X0 to the Transformer encoder for L rounds, then the output of the jth round is X j In each cycle, the input is first normalized by LN(X j ):
[0013]
[0014] Step S2.2: Normalize X j Input the multi-head self-attention module. Assume that the module has H attention heads and obtain the attention matrix A of the j-th Transformer encoder layer. j , the extraction process is as follows:
[0015]
[0016]
[0017] A j =Concat(head1,..,head H )W O (4)
[0018] Where Q, K, and V are respectively j The query, key and value matrices obtained by linear projection transformation are d k is the dimension of the key matrix, softmax is the activation function, Represents the weight matrices of the query, key, and value of the i-th self-attention head, Concat represents the concatenation operation, and W O is the projection transformation matrix;
[0019] Step S2.3: The attention matrix A in each cycle j Save, a total of L rounds, concatenate them all and discard the category tensor to get the final attention matrix Considering the meaning of semantic information in different Transformer encoder layers, we first aggregate them from the layer dimension. The aggregation formula is as follows:
[0020]
[0021]
[0022] Where r is the cross-layer relationship matrix, and E(A) is the mean of the attention matrices of different layers;
[0023] Step S2.4: Different attention heads of Transformer focus on different image features. Considering the semantic information in different attention heads, aggregation is performed from the dimension of the head. The aggregation formula is as follows:
[0024]
[0025]
[0026] Where R is the final relationship matrix, E(r) is the mean of the relationship matrices of different heads, and the relationship matrix is used for subsequent optimization of the preliminary interpretation results.
[0027] Furthermore, the specific steps of step S4 are as follows:
[0028] Step S4.1: Take out the mask set M set Put into the sequential three-branch mask module, first put all the masks M in the mask seti and its corresponding flip mask 1-M i Perform Hadamard product with the original input image I to cover the mask on the original image, and then put it into the Transformer model f(·) to obtain the corresponding confidence f(M i ⊙I) and f((1-M i )⊙I);
[0029] Step S4.2: For the first layer of three decisions, set the thresholds α, β and γ to make decisions on the mask. If the confidence f(M i ⊙I)>αβ<f((1-M i )⊙I)<α, indicating that the mask can provide object position information. The mask is placed in the positive domain POS. If the confidence f(M i ⊙I)<γor f((1-M i )⊙I)<γ, indicating that the mask does not contain valid information, the mask is placed in the negative domain NEG, and finally all the remaining masks are placed in the boundary domain BND to wait for the delay decision;
[0030] Step S4.3: Then enter the second layer of three-branch decision, and use KL divergence to make a secondary decision on the mask entering BND. KL divergence is used to measure the difference between two probability distributions, and its calculation method is as follows:
[0031]
[0032] Where P is the target distribution, Q i To approximate the distribution, we define the probability distribution f(I) obtained after the original image I is put into the model as the target distribution, and the mask M i The probability distribution f(M i ⊙I) is the approximate distribution. By using formula (9), we can calculate the KL divergence value KL(f(I)||f(M i ⊙I)), then set the threshold δ to make a decision on the mask. If KL(f(I)||f(M i ⊙I))<δor KL(f(I)||f(M i ⊙I))>1-δ, indicating that the mask is meaningful, the mask is put into POS, otherwise the mask is put into NEG;
[0033] Step S4.4: Take out all the masks in POS and record them as the active mask set M POS , the importance value of each pixel x in the input image I is calculated by applying the mask M i The output f of the corresponding category c after it is put into the model c (M i⊙I) weighted sum and divided by the expected value of the mask set E(M POS ) to obtain:
[0034]
[0035] where f c (·) represents a model specific to category c, p is the number of masks in the active mask set;
[0036] Step S4.5: Finally, the preliminary interpretation results are normalized:
[0037]
[0038] Furthermore, the specific steps of step S5 are as follows:
[0039] Step S5.1: The preliminary interpretation result S has spatial noise and edge detail information loss problems. The relationship matrix R generated by step S2 is used to optimize the preliminary interpretation result. First, the preliminary interpretation is downsampled to Then flattened to 1×N size:
[0040] S=Flatten(Downsample(S)) (12)
[0041] Where Downsample represents the downsampling operation, and Flatten represents the flattening operation;
[0042] Step S5.2: For the relationship matrix R, each row of it can reflect the relationship between the current image block and other image blocks in the image. Therefore, the cosine similarity of each row of R and S is calculated as the importance score of the current image block. The importance score P of the i-th image block is i Calculated by the following formula:
[0043]
[0044] Where · represents the vector dot product, ||·|| represents the Euclidean norm of the vector, and N is the number of image blocks;
[0045] Step S5.3: Calculate all image blocks using formula (13) to obtain the importance scores of all image blocks. Reshape it into And upsample to the original image size:
[0046] P = Upsample(Reshape(P)) (14)
[0047] Reshape represents the reshaping operation, and Upsample represents the upsampling operation;
[0048] Step S5.4: Use the importance score P to perform element-by-element weighting on the preliminary interpretation result S using the Hadamard product to highlight important areas and eliminate erroneous spatial noise to obtain the final interpretation result V:
[0049] V(x)=P(x)⊙S(x) (15)
[0050] Where ⊙ represents the Hadamard product operation;
[0051] Step S5.5: Finally, the final explanation result V is overlaid on the original image I for visualization in the form of a heat map. The darker the color of the heat map, the greater the contribution of the current image area to the model prediction, which effectively shows the key focus area when the Transformer model works and reflects the model interpretability.
[0052] Compared with the prior art, the present invention has the following beneficial effects:
[0053] 1. The present invention proposes a sequential three-branch mask model to solve the problem of uncertainty in mask quality generated by the feature map of the final layer of Transformer. By setting specific thresholds and conditions, the mask can be effectively divided into three types: positive mask, negative mask and uncertain mask. Using positive mask to perturb the model can obtain better explanation results, thereby effectively improving the model interpretability.
[0054] 2. The present invention analyzes the self-attention mechanism unique to the Transformer model, and generates a relationship matrix that can reflect the relationship between image blocks by aggregating the attention matrix of each layer of encoder in the Transformer. The importance scores of the image blocks are obtained through cosine similarity measurement, and the interpretation results are weighted and fused. This process effectively solves the noise problem existing in the preliminary interpretation results, thereby improving the model interpretation effect.
[0055] 3. The present invention combines the output information of the Transformer model with the attention information, and can provide effective interpretation results for both natural images and medical images, solving the problem that the Transformer model is difficult to interpret in practical applications, making the application of deep learning models more interpretable, thereby improving the trust and application prospects of the model in practical scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention but do not constitute a limitation of the present invention.
[0057] Figure 1 The overall framework diagram of the Transformer interpretation method based on sequential three-branch masking and attention fusion of the present invention;
[0058] Figure 2 It is a flow chart of the Transformer interpretation method based on sequential three-branch masking and attention fusion of the present invention;
[0059] Figure 3 This is an example diagram of the Transformer interpretation method based on sequential three-branch masking and attention fusion of the present invention. DETAILED DESCRIPTION
[0060] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. Of course, the specific embodiments described here are only used to explain the present invention and are not used to limit the present invention.
[0061] Example 1
[0062] See also Figures 1 to 3 The technical solution provided in this embodiment is a Transformer interpretation method based on sequential three-branch mask and attention fusion. Taking a picture of a natural image dataset as an example, from loading data to obtaining a visual interpretation result, the following steps are included:
[0063] S1: Read image I from the dataset, reshape it to the preset size of 224×224, then cut it into N non-overlapping image blocks of size Y×Y, flatten these image blocks and perform linear mapping to 768 dimensions, then concatenate the classification tensor, add the position information encoding to get the input tensor X0, input X0 into the Transformer encoder, repeat L rounds, and the output result of the i-th round is X i , the output of the last layer is X L ;
[0064]
[0065] S2: During the operation of the Transformer encoder layer, save the attention matrix A of each Transformer encoder layer j , j = 1, 2, ..., L, concatenate all the saved attention matrices and discard the category tensor Where L is the number of Transformer encoder layers, H is the number of self-attention heads in each encoder layer, and N is the number of image blocks cut into. First, layer aggregation is performed on the dimensions of the Transformer encoder layer to obtain the cross-layer relationship matrix r, that is, in Then, the head is aggregated from the dimension of the head of the self-attention layer to obtain the final relationship matrix R, that is, in
[0066] S3: Take the output of the final encoder layer of the Transformer Discard the classified tensor and reshape it into a grid shape to get the reshaped tensor Then upsample it to the preset image size to get the mask set
[0067]
[0068] S4: Set the mask set M set Put in a sequential three-branch mask module, in the first layer three-branch decision, for mask M i ∈M set , the mask M i and flip mask 1-M i After Hadamard product with the original input image I, the corresponding confidence is obtained after being put into the Transformer model f(·). After comparing with the set thresholds α, β and γ, the mask is divided into positive mask, negative mask and uncertain mask, and they are put into the positive domain POS, negative domain NEG and boundary domain BND respectively. In the second layer, the three-branch decision is made. For the mask M in the boundary domain i ∈BND, using the KL divergence information between the original image and the mask, KL(f(I)||f(M i ⊙I)), where KL(·||·) represents the KL divergence function. After comparing with the set threshold δ, the uncertain mask is divided into positive mask and negative mask again, and they are put into the positive domain POS and the negative domain NEG respectively. Finally, the positive mask M in the positive domain is POS The weighted sum of the confidence scores obtained after the corresponding Transformer is divided by the expected value of the mask set to obtain the preliminary explanation result S, and the preliminary explanation result is normalized;
[0069] S5: Downsample to Then flatten it to 1×N size and take out the aggregated Use in sequence i=1,2,...,N and S perform cosine similarity calculation, and concatenate all cosine similarity calculation results to get importance score Then reshape P into Then upsample to the size of H×W, and finally perform Hadamard product on P and S to get the final interpretation result V. Overlay it on the original image through heat map mapping to get the final visual interpretation result.
[0070] Specifically, the specific steps of step S2 are as follows:
[0071] Step S2.1: Input tensor X0 to the Transformer encoder for L rounds, then the output of the jth round is X j In each cycle, the input is first normalized by LN(X j ):
[0072]
[0073]
[0074] Step S2.2: Normalize X j Input the multi-head self-attention module. Assume that the module has H attention heads and obtain the attention matrix A of the j-th Transformer encoder layer. j , the extraction process is as follows:
[0075]
[0076]
[0077] A j =Concat(head1,..,head H )W O (twenty two)
[0078] Where Q, K, and V are respectively j The query, key and value matrices obtained by linear projection transformation are d k is the dimension of the key matrix, softmax is the activation function, Represents the weight matrices of the query, key, and value of the i-th self-attention head, Concat represents the concatenation operation, and W O is the projection transformation matrix, A j As shown in formula (23):
[0079]
[0080] Step S2.3: The attention matrix A in each cycle j Save, a total of L rounds, concatenate them all and discard the category tensor to get the final attention matrix Considering the meaning of semantic information in different Transformer encoder layers, we first aggregate them from the layer dimension. The aggregation formula is as follows:
[0081]
[0082]
[0083] Where r is the cross-layer relationship matrix, E(A) is the mean of the attention matrices of different layers, and r is shown in formula (26):
[0084]
[0085] Step S2.4: Different attention heads of Transformer focus on different image features. Considering the semantic information in different attention heads, aggregation is performed from the dimension of the head. The aggregation formula is as follows:
[0086]
[0087]
[0088] Where R is the final relationship matrix, E(r) is the mean of the relationship matrices of different heads, and the relationship matrix is used for subsequent optimization of the preliminary interpretation results. R is shown in formula (29):
[0089]
[0090] Specifically, the specific steps of step S4 are as follows:
[0091] Step S4.1: Take out the mask set M set Put into the sequential three-branch mask module, first put all the masks M in the mask set i and its corresponding flip mask 1-M i Perform Hadamard product with the original input image I to cover the mask on the original image, and then put it into the Transformer model f(·) to obtain the corresponding confidence f(M i ⊙I) and f((1-M i )⊙I);
[0092] Step S4.2: For the first layer of three decisions, set the thresholds α, β and γ to make decisions on the mask. If the confidence f(M i ⊙I)>αβ<f((1-M i )⊙I)<α, indicating that the mask can provide object position information. The mask is placed in the positive domain POS. If the confidence f(M i ⊙I)<γor f((1-M i )⊙I)<γ, indicating that the mask does not contain valid information, the mask is placed in the negative domain NEG, and finally all the remaining masks are placed in the boundary domain BND to wait for the delay decision;
[0093] Step S4.3: Then enter the second layer of three-branch decision, and use KL divergence to make a secondary decision on the mask entering BND. KL divergence is used to measure the difference between two probability distributions, and its calculation method is as follows:
[0094]
[0095] Where P is the target distribution, Q i To approximate the distribution, we define the probability distribution f(I) obtained after the original image I is put into the model as the target distribution, and the mask M i The probability distribution f(M i ⊙I) is an approximate distribution. By using formula (30), the KL divergence value KL(f(I)||f(M i ⊙I)), then set the threshold δ to make a decision on the mask. If KL(f(I)||f(M i ⊙I))<δor KL(f(I)||f(M i ⊙I))>1-δ, indicating that the mask is meaningful, the mask is put into POS, otherwise the mask is put into NEG;
[0096] Step S4.4: Take out all the masks in POS and record them as the active mask set M POS , the importance value of each pixel x in the input image I is calculated by applying the mask M i The output f of the corresponding category c after it is put into the model c (M i ⊙I) weighted sum and divided by the expected value of the mask set E(M POS ) to obtain:
[0097]
[0098] where f c (·) represents a model specific to category c, p is the number of masks in the active mask set;
[0099] Step S4.5: Finally, the preliminary interpretation results are normalized:
[0100]
[0101]
[0102] Specifically, the specific steps of step S5 are as follows:
[0103] Step S5.1: The preliminary interpretation result S has spatial noise and edge detail information loss problems. The relationship matrix R generated by step S2 is used to optimize the preliminary interpretation result. First, the preliminary interpretation is downsampled to Then flattened to 1×N size:
[0104] S=Flatten(Downsample(S)) (34)
[0105] Where Downsample represents the downsampling operation, Flatten represents the flattening operation, and S is shown in formula (35):
[0106]
[0107] Step S5.2: For the relationship matrix R, each row of it can reflect the relationship between the current image block and other image blocks in the image. Therefore, the cosine similarity of each row of R and S is calculated as the importance score of the current image block. The importance score P of the i-th image block is i Calculated by the following formula:
[0108]
[0109] Where · represents the vector dot product, ||·|| represents the Euclidean norm of the vector, and N is the number of image blocks;
[0110] Step S5.3: Use formula (36) to calculate the importance scores of all image blocks. Reshape it into And upsample to the original image size:
[0111] P = Upsample(Reshape(P)) (37)
[0112] Where Reshape represents the reshaping operation, Upsample represents the upsampling operation, and P is as shown in formula (38):
[0113]
[0114] Step S5.4: Use the importance score P to perform element-by-element weighting on the preliminary interpretation result S using the Hadamard product to highlight important areas and eliminate erroneous spatial noise to obtain the final interpretation result V:
[0115] V(x)=P(x)⊙S(x) (39) where ⊙ represents the Hadamard product operation, and V(x) is as shown in formula (40):
[0116]
[0117] As shown in formula (41), the interpretation results obtained by the traditional Transformer interpretation method in natural image datasets have spatial noise, that is, extremely high values also appear in the background area:
[0118]
[0119] Step S5.5: Finally, the final explanation result V is overlaid on the original image I for visualization in the form of a heat map. The darker the color of the heat map, the greater the contribution of the current image area to the model prediction, which effectively shows the key focus area when the Transformer model works and reflects the model interpretability.
[0120] Example 2
[0121] See also Figures 1 to 3 The technical solution provided in this embodiment is a Transformer interpretation method based on sequential three-branch mask and attention fusion. Taking a picture of a medical image dataset as an example, from loading data to obtaining a visual interpretation result, the following steps are included:
[0122] S1: Read image I from the dataset, reshape it to the preset size of 224×224, then cut it into N non-overlapping image blocks of size Y×Y, flatten these image blocks and perform linear mapping to 768 dimensions, then concatenate the classification tensor, add the position information encoding to get the input tensor X0, input X0 into the Transformer encoder, repeat L rounds, and the output result of the i-th round is X i , the output of the last layer is X L ;
[0123]
[0124] S2: During the operation of the Transformer encoder layer, save the attention matrix A of each Transformer encoder layer j , j = 1, 2, ..., L, concatenate all the saved attention matrices and discard the category tensor Where L is the number of Transformer encoder layers, H is the number of self-attention heads in each encoder layer, and N is the number of image blocks cut into. First, layer aggregation is performed on the dimensions of the Transformer encoder layer to obtain the cross-layer relationship matrix r, that is, in Then, the head is aggregated from the dimension of the head of the self-attention layer to obtain the final relationship matrix R, that is, in
[0125] S3: Take the output of the final encoder layer of the Transformer Discard the classified tensor and reshape it into a grid shape to get the reshaped tensor Then upsample it to the preset image size to get the mask set
[0126]
[0127] S4: Set the mask set M set Put in a sequential three-branch mask module, in the first layer three-branch decision, for mask M i ∈M set , the mask M i and flip mask 1-M i After Hadamard product with the original input image I, the corresponding confidence is obtained after being put into the Transformer model f(·). After comparing with the set thresholds α, β and γ, the mask is divided into positive mask, negative mask and uncertain mask, and they are put into the positive domain POS, negative domain NEG and boundary domain BND respectively. In the second layer, the three-branch decision is made. For the mask M in the boundary domain i ∈BND, using the KL divergence information between the original image and the mask, KL(f(I)||f(M i ⊙I)), where KL(·||·) represents the KL divergence function. After comparing with the set threshold δ, the uncertain mask is divided into positive mask and negative mask again, and they are put into the positive domain POS and the negative domain NEG respectively. Finally, the positive mask M in the positive domain is POS The weighted sum of the confidence scores obtained after the corresponding Transformer is divided by the expected value of the mask set to obtain the preliminary explanation result S, and the preliminary explanation result is normalized;
[0128] S5: Downsample to Then flatten it to 1×N size and take out the aggregated Use in sequence i=1,2,...,N and S perform cosine similarity calculation, and concatenate all cosine similarity calculation results to get importance score Then reshape P into Then upsample to the size of H×W, and finally perform Hadamard product on P and S to get the final interpretation result V. Overlay it on the original image through heat map mapping to get the final visual interpretation result.
[0129] Specifically, the specific steps of step S2 are as follows:
[0130] Step S2.1: Input tensor X0 to the Transformer encoder for L rounds, then the output of the jth round is X j In each cycle, the input is first normalized by LN(X j ):
[0131]
[0132]
[0133] Step S2.2: Normalize X j Input the multi-head self-attention module. Assume that the module has H attention heads and obtain the attention matrix A of the j-th Transformer encoder layer. j , the extraction process is as follows:
[0134]
[0135]
[0136] A j =Concat(head1,..,head H )W O (48)
[0137] Where Q, K, and V are respectively j The query, key and value matrices obtained by linear projection transformation are d k is the dimension of the key matrix, softmax is the activation function, Represents the weight matrices of the query, key, and value of the i-th self-attention head, Concat represents the concatenation operation, and W O is the projection transformation matrix, A j As shown in formula (49):
[0138]
[0139] Step S2.3: The attention matrix A in each cycle j Save, a total of L rounds, concatenate them all and discard the category tensor to get the final attention matrix Considering the meaning of semantic information in different Transformer encoder layers, we first aggregate them from the layer dimension. The aggregation formula is as follows:
[0140]
[0141]
[0142] Where r is the cross-layer relationship matrix, E(A) is the mean of the attention matrices of different layers, and r is shown in formula (52):
[0143]
[0144] Step S2.4: Different attention heads of Transformer focus on different image features. Considering the semantic information in different attention heads, aggregation is performed from the dimension of the head. The aggregation formula is as follows:
[0145]
[0146]
[0147] Where R is the final relationship matrix, E(r) is the mean of the relationship matrices of different heads, and the relationship matrix is used for subsequent optimization of the preliminary interpretation results. R is shown in formula (55):
[0148]
[0149] Specifically, the specific steps of step S4 are as follows:
[0150] Step S4.1: Take out the mask set M set Put into the sequential three-branch mask module, first put all the masks M in the mask set i and its corresponding flip mask 1-M i Perform Hadamard product with the original input image I to cover the mask on the original image, and then put it into the Transformer model f(·) to obtain the corresponding confidence f(M i ⊙I) and f((1-M i )⊙I);
[0151] Step S4.2: For the first layer of three decisions, set the thresholds α, β and γ to make decisions on the mask. If the confidence f(M i ⊙I)>αβ<f((1-M i )⊙I)<α, indicating that the mask can provide object position information. The mask is placed in the positive domain POS. If the confidence f(M i ⊙I)<γor f((1-M i )⊙I)<γ, indicating that the mask does not contain valid information, the mask is placed in the negative domain NEG, and finally all the remaining masks are placed in the boundary domain BND to wait for the delay decision;
[0152] Step S4.3: Then enter the second layer of three-branch decision, and use KL divergence to make a secondary decision on the mask entering BND. KL divergence is used to measure the difference between two probability distributions, and its calculation method is as follows:
[0153]
[0154] Where P is the target distribution, Q iTo approximate the distribution, we define the probability distribution f(I) obtained after the original image I is put into the model as the target distribution, and the mask M i The probability distribution f(M i ⊙I) is an approximate distribution. By using formula (56), the KL divergence value KL(f(I)||f(M i ⊙I)), then set the threshold δ to make a decision on the mask. If KL(f(I)||f(M i ⊙I))<δor KL(f(I)||f(M i ⊙I))>1-δ, indicating that the mask is meaningful, the mask is put into POS, otherwise the mask is put into NEG;
[0155] Step S4.4: Take out all the masks in POS and record them as the active mask set M POS , the importance value of each pixel x in the input image I is calculated by applying the mask M i The output f of the corresponding category c after it is put into the model c (M i ⊙I) weighted sum and divided by the expected value of the mask set E(M POS ) to obtain:
[0156]
[0157] where f c (·) represents a model specific to category c, p is the number of masks in the active mask set;
[0158] Step S4.5: Finally, the preliminary interpretation results are normalized:
[0159]
[0160]
[0161] Specifically, the specific steps of step S5 are as follows:
[0162] Step S5.1: The preliminary interpretation result S has spatial noise and edge detail information loss problems. The relationship matrix R generated by step S2 is used to optimize the preliminary interpretation result. First, the preliminary interpretation is downsampled to Then flattened to 1×N size:
[0163] S=Flatten(Downsample(S)) (60)
[0164] Where Downsample represents the downsampling operation, Flatten represents the flattening operation, and S is shown in formula (61):
[0165]
[0166] Step S5.2: For the relationship matrix R, each row of it can reflect the relationship between the current image block and other image blocks in the image. Therefore, the cosine similarity of each row of R and S is calculated as the importance score of the current image block. The importance score P of the i-th image block is i Calculated by the following formula:
[0167]
[0168] Where · represents the vector dot product, ||·|| represents the Euclidean norm of the vector, and N is the number of image blocks;
[0169] Step S5.3: Use formula (62) to calculate the importance scores of all image blocks. Reshape it into And upsample to the original image size:
[0170] P = Upsample(Reshape(P)) (63)
[0171] Where Reshape represents the reshaping operation, Upsample represents the upsampling operation, and P is as shown in formula (64):
[0172]
[0173] Step S5.4: Use the importance score P to perform element-by-element weighting on the preliminary interpretation result S using the Hadamard product to highlight important areas and eliminate erroneous spatial noise to obtain the final interpretation result V:
[0174] V(x)=P(x)⊙S(x) (65)
[0175] Where ⊙ represents the Hadamard product operation, and V(x) is as shown in formula (66):
[0176]
[0177] As shown in formula (67), the interpretation results obtained by the traditional Transformer interpretation method in natural image datasets have spatial noise, that is, extremely high values also appear in the background area:
[0178]
[0179] Step S5.5: Finally, the final explanation result V is overlaid on the original image I for visualization in the form of a heat map. The darker the color of the heat map, the greater the contribution of the current image area to the model prediction, which effectively shows the key focus area when the Transformer model works and reflects the model interpretability.
[0180] The above description is only a preferred embodiment of this embodiment and is not intended to limit this embodiment. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of this embodiment shall be included in the protection scope of this embodiment.
Claims
1. Transformer interpretation method based on sequential three-branch masking and attention fusion, characterized by: The following steps are involved: S1: Read image I from the dataset, reshape it to the preset size H×W, then cut it into N non-overlapping image blocks of size Y×Y, flatten these image blocks and linearly map them to D dimensions, then concatenate the classification tensor, add the position information encoding to get the input tensor X0, input X0 into the Transformer encoder, repeat L rounds, and the output result of the i-th round is X i , the output of the last layer is X L ; S2: During the operation of the Transformer encoder layer, save the attention matrix A of each Transformer encoder layer j , j = 1, 2, ..., L, concatenate all the saved attention matrices and discard the category tensor Where L is the number of Transformer encoder layers, H is the number of self-attention heads in each encoder layer, and N is the number of image blocks cut into. First, layer aggregation is performed on the dimensions of the Transformer encoder layer to obtain the cross-layer relationship matrix r, that is, in Then, the head is aggregated from the dimension of the head of the self-attention layer to obtain the final relationship matrix R, that is, in S3: Take the output of the final encoder layer of the Transformer Discard the classified tensor and reshape it into a grid shape to get the reshaped tensor Then upsample it to the preset image size to get the mask set S4: Set the mask set M set Put in a sequential three-branch mask module, in the first layer three-branch decision, for mask M i ∈M set , the mask M i and flip mask 1-M i After Hadamard product with the original input image I, the corresponding confidence is obtained after being put into the Transformer model f(·). After comparing with the set thresholds α, β and γ, the mask is divided into positive mask, negative mask and uncertain mask, and they are put into the positive domain POS, negative domain NEG and boundary domain BND respectively. In the second layer, the three-branch decision is made. For the mask M in the boundary domain i ∈BND, using the KL divergence information between the original image and the mask, KL(f(I)||f(M i ⊙I)), where KL(·||·) represents the KL divergence function. After comparing with the set threshold δ, the uncertain mask is divided into positive mask and negative mask again, and they are put into the positive domain POS and the negative domain NEG respectively. Finally, the positive mask M in the positive domain is POS The weighted sum of the confidence scores obtained after the corresponding Transformer is divided by the expected value of the mask set to obtain the preliminary explanation result S, and the preliminary explanation result is normalized; S5: Downsample to Then flatten it to 1×N size and take out the aggregated Use in sequence Calculate the cosine similarity with S, and concatenate all the cosine similarity calculation results to get the importance score Then reshape P into Then upsample to the size of H×W, and finally perform Hadamard product on P and S to get the final interpretation result V. Overlay it on the original image through heat map mapping to get the final visual interpretation result.
2. The Transformer interpretation method based on sequential three-branch masking and attention fusion according to claim 1, characterized in that: The specific steps of step S2 are as follows: Step S2.1: Input tensor X0 to the Transformer encoder for L rounds, then the output of the jth round is X j In each cycle, the input is first normalized by LN(X j ): Step S2.2: Normalize X j Input the multi-head self-attention module. Assume that the module has H attention heads and obtain the attention matrix A of the j-th Transformer encoder layer. j , the extraction process is as follows: head i =Attention(QW i Q ,KW i K ,VW i V ) (3) A j =Concat(head1,..,head H )W O (4) Where Q, K, and V are respectively j The query, key and value matrices obtained by linear projection transformation are d k is the dimension of the key matrix, softmax is the activation function, W i Q , W i K , W i V Represents the weight matrices of the query, key, and value of the i-th self-attention head, Concat represents the concatenation operation, and W O is the projection transformation matrix; Step S2.3: The attention matrix A in each cycle j Save, a total of L rounds, concatenate them all and discard the category tensor to get the final attention matrix Considering the meaning of semantic information in different Transformer encoder layers, we first aggregate them from the layer dimension. The aggregation formula is as follows: Where r is the cross-layer relationship matrix, and E(A) is the mean of the attention matrices of different layers; Step S2.4: Different attention heads of Transformer focus on different image features. Considering the semantic information in different attention heads, aggregation is performed from the dimension of the head. The aggregation formula is as follows: Where R is the final relationship matrix, E(r) is the mean of the relationship matrices of different heads, and the relationship matrix is used for subsequent optimization of the preliminary interpretation results.
3. The Transformer interpretation method based on sequential three-branch masking and attention fusion according to claim 1, characterized in that: The specific steps of step S4 are as follows: Step S4.1: Take out the mask set M set Put into the sequential three-branch mask module, first put all the masks M in the mask set i and its corresponding flip mask 1-M i Perform Hadamard product with the original input image I to cover the mask on the original image, and then put it into the Transformer model f(·) to obtain the corresponding confidence f(M i ⊙I) and f((1-M i )⊙I); Step S4.2: For the first layer of three decisions, set the thresholds α, β and γ to make decisions on the mask. If the confidence f(M i ⊙I)>αβ<f((1-M i )⊙I)<α, indicating that the mask can provide object position information. The mask is placed in the positive domain POS. If the confidence f(M i ⊙I)<γor f((1-M i )⊙I)<γ, indicating that the mask does not contain valid information, the mask is placed in the negative domain NEG, and finally all the remaining masks are placed in the boundary domain BND to wait for the delay decision; Step S4.3: Then enter the second layer of three-branch decision, and use KL divergence to make a secondary decision on the mask entering BND. KL divergence is used to measure the difference between two probability distributions, and its calculation method is as follows: Where P is the target distribution, Q i To approximate the distribution, we define the probability distribution f(I) obtained after the original image I is put into the model as the target distribution, and the mask M i The probability distribution f(M i ⊙I) is the approximate distribution. By using formula (9), we can calculate the KL divergence value KL(f(I)||f(M i ⊙I)), then set the threshold δ to make a decision on the mask. If KL(f(I)||f(M i ⊙I))<δor KL(f(I)||f(M i ⊙I))>1-δ, indicating that the mask is meaningful, the mask is put into POS, otherwise the mask is put into NEG; Step S4.4: Take out all the masks in POS and record them as the active mask set M POS , the importance value of each pixel x in the input image I is calculated by applying the mask M i The output f of the corresponding category c after it is put into the model c (M i ⊙I) weighted sum and divided by the expected value of the mask set E(M POS ) to obtain: where f c (·) represents a model specific to category c, p is the number of masks in the active mask set; Step S4.5: Finally, the preliminary interpretation results are normalized:
4. The Transformer interpretation method based on sequential three-branch masking and attention fusion according to claim 1, characterized in that: The specific steps of step S5 are as follows: Step S5.1: The preliminary interpretation result S has spatial noise and edge detail information loss problems. The relationship matrix R generated by step S2 is used to optimize the preliminary interpretation result. First, the preliminary interpretation is downsampled to Then flattened to 1×N size: S=Flatten(Downsample(S))(12) where Downsample represents a downsampling operation and Flatten represents a flattening operation; Step S5.2: For the relationship matrix R, each row of it can reflect the relationship between the current image block and other image blocks in the image. Therefore, the cosine similarity of each row of R and S is calculated as the importance score of the current image block. The importance score P of the i-th image block is i Calculated by the following formula: Where · represents the vector dot product, ||·|| represents the Euclidean norm of the vector, and N is the number of image blocks; Step S5.3: Calculate all image blocks using formula (13) to obtain the importance scores of all image blocks. Reshape it into And upsample to the original image size: P = Upsample (Reshape (P)) (14) where Reshape represents a reshaping operation and Upsample represents an upsampling operation; Step S5.4: Use the importance score P to perform element-by-element weighting on the preliminary interpretation result S using the Hadamard product to highlight important areas and eliminate erroneous spatial noise to obtain the final interpretation result V: V(x)=P(x)⊙S(x)(15)where ⊙ represents the Hadamard product operation; Step S5.5: Finally, the final explanation result V is overlaid on the original image I for visualization in the form of a heat map. The darker the color of the heat map, the greater the contribution of the current image area to the model prediction, which effectively shows the key focus area when the Transformer model works and reflects the model interpretability.
Citation Information
Patent Citations
Medical image depth segmentation method based on fuzzy logic
CN116188435A
Transform interpretability method of random walk mechanism based on three-confidence-degree guidance
CN118053023A
Cited By
Signal frequency domain noise reduction data processing method and system
CN122469300A
