Abstract base class for all ONNX model sessions.

Provides common functionality for model loading, caching, and inference. All specific model implementations (U2Net, ISNet, etc.) extend this class.

Example

// Create a custom session class
class MyModelSession extends BaseSession {
constructor(options?: SessionOptions) {
super('my-model', options);
}

protected getDefaultModelUrl(): string {
return '/models/my-model.onnx';
}

protected getNormalizationParams(): NormalizationParams {
return { mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], size: [320, 320] };
}

// ... implement other abstract methods
}

Hierarchy (view full)

Constructors

Methods

  • Initialize the ONNX session

    If ONNX profiling is enabled (via rembgConfig.enableONNXProfiling(true)), ONNX Runtime will collect profiling data for each inference run. Profiling data is automatically outputted to the console after each inference.

    Returns Promise<void>

  • Parameters

    • imageCanvas: HTMLCanvasElement

    Returns {
        [inputName: string]: ort.Tensor;
    }

    • [inputName: string]: ort.Tensor
  • Parameters

    • input: {
          [inputName: string]: ort.Tensor;
      }
      • [inputName: string]: ort.Tensor

    Returns Promise<OnnxValueMapType>

  • Predict masks for input image

    Parameters

    • imageCanvas: HTMLCanvasElement

    Returns Promise<HTMLCanvasElement[]>

  • generic model output processing

    Most models output a single tensor with shape [1, 1, height, width] This method extracts the data from the tensor and returns it as a Float32Array

    Parameters

    • outputs: OnnxValueMapType

      Model outputs

    Returns Float32Array<ArrayBufferLike>[]

    [Mask array]

  • Generic mask array to mask canvas processing

    Most models just normalize, resize and return the result as a mask canvas.

    Parameters

    • maskArray: Float32Array<ArrayBufferLike>

      Model output mask array

    • originalSize: {
          width: number;
          height: number;
      }

      Original image dimensions for resizing

      • width: number
      • height: number

    Returns HTMLCanvasElement

    HTMLCanvasElement

  • Get session options

    Returns SessionOptions

  • Clear all cached models from IndexedDB

    Returns Promise<void>

  • Clear cache for a specific model

    Parameters

    • modelName: string

    Returns Promise<void>