<jupyter_start><jupyter_text><jupyter_code>!pip install lime
import os
import keras
from keras.applications import inception_v3 as inc_net
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions
from skimage.io import imread
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
print('Notebook run using keras:', keras.__version__)<jupyter_output>Using TensorFlow backend.<jupyter_text>Inception<jupyter_code>#inet_model = inc_net.InceptionV3()
inet_model = inc_net.InceptionV3(include_top=True, weights='imagenet', 
                    input_tensor=None, input_shape=None, pooling=None, classes=1000)
def transform_img_fn(path_list):
    out = []
    for img_path in path_list:
        img = image.load_img(img_path, target_size=(299, 299))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = inc_net.preprocess_input(x)
        out.append(x)
    return np.vstack(out)
images = transform_img_fn([os.path.join('','American_Black_Bear.jpg')])
# I'm dividing by 2 and adding 0.5 because of how this Inception represents images
plt.imshow(images[0] / 2 + 0.5)
preds = inet_model.predict(images)
for x in decode_predictions(preds)[0]:
    print(x)<jupyter_output>('n02133161', 'American_black_bear', 0.81433386)
('n02132136', 'brown_bear', 0.029971713)
('n02134418', 'sloth_bear', 0.017064571)
('n02134084', 'ice_bear', 0.011263809)
('n03532672', 'hook', 0.0026253534)<jupyter_text>Evaluation<jupyter_code>%load_ext autoreload
%autoreload 2
import os,sys
try:
    import lime
except:
    sys.path.append(os.path.join('..', '..')) # add the current directory
    import lime
from lime import lime_image
explainer = lime_image.LimeImageExplainer()
%%time
# 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], inet_model.predict, top_labels=5, hide_color=0, num_samples=1000)
from skimage.segmentation import mark_boundaries
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], 
                                            positive_only=True, num_features=5, hide_rest=True)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
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))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10768012668250197, 'fraction_numerical': 0.05146476642913698, 'mean_word_length': 4.678651685393258, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '295823', 'license': 'Apache-2.0', 'n_tokens_mistral': 951, 'n_tokens_neox': 850, 'n_words': 190, 'path': '1_LIME_ObjectDetection.ipynb', 'repo_name': 'mohan-mj/AI-Workshop', 'size': '2526'}