---
# System prepended metadata

title: 環境架設與執行 yolov4 (Ubuntu 20.04)
tags: [Python, setup]

---

---
title: '環境架設與執行 yolov4 (Ubuntu 20.04)'
disqus: hackmd
---

環境架設與執行 yolov4(Ubuntu 20.04)
===

[TOC]

## 一、安裝環境
### 1. CUDA Toolkit and cuDNN configured and installed
安裝 TesorFlow 2.0

#### (1) 更新系統
```gherkin=
sudo apt-get update
sudo apt-get upgrade
```
#### (2) 安裝 compiler 工具
```gherkin=
sudo apt remove --purge cmake
hash -r
sudo snap install cmake --classic
觀察 cmake 版本
cmake --version
```
![](https://i.imgur.com/ZdlpyrR.png)

#### (3) 安裝 X windows libraries and OpenGL libraries
```gherkin=
udo apt-get install libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

#Along with image and video I/O libraries:
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev

#ptimization libraries:
sudo apt-get install libopenblas-dev libatlas-base-dev liblapack-dev gfortran

#HDF5 for working with large datasets
sudo apt-get install libhdf5-serial-dev
```

#### (4) 安裝 NVIDIA GPU drivers
```gherkin=
# 新增相關安裝到 apt-get的倉庫
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update

# 看推薦的版本
ubuntu-drivers devices

# 安裝推荐版本
sudo apt-get install nvidia-driver-"recommended version"

# 重新開機
sudo reboot now

# 察看 GPU 狀態
nvidia-smi
```
#### (5) 安裝 nvidia-cuda-toolkit
```gherkin=
sudo apt install nvidia-cuda-toolkit
```

#### (6) 安裝 CUDA Toolkit 11.1
先確認GPU 支援CUDA
```gherkin=
sudo lshw -numeric -C display
```

[CUDA](https://developer.nvidia.com/cuda-11.1.1-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=2004&target_type=deblocal)
https://developer.nvidia.com/cuda-toolkit-archive
![](https://i.imgur.com/dhD4unv.png)
```gherkin=
# 點開紅框會有下列訊息,依序執行安裝
cd /tmp
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.1.1/local_installers/cuda-repo-ubuntu2004-11-1-local_11.1.1-455.32.00-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-1-local_11.1.1-455.32.00-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu2004-11-1-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda
```

#### (7) 設定環境變數
```gherkin=
vim ~/.bashrc
# 輸入以下兩行並存檔離開
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
source ~/.bashrc

# 確認安裝正常
nvcc -V
```
![](https://i.imgur.com/JC9MCWo.png)

#### (8) 安裝 cuDNN
[cuDNN](https://developer.nvidia.com/rdp/cudnn-archive)
![](https://i.imgur.com/som4TO1.png)


Download cuDNN v8.0.5 (November 9th, 2020), for CUDA 11.1
```gherkin=
mv ~/Downloads/cudnn-11.1-linux-x64-v8.0.5.39.tgz /tmp
cd /tmp
tar -zxf cudnn-11.1-linux-x64-v8.0.5.39.tgz
cd cuda
sudo cp -P lib64/* /usr/local/cuda/lib64/
sudo cp -P include/* /usr/local/cuda/include/

#sudo cp cuda/include/cudnn*.h /usr/local/cuda/include 
#sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64 
#sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*

到目前為止已經安裝了
NVIDIA GPU drivers
CUDA 11.1
cuDNN 11.1 for CUDA 11.1
```

#### (9) 建立一虛擬環境
```gherkin=
conda create -n dln python=3.8
```

#### (10) 虛擬環境 安裝相關套件
```gherkin=
pip install tensorflow==2.6.0
#pip install opencv-python==4.5.1.48
#pip install opencv-contrib-python==4.5.1.48
pip install opencv-contrib-python
pip install matplotlib
pip install sklearn
pip install scikit-image
pip install imutils
pip install numpy
pip install pandas
pip install openpyxl
pip install scikit-learn
pip install progressbar2
pip install beautifulsoup4
```

## 二、 測試 TensorFlow 2.6.0
```gherkin=
python3
import tensorflow as tf
tf.__version__
```

![](https://i.imgur.com/FjUaAlO.png)

## 三、 下載 opencv source
由於虛擬環境沒有我們要的 符合NVIDIA GPU 的　opencv　版本, 所以需另外下載來源編譯使用
[opencv 下載](https://github.com/opencv/opencv)
```gherkin=
cd /tmp
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
mv opencv ~/
mv openc_contrib ~/
```

至此[網站](https://developer.nvidia.com/cuda-gpus)找電腦的NVIDIA GPU architecture version 數值
本裝置為 RTX2060 所以得到 7.5(CUDA_ARCH_BIN=7.5 cmake 會用到)
![](https://i.imgur.com/LlXjyYj.png)
```gherkin=
# Configure OpenCV with NVIDIA GPU support
cd ~/
cd opencv/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D INSTALL_C_EXAMPLES=OFF \
	-D OPENCV_ENABLE_NONFREE=ON \
	-D WITH_CUDA=ON \
	-D WITH_CUDNN=ON \
	-D OPENCV_DNN_CUDA=ON \
	-D ENABLE_FAST_MATH=1 \
	-D CUDA_FAST_MATH=1 \
	-D CUDA_ARCH_BIN=7.5 \
	-D WITH_CUBLAS=1 \
	-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
	-D HAVE_opencv_python3=ON \
	-D PYTHON_EXECUTABLE=~/.virtualenvs/opencv_cuda/bin/python \
	-D BUILD_EXAMPLES=ON ..
# 若執行 CMAKE 有發生過錯誤 而去重新配置 CUDA cuDnn 版本,請刪除build ,並重新製作 build
# 資料夾, 再重新執行一次 cmake

# Compile OpenCV with “dnn” GPU support  
make -j16

# Install OpenCV with “dnn” GPU support
sudo make install
sudo ldconfig

```



## x 參考文件

[update cmake](https://graspingtech.com/upgrade-cmake/)

[darknet](https://github.com/AlexeyAB/darknet)

https://www.youtube.com/watch?v=FE2GBeKuqpc&t=4s


https://www.pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/

https://www.pyimagesearch.com/2020/02/10/opencv-dnn-with-nvidia-gpus-1549-faster-yolo-ssd-and-mask-r-cnn/

[Ubuntu 18.04 and TensorFlow/Keras GPU install guide](https://www.pyimagesearch.com/2019/12/09/how-to-install-tensorflow-2-0-on-ubuntu/)

https://koding.work/how-to-install-cuda-and-cudnn-to-ubuntu-20-04/
https://learnopencv.com/opencv-dnn-with-gpu-support/

###### tags: `setup`, `Python`