2019年5月29日水曜日

MATLAB EXPO 2019 に参加

 MATLAB EXPO 2019 (2019-5-28、グランドニッコー東京台場)に参加しました。断片的になりますが、以下に報告しますので、ご参考になれば幸いです。公式webページなどは以下にあります。
https://www.matlabexpo.com/jp/2019.html; Twitter #matlabexpo
(講演スライドは大部分すでに公開されています。)


 このEXPOは、毎年10月に開催されていたが、今回は間を縮めて、5月に開催となった。それだけ人工知能応用への関心の高まりが持続しているのかも知れない。

 特別講演、ポスターセッションに加えて、技術セッションが7並列で実施された。どの会場(恐らく500席以上の規模)もほとんど満席だった。いろいろな産業分野へのAIの多彩な応用が広がりつつあることを実感する場であった。

[機械学習とディープラーニング]セッションより
ディープラーニングを活用した化粧品開発における微生物試験の迅速化(資生堂+MathWorks Japan)
  • 化粧品会社だが、基本理念は「安全性」のうえに築く人間科学+物質科学+生物科学にあるとのこと。
  • 化粧品開発には、微生物による汚染リスクの評価(菌種の同定、菌数カウントなど)のため、菌の培養を伴う試験に28日ほどかかる。これをDeep Learningにより大幅短縮できる見通しを得た。
  • 特に、菌の育成予測(38時間後)も、一定程度可能になったことが大きい。
  • 機械学習の専門家はいなかったが、社内でAIに興味を持つ有志が集まり、MathWorksの支援を得て、いろいろな事例研究をもとに、2018年からこのプロジェクトを始めた。
  • AIの専門家でなくても、その分野に精通する人々の熱意で、AIは有効活用できるという典型例のように思われた。
  • MathWorkの説明によれば、このために、セマンティックセグメンテーションネットワーク(SegNet)を構築しているのが、一つの特徴とのことである。

[機械学習とディープラーニング]セッションより
強化学習:最適制御のためのディープラーニングの応用(MathWorks Japan)
 この講演は図3から分かるように、聴衆が非常に多かった。強化学習(Reinforcement Learning)に関する世界の論文数は、機械学習全体の論文数の増加に伴い、図4に示されているように勢いを増している。


  • MatLabの最新版R2019aには、新たに強化学習のためのツールボックスであるReinforcement Learning Toolboxが提供されている。その解説があり、今後試行する上で大いに参考になりそうである。
  • サポートされている強化学習のアルゴリズムとしては、Q-LearningやSARSAなど有名なものを含む6種とのこと。
  • このツールボックスを使ったリファレンスアプリケーション(例題)が20件ほど公開されている。そのなかには、2足歩行、倒立振り子、グリッド移動など親しみやすいものが含まれている。

[ポスターセッション]より2件(図5参照)


機械学習や3次元点群処理技術が切り拓く植物計測の技術革新(東大大学院 農学生命科学研究科)->図5(a)
  • タイトルの前半は、植物のモニタリングにDeep Learningを活用(糖酸度比の推定などに)ということだったが、後半の「3次元点群処理 ... 」は違っていた。
  • 図からも分かるように、ライダー(3次元スキャナー)画像から、植物の葉の数を算出するという。さらに、葉面積や葉傾斜角まで推定できるという。これには、機械学習は全くやっていない!独自の方式に基づくコーディングにより、一方向のパスだけで、葉の1枚、1枚を色分けして表示させているのに驚いた。
  • この技術を使って、農場を移動しながらリアルタイムに、実っている「桃」の個数の自動計測と熟成の程度の自動判定を行う様子を、ビデオ表示していた。素晴らしい!
  • 機械学習万能じゃないなあ、という気にもなってくる。説明者によれば、「後になって、MatLabを使えばもっと楽に作れることが分かった」とのこと。確かに、MatLabは、核となる機械学習だけでなく、その上流下流の処理も一貫して支援する環境でもある。

CNN & SVMの設計・訓練・評価・実装ツール(山口東京理科大)->図5(b)
  • CNN(畳み込みニューラルネットワーク)とSVM(サポートベクタマシン)を独自のGUIで使いやすくするツールである。
  • Deep Learningばかりが目立つ昨今だが、SVMの実力と有用性を改めて認識させるツールになっているように思われる。
  • CNNのうち、Softmaxの前の段階での出力を特徴ベクトルとして、SVMへ入力することも可能にしている。
------------------------------------------------------------------------------
 今回の参加者記念品:

2019年5月26日日曜日

Season of rice planting

Rice planting has begun this year. Soon the rainy season, so frogs will croak a lot. However, in recent years, the nearby rice fields have become absolutely less. With the aging of farmers, it is feared that there are very few young people working in farming.

Atsugi, Kanagawa Japan (May 26, 2019)

Anyway, about 5 months later, rice ears will be fully grown. At that time, autumn has already deepened. By that time, I want to keep a productive life in mind.

Atsugi, Kanagawa Japan (October, 2018)

2019年5月21日火曜日

Enjoy Reinforcement Learning in Tic-tac-Toe

This app has won the "2019 June's MIT APP INVENTOR OF THE MONTH" award. 
http://appinventor.mit.edu/explore/app-month-winners-2019

The purpose of this app is to familiarize you with Reinforcement Learning (a type of artificial intelligence). This was created using MIT App Inventor. In particular, it takes advantage of the recently released Generic Event blocks.

As an example, we use Tic-Tac-Toe, a well-known and easy game. The main feature of this app is that you can adjust the strength of your opponent computer player according to the degree of reinforcement learning and enjoy the match.

The computer player learns the situation when it loses in the match, so it will become gradually stronger as the match is played many times. This app is published below and its appearance is as shown in Fig1.

MIT App Inventor Gallery (source file .aia)
ai2.appinventor.mit.edu/?galleryId=5141416460812288


3 cases of computer vs. human match

There are three cases of computer-human competition, but the last case-3 is the most attractive.

(case-1) Play against a weak computer player
-> The computer player has no knowledge, so if you don't make mistakes, you will easily win.
(case-2) Play against a very strong computer player
-> The computer player has enough learned information, so it is difficult for a human to win.
(case-3) Adjust the strength of the computer player
-> The strength of computer player can be adjusted by the degree of learning.

The battle procedure in case-1 and case-2 is described in Fig.2 and Fig.3 respectively.



Adjust the strength of the computer player (case-3)

The procedure to enjoy the match in this case-3 is in Fig.4. In order to do this, you need to make the computer learn Reinforcement Learning. For example, it takes about 30 minutes for learning to make the computer about 85% likely to be strong (that is, 85% chance of not losing). Is it too long?

Don't worry, you can interrupt learning at any time by LONG clicking the button "Reinf. Learning". Even if the power is turned off, you can continue the learning by turning on the power and clicking the button "Reinf. Learning" again. However, please keep in mind that if you click on "from scratch", computer's learning results will disappear.


The computer gets progressively stronger as learning progresses. The situation is shown in Fig5. You can use this figure as a guide to predict the required learning time (or number of games). Enjoy the game!



This application includes a text file like Fig. 6 as an asset file. This is a learning result obtained by playing about 37,000 games. Please refer to the above Fig.5. FIg.6 exemplifies what the learning content means.


In this application, we used TinyDB to store and retrieve reinforcement learning results and also used newly-provided general-purpose event blocks to operate buttons. Thanks to them, as shown in Fig. 7, the app was created very efficiently.



This app was created inspired by the following documents:
  • https://ecraft2learn.github.io/ai/AI-Teacher-Guide/chapter-6.html
  • Donald Michie, http://people.csail.mit.edu/brooks/idocs/matchbox.pdf
  • https://we-make-money-not-art.com/menace-2-an-artificial-intelligence-made-of-wooden-drawers-and-coloured-beads/
  • Oliver Child, http://chalkdustmagazine.com/features/menace-machine-educable-noughts-crosses-engine/

Comments on version upgrade

As soon as you use this app, you will want to add the following features:
  1. Specifying which one of the computer and the human takes the first move. This is not difficult, but this time I omitted it to simplify the structure of the program.
  2. Saving the results of Reinforcement Learning and making it available to other people. This is also relatively easy to achieve. Intermediate results of Reinforcement Learning are stored in TinyDB. Therefore, when necessary, the contents of TinyDB can be stored as a text file and retrieved to the outside. To use it for further Reinforcement Learning, convert the text file to TinyDB format. However, generally speaking, this conversion requires a little care. This is because the format of the value in TinyDB elements (tag, value) is various, such as a single element, list, or list of lists.

2019年5月17日金曜日

強化学習で、強力な「3目並べ」プレイヤーを作る

 この記事は、「強化学習」によって、「3目並べ」の強力なコンピュータプレイヤー(スマホアプリ)の作成をめざした報告です。簡単な問題でも良いので、自ら考え、手法を設計し、実装することは普遍の価値があると思います。

「3目並べ」で強化学習する背景
 AI囲碁で有名なAlphaGOやAlphaGo Zeroでは、「強化学習」を取り入れているとのことです。強化学習とは、参考文献[1]を引用して、「連続した一連の行動の結果、価値が最大化する(=報酬が最も多く得られる)行動を自ら学習し、選択していくだけでなく、行動についての評価も自ら更新していくこと」としておきます。

 ここでは、囲碁に比べると非常に単純な「3目並べ」(米国ではTic-Tac-Toeゲーム)で、強いコンピュータプレイヤーを、強化学習で作ります。今さら3目並べ?と思われるかも知れませんが、著名なコンピュータ科学者Donald Michieは、1960年代(約60年前)に、多数のマッチ箱を用いた手作業でこの問題を強化学習させる仕組み[2]を発表しています。また、Donald Knuthも、1958年に、当時の僅か10KBメモリのコンピュータで、Tic-Tac-ToeのLearning Programを書いた[3]とのことです。

「3目並べ」を強化学習するAndroidアプリ
 これら先人の業績に触発されて、今回、独自に強化学習を設計しました。そして、それをスマホのアプリとして作成(with MIT App Inventor)しました。どんなものかを、まず図1に示します。auto_playモードと、computer-humanモードがあります。いずれのモードでも、「負けた状況に応じてペナルティを科す」という強化学習を行います。それによって、computer playerが段々強くなっていくはずです。


 以下の図2には、両方のモードでの実行画面の例を示します。ここでは、computer playerが先手で"O"を打つことから始めています。強化学習の結果は、後でさらに改善するために、外部記憶に格納したり取り出したりできます。save DBやrestore DBなどのボタンがそれを可能にします。


強化学習の方法
 さて、どのように強化学習するのかを簡単に説明します。この「3目並べ」は、先手、後手どちらで始めても、両者がミスしない限り、必ず引き分け(tie)になることが分かっています。ですから、人間を相手にした場合、computer playerは勝つことを目指すのではなく、負けない(引き分けか、運がよければ勝つ)ことを目指します。

 今回のauto_playモードの強化学習では、まず、computerが先手でどこかに"O"を打ちます。それ以降は、相手("X")とcomputer player("O")は交互に、空いている場所にランダムに打って行きます。そして、computer playerが負けた場合には、その時の局面を記憶して、次回にまた同じ局面になった時に、前回打った場所を避けて打つ確率を高く(打てる選択肢を制限)します。それが強化学習となります。図3は、そのような学習の状況を説明しています。


強化学習の性能
 このような強化学習はどの程度の効果を発揮するのでしょうか。それを確かめた一例を図4に示します。上記のようなauto_playモードで、6万回ゲームを行った結果から、1,000ゲーム毎に、computer playerが「負けなかった率」を示しています。最後の方の1,000ゲームにおいては、負けなかった率は、99%に達しています。これをベースに人間と対戦しても、相当に強いと思いますが、もしも負けたら、また強化学習してさらに強くなります!


MIT App Inventorで作成 
 今回のスマホアプリは、MIT App Inventorで作成しました。強化学習結果の保存と取り出しにはTinyDBを、ボタンの操作には、最近新規に提供された汎用のイベントブロックを利用して、非常に効率的に作成できたと思います。図5に簡単にそれを示します。


参考文献
[1] 強化学習とは?ALPHAGOでも使われている強化学習を丁寧に解説 https://udemy.benesse.co.jp/ai/reinforcement-learning.html
[2] Menace: the Machine Educable Noughts And Crosses Engine, http://chalkdustmagazine.com/features/menace-machine-educable-noughts-crosses-engine/
[3] A guide to building AI apps and artefacts, https://ecraft2learn.github.io/ai/AI-Teacher-Guide/chapter-6.html
[4] http://www.half-real.net/tictactoe/

【補足】

  • 図4において、最初の1,000ゲームの学習の結果、computer playerが負けない率がすでに75%近くになっていますが、これは一応納得がいきます。文献[4]によれば、ゲームに決着がつくパタンは全部で255,168あるそうです。このうち、先手(computer player)が負けなかったパタン数は177,264あるとのことです。その比率は約7割になりますので。
  • 図4のデータを得るために、auto_playモードを6万ゲーム実行しました。その間、Androidスマホを6時間程度走らせました。途中で中断再開もしました。より高速な学習も可能でしょうが、スマホが過熱発熱しないようにしました!(その後の検討の結果、途中結果を表示しながらでも、もっとスピードアップしても大丈夫なことが分かりました。その場合は、約90分で、図4に相当するデータが得られます。)

2019年5月11日土曜日

MIT App Inventorの新しいイベント処理ブロック

 MIT App Inventorでは、現在進行中のiOS対応の他にも、継続的なバージョンアップが行われています。今回、待望の汎用イベント処理(リスナー)ブロックが発表されるという予告があったので、待っていました。ボストン時間の2019-5-10(金)に実施されるとのことで当日何度かアクセスしてみましたが、結局、現地時間で金曜日の23:00ころに実施されました。

 MIT App Inventorは、世界195ヶ国から登録ユーザ数=820万人、月間Active Users=101万人ほどに浸透しているシステムなので、バージョンアップリスクを勘案して、週末の深夜となった模様です。いくつかの新規項目があるのですが、ここでは、イベントリスナー関係に絞ります。

ボタンを押した場合のイベントリスナーの汎用化
 図1を使って説明します。9個のボタンがあります。そのうち、背景色が黄色のボタンを押した時だけ、そのボタンの表面に"O"を表示する、という簡単な問題です。背景色は、ボタン毎に固定ではなく、時間変化するものとしています。結論から言いますと、今回の新規ブロックを使うと、そのプログラムは、図1のようになります。これがプログラムの全てですので、その簡潔さに嬉しくなります!


 一方、従来のブロックを使った場合は、以下の図2のように書くしかないでしょう。すなわち、9個のボタンそれぞれに、ボタンが押された場合のリスナーの処理(背景色の検査とその結果に基づく"O"の表示)を書くことになります。


 Javaなどの汎用言語とちがって、MIT App Inventorでは、例えば、ボタンを押した場合のイベントのリスナーには自分自身(ボタン)しかなれませんでした。他のオブジェクトは、ボタンのリスナーにはなれませんでした。そのため、図2のようなことになってしまいます。

 今回の改訂では、例えばボタンに関していえば、どのボタンを押した場合でも、新設ブロック(any Button.click)が、そのイベントを受け付けるようになりました。もちろん、その際、イベントの発生源となるボタンを特定できますので、図1のように纏めて書けるわけです。

 このような新規ブロックは、ボタンに限らず、Action イベントやState Changedイベントを発生するオブジェクトの種類毎に用意されていますので、複雑なプログラムでのイベントの扱いが簡潔になり、開発の効率化を一層促進させると思います。

従来のイベントリスナーの自動汎用化
 今回のバージョンアップでは、従来のプログラムでの個別のイベントリスナーを、自動的に汎用化する機能までついています!すなわち、上記の例で言えば、ボタンB1に対する固有のイベントリスナーを、クリック一つで、自動的に汎用化できます。実際、図3のようにしてできた汎用リスナーが、図1のブロックとして示したものなのです。


2019年5月1日水曜日

Scratch風の開発環境でTensorFlowを利用できる

 自分のPCに環境設定せずにTensorFlowを使いたいのならば、Google Colabですね。Gmailアカウントがあれば、Googleサーバ側のGPUだって指定して学習させられます。しかし、もっと別のものもあります。それが、Snap!([1]、[2])です。

 Snap!は、Scratch風のブロック型プログラミング環境ですが、ちょっと高級です。ラムダ式(高階関数)も使いますし、TensorFlowを使うためのブロックもあるからです。Google Colabと違って、アカウント開設も、環境設定も不要であり、ローカルマシンで学習を実行させます。そのため、GPU無しのマシンだとちょっと苦しい場合があるかも知れません。小規模な試行ならば問題ないですが。

 早速ですが、Snap!の資料[2]には、以下のような例題が含まれています。

(1)簡単なデモ:乱数(実数値)に対する平方根の学習と予測
An small example: Training a model to estimate square roots
(2)Tic Tac Toe(3目並べ)ゲーム戦略:D. Knuthは1958年に機械学習を作成
Learning to win at Tic Tac Toe
(3)実用アプリ:気候データからインフルエンザの流行の度合いを予測
Applying machine learning to real-world data

 このうち、ここでは、(1)の平方根の例題を取り上げます。元の例題のソースリスト(ブロック図)を少し変更したものを使って、どんなことができるかを、説明したいと思います。最初に、図1をご覧下さい。Snap!でのDeep Learningの画面です。Scratchと同様に、(a)に置かれた種々のブロックを、編集領域(b)で組み合わせてプログラムを作ります。(c)には、メッセージや学習結果、予測結果などを表示できます。なお、(d)は、Snap!とは直接関係ありませんが、Mac Book Pro (4コア、8ハイパースレッドだがGPUは無し)での学習実行の負荷状況です。


 平方根の学習のために、入力データ(0〜50までの実数値乱数を800個)と、その入力データに対する正解ラベル(関数srtの計算値)を用意します。図2において、関数sqrの引数が空白みたいになっていますが、これが、ラムダ式(高階関数)を使っているところです。これだけも、Snap!がScratchよりも高レベル(やや高い難易度)であることを臭わせます。


 次に、この入力を使った学習を行うための、ニューラルネットワークを構成します。そのためのブロックが用意されているのです。(現時点では、dense networkに限られているようですが)ニューラルネットワークを作るための、ハイパーパラメータ(層の数と各層のノード数、最適化の手法、誤差関数の種類など)を決めます。ここでは、(a)隠れ層が3層の場合と、(b)隠れ層が1層の場合の2つを、図3のように設定しました。赤色背景のlistの右側に並んでいる数値は、各層のノード数です。これらのハイパーパラメータの値は、通常、過去の試行経験などに基づいて決めらます。


 この後は、図4のように、学習を制御するブロックを設定します。ここでも、学習をどの程度長く行うかの反復回数や、学習係数、それと、学習時のテストのために使う入力データの割合(入力データ全体に対する比率)などのパラメータの値を決めます。そして、このブロックをマウスでクリックすると学習が開始されます。一般には、学習は長時間を要しますが、ここでは、短時間で済むようにパラメータを設定してあります。


 学習が終わった後に、入力データとは別に、評価用実数値を与えて、その出力結果を観察します。図5のとおり、4つの評価用データ(0.09, 9, 25, 49)を使います。これらに対する正解の平方根は(0.03, 3.0, 5.0, 7.0)です。図5では、図3で用意した2種類のニューラルネットワークが出力した結果を示しています。隠れ層が多い(a)が、少ない(b)よりもかなり良好な(ただし、0の近傍では良くない)結果を与えていることが分かります。このような層の深さに関しての性能傾向は一般的なことと思います。


 さて、ここまでやってみると、TensorFlowを知っている人は、Snap!ではTensorFlowをベースにしているのはないかと感じると思います。そのとおりのようです。実際、Snap!には、Open support panelと言うブロックを持っていて、これを使うとtensorflow.js(JavaScript版のTensorFlow)による学習状況などを観察できます。図6にその一例を示します。


 以上は、簡単なデモプログラムにすぎませんが、定番となっている「PythonでTensorFlow」とはちょっと違う観点を与えてくれる、Snap!の魅力を垣間見ることができたように思います。冒頭で示したとおり、Snap!の解説[2]には、より実用的なアプリの例題(インフルエンザ流行の予測)等もあり、生データを入力データへ加工することも含めたブロック図も示されています。また、当然ながら、画像や音も扱えます。今後これらについても調べて行きます。

 児童、生徒にもAI(人工知能)教育を。これは、小学校での英語、プログラミングの必修化に沿った流れのようです。ScratchML4K(Machine Learning for Kids)はそのための具体的な環境です。そこでは、学習させる画像や音、テキストを自分自身で収集して学習させ(Neural Networkそのものには触れませんが)、未知のものに対する分類や予測を体験できます。

 今回のSnap!は、もう一歩深入りして、Neural Networkを具体的に構成し(ハイパーパラメータを自分で指定して)、その動作も観測(誤差の低減確認)するところまで進めています。したがって、これは、大学でのコンピュータサイエンス教育にも適していると考えられて、例えば、米国UCバークレイ校ではSnap!をベースにしたカリキュラム(苦痛ではなく、楽しみながら美しいと感じてもらえることを目指しているような)The beauty and joy of computing [3] も公開しています。

参考文献
[1] UCバークレイのSnap!サイト:https://snap.berkeley.edu
[2] Ken Kahn, A guide to building AI apps and artefacts:
https://ecraft2learn.github.io/ai/AI-Teacher-Guide/chapter-6.html
[3] The beauty and joy of computing, https://bjc.edc.org/bjc-r/course/bjc4nyc.html

2019年4月22日月曜日

A simple demo app using ThunkableX Cloud Variables

Of the few existing App Inventors, ThunkableX has its own distinctive features. Except for being able to create common source programs (blocks) for Android and iOS, I focused on the newly introduced cloud variables. These extend the scope of traditional variables to cloud space based on the Firebase (a kind of real-time database). Normally, the database is created with a user-specific id, but if you know its URL, you can access that cloud variable. When an authorized user updates the value of a variable, the event is immediately received by other apps. Such a facility enables efficient development of database applications, etc.

An example of initializing such cloud variables is shown in Figure 1. You can see that Firebase tags can be mapped to cloud variables. Hierarchical tags can also be cloud variables, such as “fireLoc/lat”.


After initializing these cloud variables, they can be referenced in several ways. Figure 2 shows five access blocks for the cloud variable Betty. Particularly, I would like to note a block called “when Cloud Betty is initialized or changed”. This block allows you to know in real-time the updates to cloud variables, and based on that you can easily set up your application logic.


Gram app [2] is a famous example of using cloud variables. This is a very fine app, but it's somewhat complicated for general programmers. So I created a simpler demo app. Figure 3 shows that. Jack wants to know the location of his daughter Betty in real-time. But, Betty allows sending her location only when she presses a button on her smartphone.


The complete block diagram of ThunkableX that achieves this is shown in Figure 4. Figure 4(a) is for the sender, Betty; and Figure 4(b) is for the receiver, Jack. This diagram shows that using cloud variables make programming much easier. You can access the contents of the database without using Firebase blocks such as registering listeners, storing values in the database, etc. In Fig. 4(a), the location information is fetched by GetCurrentLocation and stored in the cloud variable Betty. Also, in Figure 4(b), updates to the cloud variable Betty is automatically detected, so the result is immediately reflected in the map.


References
[1] thunkable cloud variables, https://docs.thunkable.com/variables
[2] the Gram app, https://x.thunkable.com/projectPage/5bbff7e035aa10fd7702f479

2019年4月21日日曜日

Firebase GPS demo made with ThunkableX (common to Android & iOS)

ThunkableX [1] is a kind of software system of App Inventor, which makes it possible to create a common source program for Android and iOS. Some of its functions have not yet been realized, but it is a wonderful system that continues to progress. Here, we created a simple GPS demo program using ThunkbleX.

Suppose Jack and Betty want to show each other the current location on the smartphone in real time. The MIT App Inventor program that realizes it is shown in the literature [2]. It uses the real-time database Firebase, but it is assumed that both have Android devices. By contrast, this article assumes Jack has an Android device but Betty has an iPhone.

I created a ThunkableX app like Fig. 1 below. The source list (block configuration) of the app used here is common to Jack and Betty. Using the Firebase, current positions of each other are displayed in real time. When you press the send button, your location information will be stored in Firebase. Fig. 2 shows the contents of Firebase. If there is a change in the Firebase, the opponent's smartphone immediately updates the map display by seeing the change event in real time.



The whole block of ThunkableX that realizes this is shown in Fig. 3. For reference [2], registration of the listener for change event as shown in Fig. 3(1) was unnecessary. However, the reason ThunkableX needs it is probably because of the commonality with iOS. When you use Firebase, you do not need to make any settings if it is a temporary test program. However, if you use it in a full-fledged app, you will need to set up your own Firebse Database URL and API Key. Otherwise, security issues may occur.


References
[1] https://thunkable.com/#/
[2] USING FIREBASE IN A REAL APP – “HERE I AM!” GPS DEMO APP (https://appinventor.pevest.com/2016/09/13/using-firebase-in-a-real-app-here-i-am-gps-demo-app/)

2019年4月15日月曜日

Using MIT App Inventor AWS AI Services Extension

Modern AI image recognition allows us to recognize not only objects, but also the accompanying text (strings). Here we will try it using Amazon AWS Rekognition. Using the APIs provided there, we can create a variety of applications on our PC, but for more convenient use, the applications on our smartphone will be preferable.

Recently, I focused on the AWS AI Services Extension developed by Ceyhun Özgün. This is an Extension that allows MIT App Inventor applications to easily take advantage of such recognition features. References [1] and [2] provide descriptions and demo programs. I've created a simple smartphone application like Fig.1 below, using this useful information as a reference.


(Please click to enlarge)

Let me illustrate the recognition results in this diagram. Fig.1 (a) is an image of the US $2 bill. Immediately, “Money, Person, Dollar” was recognized. In addition, the text in the image was also recognized. In particular, the number “J045... 5A”, which is the id of the banknote, is also recognized precisely. Fig.1 (b), on the other hand, is an image of a notebook and books. It is recognized as “Book”, and the text of the titles of the books (although handwriting is also included) is almost accurate. I think it's great. These smartphone apps are fun to use wherever you take them!

The design part of this app is shown in Fig.2. Here, I used AppyBuilder. You can choose to take a picture now or use an image that you already have in your library. After that, recognition is performed automatically and the results of recognition of objects and text are displayed.


Next Fig.3 is the complete blocks of this app. Thanks to the Extension mentioned above, you will find that it has become a very concise program. In order to use this Extension, you need to set up an AWS account. In particular, be aware that you will give your AWS Services credentials on the first run. Details of this are also described in the literature [1]. Thanks to Ceyhun Özgün!


References
[1] https://hackernoon.com/your-kid-can-code-a-fruit-detector-with-this-mit-app-inventor-aws-ai-services-extension-231665c8bcdc
[2] https://github.com/ceyhunozgun/awsAIServicesAppInventorExtension/wiki

2019年4月12日金曜日

AIによる画像認識で紙幣の記番号を抽出してみる

 先のブログ記事でコインの識別について書いた後、麻生財務相による紙幣刷新の発表がありました。その狙いのひとつは、紙幣の偽造防止とのことです。そこで、紙幣の記番号の識別も意味があるかも知れない。なぜなら、過去に大量に出回った偽造紙幣の記番号は同一だった(コピーによるため)との情報があるからです。

 では、現在のAIによる画像認識技術で、記番号抽出は可能なのでしょうか。それを少しだけですが、確かめてみました。調査対象も少なく、説得力に欠けますが、実験結果として以下に記しておきます。ここでは、AIによる画像認識技術で最強の部類に入ると思われる、IBM Watson Visual RecognitionAmazon AWS Rekognition(イメージ内テキスト)を使いました。特に自分でプログラミングしなくても、この程度のことであれば、簡単に使えるのは有り難いです。生徒の理科の実験やレポート作成にも使えるくらいです。

 まず、このWatson Visual Recognitionは、文字認識のための専用OCRソフトではありません。画像に何が写っているか分からないなかで、人の顔や動物、植物、日用品等々を認識するものであり、その一部として、文字列(テキスト)らしきものがあればそれも識別する、というものです。これに対して、Amazon AWS Rekognitionには、「イメージ内テキスト」検出というという専用オプションがあります。両者のコンセプトには違いがあるようです。ですから、一概にどちらが優れているなどという判断はしない方が良いでしょう。これを踏まえたうえで、以下の実験結果を示します。

 最初に、Watson Visual Recognitionを使います。対象画像の中に、辞書に載っている単語など、意味のある綴りがあれば、それは認識しやすいようです。実際、図1はそのことを示しています。この画像は過去のJava Oneというコンファレンスの際にもらった布製のバックパックです。それに、刺繍で幾つかの文字が縫われています。歪んだりしているのに、「java」や「diamond」「fuji」などが、見事に抽出されています。素晴らしい!



 次に、本題の紙幣の記番号の抽出を試行しますが、事前検討の結果から、こんどはAmazon AWS Rekognitionを使うことにしました。図2は米5ドル紙幣、図3は壱万円札、図4は10ユーロ紙幣です。結論から言いますと、いずれの記番号も完璧に認識(抽出)しているではありませんか!紙幣の場合は、記番号の背景に、微細で複雑な模様があるので、自動車のナンバープレートの識別などよりも難しいのではないかと思いましたが、難なく認識してしまいました。







 これらの結果を眺めていると、仮に偽造紙幣が出回っていて、その記番号がデータベースに登録されている場合、パソコンやスマホを使った安価な偽造紙幣検出アプリができてしまいそうです。例えば、大学の研究室で、学生や先生から、何とか1万円札を100枚(100万円)借りて集めます。それを、ScanSnapなどでスキャンして、スマホかPCのアプリで検出実験もできそうです。ただし、紙幣をスキャン(コピー)することは、通貨偽造に関する法律に抵触しそうなので、ご注意下さい。(下記、財務省見解参照)

 なお、紙幣の記番読取りは、すでに実用化されているという情報をいただきましたので、主な参考資料を追記しておきます。Deep Learningによる技術かどうかなどは不明ですが。
  • 現金管理の効率化と厳格化を実現するSmart Cash Stream ソリューション、日立評論2015-03, pp.38-42
  • 紙幣記番号読取装置および紙幣記番号読取方法、グローリー株式会社、特許公開番号2004-213560
----------------------------
【注】紙幣複写等に関する財務省見解
本記事は、下記の財務省見解を理解したうえで執筆しました。
----------------------------