INNvestigate - Keras Xception 模型的激活层输出方格

问题描述

我正在 Python 中运行 INNvestigate 包,以在卷积神经网络的顶部创建激活层。使用某些模型(例如 VGG-16/19resnet-50)运行时,输出符合预期。

Original Image

VGG-16

但是,当使用 XCeption 模型时,输出是方格的。

enter image description here

条形选择模型和预处理函数代码对所有模型运​​行相同。以下是所用代码的所有部分的细分。

import keras.applications as ka
import PIL.Image    

def load_image(path,size):
    ret = PIL.Image.open(path)
    ret = ret.resize((size,size))
    ret = np.asarray(ret,dtype=np.uint8).astype(np.float32)
    if ret.ndim == 2:
        # Convert gray scale image to color channels.
        ret.resize((size,size,1))
        ret = np.repeat(ret,3,axis=-1)
    return ret
    
activeModel,preprocessFunc,decodeFunc = ka.xception.Xception(),\
                                          ka.xception.preprocess_input,\
                                          ka.xception.decode_predictions
    
activeImage = load_image(imgFilename,size=activeModel.input_shape[1])
activeImage_preprocessed = preprocessFunc(activeImage[None].copy())

model_wo_softmax = innvestigate.utils.model_wo_softmax(activeModel)
    
# Get the activation parameters (this does not change when different models are chosen and therefore should not be the cause of any issues)
# Set up to look like this as the code actually can run over multiple types of activation functions as chosen by the user.
activation_param = ("deep_taylor",{},imgnetutils.heatmap,"Deep Taylor"),# Create analyzer
analyzer = innvestigate.create_analyzer(activation_param[0],# analysis method identifier
                                        model_wo_softmax,# model without softmax output
                                        neuron_selection_mode="index",# We want to select the output neuron to analyze.
                                        allow_lambda_layers=True,**activation_param[1])          # optional analysis parameters
    
# Apply analyzer w.r.t. selected class
a = analyzer.analyze(activeImage_preprocessed,neuron_selection=condition) # Condition is the class ID for the activation

a 处的输出已经是方格的,所以我不需要再进一步了。 以下是我想到/尝试过的事情的笔记

发现 Xception 的问题:

    # -------------------------------------------------------------------------------
    # - activation_param:
    #       => Activation doesn't change from usage with VGG16 (for example)
    #           and so should not be the cause of any issue (i.e. works for all other models)
    # - activeImage_preprocessed:
    #       => Right size (1,299,3) and within +/- 1 bounds. (below is from documentation)
    #           - Preprocessed numpy.array or a tf.Tensor with type float32.
    #           - The inputs pixel values are scaled between -1 and 1,sample-wise.
    #       => When plotted,looks as expected
    # - Model:
    #       => Xception model as expected
    #       => softmax is removed as expected,error thrown if it is not removed
    #
    # -------------------------------------------------------------------------------
    #  - analyzer.analyze
    #       => The output `a` here has the undesired checkered look
    #
    # -------------------------------------------------------------------------------
    # Thoughts
    # - The preprocess function for Xception is different than all others tested so far (Vgg16/19,resnet50)
    #       => Xception:
    #           - (SAME) Preprocessed numpy.array or a tf.Tensor with type float32.
    #           - (DIFFERENT) The inputs pixel values are scaled between -1 and 1,sample-wise.
    #       => VGG-16/19 resnet-50:
    #           - (SAME) Preprocessed numpy.array or a tf.Tensor with type float32.
    #           - (DIFFERENT) The images are converted from RGB to BGR,then each color channel is zero-centered
    #               with respect to the ImageNet dataset,without scaling.

任何有关为什么此输出可能会出现方格的想法将不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)