Feature visualization system and method for convolutional neural networks
By designing a feature visualization system for convolutional neural networks, the problems of poor scalability and high difficulty in using feature visualization algorithms in convolutional neural networks in the prior art are solved, and efficient feature visualization of convolutional neural network models are realized. , enhances the interpretability of the model and the applicability of the algorithm.
Patent Information
- Application Number
- CN202210584949.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-27
- Publication Date
- 2025-02-11
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The existing feature visualization algorithms are poor scalability and difficult to use when applied in convolutional neural networks, and it is difficult to adapt to the feature visualization requirements of different models and data sets.
A feature visualization system for convolutional neural networks was designed. By identifying the convolutional neural network and obtaining the network layer for the required feature visualization, a feature visualization interface based on Tensorflow and Pytorch frameworks is constructed to realize the visualization of multi-layer features. And extract and save feature information through the heat map processing and output module.
It improves the interpretability of the model, simplifies the use of feature visualization, enhances the scalability of the algorithm, can be applied to different functions and types of models, and maintains the functional integrity of feature visualization when the data set changes.
Smart Images

Figure CN115098089B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computer vision technology, and in particular relates to a feature visualization system and method for convolutional neural networks. Background Art
[0002] Convolutional neural network is one of the most widely used deep learning networks in the field of computer vision. However, since convolutional neural network has a large number of neurons and parameters, and as the number of network layers increases, the number of hidden layers also increases. At the same time, since the parameter features are selected by the machine independently during the learning process, we cannot explain the implementation and learning process of the model, resulting in poor interpretability of the model. Currently in the field of computer vision, feature visualization technology has become a technical means to enhance the interpretability of models based on deep learning.
[0003] Feature visualization technology can not only enhance the interpretability of the model, but also provide guidance for model modification and optimization, and provide reference for whether the model needs to add or delete some functional modules (such as attention mechanism, data enhancement and other modules). By visualizing the features of the model, the rationality of the data set can also be judged, thereby providing a basis for the filtering strategy of the data set.
[0004] At present, the research on feature visualization algorithms is relatively mature, but the scalability of feature visualization algorithms is poor and difficult to use. The network structure of deep learning models is diverse, with different types of network layers, such as convolutional layers, fully connected layers, pooling layers, residual connection layers, etc. How to select specific network layer categories to achieve feature visualization and how to more conveniently apply feature visualization algorithms to deep learning models are functions that existing visualization algorithms cannot achieve.
[0005] In the current feature visualization algorithms, Bolei et al. proposed the Class Activation Mapping (CAM) algorithm for feature visualization in convolutional neural networks in 2016. By adding a global average pooling layer after the last convolutional layer, the feature map corresponding to each channel passing through the pooling layer is trained to predict the weights corresponding to the category (the weights can be obtained through training or through transfer learning after pre-training the model), and then each channel is multiplied and added with the corresponding weights, and finally interpolated to the original image to obtain the final result.
[0006] In 2017, Ramprasaath et al. proposed the Grad-CAM algorithm, which was improved on the basis of the CAM algorithm. It can also identify the image position without adding a global average pooling layer. Compared with the previous CAM algorithm, Grad-CAM can visualize convolutional neural networks of any structure without modifying the network structure or retraining. Grad-CAM no longer adds a global average pooling layer after the last convolution layer. It uses the gradient of back propagation to calculate the weights, and uses the obtained weights to linearly weighted fusion with the feature maps of each channel. The final result is then weakened with the Relu function to obtain a visualization image. In summary, the existing feature visualization algorithms can realize the function of feature visualization for the model, but they are more complicated to use. For those who have not studied feature visualization algorithms, it is more difficult to use. At the same time, it is easy to use the feature visualization algorithm incorrectly and is not easy to apply to different models. Summary of the invention
[0007] In order to be able to process the relatively rich related models in the field of computer vision, the present invention will face the popular convolutional neural network, process the related models based on the convolutional neural network, realize the feature visualization system for the convolutional neural network, and enhance the interpretability of the related models. In order to allow users to better use the present invention, the feature visualization system is designed in the form of an interface to ensure the scalability of the present invention.
[0008] The present invention provides a feature visualization system for convolutional neural networks, comprising: a convolutional neural network related information acquisition module, a feature visualization interface construction module I, a feature visualization interface construction module II, a multi-layer feature visualization construction module, and a feature visualization heat map processing and output module;
[0009] The convolutional neural network related information acquisition module is used to identify the convolutional neural network and obtain the network layer of the required feature visualization;
[0010] The feature visualization interface construction module I is used to construct a feature visualization interface based on the Tensorflow framework;
[0011] The feature visualization interface construction module II is used to construct a feature visualization interface based on the Pytorch framework;
[0012] The multi-layer feature visualization construction module is used to visualize multiple or all network layer features of the convolutional neural network;
[0013] The feature visualization heat map processing and output module is used to extract features in the original image by superimposing the heat map and the original image, and save them in a local file.
[0014] The present invention provides a feature visualization method for convolutional neural networks, which is implemented based on the feature visualization system for convolutional neural networks. The method includes:
[0015] Step 1: Identify the convolutional neural network and obtain the network layer for the desired feature visualization;
[0016] Step 2: Build a feature visualization interface, including building a feature visualization interface based on Tensorflow 2.0 and building a visualization interface based on the Pytorch framework;
[0017] Step 3: Visualize the features of multiple or all layers of the convolutional neural network;
[0018] Step 4: Extract features from the original image by overlaying the heat map with the original image and save them in a local file.
[0019] The step 1 comprises:
[0020] Step 1.1: Get the network layer set of the model to be identified;
[0021] Step 1.2: Traverse the set of obtained layers to obtain the convolutional layer of the network;
[0022] Step 1.3: Determine whether the last layer of the model is a fully connected layer. If it is a fully connected layer, the model is considered to be a model based on a convolutional neural network and continue to step 1.4; otherwise, the execution ends and returns to step 1.1 to re-identify the next model to be identified;
[0023] Step 1.4: Instantiate the feature visualization class, initialize the feature visualization class and determine the basic structure of the feature visualization class;
[0024] Step 1.5: Get the pre-trained model of the target network and use the pre-trained model as a parameter of the feature visualization class and pass it into the feature visualization class;
[0025] Step 1.6: Get the name of the network layer for which feature visualization is required, and pass the network layer name as a parameter of the visualization class to the feature visualization class;
[0026] Step 1.7: If the visualization layer is not specified, call the find_last_conv_layer function to find the network layer that meets the visualization requirements;
[0027] Step 1.8: Traverse the network layers of the model in reverse order, and return the name of a layer when the output dimension of the layer is 4;
[0028] Step 1.9: If the layer with dimension 4 is not found, a ValueError is returned, indicating that the model does not have a default feature visualization network layer, and the network layer required to achieve feature visualization needs to be manually set.
[0029] Step 2 constructs a feature visualization interface based on Tensorflow 2.0, including:
[0030] Step 2.1: Initialize parameters, including the pre-trained model of the network, the index of the class of the required feature visualization, the storage path class_path of the dataset label, the label set of the dataset, the file name, the name of the corresponding layer of the network, and the name of the final result save;
[0031] Step 2.2: Establish a feature visualization model based on the Tensorflow2.0 framework;
[0032] Step 2.3: Based on the Tensorflow2.0 framework, the feature visualization of the convolutional neural network is realized to obtain the heat map matrix; the specific expression is:
[0033] Step 2.3.1: Use the model built in step 2.2 to predict the input image and obtain the prediction result and the output of the specified layer;
[0034] Step 2.3.2: If the index of the label to be visualized is not specified, the index of the label with the highest probability of prediction result is found by calling the argmax function;
[0035] Step 2.3.3: Get the score of the label corresponding to the index;
[0036] Step 2.3.4: Calculate the gradient vector of the label at the specified layer through score back propagation The formula is as follows:
[0037]
[0038] Among them, y c Represents the score under this category, F k Represents the feature map under the kth channel;
[0039] Step 2.3.5: Calculate the global average of the obtained gradient The formula is as follows:
[0040]
[0041] Where M represents the number of channels of the feature map;
[0042] Step 2.3.6: Linearly weighted fusion is performed on the output of the specified layer and the average gradient value of the corresponding channel to obtain the preliminary heat map matrix h, which has the same size as the output of the specified layer. The formula is as follows:
[0043]
[0044] Step 2.3.7: Call the maximum(heatmap,0) function to set all values in the matrix that are less than 0 to 0 to obtain the heat map matrix.
[0045] Step 2 constructs a visualization interface based on the Pytorch framework, including:
[0046] Step S2.1: Initialize parameters, including the pre-trained model of the network, the index of the class of the required feature visualization, the storage path class_path of the dataset label, the label set of the dataset, the file name, the name of the corresponding layer of the network, and the name of the final result save;
[0047] Step S2.2: Inject the forward propagation hook and the backward propagation hook into the specified layer or each layer of the model.
[0048] Step S2.3: Implement feature visualization of the convolutional neural network based on the Pytorch framework to obtain a heat map matrix; the specific expression is:
[0049] Step S2.3.1: Call model.zero_grad to clear the gradient of the model to zero;
[0050] Step S2.3.2: Call score.backward to perform back propagation. score represents the probability score of the specified category predicted by the model.
[0051] Step S2.3.3: Calculate the gradient vector of the label at the specified layer through score back propagation
[0052] Step S2.3.4: Calculate the global average of the obtained gradients
[0053] Step S2.3.5: linearly weighted fusion is performed on the output of the specified layer and the average gradient value of the corresponding channel to obtain the preliminary heat map matrix h;
[0054] Step S2.3.6: Call the maximum(heatmap,0) function to set all values in the matrix that are less than 0 to 0 to obtain the heat map matrix.
[0055] The step 3 comprises:
[0056] Step 3.1: The original image information, the preprocessed image input to the pre-trained model, and the step size step as the number of feature visualization layers. If step is 1, all network layers are visualized;
[0057] Step 3.2: Visualize the features of multiple network layers. For the Tensorflow framework, call tensorflow.keras.Model to create multiple neural network models and output the features of multiple network layers of the neural network models. For the Pytorch framework, register the forward propagation hook and the backward propagation hook for each network layer to record the output of the model's forward propagation and the gradient of the backward propagation to obtain the features of multiple networks of the model.
[0058] The step 4 comprises:
[0059] Step 4.1: Normalize the heat map matrix;
[0060] Step 4.2: Save and output the heat map; the specific expression is:
[0061] Step 4.2.1: Set all the values of the heat map to between 0 and 1, set values greater than 1 to 1, and values less than 0 to 0, to prevent overflow when setting pixel values between 0-255;
[0062] Step 4.2.2: Multiply the heat map matrix by 255 and then round it to the integer value to distribute the pixel values between 0 and 255;
[0063] Step 4.2.3: Call the resize function in the opencv library to perform linear interpolation to ensure that the heat map matrix is interpolated to the same size as the original image;
[0064] Step 4.2.4: Call applyColorMap of the opencv library to set the color to the heat map mode to accurately display the heat map;
[0065] Step 4.2.5: Set the original image to 90% transparency;
[0066] Step 4.2.6: Process the heat map and original image in steps 4.2.4 and 4.2.5, and overlay the heat map matrix onto the original image with transparency to achieve feature visualization;
[0067] Step 4.2.7: Call the imwrite function in the opencv library to write the visualized image information into a local file to obtain the final output result.
[0068] The beneficial effects of the present invention are:
[0069] The present invention proposes a feature visualization system and method for convolutional neural networks. Compared with the existing feature visualization methods, the system and method can judge the input of the model, return error information when the model is not a convolutional neural network, and continue to process when the model is a convolutional neural network; the network layer structure of the application of the feature visualization algorithm can be judged, and processing can be performed when the network layer is a convolutional layer, otherwise no processing is performed; the system and method can be processed for models that are not manually specified to be visualized, and when no manual specification is performed, the final extracted features before the last convolutional layer, that is, the final output, are used as the final feature visualization results, thereby avoiding the failure of the visualization algorithm to be used successfully due to human errors; the system is designed as a feature visualization interface, which can be applied to models of different functions and types, and the feature visualization function can also be perfectly realized for the scenario of changing the data set. BRIEF DESCRIPTION OF THE DRAWINGS
[0070] Figure 1 It is an overall flow chart of the feature visualization method for convolutional neural networks in the present invention;
[0071] Figure 2 is a flow chart of the feature visualization method in the present invention;
[0072] Figure 3 The present invention is a flowchart for identifying and obtaining a convolutional neural network. DETAILED DESCRIPTION
[0073] The invention will be further described below with reference to the accompanying drawings and specific implementation examples.
[0074] The present invention provides a feature visualization system for convolutional neural networks. The system implements an interface that directly outputs a feature visualization heat map of a specified layer by inputting the original image and model information. Compared with other feature visualization methods, the system allows users to call the interface more quickly, which significantly improves practicality. The feature visualization method also ensures the quality of the visualization features and provides users with more intuitive auxiliary guidance for model improvement.
[0075] A feature visualization system for convolutional neural networks, comprising: a convolutional neural network related information acquisition module, a feature visualization interface construction module I, a feature visualization interface construction module II, a multi-layer feature visualization construction module, and a feature visualization heat map processing and output module;
[0076] The convolutional neural network related information acquisition module is used to identify the convolutional neural network and obtain the network layer of the required feature visualization;
[0077] The feature visualization interface construction module I is used to construct a feature visualization interface based on the Tensorflow framework;
[0078] The feature visualization interface construction module II is used to construct a feature visualization interface based on the Pytorch framework;
[0079] The multi-layer feature visualization construction module is used to visualize multiple or all network layer features of the convolutional neural network;
[0080] The feature visualization heat map processing and output module is used to extract features in the original image by superimposing the heat map and the original image, and save them in a local file.
[0081] like Figures 1-2 As shown, a feature visualization method for convolutional neural networks is implemented based on the feature visualization system for convolutional neural networks, and the method includes the following steps:
[0082] Step 1: Identify the convolutional neural network and obtain the network layer for the desired feature visualization;
[0083] Deep learning networks have a variety of network structures. Convolutional neural networks are one of the most widely used deep learning network structures in the field of computer vision. In order to realize the feature visualization of most image processing networks based on deep learning, the present invention processes the convolutional neural network structure and first distinguishes the network, such as Figure 3 shown.
[0084] Step 1.1: Get the model's network layer set
[0085] The deep learning network has many types of network layers, mainly including convolutional layers, fully connected layers, shortcut layers, route layers, concat layers, etc. In order to obtain the set of layers of the model, the present invention selects the Tensorflow2.0 framework and obtains the set of all layers by calling the model.layers function, avoiding network misjudgment caused by missed detection.
[0086] Step 1.2: Traverse the collection of obtained layers to obtain the convolutional layers of the network
[0087] After processing step 1.1, the set of layers that have been obtained is traversed. Since there must be a convolutional layer in the convolutional neural network, the layer.name function is called in this step to find the convolutional layer. When a layer containing the word conv is obtained, it is returned and saved.
[0088] Step 1.3: Determination of Convolutional Neural Network
[0089] In a convolutional neural network, the last layer is generally a fully connected layer, so we continue to process the layers that meet step 1.2, and determine the category of the last layer by calling the layers[-1].name function. If it is a fully connected layer, the model is considered to be based on a convolutional neural network. Otherwise, the model is considered not to be based on a convolutional neural network and no further processing is performed.
[0090] Step 1.4: First, instantiate the feature visualization class, initialize the feature visualization class and explain the basic structure of the feature visualization class;
[0091] Step 1.5: Get the pre-trained model of the target network and use the pre-trained model as a parameter of the feature visualization class and pass it into the feature visualization class;
[0092] Step 1.6: Get the name of the network layer for which feature visualization is required, and pass the network layer name as a parameter of the visualization class to the feature visualization class;
[0093] Step 1.7: If the visualization layer is not specified, call the find_last_conv_layer function to find the network layer that meets the visualization requirements;
[0094] Step 1.8: In order to obtain the default feature visualization layer, the network layer of the model needs to be traversed in reverse order. The present invention implements the reverse traversal by calling the reversed built-in function; since most of the convolutional pooling layers are four-dimensional vectors, namely the number, length, style, and number of channels, the present invention will return the name of a layer when the output dimension of the traversal is 4;
[0095] Step 1.9: If the layer with dimension 4 is not found, a ValueError is returned, indicating that the model does not have a default feature visualization network layer, and the network layer required to achieve feature visualization needs to be manually set.
[0096] Step 2: Build a feature visualization interface, including building a feature visualization interface based on Tensorflow 2.0 and building a visualization interface based on the Pytorch framework;
[0097] Construct a feature visualization interface based on Tensorflow2.0 to process the convolutional neural network obtained after step 1. The specific process is as follows:
[0098] Step 2.1: Initialize parameters
[0099] In the present invention, it is necessary to initialize multiple contents. The initialization parameters are to save the information of the model and the data set information to the interface for subsequent processing, including the pre-trained model of the corresponding network, the index of the class of the required feature visualization, the storage path class_path of the data set label, the label set of the data set, the file name, the name of the corresponding layer of the network and the name of the final result. The implementation of the initialization of the label set of the data set and the file name is relatively complicated. The initialization of the data set label set mainly includes the following steps: first call read_class_names to read the label in the file pointed to by the data set label path class_path; then read each line of information in the file, and eliminate all line breaks; finally, store the read information in the dictionary in order to realize the initialization of the data set label set. The initialization step of the file name: call the model.name function to obtain the name of the corresponding model, and create a folder corresponding to the model name to complete the initialization of the file name.
[0100] Step 2.2: Building a feature visualization model
[0101] The present invention relies on Tensorflow2.0 to establish a feature visualization model. The specific implementation of the feature visualization model is as follows: first, the model.get_layer function is called to obtain the output of the specified layer of the model; then, tensorflow.keras.Model is called to establish a neural network model in which inputs are the inputs of the model, and outputs are the outputs of the specified layer of the model and the output of the last layer of the model;
[0102] Step 2.3: Feature visualization of convolutional neural network for image recognition
[0103] In this step, the present invention needs to receive an original image and a processed input image (need to be adapted to the model), and the specific implementation includes the following steps:
[0104] Step 2.3.1: Use the model built in step 2.2 to predict the input image and obtain the prediction result and the output of the specified layer;
[0105] Step 2.3.2: If the index of the label to be visualized is not specified, the index of the label with the highest probability of prediction result is found by calling the argmax function;
[0106] Step 2.3.3: Get the score of the label corresponding to the index;
[0107] Step 2.3.4: Calculate the gradient vector of the label at the specified layer through score back propagation. The formula is as follows:
[0108]
[0109] Among them, yc represents the score of the class class, Fk represents the feature map under the kth channel, that is, the matrix of the kth channel of the specified layer, and the final result is a vector with the same shape as the specified layer.
[0110] In this design, the tape.gradient function of Tensorflow is called to calculate the gradient value of the score after backpropagation for the output of the specified layer.
[0111] Step 2.3.5: Calculate the global average of the obtained gradients. The formula is as follows:
[0112]
[0113] Where M represents the number of channels of the feature map;
[0114] The final result is the gradient value of the kth channel of the specified layer at the specified label.
[0115] Here, the reduce_mean function is used to find the average value of the first three dimensions of the vector obtained in step 2.3.4, that is, the average value of each channel.
[0116] Step 2.3.6: Linearly weighted fusion is performed on the output of the specified layer and the average gradient value of the corresponding channel to obtain a preliminary heat map matrix. The matrix has the same size as the output of the specified layer. The formula is as follows:
[0117]
[0118] Step 2.3.7: Set all values in the matrix that are less than 0 to 0 and call the maximum(heatmap,0) function to obtain the heat map matrix.
[0119] In addition, the present invention has scalability in the framework, is applicable to Tensorflow2.0 and Pytorch versions, and also realizes the feature visualization of multiple or all network layers. The following steps will gradually introduce the implementation method of Pytorch and the implementation of feature visualization of multiple or all network layers.
[0120] Step S2: Implementation of Pytorch feature visualization interface
[0121] Step S2.1: Initialize parameters
[0122] This step is implemented based on step 2.1, and the specific parameters required are the same as those in step 2.1.
[0123] Step S2.2: Inject hooks into a specific layer or each layer of the model
[0124] This step injects each network layer of the incoming model into the hook to obtain the corresponding forward propagation output and the gradient of the back propagation. Its implementation may include the following sub-steps:
[0125] Step S2.2.1: Inject the forward propagation hook
[0126] The purpose of this step is to save the output of the forward propagation in a list by calling register_forward_hook.
[0127] Step S2.2.2: Injecting the backpropagation hook
[0128] The purpose of this step is to save the gradients after backpropagation in a list by calling register_backward_hook.
[0129] Step S2.3: Feature visualization implementation of convolutional neural network on Pytorch.
[0130] This step is based on step 2.3. The specific mathematical principle is the same as the above step; the specific expression is:
[0131] Step S2.3.1: Call model.zero_grad to clear the gradient of the model to zero;
[0132] Step S2.3.2: Call score.backward to perform back propagation. score represents the probability score of the specified category predicted by the model.
[0133] Step S2.3.3: Calculate the gradient vector of the label at the specified layer through score back propagation
[0134] Step S2.3.4: Calculate the global average of the obtained gradients
[0135] Step S2.3.5: linearly weighted fusion is performed on the output of the specified layer and the average gradient value of the corresponding channel to obtain the preliminary heat map matrix h;
[0136] Step S2.3.6: Call the maximum(heatmap,0) function to set all values in the matrix that are less than 0 to 0 to obtain the heat map matrix.
[0137] The following describes the implementation of visualizing multiple network layers or all network layers.
[0138] Step 3: Visualize the features of multiple or all layers of the convolutional neural network; the specific process is as follows:
[0139] Step 3.1: Initialize parameters; you need to pass in the original image information original_image, the preprocessed image image, and the step size step as the number of feature visualization layers. If step is 1, all network layers are visualized.
[0140] Step 3.2: Feature visualization implementation of multiple network layers
[0141] In the Tensorflow version, the present invention calls tensorflow.keras.Model to establish a neural network model in which inputs are the inputs of the model and outputs are the output of each network layer of the model and the set of outputs of the last layer of the model. The specific implementation is the same as step 2.3. In the Pytorch version, the present invention records the output of the forward propagation and the gradient of the backward propagation of the model by registering the forward propagation hook and the backward propagation hook for each network layer, and obtains the weights of the feature maps corresponding to multiple network layers of the model.
[0142] Step 4: Extract features from the original image by superimposing the heat map and the original image, and save them in a local file; the specific expression is:
[0143] Feature visualization processing and output
[0144] Step 4.1: Normalize the heatmap matrix
[0145] This step normalizes the obtained heat map matrix to obtain the normalized heat map matrix, ensuring sufficient image contrast and preventing unbalanced pixel brightness distribution. The specific implementation is: first call the reduce_max function in Tensorflow to obtain the maximum value in the matrix; then use the heat map matrix obtained in module 2 as input, and use the maximum value in the matrix as a reference for normalization, that is, divide the heat map matrix by the maximum value to achieve the normalization effect.
[0146] Step 4.2: Saving and exporting heatmap
[0147] The normalized heat map matrix processed in step 4.1 is further processed to save the heat map, obtain the original image information, and overlay the heat map onto the original image to achieve the final feature extraction of the original image. The specific implementation includes the following sub-steps:
[0148] Step 4.2.1: Set all values of the heat map to between 0 and 1. Set values greater than 1 to 1 and values less than 0 to 0 to prevent overflow when setting pixel values between 0-255.
[0149] Step 4.2.2: Multiply the heat map matrix by 255. Since pixel values cannot have decimal places, round them up to make the pixel values between 0 and 255.
[0150] Step 4.2.3: In order to ensure that the heat map can be accurately combined with the original image, the present invention calls the resize function in the opencv library to perform linear interpolation to ensure that the heat map matrix is interpolated to the same size as the original image;
[0151] Step 4.2.4: Call applyColorMap of the opencv library to set the color to the heat map mode to accurately display the heat map;
[0152] Step 4.2.5: In order to clearly observe the combination of the visualized feature map and the original image, the present invention sets the transparency of the original image to 90%;
[0153] Step 4.2.6: Process the heat map and original image in steps 4.2.4 and 4.2.5, and overlay the heat map matrix onto the original image with transparency to achieve feature visualization;
[0154] Step 4.2.7: In order to save the result of feature visualization, the present invention calls the imwrite function in the opencv library to write the visualized image information into a local file to obtain the final output result.
[0155] In this embodiment, the specific calling schemes of the interface designed by the present invention are divided into two types, one is a calling scheme under the Tensorflow2.0 framework, and the other is a calling scheme under the Pytorch1.7.0 and above frameworks.
[0156] 1)Tensorflow version
[0157] 1.1) Declare an instance of the interface class and implement it in the following way
[0158] gradcam=GradCam(model=model, classes_path="data / classes / imagenet.names", dataset="imagenet", classIdx=None, layer_name=None, save_layer_name="all")
[0159] Among them, model is an instance of the tensorflow.keras.Model class; classes_path is a string that needs to receive the storage path of the dataset label, and the labels need to be stored row by row according to category; classIdx is an integer or a list that receives the sequence of categories to be visualized. If not specified, the category with the highest probability of recognition will be selected for visualization; dataset is a string that receives the format of the dataset, which is currently divided into default and imagenet; layer_name is a string that receives the name of the network layer that needs feature visualization. If show_all_grad is called, the network layer can be unspecified here; save_layer_name is a string that receives the name of the folder to be stored at the end.
[0160] 1.2) To visualize features of a specific network layer, call apply_cls_grad
[0161] gradcam.apply_cls_grad(original_image,image)
[0162] Among them, original_image is the image data initially read, in the format of (length, width, number of channels), which can be tensor or numpy.array; image is the preprocessed image data, which can be directly used for model prediction, and can be tensor or numpy.array.
[0163] 1.3) To visualize all or multiple network layers, call show_all_grad
[0164] gradcam.show_all_grad(original_image,image,interval=0)
[0165] Where original_image and image are the same as in 1.2); interval is an integer, which indicates the number of network layers between which visualization is performed. When interval = 0, all network layers will be visualized.
[0166] 2) Pytorch version
[0167] 2.1) Declare an instance of the interface class in the following way. The following is an example:
[0168] gradcam=GradCam(model=resnet, classes_path=". / imagenet.names", dataset="imagenet", layer_name=None, save_layer_name="all")
[0169] Among them, model is an instance of the torch.nn.Module class; classes_path is a string that needs to receive the storage path of the dataset label. Except for the specific formats of yaml and imagenet, the labels need to be stored line by line according to the category; classIdx is an integer or a list that receives the sequence of categories to be visualized. If not specified, the category with the highest probability of recognition will be selected for visualization; dataset is a string that receives the format of the dataset, which is currently divided into default, imagenet and yaml; layer_name is a string that receives the name of the network layer that needs feature visualization. If show_all_grad is called, the network layer can be unspecified here; save_layer_name is a string that receives the name of the folder to be stored at the end.
[0170] 2.2) To visualize features of a specific network layer, call apply_cls_grad
[0171] gradcam.apply_cls_grad(original_image,image)
[0172] Among them, original_image is the image data initially read, the data format is (length, width, number of channels), and the class can be tensor or numpy.array; image is the preprocessed image data, which can be directly used for model prediction, and can be tensor or numpy.array.
[0173] 2.3) To feature visualize all or multiple network layers, call apply_all_grad
[0174] gradcam.apply_all_grad(original_image,image,interval=0)
[0175] Where original_image and image are the same as in 1.2); interval is an integer, which indicates the number of network layers between which visualization is performed. When interval = 0, all network layers will be visualized.
[0176] The present invention uses an interface to realize the feature visualization function of the deep model. First, it determines whether the input model is a convolutional neural network, then extracts the convolutional layer in the convolutional neural network, and provides an optional (default / manually set) convolutional layer for feature visualization; secondly, the present invention also designs a feature visualization interface, which can realize feature visualization of a specific network layer only by calling the interface; finally, the present invention integrates the result of feature visualization with the original image, and can clearly identify the features and information obtained by the network in the original image. The present invention enhances the interpretability of the model, provides a basis for the filtering strategy of the data set, and provides guidance for the addition and deletion of network modules.
Claims
1. A feature visualization method for a convolutional neural network, implemented based on the feature visualization system for a convolutional neural network, the system comprising: Convolutional neural network related information acquisition module, feature visualization interface construction module I, feature visualization interface construction module II, multi-layer feature visualization construction module, feature visualization heat map processing and output module; the convolutional neural network related information acquisition module is used to identify the convolutional neural network and obtain the network layer required for feature visualization; The feature visualization interface construction module I is used to construct a feature visualization interface based on the Tensorflow framework; The feature visualization interface construction module II is used to construct a feature visualization interface based on the Pytorch framework; the multi-layer feature visualization construction module is used to visualize multiple or all network layer features of a convolutional neural network; the feature visualization heat map processing and output module is used to extract features from the original image by superimposing the heat map and the original image, and save them in a local file, characterized in that the method includes: Step 1: Identify the convolutional neural network and obtain the network layer for the desired feature visualization; Step 1.1: Get the network layer set of the model to be identified; Step 1.2: Traverse the set of obtained layers to obtain the convolutional layer of the network; Step 1.3: Determine whether the last layer of the model is a fully connected layer. If it is a fully connected layer, the model is considered to be a model based on a convolutional neural network and continue to step 1.4; otherwise, the execution ends and returns to step 1.1 to re-identify the next model to be identified; Step 1.4: Instantiate the feature visualization class, initialize the feature visualization class and determine the basic structure of the feature visualization class; Step 1.5: Get the pre-trained model of the target network and use the pre-trained model as a parameter of the feature visualization class and pass it into the feature visualization class; Step 1.6: Get the name of the network layer for which feature visualization is required, and pass the network layer name as a parameter of the visualization class to the feature visualization class; Step 1.7: If the visualization layer is not specified, call the find_last_conv_layer function to find the network layer that meets the visualization requirements; Step 1.8: Traverse the network layers of the model in reverse order, and return the name of a layer when the output dimension of the layer is 4; Step 1.9: If the layer with dimension 4 is not found, a ValueError is returned, indicating that the model does not have a default feature visualization network layer, and the network layer required for feature visualization needs to be manually set; Step 2: Build a feature visualization interface, including building a feature visualization interface based on Tensorflow 2.0 and building a visualization interface based on the Pytorch framework; Step 3: Visualize the features of multiple or all layers of the convolutional neural network; Step 4: Extract features from the original image by overlaying the heat map with the original image and save them in a local file.
2. A feature visualization method for convolutional neural networks according to claim 1, characterized in that: Step 2 constructs a feature visualization interface based on Tensorflow 2.0, including: Step 2.1: Initialize parameters, including the pre-trained model of the network, the index of the class of the required feature visualization, the storage path class_path of the dataset label, the label set of the dataset, the file name, the name of the corresponding layer of the network, and the name of the final result save; Step 2.2: Establish a feature visualization model based on the Tensorflow2.0 framework; Step 2.3: Implement feature visualization of the convolutional neural network based on the Tensorflow2.0 framework to obtain the heat map matrix.
3. The feature visualization method for convolutional neural networks according to claim 1, characterized in that: The step 2 constructs a visualization interface based on the Pytorch framework, including: Step S2.1: Initialize parameters, including the pre-trained model of the network, the index of the class of the required feature visualization, the storage path class_path of the dataset label, the label set of the dataset, the file name, the name of the corresponding layer of the network, and the name of the final result save; Step S2.2: Inject forward propagation hook and backward propagation hook into the specified layer or each layer of the model. Step S2.3: Implement feature visualization of the convolutional neural network based on the Pytorch framework to obtain a heat map matrix.
4. The feature visualization method for convolutional neural networks according to claim 1, characterized in that: The step 3 comprises: Step 3.1: The original image information, the preprocessed image input to the pre-trained model, and the step size step as the number of feature visualization layers. If step is 1, all network layers are visualized; Step 3.2: Visualize the features of multiple network layers. For the Tensorflow framework, call tensorflow.keras.Model to create multiple neural network models and output the features of multiple network layers of the neural network models. For the Pytorch framework, register the forward propagation hook and the backward propagation hook for each network layer to record the output of the model's forward propagation and the gradient of the backward propagation to obtain the features of multiple networks of the model.
5. The feature visualization method for convolutional neural networks according to claim 1, characterized in that: The step 4 comprises: Step 4.1: Normalize the heat map matrix; Step 4.2: Save and output the heat map; the specific expression is: Step 4.2.1: Set all values of the heat map to between 0 and 1, set values greater than 1 to 1, and values less than 0 to 0, to prevent overflow when setting pixel values between 0-255; Step 4.2.2: Multiply the heat map matrix by 255 and then round it to the integer value to distribute the pixel values between 0 and 255; Step 4.2.3: Call the resize function in the opencv library to perform linear interpolation to ensure that the heat map matrix is interpolated to the same size as the original image; Step 4.2.4: Call applyColorMap of the opencv library to set the color to the heat map mode to accurately display the heat map; Step 4.2.5: Set the original image to 90% transparency; Step 4.2.6: Process the heat map and original image in steps 4.2.4 and 4.2.5, overlay the heat map matrix onto the original image with transparency, and realize feature visualization; Step 4.2.7: Call the imwrite function in the opencv library to write the visualized image information into a local file to obtain the final output result.
6. A feature visualization method for convolutional neural networks according to claim 2, characterized in that: The step 2.3 is specifically described as follows: Step 2.3.1: Use the model built in step 2.2 to predict the input image and obtain the prediction result and the output of the specified layer; Step 2.3.2: If the index of the label to be visualized is not specified, the index of the label with the highest probability of prediction result is found by calling the argmax function; Step 2.3.3: Get the score of the label corresponding to the index; Step 2.3.4: Calculate the gradient vector of the label at the specified layer through score back propagation The formula is as follows: Among them, y c Represents the score under this category, F k Represents the feature map under the kth channel; Step 2.3.5: Calculate the global average of the obtained gradient The formula is as follows: Where M represents the number of channels of the feature map; Step 2.3.6: Linearly weighted fusion is performed on the output of the specified layer and the average gradient value of the corresponding channel to obtain the preliminary heat map matrix h, which has the same size as the output of the specified layer. The formula is as follows: Step 2.3.7: Call the maximum(heatmap,0) function to set all values in the matrix that are less than 0 to 0 to obtain the heat map matrix.
7. The feature visualization method for convolutional neural networks according to claim 3, characterized in that: The step S2.3 is specifically described as follows: The specific expression is: Step S2.3.1: Call model.zero_grad to clear the gradient of the model to zero; Step S2.3.2: Call score.backward to perform back propagation. score represents the probability score of the specified category predicted by the model. Step S2.3.3: Calculate the gradient vector of the label at the specified layer through score back propagation Step S2.3.4: Calculate the global average of the obtained gradients Step S2.3.5: linearly weighted fusion is performed on the output of the specified layer and the average gradient value of the corresponding channel to obtain the preliminary heat map matrix h; Step S2.3.6: Call the maximum(heatmap,0) function to set all values in the matrix that are less than 0 to 0 to obtain the heat map matrix.