NPUs: The Rise of Neural Processing Units

Neural Processing Units (NPUs) have been around for a while, but they’ve recently entered the mainstream. But what exactly are NPUs, why do we need them, and when should we use them?

To understand their role, let’s first take a look at the processors we’ve relied on until now.

The Evolution of Processors

For much of computing history, the Central Processing Unit (CPU) has been the workhorse of general-purpose computing. CPUs handle everything from running operating systems to executing applications. However, they process tasks sequentially—one operation at a time on one piece of data at a time. Even with multi-core CPUs, each core still follows a sequential approach.

Then came Graphics Processing Units (GPUs). Originally designed for rendering graphics, GPUs excel at handling the mathematical operations needed for 2D and 3D images. These operations often involve applying the same calculation to massive amounts of data—such as processing thousands or millions of pixels simultaneously. To achieve this, GPUs contain thousands of smaller cores that work in parallel.

It didn’t take long for AI and machine learning researchers to realise that GPUs were also well-suited for AI workloads. Training and running AI models require massive computational power, and GPUs’ parallel processing capabilities significantly accelerate these tasks. However, GPUs come with a major drawback: they consume a lot of power. Some modern GPUs use over 600 watts when running at full capacity—comparable to a small electric heater—making them expensive to operate and generating substantial heat.

Enter Neural Processing Units

NPUs first emerged around 2016, with some early versions branded as Tensor Processing Units (TPUs). Unlike CPUs, which are designed for general-purpose computing, and GPUs, which provide generic parallel processing, NPUs are purpose-built for AI and machine learning tasks. They specialise in executing neural network operations, such as multiply-add calculations, with exceptional speed and efficiency.

The key advantage of NPUs is their low power consumption. While GPUs deliver impressive AI performance, they are power-hungry. NPUs, on the other hand, provide high-speed AI processing with significantly lower energy usage. This makes them ideal for AI inference—running trained models in real-world applications, including on small hardware such as smartphones. However, when it comes to training AI models, GPUs still hold the edge.

As AI continues to advance, NPUs are poised to play a crucial role, offering a balance of speed and efficiency that bridges the gap between power-hungry GPUs and the slower, more general-purpose CPUs.

Real-World Applications

NPUs are now available in many hardware platforms, including some small form-factors computers such as the OrangePi 5 Pro.

I have recently completed a successful project using a RockChip NPU. (RK3568 and RK3588). The NPU toolkits are good, but initially tricky to work with. Contact me to see if I can help with your NPU-based AI project.

Traditional Computer Vision vs. Deep Learning with Yolo v7

A tricky computer vision project

On a recent computer vision project, we were trying to detect objects in images. Nothing unusual there, but we had some specific problems: The objects could vary in size from ‘very big’ (taking up nearly half the image) down to ‘very small’ (just a few pixels across). The objects were against a very ‘noisy’ background, namely the sky:  complete with clouds and the Sun, sometimes rain, and other foreground objects such as trees. The detection accuracy had to be very high, and false-postives had to be very low: There could be no human intervention to correct mistakes. The software had to run on a credit-card sized computer (Jetson NX), alongside other software including a guidance system – the whole system was on-board a drone. And finally, it had to be fast: The camera we used was running at 30 frames-per-second, and the guidance system we were feeding into expects inputs at that rate: We had to keep up.

Traditional Computer Vision pipelines, and Genetic Algorithms

We had developed ‘pipelines’ of ‘traditional’ computer vision techniques, using OpenCV:  colour space conversions, blurring, thresholding, contour-finding, etc.  The system was working well in about 80% of cases – surprisingly well, given the range of object sizes we were trying to detect against such a noisy background.

But we got stuck chasing down that last 20% or so.  Whenever we made a change to get things working on one specific class of image, it would break another that was previously working.  We even tried automatically generating pipelines using Genetic Algorithms (not my idea, but I wish it had been!) – this generated some pipelines that worked well, but still we couldn’t achieve a system that worked well enough in all cases we might encounter.

Deep Learning: Yolo v7

The main reason for using traditional techniques was for speed – as I metioned, we had very tight timing constraints – but also because, last time we tried deep-learning models (Yolo V1 and V2), they were very bad at detecting objects that were small in the image.

But having hit a blocker in our progress, as a ‘last throw of the dice’, we decided to review the state of the art in deep-learning detectors.

After a review of the options, we settled for various reasons on Yolo v7:  Even then (summer 2024), this wasn’t by any means the newest version of Yolo, but it gave a good combination of being fully open-source, well-documented and supported, and well-integrated with the languages we wanted to use (Python and C++).

The work itself took a while:  There were a number of problems that we had to overcome, including some very technical ‘gotchas’ that nearly caused us to give up on it a few times.  Of course, we also needed a large set of labeled training data, but we already had that.

Results

In short, the results are staggeringly good.  We are now able to detect objects down to just a few pixels across, but more to the point, against very noisy backgrounds:  In some cases ‘lens flares’ caused by the camera facing directly into the sun make the target object almost invisible to the human eye – but our system based on Yolo v7 detects the objects successfully in a very high percentage of cases.  Also, the performance is exceptional – on a Jetson NX, running on the GPU, we are doing inference in around 8ms, allowing time for pre- and post-processing steps to be added and still achieve 30FPS, which is our camera frame-rate.

Yolo V7 is not a plug-and-play solution straight out of the box:  Even just for training, we had to do some careful setup and config, ensure a well-balanced training and testing set, and then train and test until we were satisfied we had a good model that could not only detect our target object, but exclude all others.  Inference (i.e. runtime), especially from C++, was far more difficult – there were one or two fairly esoteric problems.  In particular, detections were often centered correctly, but with wildly wrong ‘rectangular’ bounding boxes – it took a while to work out the solution to that one.

Summary 

There’s still a place for traditional computer vision techniques (and we still use some in this project), but Yolo and other deep-learning detectors are well worth considering.

Contact me (tom@alvervalleysoftware.com) to discuss whether I can help with your computer vision project.  If you’re thinking about using Yolo from Python and C++, I probably can…

ONNX files in OpenCV

I have been aware of OpenCV’s ‘dnn’ module for some time: Last time we tried to use it in a project was a number of years ago, and it didn’t seem to be ready for what we needed – or perhaps we just misunderstood it and didn’t give it a good enough look.

Aside from that, I’ve been using .ONNX (Open Neural Network eXchange) files for a while now. My standard usage of these is to transport a trained model from PyTorch – for example a ResNet classifier – onto a Jetson Nano, NX or Orin. PyTorch can export as .ONNX, and TensorRT on the Jetson can import them, so it’s been literally an ‘exchange’ file format for me.

However, pulling these two things together, I have recently learned that OpenCV’s ‘dnn’ module can load directly from .ONNX files, specifically including ResNet models such as the ResNet18 classifier I have recently trained for a client.

There are a few ‘tricks’ required to prepare images to be classified, and it took me a fair amount of research (including some trial-and-error, and using ChatGPT – that was a day I can never get back…) but it works now: I can classify images, using a ONNX file, in OpenCV, from either C++ or Python.

This means that models that I originally trained for Jetson hardware can now be used on any platform with OpenCV. I will be testing this on a Raspberry Pi 5 shortly to gauge performance.

Currently, it’s using CPU only – but it does use all CPU cores available – but I believe GPU is also supported given a suitably-compiled OpenCV: I may try that next.

Jetson hardware, and the ‘jetson-inference’ package

I have been involved in several projects very recently (and two ongoing) where we have used NVIDIA ‘Jetson’ hardware (Nano, Xavier / NX, and ConnectTech Rudi NX).  These machines are roughly ‘credit-card sized’ (apart from the Rudi, which has a larger but very ‘rugged’ case) and are ideal for ‘edge’ or embedded systems.

The Jetson hardware is basically a small but powerful GPU, but also including a CPU and small ‘motherboard’ providing the usual USB ports, etc.  They run a modified version of Ubuntu Linux.

In some cases I developed software in-house using OpenCV (C++ and Python).  However, I am also making more and more use of the excellent ‘jetson-inference’ library of deep-learning tools, and have now built up quite a bit of experience in using this library and developing applications and solutions based on it.

In short, it is very good for developing solutions that need:

  • Image classification (e.g. cat vs dog, or labrador vs poodle, or beach vs park)
  • Object detection (i.e. accurate location, and classification of objects – can be trained to recognise new objects, including very small/distant)
  • Pose estimation (e.g. standing, sitting, walking, pointing, waving)

I have now developed a number of solutions that have ‘gone live’ using this hardware and toolkit.  I am also experienced in the ‘back end’ tasks of training new ‘models’ to recognise new, specfic classes of objects, and porting those models to the Jetson hardware.

Please contact me to discuss whether I can help you with your Jetson-based project.  tom [at] alvervalleysoftware.com.