LoginSignup
3
3

More than 3 years have passed since last update.

【Jetson_nano】Opencv, Chainer, Keras,そしてScikit-learn環境構築♬

Last updated at Posted at 2019-06-16

Jetson nanoへOpencvをインストールするのにすごくハマったので記事にしたいと思います。
結論からいうと、前回はChainerとKerasは仮想環境へ構築しましたが、今回はこれができなかったので、仮想環境を使わないこととしました。
合わせて仮想環境を使わない場合のChainer、Keras、そしてScikit-learnの環境構築も整理しました。
これで、Jetson-nanoの環境構築は完成したと思います。

なお、結果的にOpencvのインストールは以下の参考①の記事をまるまる実施しました。
もともと、参考②のスクリプトを訂正して使用されていますが、更地からのインストールでは参考①のスクリプトがいいと思います。また、今回は更地ではありませんでしたが、最終的に参考①のスクリプトを使用しました。
また、Tensorflow-gpu及びJupyter-notebookまでのインストールは前回記事を見てください。
【参考】
Jetson Nano へ OpenCV 4.1.0 をインストールする
JEP/script/install_opencv4.0.0_Nano.sh

概略のまとめ

・Opencv4.1.0のインストール
・kerasのインストール
・matplotlibのインストール
・Chainerのインストール
・scikit-learnのインストール
インストール詳細以降はおまけ的に記載しています。

Opencv4.1.0のインストール

インストール前にスワップ領域確保

$ fallocate -l 4G swapfile
$ chmod 600 swapfile
$ mkswap swapfile
$ sudo swapon swapfile

install_opencv4.1.0_Nano.sh(参考①のスクリプト)を用意します。
そしてインストール先のDirを作成し、引数にして実行ます。

$ mkdir opencv
$ sh install_opencv4.1.0_Nano.sh opencv

ひたすら待ちますが、途中何度か認証を聞かれるので、入力します。

kerasのインストール

ここからは、pip3の最後にpermission denied除けに--userをつけています。

$ sudo apt install libatlas-base-dev gfortran
$ pip3 install -U cython --user
$ pip3 install keras --user

※今回は、gfortranは更新ありませんでした

matplotlibのインストール

$ pip3 install matplotlib --user

Chainerのインストール

$ pip3 install cupy --user
$ pip3 install chainer --user

※Cupyは時間がかかるので待ちます

scikit-learnのインストール

$ pip3 install scikit-learn --user

まとめ

・仮想環境を使わずに普通の環境にOpencv、Chainer、Keras、Scikit-learnなど当面使う環境構築を実施した

・Opencvのアプリとして暗視カメラは動作した

インストール詳細

以下に時系列なエビデンス付きイントールを記載しておきます。
※若干、試行錯誤が入っているので上記の概略とは一部異なります

Opencvのインストール

以下のOpencvは何度かリトライした結果の表示なのですが、以下のようにpython2.7とpython3.6ともにimport cv2ができ、インストールできました。

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-opencv is already the newest version (3.2.0+dfsg-4ubuntu0.1).
python3-opencv is already the newest version (3.2.0+dfsg-4ubuntu0.1).
0 upgraded, 0 newly installed, 0 to remove and 23 not upgraded.
** Install opencv-4.1.0 successfully
** Bye :)
muauan@muauan-desktop:~/Documents/Chainer$ python
Python 2.7.15+ (default, Nov 27 2018, 23:36:35) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 
[1]+  Stopped                 python
muauan@muauan-desktop:~/Documents/Chainer$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 
[2]+  Stopped                 python3

kerasのインストール

muauan@muauan-desktop:~/Documents/Chainer$ pip3 install keras
Collecting keras
  Using cached https://files.pythonhosted.org/packages/5e/10/aa32dad071ce52b5502266b5c659451cfd6ffcbf14e6c8c4f16c0ff5aaab/Keras-2.2.4-py2.py3-none-any.whl
Collecting scipy>=0.14 (from keras)
Requirement already satisfied: numpy>=1.9.1 in /home/muauan/.local/lib/python3.6/site-packages (from keras) (1.16.4)
...
Requirement already satisfied: h5py in /home/muauan/.local/lib/python3.6/site-packages (from keras) (2.9.0)
Installing collected packages: scipy, keras
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/scipy-1.3.0.dist-info'
Consider using the `--user` option or check the permissions.

上記のようにpermission deniedが出ていて、--userを付けうように推奨されています。
しかし、以下のように仮想環境でこれをやると別のエラーが出てできませんでした。

muauan@muauan-desktop:~/Documents/Chainer$ sudo pip3 install keras
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

ということで、普通の環境というかdesktop配下にインストールします。
念のため、cytonからインストールします。

muauan@muauan-desktop:~$ pip3 install -U cython --user
Collecting cython
Installing collected packages: cython
Successfully installed cython-0.29.10

無事にインストールできました。
そして、keras本体をインストールします。

muauan@muauan-desktop:~$ pip3 install keras --user
Collecting keras
  Using cached https://files.pythonhosted.org/packages/5e/10/aa32dad071ce52b5502266b5c659451cfd6ffcbf14e6c8c4f16c0ff5aaab/Keras-2.2.4-py2.py3-none-any.whl
Requirement already satisfied: h5py in ./.local/lib/python3.6/site-packages (from keras) (2.9.0)
...
Installing collected packages: scipy, keras
Successfully installed keras-2.2.4 scipy-1.3.0

※前回同様、Scipyも無事にインストールされています
そして、以下のように検証できました。

muauan@muauan-desktop:~$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import keras
Using TensorFlow backend.
>>> 

matplotlibのインストール

muauan@muauan-desktop:~$ pip3 install matplotlib --user
Collecting matplotlib
...
Successfully built matplotlib kiwisolver
Installing collected packages: cycler, kiwisolver, pyparsing, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.1.0 matplotlib-3.1.0 pyparsing-2.4.0

Chainerのインストール

Chainerのインストールで難関なのは以下のCupyのインストールです。
これも1時間程度は待たされますが、ひたすら待つと完了します。
※fastrlockのインストール時、止まったように見えるので我慢しましょう

muauan@muauan-desktop:~$ pip3 install cupy --user
Collecting cupy
...
Successfully built cupy fastrlock
Installing collected packages: fastrlock, cupy
Successfully installed cupy-6.0.0 fastrlock-0.4

そして、以下のようにChainerをインストールします。
これは、あっけなくすぐに終わります。
※何度かインストールしていたせいかも知れません

muauan@muauan-desktop:~$ pip3 install chainer --user
Collecting chainer
...
Successfully installed chainer-6.0.0 filelock-3.0.12 protobuf-3.7.1 typing-3.6.6 typing-extensions-3.7.2

無事にインストールできたことが分かります。

muauan@muauan-desktop:~$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import chainer
>>> 

Scikit-leranのインストール

これもかなり時間かかりますが、以下で無事にインストールできました。

muauan@muauan-desktop:~$ pip3 install scikit-learn --user
Collecting scikit-learn
...
Successfully installed joblib-0.13.2 scikit-learn-0.21.2

以下のように確認できます。

muauan@muauan-desktop:~$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sklearn.linear_model import LogisticRegression
>>> 

keras版MNISTで検証

muauan@muauan-desktop:~$ python3 mnist-keras.py
60000 train samples
10000 test samples
...
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 512)               401920    
_________________________________________________________________
dropout (Dropout)            (None, 512)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               262656    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                5130      
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
...
Epoch 1/20
2019-06-16 16:08:39.096871: I tensorflow/stream_executor/dso_loader.cc:153] successfully opened CUDA library libcublas.so.10.0 locally
60000/60000 [==============================] - 17s 282us/sample - loss: 0.2495 - acc: 0.9228 - val_loss: 0.1152 - val_acc: 0.9640
...
Epoch 20/20
60000/60000 [==============================] - 13s 211us/sample - loss: 0.0200 - acc: 0.9948 - val_loss: 0.1162 - val_acc: 0.9824
Test loss: 0.11615371883504044
Test accuracy: 0.9824

おまけ(暗視カメラ)

Opencvの確認を兼ねて、先日の暗視カメラを動かしてみました。
無事に動きました。
⇒詳細は別記事にしたいと思います

equalize.py
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import pylab as plt

def treatise(frame,size):
    frame1 = cv2.resize(frame, dsize=size, interpolation=cv2.INTER_CUBIC)
    gridsize=8
    bgr = frame1
    lab = cv2.cvtColor(bgr, cv2.COLOR_BGR2LAB)
    lab_planes = cv2.split(lab)
    clahe = cv2.createCLAHE(clipLimit=3.0,tileGridSize=(gridsize,gridsize))
    lab_planes[0] = clahe.apply(lab_planes[0])
    lab = cv2.merge(lab_planes)
    bgr = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
    return bgr

def histogram_equalize(img,size):
    img = cv2.resize(img, dsize=size, interpolation=cv2.INTER_CUBIC)
    b, g, r = cv2.split(img)
    red = cv2.equalizeHist(r)
    green = cv2.equalizeHist(g)
    blue = cv2.equalizeHist(b)
    return cv2.merge((blue, green, red))

def histogram_equalize_treat(img,size):
    img = cv2.resize(img, dsize=size, interpolation=cv2.INTER_CUBIC)
    b, g, r = cv2.split(img)
    red = cv2.equalizeHist(r)
    green = cv2.equalizeHist(g)
    blue = cv2.equalizeHist(b)
    bgr = cv2.merge((blue, green, red))
    lab = cv2.cvtColor(bgr, cv2.COLOR_BGR2LAB)
    lab_planes = cv2.split(lab)
    clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(4,4))
    lab_planes[0] = clahe.apply(lab_planes[0])
    lab = cv2.merge(lab_planes)
    bgr = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
    return bgr

def histogram_equalize_hsv(img,size):
    img = cv2.resize(img, dsize=size, interpolation=cv2.INTER_CUBIC)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(img)
    h = cv2.equalizeHist(h)
    s = cv2.equalizeHist(s)
    v = cv2.equalizeHist(v)
    bgr = cv2.merge((h, s, v))
    return bgr

def histogram_equalize_yuv(img,size):
    img = cv2.resize(img, dsize=size, interpolation=cv2.INTER_CUBIC)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
    img[:,:,0] = cv2.equalizeHist(img[:,:,0])
    img_output = cv2.cvtColor(img, cv2.COLOR_YUV2BGR)
    return img_output

video_input = cv2.VideoCapture(0)
size=(600,450)
while(1):
    ret, frame2 = video_input.read()
    cv2.imshow("frame2", frame2)
    bgr = treatise(frame2,size)
    cv2.imshow("equalizeHist", bgr)
    bgr = histogram_equalize(frame2,size)
    cv2.imshow("equalizeHist_rgb", bgr)
    bgr = histogram_equalize_yuv(frame2,size)
    cv2.imshow("equalizeHist_yuv", bgr)
    bgr = histogram_equalize_hsv(frame2,size)
    cv2.imshow("equalizeHist_hsv", bgr)
    bgr = histogram_equalize_treat(frame2,size)
    cv2.imshow("equalize_treat", bgr)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3