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