機械学習ライブラリCaffe で色々するための準備

超初心者のため、サンプルコードで試す。

[amazonjs asin="B00XZTYMG6″ locale="JP" title="Pythonプロフェッショナルプログラミング 第2版"]

前提

こちら

/home/ubuntu配下にanacondaとcaffeがインストールしてある。

 

リファレンスモデルをダウンロード

caffe/examples/imagenet配下にリファレンスモデルをダウンロードしてくるスクリプトをダウンロードし、実行してリファレンスモデルを取得する。

[shell]
cd ~/caffe/examples/imagenet
wget https://raw.githubusercontent.com/sguada/caffe-public/master/models/get_caffe_reference_imagenet_model.sh
chmod +x get_caffe_reference_imagenet_model.sh
./get_caffe_reference_imagenet_model.sh
[/shell]

 

モデル定義ファイルのダウンロード

caffe/examples/imagenet配下にモデル定義ファイルをダウンロードする。

[shell]
wget https://raw.githubusercontent.com/aybassiouny/wincaffe-cmake/master/examples/imagenet/imagenet_deploy.prototxt
cp imagenet_deploy.prototxt imagenet_feature.prototxt
[/shell]

 

関連ファイルをダウンロード

caffe/data/ilsvrc12配下のget_ilsvrc_aux.shスクリプトを実行し、関連ファイルをダウンロードする。

[shell]
cd ~/caffe/data/ilsvrc12/
./get_ilsvrc_aux.sh
[/shell]

~/caffe/models/bvlc_reference_caffenet配下にもbvlc_reference_caffenet.caffemodelをダウンロードする。

[shell]
cd ~/caffe/models/bvlc_reference_caffenet
wget https://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel
[/shell]

~/caffe 配下に、101_ObjectCategories.tar.gzをダウンロードし、解凍しておく。

[shell]
cd ~/caffe
wget https://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz
tar xz 101_ObjectCategories.tar.gz
[/shell]

io.pyファイルを編集

次回以降で紹介するスクリプトを実行した際に以下の様なエラーが表示された。

[shell]
Traceback (most recent call last):
File "caffe_test.py", line 13, in <module>
image_dims=(256, 256))
File "/home/ubuntu/caffe/python/caffe/classifier.py", line 34, in __init__
self.transformer.set_mean(in_, mean)
File "/home/ubuntu/caffe/python/caffe/io.py", line 255, in set_mean
raise ValueError('Mean shape incompatible with input shape.’)
ValueError: Mean shape incompatible with input shape.
[/shell]

そのため、caffe/python/caffe/io.pyを以下のように編集しておく。

[python title="caffe/python/caffe/io.pyの内容"]
#raise ValueError('Mean shape incompatible with input shape.’)
print(self.inputs[in_])
in_shape = self.inputs[in_][1:]
m_min, m_max = mean.min(), mean.max()
normal_mean = (mean – m_min) / (m_max – m_min)
mean = resize_image(normal_mean.transpose((1,2,0)),in_shape[1:]).transpose((2,0,1)) * (m_max – m_min) + m_min
[/python]