• Create a new session instance with LRU cache management.

    Creates a new session for the specified model with intelligent caching. Sessions are cached and reused when possible to improve performance. For custom models, provide the modelPath in the config parameter.

    Parameters

    • modelName: string = 'u2net'

      Name of the model to use (default: 'u2net')

    • Optional config: U2NetCustomConfig

      Configuration for custom models (required for 'u2net_custom')

    • Optional options: SessionOptions

      Session options for WebNN, WebGPU, and other settings

    Returns Promise<BaseSession>

    Promise that resolves to a BaseSession instance

    Throws

    When model name is not supported

    Throws

    When u2net_custom is used without modelPath in config

    Throws

    When model fails to load or initialize

    Example

    // Create a standard U2Net session
    const session = await newSession('u2net');

    // Create a session with WebNN acceleration
    const session = await newSession('u2net', undefined, {
    preferWebNN: true,
    webnnDeviceType: 'gpu'
    });

    // Create a custom model session
    const customSession = await newSession('u2net_custom', {
    modelPath: '/path/to/my-model.onnx',
    inputSize: [512, 512]
    });