2023년 3월 9일 목요일

Xavier NX (JetPack 5.1) - Effective Development environment using Anaconda

 Unlike Jetson Nano, Xavier NX can install SSD. This allows for much larger storage than the Jetson Nano.

When using the Jetson Nano, I made and used various SD cards depending on the purpose. However, in Xavier NX, you can create a development environment using ample SSD storage without replacing the SD card.

The development language will probably use Python and C/C++ heavily. The most difficult part of building a development environment in Xavier NX is the Python development environment. Many machine learning software use the Python language. And it requires a specific Python version, Numpy version. Therefore, if you install two or more machine learning software on one system, the version conflict problem of Python and Python modules occurs. The following figure summarizes the TensorFlow version and required Python, compiler, and CUDA versions. You may need more than one version of Python if you want to use more than one TensorFlow.

<tensorflow and required python version from https://www.tensorflow.org/install/source#tested_build_configurations>


To solve this problem, we use Python virtualization. There are two main ways to use Python virtualization. First of all, you can create a virtual environment using venv, a built-in module of Python. And the other way is to install and use Anaconda. However, using venv has one drawback. venv can manage the versions of modules that are installed by creating multiple virtual environments, but cannot change the Python version.

However, Anaconda has the advantage of specifying the desired Python version while creating a virtual environment. Therefore, you can create a virtual environment much more flexibly than venv. Therefore, many Python developers prefer Anaconda over Python's built-in venv.

<Python built-in venv vs anaconda>

Therefore, I will create a virtual environment using Anaconda. And we will install software such as PyTorch, Tensorflow, and YOLO in this virtual environment.


Anaconda

Anaconda is a conditional free and open source software name that facilitates package/dependency management and distribution of Python and R languages, suitable for scientific research and machine learning fields.

It was originally free, but starting in 2020, it is free only for individual users, universities, non-profit organizations, and small and medium-sized enterprises (SMEs) with less than 200 employees, and has been changed to a fee for the government and companies with 200 or more employees.

Anaconda includes most of the packages suitable for scientific research and machine learning applications. Therefore, there is a drawback that the size is quite large. Miniconda is a solution to this problem. Unlike anaconda, only the minimum required for operation is provided, and the user has to find and install the necessary packages himself. However, for this reason, the file is smaller and lighter than Anaconda.

Miniconda is a free minimal installer for conda. It is a small, bootstrap version of Anaconda that includes only conda, Python, the packages they depend on, and a small number of other useful packages, including pip, zlib and a few others. Use the conda install command to install 720+ additional conda packages from the Anaconda repository.

I'm going to install Anaconda on the Xavier NX. Because I am using an SSD with sufficient capacity.


Installing Anaconda on Xavier NX

There are instructions for installing Anaconda at https://www.anaconda.com/products/individual#Downloads


<Anaconda installation script download page>


Install Anaconda

After downloading the installation script, give permission for execution and install it.

wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-aarch64.sh
chmod 755 Anaconda3-2022.10-Linux-aarch64.sh
./Anaconda3-2022.10-Linux-aarch64.sh

In the previous article, I installed the SSD while installing JetPack 5.1. And I mounted /home/user (spypiggy) on the SSD. Therefore, installing all possible software into /home/spypiggy is a good way to utilize SSD storage.

When you run the Anaconda3-2022.10-Linux-aarch64.sh script, it asks for the installation path as follows. You must select the mounted path to obtain the effect of installing the SSD.

Do you accept the license terms? [yes|no]
[no] >>> yes

Anaconda3 will now be installed into this location:
/home/spypiggy/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/home/spypiggy/anaconda3] >>>
PREFIX=/home/spypiggy/anaconda3
Unpacking payload ...


When Anaconda is installed, the following content is added to the ~/.bashrc file. So, whenever you open a new shell in the future, these lines will be executed.


# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/spypiggy/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/spypiggy/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/spypiggy/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/spypiggy/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<


And in the new shell prompt, (base) is added as follows:

(base) spypiggy@spypiggy-desktop:~$

If you do not want to initialize anaconda automatically, you can delete the conda initialization part from your ~/.bashrc file. And only one line related to the following path is left in the ~/.bashrc file.

# >>> conda initialize >>>
export PATH="/home/spypiggy/anaconda3/bin:$PATH"
# <<< conda initialize <<<


Creating a new conda virtual environment

JetPack 5.1 comes with Python 3.8 installed .  However, in Anaconda, you can choose from a variety of Python versions, such as:

(base) spypiggy@spypiggy-NX:/usr/local$ conda search python
Loading channels: done
# Name                       Version           Build  Channel
python                        3.7.10      he65049a_0  pkgs/main
python                        3.7.10      he65049a_1  pkgs/main
python                        3.7.10      he65049a_3  pkgs/main
python                        3.7.11      hc137634_0  pkgs/main
python                        3.7.13      h6c906d0_1  pkgs/main
python                        3.7.13      hc137634_0  pkgs/main
python                        3.7.15      h6c906d0_0  pkgs/main
python                        3.7.15      h89984f6_1  pkgs/main
python                        3.7.16      h89984f6_0  pkgs/main
python                         3.8.8      he65049a_4  pkgs/main
python                         3.8.8      he65049a_5  pkgs/main
python                         3.8.8      he65049a_7  pkgs/main
python                        3.8.11      hc137634_0  pkgs/main
python                        3.8.11      hc137634_1  pkgs/main
python                        3.8.12      hc137634_0  pkgs/main
python                        3.8.13      h6c906d0_1  pkgs/main
python                        3.8.13      hc137634_0  pkgs/main
python                        3.8.15      h6c906d0_0  pkgs/main
python                        3.8.15      h89984f6_2  pkgs/main
python                        3.8.16      h89984f6_2  pkgs/main
python                        3.8.16      h89984f6_3  pkgs/main
python                         3.9.1      he65049a_6  pkgs/main
python                         3.9.1      he65049a_7  pkgs/main
python                         3.9.4      he65049a_0  pkgs/main
python                         3.9.4      he65049a_2  pkgs/main
python                         3.9.5      hc137634_3  pkgs/main
python                         3.9.6      hc137634_0  pkgs/main
python                         3.9.6      hc137634_1  pkgs/main
python                         3.9.7      hc137634_1  pkgs/main
python                        3.9.11      hc137634_1  pkgs/main
python                        3.9.11      hc137634_2  pkgs/main
python                        3.9.12      hc137634_0  pkgs/main
python                        3.9.12      hc137634_1  pkgs/main
python                        3.9.13      h6c906d0_1  pkgs/main
python                        3.9.13      h6c906d0_2  pkgs/main
python                        3.9.15      h6c906d0_0  pkgs/main
python                        3.9.15      h89984f6_2  pkgs/main
python                        3.9.16      h89984f6_0  pkgs/main
python                        3.9.16      h89984f6_1  pkgs/main
python                        3.9.16      h89984f6_2  pkgs/main
python                        3.10.0      hc137634_0  pkgs/main
python                        3.10.0      hc137634_2  pkgs/main
python                        3.10.0      hc137634_3  pkgs/main
python                        3.10.0      hc137634_4  pkgs/main
python                        3.10.0      hc137634_5  pkgs/main
python                        3.10.3      hc137634_5  pkgs/main
python                        3.10.4      hc137634_0  pkgs/main
python                        3.10.6      h6c906d0_0  pkgs/main
python                        3.10.6      h6c906d0_1  pkgs/main
python                        3.10.8      h6c906d0_0  pkgs/main
python                        3.10.8      h89984f6_1  pkgs/main
python                        3.10.9      h89984f6_0  pkgs/main
python                        3.10.9      h89984f6_1  pkgs/main
python                        3.10.9      h89984f6_2  pkgs/main
python                        3.11.0      h89984f6_2  pkgs/main
python                        3.11.0      h89984f6_3  pkgs/main


I will install Python 3.8 which is the same as Python installed in JetPack 5.1. Only minor versions will be upgraded from 3.8.10 to 3.8.16.

spypiggy@spypiggy-NX:~/Downloads$ conda create -n yolov8 python=3.8.16
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/spypiggy/anaconda3/envs/yolov8

  added / updated specs:
    - python=3.8.16


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2022.12.7          |   py38hd43f75c_0         149 KB
    libffi-3.4.2               |       h419075a_6         135 KB
    pip-23.0.1                 |   py38hd43f75c_0         2.5 MB
    python-3.8.16              |       h89984f6_3        12.5 MB
    setuptools-65.6.3          |   py38hd43f75c_0         1.1 MB
    wheel-0.38.4               |   py38hd43f75c_0          63 KB
    ------------------------------------------------------------
                                           Total:        16.5 MB

The following NEW packages will be INSTALLED:

  _libgcc_mutex      pkgs/main/linux-aarch64::_libgcc_mutex-0.1-main None
  _openmp_mutex      pkgs/main/linux-aarch64::_openmp_mutex-5.1-51_gnu None
  ca-certificates    pkgs/main/linux-aarch64::ca-certificates-2023.01.10-hd43f75c_0 None
  certifi            pkgs/main/linux-aarch64::certifi-2022.12.7-py38hd43f75c_0 None
  ld_impl_linux-aar~ pkgs/main/linux-aarch64::ld_impl_linux-aarch64-2.38-h8131f2d_1 None
  libffi             pkgs/main/linux-aarch64::libffi-3.4.2-h419075a_6 None
  libgcc-ng          pkgs/main/linux-aarch64::libgcc-ng-11.2.0-h1234567_1 None
  libgomp            pkgs/main/linux-aarch64::libgomp-11.2.0-h1234567_1 None
  libstdcxx-ng       pkgs/main/linux-aarch64::libstdcxx-ng-11.2.0-h1234567_1 None
  ncurses            pkgs/main/linux-aarch64::ncurses-6.4-h419075a_0 None
  openssl            pkgs/main/linux-aarch64::openssl-1.1.1t-h2f4d8fa_0 None
  pip                pkgs/main/linux-aarch64::pip-23.0.1-py38hd43f75c_0 None
  python             pkgs/main/linux-aarch64::python-3.8.16-h89984f6_3 None
  readline           pkgs/main/linux-aarch64::readline-8.2-h998d150_0 None
  setuptools         pkgs/main/linux-aarch64::setuptools-65.6.3-py38hd43f75c_0 None
  sqlite             pkgs/main/linux-aarch64::sqlite-3.40.1-hf533066_0 None
  tk                 pkgs/main/linux-aarch64::tk-8.6.12-h241ca14_0 None
  wheel              pkgs/main/linux-aarch64::wheel-0.38.4-py38hd43f75c_0 None
  xz                 pkgs/main/linux-aarch64::xz-5.2.10-h998d150_1 None
  zlib               pkgs/main/linux-aarch64::zlib-1.2.13-h998d150_0 None


Proceed ([y]/n)? y


Downloading and Extracting Packages
wheel-0.38.4         | 63 KB     | ############################################################################## | 100%
libffi-3.4.2         | 135 KB    | ############################################################################## | 100%
certifi-2022.12.7    | 149 KB    | ############################################################################## | 100%
pip-23.0.1           | 2.5 MB    | ############################################################################## | 100%
setuptools-65.6.3    | 1.1 MB    | ############################################################################## | 100%
python-3.8.16        | 12.5 MB   | ############################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate yolov8
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Retrieving notices: ...working... done

You can see that packages such as openssl, ncurses, pip and sqlite are installed together with Python 3.9.7. Packages included in Anaconda can be added with the conda install command, and packages not included can be added with the pip install command.

You can check the newly created anaconda virtual environment with the conda env list command.

spypiggy@spypiggy-desktop:~$ conda env list
# conda environments:
#
base                  *  /home/spypiggy/anaconda3
yolov8                   /home/spypiggy/anaconda3/envs/yolov8


Wrapping Up

Machine learning packages each require their own version of Python and packages. When multiple packages are run on one Xavier NX, package version problems inevitably occur. The best way to solve this problem is to use Anaconda . Anaconda has the advantage of specifying the Python version at the time of creating a virtual environment, so various Python versions can be run on a single host. In the next article, I will installPyTorch and yolov8 in the Anaconda environment.















댓글 없음:

댓글 쓰기