ONNX Runtime JavaScript API
    Preparing search index...

    Interface TensorFactory

    type TensorFactory defines the factory functions of 'Tensor' to create tensor instances from existing data or resources.

    interface TensorFactory {
        fromGpuBuffer<T extends GpuBufferDataTypes>(
            buffer: GpuBufferTypeFallback,
            options: TensorFromGpuBufferOptions<T>,
        ): TypedTensor<T>;
        fromImage(
            imageData: ImageData,
            options?: TensorFromImageDataOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromImage(
            imageElement: HTMLImageElement,
            options?: TensorFromImageElementOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromImage(
            urlSource: string,
            options?: TensorFromUrlOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromImage(
            bitmap: ImageBitmap,
            options: TensorFromImageBitmapOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromMLTensor<T extends MLTensorDataTypes>(
            tensor: MLTensorTypeFallback,
            options: TensorFromMLTensorOptions<T>,
        ): TypedTensor<T>;
        fromPinnedBuffer<
            T extends
                | "float32"
                | "uint8"
                | "int8"
                | "uint16"
                | "int16"
                | "int32"
                | "int64"
                | "bool"
                | "float16"
                | "float64"
                | "uint32"
                | "uint64"
                | "uint4"
                | "int4",
        >(
            type: T,
            buffer: DataTypeMap[T],
            dims?: readonly number[],
        ): TypedTensor<T>;
        fromTexture<T extends "float32" = "float32">(
            texture: WebGLTexture,
            options: TensorFromTextureOptions<T>,
        ): TypedTensor<"float32">;
    }

    Hierarchy (View Summary)

    Index

    Methods

    • create a tensor from a WebGPU buffer

      Type Parameters

      Parameters

      • buffer: GpuBufferTypeFallback

        the GPUBuffer object to create tensor from

      • options: TensorFromGpuBufferOptions<T>

        An optional object representing options for creating tensor from WebGPU buffer.

        The options include following properties:

        • dataType: the data type of the tensor. If omitted, assume 'float32'.
        • dims: the dimension of the tensor. Required.
        • download: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
        • dispose: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed. Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.

      Returns TypedTensor<T>

      a tensor object

    • create a tensor from an ImageData object

      Parameters

      • imageData: ImageData

        the ImageData object to create tensor from

      • Optionaloptions: TensorFromImageDataOptions

        An optional object representing options for creating tensor from ImageData.

        The following default settings will be applied:

        • tensorFormat: 'RGB'
        • tensorLayout: 'NCHW'
        • dataType: 'float32'

      Returns Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>

      A promise that resolves to a tensor object

    • create a tensor from a HTMLImageElement object

      Parameters

      • imageElement: HTMLImageElement

        the HTMLImageElement object to create tensor from

      • Optionaloptions: TensorFromImageElementOptions

        An optional object representing options for creating tensor from HTMLImageElement.

        The following default settings will be applied:

        • tensorFormat: 'RGB'
        • tensorLayout: 'NCHW'
        • dataType: 'float32'

      Returns Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>

      A promise that resolves to a tensor object

    • create a tensor from URL

      Parameters

      • urlSource: string

        a string as a URL to the image or a data URL containing the image data.

      • Optionaloptions: TensorFromUrlOptions

        An optional object representing options for creating tensor from URL.

        The following default settings will be applied:

        • tensorFormat: 'RGB'
        • tensorLayout: 'NCHW'
        • dataType: 'float32'

      Returns Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>

      A promise that resolves to a tensor object

    • create a tensor from an ImageBitmap object

      Parameters

      • bitmap: ImageBitmap

        the ImageBitmap object to create tensor from

      • options: TensorFromImageBitmapOptions

        An optional object representing options for creating tensor from URL.

        The following default settings will be applied:

        • tensorFormat: 'RGB'
        • tensorLayout: 'NCHW'
        • dataType: 'float32'

      Returns Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>

      A promise that resolves to a tensor object

    • create a tensor from a WebNN MLTensor

      Type Parameters

      Parameters

      • tensor: MLTensorTypeFallback

        the MLTensor object to create tensor from

      • options: TensorFromMLTensorOptions<T>

        An optional object representing options for creating tensor from a WebNN MLTensor.

        The options include following properties:

        • dataType: the data type of the tensor. If omitted, assume 'float32'.
        • dims: the dimension of the tensor. Required.
        • download: an optional function to download the tensor data from the MLTensor to CPU. If omitted, the MLTensor data will not be able to download. Usually, this is provided by the WebNN backend for the inference outputs. Users don't need to provide this function.
        • dispose: an optional function to dispose the tensor data on the WebNN MLTensor. If omitted, the MLTensor will not be disposed. Usually, this is provided by the WebNN backend for the inference outputs. Users don't need to provide this function.

      Returns TypedTensor<T>

      a tensor object

    • create a tensor from a pre-allocated buffer. The buffer will be used as a pinned buffer.

      Type Parameters

      • T extends
            | "float32"
            | "uint8"
            | "int8"
            | "uint16"
            | "int16"
            | "int32"
            | "int64"
            | "bool"
            | "float16"
            | "float64"
            | "uint32"
            | "uint64"
            | "uint4"
            | "int4"

      Parameters

      • type: T

        the tensor element type.

      • buffer: DataTypeMap[T]

        a TypedArray corresponding to the type.

      • Optionaldims: readonly number[]

        specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<T>

      a tensor object

    • create a tensor from a WebGL texture

      Type Parameters

      • T extends "float32" = "float32"

      Parameters

      • texture: WebGLTexture

        the WebGLTexture object to create tensor from

      • options: TensorFromTextureOptions<T>

        An optional object representing options for creating tensor from WebGL texture.

        The options include following properties:

        • width: the width of the texture. Required.
        • height: the height of the texture. Required.
        • format: the format of the texture. If omitted, assume 'RGBA'.
        • download: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
        • dispose: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed. Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.

      Returns TypedTensor<"float32">

      a tensor object