Пример #1
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()
Пример #2
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()
Пример #3
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()
Пример #4
0
def visualize_conv_filters(output_dir, model, layer_name):
  layer_idx = utilsvis.find_layer_idx(model, layer_name)

  filters = np.arange(get_num_filters(model.layers[layer_idx]))
  vis_images = []

  for idx in tqdm(filters):
      img = visualize_activation(model, layer_idx, filter_indices=idx)
      img = utilsvis.draw_text(img, 'Filter {}'.format(idx))    
      vis_images.append(img)

  stitched = utilsvis.stitch_images(vis_images, cols=32)    

  path = os.path.join(output_dir, '{}.png')
  imsave(path, stitched)
Пример #5
0
def visualiseDenseLayer(model,
                        layer_name,
                        output_classes,
                        verbose,
                        save=False):
    '''
    Makes a plot for visualising  the activations of a dense layer.
    :param model:  The model to visualise.
    :param layer_name: The name of the dense layer to visualise.
    :param output_classes: The number of activations in the layer.
    :param verbose: Print statements of progress.
    :return: N/A
    '''

    layer_index = utils.find_layer_idx(model, layer_name)
    model.layers[layer_index].activation = activations.linear
    model = utils.apply_modifications(model)

    vis_images = []
    for filter_index in range(0, output_classes):
        if (verbose):
            print("Preparing Visualisation for class {} in layer {}".format(
                filter_index, layer_name))
        visualisation = visualize_activation(model,
                                             layer_index,
                                             filter_index,
                                             max_iter=500,
                                             input_modifiers=[Jitter(16)])

        img = utils.draw_text(visualisation, 'Class {}'.format(filter_index))
        vis_images.append(img)

    stitched = utils.stitch_images(vis_images, cols=4)

    if (save):
        matplotlib.image.imsave("../SavedData/" + save, stitched)
    else:
        plt.imshow(stitched)
        plt.figure()
        plt.axis('off')
        plt.show()
Пример #6
0
    def plot_conv_weights(self):
        PATH = os.path.dirname(__file__)
        cls = _get_conv_layers(self.model)
        i=0
        for layer in cls :
            layer_name = layer.name
            print("{}/{} : {}".format(i,len(cls),layer_name))
            layer_idx = utils.find_layer_idx(self.model,layer_name)

            filters = np.arange(get_num_filters(self.model.layers[layer_idx]))
            vis_images = []
            for idx in filters[:64]:
                img = visualize_activation(self.model, layer_idx, filter_indices=idx)
                # Utility to overlay text on image.
                img = utils.draw_text(img, 'Filter {}'.format(idx))
                vis_images.append(img)
            # Generate stitched image palette with 8 cols.
            stitched = utils.stitch_images(vis_images, cols=8)
            plt.axis('off')
            plt.imsave(join(PATH,'heatmaps/'+layer_name+'.jpg'),stitched)
            i+=1
Пример #7
0
def visualiseCovolutionLayer(model,
                             selected_layer,
                             selected_indices,
                             verbose,
                             save=False):
    '''
    Makes a plot for visualising  the activations of a convolutional layer.
    :param model: The model to visualise.
    :param selected_layer: The name of the dense layer to visualise.
    :param selected_indices: The indices of the filters to visualise.
    :param verbose: Print statements of progress.
    :return: N/A.
    '''

    layer_index = utils.find_layer_idx(model, selected_layer)
    vis_images = []
    for filter_index in selected_indices:
        if (verbose):
            print("Preparing Visualisation for filter {} in layer {}".format(
                filter_index, selected_layer))
        img = visualize_activation(model, layer_index, filter_index)
        # Utility to overlay text on image.
        visualisation = utils.draw_text(
            img, 'Filter {} on layer {}'.format(filter_index, selected_layer))
        vis_images.append(visualisation)

    # Generate stitched image palette with 5 cols so we get 2 rows.
    stitched = utils.stitch_images(vis_images, cols=5)
    plt.figure()
    plt.axis('off')
    plt.imshow(stitched)

    if (save):
        plt.savefig("../SavedData/" + save)
    else:
        plt.show()
Пример #8
0
from vis.visualization import visualize_activation

from keras.models import load_model
from keras.layers import Activation
from scipy.misc import imsave

# Load the fine-tuned plankton model
model = load_model('plankton_model.h5')
# Change output activation from softmax to linear
model.layers[-1].activation = Activation('linear')
print('Model loaded.')

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

# Get dense layer visualizations for all 12 plankton categories
vis_images = []
for idx in range(12):
    img = visualize_activation(model, layer_idx, filter_indices=idx, max_iter=1000 )
    img = utils.draw_text(img, str(idx))
    vis_images.append(img)

stitched = utils.stitch_images(vis_images)   
imsave('class_%d.png' % (idx), stitched)

plt.axis('off')
plt.imshow(stitched)
plt.title(layer_name)
plt.show()
Пример #9
0
img = visualize_activation(model, layer_idx, filter_indices=3)
plt.imshow(img)

idx = 59
# Generate input image for each filter.
new_vis_images = []
for i, idx in enumerate(filters):
    # We will seed with optimized image this time.
    img = visualize_activation(model,
                               layer_idx,
                               filter_indices=idx,
                               seed_input=vis_images[i],
                               input_modifiers=[Jitter(0.05)])

    # Utility to overlay text on image.
    img = utils.draw_text(img, 'Filter {}'.format(idx))
    new_vis_images.append(img)

# Generate stitched image palette with 5 cols so we get 2 rows.
stitched = utils.stitch_images(new_vis_images, cols=5)
plt.figure()
plt.axis('off')
plt.imshow(stitched)
plt.show()

from vis.visualization import get_num_filters

selected_indices = []
for layer_name in ['block_6_conv_3']:
    layer_idx = utils.find_layer_idx(model, layer_name)
Пример #10
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)
Пример #11
0
def visualize_activation(model,
                         layer_idx,
                         filter_indices=None,
                         seed_img=None,
                         text=None,
                         act_max_weight=1,
                         lp_norm_weight=10,
                         tv_weight=10,
                         **optimizer_params):
    """Generates stitched input image(s) over all `filter_indices` in the given `layer` that maximize
    the filter output activation.

    Args:
        model: The `keras.models.Model` instance. Model input is expected to be a 4D image input of shape:
            `(samples, channels, rows, cols)` if data_format='channels_first' or `(samples, rows, cols, channels)` if data_format='channels_last'.
        layer_idx: The layer index within `model.layers` whose filters needs to be visualized.
        filter_indices: filter indices within the layer to be maximized.
            For `keras.layers.Dense` layer, `filter_idx` is interpreted as the output index.

            If you are visualizing final `keras.layers.Dense` layer, you tend to get
            better results with 'linear' activation as opposed to 'softmax'. This is because 'softmax'
            output can be maximized by minimizing scores for other classes.

        filter indices within the layer to be maximized.
            If None, all filters are visualized. (Default value = None)

            An input image is generated for each entry in `filter_indices`. The entry can also be an array.
            For example, `filter_indices = [[1, 2], 3, [4, 5, 6]]` would generate three input images. The first one
            would maximize output of filters 1, 2, 3 jointly. A fun use of this might be to generate a dog-fish
            image by maximizing 'dog' and 'fish' output in final `Dense` layer.

            For `keras.layers.Dense` layers, `filter_idx` is interpreted as the output index.

            If you are visualizing final `keras.layers.Dense` layer, you tend to get
            better results with 'linear' activation as opposed to 'softmax'. This is because 'softmax'
            output can be maximized by minimizing scores for other classes.

        seed_img: Seeds the optimization with a starting image. Initialized with a random value when set to None.
            (Default value = None)
        text: The text to overlay on top of the generated image. (Default Value = None)
        act_max_weight: The weight param for `ActivationMaximization` loss. Not used if 0 or None. (Default value = 1)
        lp_norm_weight: The weight param for `LPNorm` regularization loss. Not used if 0 or None. (Default value = 10)
        tv_weight: The weight param for `TotalVariation` regularization loss. Not used if 0 or None. (Default value = 10)
        optimizer_params: The **kwargs for optimizer [params](vis.optimizer.md##optimizerminimize). Will default to
            reasonable values when required keys are not found.

    Example:
        If you wanted to visualize the input image that would maximize the output index 22, say on
        final `keras.layers.Dense` layer, then, `filter_indices = [22]`, `layer = dense_layer`.

        If `filter_indices = [22, 23]`, then it should generate an input image that shows features of both classes.

    Returns:
        Stitched image output visualizing input images that maximize the filter output(s). (Default value = 10)
    """
    filter_indices = utils.listify(filter_indices)
    print("Working on filters: {}".format(pprint.pformat(filter_indices)))

    # Default optimizer kwargs.
    optimizer_params_default = {
        'seed_img': seed_img,
        'max_iter': 200,
        'verbose': False,
        'image_modifiers': _DEFAULT_IMG_MODIFIERS
    }
    optimizer_params_default.update(optimizer_params)
    optimizer_params = optimizer_params_default

    losses = [(ActivationMaximization(model.layers[layer_idx],
                                      filter_indices), act_max_weight),
              (LPNorm(model.input), lp_norm_weight),
              (TotalVariation(model.input), tv_weight)]

    opt = Optimizer(model.input, losses, norm_grads=False)
    img = opt.minimize(**optimizer_params)[0]
    if text:
        img = utils.draw_text(img, text)
    return img
Пример #12
0
    #modelName = "/home/daiver/coding/jff/py/keras_shape_reg/checkpoints/2017-05-19 12:47:16.562989_ep_149_train_l_0.00872_test_l_0.02065.h5"
    modelName = "/home/daiver/coding/jff/py/keras_shape_reg/checkpoints/2017-05-18 16:43:27.041387_ep_9499_train_l_0.00009_test_l_0.00706.h5"
    model = keras.models.load_model(modelName)
    print model.layers[0].name#conv1
    print model.layers[3].name#conv2
    print model.layers[5].name#conv3
    print model.layers[8].name#conv4
    print model.layers[10].name#conv5
    print model.layers[13].name#conv6
    print model.layers[15].name#conv7
    print model.layers[18].name#conv8
    print model.layers[20].name#conv8
    print model.layers[25].name#conv9
    #exit()
    
    layer_idx = 25
    num_filters = get_num_filters(model.layers[layer_idx])
    print num_filters
    filters = np.arange(get_num_filters(model.layers[layer_idx]))[:32]
    vis_images = []
    for idx in filters:
	img = visualize_activation(model, layer_idx, filter_indices=idx) 
	img = utils.draw_text(img, str(idx))
	vis_images.append(img)

    # Generate stitched image palette with 8 cols.
    stitched = utils.stitch_images(vis_images, cols=8)    
    plt.axis('off')
    plt.imshow(stitched)
    plt.show()
                               tv_weight=0.,
                               max_iter=300,
                               input_modifiers=[Jitter(16)])

    initial.append(img)
    tuples.append((name, idx))

i = 0
for name, idx in tuples:
    img = visualize_activation(model,
                               layer_idx,
                               filter_indices=idx,
                               seed_input=initial[i],
                               max_iter=300,
                               input_modifiers=[Jitter(16)])
    img = utils.draw_text(img, name)
    i += 1
    images.append(img)

stitched = utils.stitch_images(images, cols=4)
plt.figure(figsize=(20, 20))
plt.axis('off')
plt.imshow(stitched)
plt.show()

# # Visualizing Convnet Filters

# In[39]:

max_filters = 40
selected_indices = []
Пример #14
0
model.save('lenet_'+args.network+'.h5')

layer_dict = dict([(layer.name, layer) for layer in model.layers])
print(layer_dict)
layer_name = 'conv2d_1'

layer_idx = utils.find_layer_idx(model, layer_name=layer_name)

plt.rcParams['figure.figsize'] = (18, 6)
img = visualize_activation(model, layer_idx, filter_indices=1)
plt.savefig('fil_in_vis.png')


filters = np.arange(get_num_filters(model.layers[layer_idx]))

# Generate input image for each filter.
vis_images = []
for idx in filters:
    img = visualize_activation(model, layer_idx, filter_indices=idx)

    # Utility to overlay text on image.
    img = utils.draw_text(img, ' {}'.format(idx))
    vis_images.append(img)

stitched = utils.stitch_images(vis_images, cols=8)
plt.axis('off')
plt.imshow(stitched)
plt.title(layer_name)
# plt.show()
plt.savefig("filter_vis_"+args.network+".png")