理解网络预测Using LIME
This example shows how to use locally interpretable model-agnostic explanations (LIME) to understand why a deep neural network makes a classification decision.
Deep neural networks are very complex and their decisions can be hard to interpret. The LIME technique approximates the classification behavior of a deep neural network using a simpler, more interpretable model, such as a regression tree. Interpreting the decisions of this simpler model provides insight into the decisions of the neural network [1]. The simple model is used to determine the importance of features of the input data, as a proxy for the importance of the features to the deep neural network.
When a particular feature is very important to a deep network's classification decision, removing that feature significantly affects the classification score. That feature is therefore important to the simple model too.
Deep Learning Toolbox provides the我mageLIME
function to compute maps of the feature importance determined by the LIME technique. The LIME algorithm for images works by:
Segmenting an image into features.
Generating many synthetic images by randomly including or excluding features. Excluded features have every pixel replaced with the value of the image average, so they no longer contain information useful for the network.
Classifying the synthetic images with the deep network.
Fitting a simpler regression model using the presence or absence of image features for each synthetic image as binary regression predictors for the scores of the target class.The model approximates the behavior of the complex deep neural network in the region of the observation.
Computing the importance of features using the simple model, and converting this feature importance into a map that indicates the parts of the image that are most important to the model.
You can compare results from the LIME technique to other explainability techniques, such as occlusion sensitivity or Grad-CAM. For examples of how to use these related techniques, see the following examples.
Load Pretrained Network and Image
Load the pretrained network GoogLeNet.
net = googlenet;
Extract the image input size and the output classes of the network.
我nputSize = net.Layers(1).InputSize(1:2); classes = net.Layers(end).Classes;
加载图像。该图像是一个称为Sherlock的猎犬。将图像大小调整到网络输入大小。
我mg = imread("sherlock.jpg");我mg = imresize(img,inputSize);
Classify the image, and display the three classes with the highest classification score in the image title.
[YPred,scores] = classify(net,img); [~,topIdx] = maxk(scores, 3); topScores = scores(topIdx); topClasses = classes(topIdx); imshow(img) titleString = compose("%s (%.2f)",topClasses,topScores'); title(sprintf(join(titleString,"; ")));
GoogLeNet classifies Sherlock as agolden retriever
。Understandably, the network also assigns a high probability to theLabrador retriever
class. You can use我mageLIME
to understand which parts of the image the network is using to make these classification decisions.
识别网络用于分类的图像的区域
You can use LIME to find out which parts of the image are important for a class. First, look at the predicted class ofgolden retriever
。图像显示这个类的哪些部分?
By default,我mageLIME
我dentifies features in the input image by segmenting the image into superpixels. This method of segmentation requires Image Processing Toolbox; however, if you do not have Image Processing Toolbox, you can use the option“分割”,"grid"
将图像细分为正方形。
Use the我mageLIME
功能以绘制不同超级像素功能的重要性。默认情况下,简单模型是一个回归树。
map = imageLIME(net,img,YPred);
Display the image of Sherlock with the LIME map overlaid.
figure imshow(img,'InitialMagnification',150) hold上我magesc(map,'AlphaData',0.5) colormapjetcolorbar title(sprintf("Image LIME (%s)",。..YPred)) holdoff
The maps shows which areas of the image are important to the classification ofgolden retriever
。Red areas of the map have a higher importance — when these areas are removed, the score for thegolden retriever
课程下降。该网络着重于狗的脸和耳朵,以预测黄金猎犬。这与其他解释性技术一致,例如遮挡敏感性或GRAD-CAM。
Compare to Results of a Different Class
GoogLeNet predicts a score of 55% for thegolden retriever
class, and 40% for theLabrador retriever
class. These classes are very similar. You can determine which parts of the dog are more important for both classes by comparing the LIME maps computed for each class.
Using the same settings, compute the LIME map for theLabrador retriever
class.
secondClass = topClasses(2); map = imageLIME(net,img,secondClass); figure; imshow(img,'InitialMagnification',150) hold上我magesc(map,'AlphaData',0.5) colormapjetcolorbar title(sprintf("Image LIME (%s)",secondClass)) holdoff
For theLabrador retriever
class, the network is more focused on the dog's nose and eyes, rather than the ear. While both maps highlight the dog's forehead, the network has decided that the dog's ear and neck indicate thegolden retriever
class, while the dog's eye and nose indicate theLabrador retriever
class.
Compare LIME with Grad-CAM
Other image interpretability techniques such as Grad-CAM upsample the resulting map to produce a smooth heatmap of the important areas of the image. You can produce similar-looking maps with我mageLIME
, by calculating the importance of square or rectangular features and upsampling the resulting map.
To segment the image into a grid of square features instead of irregular superpixels, use the“分割”,"grid"
name-value pair. Upsample the computed map to match the image resolution using bicubic interpolation, by setting"OutputUpsampling","bicubic"
。
To increase the resolution of the initially computed map, increase the number of features to 100 by specifying the"NumFeatures",100
name-value pair. As the image is square, this produces a 10-by-10 grid of features.
石灰技术通过随机选择某些特征并用平均图像像素来替换这些特征中的所有像素,从而生成综合图像,从而有效地删除了该功能。通过设置将随机样品的数量增加到6000"NumSamples",6000
。When you increase the number of features, increasing the number of samples usually gives better results.
By default the我mageLIME
功能使用回归树作为其简单模型。相反,通过设置将线性回归模型与LASSO回归拟合"Model","linear"
。
map = imageLIME(net,img,"golden retriever",。..“分割”,"grid",。.."OutputUpsampling","bicubic",。.."NumFeatures",100,。.."NumSamples",6000,。.."Model","linear");我mshow(img,'InitialMagnification', 150) hold上我magesc(map,'AlphaData',0.5) colormapjettitle(sprintf("Image LIME (%s - linear model)",。..YPred)) holdoff
Similar to the gradient map computed by Grad-CAM, the LIME technique also strongly identifies the dog's ear as significant to the prediction ofgolden retriever
。
仅显示最重要的功能
LIME results are often plotted by showing only the most important few features. When you use the我mageLIME
function, you can also obtain a map of the features used in the computation and the calculated importance of each feature. Use these results to determine the four most important superpixel features and display only the four most important features in an image.
计算石灰图并获得特征图和每个功能的计算重要性。
[map,featureMap,featureImportance] = imageLIME(net,img,YPred);
Find the indices of the top four features.
NumTopFeatures = 4;[〜,idx] = maxk(featureimportance,numtopFeatures);
Next, mask out the image using the LIME map so only pixels in the most important four superpixels are visible. Display the masked image.
mask = ismember(featureMap,idx); maskedImg = uint8(mask).*img; figure imshow(maskedImg); title(sprintf("Image LIME (%s - top %i features)",。..YPred, numTopFeatures))
参考
[1] Ribeiro, Marco Tulio, Sameer Singh, and Carlos Guestrin. “‘Why Should I Trust You?’: Explaining the Predictions of Any Classifier.” InProceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 1135–44. San Francisco California USA: ACM, 2016. https://doi.org/10.1145/2939672.2939778.
See Also
DAGNetwork
|googlenet
|occlusionSensitivity
|我mageLIME
|gradCAM