前言
追根溯源,pytorch來自於torch,不過torch使用小衆化的luna語言,而pytorch則是python,當然,pytorch在很多框架設計思想方面都做了更新。 我們這裏也打算用pytorch框架來訓練語義分割模型。
安裝pytorch
在使用pytorch框架前,必須先安裝。 其過程相對還是比較簡單的。我這裏的平臺是:ubuntu18.0.4+python3.6
先嚐試下面這個安裝命令:
pip3 install torch, torchvision
但是發現下載速度非常慢,很難安裝成功。後來在網上找到這個命令就很快安裝好了:
python3 -m pip install --upgrade torch torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple
安裝好,輸入下面命令來進行驗證,並打印出torch和torchvision的版本號:
後續補充:
上面安裝的是cpu版本,這樣模型訓練會非常慢。下面講解gpu版本的pytorch安裝。
先從https://download.pytorch.org/whl/cu90/torch_stable.html下載torch-1.1.0-cp36-cp36m-linux_x86_64.whl和torchvision-0.3.0-cp36-cp36m-manylinux1_x86_64.whl兩個安裝包,然後敲入命令:
pip3 uninstall torch
pip3 uninstall torchvision
pip3 install xxx/torch-1.1.0-cp36-cp36m-linux_x86_64.whl xxx/torchvision-0.3.0-cp36-cp36m-manylinux1_x86_64.whl
驗證時輸入: import torchvision可能會出現下面的錯誤:
>>> import torchvision
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
。。。 。。。
。。。 。。。
from . import functional as F
File "/home/xxx/.local/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 5, in <module>
from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
ImportError: cannot import name 'PILLOW_VERSION'
解決辦法是安裝相對更低版本的pillow,如下所示
pip3 install Pillow==6.2.2
torch和torchvision import成功後,最後再使用下面命令確認pytorch的cuda狀態是否正常
如上圖所示,如果cuda狀態正常的話,其打印值應該爲True。至此,cuda版本的pytorch安裝完畢。
其它環境依賴包的安裝
pip3 install matplotlib pillow tensorboardX tqdm
上面這個命令很簡單,應該很快就能下載和安裝完成。
此外,可能還需要安裝以下兩個包:
Cython的安裝命令:
python3 -m pip install --upgrade Cython -i https://pypi.tuna.tsinghua.edu.cn/simple
pycocotools的安裝命令如下:
pip3 install pycocotools
至此,pytorch框架以及環境依賴包的安裝 基本完成。