• Stars
    star
    793
  • Rank 57,419 (Top 2 %)
  • Language
    Python
  • Created almost 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multilayer LSTM and Mixture Density Network for modelling path-level SVG Vector Graphics data in TensorFlow

Depreciated

This version of sketch-rnn has been depreciated. Please see an updated version of sketch-rnn, which is a full generative model for vector drawings.

sketch-rnn

Implementation multi-layer recurrent neural network (RNN, LSTM GRU) used to model and generate sketches stored in .svg vector graphic files. The methodology used is to combine Mixture Density Networks with a RNN, along with modelling dynamic end-of-stroke and end-of-content probabilities learned from a large corpus of similar .svg files, to generate drawings that is simlar to the vector training data.

See my blog post at blog.otoro.net for a detailed description on applying sketch-rnn to learn to generate fake Chinese characters in vector format.

Example Training Sketches (20 randomly chosen out of 11000 KanjiVG dataset):

Example Training Sketches

Generated Sketches (Temperature = 0.1):

Generated Sketches

Basic Usage

I tested the implementation on TensorFlow 0.50. I also used the following libraries to help:

svgwrite
IPython.display.SVG
IPython.display.display
xml.etree.ElementTree
argparse
cPickle
svg.path

Loading in Training Data

The training data is located inside the data subdirectory. In this repo, I've included kanji.cpkl which is a preprocessed array of KanjiVG characters.

To add a new set of training data, for example, from the TU Berlin Sketch Database, you have to create a subdirectory, say tuberlin inside the data directory, and in addition create a directory of the same name in the save directory. So you end up with data/tuberlin/ and save/tuberlin, where tuberlin is defined as a name field for flags in the training and sample programs later on. save/tuberlin will contain the check-pointed trained models later on.

Now, put a large collection of .svg files into data/tuberlin/. You can even create subdirectories within data/tuberlin/ and it will work, as the SketchLoader class will scan the entire subdirectory tree.

Currently, sketch-rnn only processes path elements inside svg files, and within the path elements, it only cares about lines and belzier curves at the moment. I found this sufficient to handle TUBerlin and KanjiVG databases, although it wouldn't be difficult to extent to process the other curve elements, even shape elements in the future.

You can use utils.py to play out some random training data after the svg files have been copied in:

%run -i utils.py
loader = SketchLoader(data_filename = 'tuberlin')
draw_stroke_color(random.choice(loader.raw_data))

Example Elephant from TU Berlin database

For this algorithm to work, I recommend the data be similar in size, and similar in style / content. For examples if we have bananas, buildings, elephants, rockets, insects of varying shapes and sizes, it would most likely just produce gibberish.

Training the Model

After the data is loaded, let's continue with the 'tuberlin' example, you can run python train.py --dataset_name tuberlin

A number of flags can be set for training if you wish to experiment with the parameters. You probably want to change these around, especially the scaling factors to better suit the sizes of your .svg data.

The default values are in train.py

--rnn_size RNN_SIZE             size of RNN hidden state (256)
--num_layers NUM_LAYERS         number of layers in the RNN (2)
--model MODEL                   rnn, gru, or lstm (lstm)
--batch_size BATCH_SIZE         minibatch size (100)
--seq_length SEQ_LENGTH         RNN sequence length (300)
--num_epochs NUM_EPOCHS         number of epochs (500)
--save_every SAVE_EVERY         save frequency (250)
--grad_clip GRAD_CLIP           clip gradients at this value (5.0)
--learning_rate LEARNING_RATE   learning rate (0.005)
--decay_rate DECAY_RATE         decay rate after each epoch (adam is used) (0.99)
--num_mixture NUM_MIXTURE       number of gaussian mixtures (24)
--data_scale DATA_SCALE         factor to scale raw data down by (15.0)
--keep_prob KEEP_PROB           dropout keep probability (0.8)
--stroke_importance_factor F    gradient boosting of sketch-finish event (200.0)
--dataset_name DATASET_NAME     name of directory containing training data (kanji)

Sampling a Sketch

I've included a pretrained model in /save so it should work out of the box. Running python sample.py --filename output --num_picture 10 --dataset_name kanji will generate an .svg file containing 10 fake Kanji characters using the pretrained model. Please run python sample.py --help to examine extra flags, to see how to change things like number of sketches per row, etc.

It should be straight forward to examine sample.py to be able to generate sketches interactively using an IPython prompt rather than in the command line. Running %run -i sample.py in an IPython interactive session would generate sketches shown in the IPython interface as well as generating an .svg output.

More useful links, pointers, datasets

  • Alex Graves' paper on text sequence and handwriting generation.

  • Karpathy's char-rnn tool, motivation for creating sketch-rnn.

  • KanjiVG. Fantastic Database of Kanji Stroke Order.

  • Very clean TensorFlow implementation of char-rnn, written by Sherjil Ozair, where I based the skeleton of this code off of.

  • svg.path. I used this well written tool to help convert path data into line data.

  • CASIA Online and Offline Chinese Handwriting Databases. Download stroke data for written cursive Simplifed Chinese.

  • How Do Humans Sketch Objects? TU Berlin Sketch Database. Would be interesting to extend this work and generate random vector art of real life stuff.

  • Doraemon in SVG format.

  • Potrace. Beautiful looking tool to convert raster bitmapped drawings into SVG for potentially scaling up resolution of drawings. Could potentially apply this to generate large amounts of training data.

  • Rendering Belzier Curve Codes. I used this very useful code to convert Belzier curves into line segments.

License

MIT

More Repositories

1

estool

Evolution Strategies Tool
Jupyter Notebook
901
star
2

write-rnn-tensorflow

Generative Handwriting using LSTM Mixture Density Network with TensorFlow
Python
692
star
3

slimevolleygym

A simple OpenAI Gym environment for single and multi-agent reinforcement learning
Python
633
star
4

WorldModelsExperiments

World Models Experiments
Jupyter Notebook
545
star
5

pytorch_notebooks

tutorial notebooks
Jupyter Notebook
371
star
6

cppn-gan-vae-tensorflow

Train CPPNs as a Generative Model, using Generative Adversarial Networks and Variational Autoencoder techniques to produce high resolution images.
Python
346
star
7

cppn-tensorflow

Very Simple and Basic Implementation of Compositional Pattern Producing Network in TensorFlow
Python
311
star
8

sketch-rnn-datasets

optional extra vector image datasets for sketch-rnn
Python
215
star
9

neuralslimevolley

Neural Slime Volleyball
JavaScript
201
star
10

supercell

supercell
Jupyter Notebook
188
star
11

rnn-tutorial

RNN Tutorial for Artists
JavaScript
183
star
12

resnet-cppn-gan-tensorflow

Using Residual Generative Adversarial Networks and Variational Auto-encoder techniques to produce high resolution images.
Python
123
star
13

backprop-neat-js

Neural Network Evolution Playground with Backprop NEAT
JavaScript
122
star
14

astool

Augmented environments with RL
Jupyter Notebook
102
star
15

image-notebook

image-notebook
Jupyter Notebook
87
star
16

gecco-tutorial-2019

2019 talk at GECCO
67
star
17

neurogram

neurogram
JavaScript
64
star
18

mdn_jax_tutorial

Mixture Density Networks (Bishop, 1994) tutorial in JAX
Jupyter Notebook
56
star
19

presentations

presentations
44
star
20

cppn-gan-vae-cifar-tensorflow

First attempt to use the previous CPPN-GAN-VAE model to train on CIFAR-10 images.
Python
39
star
21

netart-js

quick hacked together js demo to implement fixed-topology cppn in the web browser for interactive usage
JavaScript
39
star
22

sketch-rnn-flowchart

trained sketch-rnn / deployed with sketch-rnn-js on flowchart dataset
JavaScript
33
star
23

kanji2kanji

Reproduce domain transfer results in Deep Learning for Classical Japanese Literature
Jupyter Notebook
31
star
24

paperjs_box2d

demo of working with box2d and paper.js
JavaScript
31
star
25

RainbowSlimeVolley

Using Rainbow implementation in Chainer RL for Slime Volleyball Pixel Environment
Python
24
star
26

sketch-rnn-poster

Poster for "A Neural Representation for Sketch Drawings" for ICLR 2018 Conference
22
star
27

rlzoo

fork of rl-baseline-zoo
Python
21
star
28

quickdraw-ndjson-to-npz

Convert RAW Quickdraw datafiles into stroke-based .npz format with RDP at variable epsilon for sketch-rnn
Jupyter Notebook
15
star
29

sema-demo

sketch-rnn demo for seoul mediacity biennale 2018
JavaScript
13
star
30

diff-vae-tensorflow

skeleton variation encoder code in tensorflow
Python
12
star
31

pybullet_animations

pybullet_animations
12
star
32

cma

Fork of cma-es library by Nikolaus Hansen
Python
11
star
33

cantonese-list

List of 4000 Chinese characters sorted by historical usage frequency, with Cantonese yale romanization and definition
11
star
34

creatures

autonomous creatures written in processing / processing.js
Processing
7
star
35

zombie

A* search algorithm example, zombie chasing humans
Python
4
star
36

tshirtai

3
star
37

batchnorm-convnet-mnist-tensorflow

self contained skeleton script for testing and training mnist convnet+batchnorm based classifier in tensorflow
Python
2
star