Giter VIP home page Giter VIP logo

keras-sequential-ascii's Introduction

Sequential model in Keras -> ASCII

by Piotr Migdał

A library for Keras for investigating architectures and parameters of sequential models.

(discontinuted) For more general approaches, see: Simple diagrams of convoluted neural networks

Both model.summary() and graph export were not enough - I wanted array dimensions, numbers of parameters and activation functions in one place. I use it for didactic purpose.

  • TODO
    • Add ASCII art for more layers.
    • Go beyond simple sequential models (e.g. to allow merge layers); any ideas how?
    • Consider PRing to the main Keras repo, see #3873.

See this library in the wild, for example:

Installation

From PyPI:

pip install keras_sequential_ascii

Or from this repo:

pip install git+git://github.com/stared/keras-sequential-ascii.git

Usage

from keras_sequential_ascii import keras2ascii
keras2ascii(model)

Examples

Proof of principle

           OPERATION           DATA DIMENSIONS   WEIGHTS(N)   WEIGHTS(%)

               Input   #####      3   32   32
  BatchNormalization    μ|σ  -------------------        64     0.1%
                       #####      3   32   32
       Convolution2D    \|/  -------------------       448     0.8%
                relu   #####     16   30   30
       Convolution2D    \|/  -------------------      2320     4.3%
                relu   #####     16   28   28
        MaxPooling2D   Y max -------------------         0     0.0%
                       #####     16   14   14
       Convolution2D    \|/  -------------------       272     0.5%
                tanh   #####     16   14   14
             Flatten   ||||| -------------------         0     0.0%
                       #####        3136
               Dense   XXXXX -------------------     50192    94.1%
                       #####          16
             Dropout    | || -------------------         0     0.0%
                       #####          16
               Dense   XXXXX -------------------        51     0.1%
             softmax   #####           3

VGG16

           OPERATION           DATA DIMENSIONS   WEIGHTS(N)   WEIGHTS(%)

              Input   #####      3  224  224
         InputLayer     |   -------------------         0     0.0%
                      #####      3  224  224
      Convolution2D    \|/  -------------------      1792     0.0%
               relu   #####     64  224  224
      Convolution2D    \|/  -------------------     36928     0.0%
               relu   #####     64  224  224
       MaxPooling2D   Y max -------------------         0     0.0%
                      #####     64  112  112
      Convolution2D    \|/  -------------------     73856     0.1%
               relu   #####    128  112  112
      Convolution2D    \|/  -------------------    147584     0.1%
               relu   #####    128  112  112
       MaxPooling2D   Y max -------------------         0     0.0%
                      #####    128   56   56
      Convolution2D    \|/  -------------------    295168     0.2%
               relu   #####    256   56   56
      Convolution2D    \|/  -------------------    590080     0.4%
               relu   #####    256   56   56
      Convolution2D    \|/  -------------------    590080     0.4%
               relu   #####    256   56   56
       MaxPooling2D   Y max -------------------         0     0.0%
                      #####    256   28   28
      Convolution2D    \|/  -------------------   1180160     0.9%
               relu   #####    512   28   28
      Convolution2D    \|/  -------------------   2359808     1.7%
               relu   #####    512   28   28
      Convolution2D    \|/  -------------------   2359808     1.7%
               relu   #####    512   28   28
       MaxPooling2D   Y max -------------------         0     0.0%
                      #####    512   14   14
      Convolution2D    \|/  -------------------   2359808     1.7%
               relu   #####    512   14   14
      Convolution2D    \|/  -------------------   2359808     1.7%
               relu   #####    512   14   14
      Convolution2D    \|/  -------------------   2359808     1.7%
               relu   #####    512   14   14
       MaxPooling2D   Y max -------------------         0     0.0%
                      #####    512    7    7
            Flatten   ||||| -------------------         0     0.0%
                      #####       25088
              Dense   XXXXX ------------------- 102764544    74.3%
               relu   #####        4096
              Dense   XXXXX -------------------  16781312    12.1%
               relu   #####        4096
              Dense   XXXXX -------------------   4097000     3.0%
            softmax   #####        1000

keras-sequential-ascii's People

Contributors

simongeek avatar stared avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras-sequential-ascii's Issues

Error using lambda layers

           OPERATION           DATA DIMENSIONS   WEIGHTS(N)   WEIGHTS(%)

               Input   #####     72  300
          InputLayer     |   -------------------         0     0.0%
                       #####     72  300
               Dense   XXXXX -------------------     15050    92.0%
                       #####     72   50
          InputLayer     |   -------------------         0     0.0%
                       #####     72    3
         Concatenate   ????? -------------------         0     0.0%
                       #####     72   53
                LSTM   LLLLL -------------------      1180     7.0%
                tanh   #####           5
              Lambda   ????? -------------------         0     0.0%
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-222-96aac99b2755> in <module>()
----> 1 keras2ascii(lstm_model)

/anaconda3/envs/tensorflow_p27/lib/python2.7/site-packages/keras_sequential_ascii/main.pyc in sequential_model_to_ascii_printout(model, sparser, simplify, header)
    123 
    124 def sequential_model_to_ascii_printout(model, sparser=False, simplify=True, header=True):
--> 125     print_layers(jsonize(model), sparser=sparser, simplify=simplify, header=header)

/anaconda3/envs/tensorflow_p27/lib/python2.7/site-packages/keras_sequential_ascii/main.pyc in print_layers(jsonized_layers, sparser, simplify, header)
    118         print(data_template.format(
    119                 activation=each["activation"] if each["activation"] != "linear" else "",
--> 120                 shape=print_dim_tuple(each["output_shape"]),
    121                 # length=product(each["output_shape"])
    122         ))

/anaconda3/envs/tensorflow_p27/lib/python2.7/site-packages/keras_sequential_ascii/main.pyc in print_dim_tuple(t)
     83         return " ".join(["{:4d}".format(x) for x in t])
     84     else:
---> 85         return  "{:9d}".format(t[0])
     86 
     87 def print_layers(jsonized_layers, sparser=False, simplify=False, header=True):

IndexError: tuple index out of range

For model:

x = Input(shape=(72,300))
aux_input = Input(shape=(72, 3))
probs = Input(shape=(1,))
dim_reduct = Dense(50)(x)
cat = concatenate([dim_reduct, aux_input])
encoded = LSTM(5)(cat)
#cat2 = concatenate([encoded, probs])
#output = Dense(1, activation='linear')(encoded)

output = Lambda(lambda x: K.sum(x, axis=1))(encoded)

Batch Normalization layers not working?

Even though your example in the readme.md runs BN layers, it seems that I'm having issues with them anyway.

Info: I use the latest tensorflow gpu version (1.1.0) and Keras v2.0.4, under python 2.7.

Error code:

  File "/usr/local/lib/python2.7/dist-packages/keras_sequential_ascii/main.py", line 123, in sequential_model_to_ascii_printout
    print_layers(jsonize(model), sparser=sparser, simplify=simplify, header=header)
  File "/usr/local/lib/python2.7/dist-packages/keras_sequential_ascii/main.py", line 110, in print_layers

    percent_parameters=100 * each["n_parameters"] / all_weights
UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-25: ordinal not in range(128)

My guess is that the characters μ and σ displayed (that ran fine for you somehow) can't get encoded for me... How could we solve this?

EDIT: I sort of fixed it by digging into main.py (edited with nano): I found out that the template associated with BN layers were gibberish (mainly blank, with a single ^ at the end) whereas it should've been μ|σ. I just replaced it with m and v (mean and var) I guess it'll work for now

Export to PNG diagram too?

Congrats @stared for this cool project!
Do you think you could do a version that outputs such 3D diagrams too?

Or do you know another library that takes a Keras model object as input, and that can output such diagrams?

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.