How to install YoloV5 on Jetson AGX Orin

Since I have been struggling a bit trying to install YoloV5 on Jetson AGX Orin I want to share my procedure.

1: Install Jetpack and Flash the Orin using a Linux computer connected to the Jetson Orin via USB-C cable to the back interface. Not where the Network output is.
2: When prompted during the installation to flash, go with manual installation. NOT Auto.
3: Once the installation is done you should verify you dont have pytorch, torchvision etc installed.
Also check that gstreamer works.
4: I would suggest installing python virtual environment too, but its your call…

python3 <enter> (Start python3)

import cv2
cv2.__version__
import torch
print("PyTorch: "+torch.__version__)
import torchvision
print("PyTorch: "+torchvision.__version__)

If this show that you have 3.8.xx you are set to go. To install pytorch you need to have the latest build.

Check Gstreamer camera (dont have too unless you dont have one) Note that the pipeline I use is for
a 2Mpx USB3 camera from Econ-systems. This first code below will only show you “Messup” in the terminal if you cant connect to the camera with the pipeline.

import cv2
G_STREAM_TO_SCREEN = "v4l2src device=/dev/video0 ! video/x-raw , width=(int)1600 , height=(int)1300 , format=(string)GRAY8 ! nvvidconv ! video/x-raw(memory:NVMM), width=(int)1600, height=(int)1300, format=I420 ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR, width=(int)800 , height=(int)650 ! videoconvert ! appsink"
cam = cv2.VideoCapture(G_STREAM_TO_SCREEN, cv2.CAP_GSTREAMER)

if(cam.isOpened()): # True
    print("Open")
else:
    print("Messup")

Check Gstreamer without camera, test-source: (This will result in “Unable to use pipline”)

import sys
import cv2

def read_cam():
    G_STREAM_TO_SCREEN = "videotestsrc num-buffers=50 ! videoconvert ! appsink"
    cap = cv2.VideoCapture(G_STREAM_TO_SCREEN, cv2.CAP_GSTREAMER)
    if cap.isOpened():
        cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
        while True:
            ret_val, img = cap.read()
            cv2.imshow('demo',img)
            cv2.waitKey(1)
    else:
     print ("Unable to use pipline")

    cv2.destroyAllWindows()


if __name__ == '__main__':
    read_cam()

Install pytorch
Select the latest wheel (or use the next section (wget) to download the latest version (as of writing)

Install pytorch as follows. NOTE: The wget fetches the latest dev version (which is working at the moment)

wget https://developer.download.nvidia.com/compute/redist/jp/v50/pytorch/torch-1.12.0a0+2c916ef.nv22.3-cp38-cp38-linux_aarch64.whl -O torch-1.12.0a0+2c916ef.nv22.3-cp38-cp38-linux_aarch64.whl
sudo apt-get install python3-pip libopenblas-base libopenmpi-dev libomp-dev
pip3 install Cython
pip3 install numpy torch-1.12.0a0+2c916ef.nv22.3-cp38-cp38-linux_aarch64.whl

After this you have pytorch installed. Next up is torchvision:

git clone GitHub - pytorch/vision: Datasets, Transforms and Models specific to Computer Vision
cd vision
sudo python setup.py install

This installs the latest version of torchvision.
Now you should be able to see the versions of it.

python3 <enter> (Start python3)

import cv2
cv2.__version__
import torch
print("PyTorch: "+torch.__version__)
import torchvision
print("PyTorch: "+torchvision.__version__)

I hope you now have pytorch 0.12 something and torchvision 0.14…

The next thing is to clone repository from ultralyctics. That works great and works pretty much straight forward. BUT be ware that not all requirements shall be installed. Need to modify the installation packages.

Create a folder in your home dir that makes sence, like mkdir project

git clone https://github.com/ultralytics/yolov5
cd yolov5

Open the requirements.txt and modify the content as follows. (We remove things like wrong version of protobuf and pytorch etc.)

# YOLOv5 requirements
# Usage: pip install -r requirements.txt

# Base ----------------------------------------
matplotlib>=3.2.2
numpy>=1.18.5
Pillow>=7.1.2
PyYAML>=5.3.1
requests>=2.23.0
scipy>=1.4.1  # Google Colab version
tqdm>=4.41.0
pip3 install --upgrade protobuf==3.19.4

# Logging -------------------------------------
tensorboard>=2.4.1

# Plotting ------------------------------------
pandas>=1.1.4
seaborn>=0.11.0

# Extra ------------------------------------
ipython  # interactive notebook
psutil  # system utilization

I got error messages of one installation of gprcio. But I think it was installed at the end after some dependency fixes.

So notice the protobuf. This is a quite important version. If you change it or upgrade you will get errors. Its not the latest and I never managed to (or got tiered of trying) to get it working. So 3.19.4 is ok.

Now you should have all needed to run the test program.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5n - yolov5x6, custom

# Images
img = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, PIL, OpenCV, numpy, list

# Inference
results = model(img)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

Thats it.

Welcome to YoloV5 and AGX Orin.

Next steps are to create multitasking :D

1 Like

Thanks a lot for the sharing.
Good to know YoloV5 is working on Orin as well.

1 Like

Thanks for the sharing. Will it work on jetpack5.0.1? I just find torch1.13,but I did’t a match torchvision.

Hi,
The installation I did was on 5.0.1 and you need to install torch as I described. Do NOT run pip install Pytorch, but build it rather from the git. Then you get pytorch one working on Jetson. the torchvision should come from the git download as describe above. Dont look at the table on github with version-matching. Does not list the latest in development. So this is a prebuild.

wget https://developer.download.nvidia.com/compute/redist/jp/v50/pytorch/torch-1.12.0a0+2c916ef.nv22.3-cp38-cp38-linux_aarch64.whl -O torch-1.12.0a0+2c916ef.nv22.3-cp38-cp38-linux_aarch64.whl
sudo apt-get install python3-pip libopenblas-base libopenmpi-dev libomp-dev
pip3 install Cython
pip3 install numpy torch-1.12.0a0+2c916ef.nv22.3-cp38-cp38-linux_aarch64.whl
1 Like

Thank you very much, I have successfully run YOLOv5 in orin

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.