如何解决类型错误-Lime解释CNN结果

时间:2018-10-22 17:51:41

标签: python vgg-net lime

我正在将Keras与Tesorflow后端一起使用(使用python 2.7),并且在具有四个类的预训练vgg16模型之上构建了两个密集层。在验证集上,我得到了相当不错的结果。现在,我想用石灰解释我的结果。

我导入了lime软件包,并按照lime github repo https://github.com/marcotcr/lime/blob/master/doc/notebooks/Tutorial%20-%20Image%20Classification%20Keras.ipynb转换了我的一张图片。我的path_list包含一张照片。

import lime
from lime import lime_image

def transform_img_fn(path_list):
    out = []
    for img_path in path_list:
        img = image.load_img(img_path, target_size=(224, 224))
        x = image.img_to_array(img) / 255
        x = np.expand_dims(x, axis=0)
        out.append(x)
    return np.vstack(out)

check_image = transform_img_fn(path_list)

然后

check_image[0].shape
OUTPUT: (3, 224, 224)
predictions[0]
OUTPUT: array([9.67346e-01, 3.00240e-03, 2.96037e-02, 4.79915e-05], dtype=float32)

explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(check_image[0], model_top.predict, hide_color=0, num_samples=100)

我收到此错误:

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-115-e286e97e849d> in <module>()
----> 1 explanation = explainer.explain_instance(check_image[0], model_top.predict, hide_color=0, num_samples=100)

/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/lime_image.pyc in explain_instance(self, image, classifier_fn, labels, hide_color, top_labels, num_features, num_samples, batch_size, segmentation_fn, distance_metric, model_regressor, random_seed)
    165             segmentation_fn = SegmentationAlgorithm('quickshift', kernel_size=4,
    166                                                     max_dist=200, ratio=0.2,
--> 167                                                     random_seed=random_seed)
    168         try:
    169             segments = segmentation_fn(image)

/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/wrappers/scikit_image.pyc in __init__(self, algo_type, **target_params)
    103         if (self.algo_type == 'quickshift'):
    104             BaseWrapper.__init__(self, quickshift, **target_params)
--> 105             kwargs = self.filter_params(quickshift)
    106             self.set_params(**kwargs)
    107         elif (self.algo_type == 'felzenszwalb'):

/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/wrappers/scikit_image.pyc in filter_params(self, fn, override)
     82         result = {}
     83         for name, value in self.target_params.items():
---> 84             if has_arg(fn, name):
     85                 result.update({name: value})
     86         result.update(override)

/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/utils/generic_utils.pyc in has_arg(fn, arg_name)
     19         else:
     20             try:
---> 21                 arg_spec = inspect.getargspec(fn.__call__)
     22             except AttributeError:
     23                 return False

/home/ec2-user/anaconda2/envs/env1/lib/python2.7/inspect.pyc in getargspec(func)
    813         func = func.im_func
    814     if not isfunction(func):
--> 815         raise TypeError('{!r} is not a Python function'.format(func))
    816     args, varargs, varkw = getargs(func.func_code)
    817     return ArgSpec(args, varargs, varkw, func.func_defaults)

TypeError: <method-wrapper '__call__' of builtin_function_or_method object at 0x7fea20ea4e60> is not a Python function

根据文档,“ classifier_fn:获取图像列表并返回预测概率矩阵的函数”。我用model_top.predict代替了这个参数。如果我将预测值称为model_top.predict(validation_data,batch_size = 32)

,就可以得到所有预测值

任何帮助将不胜感激。

0 个答案:

没有答案