ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

centos7の機械学習の開発環境の構築メモ

centos7で機械学習の開発環境を構築する手順をまとめてみました。
内容としては開発ツールのインストール作業と簡単な動作チェックのメモになります。

OSバージョン確認

CentOSで環境構築を行います。

[test@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[test@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

開発ツール一式インストール

rootユーザで実行してます。

yum関連

# yum update
# yum upgrade 
# yum install gcc zlib-devel bzip2 bzip2-devel openssl openssl-devel readline-devel readline ncurses-devel sqlite sqlite-devel gdbm-devel expat-devel libpcap-devel xz-devel pcre-devel wget gcc-c++ libiconv patch libffi-devel
# yum groupinstall "Development tools"
# yum install tk-devel

python環境準備

python3.9.0インストール

pyenvでpython3をインストールします。

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ source .bash_profile
$ pyenv install 3.9.0
$ pyenv global 3.9.0
$ pyenv rehash
$ python --version
Python 3.9.0
pipインストール
$ pip install --upgrade pip
$ pip install argparse
$ pip install python-dateutil
$ pip install numpy
$ pip install scipy
$ pip install cython
$ pip install pandas
$ pip install setuptools
$ pip install matplotlib

機械学習関連

darknet

darknetのインストール

darknetはC言語で実装された機械学習用フレームワークです。

git clone https://github.com/pjreddie/darknet.git
cd darknet
make
vgg-conv.weightsダウンロード
$ wget http://pjreddie.com/media/files/vgg-conv.weights
テスト実行

https://pjreddie.com/darknet/nightmare/

$ ./darknet nightmare cfg/vgg-conv.cfg vgg-conv.weights ../monster04.png 10
  • 入力

  • 出力

gensim

gensimのインストール

Gensimは自然言語処理でよく使われるアルゴリズムを含むオープンソースライブラリです。
ただインストールしようとすると以下のエラーが。。。
あとで調査します。

$ pip install --upgrade gensim
Collecting gensim
  Using cached gensim-3.8.3.tar.gz (23.4 MB)
    ERROR: Command errored out with exit status 1:
     command: /home/test/.pyenv/versions/3.9.0/bin/python3.9 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-qpatpfp4/gensim_a268b52ad4aa47b5b41b6d3c6825e950/setup.py'"'"'; __file__='"'"'/tmp/pip-install-qpatpfp4/gensim_a268b52ad4aa47b5b41b6d3c6825e950/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-9do1kw6k
         cwd: /tmp/pip-install-qpatpfp4/gensim_a268b52ad4aa47b5b41b6d3c6825e950/
    Complete output (11 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/test/.pyenv/versions/3.9.0/lib/python3.9/site-packages/setuptools/__init__.py", line 23, in <module>
        from setuptools.dist import Distribution
      File "/home/test/.pyenv/versions/3.9.0/lib/python3.9/site-packages/setuptools/dist.py", line 34, in <module>
        from setuptools import windows_support
      File "/home/test/.pyenv/versions/3.9.0/lib/python3.9/site-packages/setuptools/windows_support.py", line 2, in <module>
        import ctypes
      File "/home/test/.pyenv/versions/3.9.0/lib/python3.9/ctypes/__init__.py", line 8, in <module>
        from _ctypes import Union, Structure, Array
    ModuleNotFoundError: No module named '_ctypes'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
エラーの回避方法

「libffi-devel」がインストールされていないためにおこるえらーのようです。
「libffi-devel」をインストール後にpython3.9.0を再インストールすると回避できました。

$ sudo yum install libffi-devel
$ pyenv uninstall 3.9.0
$ pyenv install 3.9.0
再度インストール
$ pip install --upgrade gensim

mecab

ソースファイルダウンロード

以下のURLからソースファイルをダウンロードします。
https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7cENtOXlicTFaRUE

mecab-ipadicインストール
$ tar zvxf mecab-ipadic-2.7.0-20070801.tar.gz
$ cd mecab-ipadic-2.7.0-20070801
$ ./configure --with-charset=utf8
$ make
$ sudo make install
テスト実行
$ mecab
新型コロナウイルス感染が一刻も早く収束することを願う。
新型    名詞,一般,*,*,*,*,新型,シンガタ,シンガタ
コロ    名詞,一般,*,*,*,*,*
新型    名詞,一般,*,*,*,*,新型,シンガタ,シンガタ
コロナ  名詞,一般,*,*,*,*,コロナ,コロナ,コロナ
ウイルス        名詞,一般,*,*,*,*,ウイルス,ウイルス,ウイルス
感染    名詞,サ変接続,*,*,*,*,感染,カンセン,カンセン
が      助詞,格助詞,一般,*,*,*,が,ガ,ガ
一刻    名詞,副詞可能,*,*,*,*,一刻,イッコク,イッコク
も      助詞,係助詞,*,*,*,*,も,モ,モ
早く    形容詞,自立,*,*,形容詞・アウオ段,連用テ接続,早い,ハヤク,ハヤク
収束    名詞,サ変接続,*,*,*,*,収束,シュウソク,シューソク
する    動詞,自立,*,*,サ変・スル,基本形,する,スル,スル
こと    名詞,非自立,一般,*,*,*,こと,コト,コト
を      助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
願う    動詞,自立,*,*,五段・ワ行促音便,基本形,願う,ネガウ,ネガウ
。      記号,句点,*,*,*,*,。,。,。
EOS

fasText

インストール
$ pip install pybind11
$ git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ python setup.py install
$ make
テスト実行
$ cat data.txt
__label__5, 新型 コロナウイルス 感染 が 一刻 も 早く 収束 する こと を 願う 。
$ ~/fastText/fasttext skipgram -input data.txt -output output_model -lr 0.025 -dim 100 -ws 5  -epoch 1 -minCount 5 -neg 5 -loss ns -bucket 2000000 -minn 3 -maxn 6  -thread 4 -t 1e-4 -lrUpdateRate 100
Read 0M words
Number of words:  0
Number of labels: 1
Progress: 100.0% words/sec/thread:     500 lr:  0.000000 avg.loss:      -nan ETA:   0h 0m 0s

PythonライブラリをインストールためcloneしたfastTextで以下を実行します。

$ pip install .
Processing /home/test/fastText
Requirement already satisfied: pybind11>=2.2 in /home/test/.pyenv/versions/3.9.0/lib/python3.9/site-packages (from fasttext==0.9.2) (2.6.1)
Requirement already satisfied: setuptools>=0.7.0 in /home/test/.pyenv/versions/3.9.0/lib/python3.9/site-packages (from fasttext==0.9.2) (49.2.1)
Requirement already satisfied: numpy in /home/test/.pyenv/versions/3.9.0/lib/python3.9/site-packages/numpy-1.20.0rc1-py3.9-linux-x86_64.egg (from fasttext==0.9.2) (1.20.0rc1)
Using legacy 'setup.py install' for fasttext, since package 'wheel' is not installed.
Installing collected packages: fasttext
  Attempting uninstall: fasttext
    Found existing installation: fasttext 0.9.2
    Uninstalling fasttext-0.9.2:
      Successfully uninstalled fasttext-0.9.2
    Running setup.py install for fasttext ... done
Successfully installed fasttext-0.9.2

pythonから使用できるか確認します。
エラーなく動作できるのでいったん作業を完了とします。

$ python
Python 3.9.0 (default, Dec  9 2020, 00:39:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fasttext
>>> model = fasttext.load_model('output_model.bin')
>>> model.get_words()
[]

chainer

インストール
$ sudo yum -y install python-devel
$ sudo yum -y install epel-release
$ sudo yum install hdf5-devel
$ pip install chainer
動作テスト
$ git clone  https://github.com/chainer/chainer
$ cd chainer/examples/mnist/
$ python train_mnist.py

参考にしたサイト

canalier.com