Main Content

imageLIME

Explain network predictions using LIME

    Description

    example

    scoreMap= imageLIME(,X,label)uses the locally-interpretable model-agnostic explanation (LIME) technique to compute a map of the importance of the features in the input imageXwhen the networkevaluates the class score for the class given bylabel。利用this function to explain classification decisions and check that your network is focusing on the appropriate features of the image.

    石灰技术近似于using a simpler, more interpretable model. By generating synthetic data from inputX, classifying the synthetic data using,然后使用结果拟合简单的回归模型imageLIMEfunction determines the importance of each feature ofXto the network's classification score for class given bylabel

    This function requires Statistics and Machine Learning Toolbox™.

    example

    [scoreMap,特色,featureImportance] = Imagelime(,X,label)also returns a map of the features used to compute the LIME results and the calculated importance of each feature.

    example

    ___= imageLIME(___,Name,Value)specifies options using one or more name-value pair arguments in addition to the input arguments in previous syntaxes. For example,“数字”,100sets the target number of features to 100.

    Examples

    全部收缩

    利用imageLIMEto visualize the parts of an image are important to a network for a classification decision.

    Import the pretrained network SqueezeNet.

    网= squeezenet;

    Import the image and resize to match the input size for the network.

    X = imread(“ laika_grass.jpg”); inputSize = net.Layers(1).InputSize(1:2); X = imresize(X,inputSize);

    Display the image. The image is of a dog named Laika.

    imshow(X)

    Classify the image to get the class label.

    标签=classify(net,X)
    标签=categoricaltoy poodle

    利用imageLIMEto determine which parts of the image are important to the classification result.

    SCOREMAP = Imagelime(Net,X,Label);

    以透明度将结果绘制在原始图像上,以查看图像的哪些区域影响分类评分。

    figure imshow(X) holdimagsc(ScoreMap,'alphadata',0.5) colormapjet

    The network focuses predominantly on Laika's head and back to make the classification decision. Laika's eye and ear are also important to the classification result.

    利用imageLIMEto determine the most important features in an image and isolate them from the unimportant features.

    Import the pretrained network SqueezeNet.

    网= squeezenet;

    Import the image and resize to match the input size for the network.

    X = imread("sherlock.jpg"); inputSize = net.Layers(1).InputSize(1:2); X = imresize(X,inputSize);

    Classify the image to get the class label.

    标签=classify(net,X)
    标签=categorical金毛寻回犬

    计算特征重要性的地图,并获得功能的地图和特征重要性。将图像分割方法设置为'网格', the number of features to64,以及合成图像的数量3072

    [scoreMap,featureMap,featureImportance] = imageLIME(net,X,label,“分割”,'网格','NumFeatures',64,'数字样本',3072);

    以透明度将结果绘制在原始图像上,以查看图像的哪些区域影响分类评分。

    figure imshow(X) holdimagsc(ScoreMap,'alphadata',0.5) colormapjetcolorbar

    利用the feature importance to find the indices of the most important five features.

    numTopFeatures = 5; [~,idx] = maxk(featureImportance,numTopFeatures);

    使用功能的地图掩盖图像,因此只能看到最重要的五个功能。显示蒙版的图像。

    mask = ismember(featureMap,idx); maskedImg = uint8(mask).*X; figure imshow(maskedImg);

    利用imageLIMEwith a custom segmentation map to view the most important features for a classification decision.

    Import the pretrained network GoogLeNet.

    网= googlenet;

    Import the image and resize to match the input size for the network.

    X = imread("sherlock.jpg"); inputSize = net.Layers(1).InputSize(1:2); X = imresize(X,inputSize);

    Classify the image to get the class label.

    标签=classify(net,X)
    标签=categorical金毛寻回犬

    Create a matrix defining a custom segmentation map which divides the image into triangular segments. Each triangular segment represents a feature.

    首先定义一个大小等于图像的输入大小的矩阵。

    segmentationMap = zeros(inputSize(1));

    Next, create a smaller segmentation map which divides a 56-by-56 pixel region into two triangular features. Assign values 1 and 2 to the upper and lower segments, representing the first and second features, respectively.

    blockSize = 56; segmentationSubset = ones(blockSize); segmentationSubset = tril(segmentationSubset) + segmentationSubset;% Set the diagonal elements to alternate values 1 and 2.segmentationSubset(1:(blockSize+1):end) = repmat([1 2],1,blockSize/2)';

    要为整个图像创建自定义分割图,请重复小型分割图。每次重复较小的地图时,都会增加特征索引值,以使每个三角形段中的像素对应于唯一功能。在最终矩阵中,值1指示第一个功能,值2第二个功能,依此类推,依此类推。

    blocksPerSide = inputSize(1)/blockSize; subset = 0;fori=1:blocksPerSideforj=1:blocksPerSide xidx = (blockSize*(i-1))+1:(blockSize*i); yidx = (blockSize*(j-1))+1:(blockSize*j); segmentationMap(xidx,yidx) = segmentationSubset + 2*subset; subset = subset + 1;endend

    View the segmentation map. This map divides the image into 32 triangular regions.

    figure imshow(X) holdimagesc(segmentationMap,'alphadata',0.8);标题(“自定义分割地图”) colormapgray

    利用imageLIMEwith the custom segmentation map to determine which parts of the image are most important to the classification result.

    scoremap = imageLime(net,x,label,。。。“分割”,分段图);

    Plot the result ofimageLIMEover the original image to see which areas of the image affect the classification score.

    figure; imshow(X) hold标题('Image LIME (Golden Retriever)') colormapjet;imagsc(ScoreMap,"AlphaData", 0.5);

    Red areas of the map have a higher importance — when these areas are removed, the score for the golden retriever class goes down. The most important feature for this classification is the ear.

    Input Arguments

    全部收缩

    图像分类网络,指定为SeriesNetwork对象或一个dagnetwork对象。你可以通过导入培训网络a pretrained network or by training your own network using thetrainNetworkfunction. For more information about pretrained networks, see预处理的深神经网络

    must contain a single input layer and a single output layer. The input layer must be animageInputLayer。The output layer must be aclassificationLayer

    Input image, specified as a numeric array.

    图像必须与网络的图像输入大小相同。The input size is specified by theInputSize网络的属性imageInputLayer

    Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

    类标签用于计算特征重要性图,该图指定为分类,char矢量,字符串标量或这些值的向量。

    如果指定labelas a vector, the software calculates the feature importance for each class label independently. In that case,scoreMap(:,:,k)featureImportance(idx,k)对应于特征重要性图和功能的重要性idxfor thekth element inlabel, respectively.

    Example:["cat" "dog"]

    Data Types:char|string|categorical

    Name-Value Arguments

    将可选的参数对Name1=Value1,...,NameN=ValueN, whereName是the argument name and价值是the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

    Example:“数字”,100,'Segmentation','grid', 'OutputUpsampling','bicubic','ExecutionEnvironment','gpu'segments the input image into a grid of approximately 100 features, executes the calculation on the GPU, and upsamples the resulting map to the same size as the input image using bicubic interpolation.

    将输入图像划分成的目标数量数量,指定为逗号分隔对'NumFeatures'和a positive integer.

    A larger value of'NumFeatures'divides the input image into more, smaller features. To get the best results when using a larger number of features, also increase the number of synthetic images using the'数字样本'name-value pair.

    The exact number of features depends on the input image and segmentation method specified using the“分割”name-value pair and can be less than the target number of features.

    • When you specify“分割”,'superpixels', the actual number of features can be greater or less than the number specified using'NumFeatures'

    • When you specify“分割”,'grid', the actual number of features can be less than the number specified using'NumFeatures'。如果您的输入图像是正方形的,请指定'NumFeatures'as a square number.

    • When you specify“细分”,分段, wheresegmentation是二维矩阵,'NumFeatures'与矩阵中的唯一元素数量相同。

    Example:“数字”,100

    Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

    要生成的合成图像数量,指定为逗号分隔对'数字样本'和a positive integer.

    A larger number of synthetic images gives better results but takes more time to compute.

    Example:“ numsamples”,1024

    Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

    分割method to use to divide the input image into features, specified as the comma-separated pair consisting of“分割”'superpixels','网格', or a two-dimensional segmentation matrix.

    TheimageLIME函数段将输入图像以以下方式取决于分割方法。

    • 'superpixels'- - - - - -Input image is divided into superpixel features, using thesuperpixels(Image Processing Toolbox)function. Features are irregularly shaped, based on the value of the pixels. This option requires Image Processing Toolbox™.

    • '网格'- - - - - -Input image is divided into a regular grid of features. Features are approximately square, based on the aspect ratio of the input image and the specified value of'NumFeatures'。网格单元的数量可能小于'NumFeatures'。如果the input image is square, specify'NumFeatures'as a square number.

    • 数字矩阵 - 输入图像使用数字矩阵作为映射分为自定义特征,其中每个像素的整数值指定相应像素的特征。'NumFeatures'与矩阵中的唯一元素数量相同。矩阵的大小必须匹配输入图像的大小。

    对于摄影图像数据,'superpixels'option usually gives better results. In this case, features are based on the contents of the image, by segmenting the image into regions of similar pixel value. For other types of images, such as spectrograms, the more regular'网格'option or a custom segmentation map can provide more useful results.

    Example:“分割”,'grid'

    Type of simple model to fit, specified as the specified as the comma-separated pair consisting of'模型''树'或者'linear'

    TheimageLIME功能使用网络对合成图像进行分类和then uses the results to fit a simple, interpretable model. The methods used to fit the results and determine the importance of each feature depend on the type of simple model used.

    • '树'- - - - - -Fit a regression tree usingfitrtree(年代tatistics and Machine Learning Toolbox)然后使用predictorImportance(年代tatistics and Machine Learning Toolbox)

    • 'linear'- - - - - -Fit a linear model with lasso regression usingfitrlinear(年代tatistics and Machine Learning Toolbox)然后使用线性模型的权重计算每个功能的重要性。

    Example:'模型','linear'

    Data Types:char|string

    分割方法的输出提升方法要使用'网格', specified as the comma-separated pair consisting of'OutputUpsampling'和上e of the following.

    • 'nearest'- - - - - -利用nearest-neighbor interpolation expand the map to the same size as the input data. The map indicates the size of the each feature with respect to the size of the input data.

    • 'bicubic'- - - - - -利用bicubic interpolation to produce a smooth map the same size as the input data.

    • 'none'- - - - - -利用no upsampling. The map can be smaller than the input data.

    如果'OutputUpsampling''nearest'或者'bicubic', the computed map is upsampled to the size of the input data using theimresizefunction.

    Example:'OutputUpsampling','bicubic'

    用于计算地图特征重要性的迷你批量的大小,指定为逗号分隔对'MiniBatchSize'和a positive integer.

    迷你批次是一组合成图像的子集。迷你批量大小指定了一次传递给网络的合成图像的数量。较大的迷你批量大小会导致更快的计算,以更多的内存为代价。

    Example:'MiniBatchSize',256

    硬件资源for computing map, specified as the comma-separated pair consisting of'ExecutionEnvironment'和上e of the following.

    • 'auto'- - - - - -利用a GPU if one is available. Otherwise, use the CPU.

    • 'cpu'- - - - - -利用the CPU.

    • 'gpu'- - - - - -利用the GPU.

    GPU选项需要并行计算工具箱™。To use a GPU for deep learning, you must also have a supported GPU device. For information on supported devices, seeGPU Support by Release(Parallel Computing Toolbox)如果you choose the“执行环境”,“ gpu”选项和并行计算工具箱或合适的GPU不可用,然后该软件返回错误。

    Example:“执行环境”,“ gpu”

    Output Arguments

    全部收缩

    Map of feature importance, returned as a numeric matrix or a numeric array. Areas in the map with higher positive values correspond to regions of input data that contribute positively to the specified classification label.

    的价值scoreMap(i,j)denotes the importance of the image pixel(i,j)to the simple model, except when you use the options“分割”,'grid', 和'OutputUpsampling','none'。In that case, thescoreMap是smaller than the input image, and the value ofscoreMap(i,j)表示该功能在位置的重要性(i,j)in the grid of features.

    如果label是specified as a vector, the change in classification score for each class label is calculated independently. In that case,scoreMap(:,:,k)corresponds to the occlusion map for thekth element inlabel

    功能地图,返回为数字矩阵。

    For each pixel(i,j)in the input image,idx = featuremap(i,j)是an integer corresponding to the index of the feature containing that pixel.

    Feature importance, returned as a numeric vector or a numeric matrix.

    的价值featureImportance(idx)是指定功能的计算重要性idx。如果you provide labels as a vector of categorical values, char vectors, or string scalars, thenfeatureImportance(idx,k)corresponds to the importance of featureidxforlabel(k)

    More About

    全部收缩

    LIME

    本地解释的模型不足解释(LIME)技术是一种解释性技术,用于解释深神网络做出的分类决策。

    Given the classification decision of deep network for a piece of input data, the LIME technique calculates the importance of each feature of the input data to the classification result.

    石灰技术近似的行为deep neural network using a simpler, more interpretable model, such as a regression tree. To map the importance of different parts of the input image, theimageLIMEfunction of performs the following steps.

    • Segment the image into features.

    • Generate synthetic image data by randomly including or excluding features. Each pixel in an excluded feature is replaced with the value of the average image pixel.

    • Classify the synthetic images using the deep network.

    • 使用每个合成图像作为目标类别得分的二进制回归预测指标的存在或不存在图像特征的存在或不存在回归模型。

    • Compute the importance of each feature using the regression model.

    The resulting map can be used to determine which features were most important to a particular classification decision. This can be especially useful for making sure your network is focusing on the appropriate features when classifying.

    版本历史

    Introduced in R2020b