raghakot / keras-vis

Neural network visualization toolkit for keras
https://raghakot.github.io/keras-vis
MIT License
2.98k stars 664 forks source link

Activation maximization with InceptionV3 #143

Open simopal6 opened 5 years ago

simopal6 commented 5 years ago

From master, I modified the activation_maximization.ipynb notebook to replace VGG16 with InceptionV3. Basically, I only changed the model instantiation to:

from keras.applications.inception_v3 import InceptionV3
model = InceptionV3(include_top=True, weights='imagenet', input_shape=(299, 299, 3))

However, I can't get meaningful images. For example, for the "ouzel" example, I get something very similar to: https://files.slack.com/files-pri/T3JPM5YNB-F7AFPBRGD/image.png (this was reported by a Slack user in September 2017).

keisen commented 5 years ago

@simopal6, Thank you. I could reproduce this issue, so I'm going to check the cause from now.

keisen commented 5 years ago

InceptionV3 said "It's 100% this is a website(class:916)" when it predicted the meaningless image (Of course the right answer is ozel). Maybe, this means that ActivationMaximization is using unfit value of parameters (ex., loss-weights, jitter, etc) for InceptionV3.

For now, I'm not sure how it should fix.

ju-w commented 5 years ago

Maybe the cause is #153

boleszek commented 5 years ago

Hello, has anybody figured out how to get meaningful keras-vis visualizations with InceptionV3?

I tried the following code (copying the example notebook and just replacing VGG16 with InceptionV3), but the visualization is a hazy mess. Is there a reason to expect this behavior from InceptionV3?

import numpy as np
from keras.applications import VGG16,InceptionV3
from vis.utils import utils
from keras import activations
from vis.visualization import visualize_activation
from vis.input_modifiers import Jitter
from matplotlib import pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (18, 6)

# Load pretrained model
#model = VGG16(weights='imagenet', include_top=True)
model = InceptionV3(weights='imagenet', include_top=True)

# Utility to search for layer index by name. 
# Alternatively we can specify this as -1 since it corresponds to the last layer.
layer_idx = utils.find_layer_idx(model, 'predictions')

# Swap softmax with linear
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)

# 20 is the imagenet category for 'ouzel'

img = visualize_activation(model, layer_idx, filter_indices=20, max_iter = 500)
plt.imshow(img)

I'm using keras 2.2.4 version with tf backend version 1.13.1, running on CPU.