Skip to main content

Image Classification

In this Quick guide, we start with image classification. It is a way for computers to recognize and label images based on what they contain. For example, a model can look at a photo and tell if it’s a cat, dog, or car.

note

If you haven't yet setup your board and installed the examples, please refer to the quick start.

info

The Quick guide is compatible with all Machina SL2600-series Kits with OOBE image with pip and python pre-installed and optimization tailored to:

NPU for SL2610-Series

MobileNet models

We'll be using MobileNet, a vision model originally developed in 2017 by Google specifically for designed for mobile and embedded devices.

The MobileNet v2 model is pre-installed on your Astra board. It has been pretrained on the ImageNet database, then quantized and compiled to the NPU on Synaptics Astra for even higher performance.

  • Input: An 224 x 224 image
  • Output: Confidence scores for 1000 ImageNet classes - we will select the top class as our classification result

Set up your environment

If you haven't done so already, setup your environment.

Clone our Examples GitHub repository and Navigate to the Repository Directory:

git clone https://github.com/synaptics-astra-demos/sl2610-examples
cd sl2610-examples

Set up your Python environment ensuring all required dependencies are installed within a virtual environment:

python3 -m venv .venv --system-site-packages
source .venv/bin/activate
pip install -r requirements.txt

Set up the display environment (required for visual output).

export XDG_RUNTIME_DIR=/var/run/user/0
export WAYLAND_DISPLAY=wayland-1

Image classification on the edge

Lets run image classification on Synaptics Astra using the Torq example below:

cd Image_classification/standalone_class

python3 classification.py \
--model mbv2.vmfb \
--image goldfish.jpg \
--labels labels.json \
--device torq
Use Case Image

And you should get the result:

Classification Results:
1. goldfish, Carassius auratus : 0.921875
2. starfish, sea star : 0.675781
3. sea slug, nudibranch : 0.617188
4. rock beauty, Holocanthus tricolor : 0.613281
5. hummingbird : 0.582031

Top Prediction: goldfish, Carassius auratus

You can look up class in the ImageNet classes list. It should have recognized the goldfish!

Classify another image

We can change a command line parameter to classify another image. Running:

python3 classification.py \
--model mbv2.vmfb \
--image cat.jpg \
--labels labels.json \
--device torq
Use Case Image

Should output the result:

Classification Results:
1. Persian cat : 0.800781
2. tabby, tabby cat : 0.714844
3. tiger cat : 0.691406
4. Egyptian cat : 0.683594
5. lynx, catamount : 0.671875

Top Prediction: Persian cat

MobleNet correctly identifies this as a cat.

Input sources

So far, we've been running image classification on single image examples. However, Astra pipelines can accept *.jpg images, a *.mp4 video, a /dev/video camera feed.

Image classification is not designed to handle multiple subjects or identify where they are in an image. For that we need Object Detection. In the next Quick guide, you will be doing Object Detection.