Git Product home page Git Product logo

lec_caffe's Introduction

#Ubuntu 14.04にcaffeをインストール ##基本的に以下のURLを参照 参考サイトにもあるようにUbuntu 14.04の最新版(その時点でのバージョンアップはすべて更新済みの状態)で確認しています.
[※注意]このREADMEはnVIDIAのCUDAを使わない方法です.
CUDAとcuDNNを使う方法も追記

Github
Ubuntu 14.04 VirtualBox VM
https://github.com/BVLC/caffe/wiki/Ubuntu-14.04-VirtualBox-VM

##前準備

ビルドに必要な最初のパッケージ群

sudo apt-get install build-essential

依存関係で必要なパッケージをインストール

sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev protobuf-compiler gfortran libjpeg62 libfreeimage-dev libatlas-base-dev git python-dev python-pip libgoogle-glog-dev libbz2-dev libxml2-dev libxslt-dev libffi-dev libssl-dev libgflags-dev liblmdb-dev python-yaml
sudo easy_install pillow

##CUDAとcuDNNを使う ###CUDAのインストール https://developer.nvidia.com/cuda-downloads#linux
ここからLinux x86のタブのUbuntu 14.04 DEB (3KB)をダウンロードする.

sudo dpkg -i cuda-repo-ubuntu1404_7.0-28_amd64.deb
sudo apt-get update
sudo apt-get install cuda

.bashrcに以下を記述
この.bashrcの記述がサンプル実行時に大切になります.

#CUDA
export CUDA_HOME=/usr/local/cuda-7.0 
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64 
 
PATH=${CUDA_HOME}/bin:${PATH} 
export PATH 

サンプルコードをコンパイル

cuda-install-samples-7.0.sh ~
cd ~/NVIDIA_CUDA-7.0_Samples
make 

####参考サイト

Installing CUDA Toolkit 7.0 on Ubuntu 14.04 Linux
http://www.r-tutor.com/gpu-computing/cuda-installation/cuda7.0-ubuntu

###cuDNNのインストール https://developer.nvidia.com/cudnnからダウンロード
※デベロッパー登録必要

tar -zxf cudnn-6.5-linux-x64-v2.tgz
cd cudnn-6.5-linux-x64-v2
sudo cp lib* /usr/local/cuda/lib64/
sudo cp cudnn.h /usr/local/cuda/include/

##本家レポジトリからClone プログラムをダウンロードしてくる場所で以下を実行

git clone https://github.com/BVLC/caffe.git
cd caffe

Python caffeを実行するために必要なパッケージをインストール. ディレクトリは先ほどcdしたcaffeで行う.

cat python/requirements.txt | xargs -L 1 sudo pip install

シンボリックリンクを作成

sudo ln -s /usr/include/python2.7/ /usr/local/include/python2.7
sudo ln -s /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ /usr/local/include/python2.7/numpy

Makefile.configを作成し,Geditで編集する

cp Makefile.config.example Makefile.config
gedit Makefile.config

###CPUのみを使う場合 8行目の#を外して

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

以下のようにする.

CPU_ONLY := 1

###CUDAとcuDNNを使う場合 5行目のコメントアウトを外す.

# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1

15行目のコメントアウトを外す.上の方法でCUDAを入れた場合,ここを外しました.

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda

###CPUのみ,CUDA使用,共通の作業 また52行目を以下のように編集.これを

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
    /usr/lib/python2.7/dist-packages/numpy/core/include

以下に変更. /localを追加

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
    /usr/local/lib/python2.7/dist-packages/numpy/core/include

##caffeをコンパイル

make pycaffe -j4
make all -j4
make test -j4

##ImageNet Caffe modelとlabelsのダウンロード

./scripts/download_model_binary.py models/bvlc_reference_caffenet
./data/ilsvrc12/get_ilsvrc_aux.sh

##サンプルプログラムのエラー処理 このままサンプルを実行すると,以下のようなエラーが.

I0615 15:54:24.737364 17294 upgrade_proto.cpp:626] Successfully upgraded file specified using deprecated V1LayerParameter
Traceback (most recent call last):
  File "python/classify.py", line 138, in <module>
    main(sys.argv)
  File "python/classify.py", line 110, in main
    channel_swap=channel_swap)
  File "/home/ry0/caffe/python/caffe/classifier.py", line 34, in __init__
    self.transformer.set_mean(in_, mean)
  File "/home/ry0/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.

参考サイト

Qiita
OSX10.10でCaffeをインストール、リファレンスモデルで画像を分類
http://qiita.com/Bonnnou_108/items/41e6dadeff1310b4eb5d#classifypy%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E7%94%BB%E5%83%8F%E5%88%86%E9%A1%9E

ここにあるようにpython/caffe/io.pyの255行目を編集する.

if ms != self.inputs[in_][1:]:
    raise ValueError('Mean shape incompatible with input shape.')
if ms != self.inputs[in_][1:]:
    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
    #raise ValueError('Mean shape incompatible with input shape.')

##サンプルを実行するための準備 以下のコードをshow_result.pyとしてcaffe/python/に保存.ファイルはこのレポジトリにあるshow_result.pyでもいいです.
このソースコードはhttp://techblog.yahoo.co.jp/programming/caffe-intro/から拝借しました.

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys, numpy

categories = numpy.loadtxt(sys.argv[1], str, delimiter="\t")
scores = numpy.load(sys.argv[2])
top_k = 3
prediction = zip(scores[0].tolist(), categories)
prediction.sort(cmp=lambda x, y: cmp(x[0], y[0]), reverse=True)
for rank, (score, name) in enumerate(prediction[:top_k], start=1):
    print('#%d | %s | %4.1f%%' % (rank, name, score * 100))

##いよいよ実行 かわいいわんちゃんをcaffeにかけてみます.この写真を保存するか,以下のコマンドのようにネット上にある画像を指定してもよい.

golden.jpg

cd python
python classify.py --raw_scale 255 https://raw.githubusercontent.com/Nishida-Lab/lec_caffe/master/golden.jpg ./result.npy
python show_result.py ../data/ilsvrc12/synset_words.txt result.npy

もしも,

...
...
ImportError: No module named skimage.io

のようなエラーが出た場合には以下のコマンドを実行してみてください.

sudo apt-get install python-skimage

これをすべて実行すると,以下のように結果が出力される.

#1 | n02099601 golden retriever | 93.5%
#2 | n02099712 Labrador retriever |  4.5%
#3 | n02111500 Great Pyrenees |  1.0%

ゴールデンレトリバーが93.5%,ラブラドールレトリバーが4.5%と高い割合でcaffeは判断してます!
おわりです!

lec_caffe's People

Contributors

ariyu avatar sarurock1992 avatar miyamon11 avatar ry0 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.