GithubHelp home page GithubHelp logo

cost must be a scalar about keras-vis HOT 4 CLOSED

raghakot avatar raghakot commented on August 30, 2024
cost must be a scalar

from keras-vis.

Comments (4)

raghakot avatar raghakot commented on August 30, 2024

Can you post a gist or sample to reproduce the issue?

from keras-vis.

fery1 avatar fery1 commented on August 30, 2024

When I want to run keras-vis, got this error.
python packages:
keras=='2.0.0'
theano=='0.9.0'

from keras-vis.

raghakot avatar raghakot commented on August 30, 2024

What did you run? Your own code or some example?

from keras-vis.

fery1 avatar fery1 commented on August 30, 2024

run examples are exist in your Github:

import numpy as np
from matplotlib import pyplot as plt

from vis.utils import utils
from vis.utils.vggnet import VGG16
from vis.visualization import visualize_activation
import keras

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()

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()

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_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()

if name == 'main':
print (keras.version)
print('Visualizing random imagenet output categories')
visualize_random(3)

print('Visualizing same filter over multiple runs')
visualize_multiple_same_filter()

print('Visualizing multiple categories')
visualize_multiple_categories()

from keras-vis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.