Skip to main content

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.

caution

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

Reference

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

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

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

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

Type definitions

interface ClassificationModule {
error: string | null;
isReady: boolean;
isGenerating: boolean;
forward: (input: string) => Promise<{ [category: string]: number }>;
}

Arguments

modelSource A string that specifies the location of the model binary. For more information, take a look at loading models page.

Returns

FieldTypeDescription
forward(input: string) => Promise<{ [category: string]: number }>Executes the model's forward pass, where input can be a fetchable resource or a Base64-encoded string.
errorstring | nullContains the error message if the model failed to load.
isGeneratingbooleanIndicates whether the model is currently processing an inference.
isReadybooleanIndicates whether the model has successfully loaded and is ready for inference.

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({
modulePath: 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