<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikolai Körber on Medium]]></title>
        <description><![CDATA[Stories by Nikolai Körber on Medium]]></description>
        <link>https://medium.com/@nikkoerber?source=rss-78f729b870d6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*BSEkIeNVgnqKuG8IOJJ4Bg.jpeg</url>
            <title>Stories by Nikolai Körber on Medium</title>
            <link>https://medium.com/@nikkoerber?source=rss-78f729b870d6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 03:21:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nikkoerber/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ScrabbleGAN — Adversarial Generation of Handwritten Text Images]]></title>
            <link>https://medium.com/data-science/scrabblegan-adversarial-generation-of-handwritten-text-images-628f8edcfeed?source=rss-78f729b870d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/628f8edcfeed</guid>
            <category><![CDATA[handwriting]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[generative-adversarial]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[Nikolai Körber]]></dc:creator>
            <pubDate>Mon, 11 May 2020 15:42:55 GMT</pubDate>
            <atom:updated>2020-11-12T07:58:25.277Z</atom:updated>
            <content:encoded><![CDATA[<h3>ScrabbleGAN — Adversarial Generation of Handwritten Text Images</h3><h4>A Tutorial on Text Generation (TensorFlow 2)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WsFF-Mx-XJemtkwBD1l3KQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@moroo">Moritz Schmidt</a> on <a href="https://unsplash.com/photos/UsTERdf7yEY">Unsplash</a></figcaption></figure><h3><strong>1. Motivation</strong></h3><p>This article presents an <a href="https://github.com/Nikolai10/scrabble-gan">open source project</a> for the adversarial generation of handwritten text images, that builds upon the ideas presented in [1, 2] and leverages the power of generative adversarial networks (GANs [3]). Although GANs (in particular cGANs [4] and variations) have received a lot of attention in the research community, little work has focused on image generation conditioned on sequences so far (not to be confused with text to image learning tasks such as [5] or [6]). Moreover, recent works ([1, 2]) have not shared their source/ evaluation code, which makes a clean comparison difficult as it is not always clear which component has led to an improvement. In this work the model/ software is evaluated on the <a href="http://www.fki.inf.unibe.ch/databases/iam-handwriting-database">IAM Handwriting Database</a> and is made available to the community for further experiments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*VJgCUPL5wslVNfILpisFaQ.gif" /><figcaption>ScrabbleGAN: Training progress between the 1st and 15th epoch (IAM Handwriting Database)</figcaption></figure><p><strong><em>Note:</em></strong><em> this article is not intended to explain state-of-the-art machine learning algorithms, but rather to highlight the design decisions made in this project. Technical details are well documented in the </em><a href="https://github.com/Nikolai10/scrabble-gan"><em>GitHub repo</em></a><em> and can be found in the respective references.</em></p><h3>2. Background/ Relevance</h3><p>In many institutions (e.g. in healthcare or finance) handwriting is still the predominant method of data collection. Although Optical character recognition (OCR) is nowadays often considered as solved, (offline) Handwriting Recognition (HWR) still remains challenging and thus complicate automatic data processing. In contrast to printed text, handwriting greatly varies in style (handwriting is known to be as unique and unmistakable as a fingerprint) and therefore requires large-scale training data. As a consequence, and due to the popularity and success of GANs, more and more works have focused on extending existing datasets. In the following section two recent papers (which this article heavily relies on) are introduced.</p><p>In [1], the authors have trained an Auxiliary Classifier GAN (AC-GAN [7]) in order to extend two benchmark datasets (French, Arabic). The main idea here is to embed each word into a 128-dimensional feature vector (similar to [5]), which is then fed into a BigGAN [8] network architecture. In order to control the textual content of the generated images a CRNN [9] is trained as auxiliary classifier. The gradients stemming from R and D are balanced (to account for their different magnitude) and finally back-propagated to G:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*5ZY6NuVEcbiAhchHaC6YRg.png" /><figcaption><a href="https://arxiv.org/pdf/1903.00277.pdf">source</a></figcaption></figure><p>Although their work has shown promising results, several limitations still exist and are addressed in [2]:</p><ul><li><strong>Generation of word images of arbitrary length:</strong> <br>in [1] all images are (white-) padded to equal size while maintaining the original aspect ratio — images that exceeded a given threshold (128x512) are removed. As a consequence, only word images of equal size can be generated across all word lengths. In [2] both G, D, and R follow the fully convolutional paradigm, which potentially allows the generation of whole sentences.</li><li><strong>Disentanglement of generation process/ embeddings:<br></strong>instead of generating the image out of an entire word representation as done in [1], each character is generated individually (hence <em>Scrabble</em>), using CNN’s property of overlapping receptive fields to account for the influence of nearby letters. This is mainly motivated by the fact that handwriting is a local process, where each letter is influenced only by its predecessor and successor. As a result, G (D) can be seen as concatenation of identical class conditional generators (real/fake classifiers):</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*203XZJo3QLRk5wUjifGPkA.png" /><figcaption><a href="https://arxiv.org/pdf/2003.10557.pdf">source</a></figcaption></figure><ul><li><strong>Removal of recurrent head in R<br></strong>the authors [2] have found, that a better R does not necessarily lead to a better overall (generation) performance. This is because the RNN layers within the CRNN network learn an implicit language model and are thus able to identify the correct character, even if it is not clearly written. Therefore, to force R to make decisions based only on visual features, the recurrent head is removed.</li></ul><h3>3. Methodology</h3><p>In this work the <a href="https://www.tensorflow.org/guide/keras/functional">functional API</a> and a <a href="https://www.tensorflow.org/tutorials/customization/custom_training">custom training loop</a> is used. The general structure is based on the TensorFlow tutorial on <a href="https://www.tensorflow.org/tutorials/generative/dcgan">Deep Convolutional Generative Adversarial Network</a>; the main differences are highlighted in the following section.</p><h4>Load and prepare the dataset</h4><p>First, the IAM Handwriting (words) Database ist converted into the following format:</p><pre><em>res/data/iamDB/words-Reading/<br>    ├── Bucket 1<br>        ├── img_i.png<br>        ├── img_i.txt<br>        ├── ...<br>    ├── Bucket 2<br>        ├── ...<br>    ├── Bucket n</em></pre><p>where<em> bucket x</em> holds all samples with transcription length <em>x </em>and<em> img_i.txt</em> contains the ground truth annotation (transcription) for <em>img_i.png.</em></p><p>To reduce noise, only words ([a-zA-Z]) which were segmented correctly (<a href="http://www.fki.inf.unibe.ch/DBs/iamDB/data/ascii/words.txt">denoted as ‘ok’</a>) are considered in this work. Furthermore, words of size &gt; <em>bucket_size (</em>here 10<em>) </em>are ignored to assert len(bucket) &gt;&gt; batch_size. In total, this preprocessing policy leads to 80.377 unique training samples.</p><p>All images are reshaped to match the aspect ratio of each character proposed by [2]: (32, 16). E.g. a word of transcription length 10 has shape (32,160) px.</p><p>Finally, similar to the <a href="https://www.tensorflow.org/tutorials/generative/dcgan">DCGAN-Tutorial</a>, images are normalized to [-1, 1] and labels are encoded by means of a char_vector (here: ‘abc…zA…Z’): e.g. ‘auto’ becomes [0, 20, 19, 14].</p><p>At each training step, a batch of random words along its encoded transcriptions (of the same random bucket) is yielded to the respective models.</p><h4>Create the models</h4><p>Fundamentally, ScrabbleGAN is a mixture of the famous BigGAN and CRNN architecture. Although there are some open source implementations of BigGAN (e.g. <a href="https://github.com/ajbrock/BigGAN-PyTorch">BigGAN-Pytorch</a>, <a href="https://github.com/google/compare_gan/blob/master/compare_gan/architectures/resnet_biggan.py">resnet_biggan</a>) — to the best of my knowledge — there is currently no “official” TF2 implementation available. As a result, I decided to rewrite a simplified version of the <a href="https://github.com/google/compare_gan">google/compare_gan</a> in Tf2 myself. Similar to the original work, the code is configurable via <a href="https://github.com/google/gin-config">Gin</a>:</p><pre>...<br>setup_optimizer.r_lr = 2E-4<br>setup_optimizer.beta_1 = 0.0<br>setup_optimizer.beta_2 = 0.999<br>setup_optimizer.loss_fn = @hinge<br>setup_optimizer.disc_iters=1<br>...<br>shared_specs.kernel_reg = @spectral_norm<br>...<br>shared_specs.g_bw_attention = &#39;B3&#39;              <br>shared_specs.d_bw_attention = &#39;B1&#39;<br>...<br>shared_specs.embed_y = (32, 8192)</pre><p>The CRNN-model is based on <a href="https://github.com/Nikolai10/crnn-tf2">CRNN-Tf2</a>. All models are implemented making use of the fully convolutional paradigm as described in section 2, to allow for flexible input and output shapes. The final “ScrabbleGAN” model is obtained by stacking these models together (composite model).</p><p>For the sake of brevity, the interested reader is referred to the <a href="https://github.com/Nikolai10/scrabble-gan">GitHub repo</a> for more information.</p><h4>Define the loss and optimizers</h4><p>In this work both the “non_saturating” and “hinge” loss are considered, which are based on <a href="https://github.com/google/compare_gan/blob/master/compare_gan/gans/loss_lib.py">compare_gan/gans/loss_lib.py</a>. For the CRNN part the <a href="https://www.tensorflow.org/api_docs/python/tf/keras/backend/ctc_batch_cost">CTC loss</a> is used as usual.</p><h4>Define the training loop</h4><p>The training loop begins with the generator receiving a set of random seeds and transcriptions from a random word list (here: <a href="https://superuser.com/questions/136236/where-on-disk-does-the-macos-system-wide-dictionary-store-the-list-of-words-i-ad">macOS word list</a>) as input to produce a set of fake images. While D(real) and R(real) are computed separately, D(fake) and R(fake) are now computed through the composite model. The loss is then calculated for each of these models, and the gradients are used to update the discriminator, recognizer and composite model. E.g. the latter is updated as follows:</p><pre>recognizer.trainable = <strong>False<br></strong>discriminator.trainable = <strong>False<br></strong>gradients_of_generator = gen_tape.gradient(g_loss_balanced, composite_gan.trainable_variables)<br>generator_optimizer.apply_gradients(zip(gradients_of_generator, composite_gan.trainable_variables))</pre><blockquote>Note: in this work, no gradient balancing has led to the best results (more information can be found in the respective references [1, 2].)</blockquote><p>Similar to [1, 2], R is trained only on real images, to prevent it from learning how to recognize generated (and potentially false) images.</p><h3>4. Results</h3><p>All experiments (different model configurations) were conducted using an <a href="https://aws.amazon.com/de/ec2/instance-types/p3/">AWS p3.2xlarge instance</a>; each model was trained for 15 epochs. Here are some visual results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Oy13ewQPvtuFud7a_nQc9g.png" /><figcaption>Comparison to [2]: The word “Supercalifragilisticexpialidocious” (34 letters) from the movie “Mary Poppins”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*fQ3DfQK-AK_eBgj5LcyW0A.png" /><figcaption>some more examples I</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*NdFiA4IP-vShed5xVYi34g.png" /><figcaption>some more examples II</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*EEaRWF75_SXfOEwGYEjTcw.png" /><figcaption>some more examples III</figcaption></figure><h4>References</h4><ol><li>E. Alonso, B. Moysset, R. Messina, <a href="https://arxiv.org/pdf/1903.00277.pdf">Adversarial Generation of Handwritten Text Images Conditioned on Sequences</a> (2019), arXiv:1903.00277</li><li>S. Fogel, H. Averbuch-Elor, S. Cohen, S. Mazor, R. Litman, <a href="https://arxiv.org/pdf/2003.10557.pdf">ScrabbleGAN: Semi-Supervised Varying Length Handwritten Text Generation</a> (2020), arXiv:2003.10557</li><li>I. J. Goodfellow, J. Pouget-Abadie, M. Mirza, B. Xu, D. Warde-Farley, S. Ozair, A. Courville, Y. Bengio, <a href="https://arxiv.org/abs/1406.2661">Generative Adversarial Networks</a> (2014), arXiv:1406.2661</li><li>M. Mirza, S. Osindero, <a href="https://arxiv.org/abs/1411.1784">Conditional Generative Adversarial Nets</a> (2014), arXiv:1411.1784</li><li>S. Reed, Z. Akata, X. Yan, L. Logeswaran, B. Schiele, H. Lee, <a href="https://arxiv.org/pdf/1605.05396.pdf">Generative Adversarial Text to Image Synthesis</a> (2016), arXiv:1605.05396</li><li>T. Qiao, J. Zhang, D. Xu, D. Tao, <a href="https://arxiv.org/abs/1903.05854">MirrorGAN: Learning Text-to-image Generation by Redescription</a> (2019), arXiv:1903.05854</li><li>A. Odena, C. Olah, J. Shlens, <a href="https://arxiv.org/pdf/1610.09585.pdf">Conditional Image Synthesis With Auxiliary Classifier GANs</a> (2016), arXiv:1610.09585</li><li>A. Brock, J. Donahue, K. Simonyan, <a href="https://arxiv.org/abs/1809.11096">Large Scale GAN Training for High Fidelity Natural Image Synthesis</a> (2018), arXiv:1809.11096</li><li>B. Shi, X. Bai, C. Yao, <a href="https://arxiv.org/pdf/1507.05717.pdf">An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition </a>(2015), arXiv:1507.05717</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=628f8edcfeed" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/scrabblegan-adversarial-generation-of-handwritten-text-images-628f8edcfeed">ScrabbleGAN — Adversarial Generation of Handwritten Text Images</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>