TensorRT系列(一)環境設定

本文章版本資訊

  • TensorRT 8.6.1

apt 安裝TensorRT的方法

https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-861/install-guide/index.html#installing-debian

利用Docker 建立TensorRT環境

docker tensorrt的版本與tensorrt版本對應表
https://docs.nvidia.com/deeplearning/frameworks/support-matrix/index.html

1
docker run --gpus all -it --rm -v local_dir:container_dir nvcr.io/nvidia/tensorrt:22.07-py3

其中local_dir換成自己電腦的某一個資料夾,他將與Docker container共用,container_dir換成container裡面的資料夾路徑

參考:
https://docs.nvidia.com/deeplearning/tensorrt/quick-start-guide/index.html#install
https://github.com/wang-xinyu/tensorrtx#tutorials

Python OpenCV圖片裁剪

OpenCV 座標

Python OpenCV 裁剪圖片

OpenCV 的蒲覑本質上就是一個 numpy array, 因此可以利用numpy array提供的方法來完成裁剪圖片的功能

1
crop_img = img[y:y+h, x:x+w] # x, y 為圖片的裁剪圖片的左上角座標,h, w 分別為裁剪圖片的高和寬

參考:
https://stackoverflow.com/a/15589825

fastapi使用自己的openapi檔

步驟

  • 參考官方離線版文件的說明
  • 寫一個route可以回傳openapi的json
  • 把openapi_url=app.openapi_url改型成
    1
    openapi_url=<回傳openapi json 的route>

ssh連線設定

非對稱式加密

  • 非對稱式加密有兩把key,一把只能加密,一把只能解密
  • 非對稱式加密public key是公開給外界,作為解密專用key,只能解密用對應的private key加密的內容
  • 非對稱式加密private key必須妥善保存,作為加密專用key,只被對應的public key解密

public key像帳號,private key像密碼

把自己的的public key提供給目標連線電腦,就像在那台帳號註冊帳號一樣

ssh會把public key存在~/.ssh/authorized_keys這個檔案裡面

  1. 利用ssh-copy-id複製public key到ssh server主機
  • linux
    1
    ssh-copy-id 192.168.122.7  # 指令為ssh-copy-id 遠端主機的ip
  • windows
    在powershell中沒有ssh-copy-id這個指令,不過我們可以用下面指令達成相同目標
    1
    type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {IP-ADDRESS-OR-FQDN} "cat >> .ssh/authorized_keys"

參考:
https://kb.iu.edu/d/aews
https://www.chrisjhart.com/Windows-10-ssh-copy-id/

更改主機名稱後RabbitMQ無法啟動

作業系統: Windows10

更改主機名稱後,會發現rabbitmq無法啟動,這時候要重新安裝Service。
注意:需要備份的資料先備份起來

  1. 利用rabbitmq提供的bat(RabbitMQ Service - remove.bat),並且以系統管理員執行
  2. 利用rabbitmq提供的bat(RabbitMQ Service - (re)install.bat),並且以系統管理員執行重新安裝service
  3. 利用rabbitmq提供的bat(RabbitMQ Service - start.bat),並且以系統管理員執行重新安裝service啟動服務
  4. 成功後會看到log資料夾出現新的log,rabbit@(新的主機名稱).log,代表成功了。

參考: https://dennymichael.net/2014/06/16/rabbitmq-change-the-hostname/

用vim作為Python IDE

vim版本:8.2

安裝套件管理員Vundle

  1. 下載Vundle plugin manager並且放到VIM套件資料夾

    1
    git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  2. 建立套件管理檔

    1
    touch ~/.vimrc
  3. 將下面指令複製到~/.vimrc檔案裡面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    set nocompatible              " required
    filetype off " required

    " set the runtime path to include Vundle and initialize
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()

    " alternatively, pass a path where Vundle should install plugins
    "call vundle#begin('~/some/path/here')

    " let Vundle manage Vundle, required
    Plugin 'gmarik/Vundle.vim'

    " add all your plugins here (note older versions of Vundle
    " used Bundle instead of Plugin)

    " ...

    " All of your Plugins must be added before the following line
    call vundle#end() " required
    filetype plugin indent on " required
  4. 安裝套件

    1. 先開啟vim
    2. 輸入:
    3. PluginInstall

操作

  • 分割畫面

    1. .vimrc 加入下面幾行
      1
      2
      3
      4
      5
      "split navigations
      nnoremap <C-J> <C-W><C-J>
      nnoremap <C-K> <C-W><C-K>
      nnoremap <C-L> <C-W><C-L>
      nnoremap <C-H> <C-W><C-H>
    2. ctrl加上vim的方向鍵(J下, K上, L右, H左)就可以移動到不同地方的split
  • Buffers

    • :ls可以看到buffer清單
    • 可以在輸入:ls後直接:b ,就可以不用記下buffer number
  • 程式碼收折

    • 在.vimrc加入下面幾行

      1
      2
      3
      4
      5
      6
      " Enable folding
      set foldmethod=indent
      set foldlevel=99

      " Enable folding with the spacebar
      nnoremap <space> za
    • 之後就可用空白鍵收折程式碼

    • 如果想要看到收折的程式碼的docstrings 可以在.vimrc加入這行

      1
      let g:SimpylFold_docstring_preview=1

參考:
vim 快捷鍵 : http://stackoverflow.com/a/5400978/1799408

https://www.openvim.com/
https://realpython.com/vim-and-python-a-match-made-in-heaven/

breakpoints : https://github.com/puremourning/vimspector

Visual Studio + CMake製作Python Extension

版本資訊
Visual Studio 2019
CMake 3.24
Python3.8.10

設定Opencv

  • 編譯OpenCV

  • 設定windows的環境變數 OpenCV_ROOT 到OpenCVConfig.cmake所在的資料夾(CMake 3.12之後的功能,文件

  • 從編譯好的OpenCVConfig.cmake我們可以看OpenCV提供了那些CMake變數給我們使用,可以參考這裡

  • 讓CMake複製dll文件

  • 設定CMake尋找Python.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
find_package(Python 3 REQUIRED 
COMPONENTS Interpreter Development.Module NumPy) # New in cmake 3.19

include_directories(${Python_INCLUDE_DIRS})
link_directories(${Python_LIBRARY_DIRS})
message(STATUS "Python Location: ${Python_INCLUDE_DIRS}")

include_directories(${Python_NumPy_INCLUDE_DIRS})
message(STATUS "NumPy Location: ${Python_NumPy_INCLUDE_DIRS}")

find_package(OpenCV REQUIRED)
if (OpenCV_FOUND)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
include_directories(${OpenCV_INCLUDE_DIRS})
link_libraries(${OpenCV_LIBRARIES})
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
else ()
message(FATAL_ERROR "Could not locate OpenCV")
endif()

原碼參考:

參考:
Python.h位置

CMake尋找Python

Windows下讓cmake找到OpenCV和Eigen

OpenCV

從(cmake 3.12)[https://cmake.org/cmake/help/v3.12/policy/CMP0074.html]之後,可以利用環境變數來告訴cmake套件的位置,在Windows下很方便,因為windows不像linux會把函式庫集中放在一起。設定方法就是利用設定環境變數

1
<PackageName>_ROOT

其中<PackageName>就是你的套件名稱,例如在這裡我們要設的環境變數就是OpenCV_ROOT,注意<PackageName>必須要和你寫在CMakeLists.txt裡面find_package(<PackageName>)大小寫都要一致

設定完後會看到cmake輸出找到的路徑

1
2
3
Environment variable OpenCV_ROOT is set to:

D:\lib\build_opencv

另外由於相容性的關係,cmake預設不會使用環境變數的OpenCV_ROOT,我們必須在CMakeLists.txt中設定打開這個功能

1
cmake_policy(SET CMP0074 NEW)

成功設定後可以看到cmake會有以下輸出

1
2
3
-- Found OpenCV: D:/lib/build_opencv (found version "4.6.0")
-- OpenCV library status:
-- version: 4.6.0

詳細寫法可以參考這個範例
https://gist.github.com/jenhaoyang/924698b31f7e3baede14286c67d6059a

Eigen3.4.0

使用find_package(Eigen3 REQUIRED)後需要get_target_property(EIGEN3_INCLUDE_DIR Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES)
,根據這個issue的寫法https://gitlab.com/libeigen/eigen/-/issues/2486


參考:
https://seanzhengw.github.io/blog/cmake/2018/04/23/cmake-find-package.html
https://cmake.org/cmake/help/v3.12/policy/CMP0074.html
https://stackoverflow.com/questions/21314893/what-is-the-default-search-path-for-find-package-in-windows-using-cmake

Ubuntu更新cmake

前一陣子在windows上編譯Nvidia triton-inference-server發現他的編譯腳本有很多方便的的操作方法,升級cmake是其中之一,注意指令中有指定ubuntu版本
cmake官方有關於apt下載的說明

1
2
3
4
5
6
7
8
9
ENV DEBIAN_FRONTEND=noninteractive
RUN apt install software-properties-common -y
RUN wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | \
gpg --dearmor - | \
tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null && \
apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main' && \
apt-get update && \
apt-get install -y --no-install-recommends \
cmake-data=3.21.1-0kitware1ubuntu20.04.1 cmake=3.21.1-0kitware1ubuntu20.04.1

參考:
https://github.com/triton-inference-server/server/blob/main/Dockerfile.QA#L65