Exemple #1
0
def visualize_multiple_categories():
    """Example to show how to visualize images that activate multiple categories
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    # Visualize [20] (ouzel) and [20, 71] (An ouzel-scorpion :D)
    indices = [20, [20, 71]]
    images = [
        visualize_activation(model,
                             layer_idx,
                             filter_indices=idx,
                             text=utils.get_imagenet_label(idx),
                             max_iter=500) for idx in indices
    ]
    cv2.imshow('Multiple category visualization', utils.stitch_images(images))
    cv2.waitKey(0)
Exemple #2
0
def generate_cam(show=True):
    """Generates a heatmap via grad-CAM method.
    First, the class prediction is determined, then we generate heatmap to visualize that class.
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    for path in [
            'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Tigerwater_edit2.jpg/170px-Tigerwater_edit2.jpg'
    ]:
        seed_img = utils.load_img(path, target_size=(224, 224))

        # Convert to BGR, create input with batch_size: 1, and predict.
        bgr_img = utils.bgr2rgb(seed_img)
        img_input = np.expand_dims(img_to_array(bgr_img), axis=0)
        pred_class = np.argmax(model.predict(img_input))

        heatmap = visualize_cam(model, layer_idx, [pred_class], seed_img)
        if show:
            plt.axis('off')
            plt.imshow(heatmap)
            plt.title('Attention - {}'.format(
                utils.get_imagenet_label(pred_class)))
            plt.show()
Exemple #3
0
def visualize_multiple_same_filter(num_runs=3):
    """Example to show how to visualize same filter multiple times via different runs.

    Args:
        num_runs: The number of times the same filter is visualized
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    # 20 is the imagenet category for 'ouzel'
    indices = [20] * num_runs
    images = [
        visualize_activation(model,
                             layer_idx,
                             filter_indices=idx,
                             text=utils.get_imagenet_label(idx),
                             max_iter=500) for idx in indices
    ]
    cv2.imshow('Multiple runs of ouzel', utils.stitch_images(images))
    cv2.waitKey(0)
def visualize_multiple_categories(show=True):
    """Example to show how to visualize images that activate multiple categories
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    # Visualize [20] (ouzel) and [20, 71] (An ouzel-scorpion :D)
    indices = [20, [20, 71]]
    images = []
    for idx in indices:
        img = visualize_class_activation(model,
                                         layer_idx,
                                         filter_indices=idx,
                                         max_iter=500)
        img = utils.draw_text(img, utils.get_imagenet_label(idx))
        images.append(img)

    # Easily stitch images via `utils.stitch_images`
    stitched = utils.stitch_images(images)
    if show:
        plt.axis('off')
        plt.imshow(stitched)
        plt.title('Multiple category visualization')
        plt.show()
Exemple #5
0
def generate_saliceny_map(show=True):
    """Generates a heatmap indicating the pixels that contributed the most towards
    maximizing the filter output. First, the class prediction is determined, then we generate heatmap
    to visualize that class.
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    for path in ['../resources/ouzel.jpg', '../resources/ouzel_1.jpg']:
        seed_img = utils.load_img(path, target_size=(224, 224))
        pred_class = np.argmax(
            model.predict(np.array([img_to_array(seed_img)])))
        heatmap = visualize_saliency(model, layer_idx, [pred_class], seed_img)

        if show:
            cv2.imshow(
                'Saliency - {}'.format(utils.get_imagenet_label(pred_class)),
                heatmap)
            cv2.waitKey(0)
Exemple #6
0
def generate_saliceny_map(show=True):
    """Generates a heatmap indicating the pixels that contributed the most towards
    maximizing the filter output. First, the class prediction is determined, then we generate heatmap
    to visualize that class.
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    for path in ['../resources/ouzel.jpg', '../resources/ouzel_1.jpg']:
        seed_img = utils.load_img(path, target_size=(224, 224))

        # Convert to BGR, create input with batch_size: 1, and predict.
        bgr_img = utils.bgr2rgb(seed_img)
        img_input = np.expand_dims(img_to_array(bgr_img), axis=0)
        pred_class = np.argmax(model.predict(img_input))

        heatmap = visualize_saliency(model, layer_idx, [pred_class], seed_img)
        if show:
            plt.axis('off')
            plt.imshow(heatmap)
            plt.title('Saliency - {}'.format(
                utils.get_imagenet_label(pred_class)))
            plt.show()
Exemple #7
0
def generate_cam(show=True):
    """Generates a heatmap via grad-CAM method.
    First, the class prediction is determined, then we generate heatmap to visualize that class.
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    for path in [
            'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Tigerwater_edit2.jpg/170px-Tigerwater_edit2.jpg'
    ]:
        seed_img = utils.load_img(path, target_size=(224, 224))
        pred_class = np.argmax(
            model.predict(np.array([img_to_array(seed_img)])))
        heatmap = visualize_cam(model, layer_idx, [pred_class], seed_img)

        if show:
            cv2.imshow(
                'Attention - {}'.format(utils.get_imagenet_label(pred_class)),
                heatmap)
            cv2.waitKey(0)
Exemple #8
0
def visualize_multiple_same_filter(num_runs=3, show=True):
    """Example to show how to visualize same filter multiple times via different runs.

    Args:
        num_runs: The number of times the same filter is visualized
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [idx for idx, layer in enumerate(model.layers) if layer.name == layer_name][0]

    # 20 is the imagenet category for 'ouzel'
    indices = [20] * num_runs
    images = []
    for idx in indices:
        img = visualize_activation(model, layer_idx, filter_indices=idx, max_iter=500)
        img = utils.draw_text(img, utils.get_imagenet_label(idx))
        images.append(img)

    # Easily stitch images via `utils.stitch_images`
    stitched = utils.stitch_images(images)
    if show:
        plt.axis('off')
        plt.imshow(stitched)
        plt.title('Multiple runs of ouzel')
        plt.show()
Exemple #9
0
def visualize_random(num_categories=10, show=True):
    """Example to show how to visualize multiple filters via activation maximization.

    Args:
        num_categories: The number of random categories to visualize. (Default Value = 5)
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [idx for idx, layer in enumerate(model.layers) if layer.name == layer_name][0]

    # Visualize couple random categories from imagenet.
    indices = np.random.permutation(1000)[:num_categories]
    images = []
    for idx in indices:
        img = visualize_activation(model, layer_idx, filter_indices=idx, max_iter=500)
        img = utils.draw_text(img, utils.get_imagenet_label(idx))
        images.append(img)

    # Easily stitch images via `utils.stitch_images`
    stitched = utils.stitch_images(images)
    if show:
        plt.axis('off')
        plt.imshow(stitched)
        plt.title('Random imagenet categories')
        plt.show()
Exemple #10
0
def visualize_random(num_categories=10):
    """Example to show how to visualize multiple filters via activation maximization.

    Args:
        num_categories: The number of random categories to visualize. (Default Value = 5)
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    # Visualize couple random categories from imagenet.
    indices = np.random.permutation(1000)[:num_categories]
    images = [
        visualize_activation(model,
                             layer_idx,
                             filter_indices=idx,
                             text=utils.get_imagenet_label(idx),
                             max_iter=500) for idx in indices
    ]

    # Easily stitch images via `utils.stitch_images`
    cv2.imshow('Random imagenet categories', utils.stitch_images(images))
    cv2.waitKey(0)
Exemple #11
0
def generate_cam(show=True):
    """Generates a heatmap via grad-CAM method.
    First, the class prediction is determined, then we generate heatmap to visualize that class.
    """
    # # Build the VGG16 network with ImageNet weights
    # model = VGG16(weights='imagenet', include_top=True)
    # print('Model loaded.')

    # Build the InceptionV3 network with ImageNet weights
    # model = InceptionV3(weights='imagenet', include_top=True)
    # print('Model loaded.')

    json_file = open('inception_v3_tf_cat_dog_top_layer.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
    # load weights into new model
    # loaded_model.load_weights("vgg16_tf_cat_dog_final_dense2.h5")
    loaded_model.load_weights("inception_v3_tf_cat_dog_top_layer.h5")
    print("Loaded model from disk")

    model = loaded_model

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_idx = [
        idx for idx, layer in enumerate(model.layers)
        if layer.name == layer_name
    ][0]

    for path in [
            '../resources/tiger.jpg', '../resources/ouzel.jpg',
            '../resources/ouzel_1.jpg'
    ]:
        # seed_img = utils.load_img(path, target_size=(224, 224))
        seed_img = utils.load_img(path, target_size=(299, 299))

        # Convert to BGR, create input with batch_size: 1, and predict.
        bgr_img = utils.bgr2rgb(seed_img)
        img_input = np.expand_dims(img_to_array(bgr_img), axis=0)
        pred_class = np.argmax(model.predict(img_input))

        heatmap = visualize_cam(model, layer_idx, [pred_class], seed_img)
        if show:
            plt.axis('off')
            plt.imshow(heatmap)
            plt.title('Attention - {}'.format(
                utils.get_imagenet_label(pred_class)))
            plt.show()
Exemple #12
0
def visualize_multiple_categories():
    """Example to show how to visualize images that activate multiple categories
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])

    # Visualize [20] (ouzel) and [20, 71] (An ouzel-scorpion :D)
    indices = [20, [20, 71]]
    idx_label_map = dict((idx, utils.get_imagenet_label(idx)) for idx in [20, 71])

    vis_img = visualize_activation(model.input, layer_dict[layer_name], max_iter=500,
                                   filter_indices=indices, idx_label_map=idx_label_map)
    cv2.imshow('Multiple category visualization', vis_img)
    cv2.waitKey(0)
Exemple #13
0
def visualize_random():
    """Example to show how to visualize multiple filters via activation maximization
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])

    # Visualize couple random categories from imagenet.
    indices = np.random.permutation(1000)[:15]
    idx_label_map = dict((idx, utils.get_imagenet_label(idx)) for idx in indices)

    vis_img = visualize_activation(model.input, layer_dict[layer_name], max_iter=500,
                                   filter_indices=indices, idx_label_map=idx_label_map)
    cv2.imshow('Random imagenet output categories', vis_img)
    cv2.waitKey(0)
Exemple #14
0
def visualize_multiple_same_filter():
    """Example to show how to visualize same filter multiple times via different runs.
    """
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    print('Model loaded.')

    # The name of the layer we want to visualize
    # (see model definition in vggnet.py)
    layer_name = 'predictions'
    layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])

    # 20 is the imagenet category for 'ouzel'
    indices = [20, 20, 20]
    idx_label_map = dict((idx, utils.get_imagenet_label(idx)) for idx in indices)

    vis_img = visualize_activation(model.input, layer_dict[layer_name], max_iter=500,
                                   filter_indices=indices, idx_label_map=idx_label_map)
    cv2.imshow('Multiple runs of ouzel', vis_img)
    cv2.waitKey(0)
Exemple #15
0
from vis.utils import utils
from keras import activations
from vis.visualization import visualize_activation
from matplotlib import pyplot as plt
from vis.input_modifiers import Jitter
import numpy as np
import os
import cv2

categorias = np.random.permutation(1000)[:15]

model = VGG16(weights='imagenet', include_top=True)
layer_idx = utils.find_layer_idx(
    model, 'predictions')  # -1 tambien vale (es la ultima)

#Softmax a linear

model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)

if not os.path.isdir("fc"):
    os.mkdir("fc")

for categoria in categorias:
    img = visualize_activation(model,
                               layer_idx,
                               filter_indices=categoria,
                               max_iter=500,
                               input_modifiers=[Jitter(16)])
    img = utils.draw_text(img, utils.get_imagenet_label(categoria))
    cv2.imwrite("fc/" + utils.get_imagenet_label(categoria) + ".png", img)
layer_name = 'predictions'
layer_idx = [
    idx for idx, layer in enumerate(model.layers) if layer.name == layer_name
][0]

# Images corresponding to tiger, penguin, dumbbell, speedboat, spider
image_paths = [
    "http://www.tigerfdn.com/wp-content/uploads/2016/05/How-Much-Does-A-Tiger-Weigh.jpg",
    "http://www.slate.com/content/dam/slate/articles/health_and_science/wild_things/2013/10/131025_WILD_AdeliePenguin.jpg.CROP.promo-mediumlarge.jpg",
    "https://www.kshs.org/cool2/graphics/dumbbell1lg.jpg",
    "http://tampaspeedboatadventures.com/wp-content/uploads/2010/10/DSC07011.jpg",
    "http://ichef-1.bbci.co.uk/news/660/cpsprodpb/1C24/production/_85540270_85540265.jpg"
]

heatmaps = []
for path in image_paths:
    # Predict the corresponding class for use in `visualize_saliency`.
    seed_img = utils.load_img(path, target_size=(224, 224))
    pred_class = np.argmax(model.predict(np.array([img_to_array(seed_img)])))

    # Here we are asking it to show attention such that prob of `pred_class`
    # is maximized.
    heatmap = visualize_saliency(model,
                                 layer_idx, [pred_class],
                                 seed_img,
                                 text=utils.get_imagenet_label(pred_class))
    heatmaps.append(heatmap)

cv2.imshow("Saliency map", utils.stitch_images(heatmaps))
cv2.waitKey(0)
Exemple #17
0
    # seed_img = utils.load_img(path, target_size=(224, 224))
    # pred_class = np.argmax(model.predict(np.array([img_to_array(seed_img)])))
    # print(utils.get_imagenet_label(pred_class))
    
    
    # seed_img = utils.load_img(path, target_size=(224, 224))
    # pred_class = np.argmax(model.predict(np.array([img_to_array(seed_img)])))
    
    print('Predicted:', decode_predictions(pred))
    print('Predicted:', decode_predictions(pred)[0][0][1])
    
    # pred_class = np.argmax(model.predict(preprocess_input(np.array([img_to_array(seed_img)]))))

    # Here we are asking it to show attention such that prob of `pred_class` is maximized.
    # heatmap = visualize_saliency(model, layer_idx, [pred_class], seed_img, text=utils.get_imagenet_label(pred_class))
    heatmap = visualize_cam(model, layer_idx, [pred_class], seed_img, text=utils.get_imagenet_label(pred_class))
    heatmaps.append(heatmap)
    
    # Generate three different images of the same output index.
    # vis_images = [visualize_activation(model, layer_idx, filter_indices=idx, text=str(idx), max_iter=500) for idx in [294, 294, 294]]
    # vis_images.append(vis_image)

name = "Gradient-based Localization map"
cv2.imshow(name, utils.stitch_images(heatmaps))
cv2.waitKey(-1)
cv2.destroyWindow(name)


# name = "Visualizations » Dense Layers"
# cv2.imshow(name, utils.stitch_images(vis_images))
# cv2.waitKey(-1)
    # seed_img = utils.load_img(path, target_size=(224, 224))
    # pred_class = np.argmax(model.predict(np.array([img_to_array(seed_img)])))
    # print(pred)

    # print('Predicted:', decode_predictions(pred))
    # print('Predicted:', decode_predictions(pred)[0][0][1])

    # print('Predicted:', decode_predictions_vader_yoda(pred))
    # print('Predicted:', decode_predictions_vader_yoda(pred)[0][0][1])

    # pred_class_vgg = np.argmax(model_vgg.predict(preprocess_input(np.array([img_to_array(seed_img)]))))
    pred_class_vgg = np.argmax(
        model_vgg.predict(np.array([img_to_array(seed_img)])))
    print("vgg16:")
    print(pred_class_vgg)
    print(utils.get_imagenet_label(pred_class_vgg))

    # Here we are asking it to show attention such that prob of `pred_class` is maximized.
    # visualize_saliency
    # heatmap = visualize_saliency(model, layer_idx, [pred_class], seed_img, text=("Custom: %s" % get_cat_dog_label(pred_class)))
    # heatmap_vgg = visualize_saliency(model_vgg, vgg_layer_idx, [pred_class_vgg], seed_img, text=("VGG16: %s" % utils.get_imagenet_label(pred_class_vgg)))
    heatmap = visualize_cam(model,
                            layer_idx, [pred_class],
                            seed_img,
                            text=("Custom: %s" %
                                  get_cat_dog_label(pred_class)))
    heatmap_vgg = visualize_cam(
        model_vgg,
        vgg_layer_idx, [pred_class_vgg],
        seed_img,
        text=("VGG16: %s" % utils.get_imagenet_label(pred_class_vgg)))