Pythonの環境構築とTensorFlow

#Pythonの環境構築とTensorFlow
[からあげさんのページ](https://karaage.hatenadiary.jp/entry/2016/04/04/073000)を参考にさせて頂きながらインストールした時のログ（備忘録）です。

##Pythonの環境構築をする。

###pyenvのインストール
    $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

####.bashrcの編集
 
    $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    $ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    $ source ~/.bashrc

###python(Anaconda)インストール
#### Anaconda2系
    $ pyenv install -l | grep "anaconda2-*
    anaconda2-2.4.0
    anaconda2-2.4.1
    anaconda2-2.5.0
    anaconda2-4.0.0
    anaconda2-4.1.0
    anaconda2-4.1.1
    anaconda2-4.2.0
    anaconda2-4.3.0
    anaconda2-4.3.1
    anaconda2-4.4.0
    anaconda2-5.0.0
    anaconda2-5.0.1
    anaconda2-5.1.0
    anaconda2-5.2.0
    anaconda2-5.3.0
    anaconda2-5.3.1
    anaconda2-2018.12
    anaconda2-2019.03
    anaconda2-2019.07

    $ pyenv install anaconda2-5.3.1
    Downloading Anaconda2-5.3.1-Linux-x86_64.sh...
    -> https://repo.continuum.io/archive/Anaconda2-5.3.1-Linux-x86_64.sh
    Installing Anaconda2-5.3.1-Linux-x86_64...
    Installed Anaconda2-5.3.1-Linux-x86_64 to ~/.pyenv/versions/anaconda2-5.3.1

#### Anaconda3系
    $ pyenv install -l | grep "anaconda3-*
    anaconda3-2.0.0
    anaconda3-2.0.1
    anaconda3-2.1.0
    anaconda3-2.2.0
    anaconda3-2.3.0
    anaconda3-2.4.0
    anaconda3-2.4.1
    anaconda3-2.5.0
    anaconda3-4.0.0
    anaconda3-4.1.0
    anaconda3-4.1.1
    anaconda3-4.2.0
    anaconda3-4.3.0
    anaconda3-4.3.1
    anaconda3-4.4.0
    anaconda3-5.0.0
    anaconda3-5.0.1
    anaconda3-5.1.0
    anaconda3-5.2.0
    anaconda3-5.3.0
    anaconda3-5.3.1
    anaconda3-2018.12
    anaconda3-2019.03
    anaconda3-2019.07
    anaconda3-2019.10
    anaconda3-2020.02

    $ pyenv install anaconda3-5.3.1
    Downloading Anaconda3-5.3.1-Linux-x86_64.sh.sh...
    -> https://repo.continuum.io/archive/Anaconda3-5.3.1-Linux-x86_64.sh
    Installing Anaconda3-5.3.1-Linux-x86_64.sh...
    Installed Anaconda3-5.3.1-Linux-x86_64.sh to /home/miyamoto/.pyenv/versions/anaconda3-5.3.1

    $ pyenv global anaconda2-5.3.1
    $ pyenv versions 
    system
    * anaconda2-5.3.1 (set by /home/miyamoto/.pyenv/version)
      anaconda3-5.3.1

    $ pyenv global anaconda3-5.3.1
    $ pyenv versions
    system
     anaconda2-5.3.1
    * anaconda3-5.3.1 (set by /home/miyamoto/.pyenv/version)

    $ pyenv global system
    $ pyenv versions
    * system (set by /home/miyamoto/.pyenv/version)
     anaconda2-5.3.1
     anaconda3-5.3.1

##pyenv-virtualenvインストール
    $ git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

    $ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    $ echo export PYENV_VIRTUALENV_DISABLE_PROMPT=1 >> ~/.bashrc
    $ source ~/.bashrc

##virtualenvのインストール
    $ sudo python3 -m pip install virtualenv
    $ virtualenv env


##学習環境構築
####TensolFlowとOpenCVのインストール
    $ pyenv global anaconda3-5.3.1
    $ conda create -n od anaconda
    $ pyenv global anaconda3-5.1.0/envs/od

    python3.8系だとtensorflow1.14.0がインストールできないので3.6系にする。
    $ conda install python=3.6.6

    $ pip install tensorflow==1.14.0
    $ pip install opencv-python


####
    $ sudo apt install protobuf-compiler
    protoc --version
    libprotoc 3.0.0

    $ cd ~ 
    $ git clone https://github.com/tensorflow/models
    $ cd ~/model/research
    $ protoc object_detection/protos/*.proto --python_out=.

####動作確認
    $ cd model/research
    $ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
    $ python object_detection/builders/model_builder_test.py
    Running tests under Python 3.6.6: ~/.pyenv/versions/anaconda3- 
    5.1.0/envs/od/bin/python
    [ RUN      ] ModelBuilderTest.test_create_experimental_model
    [       OK ] ModelBuilderTest.test_create_experimental_model 
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_model_from_config_with_example_miner
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_model_from_config_with_example_miner
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_with_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_with_matmul
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_without_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_without_matmul
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_with_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_with_matmul
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_without_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_without_matmul
    [ RUN      ] ModelBuilderTest.test_create_rfcn_model_from_config
    [       OK ] ModelBuilderTest.test_create_rfcn_model_from_config
    [ RUN      ] ModelBuilderTest.test_create_ssd_fpn_model_from_config
    [       OK ] ModelBuilderTest.test_create_ssd_fpn_model_from_config  
    [ RUN      ] ModelBuilderTest.test_create_ssd_models_from_config
    [       OK ] ModelBuilderTest.test_create_ssd_models_from_config
    [ RUN      ] ModelBuilderTest.test_invalid_faster_rcnn_batchnorm_update
    [       OK ] ModelBuilderTest.test_invalid_faster_rcnn_batchnorm_update
    [ RUN      ] ModelBuilderTest.test_invalid_first_stage_nms_iou_threshold
    [       OK ] ModelBuilderTest.test_invalid_first_stage_nms_iou_threshold
    [ RUN      ] ModelBuilderTest.test_invalid_model_config_proto
    [       OK ] ModelBuilderTest.test_invalid_model_config_proto
    [ RUN      ] ModelBuilderTest.test_invalid_second_stage_batch_size
    [       OK ] ModelBuilderTest.test_invalid_second_stage_batch_size
    [ RUN      ] ModelBuilderTest.test_session
    [  SKIPPED ] ModelBuilderTest.test_session
    [ RUN      ] ModelBuilderTest.test_unknown_faster_rcnn_feature_extractor
    [       OK ] ModelBuilderTest.test_unknown_faster_rcnn_feature_extractor
    [ RUN      ] ModelBuilderTest.test_unknown_meta_architecture
    [       OK ] ModelBuilderTest.test_unknown_meta_architecture
    [ RUN      ] ModelBuilderTest.test_unknown_ssd_feature_extractor
    [       OK ] ModelBuilderTest.test_unknown_ssd_feature_extractor
    ----------------------------------------------------------------------
    Ran 17 tests in 0.197s
    OK (skipped=1)
####cocoapiのインストール
    $ pip install git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI

####OpenCVのインストール
    $ conda install -c conda-forge opencv

####import matplotlib.pyplotでコアダンプ
```
$ python
Python 3.6.6 |Anaconda, Inc.| (default, Oct  9 2018, 12:34:16) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import  matplotlib.pyplot
Segmentation fault (core dumped)
```
```
これはこれが原因らしい。
 $ conda install -c conda-forge opencv
対策方法は
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
```

<B>これでも直る(こっちの方が良い）</b>

```
conda install qt
conda remove opencv
pip install opencv-python
```
####cap = cv2.VideoCapture(DEVICE_ID)でエラー
<B>このエラーもpipでOpenCVを再インストールしたら解決</B>

```
Traceback (most recent call last):
  File "./detect.py", line 104, in <module>
    height, width, channels = c_frame.shape
AttributeError: 'NoneType' object has no attribute 'shape'
```
##参考
https://www.petitmonte.com/python/object_detection_api.html
https://qiita.com/sage-git/items/aa3d2acd93cded4be2e8