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

interface TensorFactory {
    fromGpuBuffer<T>(buffer, options): TypedTensor<T>;
    fromImage(imageData, options?): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
    fromImage(imageElement, options?): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
    fromImage(urlSource, options?): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
    fromImage(bitmap, options): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
    fromPinnedBuffer<T>(type, buffer, dims?): TypedTensor<T>;
    fromTexture<T>(texture, options): TypedTensor<"float32">;
}

Hierarchy (view full)

Methods

  • create a tensor from a WebGPU buffer

    Type Parameters

    Parameters

    • buffer: GpuBufferType

      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

    • Optional options: 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

    • Optional options: 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.

    • Optional options: 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 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"

    Parameters

    • type: T

      the tensor element type.

    • buffer: DataTypeMap[T]

      a TypedArray corresponding to the type.

    • Optional dims: 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

Generated using TypeDoc