Пример #1
0
    def __init__(self, network, options=None, **kwargs):
        options = options or kwargs

        if isinstance(network, (list, tuple)):
            network = layers.join(*network)

        self.network = network

        if len(self.network.output_layers) != 1:
            n_outputs = len(network.output_layers)

            raise InvalidConnection("Connection should have one output "
                                    "layer, got {}".format(n_outputs))

        target = options.get('target')
        if target is not None and isinstance(target, (list, tuple)):
            options['target'] = tf.placeholder(tf.float32, shape=target)

        self.target = self.network.targets
        super(BaseOptimizer, self).__init__(**options)

        start_init_time = time.time()
        self.logs.message("TENSORFLOW",
                          "Initializing Tensorflow variables and functions.")

        self.variables = AttributeKeyDict()
        self.functions = AttributeKeyDict()
        self.network.outputs
        self.init_functions()

        self.logs.message(
            "TENSORFLOW",
            "Initialization finished successfully. It took {:.2f} seconds"
            "".format(time.time() - start_init_time))
Пример #2
0
    def __init__(self, connection, *args, **kwargs):
        self.connection = clean_layers(connection)

        self.layers = list(self.connection)
        graph = self.connection.graph

        if len(self.connection.output_layers) != 1:
            n_outputs = len(graph.output_layers)
            raise InvalidConnection("Connection should have one output "
                                    "layer, got {}".format(n_outputs))

        self.output_layer = graph.output_layers[0]

        super(ConstructibleNetwork, self).__init__(*args, **kwargs)
Пример #3
0
    def __init__(self, connection, **options):
        if len(connection) != 2:
            raise ValueError("This network should contains two layers.")

        if all(isinstance(element, int) for element in connection):
            input_layer_size, output_layer_size = connection
            connection = Input(input_layer_size) > Step(output_layer_size)

        if not isinstance(connection, LayerConnection):
            raise ValueError("Invalid connection type")

        output_layer = connection.output_layers[0]

        if not isinstance(output_layer, Step):
            raise InvalidConnection(
                "Final layer should contains step activation function "
                "(``layers.Step`` class instance).")

        super(BaseLinearNetwork, self).__init__(connection, **options)
def saliency_map(connection, image, mode='heatmap', sigma=8,
                 ax=None, show=True, **kwargs):
    """
    Saliency Map plot.

    Parameters
    ----------
    connection : network, connection
        Network based on which will be computed saliency map.

    image : 3D array-like tensor
        Image based on which will be computed saliency map.

    mode : {``raw``, ``heatmap``}
        - ``raw``
          Visualize raw gradient. White color on the plot
          defines high gradient values.

        - ``heatmap``
          Applies gaussian filter to the gradient and visualize
          as a heatmap plot.

        Defaults to ``heatmap``.

    sigma : float
        Standard deviation for kernel in Gaussian filter.
        It is used only when ``mode='heatmap'``. Defaults to ``8``.

    ax : object or None
        Matplotlib axis object. ``None`` values means that axis equal
        to the current axes instance (the same as ``ax = plt.gca()``).
        Defaults to ``None``.

    show : bool
        If parameter is equal to ``True`` then plot will be
        displayed. Defaults to ``True``.

    **kwargs
        Arguments for ``plt.imshow`` function.

    Returns
    -------
    object
        Matplotlib axis instance.

    Examples
    --------
    >>> from neupy import layers, plots
    >>>
    >>> network = layers.join(
    ...     layers.Input((3, 28, 28)),
    ...     layers.Convolution((32, 3, 3)) > layers.Relu(),
    ...     layers.Reshape(),
    ...     layers.Softmax(10),
    ... )
    >>>
    >>> dog_image = load_dog_image()
    >>> plots.saliency_map(network, dog_image)
    """
    if image.ndim == 3:
        image = np.expand_dims(image, axis=0)

    if image.ndim != 4:
        raise ValueError("Invalid image shape. Image expected to be 3D, "
                         "got {}D image".format(image.ndim))

    valid_modes = ('raw', 'heatmap')
    if mode not in valid_modes:
        raise ValueError("{!r} is invalid value for mode argument. Valid "
                         "mode values are: {!r}".format(mode, valid_modes))

    if len(connection.output_layers) != 1:
        raise InvalidConnection(
            "Cannot build saliency map for connection that has more than "
            "one output layer. Output layer can be specified explicitly. "
            "For instance, use network.end('layer-name') with specified "
            "output layer name.")

    if len(connection.input_layers) != 1:
        raise InvalidConnection(
            "Cannot build saliency map for connection that has more than "
            "one input layer. Output layer can be specified explicitly. "
            "For instance, use network.start('layer-name') with specified "
            "output layer name.")

    if len(connection.input_shape) != 3:
        raise InvalidConnection("Input layer has invalid input shape")

    if ax is None:
        ax = plt.gca()

    saliency_and_output = compile_saliency_map(connection)
    saliency, output = saliency_and_output(image)

    saliency = saliency[0].transpose((1, 2, 0))
    saliency = saliency.max(axis=2)

    if mode == 'heatmap':
        saliency = gaussian_filter(saliency, sigma=sigma)

    elif mode == 'raw':
        kwargs.setdefault('cmap', 'gray')

    ax.set_title('Predicted output #{} (0-based indeces)'.format(output))
    ax.imshow(saliency, **kwargs)

    if show:
        plt.show()

    return ax
Пример #5
0
def saliency_map(network,
                 image,
                 mode='heatmap',
                 sigma=8,
                 ax=None,
                 show=True,
                 **kwargs):
    """
    Saliency Map plot.

    Parameters
    ----------
    network : network
        Network based on which will be computed saliency map.

    image : 3D array-like tensor
        Image based on which will be computed saliency map.

    mode : {``raw``, ``heatmap``}
        - ``raw``
          Visualize raw gradient. White color on the plot
          defines high gradient values.

        - ``heatmap``
          Applies gaussian filter to the gradient and visualize
          as a heatmap plot.

        Defaults to ``heatmap``.

    sigma : float
        Standard deviation for kernel in Gaussian filter.
        It is used only when ``mode='heatmap'``. Defaults to ``8``.

    ax : object or None
        Matplotlib axis object. ``None`` values means that axis equal
        to the current axes instance (the same as ``ax = plt.gca()``).
        Defaults to ``None``.

    show : bool
        If parameter is equal to ``True`` then plot will be
        displayed. Defaults to ``True``.

    **kwargs
        Arguments for ``plt.imshow`` function.

    Returns
    -------
    object
        Matplotlib axis instance.

    Examples
    --------
    >>> from neupy import layers, plots
    >>>
    >>> network = layers.join(
    ...     layers.Input((3, 28, 28)),
    ...     layers.Convolution((32, 3, 3)) >> layers.Relu(),
    ...     layers.Reshape(),
    ...     layers.Softmax(10),
    ... )
    >>>
    >>> dog_image = load_dog_image()
    >>> plots.saliency_map(network, dog_image)
    """
    if image.ndim == 3:
        image = np.expand_dims(image, axis=0)

    if image.ndim != 4:
        raise ValueError("Invalid image shape. Image expected to be 3D, "
                         "got {}D image".format(image.ndim))

    valid_modes = ('raw', 'heatmap')
    if mode not in valid_modes:
        raise ValueError("{!r} is invalid value for mode argument. Valid "
                         "mode values are: {!r}".format(mode, valid_modes))

    if isinstance(network, BaseOptimizer):
        network = network.network

    if len(network.output_layers) != 1:
        raise InvalidConnection(
            "Cannot build saliency map for the network that "
            "has more than one output layer.")

    if len(network.input_layers) != 1:
        raise InvalidConnection(
            "Cannot build saliency map for the network that "
            "has more than one input layer.")

    if len(network.input_shape) != 4:
        raise InvalidConnection(
            "Input layer has to be 4 dimensions, but network expects "
            "{} dimensional input".format(len(network.input_shape)))

    if ax is None:
        ax = plt.gca()

    x, saliency, output_class = saliency_map_graph(network)

    session = tensorflow_session()
    saliency, output = session.run([saliency, output_class],
                                   feed_dict={x: image})

    saliency = saliency[0].max(axis=-1)

    if mode == 'heatmap':
        saliency = gaussian_filter(saliency, sigma=sigma)

    elif mode == 'raw':
        kwargs.setdefault('cmap', 'gray')

    ax.set_title('Predicted output #{} (0-based indeces)'.format(output))
    ax.imshow(saliency, **kwargs)

    if show:
        plt.show()

    return ax