tensorflow-helpers
Version:
Helper functions to use tensorflow in nodejs for transfer learning, image classification, and more
80 lines (79 loc) • 2.5 kB
TypeScript
import * as tf from '@tensorflow/tfjs';
import { CropAndResizeAspectRatio, ImageTensor } from '../image-utils';
import { ImageModelSpec } from '../image-model';
/**
* @example `loadGraphModel({ url: 'saved_model/mobilenet-v3-large-100' })`
*/
export declare function loadGraphModel(options: {
url: string;
classNames?: string[];
}): Promise<tf.GraphModel<string | tf.io.IOHandler> & {
classNames?: string[];
}>;
/**
* @example `loadGraphModel({ url: 'saved_model/emotion-classifier' })`
*/
export declare function loadLayersModel(options: {
url: string;
classNames?: string[];
}): Promise<tf.LayersModel & {
classNames?: string[];
}>;
/**
* @example ```
* cachedLoadGraphModel({
* url: 'saved_model/mobilenet-v3-large-100',
* cacheUrl: 'indexeddb://mobilenet-v3-large-100',
* })
* ```
*/
export declare function cachedLoadGraphModel(options: {
url: string;
cacheUrl: string;
checkForUpdates?: boolean;
classNames?: string[];
}): Promise<tf.GraphModel<string | tf.io.IOHandler> & {
classNames?: string[];
}>;
/**
* @example ```
* cachedLoadLayersModel({
* url: 'saved_model/emotion-classifier',
* cacheUrl: 'indexeddb://emotion-classifier',
* })
* ```
*/
export declare function cachedLoadLayersModel(options: {
url: string;
cacheUrl: string;
checkForUpdates?: boolean;
classNames?: string[];
}): Promise<tf.LayersModel & {
classNames?: string[];
}>;
export type ImageModel = Awaited<ReturnType<typeof loadImageModel>>;
/**
* @description cache image embedding keyed by filename.
* The dirname is ignored.
* The filename is expected to be content hash (w/wo extname)
*/
export type EmbeddingCache = {
get(url: string): number[] | null | undefined;
set(url: string, values: number[]): void;
};
export declare function loadImageModel<Cache extends EmbeddingCache>(options: {
url: string;
cacheUrl?: string;
checkForUpdates?: boolean;
aspectRatio?: CropAndResizeAspectRatio;
cache?: Cache | boolean;
}): Promise<{
spec: ImageModelSpec;
model: tf.GraphModel<string | tf.io.IOHandler>;
fileEmbeddingCache: Map<string, tf.Tensor<tf.Rank>> | null;
checkCache: (url: string) => tf.Tensor | void;
loadImageCropped: (url: string) => Promise<tf.Tensor4D & tf.Tensor<tf.Rank>>;
imageUrlToEmbedding: (url: string) => Promise<tf.Tensor>;
imageFileToEmbedding: (file: File) => Promise<tf.Tensor>;
imageTensorToEmbedding: (imageTensor: ImageTensor) => tf.Tensor;
}>;