Code Monkey home page Code Monkey logo

handwriting-synthesis's Introduction

Handwriting Synthesis

Implementation of the handwriting synthesis experiments in the paper Generating Sequences with Recurrent Neural Networks by Alex Graves. The implementation closely follows the original paper, with a few slight deviations, and the generated samples are of similar quality to those presented in the paper.

Web demo is available here.

Usage

lines = [
    "Now this is a story all about how",
    "My life got flipped turned upside down",
    "And I'd like to take a minute, just sit right there",
    "I'll tell you how I became the prince of a town called Bel-Air",
]
biases = [.75 for i in lines]
styles = [9 for i in lines]
stroke_colors = ['red', 'green', 'black', 'blue']
stroke_widths = [1, 2, 1, 2]

hand = Hand()
hand.write(
    filename='img/usage_demo.svg',
    lines=lines,
    biases=biases,
    styles=styles,
    stroke_colors=stroke_colors,
    stroke_widths=stroke_widths
)

Currently, the Hand class must be imported from demo.py. If someone would like to package this project to make it more usable, please contribute.

A pretrained model is included, but if you'd like to train your own, read these instructions.

Demonstrations

Below are a few hundred samples from the model, including some samples demonstrating the effect of priming and biasing the model. Loosely speaking, biasing controls the neatness of the samples and priming controls the style of the samples. The code for these demonstrations can be found in demo.py.

Demo #1:

The following samples were generated with a fixed style and fixed bias.

Smash Mouth – All Star (lyrics)

Demo #2

The following samples were generated with varying style and fixed bias. Each verse is generated in a different style.

Vanessa Carlton – A Thousand Miles (lyrics)

Demo #3

The following samples were generated with a fixed style and varying bias. Each verse has a lower bias than the previous, with the last verse being unbiased.

Leonard Cohen – Hallelujah (lyrics)

Contribute

This project was intended to serve as a reference implementation for a research paper, but since the results are of decent quality, it may be worthwile to make the project more broadly usable. I plan to continue focusing on the machine learning side of things. That said, I'd welcome contributors who can:

  • Package this, and otherwise make it look more like a usable software project and less like research code.
  • Add support for more sophisticated drawing, animations, or anything else in this direction. Currently, the project only creates some simple svg files.

handwriting-synthesis's People

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

handwriting-synthesis's Issues

No License File?

I would like to use this code for a Project. Could you please create a LICENSE.md file, stating the type of license you are using?

💡 Tip:
MIT License would be great for this project.

Tick before every line

I get a tick before the first character of every line, is there a way to remove this?
e.g.
screen shot 2018-03-10 at 22 22 02

Styles configuration

Hi,
I am working on a project where I need to define text from windows pre-defined font files.
How can styles (strokes and char files) be created for texts generated with .ttf files?

Thank you in advance.

Regards,
Roy.

ImportError: cannot import name '_like_rnncell'

System:

  • Windows 10 1803
  • Python 3.6.2
  • TensorFlow 1.11.0

Error:

Traceback (most recent call last):
File "test.py", line 1, in <module>
    import demo
File "C:\Projects\ml\handwriting-synthesis\demo.py", line 9, in <module>
    from rnn import rnn
File "C:\Projects\ml\handwriting-synthesis\rnn.py", line 10, in <module>
    from rnn_ops import rnn_free_run
File "C:\Projects\ml\handwriting-synthesis\rnn_ops.py", line 9, in <module>
    from tensorflow.python.ops.rnn_cell_impl import _concat, _like_rnncell
ImportError: cannot import name '_like_rnncell'

Solution:

Hacky, but I simply removed the reference to _like_rnncell on lines 9, 29, and 30 in rnn_ops.py. Seems to run fine afterwards (though this led to encountering Issue #17).

Data preprocessing

When I look at the relevant code for your data preprocessing, I don't quite understand the method used in it. Can you tell me the relevant literature on your method?

'tensorflow.python.eager.context' has no attribute 'in_graph_mode'

First of all, thanks for this amazing tool! It looks incredible. I'm having a little trouble getting it to run locally with Python3 on my Macbook, but that may because I'm not super familiar with python.

Here's the error when I run python3 demo.py

  File "/handwriting-synthesis/rnn_ops.py", line 246, in rnn_free_run
    states, outputs, final_state = raw_rnn(cell, loop_fn, scope=scope)
  File "/handwriting-synthesis/rnn_ops.py", line 40, in raw_rnn
    if context.in_graph_mode():
AttributeError: module 'tensorflow.python.eager.context' has no attribute 'in_graph_mode'

I found tensorflow/tensorflow#14069 which suggested trying

pip install --upgrade --force-reinstall tf-nightly

but they're talking mostly about 1.3 and 1.4 there, while I see you have 1.6 in the requirements.txt.

Can you point me in the right direction?

Strange artifacts when trying to write out consecutive letters

Something strange happens but only with the few of the consecutive letters like:
HHHHHHHHHHH
EEEEEEEEEEEEEEE
AAAAAAAAAAAA
KKKKKKKKKKKKK
ZZZZZZZZZZZZZ
XXXXXXXXXXXXX
BBBBBBBBBBBBB

Result are unexpected, by having either completely weird output or something unexpected like just single parallel lines

Extension to other languages

Hey

What changes need to be made to enable training for other languages, other than the training data? I can see some style files being used, does that need to change as well? In which case, could you share how the style.npy files were created.

Mail Merge?

What resource are there to mail merge for a list of letters but with minor differences like name or address?

AttributeError: 'module' object has no attribute 'in_graph_mode'

Any idea about this error? I get it when running python demo.py
18.04 Ubuntu running stock Python 2.7.15rc1
Have installed:
apt install python-tk
pip install numpy svgwrite matplotlib scipy tensorflow pandas sklearn

Full error:
Traceback (most recent call last): File "demo.py", line 153, in <module> hand = Hand() File "demo.py", line 37, in __init__ attention_mixture_components=10 File "/home/john/handwriting-synthesis/rnn.py", line 85, in __init__ super(rnn, self).__init__(**kwargs) File "/home/john/handwriting-synthesis/tf_base_model.py", line 108, in __init__ self.graph = self.build_graph() File "/home/john/handwriting-synthesis/tf_base_model.py", line 399, in build_graph self.loss = self.calculate_loss() File "/home/john/handwriting-synthesis/rnn.py", line 204, in calculate_loss lambda: self.sample(cell) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 432, in new_func return func(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 2063, in cond orig_res_t, res_t = context_t.BuildCondBranch(true_fn) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1913, in BuildCondBranch original_result = fn() File "/home/john/handwriting-synthesis/rnn.py", line 203, in <lambda> lambda: self.primed_sample(cell), File "/home/john/handwriting-synthesis/rnn.py", line 162, in primed_sample scope='rnn' File "/home/john/handwriting-synthesis/rnn_ops.py", line 246, in rnn_free_run states, outputs, final_state = raw_rnn(cell, loop_fn, scope=scope) File "/home/john/handwriting-synthesis/rnn_ops.py", line 40, in raw_rnn if context.in_graph_mode(): AttributeError: 'module' object has no attribute 'in_graph_mode'

Please state license

I realize this wasn't originally developed to be released as a project, but it the code has been posted and no license has been declared. This leaves a really weird limbo situation where the worst-faith actors are the most free to take advantage of the situation but honest actors who build systems that stick to licensing terms can't do anything with this at all.

It also inhibits contributions! You state in the readme that you'd be interested in accepting contributions which helped make this into a package and made it more usable. I'd be willing to contribute some towards that, but not until the licensing is clarified.

Issue #16 also broached this topic, but the OP unilaterally decided what they wanted it to be and then closed the issue and ran away. I also think the MIT license would be a good candidate for this project, but the original author(s) need to state their intention in this regard.

I'd be happy to help with advice on what different licenses are good for and how to pick. Once you do pick I'm happy to contribute the proper files to this repo so that it's properly licensed and usable by others. But I can't make the call what that license will be.

Custom Data???

is there any pre existing way that anyone has come across to make custom data in the correct format?

Python 3 TypeError: unsupported operand type(s) for +: 'map' and 'list'

  • python3 -m pip install -r requirements.txt # after #5 is merged
  • python3 demo.py
Traceback (most recent call last):
  File "demo.py", line 153, in <module>
    stroke_widths=stroke_widths
  File "demo.py", line 42, in write
    strokes = self._sample(lines, biases=biases, styles=styles)
  File "demo.py", line 61, in _sample
    c_p = drawing.encode_ascii(c_p)
  File "~/handwriting-synthesis/drawing.py", line 77, in encode_ascii
    return np.array(map(lambda x: alpha_to_num[x], ascii_string) + [0])
TypeError: unsupported operand type(s) for +: 'map' and 'list'

creating your own style?

is there a script that allows us to create our own style files from drawing, or a link to the program that was used to generate them? ^_^

Doctor style?

Any chance of featuring extreme style like doctor?
image

digits

I have an issue with the digits. In most of my test cases, digits are not produced very well and they are not readable. is it something that I can fix with re-training?

Characters 'Q', 'X' and 'Z' are missing

The defined alphabet is missing 'Q', 'X' and 'Z' characters. Is there any reason behind this?

EDIT: Answering myself, this is probably due to low number of samples for those characters in the dataset.

Cannot convert a symbolic Tensor to a numpy array.

I kept getting this error message when I was running this program. If you also run into this issue the resolution is:

Replace

np.ones_like in the rnn_cell.py file with keras.backend.ones_like.

You will also have to import keras as well so add from tensorflow import keras to the top of the rnn_cell.py file as well.

Unable to write characters such as "@", "+" or "✓", even after adding them to the valid_char_set. Problem with _sample function.

Hi,

I attempted to generate handwritten "@", "+" and "✓" signs, but was unable to.
E.g. the phone number +1234, and the email [email protected] will not generate anything at all.

Within the "write" function of demo.py (line 42) => valid_char_set = set(drawing.alphabet), alphabet is called from drawing.py. Therefore, I modified within drawing.py (see line 10), the list alphabet by adding the new characters. Adding "@" or "+" to the list caused major problems as even regular words such as "John Smith" can no longer be written i.e. no handwriting output at all.

I have identified where within demo.py, the code breaks after adding the new characters to the list. The line strokes = self._sample(lines, biases=biases, styles=styles) (line 61), calls the function _sample. Within _sample, it is from line 94 to line 106, which fails (see below)

    [samples] = self.nn.session.run(
        [self.nn.sampled_sequence],
        feed_dict={
            self.nn.prime: styles is not None,
            self.nn.x_prime: x_prime,
            self.nn.x_prime_len: x_prime_len,
            self.nn.num_samples: num_samples,
            self.nn.sample_tsteps: max_tsteps,
            self.nn.c: chars,
            self.nn.c_len: chars_len,
            self.nn.bias: biases
        }
    ) 

Would anyone be able to provide an explanation and solution for this please? i.e. explanation to why nothing is drawn at all, and why adding an additional character could not solve this. And what the solution would be to generate handwriting for "@", "+" and "✓"?

Many Thanks!

PS: I also attempted on the web demo to create"@", "+" and "✓". Although not drawn properly, at least handwriting can be generated.

Dataset requirements

How can I create a new dataset from images of handwriting? Are the XML files in the IAM set some sort of SVG format that I could interpret my images into? How many writing samples should it take to decently train the model?

Upgraded code to run on Python 3.8 and TensorFlow 2?

I haven't found the version of this repo to work with latest Python 3.8 and TensorFlow 2.4, so working on updating it.

I used the TensorFlow upgrade scripts recommended by TensorFlow to convert the code from 1.X to 2.X. It can be found here https://www.tensorflow.org/guide/upgrade

Haven't completely converted yet, but working on it. Looks like there are some functions that are deprecated. Is anyone else working on upgrading to work with TF 2? Thanks

Rick Astley

Demo 3 is "Rick Astley - Never Gonna Give You Up", not "Leonard Cohen - Hallelujah".

int32 does not support GPU computations.

At multiple places int32 is being used for tensorflow computations. As you know, this prevents the code from using GPU. See this StackOverflow.

Could you please elaborate on why using float64 be a bad idea?
How much would the code need to be changed to make this happen?
Is this doable?

About API

Why not make an API and let Python or other languages can use it?
My Idea:
- Python post a request to it,it has "text","style" and other keys.
- It recive a key named "text" and do something for it.
- It return a picture and it will named "xx.png"
Maybe.If the idea comes true,I will thanks a lot.

Increasing validation loss

I just finished running a re-training session and the results were extremely impressive! However I'm noticing a loss in numbers. What are some ways I could improve the training?

Sometimes unexpected results with this string

Enter this string "Hawaii lieben leidige Fijii"
Style 6
Speed , legibility and stroke width at default .

After "Fijii" the Ai continues with another ~6 characters of jibber jabber in > 50% of the cases.

That might be an interesting corner case too look at.

Thanks for the great work.

How to train it on other language's?

Hello 👋 I would like to know if it possible to train a model on other languages like Hebrew, if u can help me with that I will be very happy 😊 THX🙏

Is this the code underpinning https://www.calligrapher.ai ?

Ran across https://www.calligrapher.ai on HN and someone mentioned that this repo appeared to be the source for that (or at least, the tech used). If so, that might be cool to call out in the readme.

There's technically no license in this repo, so depending on where you live this code is all rights reserved despite what's at the bottom of the readme, so if that's just someone who used your code without informing you, I guess you might now know >_>

Can not concatenate dict_values to list

I am trying to train the model. I have prepared the data by running prepare_data.py. When I run the rnn.py I am getting the following error:

Traceback (most recent call last):
File "rnn.py", line 235, in
nn.fit()
File "D:\Data\handwriting-synthesis-master\handwriting-synthesis-master\tf_base_model.py", line 163, in fit
fetches=[self.loss] + self.metrics.values(),
TypeError: can only concatenate list (not "dict_values") to list

Waiting for your reply. Thanks...

stroke source

Hi. where did you get the stroke.npy from for each of the styles?

Public Forum for this Topic and Adding a Slant

Hi Everyone,
Is anyone aware of a public forum to tackle questions about this information? I believe this part of the forum is for technical bug's more then Q&A.
I would like to add a slight slant to the end of the sentences as most normal handwriting takes this into account as they slide to the end of the paper.

Modify generated text by hand

I would like to generate some words with accentuation marks, like those in Spanish, Portuguese or French. I know there is the RHIMES dataset that includes accents from French, but I am willing to generate spanish-only forms like ñ or ü. Take the next .svg for example:

"españa"

Is there any way to manually add a - above the n during the generation process? Perhaps splitting the word before getting the samples and apply it just to the n?

The current model does not support capital Q and Z

Hi,

When I give words with capital Z and Q in them, I get a complaint that the model does not have those letters as its trained letters. However, I noticed that your web demo has not issue with those letter.
Could you please share your model that includes those characters?

Would't run on tensorflow 1.6.0

I tried to install an older setup and still doesn't work.

Python: 3.5.4
TensorFlow: 1.6.0

Traceback (most recent call last):
  File "R:\handwriting-synthesis-master\demo.py", line 9, in <module>
    from rnn import rnn
  File "R:\handwriting-synthesis-master\rnn.py", line 9, in <module>
    from rnn_cell import LSTMAttentionCell
  File "R:\handwriting-synthesis-master\rnn_cell.py", line 16, in <module>
    class LSTMAttentionCell(tf.compat.v1.nn.rnn_cell.RNNCell):
AttributeError: module 'tensorflow.python.util.compat' has no attribute 'v1'

would you please give us a working environment? which python+tensorflow version would be appropriate?

Problem with setup - versions control

I am having trouble with my installations, and would love to know from one of you who did manage to install and run what are your versions.
I tried:

Python3.7 with tensorflow 1.8/1.6/1.5 - all leading to missing modules such as:
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
I followed this thread about it: tensorflow/tensorflow#20690

Python3.6.8 with tensorflow 1.6.0:
tensorflow couldn't be installed due to
protobuf requires Python '>=3.7' but the running Python is 3.6.8

Wound't run on current python/tensorflow

The results are impressive, perhaps you can upgrade to using tf 2.x OR give instructions on how to install using tf 1.x. OR allow us to use calligrapher.ai programmatically.

I'm trying to create a portable windows library of it but tensorflow.contrib must be removed.

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.