””Caffeで手軽に画像分類”の”リファレンスモデルでの分類”をやってみた”の続き。
”Caffeで手軽に画像分類”の”Caffeを特徴抽出器として使った分類”をやってみることにした。
imagenet_deploy.prototxt を imagenet_feature.prototxt としてコピーするのだが、caffe/examples/imagenet ディレクトリに imagenet_deploy.prototxt が見つからない。
検索したら、ここにあった。
gedit imagenet_deploy.prototxt で gedit を立ち上げてコピペをした。
これで、imagenet_deploy.prototxt ができたので、下のコマンドで imagenet_feature.prototxt にコピーした。
cp examples/imagenet/imagenet_deploy.prototxt examples/imagenet/imagenet_feature.prototxt
gedit で、imagenet_feature.prototxt を編集した。変更箇所をピンクのアンダーラインで示す。
gedit examples/imagenet/imagenet_feature.prototxt
gedit で、feature.py を作成した。
gedit feature.py
特徴抽出を実行するPythonスクリプトを実行するとエラーになった。
python feature.py 101_ObjectCategories/airplanes/image_0001.jpg
エラー内容は以下の通り。
エラー内容を検索すると、”[Ubuntu | Python wrapper]: cannot "import caffe" #720”がヒットした。
”[Ubuntu | Python wrapper]: cannot "import caffe" #720””に従って、.bashrc を編集した。
gedit ~/.bashrc
.bashrc ファイルに export PYTHONPATH=/home/ono/Deep_Learning/caffe/python を追加した。
source ~/.bashrc を実行して、設定内容を反映させた。
また、エラーが出た。今度は、set_phase_test の属性が無いというエラーだった。
エラーを検索すると、”AttributeError: 'Classifier' object has no attribute 'set_phase_test'”が見つかった。
それによると、 net.set_phase_test() を caffe.set_phase_test() にすれば良いらしい?
もう一度、python feature.py 101_ObjectCategories/airplanes/image_0001.jpg を実行すると、set_mode_cpu() でもエラーが出たので、caffe.set_mode_cpu() に変更した。
結局、12行目と13行目を下のように変更した。
これで、特徴抽出を実行するPythonスクリプトを実行するとうまく行った。
値が見えないので、もう一度、特徴抽出を実行するPythonスクリプトをパイプして less に入れた。
python feature.py 101_ObjectCategories/airplanes/image_0001.jpg | less
見きれないので、特徴抽出を実行するPythonスクリプトの出力を temp.txt に出力した。
python feature.py 101_ObjectCategories/airplanes/image_0001.jpg > temp.txt
数値はすべて1行に書かれている。これを、libsvmフォーマットにする必要がある。
”Caffeで手軽に画像分類”の”Caffeを特徴抽出器として使った分類”をやってみることにした。
imagenet_deploy.prototxt を imagenet_feature.prototxt としてコピーするのだが、caffe/examples/imagenet ディレクトリに imagenet_deploy.prototxt が見つからない。
検索したら、ここにあった。
gedit imagenet_deploy.prototxt で gedit を立ち上げてコピペをした。
これで、imagenet_deploy.prototxt ができたので、下のコマンドで imagenet_feature.prototxt にコピーした。
cp examples/imagenet/imagenet_deploy.prototxt examples/imagenet/imagenet_feature.prototxt
gedit で、imagenet_feature.prototxt を編集した。変更箇所をピンクのアンダーラインで示す。
gedit examples/imagenet/imagenet_feature.prototxt
gedit で、feature.py を作成した。
gedit feature.py
特徴抽出を実行するPythonスクリプトを実行するとエラーになった。
python feature.py 101_ObjectCategories/airplanes/image_0001.jpg
エラー内容は以下の通り。
Traceback (most recent call last):
File "feature.py", line 3, in <module>
import sys, os, os.path, numpy, caffe
ImportError: No module named caffe
エラー内容を検索すると、”[Ubuntu | Python wrapper]: cannot "import caffe" #720”がヒットした。
”[Ubuntu | Python wrapper]: cannot "import caffe" #720””に従って、.bashrc を編集した。
gedit ~/.bashrc
.bashrc ファイルに export PYTHONPATH=/home/ono/Deep_Learning/caffe/python を追加した。
source ~/.bashrc を実行して、設定内容を反映させた。
また、エラーが出た。今度は、set_phase_test の属性が無いというエラーだった。
AttributeError: 'Classifier' object has no attribute 'set_phase_test'
エラーを検索すると、”AttributeError: 'Classifier' object has no attribute 'set_phase_test'”が見つかった。
それによると、 net.set_phase_test() を caffe.set_phase_test() にすれば良いらしい?
もう一度、python feature.py 101_ObjectCategories/airplanes/image_0001.jpg を実行すると、set_mode_cpu() でもエラーが出たので、caffe.set_mode_cpu() に変更した。
結局、12行目と13行目を下のように変更した。
caffe.set_phase_test()
caffe.set_mode_cpu()
これで、特徴抽出を実行するPythonスクリプトを実行するとうまく行った。
値が見えないので、もう一度、特徴抽出を実行するPythonスクリプトをパイプして less に入れた。
python feature.py 101_ObjectCategories/airplanes/image_0001.jpg | less
見きれないので、特徴抽出を実行するPythonスクリプトの出力を temp.txt に出力した。
python feature.py 101_ObjectCategories/airplanes/image_0001.jpg > temp.txt
数値はすべて1行に書かれている。これを、libsvmフォーマットにする必要がある。