레이블이 torchvision인 게시물을 표시합니다. 모든 게시물 표시
레이블이 torchvision인 게시물을 표시합니다. 모든 게시물 표시

2023년 3월 14일 화요일

Xavier NX - YOLOv8 Video Object Detection (JetPack 5.1)

 I mainly use OpenCV for image and video processing. However, a new function has been added for video processing in PyTorch. A new VideoReader class has been added to torchvision.io in addition to the previously used read_video function.

Now (2023.3) is in the beta stage, but it seems to be mainly used for video processing in the future.

The following is the VideoReader guide page on the PyTorch homepage.


READING/WRITING IMAGES AND VIDEOS

The torchvision.io package provides functions for performing IO operations. They are currently specific to reading and writing video and images.

Video

read_video(filename[, start_pts, end_pts, ...])

Reads a video from a file, returning both the video frames as well as the audio frames

read_video_timestamps(filename[, pts_unit])

List the video frames timestamps.

write_video(filename, video_array, fps[, ...])

Writes a 4d tensor in [T, H, W, C] format in a video file

Fine-grained video API

In addition to the read_video function, we provide a high-performance lower-level API for more fine-grained control compared to the read_video function. It does all this whilst fully supporting torchscript.

WARNING

The fine-grained video API is in Beta stage, and backward compatibility is not guaranteed.

VideoReader(path[, stream, num_threads, device])

Fine-grained video-reading API


For video processing, I will compare the performance of using OpenCV, which I mainly used before, and the new videoReader of torchvision.io.

And in the Anaconda virtual environment, OpenCV 4.7 was built directly to support OpenCV video processing. So I will be using OpenCV 4.7 in Anaconda environment.

First, we will compare the video processing speed by reading video frames using OpenCV and then inputting them to the YOLOv8 model.


YOLOv8 Video Detection

All ML models take a lot of time to load. And it takes a lot of time to process the first instance. Therefore, I will process the first frame after loading the model in the performance measurement and then performance measurement from the second frame.

The video to be used for the test is a 340X256 video with a playback time of 10.922 seconds.

<Test Video file WUzgd7C1pWA.mp4>

This file can be downloaded from https://github.com/pytorch/vision/blob/main/test/assets/videos.


YOLOv8 video processing using only OpenCV

First, I will process the video frame using OpenCV. Opening and framing video files using OpenCV is a method that has been handled a lot in the previous example, and it is very easy to use because there are many examples. 

And for OpenCV video processing in anaconda virtual environment, I have built and used OpenCV 4.7 myself for Anaconda. OpenCV 4.7 build for Anaconda

Refer to Installing the Latest Version of OpenCV (ver 4.7) on Xavier NX (JetPack 5.1).


from ultralytics import YOLO
import cv2
import time, sys
import torchvision
import torchvision.transforms as T

colors = [(255,0 , 0), (0,255,0), (0,0,255)]
font = cv2.FONT_HERSHEY_SIMPLEX   
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')

def draw(img, boxes):
    index = 0
    for box in boxes.data:
        p1 =  (int(box[0].item()), int(box[1].item()))
        p2 =  (int(box[2].item()), int(box[3].item()))
        img = cv2.rectangle(img, p1, p2, colors[index % len(colors)], 3)
        text = label_map[int(box[5].item())] + " %4.2f"%(box[4].item()) 
        cv2.putText(img, text, (p1[0], p1[1] - 10), font, fontScale = 1, color = colors[index % len(colors)], thickness = 2)
        index += 1
    # cv2.imshow("draw", img)
    # cv2.waitKey(1)
    out_video.write(img)


# Load a model
model = YOLO("yolov8n.pt")  # load an official model
label_map = model.names

f = 0
net_total = 0.0
total = 0.0

cap = cv2.VideoCapture("./WUzgd7C1pWA.mp4")
# Skip First frame
ret, img = cap.read()
if ret == False:
    print('Video File Read Error')    
    sys.exit(0)

results = model(img)  # predict on an image
h, w, c = img.shape
print('Video Frame shape H:%d, W:%d, Channel:%d'%(h, w, c))

fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out_video = cv2.VideoWriter('./cv_result.mp4', fourcc, cap.get(cv2.CAP_PROP_FPS), (w, h))


while cap.isOpened():
    s = time.time()
    ret, img = cap.read()
    if ret == False:
        break

    results = model(img)  # predict on an image
    net_e = time.time()
    for result in results:
        draw(result.orig_img, result.boxes)
    e = time.time()
    net_total += (net_e - s)
    total += (e - s)
    f += 1

    
fps = f / total 
net_fps = f / net_total 

print("Total processed frames:%d"%f)
print("FPS:%4.2f"%fps)
print("Net FPS:%4.2f"%net_fps)
cv2.destroyAllWindows()
cap.release()
out_video.release()

<video_detect_cv.py>

Now let's run Python code. You must run Python code in the YOLOv8 virtual environment that we have created so far.

(yolov8) spypiggy@spypiggy-NX:~/src/yolov8$ python video_detect_cv.py 

0: 512x640 4 persons, 100.9ms
Speed: 3.9ms preprocess, 100.9ms inference, 12.0ms postprocess per image at shape (1, 3, 640, 640)
Video Frame shape H:256, W:340, Channel:3

......

0: 512x640 4 persons, 1 car, 1 truck, 35.2ms
Speed: 1.6ms preprocess, 35.2ms inference, 5.8ms postprocess per image at shape (1, 3, 640, 640)

0: 512x640 4 persons, 1 truck, 35.6ms
Speed: 1.5ms preprocess, 35.6ms inference, 5.7ms postprocess per image at shape (1, 3, 640, 640)
Total processed frames:326
FPS:15.20
Net FPS:18.27

And this program creates a cv_result.mp4 video that displays the recognized results in a bounding box. If you open the video, you can see that it is displayed properly.

<cv_result.mp4>


The FPS marked two things. The first is the calculation of the time of the entire process, including inputting a frame into the YOLOv8 model and receiving a result value, screen processing of the result value, and storing a video.

And the second is the value calculated only the processing time of the YOLOv8 model.

YOLOv8's lightest model, yolov8n.Considering the use of pt, the values of FPS:15.20 and NetFPS:18.27 are not considered excellent performance.


YOLOv8 video processing using OpenCV and PyTorch VideoReader

Second, I will use VideoReader provided by Pythochi. However, Video Reader is used to read frames and is not directly related to the YOLOv8 model, so the performance will not be much different.

from ultralytics import YOLO
import cv2
import time, sys
import torchvision
import torchvision.transforms as T

colors = [(255,0 , 0), (0,255,0), (0,0,255)]
font = cv2.FONT_HERSHEY_SIMPLEX   
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')

def draw(img, boxes):
    index = 0
    for box in boxes.data:
        p1 =  (int(box[0].item()), int(box[1].item()))
        p2 =  (int(box[2].item()), int(box[3].item()))
        img = cv2.rectangle(img, p1, p2, colors[index % len(colors)], 3)
        text = label_map[int(box[5].item())] + " %4.2f"%(box[4].item()) 
        cv2.putText(img, text, (p1[0], p1[1] - 10), font, fontScale = 1, color = colors[index % len(colors)], thickness = 2)
        index += 1
    # cv2.imshow("draw", img)
    # cv2.waitKey(1)
    out_video.write(img)


# Load a model
model = YOLO("yolov8n.pt")  # load an official model
label_map = model.names

f = 0
net_total = 0.0
total = 0.0
reader = torchvision.io.VideoReader("./WUzgd7C1pWA.mp4", "video")
meta = reader.get_metadata()
fps = meta["video"]['fps'][0]
frame = next(reader)
img = T.ToPILImage()(frame["data"])
results = model(img)  # predict on an image

shape = frame['data'].shape     # pytorch image tensor shape is C H W
size = (frame['data'].shape[2], frame['data'].shape[1]) 
out_video = cv2.VideoWriter('./torch_result.mp4', fourcc, fps, size)


while frame:
    s = time.time()
    try:
        frame = next(reader)
    except StopIteration:    
        break
    img = T.ToPILImage()(frame["data"])
    results = model(img)  # predict on an image
    net_e = time.time()

    for result in results:
        draw(result.orig_img, result.boxes)
    e = time.time()
    net_total += (net_e - s)
    total += (e - s)
    f += 1

    
fps = f / total 
net_fps = f / net_total 

print("Total processed frames:%d"%f)
print("FPS:%4.2f"%fps)
print("Net FPS:%4.2f"%net_fps)
cv2.destroyAllWindows()
out_video.release()

<video_detect_torch.py>


let's run torchvision based Python code. 

(yolov8) spypiggy@spypiggy-NX:~/src/yolov8$ python video_detect_torch.py 

0: 512x640 4 persons, 83.5ms
Speed: 2.3ms preprocess, 83.5ms inference, 10.8ms postprocess per image at shape (1, 3, 640, 640)

0: 512x640 2 persons, 50.0ms
Speed: 2.3ms preprocess, 50.0ms inference, 7.0ms postprocess per image at shape (1, 3, 640, 640)

......

0: 512x640 4 persons, 1 car, 1 truck, 33.7ms
Speed: 1.4ms preprocess, 33.7ms inference, 5.4ms postprocess per image at shape (1, 3, 640, 640)

0: 512x640 4 persons, 1 truck, 33.5ms
Speed: 1.5ms preprocess, 33.5ms inference, 5.4ms postprocess per image at shape (1, 3, 640, 640)
Total processed frames:326
FPS:14.22
Net FPS:16.87

Compared to the case of using only OpenCV, it can be seen that the speed decreased slightly. However, it is difficult to evaluate the exact performance of the Video Reader because it is the result of only one test using a 10-second low-resolution video. 

And it is the performance of the YOLO model that has the greatest impact on the actual FPS. This time, we will test the code that has slightly increased the size of the model. Let's test it by changing the name of the model from the smallest "yolo8n.pt" to the medium "yolo8m.pt" in the previous source codes.


YOLOv8 Video Proecssing Performance

The table below shows the performance (FPS) when executing Object Detection using sample videos and storing videos showing bounding boxes.

<Video Processing Performance by Model>


If you use a higher resolution video file, it will probably slow down a little bit.

I use 10 FPS as the basis when evaluating the model. If it is less than 10 FPS, it is judged that it is difficult to use in commercial projects.  For YOLOv8, yolov8l.It is not easy to use in Xavier NX because the processing speed is low from the pt model.


Wrapping up

We briefly looked at the method and performance of using the YOLOv8 object detection model for video.

Personally, I feel that Xavier NX has lower performance than I thought.

In the next article, we will learn how to convert the YOLOv8 model to TensorRT, which can be optimized for NVidia GPUs, to increase performance.

The source code can be downloaded from my GitHub.









2020년 7월 22일 수요일

Jetson Xavier NX - Python virtual environment and ML platforms(tensorflow, Pytorch) installation

This article is in line with the Jetson Xavier NX-JetPack 4.4 (production release) headless setup described in the previous blog.
So far, I haven't used the Python virtual environment while using the Jetson Nano. The reason is that Jetson Nano only supports SD cards, so it is convenient to prepare several SD cards and install and use the necessary packages. And because JetPack is updated frequently, it was also a big reason to prepare a new image on an SD card from time to time. However, the Xavier NX does offer an M.2 slot for SSD installation. SSDs offer significantly better performance than SD cards or eMMC memory.  Therefore, I will install and use a SSD on the Xavier NX.  When using the Jetson Nano, replacing the SD card was like using a completely new system. However, if you installed the package or saved data using an SSD, it is convenient to continue using the SSD regardless of the SD card replacement.
I will be mainly working on AI edge computing on Xavier NX. And I will use Python 3 as the development language. In the future, most of the data or packages will be installed on SSDs to improve performance and to enable the system to be used without replacing SD cards.

Creating a Python virtual environment

You can install TensorFlow, PyTorch, Yolo, etc. frameworks for use in one virtual environment, or you can create multiple Python virtual environments to install each framework in a separate virtual environment. Just choose the method that suits your Hankyung. Sometimes I feel the need to use TensorFlow and PyTorch together, so I will install multiple machine learning frameworks on a single virtual machine.

spypiggy@XavierNX:~$ sudo apt-get install python3-dev python3-pip
spypiggy@XavierNX:~$ pip3 install  -U virtualenv

The advantage of the Python virtual environment is that all packages installed in the virtual environment are installed in the virtual environment, so if you delete the virtual environment, everything installed in the virtual environment is cleared and does not affect other Python virtual environments.
Make sure to create the Python virtual environment on the SSD installed earlier. Previously, the SSD was mounted at /home/spypiggy. Therefore, create a virtual environment under /home/spypiggy.

Create a Python virtual environment with the following command. When you run this command, the core elements of your current Python 3 are copied to the /home/spypiggy/python directory.

spypiggy@XavierNX:~$ python3 -m virtualenv --system-site-packages /home/spypiggy/python

To enter the virtual environment, use the source command. Notice that the prompt name has been changed.

spypiggy@XavierNX:~$ source /home/spypiggy/python/bin/activate
(python) spypiggy@XavierNX:~$

From now on, all packages installed using the pip3 command are installed under the /home/spypiggy/python directory, and these packages are effective only when working in a virtual environment.


Install machine learning related platforms

Tensorflow

Do not follow the installation method of Tensorflow homepage. NVidia provides TensorFlow optimized for Jetpack version.
You can download the appropriate version for JetPack4.4 from the https://developer.download.nvidia.com/compute/redist/jp/v44/tensorflow/ page.
If the 4.6 version will be released after a while, you can probably search https://developer.download.nvidia.com/compute/redist/jp/v46/tensorflow/.


The naming convention is as follows.

TensorFlow version + NVIDIA container version of TensorFlow + Python version + CPU.

It is safe to select the latest version with a higher NVIDIA container version of TensorFlow value from the same TensorFlow version.
Select the desired version from TensorFlow 2.X version and 1.X version, and then install it. If you want to use both, you can create a new virtual environment and install it in another virtual environment. I will install tensorflow-1.15.2+nv20.6-cp36-cp36m-linux_aarch64.whl.

(python) spypiggy@XavierNX:~$ sudo apt-get install -y libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev
(python) spypiggy@XavierNX:~$ pip3 install numpy grpcio absl-py py-cpuinfo psutil portpicker six mock requests gast h5py astor termcolor protobuf keras-applications keras-preprocessing wrapt google-pasta(python) spypiggy@XavierNX:~$ wget https://developer.download.nvidia.com/compute/redist/jp/v44/tensorflow/tensorflow-1.15.2+nv20.6-cp36-cp36m-linux_aarch64.whl
(python) spypiggy@XavierNX:~$ wget https://developer.download.nvidia.com/compute/redist/jp/v44/tensorflow/tensorflow-1.15.2+nv20.6-cp36-cp36m-linux_aarch64.whl
(python) spypiggy@XavierNX:~$ pip3 install tensorflow-1.15.2+nv20.6-cp36-cp36m-linux_aarch64.whl

After installation, you can check as follows.

(python) spypiggy@XavierNX:~$ python
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-07-22 09:45:49.770178: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.2
WARNING:tensorflow:Deprecation warnings have been disabled. Set TF_ENABLE_DEPRECATION_WARNINGS=1 to re-enable them.
>>> tf.__version__
'1.15.2'


PyTorch

How to install PyTorch 1.6 on Jetson Nano using JetPack 4.4 (DP Version) is explained in this blog. How to install PyTorch on Xavier NX is not much different from the content of this blog.
Like TensorFlow, NVidia provides an installation package optimized for Jetson. Install this package.

We always use Python 3.X. Therefore, download the whl file that can be used in Python 3.6. And install the necessary packages as follows. JetPack 4.3 and later comes with OpenCV 4.1.1 and numpy. So you don't have to install numpy anymore.

(python) spypiggy@XavierNX:~$ sudo apt-get update
(python) spypiggy@XavierNX:~$ wget https://nvidia.box.com/shared/static/yr6sjswn25z7oankw8zy1roow9cy5ur1.whl -O torch-1.6.0-cp36-cp36m-linux_aarch64.whl (python) spypiggy@XavierNX:~$ sudo apt-get install python3-pip libopenblas-base libopenmpi-dev (python) spypiggy@XavierNX:~$ pip3 install Cython (python) spypiggy@XavierNX:~$ pip3 install torch-1.6.0-cp36-cp36m-linux_aarch64.whl

Download Torchvision for Jetson Xavier NX

If you have successfully installed PyTorch 1.6.0, install Torchvision 0.7.0. The latest version of torchvision can be found at https://github.com/pytorch/vision/releases.

(python) spypiggy@XavierNX:~$ sudo apt-get install libjpeg-dev zlib1g-dev
(python) spypiggy@XavierNX:~$ wget https://github.com/pytorch/vision/archive/v0.6.1.tar.gz   
(python) spypiggy@XavierNX:~$ cd v0.7.0
(python) spypiggy@XavierNX:~/v0.7.0$ python3 setup.py install
(python) spypiggy@XavierNX:~/v0.7.0$ sudo apt-get install libfreetype6-dev
(python) spypiggy@XavierNX:~/v0.7.0$ pip3 uninstall pillow
(python) spypiggy@XavierNX:~/v0.7.0$ pip3 install --no-cache-dir pillow

Let's check whether the installation is correct.
If you see the screen like this, the installation is successful.

root@jetpack-4:/usr/local/src/torchvision# python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torchvision
>>> torch.__version__
'1.6.0'
>>> torchvision.__version__
'0.7.0
>>>


Wrapping up

So far, in Xavier NX, I have created a Python virtual environment in SSD storage and installed the machine learning platform Tensorflow and PyTorch. Unlike Jetson Nano, Xavier NX can be equipped with SSD. It is recommended to use SSD actively to improve performance. To install as many Python packages as possible in the SSD repository, I created a Python virtual environment and installed TensorFlow, PyTorch, etc.
I'll run a lot of tests that I've done on Jetson Nano so far on Xavier and post articles comparing performance.
























2020년 6월 21일 일요일

Jetson Nano - Detectron2 Pose Estimation

As for PoseEstimation in PyTorch, I wrote a blog(https://spyjetson.blogspot.com/2019/10/jetsonnano-human-pose-estimation-using_16.html) last year. The conclusion at this time was that PyTorch's PoseEstimation using the ResNet50 model is excellent in accuracy, but difficult to use in the Jetson Nano due to its low performance of 0.2FPS.  In this article, I will look at the performance of Detectron2's Pose Estimation and compare it to the performance introduced in the previous post.
Since Detectron2 has little difference in usage even when using various models, PoseEstimation is quite easy to understand if you look at the Segmentation contents introduced in the previous blog.

Prerequisites

This article assumes that Jetson Nano uses JetPack 4.4 DP or higher and PyTorch 1.5.0 or higher and torchvision 0.6.0 or higher. 
 
 

Keypoint Detection

At the detectron2's github page https://github.com/facebookresearch/detectron2 , You can see the keypoint detection models supported by Detectron2 by looking in the config directory.



In addition to the rcnn-Resnet-50 model used in last year's article, the rcnn-Resnet101 and rcnn-X101 models have been added to Detectron2.

Let's modify the example used in Detectron2 Segmentation and run the example created for Keypoint Detection.

from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import cv2
import numpy as np
import requests, sys, time, os
from PIL import Image, ImageDraw
import argparse



COLORS = [(0, 45, 74, 224), (85, 32, 98, 224), (93, 69, 12, 224),
          (49, 18, 55, 224), (46, 67, 18, 224), (30, 74, 93, 224)]

help = 'Base-Keypoint-RCNN-FPN'
help += ',keypoint_rcnn_R_101_FPN_3x'
help += ',keypoint_rcnn_R_50_FPN_1x'
help += ',keypoint_rcnn_R_50_FPN_3x'
help += ',keypoint_rcnn_X_101_32x8d_FPN_3x'


parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default = 'keypoint_rcnn_R_50_FPN_1x', help = help)
parser.add_argument('--file', type=str, default = '')
parser.add_argument('--size', type=str, default = '640X480', help = 'image inference size ex:320X240')

opt = parser.parse_args()

W, H = opt.size.split('X')
if opt.file == '':
    url = 'http://images.cocodataset.org/val2017/000000439715.jpg'
    img = Image.open(requests.get(url, stream=True).raw).resize((int(W),int(H)))
    im = np.asarray(img, dtype="uint8")
    height, width, channels = im.shape
    if channels == 3:
        im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
    else:
        im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
    
else:
    im = cv2.imread(opt.file, cv2.IMREAD_COLOR)
    height, width, channels = im.shape
    

print('image W:%d H:%d'%(width, height))

network_model = 'COCO-Keypoints/' + opt.model + '.yaml'


cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(model_zoo.get_config_file(network_model))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(network_model)
predictor = DefaultPredictor(cfg)


for i in range (2):
    fps_time  = time.perf_counter()
    outputs = predictor(im)
    fps = 1.0 / (time.perf_counter() - fps_time)
    if i == 1:
        print('===== output =====')
        print(outputs)
        print("Net FPS: %f" % (fps))
        v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
        out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
    
cv2.imwrite("detectron2_poseestimation_%s_result.jpg"%(opt.model), out.get_image()[:, :, ::-1])
cv2.imwrite("./source_image.jpg", im)
<poseestimation.py>

Run the code.

root@jetpack-4:/usr/local/src/detectron2_test# python3 poseestimation.py --file='../test_images/peds_0.jpg'
image W:1920 H:1080
===== output =====
{'instances': Instances(num_instances=4, image_height=1080, image_width=1920, fields=[pred_boxes: Boxes(tensor([[1036.1870,  106.6064, 1403.8110,  973.7678],
        [ 441.3257,  123.6492,  735.9746,  978.6694],
        [ 745.8071,  183.1393, 1015.6016,  935.7772],
        [1483.9121,  160.6765, 1733.1891,  879.7750]], device='cuda:0')), 
scores: tensor([0.9999, 0.9999, 0.9994, 0.9994], device='cuda:0'),
pred_classes: tensor([0, 0, 0, 0], device='cuda:0'),
pred_keypoints: tensor([[[1.2279e+03, 2.2812e+02, 1.2061e+00], [1.2466e+03, 2.1231e+02, 1.5694e+00], [1.2135e+03, 2.1087e+02, 1.2504e+00], [1.2739e+03, 1.8930e+02, 7.5815e-01], [1.1949e+03, 1.9073e+02, 6.5784e-01], [1.3284e+03, 2.7702e+02, 3.0210e-01], [1.1532e+03, 2.7558e+02, 5.2108e-01], [1.3543e+03, 4.0213e+02, 3.6574e-01], [1.1087e+03, 3.6906e+02, 4.3612e-01], [1.3643e+03, 5.2437e+02, 5.2245e-01], [1.0742e+03, 4.2802e+02, 4.0177e-01], [1.2839e+03, 5.4306e+02, 1.1480e-01], [1.1762e+03, 5.3731e+02, 1.2940e-01], [1.2566e+03, 7.2426e+02, 2.7323e-01], [1.1863e+03, 7.1276e+02, 2.8741e-01], [1.2322e+03, 8.9395e+02, 2.2146e-01], [1.2006e+03, 8.7670e+02, 1.3534e-01]], [[6.2027e+02, 2.0930e+02, 9.4037e-01], [6.3608e+02, 1.9490e+02, 1.0583e+00], [6.0446e+02, 1.9202e+02, 9.3918e-01], [6.5620e+02, 2.0066e+02, 1.4123e+00], [5.7859e+02, 1.9922e+02, 1.7396e+00], [6.8926e+02, 2.9422e+02, 2.8203e-01], [5.2397e+02, 2.8990e+02, 3.2651e-01], [7.1082e+02, 4.3097e+02, 3.6540e-01], [4.8516e+02, 4.0218e+02, 3.4584e-01], [7.0795e+02, 5.5332e+02, 6.2979e-01], [4.8229e+02, 4.9142e+02, 5.2058e-01], [6.4614e+02, 5.3317e+02, 1.1674e-01], [5.4266e+02, 5.3317e+02, 1.5501e-01], [6.0877e+02, 7.0734e+02, 2.0952e-01], [5.7428e+02, 7.0878e+02, 2.1423e-01], [5.8290e+02, 9.1749e+02, 3.0073e-01], [6.2458e+02, 8.4120e+02, 1.8838e-01]], [[8.6994e+02, 2.5006e+02, 2.4667e+00], [8.8573e+02, 2.3710e+02, 2.5618e+00], [8.5559e+02, 2.3710e+02, 2.4670e+00], [9.0582e+02, 2.5150e+02, 1.2572e+00], [8.3550e+02, 2.5150e+02, 1.3055e+00], [9.5318e+02, 3.3208e+02, 2.7176e-01], [7.9388e+02, 3.4360e+02, 2.9622e-01], [9.8475e+02, 4.4001e+02, 3.2787e-01], [7.7092e+02, 4.5728e+02, 2.9786e-01], [9.7327e+02, 5.4651e+02, 5.8070e-01], [7.6518e+02, 5.6665e+02, 3.4241e-01], [9.2878e+02, 5.6090e+02, 1.0988e-01], [8.2402e+02, 5.6090e+02, 1.0114e-01], [9.2591e+02, 7.0768e+02, 1.8532e-01], [8.2832e+02, 7.2495e+02, 2.0711e-01], [9.2878e+02, 8.1274e+02, 2.8305e-01], [8.3406e+02, 8.9764e+02, 2.3432e-01]], [[1.5863e+03, 2.2468e+02, 2.2018e-01], [1.5863e+03, 2.1317e+02, 2.2034e-01], [1.6580e+03, 2.2324e+02, 2.6580e-01], [1.5921e+03, 2.2180e+02, 7.7730e-01], [1.6551e+03, 2.2611e+02, 9.5655e-01], [1.5505e+03, 2.9946e+02, 2.8758e-01], [1.6938e+03, 2.9659e+02, 2.5435e-01], [1.5161e+03, 4.0877e+02, 3.5267e-01], [1.7024e+03, 3.9438e+02, 3.7222e-01], [1.5004e+03, 4.8499e+02, 4.2061e-01], [1.6494e+03, 4.0589e+02, 4.2102e-02], [1.5777e+03, 5.0369e+02, 6.9173e-02], [1.6608e+03, 5.1951e+02, 8.7721e-02], [1.5835e+03, 6.7483e+02, 2.1163e-01], [1.6351e+03, 6.6189e+02, 2.8551e-01], [1.6035e+03, 8.4310e+02, 1.3873e-01], [1.6279e+03, 8.1146e+02, 1.0684e-01]]], device='cuda:0')])} Net FPS: 0.194772 Segmentation fault (core dumped)

And you can get the result image like this.

<detectron2_mask_rcnn_R_50_FPN_1x_result.jpg>


We also confirmed that KeyPoint Detection works in the same way as Segmentation. Now let's try to handle the result outputs in our own way without using Detectron2's Visualizer.

Under the Hood

Let's take a look at the result value output by the print function above.
  • num_instances : Number of person recognized
  • image_height :Inference image height
  • image_width :Inference image width
  • pred_boxes : Recognized person's location coordinates
  • scores :Probability of being a person
  • pred_classes : Classification of predicted objects. KeyPoint Detection classifies only one person class. Therefore, only 0 is present in pred_classes.
  • pred_keypoints : KeyPoints Information. a Tensor of shape (N, num_keypoint, 3). Each row in the last dimension is (x, y, score). Scores are larger than 0
The official description of these values ​​can be found at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format.

This python file checks the metadata of the KeypointDetection dataset.

from detectron2 import model_zoo
from detectron2.data import MetadataCatalog
from detectron2.config import get_cfg


network_model = 'COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x.yaml'
cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(model_zoo.get_config_file(network_model))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(network_model)
cls = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes
print('NetworkModel[%s] category:%d'%(network_model, len(cls)))
print(cls)

kname = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).keypoint_names
print('keypoint_names')
print(kname)

krule = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).keypoint_connection_rules
print('keypoint_connection_rules')
print(krule)
<metadata_keypoint.py>

Run the code.

root@jetpack-4:/usr/local/src/detectron2_test# python3 metadata.py
NetworkModel[COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x.yaml] category:1
['person']
keypoint_names
('nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle')
keypoint_connection_rules
[('left_ear', 'left_eye', (102, 204, 255)), ('right_ear', 'right_eye', (51, 153, 255)), ('left_eye', 'nose', (102, 0, 204)), ('nose', 'right_eye', (51, 102, 255)), ('left_shoulder', 'right_shoulder', (255, 128, 0)), ('left_shoulder', 'left_elbow', (153, 255, 204)), ('right_shoulder', 'right_elbow', (128, 229, 255)), ('left_elbow', 'left_wrist', (153, 255, 153)), ('right_elbow', 'right_wrist', (102, 255, 224)), ('left_hip', 'right_hip', (255, 102, 0)), ('left_hip', 'left_knee', (255, 255, 77)), ('right_hip', 'right_knee', (153, 255, 204)), ('left_knee', 'left_ankle', (191, 255, 128)), ('right_knee', 'right_ankle', (255, 195, 77))]

As you can see, the Keypoint detection model has only 1 class category, 17 keypoints, and 14 keypoint connections.

Now let's print out the KeyPoint value to the image location without using Detectron2's Visualizer.

from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import cv2
import numpy as np
import requests, sys, time, os
from PIL import Image, ImageDraw
import argparse



COLORS = [(0, 45, 74), (85, 32, 98), (93, 69, 12), (49, 18, 55), (46, 67, 18), (30, 74, 93),
(218, 0, 0), (0, 218, 0), (0, 0, 218),(218, 218, 0), (0, 218, 218), (218, 0, 218),
(128, 0, 0), (0, 128, 0), (0, 0, 128),(128, 128, 0), (0, 128, 128)]

help = 'Base-Keypoint-RCNN-FPN'
help += ',keypoint_rcnn_R_101_FPN_3x'
help += ',keypoint_rcnn_R_50_FPN_1x'
help += ',keypoint_rcnn_R_50_FPN_3x'
help += ',keypoint_rcnn_X_101_32x8d_FPN_3x'


parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default = 'keypoint_rcnn_R_50_FPN_1x', help = help)
parser.add_argument('--file', type=str, default = '')
parser.add_argument('--size', type=str, default = '640X480', help = 'image inference size ex:320X240')

opt = parser.parse_args()

W, H = opt.size.split('X')
if opt.file == '':
    url = 'http://images.cocodataset.org/val2017/000000439715.jpg'
    img = Image.open(requests.get(url, stream=True).raw).resize((int(W),int(H)))
    im = np.asarray(img, dtype="uint8")
    height, width, channels = im.shape
    if channels == 3:
        im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
    else:
        im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
    
else:
    im = cv2.imread(opt.file, cv2.IMREAD_COLOR)
    height, width, channels = im.shape
    

print('image W:%d H:%d'%(width, height))

network_model = 'COCO-Keypoints/' + opt.model + '.yaml'


cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(model_zoo.get_config_file(network_model))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(network_model)
predictor = DefaultPredictor(cfg)
kname = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).keypoint_names

im2 = im.copy()
font = cv2.FONT_HERSHEY_SIMPLEX  # normal size sans-serif font
outputs = predictor(im)
kpersons = outputs["instances"].pred_keypoints
#for kpoints in kperson[0]:
for kperson in kpersons:
    print('==== person ====')
    for i in range(0, len(kperson)):
        kpoints = kperson[i].cpu()
        x = kpoints[0] 
        y = kpoints[1]
        print('%-20s position  (%f, %f)'%(kname[i], x,y))
        cv2.circle(im2,  (int(x), int(y)), 10, color = COLORS[i], thickness=4)
        cv2.putText(im2, kname[i], (int(x) - 20, int(y) - 10), font, fontScale = 1, color = COLORS[i], thickness = 2)

cv2.imwrite("detectron2_PoseEstimation_%s_result.jpg"%(opt.model), im2)
<poseestimation2.py>


Run the code.

root@jetpack-4:/usr/local/src/detectron2_test# python3 poseestimation2.py --file='../test_images/peds_0.jpg'
image W:1920 H:1080
==== person ====
nose                 position  (1227.897217, 228.124039)
left_eye             position  (1246.565674, 212.305176)
right_eye            position  (1213.536987, 210.867111)
left_ear             position  (1273.850220, 189.295929)
right_ear            position  (1194.868530, 190.734009)
left_shoulder        position  (1328.419312, 277.018738)
right_shoulder       position  (1153.223633, 275.580627)
left_elbow           position  (1354.267944, 402.131531)
right_elbow          position  (1108.706665, 369.055725)
left_wrist           position  (1364.320190, 524.368225)
right_wrist          position  (1074.241943, 428.016998)
left_hip             position  (1283.902466, 543.063293)
right_hip            position  (1176.200073, 537.310913)
left_knee            position  (1256.617798, 724.261169)
right_knee           position  (1186.252319, 712.756531)
left_ankle           position  (1232.205322, 893.954407)
right_ankle          position  (1200.612671, 876.697510)
==== person ====
nose                 position  (620.270996, 209.295181)
left_eye             position  (636.081421, 194.900925)
right_eye            position  (604.460571, 192.022064)
left_ear             position  (656.203857, 200.658615)
right_ear            position  (578.588989, 199.219177)
left_shoulder        position  (689.261963, 294.221436)
right_shoulder       position  (523.971130, 289.903137)
left_elbow           position  (710.821655, 430.967041)
right_elbow          position  (485.163757, 402.178497)
left_wrist           position  (707.947021, 553.318420)
right_wrist          position  (482.289093, 491.423035)
left_hip             position  (646.142639, 533.166443)
right_hip            position  (542.656189, 533.166443)
left_knee            position  (608.772522, 707.337158)
right_knee           position  (574.277039, 708.776611)
left_ankle           position  (582.900879, 917.493591)
right_ankle          position  (624.582947, 841.204041)
==== person ====
nose                 position  (869.941284, 250.056412)
left_eye             position  (885.727112, 237.104706)
right_eye            position  (855.590515, 237.104706)
left_ear             position  (905.818115, 251.495499)
right_ear            position  (835.499390, 251.495499)
left_shoulder        position  (953.175659, 332.083893)
right_shoulder       position  (793.882202, 343.596497)
left_elbow           position  (984.747375, 440.014771)
right_elbow          position  (770.920898, 457.283691)
left_wrist           position  (973.266785, 546.506531)
right_wrist          position  (765.180664, 566.653625)
left_hip             position  (928.779419, 560.897278)
right_hip            position  (824.018799, 560.897278)
left_knee            position  (925.909241, 707.683228)
right_knee           position  (828.323975, 724.952209)
left_ankle           position  (928.779419, 812.735962)
right_ankle          position  (834.064331, 897.641602)
==== person ====
nose                 position  (1586.344971, 224.676224)
left_eye             position  (1586.344971, 213.170654)
right_eye            position  (1657.976074, 223.238022)
left_ear             position  (1592.075439, 221.799850)
right_ear            position  (1655.110840, 226.114426)
left_shoulder        position  (1550.529297, 299.462494)
right_shoulder       position  (1693.791870, 296.586090)
left_elbow           position  (1516.146240, 408.765472)
right_elbow          position  (1702.387573, 394.383484)
left_wrist           position  (1500.387329, 484.989899)
right_wrist          position  (1649.380371, 405.889069)
left_hip             position  (1577.749146, 503.686462)
right_hip            position  (1660.841309, 519.506653)
left_knee            position  (1583.479614, 674.831909)
right_knee           position  (1635.054077, 661.888123)
left_ankle           position  (1603.536377, 843.101074)
right_ankle          position  (1627.890991, 811.460571)
Segmentation fault (core dumped)

And you can get the result image like this.

<detectron2_PoseEstimation_rcnn_R_50_FPN_1x_result.jpg>


Now you can freely use the keypoint location and name without the help of Detectron2's Visualizer.

Wrapping up

So far, I have looked at PoseEstimation supported by Detectron2 on Jetson Nano. As with PyTorch's PoseEstimation blog, which we looked at earlier, despite its excellent accuracy, it still has the drawback of being too slow for use with the Jetson Nano. I always use 10FPS as a criterion for actual availability.
The current speed is unreasonable for use with the Jetson Nano.
Perhaps it can be used on Xavier NX or higher hardware.
Detectron2 has been reorganized to be fairly easy to use, and it is a system that will continue to evolve in FaceBook AI Research in the future.
And they promised to release models with improved processing speed based on R-CNN models such as FBNet, ShuffleNet, and MobileNet. Please check Detectron2's official github https://github.com/facebookresearch/detectron2 from time to time.