Transformers.js provides browser-based machine learning capabilities that mirror the Hugging Face Transformers Python library. It supports a wide range of pretrained models across multiple domains.
Under the hood, Transformers.js uses ONNX Runtime for model execution in the browser. You can convert existing PyTorch, TensorFlow, or JAX models to ONNX format using the Optimum library.
example.js
import * as transformers from from '@huggingface/transformers';
const path = 'xenova/resnet-50';
const options = {
dtype: 'fp16',
device: 'webnn-gpu', // 'webnn', 'webnn-cpu', 'webnn-npu'
session_options: {
freeDimensionOverrides: {
batch_size: 1,
num_channels: 3,
height: 224,
width: 224,
},
context: {},
},
};
const classifier = await transformers.pipeline('image-classification', path, options);
let [err, output] = await asyncErrorHandling(classifier(imageUrl, { topk: 3 }));
WebNN does not currently support dynamic shapes and requires
free_dimension_overrides
to be set.
free_dimension_overrides
can be set in sample code above or just in config.json of your models as a field within “transformers.js_config”. Whenfree_dimension_overrides
is not set, you may experience significant performance degradation.
Last updated on