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.
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.
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
- For detailed API Reference for
useClassificationsee:useClassificationAPI Reference. - For all classification models available out-of-the-box in React Native ExecuTorch see: Classification Models.
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:
model- An object containing:modelName- The name of a built-in model. SeeClassificationModelSourcesfor the list of supported models.modelSource- The location of the model binary (a URL or a bundled resource).
- An optional flag
preventLoadwhich prevents auto-loading of the model.
The hook is generic over the model config — TypeScript automatically infers the correct label type based on the modelName you provide. No explicit generic parameter is needed.
You need more details? Check the following resources:
- For detailed information about
useClassificationarguments check this section:useClassificationarguments. - For all classification models available out-of-the-box in React Native ExecuTorch see: Classification Models.
- For more information on loading resources, take a look at loading models page.
Returns
useClassification returns a ClassificationType object containing:
isReady- Whether the model is loaded and ready to process images.isGenerating- Whether the model is currently processing an image.error- An error object if the model failed to load or encountered a runtime error.downloadProgress- A value between 0 and 1 representing the download progress of the model binary.forward- A function to run inference on an image.
Running the model
To run the model, use the forward method. It accepts one argument — the image to classify. The image can be a remote URL, a local file URI, a base64-encoded image (whole URI or only raw base64), or a PixelData object (raw RGB pixel buffer). The function returns a promise resolving to an object mapping label keys to their probabilities.
Images from external sources are stored in your application's temporary directory.
VisionCamera integration
See the full guide: VisionCamera Integration.
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
| Model | Number of classes | Class list | Quantized |
|---|---|---|---|
| efficientnet_v2_s | 1000 | ImageNet1k_v1 | Yes |