Grad-CAM improvement method based on gradient flow direction correction and feature contribution degree redistribution
The improved Grad-CAM method, which corrects gradient flow direction and redistributes feature contribution, solves the localization ambiguity and feature correlation problems of the traditional Grad-CAM algorithm, improves the localization accuracy of heatmaps and the ability to identify small targets, and maintains computational efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-16
- Publication Date
- 2026-03-13
AI Technical Summary
Traditional Grad-CAM algorithms suffer from problems such as gradient averaging leading to ambiguous localization, failure to consider the correlation between feature channels, and reliance on single-level features, resulting in low heatmap localization accuracy and insufficient ability to locate small targets.
The method of gradient flow direction correction and feature contribution redistribution is adopted. The gradient flow direction correction process preserves the spatial distribution information of the gradient, calculates the cosine similarity between feature map channels and redistributes them, and fuses the feature maps of intermediate convolutional layers to generate an explanatory heatmap.
It improves the localization accuracy of heatmaps, reduces overactivation in irrelevant areas, enhances the ability to locate small targets and detailed features, and maintains the lightweight nature of computation.
Smart Images

Figure CN121660000A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of deep learning model interpretation technology, and in particular to an improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution. Background Technology
[0002] With the rapid development of deep learning technology, convolutional neural networks have achieved remarkable success in fields such as computer vision. However, deep learning models are often regarded as "black boxes," and their decision-making process lacks transparency, which limits their application in fields such as deep learning models where interpretability is highly demanding.
[0003] Grad-CAM (Gradient-weighted Class Activation Mapping) is a commonly used model interpretation method. It calculates the gradient of the target class score with respect to the feature map of the last convolutional layer, obtaining the importance weights of the feature map channels, and then generating a visual heatmap to intuitively show the regions that the model focuses on in its decisions. However, the traditional Grad-CAM algorithm has the following drawbacks: Gradient averaging leads to ambiguous positioning. Traditional Grad-CAM performs global averaging of gradients to calculate channel weights, which loses the spatial distribution information of the gradients, resulting in low positioning accuracy of the generated heatmap.
[0004] The correlation between feature channels is not considered. Different feature channels may express similar semantic information. Traditional Grad-CAM calculates the weight of each channel independently and does not adjust the weight of redundant features, which can easily lead to overactivation of irrelevant regions in the heatmap.
[0005] Relying solely on a single layer of features, traditional Grad-CAM uses only the last layer of convolutional feature maps. While the last layer of feature maps typically contains high-level semantic information, it lacks detailed information, resulting in insufficient ability of heatmaps to locate small targets or detailed features.
[0006] Therefore, the traditional Grad-CAM algorithm needs to be improved to enhance its interpretation accuracy and robustness. Summary of the Invention
[0007] The purpose of this invention is to overcome the shortcomings of the prior art and provide an improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution, so as to solve the problems of ambiguous localization, failure to consider feature correlation and reliance on single-level features in the traditional Grad-CAM algorithm.
[0008] To achieve the above objectives, the present invention adopts the following technical solution: An improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution includes the following steps: Step 1: Obtain the feature map of the target convolutional layer in the convolutional neural network and the original gradient of the feature map with respect to the target category score; Step 2: Perform gradient flow direction correction processing on the original gradient to obtain the gradient weights of each feature map channel; Step 3: Calculate the cosine similarity between feature map channels, and redistribute the feature contribution of the gradient weights obtained in Step 2 based on the cosine similarity. Step 4: Extract the feature maps of the intermediate convolutional layers in the convolutional neural network, calculate their corresponding gradient weights, and perform downsampling. Step 5: Fuse the feature map of the target convolutional layer corresponding to the gradient weights after the redistribution in step 3 with the feature map of the intermediate convolutional layer after the processing in step 4, and generate the final explanatory heatmap after processing with the activation function.
[0009] Furthermore, the gradient flow direction correction process in step two includes: The original gradient is subjected to a two-dimensional Gaussian filter to obtain a smoothed gradient. Calculate the spatial flow direction matrix of the smoothed gradient, where each element of the spatial flow direction matrix is obtained by weighted average of the gradients of its neighborhood. The gradient weights of each feature map channel are calculated based on the peak response and gradient variance of the spatial flow matrix.
[0010] Furthermore, the formula for calculating the spatial flow direction matrix is as follows: in, Let be the flow direction matrix value of the k-th feature map at position (i,j). σ is the original gradient, σ is the standard deviation of the Gaussian filter, and dx and dy are the neighborhood offsets.
[0011] Furthermore, the formula for calculating the gradient weights is as follows: in, The gradient weights of the k-th feature map are... This represents the maximum value of the spatial flow direction matrix. Let be the variance of the spatial flow matrix, and sigmoid be the activation function.
[0012] Furthermore, the feature contribution redistribution in step three includes: Calculate the cosine similarity matrix between feature map channels; The cosine similarity matrix is filtered according to a preset threshold to obtain a redundant channel group; The gradient weights within each redundant channel group are normalized and redistributed.
[0013] Furthermore, the formula for calculating the cosine similarity matrix is as follows: in, Let be the cosine similarity between the m-th channel and the n-th channel. , These are the feature maps of the m-th and n-th channels, respectively, and · represents the vector dot product. This represents the L2 norm of a vector.
[0014] Furthermore, the formula for calculating the normalized redistribution is: in, Let k be the p-th redundant channel group, and k be the channel index within that group. These are the assigned gradient weights.
[0015] Furthermore, step four includes: Calculate the mutual information between the feature maps of the intermediate convolutional layers and the feature map of the target convolutional layer; Filter the intermediate layer feature map channels whose mutual information is greater than a preset threshold; The gradient weights of the intermediate layer feature map channels after filtering are calculated using the gradient flow direction correction process described in step two. The intermediate layer feature maps are downsampled to make their size similar to that of the target convolutional layer feature maps. Figure 1 To.
[0016] Furthermore, the calculation formula for fusion in step five is as follows: in, For the final interpretation of the heat map, , To integrate weights and , The gradient weights are the feature maps of the target convolutional layer. For the feature map of the target convolutional layer, These are the gradient weights of the feature maps in the intermediate convolutional layers. This is the feature map of the intermediate convolutional layer after downsampling, and ReLU is the activation function.
[0017] Compared with the prior art, the present invention has the following beneficial effects: This invention replaces the traditional global gradient averaging in Grad-CAM with gradient flow direction correction processing, preserving the spatial distribution information of the gradient. This makes the calculated channel weights more reflective of the actual contribution of each region of the feature map to the target category, thus improving the localization accuracy of the heatmap.
[0018] This invention redistributes feature contributions based on cosine similarity between feature channels, effectively avoiding the weight superposition of redundant feature channels, reducing overactivation of irrelevant regions in the heatmap, and enhancing the accuracy of model interpretation.
[0019] This invention integrates features from intermediate convolutional layers for detail compensation, making full use of the detailed information contained in the intermediate layer features. This overcomes the detail loss problem caused by traditional Grad-CAM relying solely on the features of the last layer, and in particular improves the ability to locate small targets and detailed features.
[0020] This invention achieves the above improvements while maintaining the lightweight characteristics of traditional Grad-CAM, with only a small increase in additional computation, making it easy to apply and promote in practice. Attached Figure Description
[0021] Figure 1 This is a block diagram of the Grad-CAM improvement method based on gradient flow direction correction and feature contribution redistribution as described in this invention. Figure 2 This is a schematic diagram of the intermediate layer feature processing flow described in this invention. Detailed Implementation
[0022] The present invention will now be described in detail with reference to the accompanying drawings.
[0023] The present invention will be further described in detail below with reference to specific embodiments.
[0024] The Grad-CAM improvement method based on gradient flow direction correction and feature contribution redistribution provided by this invention includes the following steps: Step 1: Obtain the feature map of the target convolutional layer in the convolutional neural network and the original gradient of the feature map with respect to the target category score.
[0025] In this embodiment, the last convolutional layer of the convolutional neural network is selected as the target convolutional layer, denoted as . Its output feature map is ,in Let H be the feature map of the k-th channel, where K is the number of channels, and H and W are the height and width of the feature map, respectively.
[0026] The target category score is calculated using the backpropagation algorithm. For feature maps The original gradient ,in Let be the gradient of the feature map of the k-th channel.
[0027] Step 2: Perform gradient flow direction correction on the original gradient to obtain the gradient weights of each feature map channel.
[0028] Specifically, it includes: A two-dimensional Gaussian filter is applied to the original gradient to smooth out the noise gradient. The standard deviation σ of the Gaussian filter can be set according to the actual application scenario.
[0029] Calculate the spatial flow direction matrix of the smoothed gradient. The spatial flow direction matrix of the k-th channel The calculation formula is: in, for The element values at position (i,j), dx and dy, range from {-1, 0, 1}, representing the offset of the 3×3 neighborhood.
[0030] Calculate the gradient weights of each feature map channel based on the spatial flow matrix. The calculation formula is: in, for The maximum value reflects the peak response of the gradient; for The variance reflects the consistency of the gradient response; the sigmoid function maps the variance value to the [0,1] interval. The larger the variance, the closer the output of the sigmoid function is to 1, and vice versa. The gradient weight calculated by this formula takes into account both the strength and consistency of the gradient, and can effectively filter out noisy gradients.
[0031] Step 3: Calculate the cosine similarity between feature map channels, and redistribute the feature contribution of the gradient weights obtained in Step 2 based on the cosine similarity.
[0032] Specifically, it includes: Flatten the feature map of each channel into a vector and calculate the cosine similarity matrix between channels. ,in The cosine similarity between the m-th channel and the n-th channel is calculated using the following formula: in, , and are the flattened feature vectors of the m-th and n-th channels, respectively, and · represents the vector dot product. This represents the L2 norm of the vectors. The cosine similarity ranges from -1 to 1, with a larger value indicating greater similarity in features between the two channels.
[0033] Setting a cosine similarity threshold T will satisfy... The channel pairs are considered redundant channels, and a redundant channel group is constructed. Each redundant channel group It contains a set of semantically similar feature channels.
[0034] The gradient weights within each redundant channel group are normalized and redistributed using the following formula: Where k is the redundant channel group The channel index is used within the system. This redistribution process keeps the sum of the weights of semantically similar channels unchanged, avoiding localization ambiguity caused by the superposition of redundant feature weights.
[0035] Step 4: Extract the feature maps of the intermediate convolutional layers in the convolutional neural network, calculate their corresponding gradient weights, and perform downsampling.
[0036] Specifically, it includes: We select the second-to-last convolutional layer in the intermediate convolutional neural network as the source of features for this intermediate layer, denoted as . Its output feature map is ,in Let L be the feature map of the l-th channel, where L is the number of channels, and H and W are the height and width of the feature map, respectively.
[0037] Calculate the mutual information between the intermediate layer feature maps and the target convolutional layer feature maps, filter the intermediate layer feature map channels with mutual information greater than a preset threshold, and retain the intermediate layer features that are strongly correlated with the target layer features.
[0038] Using the gradient flow direction correction process described in step two, the gradient weights of the filtered intermediate layer feature map channels are calculated. , where M is the number of intermediate layer channels after filtering.
[0039] Bilinear interpolation is used to downsample the intermediate layer feature maps, making their size similar to that of the target convolutional layer feature maps. Figure 1 Thus, the downsampled intermediate layer feature map is obtained. ,in .
[0040] Step 5: Fuse the feature map of the target convolutional layer corresponding to the gradient weights after the redistribution in step 3 with the feature map of the intermediate convolutional layer after the processing in step 4, and generate the final explanatory heatmap after processing with the activation function.
[0041] Specifically, the fusion formula is as follows: in, For the final interpretation of the heat map; , To integrate weights, satisfy ReLU is an activation function used to filter out features that contribute negatively.
[0042] In summary, this invention innovatively solves the shortcomings of the traditional Grad-CAM algorithm, such as gradient averaging leading to ambiguous localization, failure to consider feature correlation in channel weight calculation, and reliance on features from only a single level. This invention is applicable to scenarios requiring high-precision interpretation, such as model interpretation and analysis in the field of deep learning.
Claims
1. An improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution, characterized in that, Includes the following steps: Step 1: Obtain the feature map of the target convolutional layer in the convolutional neural network and the original gradient of the feature map with respect to the target category score; Step 2: Perform gradient flow direction correction processing on the original gradient to obtain the gradient weights of each feature map channel; Step 3: Calculate the cosine similarity between feature map channels, and redistribute the feature contribution of the gradient weights obtained in Step 2 based on the cosine similarity. Step 4: Extract the feature maps of the intermediate convolutional layers in the convolutional neural network, calculate their corresponding gradient weights, and perform downsampling. Step 5: Fuse the target convolutional layer feature map corresponding to the gradient weights after the redistribution in Step 3 with the intermediate convolutional layer feature map after the processing in Step 4, and generate the final explanatory heatmap after processing with the activation function.
2. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 1, characterized in that, The gradient flow direction correction process in step two includes: The original gradient is subjected to a two-dimensional Gaussian filter to obtain a smoothed gradient. Calculate the spatial flow direction matrix of the smoothed gradient, where each element of the spatial flow direction matrix is obtained by weighted average of the gradients of its neighborhood. The gradient weights of each feature map channel are calculated based on the peak response and gradient variance of the spatial flow matrix.
3. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 2, characterized in that, The formula for calculating the spatial flow direction matrix is: in, Let be the flow direction matrix value of the k-th feature map at position (i,j). σ is the original gradient, σ is the standard deviation of the Gaussian filter, and dx and dy are the neighborhood offsets.
4. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 2, characterized in that, The formula for calculating the gradient weights is: in, The gradient weights of the k-th feature map are... This represents the maximum value of the spatial flow direction matrix. Let be the variance of the spatial flow matrix, and sigmoid be the activation function.
5. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 1, characterized in that, The feature contribution reallocation in step three includes: Calculate the cosine similarity matrix between feature map channels; The cosine similarity matrix is filtered according to a preset threshold to obtain a redundant channel group; The gradient weights within each redundant channel group are normalized and redistributed.
6. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 5, characterized in that, The formula for calculating the cosine similarity matrix is: in, Let be the cosine similarity between the m-th channel and the n-th channel. , These are the feature maps of the m-th and n-th channels, respectively, and · represents the vector dot product. This represents the L2 norm of a vector.
7. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 5, characterized in that, The formula for calculating the normalized redistribution is as follows: in, Let k be the p-th redundant channel group, and k be the channel index within that group. These are the assigned gradient weights.
8. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 1, characterized in that, Step four includes: Calculate the mutual information between the feature maps of the intermediate convolutional layers and the feature map of the target convolutional layer; Filter the intermediate layer feature map channels whose mutual information is greater than a preset threshold; The gradient weights of the intermediate layer feature map channels after filtering are calculated using the gradient flow direction correction process described in step two. The intermediate layer feature map is downsampled to make its size consistent with the target convolutional layer feature map.
9. The improved Grad-CAM method based on gradient flow direction correction and feature contribution redistribution according to claim 1, characterized in that, The calculation formula for fusion in step five is as follows: in, For the final interpretation of the heat map, , To integrate weights and , The gradient weights are the feature maps of the target convolutional layer. For the feature map of the target convolutional layer, These are the gradient weights of the feature maps in the intermediate convolutional layers. This is the feature map of the intermediate convolutional layer after downsampling, and ReLU is the activation function.