tesseract-ocr その1 動作確認

インストール

sudo apt-get install tesseract-ocr

色々と依存が有った気がします・・。

PATH を通す

.bashrc に以下を追加

export TESSDATA_PREFIX=/usr/share/tesseract-ocr/

バージョンを確認

$ tesseract -v
tesseract 3.03
 leptonica-1.70
  libgif 4.1.6(?) : libjpeg 8d : libpng 1.2.50 : libtiff 4.0.3 : zlib 1.2.8 : webp 0.4.0

画像を準備する

f:id:pongsuke:20170412102816p:plain

動かす

$ tesseract hw.png out -l eng
Tesseract Open Source OCR Engine v3.03 with Leptonica

$ cat out.txt 
Hellow world.

日本語を試す

言語データを追加する

Version 3.04 向けのデータを使ってみる(失敗)

Version 3.04 向けのデータはココ。

GitHub - tesseract-ocr/tessdata at 3.04.00

jpn.traineddata を /usr/share/tesseract-ocr/tessdata/ に入れます。

試す

f:id:pongsuke:20170412103533p:plain

$ tesseract hw.jp.png out -l jpn
Tesseract Open Source OCR Engine v3.03 with Leptonica
read_params_file: parameter not found: allow_blob_division

どうやらこれは、apt-get でインストールされた tesseract のバージョンが 3.03 なのに対し、言語データが 3.04 向けなのが原因の様です。

Version 3.02 向けのデータを使ってみる(動く)

次に、Version 3.02 向けを使う。

tesseract-ocr alternative download - Browse Files at SourceForge.net

$ tesseract hw.jp.png out -l jpn
Tesseract Open Source OCR Engine v3.03 with Leptonica

$ cat out.txt 
こんにちは。

chainer いろは

箇条書きで

学習済みモデルの保存と利用は?

せっかく学習させたので、学習済みのデータを使いたい。

どうやる?

chainer.serialisersを使う。

注意点は、ダンプしたのと同じ環境じゃないと動かない。

model の種類、GPUの使用/不使用 など、らしい。

Model 書き出し
 ...
from chainer import serializers

 ...

# npz形式で書き出し
serializers.save_npz("model.002.npz", model)
# hdf5形式で書き出し
# serializers.save_hdf5('modelhdf5', model)
Model 読み込み
 ...
from chainer import serializers
 ...


model   = Model()
# npz形式で読み込み
serializers.load_npz("model.002.npz", model)
# hdf5形式で読み込み
# serializers.load_hdf5("modelhdf5", model)

CSLAIER で学習させると、一式をzipでダウンロードできます。

GPUでの計算にするには?

gpu の初期設定と、 nummpy を chainer.cuda.cupy に変更する。

gpu_device = 0
chainer.cuda.get_device(gpu_device).use()
model.to_gpu(gpu_device)
xp = chainer.cuda.cupy

Chainer cifar を試す

README 曰く、train_cifar.py は、デフォルトで GPU 0 を使う。

$ python train_cifar.py

Mnist のように、データセットのダウンロードから始まります。

Estimated time to finish: 3:57:33

とでました。

初回の結果

$ python train_cifar.py 
GPU: 0
# Minibatch-size: 128
# epoch: 300

Using CIFAR10 dataset.
Downloading from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz...
epoch       main/loss   validation/main/loss  main/accuracy  validation/main/accuracy  elapsed_time
1           2.40104     1.94277               0.161325       0.217168                  51.6946       
2           1.81075     1.66216               0.285826       0.347706                  100.377       
3           1.50989     1.42922               0.420052       0.475771                  149.203       
4           1.27429     1.15027               0.543938       0.594343                  198.176       
5           1.0685      1.00839               0.627757       0.655756                  247.159       
6           0.953728    0.983496              0.673437       0.667623                  296.061       
7           0.871836    0.878536              0.710038       0.707476                  345.054       
8           0.825212    0.826754              0.729547       0.733782                  393.962       
9           0.789095    0.813937              0.745165       0.739517                  442.956       
10          0.753746    0.79951               0.757433       0.742089                  491.96        
11          0.729694    0.760835              0.767007       0.762658                  540.833       

 ...

285         0.00550012  0.524492              0.998881       0.899031                  13941.3       
286         0.00420565  0.549105              0.999379       0.89646                   13990.1       
287         0.00410778  0.539779              0.999341       0.897547                  14039         
288         0.00399757  0.536811              0.999419       0.897646                  14087.7       
289         0.00549819  0.532966              0.999021       0.898042                  14136.6       
290         0.00449508  0.525496              0.999341       0.899229                  14185.5       
291         0.00408126  0.528732              0.999459       0.898438                  14234.3       
292         0.00433158  0.529872              0.999361       0.897152                  14283.2       
293         0.00442761  0.534484              0.999321       0.897745                  14332.1       
294         0.00480573  0.52566               0.999179       0.898734                  14380.9       
295         0.0047175   0.535832              0.999321       0.899525                  14429.8       
296         0.00457282  0.548527              0.999299       0.89468                   14478.7       
297         0.00466009  0.538513              0.999241       0.896657                  14527.7       
298         0.00482552  0.535228              0.999201       0.898438                  14576.8       
299         0.00517972  0.528672              0.998978       0.899328                  14625.6       
300         0.00443006  0.533218              0.999261       0.899723                  14674.5

f:id:pongsuke:20170317111704p:plain

CSLAIER

GitHub - SonyCSL/CSLAIER

README から、必要なパッケージを入れる。

$ sudo apt-get install python-pip
$ sudo apt-get install python-dev
$ sudo apt-get install libhdf5-dev
$ sudo apt-get install python-opencv
$ sudo apt-get install sqlite3
$ sudo apt-get install python-matplotlib
$ sudo apt-get install nkf

どこまで必要か不明だが、メモ。

$ sudo apt-get install liblapack-dev
$ sudo pip install scipy
$ sudo apt-get install gfortran
$ git clone https://github.com/SonyCSL/CSLAIER
$ cd CSLAIER
$ ./setup.sh
$ sudo pip install -r requirements.txt

LAN内部の別マシンからアクセスしたいので、cfgを変える。

$ cat cslaier.cfg 
#HOST='localhost'
HOST='192.168.100.50'
PORT=8080
DATABASE_PATH='cslaier.db'
DEBUG=True
UPLOADED_RAW_FILE='uploaded_raw_files'
UPLOADED_FILE='uploaded_files'
PREPARED_DATA='prepared_data'
TRAINED_DATA='trained_data'
INSPECTION_TEMP='inspection_temp'
LOG_DIR='logs'

PORT を 80 にしたくなるのだけど、80にするは Permission denied がでて、思わず suro ./run.sh しちゃうんですが、そうすると、データも root 権限で作られちゃって、色々怖い。

データとモデルを登録してスタートする、、、。

動き始める?のが遅くて不安になるが、TOP で見ると Python が 100% CPU 使っていたりするから、何かしているんだろうなーと思える。

f:id:pongsuke:20170315181235p:plain

f:id:pongsuke:20170315181236p:plain

10エポック終了

f:id:pongsuke:20170316094637p:plain

inspect させる

f:id:pongsuke:20170322175752j:plain

nin で classify であれば、画像をアップロード出来ます。

Chainer imagenet を試す

Version 1.21.0 のドキュメント等

chainer/examples/imagenet at v1.21.0 · pfnet/chainer · GitHub

優しく解説してくださっているブログ

PFN発のディープラーニングフレームワークchainerで画像分類をするよ(chainerでニューラルネット1) - 人工言語処理入門

ChainerのNINで自分の画像セットを深層学習させて認識させる - shi3zの長文日記

Chainer imagenet では、

  1. 画像(の入ったディレクトリ)
  2. test.txt
  3. train.txt

の3つを準備する必要がある。

また、label.txt があると、ラベリングもできると言うことかな?

make_train_data をつかう

上記3つの準備の仕方は色々有ると思うが、make_train_data.py は、

/Dir_name/Cat/cat001.jpg cat002.jpg cat003.jpg ...
         /Dog/dog001.jpg dog002.jpg dog003.jpg ...
         ...

のようなディレクトリ構造にしておくと

  1. すべての画像が入った images ディレクトリ (Dir_name 以下の画像を1箇所にまとめてくれる)
  2. train.txt (適当に train, test に振り分けている)
  3. test.txt
  4. label.tx (サブディレクトリ名を転用している)

を作成してくれる。

画像サイズを 255x255 にする

Chainer imagenet では、 255x255 を使用することに成っている。
なので、整形が必要になる。

$ mv images images.org
$ mkdir images
$ python ../../crop.py images.org images

平均画像を作る

「平均画像」ってのが必要らしい。

$ python (chainer_dir)/examples/imagenet/compute_mean.py train.txt

学習

$ python ../../train_imagenet.py  -g 0 -E 10 train.txt test.txt 2>&1 | tee log

進捗をグラフで見る

$ cat log | grep iteration > log_pretty
$ python ../../plot.py log_pretty

推定する

$ python resize.py panda.jpg
$ python inspection.py resized/panda.jpg

パンダの画像でテスト行った所、どうもパンダは認識率が低い。
soccer_ball との判定が多い。

データセット

Caltech101

f:id:pongsuke:20170316123443j:plain

Caltech101

カテゴリ頃にディレクトリ分けされていて make_train_data を使用できる。
Total 102 Classes, 9145 Images
パンダ有る

Caltech256

f:id:pongsuke:20170316123454j:plain

Caltech256

カテゴリ頃にディレクトリ分けされていて make_train_data を使用できる。
Total 257 Classes, 30608 Images
パンダ無い

hiragana73

f:id:pongsuke:20170316123500j:plain

文字画像データセット(平仮名73文字版)を試験公開しました | NDLラボ

ひらがなデータ

png なので、make_train_data.py の ls /*.jpg の部分を書き換える。

imageFiles = cmd("ls "+workdir+"/*.png")

find -name .jpg -o -name .png でも、できそうだが、./ が付いたりしてその処理まで確認しないとダメだから、今度やろう・・・。

ラベルデータ

f:id:pongsuke:20170316182110p:plain

をキャプチャした物を判定させてみた。

f:id:pongsuke:20170316182639j:plain

$ ../../inspection.py a.jpg 
#1 | U3042 あ | 89.7%
#2 | U3090 ゐ |  4.1%
#3 | U308B る |  1.3%
#4 | U308F わ |  1.1%
#5 | U3082 も |  1.0%
#6 | U304A お |  0.5%
#7 | U3089 ら |  0.5%
#8 | U306A な |  0.3%
#9 | U3092 を |  0.2%
#10 | U3061 ち |  0.2%
#11 | U307F み |  0.2%
#12 | U304D き |  0.2%
#13 | U304B か |  0.1%
#14 | U306D ね |  0.1%
#15 | U3081 め |  0.0%
#16 | U3093 ん |  0.0%
#17 | U308A り |  0.0%
#18 | U306E の |  0.0%
#19 | U306F は |  0.0%
#20 | U3046 う |  0.0%

以下は私的挫折メモ

Flickr Style から取得したデータの整理(挫折)

crop.py を実行した際にエラーが出て気がついたのですが、this image is not avaiable now. などという記載のある小さい画像がまじりっています。
これはマズイので、除外します。

sed でやる方法(失敗)

sedコマンドで、train.txt と test.txt を修正するスクリプトを作ります。
完成形は sed -i "/\/2204798837_68dba55252.jpg/d" train.txt など。

$ find ./images_raw/ -size -15k -print | xargs mv -t ./image_ng/
$ find ./image_ng -type f | wc -l
9309

head で絞ってテスト。
問題なければファイルにする。

$ find ./image_ng/ -type f | head | sed -e 's/^.\/image_ng/sed -i \"\/\\/' | sed -e 's/$/\/d" train.txt/'
sed -i "/\/2204798837_68dba55252.jpg/d" train.txt
sed -i "/\/12666574533_3d6176a4a0.jpg/d" train.txt
sed -i "/\/12443728084_afb95621fc.jpg/d" train.txt
sed -i "/\/12431376425_a54d60bae1.jpg/d" train.txt
sed -i "/\/9415591322_9c69b2858a.jpg/d" train.txt
sed -i "/\/8679566699_bd290dfc1a.jpg/d" train.txt
sed -i "/\/8576572799_b84b8100f5.jpg/d" train.txt
sed -i "/\/12815768204_bed49ab0fd.jpg/d" train.txt
sed -i "/\/10555846683_fbc5d58f97.jpg/d" train.txt
sed -i "/\/11007037066_d975b7e769.jpg/d" train.txt

$ find ./image_ng/ -type f | head | sed -e 's/^.\/image_ng/sed -i \"\/\\/' | sed -e 's/$/\/d" train.txt/' > ng_list.sh

バックアップを取って、実行。

63831 が、9309 減って 54522 になる予定。

$ cp train.txt train.txt.org
$ cat train.txt.org | wc -l
63831

$ sh ng_list.sh
$ cat train.txt | wc -l
56634

あれ?

grep でやる(中断)

リストを読み込んで、一致を弾く。

$ find ./image_ng -type f | sed -e 's/^.\/image_ng//' > ng.pattern.txt
$ head ng.pattern.txt 
/2204798837_68dba55252.jpg
/12666574533_3d6176a4a0.jpg
/12443728084_afb95621fc.jpg
/12431376425_a54d60bae1.jpg
/9415591322_9c69b2858a.jpg
/8679566699_bd290dfc1a.jpg
/8576572799_b84b8100f5.jpg
/12815768204_bed49ab0fd.jpg
/10555846683_fbc5d58f97.jpg
/11007037066_d975b7e769.jpg


$ grep -f ng.pattern.txt -v train.txt.org > train.txt

(待てど暮らせど終わらないので kill した。。。)

メモリに展開しきれないのかな?

crop.py 改定

        if src is None:
                continue

として、問題の出たファイルはスルーさせてみた。

$ find images -type f | wc -l
79785

train.txt と test.txt に登場するのに、images ディレクトリに無い行がありますが、その不一致は吸収してくれるのか、とりあえず先に進めます。

epoch       iteration   main/loss   validation/main/loss  main/accuracy  validation/main/accuracy  lr        
0           1000        3.05539                           0.08925                                  0.01        
1           2000        2.81914                           0.134531                                 0.01        
1           3000        2.73034                           0.163969                                 0.01        
2           4000        2.69752                           0.168375                                 0.01        
2           5000        2.6506                            0.188594                                 0.01        
3           6000        2.62537                           0.189906                                 0.01        
3           7000        2.59254                           0.201906                                 0.01        
4           8000        2.5857                            0.2035                                   0.01        
4           9000        2.56082                           0.211719                                 0.01        
5           10000       2.54502                           0.219031                                 0.01        
5           11000       2.52094                           0.221563                                 0.01        
6           12000       2.51799                           0.223937                                 0.01        
6           13000       2.49525                           0.229719                                 0.01        
7           14000       2.49081                           0.233094                                 0.01        
7           15000       2.46874                           0.238406                                 0.01        
8           16000       2.46281                           0.241531                                 0.01        
8           17000       2.44324                           0.243969                                 0.01        
9           18000       2.44358                           0.248281                                 0.01        
9           19000       2.43107                           0.250562                                 0.01

chainer Faster R-CNN

GitHub - mitmul/chainer-faster-rcnn: Object Detection with Faster R-CNN in Chainer

インストール

事前に cython 入れます。

$ sudo easy_install cython
$ git clone https://github.com/mitmul/chainer-faster-rcnn
$ cd chainer-faster-rcnn/lib
$ python setup.py build_ext -i
$ cd ..
$ mkdir data
$ cd data
$ wget https://dl.dropboxusercontent.com/u/2498135/faster-rcnn/VGG16_faster_rcnn_final.model
$ cd ..
$ $ python forward.py --img_fn test.jpg --gpu 0

実行すると、result.jpg が出来上がります。

f:id:pongsuke:20170314155511j:plain

f:id:pongsuke:20170314155545j:plain

chainer deel を試す

Deel を試します。

GitHub - uei/deel: Deel; A High level deep learning description language

$ sudo apt-get install libopencv-dev
$ sudo apt-get install python-opencv
$ sudo apt-get install libhdf5-dev
$ git clone http://github.com/uei/deel
$ cd deel
$ python setup.py install

$ cd data
$ ./getCaltech101.sh
(エラー出た)
ValueError: operands could not be broadcast together with shapes (3,144,145) (3,817,656) (3,144,145)



$ cd ../misc
$ ./getPretrainedModels.sh

テスト

$ python examples/gender.py panda.jpg 
Loading gender_net.caffemodel
#1 | male | 63.8%
#2 | female | 36.2%