PyTorch for Jetson Nano/TX2

来源:https://devtalk.nvidia.com/default/topic/1049071/jetson-nano/pytorch-for-jetson-nano/

Below are pre-built PyTorch 1.1 pip wheel installers for Python 2.7 and Python 3.6 on Jetson Nano, Jetson TX2, and Jetson AGX Xavier with JetPack 4.2.

note: these binaries are for ARM aarch64 architecture, so run these commands on a Jetson (not on a host PC)

#Python 2.7
wget https://nvidia.box.com/shared/static/m6vy0c7rs8t1alrt9dqf7yt1z587d1jk.whl -O torch-1.1.0a0+b457266-cp27-cp27mu-linux_aarch64.whl
pip install torch-1.1.0a0+b457266-cp27-cp27mu-linux_aarch64.whl
#Python 3.6
wget https://nvidia.box.com/shared/static/veo87trfaawj5pfwuqvhl6mzc5b55fbj.whl -O torch-1.1.0a0+b457266-cp36-cp36m-linux_aarch64.whl
pip3 install numpy torch-1.1.0a0+b457266-cp36-cp36m-linux_aarch64.whl

Verification
To verify that PyTorch has been installed correctly on your system, launch an interactive Python interpreter from terminal,
(‘python’ command for 2.7 or ‘python3’ for 3.6) and run the following:

>>> import torch
>>> print(torch.__version__)
>>> print('CUDA available: ' + str(torch.cuda.is_available()))
>>> a = torch.cuda.FloatTensor(2).zero_()
>>> print('Tensor a = ' + str(a))
>>> b = torch.randn(2).cuda()
>>> print('Tensor b = ' + str(b))
>>> c = a + b
>>> print('Tensor c = ' + str(c))

Build Instructions
Below are the steps used to build the wheels. These were compiled in a couple of hours on a Xavier for Nano, TX2, and Xavier.

Note that if you are trying to build on Nano, you will need to mount a swap file.

Max Performance

$ sudo nvpmodel -m 0
$ sudo ~/jetson_clocks.sh

Download PyTorch sources

$ git clone --recursive http://github.com/pytorch/pytorch
$ cd pytorch

Set Build Options

$ export USE_NCCL=0
$ export USE_DISTRIBUTED=0
$ export TORCH_CUDA_ARCH_LIST="5.3;6.2;7.2"

(remember to re-export these environment variables if you change terminal)

Build wheel for Python 2.7 (to pytorch/dist)

$ sudo apt-get install python-pip cmake
$ pip install -U pip

$ sudo pip install -U setuptools
$ sudo pip install -r requirements.txt

$ pip install scikit-build --user
$ pip install ninja --user

$ python setup.py bdist_wheel

Build wheel for Python 3.6 (to pytorch/dist)

$ sudo apt-get install python3-pip cmake

$ sudo pip3 install -U setuptools
$ sudo pip3 install -r requirements.txt

$ pip3 install scikit-build --user
$ pip3 install ninja --user

$ python3 setup.py bdist_wheel

Note on upgrading pip
If you get this error from pip/pip3 after upgrading pip with “pip install -U pip”:

pip
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in 
    from pip import main
ImportError
: cannot import name 'main'

You can either downgrade pip to it’s original version:

# Python 2.7
$ sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall

# Python 3.6
$ sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

-or- you can patch /usr/bin/pip (or /usr/bin/pip3)

diff --git a/pip b/pip
index 56bbb2b..62f26b9 100755
--- a/pip
+++ b/pip
@@ -6,6 +6,6 @@ import sys
 # Run the main entry point, similarly to how setuptools does it, but because
 # we didn't install the actual entry point from setup.py, don't use the
 # pkg_resources API.
-from pip import main
+from pip import __main__
 if __name__ == '__main__':
-    sys.exit(main())
+    sys.exit(__main__._main())
## start, please enjoy...
- install cuda9 and cudnn7 (from host computer)  
- login tx2
- install python3.6 and pip3  
- install dependent libraries  
- install libraries of python3.6  
- install opencv3.4.3 from source  
- install pytorch from source  
- install torchvision  


#### install cuda9&cudnn7 from your own computer (host)
chmod +x JetPack-L4T-3.3-linux-x64_b39.run # please download it before these ops  
sudo sh JetPack-L4T-3.3-linux-x64_b39.run  


#### ssh to login in your tx2 machine
sudo nvpmodel -m 0 # maximize the gpu power  
sudo /home/nvidia/jetson_clocks.sh # maximize the cpu power   
head -n 1 /etc/nv_tegra_release # see what kind of JetPack do u install  
cat /usr/include/cudnn.h | grep CUDNN_MAJOR -A 2 # check the version of cuda and cudnn  
cat /proc/cpuinfo # see cpu info  


#### install tools
cd &&  mkdir tools/  
sudo add-apt-repository universe  
sudo apt-get update  
export LC_ALL="en_US.UTF-8"  
export LC_CTYPE="en_US.UTF-8"  
sudo dpkg-reconfigure locales  
sudo apt-get -y install tmux git ssh zssh vim htop locate  
sudo apt-get install apt-transport-https ca-certificates  
sudo apt-get update  


#### install gcc-5 && g++-5 is they do not exist
sudo apt-get update -y  
sudo apt-get upgrade -y  
sudo apt-get dist-upgrade -y  
sudo apt-get install gir1.2-packagekitglib-1.0 # =1.1.0-2 ** the key **   
sudo apt-get install build-essential software-properties-common -y  
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y  
sudo apt-get update -y  
sudo apt-get install gcc-5 g++-5  
gcc --version  
g++ --version  
cd /usr/bin/ # it will install the gcc-8&g++-8  
sudo rm gcc && sudo ln -s gcc-5 gcc  
sudo rm g++ && sudo ln -s g++-5 g++  


#### install or update cmake-3.14
cmake --version # please check the version of cmake before installing it  
cd && cd tools/  
wget https://github.com/Kitware/CMake/releases/download/v3.14.1/cmake-3.14.1.tar.gz  
tar xvf cmake-3.14.1.tar.gz && cd cmake-3.14.1  
sudo ./bootstrap && sudo make && sudo make install  
cd && cmake --version  


#### install gfortran-5 and gfortran
cd /usr/lib/python3/dist-packages/  
sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so  
cd && cd tools/  
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y  
sudo apt-get update  
sudo apt-get -f install  
sudo apt-get -y install gfortran-5  
sudo apt-get -y install libblas-dev liblapack-dev libatlas-base-dev  
sudo apt-get -y install gfortran  


#### install aptitude and use it to install gfortran and scikit-build
sudo apt-get -y install aptitude  
sudo aptitude -y install gfortran-5  
sudo aptitude -y install gfortran  
sudo pip3 install scikit-build # please install pip3  


#### install python3.6
cd &&  mkdir tools/pip3 && cd tools/pip3  
sudo add-apt-repository ppa:deadsnakes/ppa -y  
sudo apt-get update  
sudo apt-get -y install python3.6 -y  
cd - && cp ~/.bashrc ~/.bashrc.bak  
echo "alias python3='/usr/bin/python3.6'" >> ~/.bashrc  
source ~/.bashrc  
python3 --version  
cd  


#### install pip3
cd && cd tools/pip3  
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate  
sudo python3.6 get-pip.py  
pip3 --version   
sudo pip3 install pip --upgrade  
cd  && pip3 --version  


#### install python3.6 dev and setuptools
cd && cd tools  
sudo apt-get -y install libexpat1=2.1.0-7 --upgrade  
sudo apt-get -y install libexpat1-dev  
sudo apt-get -y install libpython3.6-dev  
sudo apt-get -y install python3.6-dev  
sudo apt-get -y install python3-dev  
sudo apt-get -y install libhdf5-dev
sudo pip3 install -U pip setuptools  
sudo easy_install3 pip  
sudo python3.6 -m pip install -U pip  
sudo apt-get -y install python3-setuptools  
sudo apt-get -y install python3-sklearn # maybe no need  
sudo apt-get -y python3-numpy # maybe no need  
sudo apt-get -y python3-py # maybe no need  
sudo apt-get -y python3-pytest # maybe no need  


#### install jpeg-dev, libfreetype6-dev, libpng12-dev
sudo apt-get -y install libfreetype6-dev  
sudo apt-get -y install libjpeg-dev  
sudo apt-get -y install zlib1g-dev  
sudo apt-get -y install libtbb2  
sudo apt-get -y install libpng12-0=1.2.54-1ubuntu1  
sudo apt-get -y install libpng-dev  
sudo apt-get -y install libfreetype6-dev  
sudo apt-get -y install pkg-config # GTK development libraries   
sudo apt-get -y install libpng12-dev 
sudo apt-get -y install libhdf5-dev   
sudo apt-get -y install python3.6-tk # sudo apt-get -y install python3-tk


#### install libraries of python3.6
sudo pip3 install --upgrade pip  
sudo pip3 install --upgrade setuptools  
sudo pip3 install scikit-build  
sudo pip3 install numpy  
sudo python3.6 -m pip install numpy --upgrade  
sudo pip3 install pyyaml  
sudo pip3 install tqdm  
sudo easy_install packaging  
sudo pip3 install cython  
sudo pip3 install pycocotools  
sudo pip3 install yacs  
sudo easy_install-3.6 -U six  
sudo pip3 install scikit-image  
sudo pip3 install flask  
sudo pip3 install tornado  
sudo pip3 install pandas  
sudo pip3 install xlwt    
sudo pip3 install h5py  
sudo pip3 install scipy matplotlib ninja  


#### install scipy from source if pip3 can not install it
cd && cd tools/  
git clone https://github.com/scipy/scipy.git  
cd scipy  
git clean -xdf  
sudo python3.6 setup.py install --user  
cd - && python3.6 -c "import scipy; print(scipy.__version__)"   


### install matplotlib from source if pip3 can not install it
cd && cd tools/  
git clone git://github.com/matplotlib/matplotlib.git  
cd matplotlib/   
sudo apt-get build-dep python-matplotlib
cd - && python3.6 -c "import matplotlib; print(matplotlib.__version__)"  


#### install cffi
cd && cd tools/  
sudo apt-get -y install libffi-dev  
sudo pip3 install cffi  


#### install ninja from source if pip3 can not install it
cd && cd tools/  
git clone https://github.com/scikit-build/ninja-python-distributions.git  
cd ninja-python-distributions  
sudo python3.6 setup.py install # will raise an error that some package can't download and then  
cd _skbuild/linux-aarch64-3.6/cmake-build  
sudo rm v1.9.0.g5b44b.kitware.dyndep-1.jobserver-1.tar.gz  
sudo wget https://github.com/kitware/ninja/archive/v1.9.0.g5b44b.kitware.dyndep-1.jobserver-1.tar.gz --no-check-certificate  
cd -  # keep install  
sudo python3.6 setup.py install  
cd && cd tools/  
python3.6 -c "import ninja;print(ninja.__version__)"  


#### prepare libraries for opencv3.4.3
sudo apt-get install libavcodec-dev  
sudo apt-get install libavformat-dev  
sudo apt-get install libavutil-dev  
sudo apt-get install libswscale-dev  
sudo apt-get install libglew-dev  
sudo apt-get install libtiff5-dev  
sudo apt-get install libpostproc-dev  
sudo apt-get install libtbb-dev  
sudo apt-get install libv4l-dev  
sudo apt-get install v4l2ucp   
sudo apt-get install v4l-utils  
sudo apt-get install libeigen3-dev  
sudo apt-get install libglew1.6-dev  
sudo apt-get install ffmpeg 
sudo apt-get install libcairo2-dev  
sudo apt-get install libharfbuzz-dev  
sudo apt-get install libpango1.0-dev  
sudo apt-get install libgtk2.0-dev  
sudo apt-get install libjasper-dev  
sudo apt-get install libtiff-dev  
sudo apt-get install libdc1394-22-dev  
sudo apt-get install libgtkglext1  
sudo apt-get install libgtkglext1-dev  
sudo apt-get install libgstreamer1.0-dev  
sudo apt-get install libgstreamer-plugins-base1.0-dev  
sudo apt-get install libwayland-dev  
sudo apt-get install libegl1-mesa-dev  
sudo apt-get install libgles2-mesa-dev 
sudo apt-get install qtbase5-dev  
sudo apt-get install libxvidcore-dev  
sudo apt-get install libx264-dev  
sudo apt-get install qt5-default  
sudo apt-get install libdbus-1-dev  
sudo apt-get install libatspi2.0-dev  
sudo apt-get install libatk-bridge2.0-dev   
sudo vim /usr/local/cuda/include/cuda_gl_interop.h # comment line 62-68, exclude this line '#include ', see 'https://jkjung-avt.github.io/opencv3-on-tx2/' for more details  
cd /usr/lib/aarch64-linux-gnu/  
sudo ln -sf tegra/libGL.so libGL.so # fix the symbolic link of libGL.so  


#### install opencv-3.4.3
cd && cd tools/  
git clone https://github.com/opencv/opencv.git  
git clone https://github.com/opencv/opencv_extra.git # if u don't need it, please comment it  
cd opencv/  
git checkout 3.4.3  
vim cmake/OpenCVDetectPython.cmake # remove 'find_python(2.7 "${MIN_VER_PYTHON2}"' these lines and change 'find_python(3.4 "${MIN_VER_PYTHON3}"' to 'find_python(3.6 "${MIN_VER_PYTHON3}"', so that it can find python3.6  
sudo rm build/ -rf  
mkdir build/ && cd build/  
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
           -D PYTHON_EXECUTABLE=/usr/bin/python3.6 \
           -D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python3.6/dist-packages \
           -D BUILD_OPENCV_PYTHON3=YES \
           -D CMAKE_INSTALL_PREFIX=/usr/local \
           -D WITH_CUDA=ON \
           -D CUDA_ARCH_BIN="6.2" \
           -D CUDA_ARCH_PTX="" \
           -D ENABLE_FAST_MATH=ON \
           -D CUDA_FAST_MATH=ON \
           -D WITH_CUBLAS=ON \
           -D WITH_LIBV4L=ON \
           -D WITH_GSTREAMER=ON \
           -D WITH_GSTREAMER_0_10=OFF \
           -D ENABLE_NEON=ON \
           -D BUILD_TESTS=OFF \
           -D BUILD_PERF_TESTS=OFF \
           -D BUILD_EXAMPLES=OFF \
           -D WITH_QT=ON \
           -D WITH_OPENGL=ON ..
sudo make -j4  
sudo make install  
cd ../.. 
locate cv2.so  
python3.6 -c "import cv2; print(cv2.__version__)"  


#### before installing pytorch, add env path for cudnn and python3.6
echo "export CUDNN_LIB_DIR=/usr/lib/aarch64-linux-gnu" >> ~/.bashrc  
echo "export CUDNN_INCLUDE_DIR=/usr/include" >> ~/.bashrc  
echo "PYTHONPATH=/usr/local/lib/python3.6/dist-packages" >> ~/.bashrc  
source ~/.bashrc  
echo $PYTHONPATH  


#### git clone pytorch, and do these modification
cd && cd tools/  
git clone http://github.com/pytorch/pytorch # see https://github.com/Bfzanchetta/DLARM/blob/master/jetsontx2/pytorch_install.sh for more information    
cd pytorch  
vim CMakeLists.txt # CmakeLists.txt : Change NCCL to 'Off' on line 100 or line 98  
vim tools/setup_helpers/nccl.py # nccl.py : Change 'USE_SYSTEM_NCCL' to 'False' on line 10 and  Add 'USE_NCCL = False' on line 9  
vim torch/csrc/cuda/nccl.h # nccl.h : Comment self-include on line 8 and Comment entire code from line 21 to 28  
vim torch/csrc/distributed/c10d/ddp.cpp # ddp.cpp : Comment nccl.h include on line 6 and Comment torch::cuda::nccl::reduce on line 182  


#### install pytorch
cd && sudo ./jetson_clocks.sh && sudo nvpmodel -m 0 && cd -    
git tag && git checkout v1.0.1 # if u want to install v1.0.1  
sudo pip3 install setuptools --upgrade  
sudo pip3 install -r requirements.txt  
git submodule update --init --recursive    
sudo python3.6 setup.py build_deps # old version: u can comment it  
sudo python3.6 setup.py build develop  
cd  


#### install torchvision
sudo pip3 --no-cache-dir install torchvision  


#### build maskrcnn-benchmark
cd && mkdir repos/ && cd repos/  
git clone https://github.com/facebookresearch/maskrcnn-benchmark.git  
cd maskrcnn-benchmark/  
sudo python3.6 setup.py build develop  

你可能感兴趣的:(Tegra,pytorch技巧,nano,tx2,pytorch)