chore: update node_modules with new binary files and dependencies
- Add new binary files for nodemon, onnxruntime-web, and xenova/transformers - Update various JavaScript and TypeScript files in node_modules - Remove unused files and dependencies - Add new test fixtures and documentation files
This commit is contained in:
102
node_modules/onnxruntime-common/lib/backend-impl.ts
generated
vendored
Normal file
102
node_modules/onnxruntime-common/lib/backend-impl.ts
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {Backend} from './backend';
|
||||
|
||||
interface BackendInfo {
|
||||
backend: Backend;
|
||||
priority: number;
|
||||
|
||||
initPromise?: Promise<void>;
|
||||
initialized?: boolean;
|
||||
aborted?: boolean;
|
||||
}
|
||||
|
||||
const backends: {[name: string]: BackendInfo} = {};
|
||||
const backendsSortedByPriority: string[] = [];
|
||||
|
||||
/**
|
||||
* Register a backend.
|
||||
*
|
||||
* @param name - the name as a key to lookup as an execution provider.
|
||||
* @param backend - the backend object.
|
||||
* @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority
|
||||
* < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export const registerBackend = (name: string, backend: Backend, priority: number): void => {
|
||||
if (backend && typeof backend.init === 'function' && typeof backend.createSessionHandler === 'function') {
|
||||
const currentBackend = backends[name];
|
||||
if (currentBackend === undefined) {
|
||||
backends[name] = {backend, priority};
|
||||
} else if (currentBackend.priority > priority) {
|
||||
// same name is already registered with a higher priority. skip registeration.
|
||||
return;
|
||||
} else if (currentBackend.priority === priority) {
|
||||
if (currentBackend.backend !== backend) {
|
||||
throw new Error(`cannot register backend "${name}" using priority ${priority}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (priority >= 0) {
|
||||
const i = backendsSortedByPriority.indexOf(name);
|
||||
if (i !== -1) {
|
||||
backendsSortedByPriority.splice(i, 1);
|
||||
}
|
||||
|
||||
for (let i = 0; i < backendsSortedByPriority.length; i++) {
|
||||
if (backends[backendsSortedByPriority[i]].priority <= priority) {
|
||||
backendsSortedByPriority.splice(i, 0, name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
backendsSortedByPriority.push(name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
throw new TypeError('not a valid backend');
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve backend by specified hints.
|
||||
*
|
||||
* @param backendHints - a list of execution provider names to lookup. If omitted use registered backends as list.
|
||||
* @returns a promise that resolves to the backend.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export const resolveBackend = async(backendHints: readonly string[]): Promise<Backend> => {
|
||||
const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints;
|
||||
const errors = [];
|
||||
for (const backendName of backendNames) {
|
||||
const backendInfo = backends[backendName];
|
||||
if (backendInfo) {
|
||||
if (backendInfo.initialized) {
|
||||
return backendInfo.backend;
|
||||
} else if (backendInfo.aborted) {
|
||||
continue; // current backend is unavailable; try next
|
||||
}
|
||||
|
||||
const isInitializing = !!backendInfo.initPromise;
|
||||
try {
|
||||
if (!isInitializing) {
|
||||
backendInfo.initPromise = backendInfo.backend.init();
|
||||
}
|
||||
await backendInfo.initPromise;
|
||||
backendInfo.initialized = true;
|
||||
return backendInfo.backend;
|
||||
} catch (e) {
|
||||
if (!isInitializing) {
|
||||
errors.push({name: backendName, err: e});
|
||||
}
|
||||
backendInfo.aborted = true;
|
||||
} finally {
|
||||
delete backendInfo.initPromise;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`no available backend found. ERR: ${errors.map(e => `[${e.name}] ${e.err}`).join(', ')}`);
|
||||
};
|
||||
49
node_modules/onnxruntime-common/lib/backend.ts
generated
vendored
Normal file
49
node_modules/onnxruntime-common/lib/backend.ts
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {InferenceSession} from './inference-session';
|
||||
import {OnnxValue} from './onnx-value';
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export declare namespace SessionHandler {
|
||||
type FeedsType = {[name: string]: OnnxValue};
|
||||
type FetchesType = {[name: string]: OnnxValue | null};
|
||||
type ReturnType = {[name: string]: OnnxValue};
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent a handler instance of an inference session.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export interface SessionHandler {
|
||||
dispose(): Promise<void>;
|
||||
|
||||
readonly inputNames: readonly string[];
|
||||
readonly outputNames: readonly string[];
|
||||
|
||||
startProfiling(): void;
|
||||
endProfiling(): void;
|
||||
|
||||
run(feeds: SessionHandler.FeedsType, fetches: SessionHandler.FetchesType,
|
||||
options: InferenceSession.RunOptions): Promise<SessionHandler.ReturnType>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent a backend that provides implementation of model inferencing.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export interface Backend {
|
||||
/**
|
||||
* Initialize the backend asynchronously. Should throw when failed.
|
||||
*/
|
||||
init(): Promise<void>;
|
||||
|
||||
createSessionHandler(uriOrBuffer: string|Uint8Array, options?: InferenceSession.SessionOptions):
|
||||
Promise<SessionHandler>;
|
||||
}
|
||||
|
||||
export {registerBackend} from './backend-impl';
|
||||
37
node_modules/onnxruntime-common/lib/env-impl.ts
generated
vendored
Normal file
37
node_modules/onnxruntime-common/lib/env-impl.ts
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {Env} from './env';
|
||||
|
||||
type LogLevelType = Env['logLevel'];
|
||||
export class EnvImpl implements Env {
|
||||
constructor() {
|
||||
this.wasm = {};
|
||||
this.webgl = {};
|
||||
this.logLevelInternal = 'warning';
|
||||
}
|
||||
|
||||
// TODO standadize the getter and setter convention in env for other fields.
|
||||
set logLevel(value: LogLevelType) {
|
||||
if (value === undefined) {
|
||||
return;
|
||||
}
|
||||
if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) {
|
||||
throw new Error(`Unsupported logging level: ${value}`);
|
||||
}
|
||||
this.logLevelInternal = value;
|
||||
}
|
||||
get logLevel(): LogLevelType {
|
||||
return this.logLevelInternal;
|
||||
}
|
||||
|
||||
debug?: boolean;
|
||||
|
||||
wasm: Env.WebAssemblyFlags;
|
||||
|
||||
webgl: Env.WebGLFlags;
|
||||
|
||||
[name: string]: unknown;
|
||||
|
||||
private logLevelInternal: Required<LogLevelType>;
|
||||
}
|
||||
118
node_modules/onnxruntime-common/lib/env.ts
generated
vendored
Normal file
118
node_modules/onnxruntime-common/lib/env.ts
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {EnvImpl} from './env-impl';
|
||||
export declare namespace Env {
|
||||
export type WasmPrefixOrFilePaths = string|{
|
||||
'ort-wasm.wasm'?: string;
|
||||
'ort-wasm-threaded.wasm'?: string;
|
||||
'ort-wasm-simd.wasm'?: string;
|
||||
'ort-wasm-simd-threaded.wasm'?: string;
|
||||
};
|
||||
export interface WebAssemblyFlags {
|
||||
/**
|
||||
* set or get number of thread(s). If omitted or set to 0, number of thread(s) will be determined by system. If set
|
||||
* to 1, no worker thread will be spawned.
|
||||
*
|
||||
* This setting is available only when WebAssembly multithread feature is available in current context.
|
||||
*
|
||||
* @defaultValue `0`
|
||||
*/
|
||||
numThreads?: number;
|
||||
|
||||
/**
|
||||
* set or get a boolean value indicating whether to enable SIMD. If set to false, SIMD will be forcely disabled.
|
||||
*
|
||||
* This setting is available only when WebAssembly SIMD feature is available in current context.
|
||||
*
|
||||
* @defaultValue `true`
|
||||
*/
|
||||
simd?: boolean;
|
||||
|
||||
/**
|
||||
* Set or get a number specifying the timeout for initialization of WebAssembly backend, in milliseconds. A zero
|
||||
* value indicates no timeout is set.
|
||||
*
|
||||
* @defaultValue `0`
|
||||
*/
|
||||
initTimeout?: number;
|
||||
|
||||
/**
|
||||
* Set a custom URL prefix to the .wasm files or a set of overrides for each .wasm file. The override path should be
|
||||
* an absolute path.
|
||||
*/
|
||||
wasmPaths?: WasmPrefixOrFilePaths;
|
||||
|
||||
/**
|
||||
* Set or get a boolean value indicating whether to proxy the execution of main thread to a worker thread.
|
||||
*
|
||||
* @defaultValue `false`
|
||||
*/
|
||||
proxy?: boolean;
|
||||
}
|
||||
|
||||
export interface WebGLFlags {
|
||||
/**
|
||||
* Set or get the WebGL Context ID (webgl or webgl2).
|
||||
*
|
||||
* @defaultValue `'webgl2'`
|
||||
*/
|
||||
contextId?: 'webgl'|'webgl2';
|
||||
/**
|
||||
* Set or get the maximum batch size for matmul. 0 means to disable batching.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
matmulMaxBatchSize?: number;
|
||||
/**
|
||||
* Set or get the texture cache mode.
|
||||
*
|
||||
* @defaultValue `'full'`
|
||||
*/
|
||||
textureCacheMode?: 'initializerOnly'|'full';
|
||||
/**
|
||||
* Set or get the packed texture mode
|
||||
*
|
||||
* @defaultValue `false`
|
||||
*/
|
||||
pack?: boolean;
|
||||
/**
|
||||
* Set or get whether enable async download.
|
||||
*
|
||||
* @defaultValue `false`
|
||||
*/
|
||||
async?: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export interface Env {
|
||||
/**
|
||||
* set the severity level for logging.
|
||||
*
|
||||
* @defaultValue `'warning'`
|
||||
*/
|
||||
logLevel?: 'verbose'|'info'|'warning'|'error'|'fatal';
|
||||
/**
|
||||
* Indicate whether run in debug mode.
|
||||
*
|
||||
* @defaultValue `false`
|
||||
*/
|
||||
debug?: boolean;
|
||||
|
||||
/**
|
||||
* Represent a set of flags for WebAssembly
|
||||
*/
|
||||
wasm: Env.WebAssemblyFlags;
|
||||
|
||||
/**
|
||||
* Represent a set of flags for WebGL
|
||||
*/
|
||||
webgl: Env.WebGLFlags;
|
||||
|
||||
[name: string]: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent a set of flags as a global singleton.
|
||||
*/
|
||||
export const env: Env = new EnvImpl();
|
||||
24
node_modules/onnxruntime-common/lib/index.ts
generated
vendored
Normal file
24
node_modules/onnxruntime-common/lib/index.ts
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
/**
|
||||
* # ONNX Runtime JavaScript API
|
||||
*
|
||||
* ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages:
|
||||
*
|
||||
* - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node)
|
||||
* - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web)
|
||||
* - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native)
|
||||
*
|
||||
* See also:
|
||||
* - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript.html)
|
||||
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
|
||||
*
|
||||
* @packageDocumentation
|
||||
*/
|
||||
|
||||
export * from './backend';
|
||||
export * from './env';
|
||||
export * from './inference-session';
|
||||
export * from './tensor';
|
||||
export * from './onnx-value';
|
||||
208
node_modules/onnxruntime-common/lib/inference-session-impl.ts
generated
vendored
Normal file
208
node_modules/onnxruntime-common/lib/inference-session-impl.ts
generated
vendored
Normal file
@@ -0,0 +1,208 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {SessionHandler} from './backend';
|
||||
import {resolveBackend} from './backend-impl';
|
||||
import {InferenceSession as InferenceSessionInterface} from './inference-session';
|
||||
import {OnnxValue} from './onnx-value';
|
||||
import {Tensor} from './tensor';
|
||||
|
||||
type SessionOptions = InferenceSessionInterface.SessionOptions;
|
||||
type RunOptions = InferenceSessionInterface.RunOptions;
|
||||
type FeedsType = InferenceSessionInterface.FeedsType;
|
||||
type FetchesType = InferenceSessionInterface.FetchesType;
|
||||
type ReturnType = InferenceSessionInterface.ReturnType;
|
||||
|
||||
export class InferenceSession implements InferenceSessionInterface {
|
||||
private constructor(handler: SessionHandler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
run(feeds: FeedsType, options?: RunOptions): Promise<ReturnType>;
|
||||
run(feeds: FeedsType, fetches: FetchesType, options?: RunOptions): Promise<ReturnType>;
|
||||
async run(feeds: FeedsType, arg1?: FetchesType|RunOptions, arg2?: RunOptions): Promise<ReturnType> {
|
||||
const fetches: {[name: string]: OnnxValue|null} = {};
|
||||
let options: RunOptions = {};
|
||||
// check inputs
|
||||
if (typeof feeds !== 'object' || feeds === null || feeds instanceof Tensor || Array.isArray(feeds)) {
|
||||
throw new TypeError(
|
||||
'\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.');
|
||||
}
|
||||
|
||||
let isFetchesEmpty = true;
|
||||
// determine which override is being used
|
||||
if (typeof arg1 === 'object') {
|
||||
if (arg1 === null) {
|
||||
throw new TypeError('Unexpected argument[1]: cannot be null.');
|
||||
}
|
||||
if (arg1 instanceof Tensor) {
|
||||
throw new TypeError('\'fetches\' cannot be a Tensor');
|
||||
}
|
||||
|
||||
if (Array.isArray(arg1)) {
|
||||
if (arg1.length === 0) {
|
||||
throw new TypeError('\'fetches\' cannot be an empty array.');
|
||||
}
|
||||
isFetchesEmpty = false;
|
||||
// output names
|
||||
for (const name of arg1) {
|
||||
if (typeof name !== 'string') {
|
||||
throw new TypeError('\'fetches\' must be a string array or an object.');
|
||||
}
|
||||
if (this.outputNames.indexOf(name) === -1) {
|
||||
throw new RangeError(`'fetches' contains invalid output name: ${name}.`);
|
||||
}
|
||||
fetches[name] = null;
|
||||
}
|
||||
|
||||
if (typeof arg2 === 'object' && arg2 !== null) {
|
||||
options = arg2;
|
||||
} else if (typeof arg2 !== 'undefined') {
|
||||
throw new TypeError('\'options\' must be an object.');
|
||||
}
|
||||
} else {
|
||||
// decide whether arg1 is fetches or options
|
||||
// if any output name is present and its value is valid OnnxValue, we consider it fetches
|
||||
let isFetches = false;
|
||||
const arg1Keys = Object.getOwnPropertyNames(arg1);
|
||||
for (const name of this.outputNames) {
|
||||
if (arg1Keys.indexOf(name) !== -1) {
|
||||
const v = (arg1 as InferenceSessionInterface.NullableOnnxValueMapType)[name];
|
||||
if (v === null || v instanceof Tensor) {
|
||||
isFetches = true;
|
||||
isFetchesEmpty = false;
|
||||
fetches[name] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isFetches) {
|
||||
if (typeof arg2 === 'object' && arg2 !== null) {
|
||||
options = arg2;
|
||||
} else if (typeof arg2 !== 'undefined') {
|
||||
throw new TypeError('\'options\' must be an object.');
|
||||
}
|
||||
} else {
|
||||
options = arg1 as RunOptions;
|
||||
}
|
||||
}
|
||||
} else if (typeof arg1 !== 'undefined') {
|
||||
throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.');
|
||||
}
|
||||
|
||||
// check if all inputs are in feed
|
||||
for (const name of this.inputNames) {
|
||||
if (typeof feeds[name] === 'undefined') {
|
||||
throw new Error(`input '${name}' is missing in 'feeds'.`);
|
||||
}
|
||||
}
|
||||
|
||||
// if no fetches is specified, we use the full output names list
|
||||
if (isFetchesEmpty) {
|
||||
for (const name of this.outputNames) {
|
||||
fetches[name] = null;
|
||||
}
|
||||
}
|
||||
|
||||
// feeds, fetches and options are prepared
|
||||
|
||||
const results = await this.handler.run(feeds, fetches, options);
|
||||
const returnValue: {[name: string]: OnnxValue} = {};
|
||||
for (const key in results) {
|
||||
if (Object.hasOwnProperty.call(results, key)) {
|
||||
returnValue[key] = new Tensor(results[key].type, results[key].data, results[key].dims);
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
static create(path: string, options?: SessionOptions): Promise<InferenceSessionInterface>;
|
||||
static create(buffer: ArrayBufferLike, options?: SessionOptions): Promise<InferenceSessionInterface>;
|
||||
static create(buffer: ArrayBufferLike, byteOffset: number, byteLength?: number, options?: SessionOptions):
|
||||
Promise<InferenceSessionInterface>;
|
||||
static create(buffer: Uint8Array, options?: SessionOptions): Promise<InferenceSessionInterface>;
|
||||
static async create(
|
||||
arg0: string|ArrayBufferLike|Uint8Array, arg1?: SessionOptions|number, arg2?: number,
|
||||
arg3?: SessionOptions): Promise<InferenceSessionInterface> {
|
||||
// either load from a file or buffer
|
||||
let filePathOrUint8Array: string|Uint8Array;
|
||||
let options: SessionOptions = {};
|
||||
|
||||
if (typeof arg0 === 'string') {
|
||||
filePathOrUint8Array = arg0;
|
||||
if (typeof arg1 === 'object' && arg1 !== null) {
|
||||
options = arg1;
|
||||
} else if (typeof arg1 !== 'undefined') {
|
||||
throw new TypeError('\'options\' must be an object.');
|
||||
}
|
||||
} else if (arg0 instanceof Uint8Array) {
|
||||
filePathOrUint8Array = arg0;
|
||||
if (typeof arg1 === 'object' && arg1 !== null) {
|
||||
options = arg1;
|
||||
} else if (typeof arg1 !== 'undefined') {
|
||||
throw new TypeError('\'options\' must be an object.');
|
||||
}
|
||||
} else if (
|
||||
arg0 instanceof ArrayBuffer ||
|
||||
(typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) {
|
||||
const buffer = arg0;
|
||||
let byteOffset = 0;
|
||||
let byteLength = arg0.byteLength;
|
||||
if (typeof arg1 === 'object' && arg1 !== null) {
|
||||
options = arg1;
|
||||
} else if (typeof arg1 === 'number') {
|
||||
byteOffset = arg1;
|
||||
if (!Number.isSafeInteger(byteOffset)) {
|
||||
throw new RangeError('\'byteOffset\' must be an integer.');
|
||||
}
|
||||
if (byteOffset < 0 || byteOffset >= buffer.byteLength) {
|
||||
throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`);
|
||||
}
|
||||
byteLength = arg0.byteLength - byteOffset;
|
||||
if (typeof arg2 === 'number') {
|
||||
byteLength = arg2;
|
||||
if (!Number.isSafeInteger(byteLength)) {
|
||||
throw new RangeError('\'byteLength\' must be an integer.');
|
||||
}
|
||||
if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) {
|
||||
throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`);
|
||||
}
|
||||
if (typeof arg3 === 'object' && arg3 !== null) {
|
||||
options = arg3;
|
||||
} else if (typeof arg3 !== 'undefined') {
|
||||
throw new TypeError('\'options\' must be an object.');
|
||||
}
|
||||
} else if (typeof arg2 !== 'undefined') {
|
||||
throw new TypeError('\'byteLength\' must be a number.');
|
||||
}
|
||||
} else if (typeof arg1 !== 'undefined') {
|
||||
throw new TypeError('\'options\' must be an object.');
|
||||
}
|
||||
filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength);
|
||||
} else {
|
||||
throw new TypeError('Unexpected argument[0]: must be \'path\' or \'buffer\'.');
|
||||
}
|
||||
|
||||
// get backend hints
|
||||
const eps = options.executionProviders || [];
|
||||
const backendHints = eps.map(i => typeof i === 'string' ? i : i.name);
|
||||
const backend = await resolveBackend(backendHints);
|
||||
const handler = await backend.createSessionHandler(filePathOrUint8Array, options);
|
||||
return new InferenceSession(handler);
|
||||
}
|
||||
|
||||
startProfiling(): void {
|
||||
this.handler.startProfiling();
|
||||
}
|
||||
endProfiling(): void {
|
||||
this.handler.endProfiling();
|
||||
}
|
||||
|
||||
get inputNames(): readonly string[] {
|
||||
return this.handler.inputNames;
|
||||
}
|
||||
get outputNames(): readonly string[] {
|
||||
return this.handler.outputNames;
|
||||
}
|
||||
|
||||
private handler: SessionHandler;
|
||||
}
|
||||
380
node_modules/onnxruntime-common/lib/inference-session.ts
generated
vendored
Normal file
380
node_modules/onnxruntime-common/lib/inference-session.ts
generated
vendored
Normal file
@@ -0,0 +1,380 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {InferenceSession as InferenceSessionImpl} from './inference-session-impl';
|
||||
import {OnnxValue} from './onnx-value';
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-redeclare */
|
||||
|
||||
export declare namespace InferenceSession {
|
||||
// #region input/output types
|
||||
|
||||
type OnnxValueMapType = {readonly [name: string]: OnnxValue};
|
||||
type NullableOnnxValueMapType = {readonly [name: string]: OnnxValue | null};
|
||||
|
||||
/**
|
||||
* A feeds (model inputs) is an object that uses input names as keys and OnnxValue as corresponding values.
|
||||
*/
|
||||
type FeedsType = OnnxValueMapType;
|
||||
|
||||
/**
|
||||
* A fetches (model outputs) could be one of the following:
|
||||
*
|
||||
* - Omitted. Use model's output names definition.
|
||||
* - An array of string indicating the output names.
|
||||
* - An object that use output names as keys and OnnxValue or null as corresponding values.
|
||||
*
|
||||
* @remark
|
||||
* different from input argument, in output, OnnxValue is optional. If an OnnxValue is present it will be
|
||||
* used as a pre-allocated value by the inference engine; if omitted, inference engine will allocate buffer
|
||||
* internally.
|
||||
*/
|
||||
type FetchesType = readonly string[]|NullableOnnxValueMapType;
|
||||
|
||||
/**
|
||||
* A inferencing return type is an object that uses output names as keys and OnnxValue as corresponding values.
|
||||
*/
|
||||
type ReturnType = OnnxValueMapType;
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region session options
|
||||
|
||||
/**
|
||||
* A set of configurations for session behavior.
|
||||
*/
|
||||
export interface SessionOptions {
|
||||
/**
|
||||
* An array of execution provider options.
|
||||
*
|
||||
* An execution provider option can be a string indicating the name of the execution provider,
|
||||
* or an object of corresponding type.
|
||||
*/
|
||||
executionProviders?: readonly ExecutionProviderConfig[];
|
||||
|
||||
/**
|
||||
* The intra OP threads number.
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
|
||||
*/
|
||||
intraOpNumThreads?: number;
|
||||
|
||||
/**
|
||||
* The inter OP threads number.
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
|
||||
*/
|
||||
interOpNumThreads?: number;
|
||||
|
||||
/**
|
||||
* The optimization level.
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
graphOptimizationLevel?: 'disabled'|'basic'|'extended'|'all';
|
||||
|
||||
/**
|
||||
* Whether enable CPU memory arena.
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
enableCpuMemArena?: boolean;
|
||||
|
||||
/**
|
||||
* Whether enable memory pattern.
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
enableMemPattern?: boolean;
|
||||
|
||||
/**
|
||||
* Execution mode.
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
executionMode?: 'sequential'|'parallel';
|
||||
|
||||
/**
|
||||
* Wether enable profiling.
|
||||
*
|
||||
* This setting is a placeholder for a future use.
|
||||
*/
|
||||
enableProfiling?: boolean;
|
||||
|
||||
/**
|
||||
* File prefix for profiling.
|
||||
*
|
||||
* This setting is a placeholder for a future use.
|
||||
*/
|
||||
profileFilePrefix?: string;
|
||||
|
||||
/**
|
||||
* Log ID.
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
logId?: string;
|
||||
|
||||
/**
|
||||
* Log severity level. See
|
||||
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
logSeverityLevel?: 0|1|2|3|4;
|
||||
|
||||
/**
|
||||
* Log verbosity level.
|
||||
*
|
||||
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
|
||||
*/
|
||||
logVerbosityLevel?: number;
|
||||
|
||||
/**
|
||||
* Store configurations for a session. See
|
||||
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
|
||||
* onnxruntime_session_options_config_keys.h
|
||||
*
|
||||
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
|
||||
*
|
||||
* @example
|
||||
* ```js
|
||||
* extra: {
|
||||
* session: {
|
||||
* set_denormal_as_zero: "1",
|
||||
* disable_prepacking: "1"
|
||||
* },
|
||||
* optimization: {
|
||||
* enable_gelu_approximation: "1"
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
extra?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
// #region execution providers
|
||||
|
||||
// Currently, we have the following backends to support execution providers:
|
||||
// Backend Node.js binding: supports 'cpu' and 'cuda'.
|
||||
// Backend WebAssembly: supports 'cpu', 'wasm' and 'xnnpack'.
|
||||
// Backend ONNX.js: supports 'webgl'.
|
||||
interface ExecutionProviderOptionMap {
|
||||
cpu: CpuExecutionProviderOption;
|
||||
cuda: CudaExecutionProviderOption;
|
||||
wasm: WebAssemblyExecutionProviderOption;
|
||||
webgl: WebGLExecutionProviderOption;
|
||||
xnnpack: XnnpackExecutionProviderOption;
|
||||
}
|
||||
|
||||
type ExecutionProviderName = keyof ExecutionProviderOptionMap;
|
||||
type ExecutionProviderConfig =
|
||||
ExecutionProviderOptionMap[ExecutionProviderName]|ExecutionProviderOption|ExecutionProviderName|string;
|
||||
|
||||
export interface ExecutionProviderOption {
|
||||
readonly name: string;
|
||||
}
|
||||
export interface CpuExecutionProviderOption extends ExecutionProviderOption {
|
||||
readonly name: 'cpu';
|
||||
useArena?: boolean;
|
||||
}
|
||||
export interface CudaExecutionProviderOption extends ExecutionProviderOption {
|
||||
readonly name: 'cuda';
|
||||
deviceId?: number;
|
||||
}
|
||||
export interface WebAssemblyExecutionProviderOption extends ExecutionProviderOption {
|
||||
readonly name: 'wasm';
|
||||
}
|
||||
export interface WebGLExecutionProviderOption extends ExecutionProviderOption {
|
||||
readonly name: 'webgl';
|
||||
// TODO: add flags
|
||||
}
|
||||
export interface XnnpackExecutionProviderOption extends ExecutionProviderOption {
|
||||
readonly name: 'xnnpack';
|
||||
}
|
||||
// #endregion
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region run options
|
||||
|
||||
/**
|
||||
* A set of configurations for inference run behavior
|
||||
*/
|
||||
export interface RunOptions {
|
||||
/**
|
||||
* Log severity level. See
|
||||
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
logSeverityLevel?: 0|1|2|3|4;
|
||||
|
||||
/**
|
||||
* Log verbosity level.
|
||||
*
|
||||
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
|
||||
*/
|
||||
logVerbosityLevel?: number;
|
||||
|
||||
/**
|
||||
* Terminate all incomplete OrtRun calls as soon as possible if true
|
||||
*
|
||||
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
|
||||
*/
|
||||
terminate?: boolean;
|
||||
|
||||
/**
|
||||
* A tag for the Run() calls using this
|
||||
*
|
||||
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
|
||||
*/
|
||||
tag?: string;
|
||||
|
||||
/**
|
||||
* Set a single run configuration entry. See
|
||||
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
|
||||
* onnxruntime_run_options_config_keys.h
|
||||
*
|
||||
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```js
|
||||
* extra: {
|
||||
* memory: {
|
||||
* enable_memory_arena_shrinkage: "1",
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
extra?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region value metadata
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
interface ValueMetadata {
|
||||
// TBD
|
||||
}
|
||||
|
||||
// #endregion
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent a runtime instance of an ONNX model.
|
||||
*/
|
||||
export interface InferenceSession {
|
||||
// #region run()
|
||||
|
||||
/**
|
||||
* Execute the model asynchronously with the given feeds and options.
|
||||
*
|
||||
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
|
||||
* @param options - Optional. A set of options that controls the behavior of model inference.
|
||||
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
|
||||
*/
|
||||
run(feeds: InferenceSession.FeedsType, options?: InferenceSession.RunOptions): Promise<InferenceSession.ReturnType>;
|
||||
|
||||
/**
|
||||
* Execute the model asynchronously with the given feeds, fetches and options.
|
||||
*
|
||||
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
|
||||
* @param fetches - Representation of the model output. See type description of `InferenceSession.OutputType` for
|
||||
* detail.
|
||||
* @param options - Optional. A set of options that controls the behavior of model inference.
|
||||
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
|
||||
*/
|
||||
run(feeds: InferenceSession.FeedsType, fetches: InferenceSession.FetchesType,
|
||||
options?: InferenceSession.RunOptions): Promise<InferenceSession.ReturnType>;
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region profiling
|
||||
|
||||
/**
|
||||
* Start profiling.
|
||||
*/
|
||||
startProfiling(): void;
|
||||
|
||||
/**
|
||||
* End profiling.
|
||||
*/
|
||||
endProfiling(): void;
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region metadata
|
||||
|
||||
/**
|
||||
* Get input names of the loaded model.
|
||||
*/
|
||||
readonly inputNames: readonly string[];
|
||||
|
||||
/**
|
||||
* Get output names of the loaded model.
|
||||
*/
|
||||
readonly outputNames: readonly string[];
|
||||
|
||||
// /**
|
||||
// * Get input metadata of the loaded model.
|
||||
// */
|
||||
// readonly inputMetadata: ReadonlyArray<Readonly<InferenceSession.ValueMetadata>>;
|
||||
|
||||
// /**
|
||||
// * Get output metadata of the loaded model.
|
||||
// */
|
||||
// readonly outputMetadata: ReadonlyArray<Readonly<InferenceSession.ValueMetadata>>;
|
||||
|
||||
// #endregion
|
||||
}
|
||||
|
||||
export interface InferenceSessionFactory {
|
||||
// #region create()
|
||||
|
||||
/**
|
||||
* Create a new inference session and load model asynchronously from an ONNX model file.
|
||||
*
|
||||
* @param uri - The URI or file path of the model to load.
|
||||
* @param options - specify configuration for creating a new inference session.
|
||||
* @returns A promise that resolves to an InferenceSession object.
|
||||
*/
|
||||
create(uri: string, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
|
||||
|
||||
/**
|
||||
* Create a new inference session and load model asynchronously from an array bufer.
|
||||
*
|
||||
* @param buffer - An ArrayBuffer representation of an ONNX model.
|
||||
* @param options - specify configuration for creating a new inference session.
|
||||
* @returns A promise that resolves to an InferenceSession object.
|
||||
*/
|
||||
create(buffer: ArrayBufferLike, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
|
||||
|
||||
/**
|
||||
* Create a new inference session and load model asynchronously from segment of an array bufer.
|
||||
*
|
||||
* @param buffer - An ArrayBuffer representation of an ONNX model.
|
||||
* @param byteOffset - The beginning of the specified portion of the array buffer.
|
||||
* @param byteLength - The length in bytes of the array buffer.
|
||||
* @param options - specify configuration for creating a new inference session.
|
||||
* @returns A promise that resolves to an InferenceSession object.
|
||||
*/
|
||||
create(buffer: ArrayBufferLike, byteOffset: number, byteLength?: number, options?: InferenceSession.SessionOptions):
|
||||
Promise<InferenceSession>;
|
||||
|
||||
/**
|
||||
* Create a new inference session and load model asynchronously from a Uint8Array.
|
||||
*
|
||||
* @param buffer - A Uint8Array representation of an ONNX model.
|
||||
* @param options - specify configuration for creating a new inference session.
|
||||
* @returns A promise that resolves to an InferenceSession object.
|
||||
*/
|
||||
create(buffer: Uint8Array, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
|
||||
|
||||
// #endregion
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
export const InferenceSession: InferenceSessionFactory = InferenceSessionImpl;
|
||||
13
node_modules/onnxruntime-common/lib/onnx-value.ts
generated
vendored
Normal file
13
node_modules/onnxruntime-common/lib/onnx-value.ts
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {Tensor} from './tensor';
|
||||
|
||||
type NonTensorType = never;
|
||||
|
||||
/**
|
||||
* Type OnnxValue Represents both tensors and non-tensors value for model's inputs/outputs.
|
||||
*
|
||||
* NOTE: currently not support non-tensor
|
||||
*/
|
||||
export type OnnxValue = Tensor|NonTensorType;
|
||||
522
node_modules/onnxruntime-common/lib/tensor-impl.ts
generated
vendored
Normal file
522
node_modules/onnxruntime-common/lib/tensor-impl.ts
generated
vendored
Normal file
@@ -0,0 +1,522 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {Tensor as TensorInterface, TensorFromImageOptions, TensorToImageDataOptions} from './tensor';
|
||||
|
||||
type TensorType = TensorInterface.Type;
|
||||
type TensorDataType = TensorInterface.DataType;
|
||||
|
||||
type SupportedTypedArrayConstructors = Float32ArrayConstructor|Uint8ArrayConstructor|Int8ArrayConstructor|
|
||||
Uint16ArrayConstructor|Int16ArrayConstructor|Int32ArrayConstructor|BigInt64ArrayConstructor|Uint8ArrayConstructor|
|
||||
Float64ArrayConstructor|Uint32ArrayConstructor|BigUint64ArrayConstructor;
|
||||
type SupportedTypedArray = InstanceType<SupportedTypedArrayConstructors>;
|
||||
|
||||
const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && typeof BigInt64Array.from === 'function';
|
||||
const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && typeof BigUint64Array.from === 'function';
|
||||
|
||||
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
|
||||
const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map<string, SupportedTypedArrayConstructors>([
|
||||
['float32', Float32Array],
|
||||
['uint8', Uint8Array],
|
||||
['int8', Int8Array],
|
||||
['uint16', Uint16Array],
|
||||
['int16', Int16Array],
|
||||
['int32', Int32Array],
|
||||
['bool', Uint8Array],
|
||||
['float64', Float64Array],
|
||||
['uint32', Uint32Array],
|
||||
]);
|
||||
|
||||
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
|
||||
const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map<SupportedTypedArrayConstructors, TensorType>([
|
||||
[Float32Array, 'float32'],
|
||||
[Uint8Array, 'uint8'],
|
||||
[Int8Array, 'int8'],
|
||||
[Uint16Array, 'uint16'],
|
||||
[Int16Array, 'int16'],
|
||||
[Int32Array, 'int32'],
|
||||
[Float64Array, 'float64'],
|
||||
[Uint32Array, 'uint32'],
|
||||
]);
|
||||
|
||||
if (isBigInt64ArrayAvailable) {
|
||||
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array);
|
||||
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64');
|
||||
}
|
||||
if (isBigUint64ArrayAvailable) {
|
||||
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array);
|
||||
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64');
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate size from dims.
|
||||
*
|
||||
* @param dims the dims array. May be an illegal input.
|
||||
*/
|
||||
const calculateSize = (dims: readonly unknown[]): number => {
|
||||
let size = 1;
|
||||
for (let i = 0; i < dims.length; i++) {
|
||||
const dim = dims[i];
|
||||
if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) {
|
||||
throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`);
|
||||
}
|
||||
if (dim < 0) {
|
||||
throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`);
|
||||
}
|
||||
size *= dim;
|
||||
}
|
||||
return size;
|
||||
};
|
||||
|
||||
export class Tensor implements TensorInterface {
|
||||
// #region constructors
|
||||
constructor(type: TensorType, data: TensorDataType|readonly number[]|readonly boolean[], dims?: readonly number[]);
|
||||
constructor(data: TensorDataType|readonly boolean[], dims?: readonly number[]);
|
||||
constructor(
|
||||
arg0: TensorType|TensorDataType|readonly boolean[], arg1?: TensorDataType|readonly number[]|readonly boolean[],
|
||||
arg2?: readonly number[]) {
|
||||
let type: TensorType;
|
||||
let data: TensorDataType;
|
||||
let dims: typeof arg1|typeof arg2;
|
||||
// check whether arg0 is type or data
|
||||
if (typeof arg0 === 'string') {
|
||||
//
|
||||
// Override: constructor(type, data, ...)
|
||||
//
|
||||
type = arg0;
|
||||
dims = arg2;
|
||||
if (arg0 === 'string') {
|
||||
// string tensor
|
||||
if (!Array.isArray(arg1)) {
|
||||
throw new TypeError('A string tensor\'s data must be a string array.');
|
||||
}
|
||||
// we don't check whether every element in the array is string; this is too slow. we assume it's correct and
|
||||
// error will be populated at inference
|
||||
data = arg1;
|
||||
} else {
|
||||
// numeric tensor
|
||||
const typedArrayConstructor = NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0);
|
||||
if (typedArrayConstructor === undefined) {
|
||||
throw new TypeError(`Unsupported tensor type: ${arg0}.`);
|
||||
}
|
||||
if (Array.isArray(arg1)) {
|
||||
// use 'as any' here because TypeScript's check on type of 'SupportedTypedArrayConstructors.from()' produces
|
||||
// incorrect results.
|
||||
// 'typedArrayConstructor' should be one of the typed array prototype objects.
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
data = (typedArrayConstructor as any).from(arg1);
|
||||
} else if (arg1 instanceof typedArrayConstructor) {
|
||||
data = arg1;
|
||||
} else {
|
||||
throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//
|
||||
// Override: constructor(data, ...)
|
||||
//
|
||||
dims = arg1;
|
||||
if (Array.isArray(arg0)) {
|
||||
// only boolean[] and string[] is supported
|
||||
if (arg0.length === 0) {
|
||||
throw new TypeError('Tensor type cannot be inferred from an empty array.');
|
||||
}
|
||||
const firstElementType = typeof arg0[0];
|
||||
if (firstElementType === 'string') {
|
||||
type = 'string';
|
||||
data = arg0;
|
||||
} else if (firstElementType === 'boolean') {
|
||||
type = 'bool';
|
||||
// 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is
|
||||
// wrong type. We use 'as any' to make it happy.
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
data = Uint8Array.from(arg0 as any[]);
|
||||
} else {
|
||||
throw new TypeError(`Invalid element type of data array: ${firstElementType}.`);
|
||||
}
|
||||
} else {
|
||||
// get tensor type from TypedArray
|
||||
const mappedType =
|
||||
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor as SupportedTypedArrayConstructors);
|
||||
if (mappedType === undefined) {
|
||||
throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`);
|
||||
}
|
||||
type = mappedType;
|
||||
data = arg0 as SupportedTypedArray;
|
||||
}
|
||||
}
|
||||
|
||||
// type and data is processed, now processing dims
|
||||
if (dims === undefined) {
|
||||
// assume 1-D tensor if dims omitted
|
||||
dims = [data.length];
|
||||
} else if (!Array.isArray(dims)) {
|
||||
throw new TypeError('A tensor\'s dims must be a number array');
|
||||
}
|
||||
|
||||
// perform check
|
||||
const size = calculateSize(dims);
|
||||
if (size !== data.length) {
|
||||
throw new Error(`Tensor's size(${size}) does not match data length(${data.length}).`);
|
||||
}
|
||||
|
||||
this.dims = dims as readonly number[];
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
this.size = size;
|
||||
}
|
||||
// #endregion
|
||||
/**
|
||||
* Create a new tensor object from image object
|
||||
*
|
||||
* @param buffer - Extracted image buffer data - assuming RGBA format
|
||||
* @param imageFormat - input image configuration - required configurations height, width, format
|
||||
* @param tensorFormat - output tensor configuration - Default is RGB format
|
||||
*/
|
||||
private static bufferToTensor(buffer: Uint8ClampedArray|undefined, options: TensorFromImageOptions): Tensor {
|
||||
if (buffer === undefined) {
|
||||
throw new Error('Image buffer must be defined');
|
||||
}
|
||||
if (options.height === undefined || options.width === undefined) {
|
||||
throw new Error('Image height and width must be defined');
|
||||
}
|
||||
|
||||
const {height, width} = options;
|
||||
|
||||
const norm = options.norm;
|
||||
let normMean: number;
|
||||
let normBias: number;
|
||||
if (norm === undefined || norm.mean === undefined) {
|
||||
normMean = 255;
|
||||
} else {
|
||||
normMean = norm.mean;
|
||||
}
|
||||
if (norm === undefined || norm.bias === undefined) {
|
||||
normBias = 0;
|
||||
} else {
|
||||
normBias = norm.bias;
|
||||
}
|
||||
|
||||
const inputformat = options.bitmapFormat !== undefined ? options.bitmapFormat : 'RGBA';
|
||||
// default value is RGBA since imagedata and HTMLImageElement uses it
|
||||
|
||||
const outputformat = options.tensorFormat !== undefined ?
|
||||
(options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') :
|
||||
'RGB';
|
||||
const offset = height * width;
|
||||
const float32Data = outputformat === 'RGBA' ? new Float32Array(offset * 4) : new Float32Array(offset * 3);
|
||||
|
||||
// Default pointer assignments
|
||||
let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
|
||||
let rTensorPointer = 0, gTensorPointer = offset, bTensorPointer = offset * 2, aTensorPointer = -1;
|
||||
|
||||
// Updating the pointer assignments based on the input image format
|
||||
if (inputformat === 'RGB') {
|
||||
step = 3;
|
||||
rImagePointer = 0;
|
||||
gImagePointer = 1;
|
||||
bImagePointer = 2;
|
||||
aImagePointer = -1;
|
||||
}
|
||||
|
||||
// Updating the pointer assignments based on the output tensor format
|
||||
if (outputformat === 'RGBA') {
|
||||
aTensorPointer = offset * 3;
|
||||
} else if (outputformat === 'RBG') {
|
||||
rTensorPointer = 0;
|
||||
bTensorPointer = offset;
|
||||
gTensorPointer = offset * 2;
|
||||
} else if (outputformat === 'BGR') {
|
||||
bTensorPointer = 0;
|
||||
gTensorPointer = offset;
|
||||
rTensorPointer = offset * 2;
|
||||
}
|
||||
|
||||
for (let i = 0; i < offset;
|
||||
i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) {
|
||||
float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias) / normMean;
|
||||
float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias) / normMean;
|
||||
float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias) / normMean;
|
||||
if (aTensorPointer !== -1 && aImagePointer !== -1) {
|
||||
float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias) / normMean;
|
||||
}
|
||||
}
|
||||
|
||||
// Float32Array -> ort.Tensor
|
||||
const outputTensor = outputformat === 'RGBA' ? new Tensor('float32', float32Data, [1, 4, height, width]) :
|
||||
new Tensor('float32', float32Data, [1, 3, height, width]);
|
||||
return outputTensor;
|
||||
}
|
||||
|
||||
// #region factory
|
||||
static async fromImage(imageData: ImageData, options?: TensorFromImageOptions): Promise<Tensor>;
|
||||
static async fromImage(imageElement: HTMLImageElement, options?: TensorFromImageOptions): Promise<Tensor>;
|
||||
static async fromImage(bitmap: ImageBitmap, options: TensorFromImageOptions): Promise<Tensor>;
|
||||
static async fromImage(url: string, options?: TensorFromImageOptions): Promise<Tensor>;
|
||||
|
||||
static async fromImage(image: ImageData|HTMLImageElement|ImageBitmap|string, options?: TensorFromImageOptions):
|
||||
Promise<Tensor> {
|
||||
// checking the type of image object
|
||||
const isHTMLImageEle = typeof (HTMLImageElement) !== 'undefined' && image instanceof HTMLImageElement;
|
||||
const isImageDataEle = typeof (ImageData) !== 'undefined' && image instanceof ImageData;
|
||||
const isImageBitmap = typeof (ImageBitmap) !== 'undefined' && image instanceof ImageBitmap;
|
||||
const isURL = typeof (String) !== 'undefined' && (image instanceof String || typeof image === 'string');
|
||||
|
||||
let data: Uint8ClampedArray|undefined;
|
||||
let tensorConfig: TensorFromImageOptions = {};
|
||||
|
||||
// filling and checking image configuration options
|
||||
if (isHTMLImageEle) {
|
||||
// HTMLImageElement - image object - format is RGBA by default
|
||||
const canvas = document.createElement('canvas');
|
||||
const pixels2DContext = canvas.getContext('2d');
|
||||
|
||||
if (pixels2DContext != null) {
|
||||
let height = image.naturalHeight;
|
||||
let width = image.naturalWidth;
|
||||
|
||||
if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) {
|
||||
height = options.resizedHeight;
|
||||
width = options.resizedWidth;
|
||||
}
|
||||
|
||||
if (options !== undefined) {
|
||||
tensorConfig = options;
|
||||
if (options.tensorFormat !== undefined) {
|
||||
throw new Error('Image input config format must be RGBA for HTMLImageElement');
|
||||
} else {
|
||||
tensorConfig.tensorFormat = 'RGBA';
|
||||
}
|
||||
if (options.height !== undefined && options.height !== height) {
|
||||
throw new Error('Image input config height doesn\'t match HTMLImageElement height');
|
||||
} else {
|
||||
tensorConfig.height = height;
|
||||
}
|
||||
if (options.width !== undefined && options.width !== width) {
|
||||
throw new Error('Image input config width doesn\'t match HTMLImageElement width');
|
||||
} else {
|
||||
tensorConfig.width = width;
|
||||
}
|
||||
} else {
|
||||
tensorConfig.tensorFormat = 'RGBA';
|
||||
tensorConfig.height = height;
|
||||
tensorConfig.width = width;
|
||||
}
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
pixels2DContext.drawImage(image, 0, 0, width, height);
|
||||
data = pixels2DContext.getImageData(0, 0, width, height).data;
|
||||
} else {
|
||||
throw new Error('Can not access image data');
|
||||
}
|
||||
|
||||
} else if (isImageDataEle) {
|
||||
// ImageData - image object - format is RGBA by default
|
||||
const format = 'RGBA';
|
||||
let height: number;
|
||||
let width: number;
|
||||
|
||||
if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) {
|
||||
height = options.resizedHeight;
|
||||
width = options.resizedWidth;
|
||||
} else {
|
||||
height = image.height;
|
||||
width = image.width;
|
||||
}
|
||||
|
||||
if (options !== undefined) {
|
||||
tensorConfig = options;
|
||||
if (options.bitmapFormat !== undefined && options.bitmapFormat !== format) {
|
||||
throw new Error('Image input config format must be RGBA for ImageData');
|
||||
} else {
|
||||
tensorConfig.bitmapFormat = 'RGBA';
|
||||
}
|
||||
} else {
|
||||
tensorConfig.bitmapFormat = 'RGBA';
|
||||
}
|
||||
|
||||
tensorConfig.height = height;
|
||||
tensorConfig.width = width;
|
||||
|
||||
if (options !== undefined) {
|
||||
const tempCanvas = document.createElement('canvas');
|
||||
|
||||
tempCanvas.width = width;
|
||||
tempCanvas.height = height;
|
||||
|
||||
const pixels2DContext = tempCanvas.getContext('2d');
|
||||
|
||||
if (pixels2DContext != null) {
|
||||
pixels2DContext.putImageData(image, 0, 0);
|
||||
data = pixels2DContext.getImageData(0, 0, width, height).data;
|
||||
} else {
|
||||
throw new Error('Can not access image data');
|
||||
}
|
||||
} else {
|
||||
data = image.data;
|
||||
}
|
||||
|
||||
} else if (isImageBitmap) {
|
||||
// ImageBitmap - image object - format must be provided by user
|
||||
if (options === undefined) {
|
||||
throw new Error('Please provide image config with format for Imagebitmap');
|
||||
}
|
||||
if (options.bitmapFormat !== undefined) {
|
||||
throw new Error('Image input config format must be defined for ImageBitmap');
|
||||
}
|
||||
|
||||
const pixels2DContext = document.createElement('canvas').getContext('2d');
|
||||
|
||||
if (pixels2DContext != null) {
|
||||
const height = image.height;
|
||||
const width = image.width;
|
||||
pixels2DContext.drawImage(image, 0, 0, width, height);
|
||||
data = pixels2DContext.getImageData(0, 0, width, height).data;
|
||||
if (options !== undefined) {
|
||||
// using square brackets to avoid TS error - type 'never'
|
||||
if (options.height !== undefined && options.height !== height) {
|
||||
throw new Error('Image input config height doesn\'t match ImageBitmap height');
|
||||
} else {
|
||||
tensorConfig.height = height;
|
||||
}
|
||||
// using square brackets to avoid TS error - type 'never'
|
||||
if (options.width !== undefined && options.width !== width) {
|
||||
throw new Error('Image input config width doesn\'t match ImageBitmap width');
|
||||
} else {
|
||||
tensorConfig.width = width;
|
||||
}
|
||||
} else {
|
||||
tensorConfig.height = height;
|
||||
tensorConfig.width = width;
|
||||
}
|
||||
return Tensor.bufferToTensor(data, tensorConfig);
|
||||
} else {
|
||||
throw new Error('Can not access image data');
|
||||
}
|
||||
|
||||
} else if (isURL) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const canvas = document.createElement('canvas');
|
||||
const context = canvas.getContext('2d');
|
||||
if (!image || !context) {
|
||||
return reject();
|
||||
}
|
||||
const newImage = new Image();
|
||||
newImage.crossOrigin = 'Anonymous';
|
||||
newImage.src = image as string;
|
||||
newImage.onload = () => {
|
||||
canvas.width = newImage.width;
|
||||
canvas.height = newImage.height;
|
||||
context.drawImage(newImage, 0, 0, canvas.width, canvas.height);
|
||||
const img = context.getImageData(0, 0, canvas.width, canvas.height);
|
||||
if (options !== undefined) {
|
||||
// using square brackets to avoid TS error - type 'never'
|
||||
if (options.height !== undefined && options.height !== canvas.height) {
|
||||
throw new Error('Image input config height doesn\'t match ImageBitmap height');
|
||||
} else {
|
||||
tensorConfig.height = canvas.height;
|
||||
}
|
||||
// using square brackets to avoid TS error - type 'never'
|
||||
if (options.width !== undefined && options.width !== canvas.width) {
|
||||
throw new Error('Image input config width doesn\'t match ImageBitmap width');
|
||||
} else {
|
||||
tensorConfig.width = canvas.width;
|
||||
}
|
||||
} else {
|
||||
tensorConfig.height = canvas.height;
|
||||
tensorConfig.width = canvas.width;
|
||||
}
|
||||
resolve(Tensor.bufferToTensor(img.data, tensorConfig));
|
||||
};
|
||||
});
|
||||
} else {
|
||||
throw new Error('Input data provided is not supported - aborted tensor creation');
|
||||
}
|
||||
|
||||
if (data !== undefined) {
|
||||
return Tensor.bufferToTensor(data, tensorConfig);
|
||||
} else {
|
||||
throw new Error('Input data provided is not supported - aborted tensor creation');
|
||||
}
|
||||
}
|
||||
|
||||
toImageData(options?: TensorToImageDataOptions): ImageData {
|
||||
const pixels2DContext = document.createElement('canvas').getContext('2d');
|
||||
let image: ImageData;
|
||||
if (pixels2DContext != null) {
|
||||
// Default values for height and width & format
|
||||
const width = this.dims[3];
|
||||
const height = this.dims[2];
|
||||
const channels = this.dims[1];
|
||||
|
||||
const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB';
|
||||
const normMean = options !== undefined ? (options.norm?.mean !== undefined ? options.norm.mean : 255) : 255;
|
||||
const normBias = options !== undefined ? (options.norm?.bias !== undefined ? options.norm.bias : 0) : 0;
|
||||
const offset = height * width;
|
||||
|
||||
if (options !== undefined) {
|
||||
if (options.height !== undefined && options.height !== height) {
|
||||
throw new Error('Image output config height doesn\'t match tensor height');
|
||||
}
|
||||
if (options.width !== undefined && options.width !== width) {
|
||||
throw new Error('Image output config width doesn\'t match tensor width');
|
||||
}
|
||||
if (options.format !== undefined && (channels === 4 && options.format !== 'RGBA') ||
|
||||
(channels === 3 && (options.format !== 'RGB' && options.format !== 'BGR'))) {
|
||||
throw new Error('Tensor format doesn\'t match input tensor dims');
|
||||
}
|
||||
}
|
||||
|
||||
// Default pointer assignments
|
||||
const step = 4;
|
||||
let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
|
||||
let rTensorPointer = 0, gTensorPointer = offset, bTensorPointer = offset * 2, aTensorPointer = -1;
|
||||
|
||||
// Updating the pointer assignments based on the input image format
|
||||
if (inputformat === 'RGBA') {
|
||||
rTensorPointer = 0;
|
||||
gTensorPointer = offset;
|
||||
bTensorPointer = offset * 2;
|
||||
aTensorPointer = offset * 3;
|
||||
} else if (inputformat === 'RGB') {
|
||||
rTensorPointer = 0;
|
||||
gTensorPointer = offset;
|
||||
bTensorPointer = offset * 2;
|
||||
} else if (inputformat === 'RBG') {
|
||||
rTensorPointer = 0;
|
||||
bTensorPointer = offset;
|
||||
gTensorPointer = offset * 2;
|
||||
}
|
||||
|
||||
image = pixels2DContext.createImageData(width, height);
|
||||
|
||||
for (let i = 0; i < height * width;
|
||||
rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) {
|
||||
image.data[rImagePointer] = ((this.data[rTensorPointer++] as number) - normBias) * normMean; // R value
|
||||
image.data[gImagePointer] = ((this.data[gTensorPointer++] as number) - normBias) * normMean; // G value
|
||||
image.data[bImagePointer] = ((this.data[bTensorPointer++] as number) - normBias) * normMean; // B value
|
||||
image.data[aImagePointer] =
|
||||
aTensorPointer === -1 ? 255 : ((this.data[aTensorPointer++] as number) - normBias) * normMean; // A value
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new Error('Can not access image data');
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
// #region fields
|
||||
readonly dims: readonly number[];
|
||||
readonly type: TensorType;
|
||||
readonly data: TensorDataType;
|
||||
readonly size: number;
|
||||
// #endregion
|
||||
|
||||
// #region tensor utilities
|
||||
reshape(dims: readonly number[]): Tensor {
|
||||
return new Tensor(this.type, this.data, dims);
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
32
node_modules/onnxruntime-common/lib/tensor-utils.ts
generated
vendored
Normal file
32
node_modules/onnxruntime-common/lib/tensor-utils.ts
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {Tensor, TensorToImageDataOptions, TypedTensor} from './tensor';
|
||||
|
||||
interface Properties {
|
||||
/**
|
||||
* Get the number of elements in the tensor.
|
||||
*/
|
||||
readonly size: number;
|
||||
}
|
||||
|
||||
export interface TypedShapeUtils<T extends Tensor.Type> {
|
||||
/**
|
||||
* Create a new tensor with the same data buffer and specified dims.
|
||||
*
|
||||
* @param dims - New dimensions. Size should match the old one.
|
||||
*/
|
||||
reshape(dims: readonly number[]): TypedTensor<T>;
|
||||
}
|
||||
|
||||
// TODO: add more tensor utilities
|
||||
export interface TypedTensorUtils<T extends Tensor.Type> extends Properties, TypedShapeUtils<T> {
|
||||
/**
|
||||
* creates an ImageData instance from tensor
|
||||
*
|
||||
* @param tensorFormat - Interface describing tensor instance - Defaults: RGB, 3 channels, 0-255, NHWC
|
||||
* 0-255, NHWC
|
||||
* @returns An ImageData instance which can be used to draw on canvas
|
||||
*/
|
||||
toImageData(options?: TensorToImageDataOptions): ImageData;
|
||||
}
|
||||
357
node_modules/onnxruntime-common/lib/tensor.ts
generated
vendored
Normal file
357
node_modules/onnxruntime-common/lib/tensor.ts
generated
vendored
Normal file
@@ -0,0 +1,357 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import {Tensor as TensorImpl} from './tensor-impl';
|
||||
import {TypedTensorUtils} from './tensor-utils';
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-redeclare */
|
||||
|
||||
/**
|
||||
* represent a basic tensor with specified dimensions and data type.
|
||||
*/
|
||||
interface TypedTensorBase<T extends Tensor.Type> {
|
||||
/**
|
||||
* Get the dimensions of the tensor.
|
||||
*/
|
||||
readonly dims: readonly number[];
|
||||
/**
|
||||
* Get the data type of the tensor.
|
||||
*/
|
||||
readonly type: T;
|
||||
/**
|
||||
* Get the buffer data of the tensor.
|
||||
*/
|
||||
readonly data: Tensor.DataTypeMap[T];
|
||||
}
|
||||
|
||||
export declare namespace Tensor {
|
||||
interface DataTypeMap {
|
||||
float32: Float32Array;
|
||||
uint8: Uint8Array;
|
||||
int8: Int8Array;
|
||||
uint16: Uint16Array;
|
||||
int16: Int16Array;
|
||||
int32: Int32Array;
|
||||
int64: BigInt64Array;
|
||||
string: string[];
|
||||
bool: Uint8Array;
|
||||
float16: never; // hold on using Uint16Array before we have a concrete solution for float 16
|
||||
float64: Float64Array;
|
||||
uint32: Uint32Array;
|
||||
uint64: BigUint64Array;
|
||||
// complex64: never;
|
||||
// complex128: never;
|
||||
// bfloat16: never;
|
||||
}
|
||||
|
||||
interface ElementTypeMap {
|
||||
float32: number;
|
||||
uint8: number;
|
||||
int8: number;
|
||||
uint16: number;
|
||||
int16: number;
|
||||
int32: number;
|
||||
int64: bigint;
|
||||
string: string;
|
||||
bool: boolean;
|
||||
float16: never; // hold on before we have a concret solution for float 16
|
||||
float64: number;
|
||||
uint32: number;
|
||||
uint64: bigint;
|
||||
// complex64: never;
|
||||
// complex128: never;
|
||||
// bfloat16: never;
|
||||
}
|
||||
|
||||
type DataType = DataTypeMap[Type];
|
||||
type ElementType = ElementTypeMap[Type];
|
||||
|
||||
/**
|
||||
* represent the data type of a tensor
|
||||
*/
|
||||
export type Type = keyof DataTypeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
|
||||
*/
|
||||
export interface TypedTensor<T extends Tensor.Type> extends TypedTensorBase<T>, TypedTensorUtils<T> {}
|
||||
/**
|
||||
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
|
||||
*/
|
||||
export interface Tensor extends TypedTensorBase<Tensor.Type>, TypedTensorUtils<Tensor.Type> {}
|
||||
|
||||
export interface TensorConstructor {
|
||||
// #region specify element type
|
||||
/**
|
||||
* Construct a new string tensor object from the given type, data and dims.
|
||||
*
|
||||
* @param type - Specify the element type.
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(type: 'string', data: Tensor.DataTypeMap['string']|readonly string[],
|
||||
dims?: readonly number[]): TypedTensor<'string'>;
|
||||
|
||||
/**
|
||||
* Construct a new bool tensor object from the given type, data and dims.
|
||||
*
|
||||
* @param type - Specify the element type.
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(type: 'bool', data: Tensor.DataTypeMap['bool']|readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
|
||||
|
||||
/**
|
||||
* Construct a new numeric tensor object from the given type, data and dims.
|
||||
*
|
||||
* @param type - Specify the element type.
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new<T extends Exclude<Tensor.Type, 'string'|'bool'>>(
|
||||
type: T, data: Tensor.DataTypeMap[T]|readonly number[], dims?: readonly number[]): TypedTensor<T>;
|
||||
// #endregion
|
||||
|
||||
// #region infer element types
|
||||
|
||||
/**
|
||||
* Construct a new float32 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Float32Array, dims?: readonly number[]): TypedTensor<'float32'>;
|
||||
|
||||
/**
|
||||
* Construct a new int8 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Int8Array, dims?: readonly number[]): TypedTensor<'int8'>;
|
||||
|
||||
/**
|
||||
* Construct a new uint8 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Uint8Array, dims?: readonly number[]): TypedTensor<'uint8'>;
|
||||
|
||||
/**
|
||||
* Construct a new uint16 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Uint16Array, dims?: readonly number[]): TypedTensor<'uint16'>;
|
||||
|
||||
/**
|
||||
* Construct a new int16 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Int16Array, dims?: readonly number[]): TypedTensor<'int16'>;
|
||||
|
||||
/**
|
||||
* Construct a new int32 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Int32Array, dims?: readonly number[]): TypedTensor<'int32'>;
|
||||
|
||||
/**
|
||||
* Construct a new int64 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: BigInt64Array, dims?: readonly number[]): TypedTensor<'int64'>;
|
||||
|
||||
/**
|
||||
* Construct a new string tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
|
||||
|
||||
/**
|
||||
* Construct a new bool tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
|
||||
|
||||
/**
|
||||
* Construct a new float64 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Float64Array, dims?: readonly number[]): TypedTensor<'float64'>;
|
||||
|
||||
/**
|
||||
* Construct a new uint32 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Uint32Array, dims?: readonly number[]): TypedTensor<'uint32'>;
|
||||
|
||||
/**
|
||||
* Construct a new uint64 tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: BigUint64Array, dims?: readonly number[]): TypedTensor<'uint64'>;
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region fall back to non-generic tensor type declaration
|
||||
|
||||
/**
|
||||
* Construct a new tensor object from the given type, data and dims.
|
||||
*
|
||||
* @param type - Specify the element type.
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(type: Tensor.Type, data: Tensor.DataType|readonly number[]|readonly boolean[], dims?: readonly number[]): Tensor;
|
||||
|
||||
/**
|
||||
* Construct a new tensor object from the given data and dims.
|
||||
*
|
||||
* @param data - Specify the tensor data
|
||||
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
|
||||
*/
|
||||
new(data: Tensor.DataType, dims?: readonly number[]): Tensor;
|
||||
// #endregion
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the image format. Assume 'RGBA' if omitted.
|
||||
*/
|
||||
export type ImageFormat = 'RGB'|'RGBA'|'BGR'|'RBG';
|
||||
|
||||
/**
|
||||
* Describes Tensor configuration to an image data.
|
||||
*/
|
||||
export interface TensorToImageDataOptions {
|
||||
/**
|
||||
* Describes Tensor channels order.
|
||||
*/
|
||||
format?: ImageFormat;
|
||||
/**
|
||||
* Tensor channel layout - default is 'NHWC'
|
||||
*/
|
||||
tensorLayout?: 'NHWC'|'NCHW';
|
||||
/**
|
||||
* Describes Tensor Height - can be accessed via tensor dimensions as well
|
||||
*/
|
||||
height?: number;
|
||||
/**
|
||||
* Describes Tensor Width - can be accessed via tensor dimensions as well
|
||||
*/
|
||||
width?: number;
|
||||
/**
|
||||
* Describes normalization parameters to ImageData conversion from tensor - default values - Bias: 0, Mean: 255
|
||||
*/
|
||||
norm?: {
|
||||
bias?: number; // Todo add support - |[number,number,number]|[number,number,number,number];
|
||||
mean?: number; // Todo add support - |[number,number,number]|[number,number,number,number];
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Describes Tensor and Image configuration to an image data.
|
||||
*/
|
||||
export interface TensorFromImageOptions {
|
||||
/**
|
||||
* Describes image data format - will be used only in the case of ImageBitMap
|
||||
*/
|
||||
bitmapFormat?: ImageFormat;
|
||||
/**
|
||||
* Describes Tensor channels order - can differ from original image
|
||||
*/
|
||||
tensorFormat?: ImageFormat;
|
||||
/**
|
||||
* Tensor data type - default is 'float32'
|
||||
*/
|
||||
dataType?: 'float32'|'uint8';
|
||||
/**
|
||||
* Tensor channel layout - default is 'NHWC'
|
||||
*/
|
||||
tensorLayout?: 'NHWC'|'NCHW';
|
||||
/**
|
||||
* Describes Image Height - Required only in the case of ImageBitMap
|
||||
*/
|
||||
height?: number;
|
||||
/**
|
||||
* Describes Image Width - Required only in the case of ImageBitMap
|
||||
*/
|
||||
width?: number;
|
||||
/**
|
||||
* Describes resized height - can be accessed via tensor dimensions as well
|
||||
*/
|
||||
resizedHeight?: number;
|
||||
/**
|
||||
* Describes resized width - can be accessed via tensor dimensions as well
|
||||
*/
|
||||
resizedWidth?: number;
|
||||
/**
|
||||
* Describes normalization parameters to tensor conversion from image data - default values - Bias: 0, Mean: 255
|
||||
*/
|
||||
norm?: {
|
||||
bias?: number; // Todo add support - |[number,number,number]|[number,number,number,number];
|
||||
mean?: number; // Todo add support - |[number,number,number]|[number,number,number,number];
|
||||
};
|
||||
}
|
||||
export interface TensorFactory {
|
||||
/**
|
||||
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
|
||||
*
|
||||
* @param imageData - {ImageData} - composed of: Uint8ClampedArray, width. height - uses known pixel format RGBA
|
||||
* @param options - Optional - Interface describing input image & output tensor -
|
||||
* Input Defaults: RGBA, 3 channels, 0-255, NHWC - Output Defaults: same as input parameters
|
||||
* @returns A promise that resolves to a tensor object
|
||||
*/
|
||||
fromImage(imageData: ImageData, options?: TensorFromImageOptions): Promise<Tensor>;
|
||||
|
||||
/**
|
||||
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
|
||||
*
|
||||
* @param imageElement - {HTMLImageElement} - since the data is stored as ImageData no need for format parameter
|
||||
* @param options - Optional - Interface describing input image & output tensor -
|
||||
* Input Defaults: RGBA, 3 channels, 0-255, NHWC - Output Defaults: same as input parameters
|
||||
* @returns A promise that resolves to a tensor object
|
||||
*/
|
||||
fromImage(imageElement: HTMLImageElement, options?: TensorFromImageOptions): Promise<Tensor>;
|
||||
|
||||
/**
|
||||
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
|
||||
*
|
||||
* @param url - {string} - Assuming the string is a URL to an image
|
||||
* @param options - Optional - Interface describing input image & output tensor -
|
||||
* Input Defaults: RGBA, 3 channels, 0-255, NHWC - Output Defaults: same as input parameters
|
||||
* @returns A promise that resolves to a tensor object
|
||||
*/
|
||||
fromImage(url: string, options?: TensorFromImageOptions): Promise<Tensor>;
|
||||
|
||||
/**
|
||||
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
|
||||
*
|
||||
* @param bitMap - {ImageBitmap} - since the data is stored as ImageData no need for format parameter
|
||||
* @param options - NOT Optional - Interface describing input image & output tensor -
|
||||
* Output Defaults: same as input parameters
|
||||
* @returns A promise that resolves to a tensor object
|
||||
*/
|
||||
fromImage(bitmap: ImageBitmap, options: TensorFromImageOptions): Promise<Tensor>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
export const Tensor = TensorImpl as TensorConstructor;
|
||||
Reference in New Issue
Block a user