基於Ubuntu 18.04.3操作系統的TensorFlow 2.1.0、PyTorch 1.4.0、OpenCV 4.2.0、Darknet深度學習環境搭建

今天是初五,民間有破五的習俗,簡單來說就是說該工作啦。
回顧研究生畢業的一年半,期間從事過桌面應用開發、CAN\串口開發、無人機目標檢測開發、爬蟲、數據工程、大數據系統運維等。研究生期間學的深度學習也好久沒再接觸,自己配的深度學習機器(菜鳥攢機之深度學習(上))(菜鳥攢機之深度學習(下))在家吃灰了好久,當時還是卡皇的GTX 1080也已經被1080 Ti、2080、2080 Ti取代,TensorFlow也從1.8迭代到了2.1,Caffe、MXNet、Torch逐漸退出人們視野,形成TensorFlow、PyTorch雙雄爭霸的局面,Python2也退出歷史舞臺,Python3迭代到3.8。隨着工作中的不斷學習積累,我也從Linux、Python小白中的小白,成長爲能夠獨立處理一些技術問題的中級用戶,對Linux、Python也有了一些更深刻的認識。
雖然當前的工作主要集中在數據工程、後端、爬蟲,但我還是更喜歡深度學習,那種從Top1 accuracy、Top5 accuracy曲線逐漸上升,Loss曲線逐漸下降帶來的心理滿足感是別的技術所無法取代的。趁着這兩天不忙,打算把家裏的機器重新部署一次深度學習環境,並把過程記錄下來,希望能幫到更多人。

願疫情早日結束!!!!!!!!!!!!!


系統環境:

  • Ubuntu 18.04.3 最小安裝版
  • i7-7700K
  • NVIDIA GTX 1080
  • 16G RAM
  • 256 SSD

1 Ubuntu系統安裝

1.1 製作安裝U盤

2020-01-29
Ubuntu官網下載ISO鏡像

  • Ubuntu下製作

使用Startup Disk Creator圖形化工具製作
在這裏插入圖片描述
如果找不到,則通過

>>> sudo apt install usb-creator-gtk
  • Windows下製作

使用Refus圖形化工具

  • OS X下製作

使用Etcher圖形化工具

1.2 安裝系統

按步驟安裝即可
我建議是最小化安裝,好多沒用的軟件如:libreoffice就不會裝上去,節省空間
切記在BIOS將secure boot關閉,否則即便安裝成功,啓動也會出問題。

2 配置基礎環境

2.1 更新系統

# 備份
>>> sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 修改爲清華源
>>> sudo vi /etc/apt/sources.list
# 默認註釋了源碼鏡像以提高 apt update 速度,如有需要可自行取消註釋
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 預發佈軟件源,不建議啓用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
>>> sudo apt update
>>> sudo apt upgrade

2.2 安裝NVIDIA驅動

>>> sudo add-apt-repository ppa:graphics-drivers/ppa
>>> sudo apt update
>>> sudo ubuntu-drivers autoinstall
>>> nvidia-smi
Wed Jan 29 22:30:34 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.48.02    Driver Version: 440.48.02    CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1080    Off  | 00000000:01:00.0  On |                  N/A |
|  0%   36C    P8    16W / 260W |    519MiB /  8116MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1206      G   /usr/lib/xorg/Xorg                            18MiB |
|    0      1252      G   /usr/bin/gnome-shell                          48MiB |
|    0      1506      G   /usr/lib/xorg/Xorg                           155MiB |
|    0      1649      G   /usr/bin/gnome-shell                         133MiB |
|    0      1982      G   ...uest-channel-token=10485691146349354111   158MiB |
+-----------------------------------------------------------------------------+
>>> sudo reboot now

2.3 安裝搜狗輸入法

官網下載安裝包,雙擊安裝,再通過settings→Region&Language→Manage Installed Language將輸入法框架從iBus換成fcitx
在這裏插入圖片描述
在這裏插入圖片描述
重新啓動
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
到這裏就能切換出搜狗拼音了
如果使用過程中出現候選框亂碼現象:
在這裏插入圖片描述
則通過

>>> cd ~/.config
>>> sudo rm -rf SogouPY* sogou*

再註銷重啓就恢復正常了

開啓ssh遠程連接

ssh分爲客戶端與服務端
默認安裝有客戶端openssh-client
想要遠程連接到本機,需要安裝openssh-server
通過以下命令查看本機ssh相關進程

>>> dpkg -l | grep ssh
ii  libssh-gcrypt-4:amd64                      0.8.0~20170825.94fa1e38-1ubuntu0.5              amd64        tiny C SSH library (gcrypt flavor)
ii  openssh-client                             1:7.6p1-4ubuntu0.3                              amd64        secure shell (SSH) client, for secure access to remote machines
>>> sudo apt install openssh-server
>>> dpkg -l | grep ssh
ii  libssh-gcrypt-4:amd64                      0.8.0~20170825.94fa1e38-1ubuntu0.5              amd64        tiny C SSH library (gcrypt flavor)
ii  openssh-client                             1:7.6p1-4ubuntu0.3                              amd64        secure shell (SSH) client, for secure access to remote machines
ii  openssh-server                             1:7.6p1-4ubuntu0.3                              amd64        secure shell (SSH) server, for secure access from remote machines
ii  openssh-sftp-server                        1:7.6p1-4ubuntu0.3                              amd64        secure shell (SSH) sftp server module, for SFTP access from remote machines
ii  ssh-import-id                              5.7-0ubuntu1.1                                  all          securely retrieve an SSH public key and install it locally

開啓、關閉、查看狀態

>>> systemctl start sshd.service
>>> systemctl stop sshd.service
>>> systemctl status sshd.service

2.4 安裝SS、genpac、proxychains

在這裏插入圖片描述在這裏插入圖片描述
參考:
https://github.com/JinnLynn/genpac
https://askubuntu.com/questions/60218/how-to-add-a-directory-to-the-path
https://askubuntu.com/questions/799302/ubuntu-cant-find-an-executable-file-in-local-bin
https://www.litcc.com/2016/12/29/Ubuntu16-shadow \
socks-pac/index.html
http://www.phpvar.com/archives/4567.html

2.5 安裝pip3

關於python3-pip和get-pip.py兩種安裝方法早有討論:https://github.com/pypa/pip/issues/5599
目前的結論是,通過apt install python3-pip方式安裝的是針對debin系統的pip,版本比較低,目前僅爲9.0.1,而通過get-pip.py方式安裝的爲pypa提供的最新版pip,目前爲20.0.2。但大部分人推薦使用python3-pip,而非最新的pip。如果不慎兩種方法都安裝了,可通過以下命令進行重置:

>>> python3 -m pip uninstall pip setuptools wheel
>>> sudo apt-get --reinstall install  python3-setuptools python3-wheel python3-pip

但考慮到加速pip包下載,需要使用高版本pip纔有的config功能,因而:

# 升級至最新版
>>> pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U pip
>>> pip3 --version
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
pip 20.0.2 from /home/microfat/.local/lib/python3.6/site-packages/pip (python 3.6)
# 通過設置config改變pip源爲清華源
>>> pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

默認源:
https://pypi.python.org/simple
配置文件位置:~/.config/pip/pip.conf
參考:https://github.com/pypa/pip/issues/5599#issuecomment-416318017

2.6 安裝git、zsh、oh-my-zsh


如果在安裝oh-my-zsh中存在下載過慢的問題,可通過socks5加速,前提是SS已經部署好了

>>> sh -c "$(curl --socks5 127.0.0.1:1080 -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

2.7 安裝CUDA、cuDNN

下載

在這裏插入圖片描述
在這裏插入圖片描述

安裝

CUDA:

>>> sudo sh cuda_10.1.105_418.39_linux.run
│ CUDA Installer                                                               │
│ - [ ] Driver                                                                 │
│      [ ] 418.39                                                              │
│ + [X] CUDA Toolkit 10.1                                                      │
│   [ ] CUDA Samples 10.1                                                      │
│   [X] CUDA Demo Suite 10.1                                                   │
│   [X] CUDA Documentation 10.1                                                │
│   Install                                                                    │
│   Options  
# 選擇Install後
===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-10.1/
Samples:  Not Selected

Please make sure that
 -   PATH includes /usr/local/cuda-10.1/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-10.1/lib64, or, add /usr/local/cuda-10.1/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-10.1/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-10.1/doc/pdf for detailed information on setting up CUDA.
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 418.00 is required for CUDA 10.1 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run --silent --driver

Logfile is /var/log/cuda-installer.log
# 配置路徑
>>> vim .zshrc
export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
# 使生效
>>> source .zshrc
# 檢查是否安裝成功
>>> nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Fri_Feb__8_19:08:17_PST_2019
Cuda compilation tools, release 10.1, V10.1.105

cuDNN:

# 解壓
>>> tar -xzvf cudnn-10.1-linux-x64-v7.6.5.32.tgz
cuda/include/cudnn.h
cuda/NVIDIA_SLA_cuDNN_Support.txt
cuda/lib64/libcudnn.so
cuda/lib64/libcudnn.so.7
cuda/lib64/libcudnn.so.7.6.5
cuda/lib64/libcudnn_static.a
# 複製
>>> sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
>>> sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
# 修改權限
>>> cd /usr/local/cuda/lib64
>>> sudo chmod +r libcudnn.so.7.6.5
>>> sudo ln -sf libcudnn.so.7.6.5 libcudnn.so.7
>>> sudo ln -sf libcudnn.so.7 libcudnn.so
>>> sudo ldconfig

參考:https://blog.csdn.net/xinjieyuan/article/details/100667803

3 安裝TensorFlow、PyTorch

3.1 安裝TensorFlow 2.1.0

>>> pip3 install tensorflow-gpu
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting tensorflow-gpu
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0a/93/c7bca39b23aae45cd2e85ad3871c81eccc63b9c5276e926511e2e5b0879d/tensorflow_gpu-2.1.0-cp36-cp36m-manylinux2010_x86_64.whl (421.8 MB)
     |████████████████████████████████| 421.8 MB 7.7 kB/s 
Requirement already satisfied: six>=1.12.0 in /home/microfat/.local/lib/python3.6/site-packages (from tensorflow-gpu) (1.14.0)
Collecting grpcio>=1.8.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/17/8f/f79c5c174bebece41f824dd7b1ba98da45dc2d4c373b38ac6a7f6a5acb5e/grpcio-1.26.0-cp36-cp36m-manylinux2010_x86_64.whl (2.4 MB)
     |████████████████████████████████| 2.4 MB 6.9 MB/s 
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /usr/lib/python3/dist-packages (from tensorflow-gpu) (0.30.0)
Requirement already satisfied: numpy<2.0,>=1.16.0 in /home/microfat/.local/lib/python3.6/site-packages (from tensorflow-gpu) (1.18.1)
Collecting google-pasta>=0.1.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c3/fd/1e86bc4837cc9a3a5faf3db9b1854aa04ad35b5f381f9648fbe81a6f94e4/google_pasta-0.1.8-py3-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 5.1 MB/s 
Collecting astor>=0.6.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c3/88/97eef84f48fa04fbd6750e62dcceafba6c63c81b7ac1420856c8dcc0a3f9/astor-0.8.1-py2.py3-none-any.whl (27 kB)
Collecting opt-einsum>=2.3.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b8/83/755bd5324777875e9dff19c2e59daec837d0378c09196634524a3d7269ac/opt_einsum-3.1.0.tar.gz (69 kB)
     |████████████████████████████████| 69 kB 4.9 MB/s 
Collecting wrapt>=1.11.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/67/b2/0f71ca90b0ade7fad27e3d20327c996c6252a2ffe88f50a95bba7434eda9/wrapt-1.11.1.tar.gz (27 kB)
Collecting keras-preprocessing>=1.1.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/6a/8c1f62c37212d9fc441a7e26736df51ce6f0e38455816445471f10da4f0a/Keras_Preprocessing-1.1.0-py2.py3-none-any.whl (41 kB)
     |████████████████████████████████| 41 kB 2.8 MB/s 
ERROR: Could not find a version that satisfies the requirement tensorboard<2.2.0,>=2.1.0 (from tensorflow-gpu) (from versions: 1.0.0a3, 1.0.0a4, 1.0.0a5, 1.0.0a6, 1.6.0rc0, 1.6.0, 1.7.0, 1.8.0, 1.9.0, 1.10.0, 1.11.0, 1.12.0, 1.12.1, 1.12.2, 1.13.0, 1.13.1, 1.14.0, 1.15.0, 2.0.0, 2.0.1, 2.0.2)
ERROR: No matching distribution found for tensorboard<2.2.0,>=2.1.0 (from tensorflow-gpu)

出現錯誤是因爲前文修改了pip的源爲清華源,而源中缺少tensorboard對應版本,因而需要改回官方源

>>> pip3 config set global.index-url https://pypi.python.org/simple
# 爲避免安裝過程中出現
ERROR: launchpadlib 1.10.6 requires testresources, which is not installed.
>>> sudo apt install python3-testresources
# 使用proxyc加速
>>> p104 pip3 install tensorflow-gpu
...
Successfully installed absl-py-0.9.0 astor-0.8.1 cachetools-4.0.0 gast-0.2.2 google-auth-1.11.0 google-auth-oauthlib-0.4.1 google-pasta-0.1.8 grpcio-1.26.0 h5py-2.10.0 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 oauthlib-3.1.0 opt-einsum-3.1.0 protobuf-3.11.2 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.22.0 requests-oauthlib-1.3.0 rsa-4.0 scipy-1.4.1 setuptools-45.1.0 tensorboard-2.1.0 tensorflow-estimator-2.1.0 tensorflow-gpu-2.1.0 termcolor-1.1.0 werkzeug-0.16.1 wrapt-1.11.2
# 測試
>>> ipython
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.11.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import tensorflow as tf                                                                                                                                                                                    
2020-01-31 21:43:18.502434: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer.so.6'; dlerror: libnvinfer.so.6: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda/lib64:/usr/local/cuda-10.1/lib64
2020-01-31 21:43:18.502499: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer_plugin.so.6'; dlerror: libnvinfer_plugin.so.6: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda/lib64:/usr/local/cuda-10.1/lib64
2020-01-31 21:43:18.502507: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:30] Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.

In [2]: tf.__version__                                                                                                                                                                                             
Out[2]: '2.1.0'

In [3]: exit

參考:https://www.discoverbits.in/864/error-launchpadlib-requires-testresources-which-installed

3.2 安裝PyTorch 1.4.0

>>> ipython
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.11.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import torch                                                                                                                                                                                               

In [2]: torch.__version__                                                                                                                                                                                          
Out[2]: '1.4.0'

In [3]: exit

3.3 安裝OpenCV 4.2.0

安裝必要的依賴:

>>> sudo apt install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
>>> sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev libavresample-dev
>>> sudo apt install libxvidcore-dev libx264-dev libx265-dev 
>>> sudo apt install libatlas-base-dev gfortran
>>> sudo apt-get --reinstall install ffmpeg
# 如果已經安裝SS,可使用下面命令加速git clone
>>> git config --global https.proxy 'socks5://127.0.0.1:1080'
>>> git config --global http.proxy 'socks5://127.0.0.1:1080'
>>> git clone https://github.com/opencv/opencv.git
>>> cd opencv
>>> git clone https://github.com/opencv/opencv_contrib.git
>>> mkdir build
>>> cd build
>>> p104 cmake \
		-D CMAKE_BUILD_TYPE=RELEASE \
		-D CMAKE_INSTALL_PREFIX=/usr/local \
		-D OPENCV_GENERATE_PKGCONFIG=ON \ # 安裝darknet時需要
		-D WITH_TBB=ON \
		-D WITH_V4L=ON \
		-D WITH_OPENGL=ON \
		-D WITH_CUDA=ON \
		-D ENABLE_FAST_MATH=1 \
		-D CUDA_FAST_MATH=1 \
		-D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" \
		-D WITH_CUBLAS=1 \
		-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
		
-- Failed to download NVIDIA_Optical_Flow_1_0 Headers
-- 
-- General configuration for OpenCV 4.2.0-dev =====================================
--   Version control:               4.2.0-216-g0dd1ce1a4f
-- 
--   Extra modules:
--     Location (extra):            /home/microfat/Git/opencv/opencv_contrib/modules
--     Version control (extra):     4.2.0-21-ge8916dae
-- 
--   Platform:
--     Timestamp:                   2020-02-04T14:21:02Z
--     Host:                        Linux 5.3.0-28-generic x86_64
--     CMake:                       3.10.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (15 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (6 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 7.4.0)
--     C++ flags (Release):         -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed  
--     Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cvv gapi hdf java js matlab ovis python2 sfm viz xfeatures2d
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  NO
--     OpenGL support:              NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        libjpeg-turbo (ver 2.0.2-62)
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        build (ver 42 - 4.0.10)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                YES (3.7.0)
--     GStreamer:                   NO
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Lapack:                      YES (/usr/lib/x86_64-linux-gnu/liblapack.so /usr/lib/x86_64-linux-gnu/libcblas.so /usr/lib/x86_64-linux-gnu/libatlas.so)
--     Eigen:                       NO
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   NVIDIA CUDA:                   YES (ver 10.1, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             30 35 37 50 52 60 61 70 75
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 7.6.5)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/microfat/Git/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.6.9)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.9)
--     numpy:                       /home/microfat/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.18.1)
--     install path:                lib/python3.6/dist-packages/cv2/python-3.6
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/microfat/Git/opencv/build
# 確認相關文件是否下載成功
>>> cd opencv/.cache
.cache
│
└───xfeatures2d
    ├───boostdesc
    │       0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i
    │       0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i
    │       202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i
    │       232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i
    │       324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i
    │       98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i
    │       e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i
    │
    └───vgg
            151805e03568c9f490a5e3a872777b75-vgg_generated_120.i
            7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i
            7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i
            e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i
>>> make -j8 # 開啓8線程,默認單線程
>>> sudo make install

參考:
https://github.com/opencv/opencv_contrib/issues/1301#issuecomment-318639639
https://blog.csdn.net/Felaim/article/details/103971397

安裝darknet

>>> git clone https://github.com/AlexeyAB/darknet.git
>>> cd darknet
>>> vim Makefile
GPU=1
CUDNN=1
CUDNN_HALF=0
OPENCV=1
AVX=1
OPENMP=1
LIBSO=1
ZED_CAMERA=0
# 從OpenCV4開始,opencv.pc文件改名爲opencv4.pc,而darknet默認找opencv.pc文件,因而會報錯
>>> make
chmod +x *.sh
g++ -std=c++11 -Iinclude/ -I3rdparty/stb/include -DOPENCV `pkg-config --cflags opencv` -DGPU -I/usr/local/cuda/include/ -DCUDNN -Wall -Wfatal-errors -Wno-unused-result -Wno-unknown-pragmas -fPIC -ffp-contract=fast -mavx -mavx2 -msse3 -msse4.1 -msse4.2 -msse4a -Ofast -DOPENCV -fopenmp -DGPU -DCUDNN -I/usr/local/cudnn/include -fPIC -c ./src/image_opencv.cpp -o obj/image_opencv.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
./src/image_opencv.cpp:15:10: fatal error: opencv2/core/version.hpp: No such file or directory
 #include <opencv2/core/version.hpp>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:159: recipe for target 'obj/image_opencv.o' failed
make: *** [obj/image_opencv.o] Error 1
# 需要將其進行重命名
>>> cd /usr/local/lib/pkgconfig
>>> cp opencv4.pc opencv.pc

測試:

# 下載測試圖片、weights
>>> mkdir test
>>> mkdir weights
>>> cd test
>>> wget https://github.com/pjreddie/darknet/blob/master/data/dog.jpg
>>> cd ..
>>> cd weights
>>> wget https://pjreddie.com/media/files/yolov3.weights
>>> cd ..
>>> ./darknet detector test ./cfg/coco.data ./cfg/yolov3.cfg ./weights/yolov3.weights
...
Total BFLOPS 100.606 
avg_outputs = 1126963 
 Allocate additional workspace_size = 52.43 MB 
Loading weights from ./weights/csresnext50-panet-spp-original-optimal_final.weights...
 seen 64, trained: 32032 K-images (500 Kilo-batches_64) 
Done! Loaded 138 layers from weights-file 
Enter Image Path: ./test/dog.jpg

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章