예제 #1
0
def plot(model, to_file='model.png', show_shapes=False, show_layer_names=True):
    try:
        # pydot-ng is a fork of pydot that is better maintained
        import pydot_ng as pydot
    except ImportError:
        # fall back on pydot if necessary
        import pydot

    if hasattr(pydot, 'find_graphviz'):
        if not pydot.find_graphviz():
            raise RuntimeError('Failed to import pydot. You must install pydot'
                               ' and graphviz for `pydotprint` to work.')
    else:
        pydot.Dot.create(pydot.Dot())
    dot = model_to_dot(model, show_shapes, show_layer_names)
    _, format = os.path.splitext(to_file)
    if not format:
        format = 'png'
    else:
        format = format[1:]
    dot.write(to_file, format=format)
예제 #2
0
def plot_model_if_pydot(model, file):
    """
    Plot models with graphivz library if pydot and graphviz are installed else do nothing
    :param model:
    :param file:
    :return:
    """
    try:
        # pydot-ng is a fork of pydot that is better maintained.
        import pydot_ng as pydot
    except ImportError:
        # Fall back on pydot if necessary.
        try:
            import pydot
        except ImportError:
            pydot = None

    from keras.utils.vis_utils import plot_model
    if pydot is not None and pydot.find_graphviz():
        plot_model(
            model, to_file=file, show_shapes=True, show_layer_names=True)
    else:
        print("Pydot is None or not graphviz")
예제 #3
0
파일: printing.py 프로젝트: JinCSU/Theano
import numpy as np
from six import string_types, integer_types, iteritems

import theano
from theano import gof
from theano import config
from six.moves import StringIO, reduce
from theano.gof import Op, Apply
from theano.compile import Function, debugmode, SharedVariable
from theano.compile.profilemode import ProfileMode

# pydot-ng is a fork of pydot that is better maintained, and works
# with more recent version of its dependencies (in particular pyparsing)
try:
    import pydot_ng as pd
    if pd.find_graphviz():
        pydot_imported = True
    else:
        pydot_imported = False
except Exception:
    # Sometimes, a Windows-specific exception is raised
    pydot_imported = False
# Fall back on pydot if necessary
if not pydot_imported:
    try:
        import pydot as pd
        if pd.find_graphviz():
            pydot_imported = True
    except Exception:
        pass
예제 #4
0
import os
import pdb

from ..layers.wrappers import Wrapper
from ..models import Sequential

try:
    # pydot-ng is a fork of pydot that is better maintained
    import pydot_ng as pydot
except ImportError:
    # fall back on pydot if necessary
    import pydot
if not pydot.find_graphviz():
    raise RuntimeError('Failed to import pydot. You must install pydot'
                       ' and graphviz for `pydotprint` to work.')


def graph_node(layers, dot, model, show_shapes=False, show_layer_names=False):
    for l, layer in enumerate(layers):
        layer_id = str(id(layer))

        # Append a wrapped layer's label to node's label, if it exists.
        layer_name = layer.name
        class_name = layer.__class__.__name__
        if class_name in [
                "Dropout", "BatchNormalization", "Activation", "Flatten"
        ]:
            continue
        if class_name == "Model":
            layer.layers[0].inbound_nodes = layer.inbound_nodes
            graph_node(layer.layers, dot, layer, show_shapes=show_shapes)
예제 #5
0
import numpy as np
import os
from functools import reduce
from six import iteritems, itervalues

import theano
from theano import gof
from theano.compile.profilemode import ProfileMode
from theano.compile import Function
from theano.compile import builders

pydot_imported = False
try:
    # pydot-ng is a fork of pydot that is better maintained
    import pydot_ng as pd
    if pd.find_graphviz():
        pydot_imported = True
except ImportError:
    try:
        # fall back on pydot if necessary
        import pydot as pd
        if pd.find_graphviz():
            pydot_imported = True
    except ImportError:
        pass  # tests should not fail on optional dependency


class PyDotFormatter(object):
    """Create `pydot` graph object from Theano function.

    Parameters
예제 #6
0
def _check_pydot():
    if not (pydot and pydot.find_graphviz()):
        raise ImportError('Failed to import pydot. You must install pydot'
                          ' and graphviz for `pydotprint` to work.')
예제 #7
0
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import UpSampling2D
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from skimage import io
from skimage import transform
from tensorflow.keras.layers import add
from matplotlib import pyplot
from tensorflow.keras.models import load_model
from tensorflow.keras import backend
from tensorflow.keras.layers import Layer

pydot.find_graphviz()


# mini-batch standard deviation layer
class MinibatchStdev(Layer):
    # initialize the layer
    def __init__(self, **kwargs):
        super(MinibatchStdev, self).__init__(**kwargs)

    # perform the operation
    def call(self, inputs):
        # calculate the mean value for each pixel across channels
        mean = backend.mean(inputs, axis=0, keepdims=True)
        # calculate the squared differences between pixel values and mean
        squ_diffs = backend.square(inputs - mean)
        # calculate the average of the squared differences (variance)
예제 #8
0
import itertools
from keras.layers.containers import Graph, Sequential
from keras.layers.core import Merge

try:
    # pydot-ng is a fork of pydot that is better maintained
    import pydot_ng as pydot
except ImportError:
    # fall back on pydot if necessary
    import pydot
if not pydot.find_graphviz():
    raise RuntimeError("Failed to import pydot. You must install pydot"
                       " and graphviz for `pydotprint` to work.")


def layer_typename(layer):
    return type(layer).__module__ + "." + type(layer).__name__


def get_layer_to_name(model):
    """Returns a dict mapping layer to their name in the model"""
    if not isinstance(model, Graph):
        return {}
    else:
        node_to_name = itertools.chain(
            model.nodes.items(), model.inputs.items(), model.outputs.items()
        )
        return {v: k for k, v in node_to_name}


class ModelToDot(object):
예제 #9
0
def _check_pydot():
  if not (pydot and pydot.find_graphviz()):
    raise ImportError('Failed to import pydot. You must install pydot'
                      ' and graphviz for `pydotprint` to work.')
예제 #10
0
def model_to_dot(model, show_shapes=False, show_layer_names=True):
    try:
        # pydot-ng is a fork of pydot that is better maintained
        import pydot_ng as pydot
    except ImportError:
        # fall back on pydot if necessary
        import pydot

    if hasattr(pydot, 'find_graphviz'):
        if not pydot.find_graphviz():
            raise RuntimeError('Failed to import pydot. You must install pydot'
                               ' and graphviz for `pydotprint` to work.')
    else:
        pydot.Dot.create(pydot.Dot())
    from keras.models import Sequential

    dot = pydot.Dot()
    dot.set('rankdir', 'TB')
    dot.set('concentrate', True)
    dot.set_node_defaults(shape='record')

    if isinstance(model, Sequential):
        if not model.built:
            model.build()
        model = model.model
    layers = model.layers

    # Create graph nodes.
    for layer in layers:
        layer_id = str(id(layer))

        # Append a wrapped layer's label to node's label, if it exists.
        layer_name = layer.name
        class_name = layer.__class__.__name__
        if isinstance(layer, Wrapper):
            layer_name = '{}({})'.format(layer_name, layer.layer.name)
            child_class_name = layer.layer.__class__.__name__
            class_name = '{}({})'.format(class_name, child_class_name)

        # Create node's label.
        if show_layer_names:
            label = '{}: {}'.format(layer_name, class_name)
        else:
            label = class_name

        # Rebuild the label as a table including input/output shapes.
        if show_shapes:
            try:
                outputlabels = str(layer.output_shape)
            except:
                outputlabels = 'multiple'
            if hasattr(layer, 'input_shape'):
                inputlabels = str(layer.input_shape)
            elif hasattr(layer, 'input_shapes'):
                inputlabels = ', '.join(
                    [str(ishape) for ishape in layer.input_shapes])
            else:
                inputlabels = 'multiple'
            label = '%s\n|{input:|output:}|{{%s}|{%s}}' % (label, inputlabels,
                                                           outputlabels)

        node = pydot.Node(layer_id, label=label)
        dot.add_node(node)

    # Connect nodes with edges.
    for layer in layers:
        layer_id = str(id(layer))
        for i, node in enumerate(layer.inbound_nodes):
            node_key = layer.name + '_ib-' + str(i)
            if node_key in model.container_nodes:
                for inbound_layer in node.inbound_layers:
                    inbound_layer_id = str(id(inbound_layer))
                    layer_id = str(id(layer))
                    dot.add_edge(pydot.Edge(inbound_layer_id, layer_id))
    return dot
예제 #11
0
import pydot_ng as pydot
print(pydot.find_graphviz())

from captcha.image import ImageCaptcha
import matplotlib.pyplot as plt
import numpy as np
import random

import string
characters = string.digits + string.ascii_uppercase
print(characters)
width, height, n_len, n_class = 170, 80, 4, len(characters)

generator = ImageCaptcha(width=width, height=height)
random_str = ''.join([random.choice(characters) for j in range(4)])
img = generator.generate_image(random_str)

# plt.imshow(img)
# plt.title(random_str)
# plt.show()


# 无限生成数据函数
def gen(batch_size=32):
    X = np.zeros(
        (batch_size, height, width, 3), dtype=np.uint8
    )  # X 的形状是 (batch_size, height, width, 3),比如一批生成32个样本,图片宽度为170,高度为80,那么形状就是 (32, 80, 170, 3),取第一张图就是 X[0]。
    y = [
        np.zeros((batch_size, n_class), dtype=np.uint8) for i in range(n_len)
    ]  # y 的形状是四个 (batch_size, n_class),如果转换成 numpy 的格式,则是 (n_len, batch_size, n_class),比如一批生成32个样本,验证码的字符有36种,长度是4位,那么它的形状就是4个 (32, 36),也可以说是 (4, 32, 36)
    generator = ImageCaptcha(width=width, height=height)
예제 #12
0
import pydot_ng as pydot
import unittest2 as unittest


PY3 = not sys.version_info < (3, 0, 0)

if PY3:
    NULL_SEP = b''
    xrange = range
else:
    NULL_SEP = ''
    bytes = str


DOT_BINARY_PATH = pydot.find_graphviz()['dot']
TEST_DIR = os.path.dirname(__file__)
REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, 'graphs')
MY_REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, 'my_tests')


class TestGraphAPI(unittest.TestCase):

    def setUp(self):
        self._reset_graphs()

    def _reset_graphs(self):
        self.graph_directed = pydot.Graph('testgraph', graph_type='digraph')

    def test_keep_graph_type(self):
        g = pydot.Dot(graph_name='Test', graph_type='graph')
예제 #13
0
import subprocess
import sys
from hashlib import sha256

import pydot_ng as pydot

PY3 = not sys.version_info < (3, 0, 0)

if PY3:
    NULL_SEP = b""
    xrange = range
else:
    NULL_SEP = ""
    bytes = str

DOT_BINARY_PATH = pydot.find_graphviz()["dot"]

TEST_DIR = os.path.dirname(__file__)
REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, "graphs")
MY_REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, "my_tests")

TESTS_DIRS = (
    ("regressions", REGRESSION_TESTS_DIR),
    ("my_regressions", MY_REGRESSION_TESTS_DIR),
)


def list_dots(path):
    searchpath = os.path.join(path, "*.dot")
    return [f for f in glob.glob(searchpath)]