Multi-categorical text classification with LSTM

I created the prototype of a web application for customer service that uses sequence classification with Keras. This prototype’s purpose is to reply the proper response of some categories to our customer are based on the questions customer sent to us. The questions relate to some categories, and then the application predicts to which category a question belongs.

If you are looking for the same situation, this sample might be helpful for you.

You can see the whole source code in GitHub.

Collect text data

Before creating a classification model, collect data set for creating it. Many classification’s articles on the internet use the IMDB movie review data set, I think. Instead, I use customer services’ question and its categories in our product. I collected this data and store as TSV file.

File format

The format is TSV, and it consists id, question, answer, and the category of question like this:

id question answer category

This raw data set has about 9000 samples. But they include unusable data and have about 15 categories of question.

Load data

Load data from TSV formatted file.

import json
import numpy as np
import csv

issues = []

with open("data/issues.tsv", 'r', encoding="utf-8") as tsv:
    tsv = csv.reader(tsv, delimiter='\t')

    for row in tsv:
        row = []
        row.append(row[1]) # question
        row.append(row[2]) # answer
        row.append(row[3]) # category

        issues.append(row)

Pre-process text

Remove unnecessary characters

These samples are rough for learning. It means that some sample has no question text, and has an e-mail address and symbol like a hyphen. So We have to remove these unnecessary characters.

I removed these with just regular expression and the
question which is an empty string like this:

filtered_text = []
text = ["長らくお時間を頂戴しております。version: 1.2.3 ----------------------------------------"]

for t in issues:
    result = re.compile('-+').sub('', t)
    result = re.compile('[0-9]+').sub('0', result)
    result = re.compile('\s+').sub('', result)
    # ... and many regular expression substitutions

    # remove empty string question
    if len(result) > 0:
        sub_texts.append(result)

    filtered_text.append(result)
    print("text:%s" % result)
    # text:長らくお時間を頂戴しております。

Create samples and labels

Create samples and labels from the data set. It has about 15 categories of labels. And I select two label types, ‘Account’ as two and ‘Payment’ as three; they are question’s categories. And add the other all labels as one which includes the other categories excepts Account, Payment. The samples and labels have to be the same size roughly because LSTM learning wouldn’t work well if one of these is more or less. In this case, cap the samples’ size it’s 700 samples because the payment label has only 688 samples.

Create samples and labels

labels = []
samples = []
threshold = 700
cnt1 = 0
cnt2 = 0
cnt3 = 0

for i, row in enumerate(filtered_samples):
    if 'Account' in row[2]:
        if cnt2 < threashold:
            cnt1 += 1
            labels.append(2)
            samples.append(row[0])
    elif 'Payment' in row[2]:
        if cnt3 < threashold:
            cnt3 += 1
            labels.append(3)
            samples.append(row[0])
    else:
        if cnt1 < threashold:
            cnt1 += 1
            labels.append(1)
            samples.append(row[0])

filtered_samples is what we removed some symbols, e-mail address or something like these from the samples.

Separate the words by MeCab

The questions in the samples written in Japanese. So have to separate words into each word with space. Below is a question text in Japanese:

長らくお時間を頂戴しております

I used MeCab to get space-separated words:

import MeCab
import re

def tokenize(text):
    wakati = MeCab.Tagger("-O wakati")
    wakati.parse("")
    words = wakati.parse(text)

    # Make word list
    if words[-1] == u"\n":
        words = words[:-1]

    return words

texts = [tokenize(a) for a in samples]

This tokenize function returns space-separated words:

長らく お 時間 を 頂戴 し て おり ます

Divde the samples and labels

Divide the samples and labels into training data and validation data:

from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import numpy as np
from keras.utils.np_utils import to_categorical

maxlen = 1000
training_samples = 1600 # training data 80 : validation data 20
validation_samples = len(texts) - training_samples
max_words = 15000

# create word index
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)

word_index = tokenizer.word_index
print("Found {} unique tokens.".format(len(word_index)))

data = pad_sequences(sequences, maxlen=maxlen)

# to binary class matrix
categorical_labels = to_categorical(labels)
labels = np.asarray(categorical_labels)

print("Shape of data tensor:{}".format(data.shape))
print("Shape of label tensor:{}".format(labels.shape))

# shuffle indices
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]

x_train = data[:training_samples]
y_train = labels[:training_samples]
x_val = data[training_samples: training_samples + validation_samples]
y_val = labels[training_samples: training_samples + validation_samples]

The data is integer sequese like this:

[0, 0, 0, 10, 5, 24]

Each non-zero integer relates to a word and the zero stands for “empty word.” Therefore, this words size is just three and the rest of the sequence will be filled with zero.

Create a model and learn features

I used Keras for learning features. It includes LSTM and Word embedding. LSTM is used for a sequence classification problem, sequence regression problem and so on.

Create a model

from keras.models import Sequential
from keras.layers import Flatten, Dense, Embedding
from keras.layers import LSTM

model = Sequential()
model.add(Embedding(15000, 100, input_length=maxlen))
model.add(LSTM(32))
model.add(Dense(4, activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()

This model learns with LSTM and also word embedding with Embedding(...) at the same time. We can also use pre-trained word embedding instead learning word embedding.

Learn features

Just call model.fit()

history = model.fit(x_train, y_train, epochs=15, batch_size=32, validation_split=0.2, validation_data=(x_val, y_val))

Plot the result

%matplotlib inline

import matplotlib.pyplot as plt

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) + 1)

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()

The result is like this:

Finally, the validation accuracy becomes about 90 percent.

Save the model

Save the model and weights learned.

model.save('pre_trained_model.h5')

Create a web application

I wanted to use the pre-trained model with a web application. So I used Flask this time because its language is the same as Keras. And this application is simple, receives a text, predicts and then responses its category to the user. This application has a text area, an ask button and the result of a prediction.

Predict a certain question

Before predicting a text, we have to calculate the word index the same as we created for creating the pre-trained model.

app.py

# load the pre traind model
model = load_model('../pre_trained_model.h5')

# we have to pass padded_seq as 2-dimentional array
result = model.predict([padded_seq])

Get the classified result:

np.argmax(res[0])

Please see the whole source code in my repository.

Reference

Deep Learning with Python This book helpful for me!

Use Mono 2.X on Ubuntu 14.04 LTS

Unfortunately, I had to use the Mono which version is 2.X in my project. It’s very old, released in 2012! So I installed it on Ubuntu 16.04 LTS from source and do make. Then the error occurred in doing make like this:

./.libs/libmini-static.a(libmini_static_la-mini.o): In function `mono_get_jit_tls_offset':
/home/vagrant/mono-2.11.4/mono/mini/mini.c:2506: undefined reference to `mono_jit_tls'
/home/vagrant/mono-2.11.4/mono/mini/mini.c:2506: undefined reference to `mono_jit_tls'
collect2: error: ld returned 1 exit status
Makefile:1351: recipe for target 'mono' failed
make[4]: *** [mono] Error 1
make[4]: Leaving directory '/home/vagrant/mono-2.11.4/mono/mini'
Makefile:1209: recipe for target 'all' failed
make[3]: *** [all] Error 2
make[3]: Leaving directory '/home/vagrant/mono-2.11.4/mono/mini'
Makefile:344: recipe for target 'all-recursive' failed
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory '/home/vagrant/mono-2.11.4/mono'
Makefile:419: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/vagrant/mono-2.11.4'
Makefile:344: recipe for target 'all' failed
make: *** [all] Error 2

So I ended up using Ubuntu 14.04 LTS for Mono 2.X.

The error might occur by the libraries that are gcc or something related to compilation.

How to install CUDA and cuDNN on Ubuntu 16.04 LTS

I’ve been running this machine for TensorFlow and Keras with Jupyter notebook.

These are my environments:

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

$ lspci | grep VGA
01:00.0 VGA compatible controller: NVIDIA Corporation GP104 [GeForce GTX 1080] (rev a1)

Disable secure boot on UEFI

First, we need to disable “secure boot” on UEFI menu because it prevents NVIDIA driver from loading into OS. So enter UEFI menu while boot time by pressing the key something like the delete key.

That’s the first time I heard that. I used to use BIOS, but now, a lot of motherboards recently released use UEFI. And UEFI has secure boot (see detail here, this was helpful for me).

If you use an ASUS motherboard, this document also can be helpful.

Remove old NVIDIA driver and CUDA

$ dpkg -l | grep nvidia
$ dpkg -l | grep cuda
$ sudo apt-get --purge remove nvidia-*
$ sudo apt-get --purge remove cuda-*

Install new NVIDIA driver and CUDA9.0

Download CUDA Toolkit 9.0

Download from NVIDIA developer site.

Instatll

$ sudo dpkg -i cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
$ sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get install cuda

Add PATH and LD_LIBRARY_PATH

~/.bashrc

export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"

Then reboot.

sudo reboot

Check that the driver was installed

$ nvidia-smi

This command shows the status of the graphics card installed on your machine like this:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.25                 Driver Version: 390.25                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1080    Off  | 00000000:01:00.0  On |                  N/A |
| 27%   36C    P8    12W / 180W |   7753MiB /  8116MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1351      G   /usr/lib/xorg/Xorg                            24MiB |
|    0     12638      C   /home/jupyter/tensorflow/bin/python3        7717MiB |
+-----------------------------------------------------------------------------+

Install cuDNN

Download cuDNN

Download Debian package from NVIDA developer site.

Install the rutime library

$ sudo dpkg -i libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb

Install the developer library

$ sudo dpkg -i libcudnn7-dev_7.0.5.15-1+cuda9.0_amd64.deb

Add CUDA_HOME

~/.bashrc

export CUDA_HOME=/usr/local/cuda-9.0

Check that the cuDNN works

$ cp -r /usr/src/cudnn_samples_v7/ $HOME
$ cd  $HOME/cudnn_samples_v7/mnistCUDNN
$ make clean && make
$ ./mnistCUDNN
Test passed!

Stop LightDM

LightDM start to run when you install CUDA. If you use Ubuntu server, it’s not necessary to run.

Disable LigthDM

Rewrite GRUB_CMDLINE_LINUX like below:

/etc/default/grub

GRUB_CMDLINE_LINUX="systemd.unit=multi-user.target"

Update and reboot.

sudo update-grub
sudo reboot

The solution of “​<Error>: Could not successfully update network info during initialization.”

What happened

The error message like below happened on iOS when launching an app and crash immediately.

​<Error>: Could not successfully update network info during initialization.

Before this message happened, I purchased a lot of auto-renewal subscriptions, about 300 times, and restore over and over again for testing purchasing subscriptions.

The solution

Restore your device to factory settings. For me, the error solved by it.

Reproduce transactions as new transaction id when repurchasing an auto-renewable subscription

I was building an app which has auto-renewable subscriptions.
When I try to resubscribe the subscription I subscribed before, then iOS shows the dialog, “You’re currently subscribed to this.”, And enqueue all of the transactions I purchased in the past into a default transaction queue. The statuses of the transactions are SKPaymentTransactionStatePurchased. As a result of this behavior, I have to process vast of transactions. This happening was in the Sandbox environment.
 
I expected to enqueue only one transaction as SKPaymentTransactionStatePurchased.
 
The following list is my scenario that reproduces all transactions:
1. Subscribe a product
2. Subscribe the product purchased again
3. The dialog, “You’re currently subscribed to this.”, is popped up
4. Tap ok button
5. Press Home button then close my app
6. Tap and launch my app again

I tried to solve this problem throughout the day. Finally, I created a new sandbox user and just used it for purchasing subscription. Then this behavior has no longer occurred. I don’t know why such things happened.

The test cases for In-App Purchase on iOS

I came up to implement In-App Purchase on iOS with Unity.
I looked for the test cases for In-App Purchase, however, couldn’t find their cases. Therefore, I memorize the test cases I checked myself.

At first, please read In-App Purchase Best Practice. We should implement your In-App Purchase according to this document.

For instance, I implement below list:

  • Add a transaction queue observer at application launch
  • Query the App Store for product information before presenting your app’s store UI

Figure 1 shows the process, second of the list, from showing available products to purchasing them.

Figure 1.

  1. Retrieve product identifiers from a data store
  2. Request products from Apple Store
  3. Show available products on an app

Below, the test cases I checked. the left side of the table are test cases and the right side is expected results.

Purchasing and Subscribing test cases

Test case Expected result
Show a product list scene The product list scene shows buyable products
Buy products All products can buy
Close an app in the middle of a purchasing The item corresponds to the purchased product is given the user next launch
Disconnect internet connection in the middle of a purchasing The items correspond to the purchased are given the user next startup
Purchase a subscription product in advance. Switch an Apple account to another one on the same Device The purchased subscription product must get unbuyable
Purchase a subscription product by the same Apple account on deferent devices (purchase a product on Device-A, then purchase it on Device-B) Must purchase the product on Device-A and must not purchase it on Device-B
Purchase a subscription product with the same Apple account among two devices Must purchase the product on both devices
Purchase a subscription product in advance and close the app completely. Wait until the subscription is updated by Apple Store and launch the app Must give the user the items corresponding to the subscribed product

Restoring

Implement a restoring product for subscription products.

Test case Expected result
Restore the subscription after the subscription product is renewed The app gives the items that corresponding to the subscription to the user | Buy a subscription product with the same Apple account. Restore Device-A, next Device-B
Buy a subscription product in advance. Use both of devices, Device-A and Device-B. Then restore Device-A next Device-B An app gives the items corresponding to the subscription to Device-A, although it doesn’t on Device-B
Launch an app after closing the app in the middle of Restoring Purchased Products Do not execute unprocessed transactions
Sign out from App Store, then launch an app The user is prompted to sign in App Store if unprocessed transactions are existed
Tap Buy a product button multiple times Show only one In-App Purchase dialog
Use unregistered product identifiers in iTunes connect that it’s in part of an app’s buyable item list Do not show the unregistered products in app’s item list

CoreMLを使ったGenerative Adversarial NetworkのiOS Appを作る

English page

iOS上でCoreMLを使って手書き文字を生成するアプリを作成しました

手書き文字の生成からアプリの公開までをまとめておきたいと思います。

使ったソフトウェア

  • Xcode 9.2 (9C40b)
  • Docker 17.09.1-ce
  • TensorFlow docker imagesha256:1bb38d61d261e5c9230a1e60b5d200088eb03014fdb35a91859fa55ea0d2c4d5, https://hub.docker.com/r/tensorflow/tensorflow
  • TensorFlow 1.2.1
  • Keras 2.0.6
  • coremltools 0.7

手書き文字画像の生成からiOS appのリリースまで

図1は手書き文字画像の生成からiOS appのリリースまでの手順を表しています。

the flow of distributing an app
図1. iOS appリリースまでの手順

DockerのインストールしてTensorFlowとKerasが動作するJupyter Notebookを用意する

Kerasモデルを作成するためにDockerをインストールします。

以下のコマンドを実行します。

$ docker run -d --name notebook tensorflow/tensorflow:latest

KerasとCoreML converterをインストールする

$ docker exec -it notebook /bin/bash
$ pip install -U keras
$ pip install -U coremltools

Jupyter Notebookにアクセスできることを確認する

Jupyter NotebookにアクセスするためのURLを取得します。

$ docker logs notebook
# 以下のようなトークンがついたURLが表示されると思います
#
#    Copy/paste this URL into your browser when you connect for the first time,
#    to login with a token:
#        http://localhost:8888/?token=ecf1a2471670eb6863195ab530d6ac1d5cc27511faca0afe

上記のURLをブラウザで開くとアクセスできると思います。
これでKerasのコードを書けるようになります。

Kerasモデルを作成する

Jupyter Notebookに新しいノートを追加してGANモデルのコードを書きます。GANのコードはこちらを参考にしました。このコードをコピーしてcellに貼り付けます。モデルをディレクトリにセーブするために以下のようにコードを追加します。

if __name__ == '__main__':
    gan = GAN()
    gan.train(epochs=30000, batch_size=32, save_interval=200)
    gan.discriminator.save('./discriminator.h5') // discriminatorモデルを保存するディレクトリを指定します
    gan.generator.save('./generator.h5') // generatorモデルを保存するディレクトリを指定します

あとはcellを実行します。MacBook Pro 2016で実行した感じだと25分ほどかかりました。

このcellを実行すると学習済みモデルが指定のディレクトリに”.h5″のファイルが保存されます。

KerasモデルをCoreMLモデルに変換する

$ coremlconverter --srcModelPath ./keras_model.h5 --dstModelPath ./coreml_model.mlmodel --inputNames gunInput --outputNames ganOutput

このコマンドでKerasのモデルをCoreMLのモデルにコンバートします。コンバートが完了すると”.mlmodel”のCoreMLモデルファイルが生成されます。

手書き文字画像を生成するアプリを作成する

手書き文字画像を表示するにはWeb application、iOS、Androidや単純にJupyter Notebookを使う方法がありますが、iOSを触ってみたかったのでiOSにしました。

アプリを作成する

作成したアプリのソースコードはこちらで公開しています。

図2はアプリのスクリーンショットを示しています。


図2. アプリ (GANs generator)のスクリーンショット

このアプリはCoreMLを使用してKerasのモデルから28 x 28の手書き文字画像を生成します。手書き文字画像はUIKitを使用して表示します。また、手書き文字画像を再生成するボタンがあります。

手書き文字画像を生成する

最初にKerasモデルからコンバートしたCoreMLモデルをXcodeのプロジェクトにインポートします。図3のようになっていると思います。


図3. CoreMLモデルのプロパティ

Xcodeにモデルをインポートすると、モデルのクラスが自動的に生成されます。このクラスは手書き文字画像を生成するpredictionメソッドが実装されています。そして、predictionメソッドの第1引数にKerasモデルのinputが指定されます。第1引数のラベルにはCoreML converterの--inputNamesで指定した値が使用されます。この場合だとクラスのpredictionメソッドの第一引数のラベルをganInput、出力された手書き文字画像をganOutputとして定義しています。この定義は図3のように”.mlmodel”ファイルのModel Evaluation ParametersInputsOutputsで確認することができます。

// Create a gan instance
var model = gan()
// Generate a handwritten image
var output = model.prediction()

// render hand-written image
let HIGHT = 27
let WIDTH = 27

for i in 0...HIGHT {
  for j in 0...WIDTH {
    //create the path
    let plusPath = UIBezierPath()

    //set the path's line width to the height of the stroke
    plusPath.lineWidth = Constants.plusLineWidth

    //move the initial point of the path
    //to the start of the horizontal stroke
    plusPath.move(to: CGPoint(
      x: CGFloat(j * 10),
      y: CGFloat(i * 10) + Constants.plusLineWidth / 2
    ))

    //add a point to the path at the end of the stroke
    plusPath.addLine(to: CGPoint(
      x: CGFloat((j * 10) + 10),
      y: CGFloat(i * 10) + Constants.plusLineWidth / 2
    ))

    //set the stroke color
    let index: [NSNumber] = [0 as NSNumber, i as NSNumber, j as NSNumber]
    UIColor(white: CGFloat(truncating: out.gan_out[index]), alpha: CGFloat(1)).setStroke()

    //draw the stroke
    plusPath.stroke()
  }
}

このプロジェクトをXcodeで開いてシミュレーターで確認することができます。

Apple Developer Programに登録する

アプリの動作を確認したら、App Storeにこのアプリを公開します。もし、Apple Developer Programに登録してなけらば、こちらで登録を済ませます。アプリをApp Storeに公開するにはApple Developer Programの登録が必要です。

App Storeに登録する

詳しくは公式ドキュメントを参照してください。

  1. Archiving
    Product > Archive

  2. Validating
    archivingが完了するとArchive organizerValidateができるようになるのでApp Storeにアップロードする前に実行して、アプリに問題がないかチェックしておきます。

  3. App Storeにアプリをアップロードする
    Upload to App Storeをクリックします

アプリを公開する

詳しくは公式ドキュメントを参照してください。

  1. アプリをiTunes connectに登録する
  2. アプリのアイコンとプレビュー、スクリーンショットを追加する
  3. App Reviewに出す
  4. アプリをリリースする

Create a Generative Adversarial Network iOS App with CoreML

I created the app that generates a handwritten image with CoreML on iOS.

I’m going to explain the process to release this app from beginning to end.

The software versions

  • Xcode 9.2 (9C40b)
  • Docker 17.09.1-ce
  • TensorFlow docker imagesha256:1bb38d61d261e5c9230a1e60b5d200088eb03014fdb35a91859fa55ea0d2c4d5, https://hub.docker.com/r/tensorflow/tensorflow
  • TensorFlow 1.2.1
  • Keras 2.0.6
  • coremltools 0.7

The process to make handwritten image generator app on iOS and submit it to the App Store

As shown in figure 1, it illustrates the path of distributing this app through App Store.

  1. Create a GAN (Generative Adversarial Network) model on Keras
  2. Convert a Keras GAN model to CoreML model
  3. Create an app to show handwritten images generated by the CoreML model
  4. Distribute the app

the flow of distributing an app
Figure 1. The path of distributing this app through App Store

Install Docker and run Jupyter Notebook that includes TensorFlow and Keras

At first, To create a Keras model, we have to install Docker.

Install Docker

On Mac, get it in this page at the Stable channel and just install it.

Run Jupiter Notebook with TensorFlow

Fortunately, there is the Docker image that includes TensorFlow.

Just type this command:

$ docker run -d --name notebook tensorflow/tensorflow:latest

Install Keras and CoreML converter

$ docker exec -it notebook /bin/bash
$ pip install -U keras
$ pip install -U coremltools

Check that you can access the Jupyter Notebook

Before access it, you need to get an access token.

$ docker logs notebook
# You will find an access token like this:
#
#    Copy/paste this URL into your browser when you connect for the first time,
#    to login with a token:
#        http://localhost:8888/?token=ecf1a2471670eb6863195ab530d6ac1d5cc27511faca0afe

Copy the URL with an access token and access on your browser.

Now it’s done! You come can execute Keras code!

Make a Keras model

Add a new note to Jupyter Notebook and write a GAN model. I referred to this source code. Copy this code and paste to your Jupyter Notebook’s cell and add the code below. It will save a model in a directory.

if __name__ == '__main__':
    gan = GAN()
    gan.train(epochs=30000, batch_size=32, save_interval=200)
    gan.discriminator.save('./discriminator.h5') // Specify the directory saved the discriminator model
    gan.generator.save('./generator.h5') // Specify the directory saved the generator model

Then just run the cell. It took time about for 25 minutes on my MacBook Pro 2016 to training this model.

Keras create a learned model in the directory where executed the cell. It is followed by “.h5”.

Convert a Keras model to CoreML model

$ coremlconverter --srcModelPath ./keras_model.h5 --dstModelPath ./coreml_model.mlmodel --inputNames gunInput --outputNames ganOutput

This command uses TensorFlow in the background and converts to a CoreML model. After the converting, it makes a CoreML model is followed by .mlmodel in the specified directory. This file is a CoreML converted model from Keras model.

Create an app to show handwritten images

There are some ways to create an app that draws a handwritten image. For instance, as a web application or mobile application. This time, I created an app for iOS.

Create the app

Here is the source code for the app:
https://github.com/yanak/gangen/blob/master/Gangen/HandwrittenImage.swift

figure 2 is the app’s screenshot.


Figure 2. The GANs generator app’s screenshot

This app generates a 28 x 28 handwritten image using the CoreML model converted from Keras, and that renders the image by UIKit. There is the regenerate button that regenerates a handwritten image.

Generate a handwritten image

First, import the CoreML model converted from the Keras model to Xcode project directory. It shows like figure 3:


Figure 3. the CoreML model properties

After Xcode imports, a model, the class of a model is automatically generated by Xcode. this class has the prediction method and it specifies Keras model’s input as the first argument. The first argument’s label uses the name of the CoreML converter’s --inputName option value. In this case, The class defines the label of prediction as ganInput and a generated handwritten image’s shape as ganOutput. You can see its definitions in the Model Evaluation Parameters in the CoreML model(see figure 3.)

// Create a gan instance
var model = gan()
// Generate a handwritten image
var output = model.prediction()

// render hand-written image
let HIGHT = 27
let WIDTH = 27

for i in 0...HIGHT {
  for j in 0...WIDTH {
    //create the path
    let plusPath = UIBezierPath()

    //set the path's line width to the height of the stroke
    plusPath.lineWidth = Constants.plusLineWidth

    //move the initial point of the path
    //to the start of the horizontal stroke
    plusPath.move(to: CGPoint(
      x: CGFloat(j * 10),
      y: CGFloat(i * 10) + Constants.plusLineWidth / 2
    ))

    //add a point to the path at the end of the stroke
    plusPath.addLine(to: CGPoint(
      x: CGFloat((j * 10) + 10),
      y: CGFloat(i * 10) + Constants.plusLineWidth / 2
    ))

    //set the stroke color
    let index: [NSNumber] = [0 as NSNumber, i as NSNumber, j as NSNumber]
    UIColor(white: CGFloat(truncating: out.gan_out[index]), alpha: CGFloat(1)).setStroke()

    //draw the stroke
    plusPath.stroke()
  }
}

Open this project, you can check to run it in a simulator like this:

Enroll Apple Developer Program

After check running the app, submit it to the App Store! If you don’t enroll Apple Developer Program yet, you need to do it in here because it needs to submit the app to the App Store.

Submit to the App Store

In detail, see Submitting Your Apps

  1. Archiving
    To archive, on Xcode, Product > Archive

  2. Validating
    Xcode shows the archived app in the Archives organizer. Validate an archived app before uploading to App Store.

  3. Upload to iTunes connect
    Click Upload to App Store

Publish app

In detail, see iTunes Connect Developer Help page.

  1. Add an app in iTunes Connect
  2. Add app icon, app previews and screenshots.
  3. Submit an app to App Review
  4. Release an app