Skip to main content
Version: Next

useClassification

Image classification is the process of assigning a label to an image that best describes its contents. For example, when given an image of a puppy, the image classifier should assign the puppy class to that image.

info

Usually, the class with the highest probability is the one that is assigned to an image. However, if there are multiple classes with comparatively high probabilities, this may indicate that the model is not confident in its prediction.

warning

It is recommended to use models provided by us, which are available at our Hugging Face repository. You can also use constants shipped with our library.

API Reference

High Level Overview

import { useClassification, EFFICIENTNET_V2_S } from 'react-native-executorch';

const model = useClassification({ model: EFFICIENTNET_V2_S });

const imageUri = 'file::///Users/.../cute_puppy.png';

try {
const classesWithProbabilities = await model.forward(imageUri);
} catch (error) {
console.error(error);
}

Arguments

useClassification takes ClassificationProps that consists of:

You need more details? Check the following resources:

Returns

useClassification returns an object called ClassificationType containing bunch of functions to interact with Classification models. To get more details please read: ClassificationType API Reference.

Running the model

To run the model, you can use the forward method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns a promise, which can resolve either to an error or an object containing categories with their probabilities.

info

Images from external sources are stored in your application's temporary directory.

Example

import { useClassification, EFFICIENTNET_V2_S } from 'react-native-executorch';

function App() {
const model = useClassification({ model: EFFICIENTNET_V2_S });

// ...
const imageUri = 'file:///Users/.../cute_puppy.png';

try {
const classesWithProbabilities = await model.forward(imageUri);

// Extract three classes with the highest probabilities
const topThreeClasses = Object.entries(classesWithProbabilities)
.sort(([, a], [, b]) => b - a)
.slice(0, 3)
.map(([label, score]) => ({ label, score }));
} catch (error) {
console.error(error);
}
// ...
}

Supported models

ModelNumber of classesClass list
efficientnet_v2_s1000ImageNet1k_v1