示例#1
0
def lime_explainer(image, preds):
    for x in preds.argsort()[0][-5:]:
        print(x, names[x], preds[0, x])
        top_indeces.append(x)
    tmp = datetime.now()
    explainer = lime_image.LimeImageExplainer()
    # Hide color is the color for a superpixel turned OFF. Alternatively, if it is NONE, the superpixel will be replaced by the average of its pixels
    explanation = explainer.explain_instance(image,
                                             predict_fn,
                                             top_labels=5,
                                             hide_color=0,
                                             num_samples=1000)
    #to see the explanation for the top class
    temp, mask = explanation.get_image_and_mask(top_indeces[4],
                                                positive_only=True,
                                                num_features=5,
                                                hide_rest=True)
    im_top1 = mark_boundaries(temp / 2 + 0.5, mask)
    #print "iminfo",im_top1.shape, im_top1.dtype
    im_top1 = im_top1[:, :, (2, 1, 0)]  #BGR to RGB
    temp1, mask1 = explanation.get_image_and_mask(top_indeces[3],
                                                  positive_only=True,
                                                  num_features=100,
                                                  hide_rest=True)
    im_top2 = mark_boundaries(temp1 / 2 + 0.5, mask1)
    im_top2 = im_top2[:, :, (2, 1, 0)]  #BGR to RGB
    del top_indeces[:]
    return im_top1, im_top2
示例#2
0
def lime(model):

    #process image
    mnist = MNIST()
    image, label = mnist.get_batch(1, dataset='testing')
    image = image[0]
    image = np.concatenate((image, image, image), axis=2)
    print(image.shape)
    print('Print image:')

    plt.imshow(np.squeeze(image) / 2 + 0.5)
    plt.show()

    print(model.compute_error(image, expand_dims=True))

    #Explain
    explainer = lime_image.LimeImageExplainer()
    explanation = explainer.explain_instance(image,
                                             model.compute_error,
                                             top_labels=2)
    print(explanation)

    #Show superpixels
    temp, mask = explanation.get_image_and_mask(explanation.top_labels[0],
                                                positive_only=True,
                                                hide_rest=True)
    #pdb.set_trace()
    print('Print superpixels:')
    marked = seg.mark_boundaries(temp / 2 + 0.5, mask).astype(np.uint8)
    plt.imshow(marked)
    plt.show()
示例#3
0
def explain_with_lime():
    for i in range(50):
        img = cv2.imread('data/breakout/images/breakout-' + str(i) + '.png')

        # Revert image to array
        for it in range(4):
            img = image.img_to_array(img[:, :, it * 3:3 * (it + 1)])

            output.append(img)

        actions = str(f.readline())
        actset.append(actions)

        predictions = get_prediction(output[i][i][i][0], actset[i])

    explainer = lime_image.LimeImageExplainer()

    # Hide color is the color for a superpixel turned OFF. Alternatively, if
    # it is NONE, the superpixel will be replaced by the average of its pixels

    explanation = explainer.explain_instance(output[0][0],
                                             actset[0],
                                             model,
                                             hide_color=0,
                                             num_samples=1000)

    from skimage.segmentation import mark_boundaries

    temp, mask = explanation.get_image_and_mask(295,
                                                positive_only=True,
                                                num_features=5,
                                                hide_rest=True)
    fig = (mark_boundaries(temp / 2 + 0.5, mask))
    plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
    fig.savefig("data/breakout/LIME_output/LIME_breakout.png")
示例#4
0
def main():
    # Synset words
    classes = get_classtable()
    # Images
    image_paths = ['samples/cat_dog.png']
    images = load_images(image_paths)

    probs = predict(images)
    probs, ids = probs.sort(dim=1, descending=True)
    for i in range(5):
        print('#{}: class: {}, prob: {}'.format(i, classes[ids[0, i]], probs[0, i]))
    
    # lime
    explainer = lime_image.LimeImageExplainer()
    start = time.time()
    explanation = explainer.explain_instance(
            images[0],
            predict_numpy,
            top_labels=5,
            hide_color=0,
            num_samples=1000,
        )
    for i in range(5):
        temp, mask = explanation.get_image_and_mask(explanation.top_labels[i], positive_only=True, num_features=5, hide_rest=True)
        img_boundary1 = mark_boundaries(temp/255.0, mask)
        cv2.imwrite('{}.jpg'.format(classes[ids[0, i]]), (img_boundary1[:, :, ::-1] * 255).astype(np.uint8))
def generate_explanation(model, input_image):
    explainer = lime_image.LimeImageExplainer()
    explanation = explainer.explain_instance(image=input_image, classifier_fn=model.predict, hide_color=0,
                                             num_samples=1000, random_seed=18)
    temp, mask = explanation.get_image_and_mask(label=explanation.top_labels[0], positive_only=True, num_features=5,
                                                hide_rest=False)
    return temp, mask
示例#6
0
    def CalcExplainAndShow(self, id_=None):
        #Step 1 : Do the Calculation for the prediction using the pretrained model
        prediction = self.CalcProbImage(id_=id_)
        name_list = []
        id_list = []

        for x in prediction[0].argsort()[0][-2:]:
            name_list.insert(0, names[x].split(',')[0].capitalize())
            id_list.insert(0, x)

        #Step 2 : Get the explanation for the prediction
        explainer = lime_image.LimeImageExplainer()

        #Step 3 : Create the button for each of the 5 best predictions
        button_1 = Button(
            self.top_complex,
            text=name_list[0],
            command=lambda: self.ExplainAndShow(name=name_list[0],
                                                id_=id_list[0],
                                                explainer=explainer,
                                                image=prediction[1]),
            bg="turquoise",
            width=27)
        button_1.place(x=675, y=300)

        button_2 = Button(
            self.top_complex,
            text=name_list[1],
            command=lambda: self.ExplainAndShow(name=name_list[1],
                                                id_=id_list[1],
                                                explainer=explainer,
                                                image=prediction[1]),
            bg="tan",
            width=27)
        button_2.place(x=675, y=330)
def get_lime(img_indices):
    images, labels = train_set.getbatch(img_indices)
    fig, axs = plt.subplots(2, 4, figsize=(15, 8))
    for i, img in enumerate(images):
        convert_img = cv2.cvtColor(
            img.permute(1, 2, 0).numpy(), cv2.COLOR_RGB2BGR)
        axs[0][i].imshow(convert_img)
    for idx, (image, label) in enumerate(
            zip(images.permute(0, 2, 3, 1).numpy(), labels)):
        x = image.astype(np.double)
        explainer = lime_image.LimeImageExplainer()
        explaination = explainer.explain_instance(image=x,
                                                  classifier_fn=predict,
                                                  segmentation_fn=segmentation)

        lime_img, mask = explaination.get_image_and_mask(label=label.item(),
                                                         positive_only=False,
                                                         hide_rest=False,
                                                         num_features=11,
                                                         min_weight=0.05)

        axs[1][idx].imshow(lime_img)

    plt.show()
    print(labels)
示例#8
0
    def __init__(self, *argv, **kwargs):
        """
        Initialize lime Image explainer object
        """
        super(LimeImageExplainer, self).__init__(*argv, **kwargs)

        self.explainer = lime_image.LimeImageExplainer(*argv, **kwargs)
示例#9
0
文件: lime.py 项目: samsonq/COVID-XAI
 def __init__(self, model, num_samples=1000):
     if isinstance(model, str):
         self.model = load_model(model)
     else:
         self.model = model
     self.num_samples = num_samples
     self.explainer = lime_image.LimeImageExplainer()
def explain_with_lime(img_p, foolbox_model, random_seed=42):
    img = get_image(img_p)
    explainer = lime_image.LimeImageExplainer()
    transf_img = np.array(get_pil_transform()(img))
    classifier_fn = partial(foolbox_predict, foolbox_model)
    explanation = explainer.explain_instance(transf_img,
                                             classifier_fn,  # classification function
                                             top_labels=5,
                                             hide_color=0,
                                             random_seed=random_seed,
                                             num_samples=1000)  # number of images that will be sent to classification function
    # temp, mask = explanation.get_image_and_mask(explanation.top_labels[0],
    #                                             positive_only=True,
    #                                             num_features=5,
    #                                             hide_rest=False)
    temp, mask = explanation.get_image_and_mask(explanation.top_labels[0],
                                                positive_only=False,
                                                num_features=10,
                                                hide_rest=False)
    print(f"Top labels for image '{img_p}':")
    for idx, label_idx in enumerate(explanation.top_labels, 1):
        print(f"\t{idx})  '{imagenet_label_dict[label_idx]} ({label_idx})'")

    probs = classifier_fn([transf_img])
    print(f"Predicted class: {probs.argmax()}, probability: {probs.max()}")

    img_boundary1 = mark_boundaries(temp / 255.0, mask)
    return img_boundary1
示例#11
0
 def __init__(self, model: nn.Module):
     self.model = model
     self.explainer = lime_image.LimeImageExplainer(verbose=False)
     self.segmenter = SegmentationAlgorithm('quickshift',
                                            kernel_size=1,
                                            max_dist=200,
                                            ratio=0.2)
示例#12
0
    def explain_image(self, model, data, class_to_explain):
        explainer = lime_image.LimeImageExplainer()
        if data.shape[1] < 50:
            segmenter = SegmentationAlgorithm(
                'quickshift', kernel_size=1, max_dist=200, ratio=0.2)

            explanation = explainer.explain_instance(data[0],
                                                     model.predict,
                                                     top_labels=self.top_labels,
                                                     # hide_color=0,
                                                     num_samples=self.num_samples,
                                                     segmentation_fn=segmenter)

        else:
            explanation = explainer.explain_instance(data[0],
                                                     model.predict,
                                                     top_labels=self.top_labels,
                                                     # hide_color=0,
                                                     num_samples=self.num_samples)

        temp, mask = explanation.get_image_and_mask(class_to_explain,
                                                    positive_only=False,
                                                    num_features=self.num_features,
                                                    hide_rest=False)

        return mark_boundaries(temp / 2 + 0.5, mask)
示例#13
0
def lime_exp():
    explainer = lime_image.LimeImageExplainer()

    # Hide color is the color for a superpixel turned OFF. Alternatively, if
    # it is NONE, the superpixel will be replaced by the average of its pixels

    # explain_instance(image, classifier_fn, labels=(1, ), hide_color=None, top_labels=5, num_features=100000, num_samples=1000, batch_size=10, qs_kernel_size=4, distance_metric='cosine', model_regressor=None)

    # My note: Get one explainer for now
    # for i in range(len(output)):
    explanation = explainer.explain_instance(output[0][0],
                                             actions,
                                             model,
                                             hide_color=0,
                                             num_samples=1000)

    from skimage.segmentation import mark_boundaries

    temp, mask = explanation.get_image_and_mask(295,
                                                positive_only=True,
                                                num_features=5,
                                                hide_rest=True)
    fig = (mark_boundaries(temp / 2 + 0.5, mask))
    plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
    fig.savefig("output.png")
示例#14
0
def explain(image, my_model, prediction_rank=0, show_img=True):
    start = time.time()
    explainer = lime_image.LimeImageExplainer(verbose=True)
    explanation = explainer.explain_instance(image,
                                             my_model.predict,
                                             top_labels=5,
                                             hide_color=0,
                                             num_samples=1000)
    # print(explanation)

    decoder = get_imagenet_to_label()
    print(explanation.top_labels[prediction_rank],
          decoder[explanation.top_labels[prediction_rank]])

    temp, mask = explanation.get_image_and_mask(
        explanation.top_labels[prediction_rank],
        positive_only=False,
        num_features=5,
        hide_rest=False
    )  # num_features is top super pixel that gives positive value

    print("Explanation time", time.time() - start)
    if show_img:
        plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
        plt.show()
    masked_image = mark_boundaries(temp / 2 + 0.5, mask)
    return masked_image
示例#15
0
def explain(input, img, y, pred_class, img_name):
    # Initiate explainer instance
    explainer = lime_image.LimeImageExplainer()
    # Get the explaination of an image
    explaination = explainer.explain_instance(image=img,
                                              classifier_fn=predict,
                                              segmentation_fn=segmentation)

    # Get processed image
    lime_img, mask = explaination.get_image_and_mask(label=y,
                                                     positive_only=False,
                                                     hide_rest=False,
                                                     num_features=5,
                                                     min_weight=0.0)

    # save the image
    lime_img = lime_img.astype('float32')
    lime_img = lime_img / 255
    # y:real wolf == 1; pred_class: guess wolf == 1;
    plt.imsave(f'./explanation/{argv[1]}/{img_name[:9]}_{y}{pred_class}.jpg',
               lime_img)
    # plt.imsave(f'./explanation/random/{clas}_{y}_{img_name[:9]}_mark.jpg', mark_boundaries(lime_img, mask))
    # plt.imsave(f'test.jpg', lime_img)

    pred = model.predict(input)[0][0]
    if float(pred) > 0.99 or float(pred) < 0.01:
        plt.imsave(
            f'./explanation/{argv[1]}_verysure/{img_name[:9]}_{y}{pred_class}.jpg',
            lime_img)
示例#16
0
def plotLime(output_path, model, train_x, train_y):
    def segmentation(data):
        return slic(data, n_segments=100, compactness=1, sigma=1)

    images, labels = [
        train_x[83][..., ::-1], train_x[4218], train_x[4707][..., ::-1],
        train_x[8598][..., ::-1]
    ], train_y[[83, 4218, 4707, 8598]]
    fig, axs = plt.subplots(1, 4)

    for idx, (image, label) in enumerate(zip(images, labels)):
        explainer = lime_image.LimeImageExplainer()
        explaination = explainer.explain_instance(
            image=image,
            hide_color=None,
            classifier_fn=model.predict_on_batch,
            segmentation_fn=segmentation)
        lime_img, mask = explaination.get_image_and_mask(
            label=explaination.top_labels[0],
            positive_only=False,
            hide_rest=False,
            num_features=11,
            min_weight=0.05)
        axs[idx].imshow(lime_img[..., ::-1] if idx == 1 else lime_img)

    plt.savefig(os.path.join(output_path, '5-3.png'))
    plt.close()
示例#17
0
    def plot_task_3(self):
        def predict_fn(image):
            image = torch.from_numpy(image[:, :, :, 0]).unsqueeze(1)
            pred = self.model(image)
            pred = pred.squeeze().detach().numpy()
            return pred

        def segmentation_fn(image):
            image = image.astype(np.float64)
            segments = slic(image, n_segments=100, compactness=10)
            return segments

        explainer = lime_image.LimeImageExplainer()

        for label in range(7):
            image = self.get_image_for_label(label, 0)
            image = image.squeeze().numpy()
            explanation = explainer.explain_instance(
                image,
                classifier_fn=predict_fn,
                top_labels=7,
                num_features=10000,
                segmentation_fn=segmentation_fn,
                random_seed=19961004)
            image, mask = explanation.get_image_and_mask(label,
                                                         positive_only=False,
                                                         num_features=5,
                                                         hide_rest=False)

            plt.imshow(image)
            #plt.show()
            plt.savefig(os.path.join(self.output, 'fig3_{}.jpg'.format(label)))
            plt.close()
示例#18
0
def explain(model, img, topLabels, numSamples, numFeatures, hideRest, hideColor, positiveOnly):
            
    img, oldImg = transform_img_fn(img)
    img = img*(1./255)
    prediction = model.predict(img)
    explainer = lime_image.LimeImageExplainer()
    img = np.squeeze(img)
    explanation = explainer.explain_instance(img, model.predict, top_labels=topLabels, hide_color=hideColor, num_samples=numSamples)
    temp, mask = explanation.get_image_and_mask(getTopPrediction(prediction[0]), positive_only=positiveOnly, num_features=numFeatures, hide_rest=hideRest)
    tempMask = mask * 255
    temp = Image.fromarray(np.uint8(tempMask))
    temp = temp.resize((oldImg.width, oldImg.height))
    temp = image.img_to_array(temp)
    temp = temp * 1./255
    temp = temp.astype(np.int64)
    temp = np.squeeze(temp)
    oldImgArr = image.img_to_array(oldImg)
    oldImgArr = oldImgArr * (1./255)
    oldImgArr = oldImgArr.astype(np.float64)
    imgExplained = mark_boundaries(oldImgArr, temp)
    imgFinal = np.uint8(imgExplained*255)
    img = Image.fromarray(imgFinal)
    imgByteArr = io.BytesIO()
    img.save(imgByteArr, format='JPEG')
    imgByteArr = imgByteArr.getvalue()


    return imgByteArr
示例#19
0
def lime_exp():
    print "============= LIME STARTING ============= \n"

    explainer = lime_image.LimeImageExplainer()
    # print "imgset length: ", len(imgset), '\n'

    # Hide color is the color for a superpixel turned OFF. Alternatively, if
    # it is NONE, the superpixel will be replaced by the average of its pixels

    # explain_instance(image, classifier_fn, labels=(1, ), hide_color=None, top_labels=5, num_features=100000, num_samples=1000, batch_size=10, qs_kernel_size=4, distance_metric='cosine', model_regressor=None)
    # for i in range(50):
    #    lime_img = np.reshape(imgset[i], (1, 84, 84, 3))
    # print "imgset 0: ", imgset[0]
    preds = get_pd(imgset[0], predictor)
    explanation = explainer.explain_instance(nontrans_imgset[0],
                                             preds,
                                             actset[0],
                                             hide_color=0,
                                             num_samples=1000)

    temp, mask = explanation.get_image_and_mask(295,
                                                positive_only=True,
                                                num_features=5,
                                                hide_rest=True)
    fig = (mark_boundaries(temp / 2 + 0.5, mask))
    plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
    fig.savefig("limeoutput.png")
示例#20
0
def LIME_explainer(model, path, preprocess_fn):
    """
    This function takes in a trained model and a path to an image and outputs 5
    visual explanations using the LIME model
    """
    def image_and_mask(title,
                       positive_only=True,
                       num_features=5,
                       hide_rest=True):
        temp, mask = explanation.get_image_and_mask(
            explanation.top_labels[0],
            positive_only=positive_only,
            num_features=num_features,
            hide_rest=hide_rest)
        plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
        plt.title(title)
        plt.show()

    image = imread(path)
    if len(image.shape) == 2:
        image = np.stack([image, image, image], axis=-1)
    image = preprocess_fn(image)
    image = resize(image, (hp.img_size, hp.img_size, 3))

    explainer = lime_image.LimeImageExplainer()

    explanation = explainer.explain_instance(image.astype('double'),
                                             model.predict,
                                             top_labels=5,
                                             hide_color=0,
                                             num_samples=1000)

    # The top 5 superpixels that are most positive towards the class with the
    # rest of the image hidden
    image_and_mask("Top 5 superpixels",
                   positive_only=True,
                   num_features=5,
                   hide_rest=True)

    # The top 5 superpixels with the rest of the image present
    image_and_mask("Top 5 with the rest of the image present",
                   positive_only=True,
                   num_features=5,
                   hide_rest=False)

    # The 'pros and cons' (pros in green, cons in red)
    image_and_mask("Pros(green) and Cons(red)",
                   positive_only=False,
                   num_features=10,
                   hide_rest=False)

    # Select the same class explained on the figures above.
    ind = explanation.top_labels[0]
    # Map each explanation weight to the corresponding superpixel
    dict_heatmap = dict(explanation.local_exp[ind])
    heatmap = np.vectorize(dict_heatmap.get)(explanation.segments)
    plt.imshow(heatmap, cmap='RdBu', vmin=-heatmap.max(), vmax=heatmap.max())
    plt.colorbar()
    plt.title("Map each explanation weight to the corresponding superpixel")
    plt.show()
示例#21
0
def limeify(image_to_explain, trained_pipeline, class_names):
    logger.info("Start a LIME explanation")
    lime_image_probabilities = trained_pipeline.predict(np.array([image_to_explain]))[0]
    image_probabilities = tuple(zip(class_names, lime_image_probabilities))
    logger.info(
        "Models predicted probabilities for image:\n" + pformat(image_probabilities)
    )
    plt.imshow(image_to_explain)
    plt.show()
    explainer = lime_image.LimeImageExplainer()
    segmenter = SegmentationAlgorithm(
        "quickshift", kernel_size=1, max_dist=200, ratio=0.2
    )
    explanation = explainer.explain_instance(
        image_to_explain,
        trained_pipeline.predict,
        top_labels=10,
        num_samples=1000,
        segmentation_fn=segmenter,
    )
    logger.info("Done with a LIME")
    temp, mask = explanation.get_image_and_mask(
        explanation.top_labels[0], positive_only=False, num_features=5, hide_rest=False
    )
    plt.imshow(mark_boundaries(temp, mask))
    plt.show()
示例#22
0
    def explain(self, model, image_path):
        image = prepare_for_prediction(model, image_path, expand_dims=False)

        label_onehot = model.predict(tf.expand_dims(image, 0))[0]
        label = tf.math.argmax(label_onehot)

        image = tf.cast(image, dtype=tf.float64)
        explainer = lime_image.LimeImageExplainer()
        explanation = explainer.explain_instance(image.numpy(),
                                                 model.predict,
                                                 top_labels=5,
                                                 hide_color=0,
                                                 num_samples=500)
        temp, mask = explanation.get_image_and_mask(label.numpy(),
                                                    positive_only=True,
                                                    num_features=5,
                                                    hide_rest=True)

        result_image = mark_boundaries(temp / 2 + 0.5, mask)
        result_image = tf.image.convert_image_dtype(result_image,
                                                    dtype=tf.uint8,
                                                    saturate=True)

        image_base46 = get_base64png(result_image)
        result = {"label_id": float(label), "image": image_base46}
        return result
示例#23
0
    def estimate_gradient(self, x_tensor, target_model):
        """
        Estimate the gradient of x_tensor on the target_model
        :param x_tensor:
        :param target_model:
        :return: Estimated W, Sample Radius, Samples
        """
        def predict_fun(x):
            # The original gray image is change to RGB image by LIME by default.
            # To use the original classifier, we need to remove the channels added by LIME
            x = torch.tensor(x[:, :, :, 0], device=config.DEVICE, dtype=torch.float64).view(-1, self.var_num)
            rst = target_model.predicts(x).cpu().numpy()  # Output 1 * 2 array
            return rst

        x_tensor = x_tensor.view(self.img_size, self.img_size)
        explainer = lime_image.LimeImageExplainer(feature_selection='none')
        explanation = explainer.explain_instance(x_tensor.cpu().numpy(), predict_fun, top_labels=None, hide_color=0,
                                                 num_samples=self.var_num + 2, num_features=self.var_num,
                                                 segmentation_fn=self.segmenter, labels=(0, 1))

        self.py = explanation.local_pred
        # We only consider the weights related to positive labels
        w_lime = sorted(explanation.local_exp[1], key=lambda i: i[0])
        w_lime = torch.tensor([v for _, v in w_lime], dtype=torch.float64, device=config.DEVICE).unsqueeze(0)
        return w_lime
示例#24
0
def explain_multiple(model, img, topLabels, numSamples, numFeatures, hideRest,
                     hideColor, positiveOnly):
    img, oldImg = transform_img_fn(img)
    img = img * (1. / 255)
    prediction = model.predict(img)
    explainer = lime_image.LimeImageExplainer()
    img = np.squeeze(img)
    explanation = explainer.explain_instance(img,
                                             model.predict,
                                             top_labels=topLabels,
                                             hide_color=hideColor,
                                             num_samples=numSamples)

    topClasses = getTopXpredictions(prediction, topLabels)

    explanations = []

    for cl in topClasses:

        temp, mask = explanation.get_image_and_mask(cl[0],
                                                    positive_only=positiveOnly,
                                                    num_features=numFeatures,
                                                    hide_rest=hideRest)
        imgExplained = mark_boundaries(temp, mask)
        img = Image.fromarray(np.uint8(imgExplained * 255))
        imgByteArr = io.BytesIO()
        img.save(imgByteArr, format='JPEG')
        imgByteArr = imgByteArr.getvalue()

        explanations.append((cl[0], cl[1], imgByteArr))

    return (explanations, len(explanations))
示例#25
0
def explain(modelPath, img):

    modelPath = "C:/Users/Alex Heimerl/Desktop/test/vgg16_pokemon_100.h5"

    model = load_model(modelPath)

    img = img / 255

    prediction = model.predict(img)

    explainer = lime_image.LimeImageExplainer()

    explanation = explainer.explain_instance(np.squeeze(img),
                                             model.predict,
                                             top_labels=2,
                                             hide_color=0,
                                             num_samples=1000)
    temp, mask = explanation.get_image_and_mask(getTopPrediction(
        prediction[0]),
                                                positive_only=True,
                                                num_features=50,
                                                hide_rest=True)
    imgExplained = mark_boundaries(temp, mask)

    img = Image.fromarray(np.uint8(imgExplained * 255))

    imgByteArr = io.BytesIO()
    img.save(imgByteArr, format='JPEG')
    imgByteArr = imgByteArr.getvalue()

    return imgByteArr
示例#26
0
def inception():

    images = transform_img_fn_inception(
        [r'C:/Users/Alex Heimerl/Desktop/nova/Scripts/Capture1.jpg'])

    inet_model = VGG16(weights='imagenet', include_top=False)
    x = inet_model.output
    x = GlobalAveragePooling2D()(x)
    # let's add a fully-connected layer
    x = Dense(1024, activation='relu')(x)
    # and a logistic layer -- let's say we have 200 classes
    predictions = Dense(2, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=inet_model.input, outputs=predictions)
    model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
    preds = model.predict(images)
    print(preds)

    explainer = lime_image.LimeImageExplainer()

    # Hide color is the color for a superpixel turned OFF. Alternatively, if it is NONE, the superpixel will be replaced by the average of its pixels
    explanation = explainer.explain_instance(images[0] * 1. / 255,
                                             model.predict,
                                             top_labels=5,
                                             hide_color=0,
                                             num_samples=100)
    temp, mask = explanation.get_image_and_mask(1,
                                                positive_only=True,
                                                num_features=10,
                                                hide_rest=True)
    img_exp = mark_boundaries(temp, mask)
    plt.imshow(img_exp)
    plt.show()
示例#27
0
def call_lime(predit_data, i=0):
    #
    class_names = sorted(predit_data.class_indices.items(),
                         key=lambda pair: pair[1])
    class_names = np.array([key.title() for key, value in class_names])
    #
    final_model = combine_model()

    # print prediction result
    predicted_batch = final_model.predict(predit_data)
    predicted_id = np.argmax(predicted_batch, axis=-1)
    label_id = np.argmax(label_batch, axis=-1)
    print("Actual Style is: ", class_names[label_id[i]])
    for j in range(0, 7, 1):
        print("Prediction for ", class_names[j], ": ", predicted_batch[i, j])
#
    explainer = lime_image.LimeImageExplainer()
    explanation = explainer.explain_instance(image_batch[i],
                                             final_model.predict,
                                             top_labels=5,
                                             hide_color=0,
                                             num_samples=1000)
    # show the top class
    temp, mask = explanation.get_image_and_mask(explanation.top_labels[0],
                                                positive_only=True,
                                                num_features=5,
                                                hide_rest=False)
    plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
    color = "green" if predicted_id[i] == label_id[i] else "red"
    plt.title(class_names[label_id[i]], color=color)
    plt.suptitle("Model predictions (green: correct, red: incorrect)")
    #    plt.show()
    #
    return explanation
示例#28
0
def datscan_explain(datscan_sample):
    # Some imports
    import keras
    from keras.models import load_model
    from keras.applications.vgg16 import VGG16, preprocess_input
    import tensorflow as tf
    import numpy as np
    from numpy import asarray
    import cv2
    import math
    import os.path
    IMG_SIZE = (224, 224)

    basename = os.path.basename(datscan_sample)
    save_path = './static/images/explanations/' + str(basename)
    vgg16 = load_model('./notebooks/spect_trained_final_1.h5')

    X = []
    img = cv2.imread(datscan_sample)
    #img = cv2.imread('60.jpg')
    X.append(img)
    X = np.array(X)

    scan_sample = preprocess_imgs(set_name=X, img_size=IMG_SIZE)

    #Import LIME
    import lime
    from lime import lime_image
    from skimage.segmentation import mark_boundaries
    import matplotlib.pyplot as plt

    # create lime ImageExplainer
    explainer = lime_image.LimeImageExplainer()

    # Choosing the image from X_test_prep set
    image = scan_sample[0].astype(np.uint8)

    # Apply LIME to the image
    print("APPLYING LIME, THIS IS A CPU INTENSIVE PROCESS AND WILL TAKE TIME")
    explanation = explainer.explain_instance(image,
                                             vgg16.predict,
                                             top_labels=2,
                                             hide_color=0,
                                             num_samples=1000)

    #Arguments for get_image_and_mask() method defined below

    temp, mask = explanation.get_image_and_mask(0,
                                                positive_only=True,
                                                num_features=5,
                                                hide_rest=False)

    # Alert! You need to delete the explained.jpg file everytime you want to run an aexplanation on a new image.
    # Or else you will get a file overwrite error. Need to think of a fix for this.
    plt.imshow(mark_boundaries(temp / 2 + 0.5, mask).astype(np.uint8))
    plt.imsave(save_path,
               mark_boundaries(temp / 2 + 0.5, mask).astype(np.uint8))

    return save_path
 def analyzeIoU(self,
                model,
                x_train_batch,
                y_train_batch,
                shapely_polygons_batch,
                num_accurate=10,
                num_inaccurate=10,
                good=0.7,
                bad=0.3,
                limit=30):
     """Calculates and finds images with most accurate/inaccurate IoU
     
     Args:
         x_train_batch (np.array): array of training images
         y_train_batch (np.array): array of labels (labels are 1-hot vectors)
         shapely_polygons_batch (np.array): array of shapely polygons for each images' obj seg
         num_accurate (int): number of 'accurate' segmentations to find
         num_inaccurate (int): number of 'inaccurate' segmentations to find
         good (float): minimal IoU score to be considered an 'accurate' segmentation
         bad (float): maximal IoU score to be considered an 'inaccurate' segmentation
         limit (int): number of segmentations to consider and calcIoU for
     
     Returns:
         info (array): contains (i, expl) tuples for IoU accepted as accurate or inaccurate
                           i is the index of the image in x_train_batch
                           expl is the explanation created on the corresponding ith image
     """
     x = len(x_train_batch)
     assert len(y_train_batch) == x
     assert len(shapely_polygons_batch) == x
     i, acc, not_acc = 0, 0, 0
     info = []  # contains tuples (i, expl)
     while acc < num_accurate and not_acc < num_inaccurate or i < x:
         explainer = lime_image.LimeImageExplainer()
         s = time.time()
         expl = explainer.explain_instance(x_train_batch[i],
                                           model.predict,
                                           top_labels=1,
                                           hide_color=0,
                                           num_samples=100,
                                           timed=True,
                                           batch_size=128,
                                           use_bandits=self.use_bandits)
         diff = time.time() - s
         s = time.time()
         score = calcIoU(shapely_polygons_batch[i], explainer.segments,
                         explainer.features_to_use)
         print("Score: {}".format(score))
         print("Explanation time: {}".format(diff))
         print("Score time: {}".format(time.time() - s))
         if scores <= bad:
             not_acc += 1
             info.append((i, expl))
         elif scores >= good:
             acc += 1
             info.append((i, expl))
         i += 1
     return info
示例#30
0
    def explain(self, image, target_class):
        explainer = lime_image.LimeImageExplainer()
        explanation = explainer.explain_instance(
            image, self.model.predict, top_labels=None,
            labels=(target_class,), num_samples=1000)
        temp, mask = explanation.get_image_and_mask(
            target_class, positive_only=False, num_features=10, hide_rest=True)

        return (mask == 2).astype(int), (mask == 1).astype(int)