Thursday, September 14, 2023

NVIDIA Jetson YOLOv8 Object Detection

NVIDIA Jetson YOLOv8 Object Detection 







YOLOv8 from Ultralytics

Ultralytics YOLOv8 is a cutting-edge, state-of-the-art (SOTA) model that builds upon the success of previous YOLO versions and introduces new features and improvements to further boost performance and flexibility. YOLOv8 is designed to be fast, accurate, and easy to use, making it an excellent choice for a wide range of object detection and tracking, instance segmentation, image classification and pose estimation tasks

YOLO: A Brief History

YOLO (You Only Look Once), a popular object detection and image segmentation model, was developed by Joseph Redmon and Ali Farhadi at the University of Washington. Launched in 2015, YOLO quickly gained popularity for its high speed and accuracy.

  • YOLOv2, released in 2016, improved the original model by incorporating batch normalization, anchor boxes, and dimension clusters.
  • YOLOv3, launched in 2018, further enhanced the model's performance using a more efficient backbone network, multiple anchors and spatial pyramid pooling.
  • YOLOv4 was released in 2020, introducing innovations like Mosaic data augmentation, a new anchor-free detection head, and a new loss function.
  • YOLOv5 further improved the model's performance and added new features such as hyperparameter optimization, integrated experiment tracking and automatic export to popular export formats.
  • YOLOv6 was open-sourced by Meituan in 2022 and is in use in many of the company's autonomous delivery robots.
  • YOLOv7 added additional tasks such as pose estimation on the COCO keypoints dataset.
  • YOLOv8 is the latest version of YOLO by Ultralytics. As a cutting-edge, state-of-the-art (SOTA) model, YOLOv8 builds on the success of previous versions, introducing new features and improvements for enhanced performance, flexibility, and efficiency. YOLOv8 supports a full range of vision AI tasks, including detectionsegmentationpose estimationtracking, and classification. This versatility allows users to leverage YOLOv8's capabilities across diverse applications and domains.

Models

YOLOv8 DetectSegment and Pose models pretrained on the COCO dataset are available here, as well as YOLOv8 Classify models pretrained on the ImageNet dataset. Track mode is available for all Detect, Segment and Pose models.

All Models download automatically from the latest Ultralytics release on first use.














YOLOv8 on NVIDIA Jetson Nano



Test on NVIDIA Jetson Nano

Detect Image with Pytorch Model = 108.3 ms
Detect Image with TensorRt Model = 75.8 ms








Install Torch and TorchVision

uninstall old version first. ( if you have )

jetson@nano:~$ sudo pip3 uninstall torch torchvision scipy pandas urllib3 -y


jetson@nano:~$ sudo pip3 install scipy pandas urllib3



For Torch

Wheel Method

# download the wheel

$ gdown https://drive.google.com/uc?id=1TqC6_2cwqiYacjoLhLgrZoap6-sVL2sd


# install PyTorch 1.10.0

$ sudo -H pip3 install torch-1.10.0a0+git36449ea-cp36-cp36m-linux_aarch64.whl


From Source

jetson@nano:~$ sudo apt-get install -y libopenblas-base libopenmpi-dev


jetson@nano:~$ wget https://nvidia.box.com/shared/static/fjtbno0vpo676a25cgvuqc1wty0fkkg6.whl -O torch-1.10.0-cp36-cp36m-linux_aarch64.whl


jetson@nano:~$ sudo -H pip3 install torch-1.10.0-cp36-cp36m-linux_aarch64.whl




For TorchVision

# download TorchVision 0.11.0

$ gdown https://drive.google.com/uc?id=1C7y6VSIBkmL2RQnVy8xF9cAnrrpJiJ-K

# install TorchVision 0.11.0

$ sudo -H pip3 install torchvision-0.11.0a0+fa347eb-cp36-cp36m-linux_aarch64.whl



jetson@nano:~$ sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev


jetson@nano:~$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev


jetson@nano:~$ gdown https://drive.google.com/uc?id=1C7y6VSIBkmL2RQnVy8xF9cAnrrpJiJ-K


jetson@nano:~$ sudo -H pip3 install torchvision-0.11.0a0+fa347eb-cp36-cp36m-linux_aarch64.whl


*** if not work for you try install from source

From Scratch

sudo apt install -y libjpeg-dev zlib1g-dev

git clone --branch v0.11.1 https://github.com/pytorch/vision torchvision

cd torchvision

sudo python3 setup.py install



Test your Torch Library

jetson@nano:~$ python3

Python 3.6.9 (default, Mar 15 2022, 13:55:28) 

[GCC 8.4.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import torch

>>> torch.cuda.is_available()

True

>>> 










Install ultralytics from Source

git clone https://github.com/amphancm/ultralytics.git


jetson@nano:~$ git clone https://github.com/amphancm/ultralytics.git


jetson@nano:~$ cd ultralytics/


jetson@nano:~/ultralytics$ sudo python3 setup.py install



Using /usr/local/lib/python3.6/dist-packages

Finished processing dependencies for ultralytics==8.0.51


We use ultralytics 8.0.51 version


Run Object Detection with Pytorch Model ( model.pt )



jetson@nano:~$ mkdir YOLOv8


jetson@nano:~/YOLOv8$ yolo detect predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg' show=True


Downloading https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt to yolov8n.pt...

100%|███████████████████████████████████████| 6.23M/6.23M [00:00<00:00, 10.1MB/s]

Unable to init server: Could not connect: Connection refused

WARNING ⚠️ Environment does not support cv2.imshow() or PIL Image.show()

OpenCV(4.6.0) /home/jetson/opencv/modules/highgui/src/window_gtk.cpp:635: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Ultralytics YOLOv8.0.51 🚀 Python-3.6.9 torch-1.10.0 CUDA:0 (NVIDIA Tegra X1, 3963MiB)

YOLOv8n summary (fused): 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs

Downloading https://ultralytics.com/images/bus.jpg to bus.jpg...

100%|█████████████████████████████████████████| 476k/476k [00:00<00:00, 16.1MB/s]

image 1/1 /home/jetson/YOLOv8/bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 89.7ms

Speed: 25.0ms preprocess, 89.7ms inference, 273.7ms postprocess per image at shape (1, 3, 640, 640)

jetson@nano:~/YOLOv8



Convert Model to TensorRT ( Engine file ) for better performance





Convert PT (pytorch) to engine (TensorRT)


yolo export model=yolov8n.pt format=engine half=True device=0


jetson@nano:~$ yolo export model=yolov8n.pt format=engine half=True device=0



TensorRT: export success ✅ 350.0s, saved as yolov8n.engine (12.8 MB)

Export complete (374.0s)

Results saved to /home/jetson/YOLOv8

Predict:         yolo predict task=detect model=yolov8n.engine imgsz=640 

Validate:        yolo val task=detect model=yolov8n.engine imgsz=640 data=coco.yaml



89.7ms Speed : 25.0ms preprocess, 89.7ms inference, 273.7ms

postprocess per image at shape (1, 3, 640, 640)





Now You RUN with Engine Model file




jetson@nano:~$ yolo detect predict model=yolov8n.engine source=’bus.jpg’ show=True




Compare Model PT and Engine

On NVDIA Jetson Nano
Detect Image with Pytorch Model = 108.3 ms
Detect Image with TensorRt Model = 75.8 ms















On NVDIA Jetson Xavier NX

Detect Image with Pytorch Model = 204.6 ms
Detect Image with TensorRt Model = 49.9 ms



Reference









Adun Nantakaew อดุลย์ นันทะแก้ว 081-6452400
LINE : adunnan





Tuesday, August 23, 2022

Train YOLOv4 CrowdHuman on Colab for NVIDIA Jetson

Train YOLOv4 CrowdHuman on Colab for NVIDIA Jetson

For doing training on Google Colab, I use a "416x416" yolov4 model as example. I have put all data processing and training commands into an IPython Notebook. So training the "yolov4-crowdhuman-416x416" model on Google Colab is just as simple as: 

(1) opening the Notebook on Google Colab, 

(2) mount your Google Drive, 

(3) run all cells in the Notebook.

CrowdHuman is a benchmark dataset to better evaluate detectors in crowd scenarios. The CrowdHuman dataset is large, rich-annotated and contains high diversity. CrowdHuman contains 15000, 4370 and 5000 images for training, validation, and testing, respectively. There are a total of 470K human instances from train and validation subsets and 23 persons per image, with various kinds of occlusions in the dataset. Each human instance is annotated with a head bounding-box, human visible-region bounding-box and human full-body bounding-box. We hope our dataset will serve as a solid baseline and help promote future research in human detection tasks.

We've 2 choices for Training Model.

  1. Darknet method
  2. Pytorch method


For DarkNet Method and Source Code

https://github.com/jkjung-avt/yolov4_crowdhuman


Deploying onto Jetson

To deploy the trained "yolov4-crowdhuman-416x416" model onto Jsetson Nano, I'd use my jkjung-avt/tensorrt_demos code to build/deploy it as a TensorRT engine. Here are the detailed steps:


Download the "yolov4-crowdhuman-416x416" model. More specifically, get "yolov4-crowdhuman-416x416.cfg" from this repository and download "yolov4-crowdhuman-416x416_best.weights" file from your Google Drive. Rename the .weights file so that it matches the .cfg file.

$ cd ${HOME}/project/tensorrt_demos/yolo
$ wget https://raw.githubusercontent.com/jkjung-avt/yolov4_crowdhuman/master/cfg/yolov4-crowdhuman-416x416.cfg


Then build the TensorRT (FP16) engine. Note the "-c 2" in the command-line option is for specifying that the model is for detecting 2 classes of objects.

$ python3 yolo_to_onnx.py -c 2 -m yolov4-crowdhuman-416x416
$ python3 onnx_to_tensorrt.py -c 2 -m yolov4-crowdhuman-416x416

Test the TensorRT engine. 

$ python3 trt_yolo.py -c 2 -m yolov4-crowdhuman-416-416 \
--image /home/jetson/Pictures/your_image.jpg

Part 2 Deploy Yolov4 CrowdHuman to NVIDIA Jetson

 


Run YOLOv4 CrowdHuman 416x416 on NVIDIA Jetson Nano


Run YOLOv4 CrowdHuman 416x416 on NVIDIA Jetson Xavier NX


Compare Performance 



Run YOLOv4-tiny CrowdHuman 416x416 on NVIDIA Jetson Nano


Performance 11-13 FPS





Compare YOLOv4 vs. YOLOv4-tiny on NVIDIA Jetson Nano


Reference

CrownHuman Dataset https://www.crowdhuman.org/

DarkNet YOLO https://github.com/AlexeyAB/darknet

TensorRT YOLO For Custom Trained Models https://jkjung-avt.github.io/trt-yolo-custom-updated/