tfjs-image-recognition-base
Advanced tools
Comparing version 0.1.3 to 0.2.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var util_1 = require("util"); | ||
var utils_1 = require("../utils"); | ||
var Dimensions = /** @class */ (function () { | ||
function Dimensions(width, height) { | ||
if (!util_1.isNumber(width) || !util_1.isNumber(height)) { | ||
if (!utils_1.isValidNumber(width) || !utils_1.isValidNumber(height)) { | ||
throw new Error("Dimensions.constructor - expected width and height to be valid numbers, instead have " + JSON.stringify({ width: width, height: height })); | ||
@@ -8,0 +8,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var env_1 = require("../env"); | ||
var isMediaLoaded_1 = require("./isMediaLoaded"); | ||
function awaitMediaLoaded(media) { | ||
return new Promise(function (resolve, reject) { | ||
if (media instanceof HTMLCanvasElement || isMediaLoaded_1.isMediaLoaded(media)) { | ||
if (media instanceof env_1.env.getEnv().Canvas || isMediaLoaded_1.isMediaLoaded(media)) { | ||
return resolve(); | ||
@@ -8,0 +9,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var env_1 = require("../env"); | ||
function bufferToImage(buf) { | ||
@@ -13,3 +14,3 @@ return new Promise(function (resolve, reject) { | ||
} | ||
var img = new Image(); | ||
var img = env_1.env.getEnv().createImageElement(); | ||
img.onload = function () { return resolve(img); }; | ||
@@ -16,0 +17,0 @@ img.onerror = reject; |
import { IDimensions } from '../classes/Dimensions'; | ||
export declare function createCanvas({ width, height }: IDimensions): HTMLCanvasElement; | ||
export declare function createCanvasFromMedia(media: HTMLImageElement | HTMLVideoElement, dims?: IDimensions): HTMLCanvasElement; | ||
export declare function createCanvas({width, height}: IDimensions): HTMLCanvasElement; | ||
export declare function createCanvasFromMedia(media: HTMLImageElement | HTMLVideoElement | ImageData, dims?: IDimensions): HTMLCanvasElement; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var env_1 = require("../env"); | ||
var getContext2dOrThrow_1 = require("./getContext2dOrThrow"); | ||
@@ -8,3 +9,4 @@ var getMediaDimensions_1 = require("./getMediaDimensions"); | ||
var width = _a.width, height = _a.height; | ||
var canvas = document.createElement('canvas'); | ||
var createCanvasElement = env_1.env.getEnv().createCanvasElement; | ||
var canvas = createCanvasElement(); | ||
canvas.width = width; | ||
@@ -16,3 +18,4 @@ canvas.height = height; | ||
function createCanvasFromMedia(media, dims) { | ||
if (!isMediaLoaded_1.isMediaLoaded(media)) { | ||
var ImageData = env_1.env.getEnv().ImageData; | ||
if (!(media instanceof ImageData) && !isMediaLoaded_1.isMediaLoaded(media)) { | ||
throw new Error('createCanvasFromMedia - media has not finished loading yet'); | ||
@@ -22,3 +25,8 @@ } | ||
var canvas = createCanvas({ width: width, height: height }); | ||
getContext2dOrThrow_1.getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height); | ||
if (media instanceof ImageData) { | ||
getContext2dOrThrow_1.getContext2dOrThrow(canvas).putImageData(media, 0, 0); | ||
} | ||
else { | ||
getContext2dOrThrow_1.getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height); | ||
} | ||
return canvas; | ||
@@ -25,0 +33,0 @@ } |
@@ -6,2 +6,3 @@ "use strict"; | ||
var PredictedBox_1 = require("../classes/PredictedBox"); | ||
var env_1 = require("../env"); | ||
var utils_1 = require("../utils"); | ||
@@ -14,4 +15,5 @@ var drawBox_1 = require("./drawBox"); | ||
function drawDetection(canvasArg, detection, options) { | ||
var Canvas = env_1.env.getEnv().Canvas; | ||
var canvas = resolveInput_1.resolveInput(canvasArg); | ||
if (!(canvas instanceof HTMLCanvasElement)) { | ||
if (!(canvas instanceof Canvas)) { | ||
throw new Error('drawDetection - expected canvas to be of type: HTMLCanvasElement'); | ||
@@ -18,0 +20,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var env_1 = require("../env"); | ||
function fetchOrThrow(url, init) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var res; | ||
var fetch, res; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, fetch(url, init)]; | ||
case 0: | ||
fetch = env_1.env.getEnv().fetch; | ||
return [4 /*yield*/, fetch(url, init)]; | ||
case 1: | ||
@@ -11,0 +14,0 @@ res = _a.sent(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Dimensions_1 = require("../classes/Dimensions"); | ||
var env_1 = require("../env"); | ||
function getMediaDimensions(input) { | ||
if (input instanceof HTMLImageElement) { | ||
var _a = env_1.env.getEnv(), Image = _a.Image, Video = _a.Video; | ||
if (input instanceof Image) { | ||
return new Dimensions_1.Dimensions(input.naturalWidth, input.naturalHeight); | ||
} | ||
if (input instanceof HTMLVideoElement) { | ||
if (input instanceof Video) { | ||
return new Dimensions_1.Dimensions(input.videoWidth, input.videoHeight); | ||
@@ -10,0 +12,0 @@ } |
@@ -5,2 +5,3 @@ "use strict"; | ||
var tf = require("@tensorflow/tfjs-core"); | ||
var env_1 = require("../env"); | ||
var utils_1 = require("../utils"); | ||
@@ -13,3 +14,3 @@ function imageTensorToCanvas(imgTensor, canvas) { | ||
case 0: | ||
targetCanvas = canvas || document.createElement('canvas'); | ||
targetCanvas = canvas || env_1.env.getEnv().createCanvasElement(); | ||
_a = imgTensor.shape.slice(utils_1.isTensor4D(imgTensor) ? 1 : 0), height = _a[0], width = _a[1], numChannels = _a[2]; | ||
@@ -16,0 +17,0 @@ imgTensor3D = tf.tidy(function () { return imgTensor.as3D(height, width, numChannels).toInt(); }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var env_1 = require("../env"); | ||
var createCanvas_1 = require("./createCanvas"); | ||
@@ -8,3 +9,4 @@ var getContext2dOrThrow_1 = require("./getContext2dOrThrow"); | ||
if (centerImage === void 0) { centerImage = false; } | ||
if (!(input instanceof HTMLImageElement || input instanceof HTMLCanvasElement)) { | ||
var _a = env_1.env.getEnv(), Image = _a.Image, Canvas = _a.Canvas; | ||
if (!(input instanceof Image || input instanceof Canvas)) { | ||
throw new Error('imageToSquare - expected arg0 to be HTMLImageElement | HTMLCanvasElement'); | ||
@@ -17,3 +19,3 @@ } | ||
var targetCanvas = createCanvas_1.createCanvas({ width: inputSize, height: inputSize }); | ||
var inputCanvas = input instanceof HTMLCanvasElement ? input : createCanvas_1.createCanvasFromMedia(input); | ||
var inputCanvas = input instanceof Canvas ? input : createCanvas_1.createCanvasFromMedia(input); | ||
var offset = Math.abs(width - height) / 2; | ||
@@ -20,0 +22,0 @@ var dx = centerImage && width < height ? offset : 0; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var env_1 = require("../env"); | ||
function isMediaElement(input) { | ||
return input instanceof HTMLImageElement | ||
|| input instanceof HTMLVideoElement | ||
|| input instanceof HTMLCanvasElement; | ||
var _a = env_1.env.getEnv(), Image = _a.Image, Canvas = _a.Canvas, Video = _a.Video; | ||
return input instanceof Image | ||
|| input instanceof Canvas | ||
|| input instanceof Video; | ||
} | ||
exports.isMediaElement = isMediaElement; | ||
//# sourceMappingURL=isMediaElement.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var env_1 = require("../env"); | ||
function isMediaLoaded(media) { | ||
return (media instanceof HTMLImageElement && media.complete) | ||
|| (media instanceof HTMLVideoElement && media.readyState >= 3); | ||
var _a = env_1.env.getEnv(), Image = _a.Image, Video = _a.Video; | ||
return (media instanceof Image && media.complete) | ||
|| (media instanceof Video && media.readyState >= 3); | ||
} | ||
exports.isMediaLoaded = isMediaLoaded; | ||
//# sourceMappingURL=isMediaLoaded.js.map |
@@ -1,1 +0,2 @@ | ||
export declare function loadWeightMap(uri: string | undefined, defaultModelName: string): Promise<any>; | ||
import * as tf from '@tensorflow/tfjs-core'; | ||
export declare function loadWeightMap(uri: string | undefined, defaultModelName: string): Promise<tf.NamedTensorMap>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tf = require("@tensorflow/tfjs-core"); | ||
var env_1 = require("../env"); | ||
var padToSquare_1 = require("../ops/padToSquare"); | ||
@@ -36,3 +37,3 @@ var utils_1 = require("../utils"); | ||
} | ||
var canvas = input instanceof HTMLCanvasElement ? input : createCanvas_1.createCanvasFromMedia(input); | ||
var canvas = input instanceof env_1.env.getEnv().Canvas ? input : createCanvas_1.createCanvasFromMedia(input); | ||
_this._canvases[idx] = canvas; | ||
@@ -136,3 +137,3 @@ _this._inputDimensions[idx] = [canvas.height, canvas.width, 3]; | ||
} | ||
if (input instanceof HTMLCanvasElement) { | ||
if (input instanceof env_1.env.getEnv().Canvas) { | ||
return tf.fromPixels(imageToSquare_1.imageToSquare(input, inputSize, isCenterInputs)); | ||
@@ -139,0 +140,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var env_1 = require("../env"); | ||
function resolveInput(arg) { | ||
if (typeof arg === 'string') { | ||
if (!env_1.env.isNodejs() && typeof arg === 'string') { | ||
return document.getElementById(arg); | ||
@@ -6,0 +7,0 @@ } |
export * from './classes'; | ||
export * from './common'; | ||
export * from './dom'; | ||
export * from './env'; | ||
export * from './ops'; | ||
export * from './utils'; | ||
export * from './NeuralNetwork'; |
@@ -7,2 +7,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./dom"), exports); | ||
tslib_1.__exportStar(require("./env"), exports); | ||
tslib_1.__exportStar(require("./ops"), exports); | ||
@@ -9,0 +10,0 @@ tslib_1.__exportStar(require("./utils"), exports); |
import * as tf from '@tensorflow/tfjs-core'; | ||
import { ParamMapping } from './common'; | ||
export declare class NeuralNetwork<TNetParams> { | ||
export declare abstract class NeuralNetwork<TNetParams> { | ||
private _name; | ||
@@ -30,9 +30,13 @@ protected _params: TNetParams | undefined; | ||
load(weightsOrUrl: Float32Array | string | undefined): Promise<void>; | ||
loadFromUri(uri: string | undefined): Promise<void>; | ||
loadFromDisk(filePath: string | undefined): Promise<void>; | ||
loadFromWeightMap(weightMap: tf.NamedTensorMap): void; | ||
extractWeights(weights: Float32Array): void; | ||
private traversePropertyPath; | ||
protected loadQuantizedParams(_: any): Promise<{ | ||
private traversePropertyPath(paramPath); | ||
protected abstract getDefaultModelName(): string; | ||
protected abstract extractParamsFromWeigthMap(weightMap: tf.NamedTensorMap): { | ||
params: TNetParams; | ||
paramMappings: ParamMapping[]; | ||
}>; | ||
protected extractParams(_: any): { | ||
}; | ||
protected abstract extractParams(weights: Float32Array): { | ||
params: TNetParams; | ||
@@ -39,0 +43,0 @@ paramMappings: ParamMapping[]; |
@@ -5,2 +5,6 @@ "use strict"; | ||
var tf = require("@tensorflow/tfjs-core"); | ||
var getModelUris_1 = require("./common/getModelUris"); | ||
var dom_1 = require("./dom"); | ||
var env_1 = require("./env"); | ||
var weightsLoaderFactory_1 = require("./weightsLoaderFactory"); | ||
var NeuralNetwork = /** @class */ (function () { | ||
@@ -88,5 +92,4 @@ function NeuralNetwork(_name) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var _a, paramMappings, params; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
@@ -97,10 +100,23 @@ if (weightsOrUrl instanceof Float32Array) { | ||
} | ||
if (weightsOrUrl && typeof weightsOrUrl !== 'string') { | ||
throw new Error(this._name + ".load - expected model uri, or weights as Float32Array"); | ||
return [4 /*yield*/, this.loadFromUri(weightsOrUrl)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
NeuralNetwork.prototype.loadFromUri = function (uri) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var weightMap; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (uri && typeof uri !== 'string') { | ||
throw new Error(this._name + ".loadFromUri - expected model uri"); | ||
} | ||
return [4 /*yield*/, this.loadQuantizedParams(weightsOrUrl)]; | ||
return [4 /*yield*/, dom_1.loadWeightMap(uri, this.getDefaultModelName())]; | ||
case 1: | ||
_a = _b.sent(), paramMappings = _a.paramMappings, params = _a.params; | ||
this._paramMappings = paramMappings; | ||
this._params = params; | ||
weightMap = _a.sent(); | ||
this.loadFromWeightMap(weightMap); | ||
return [2 /*return*/]; | ||
@@ -111,2 +127,33 @@ } | ||
}; | ||
NeuralNetwork.prototype.loadFromDisk = function (filePath) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var readFile, _a, manifestUri, modelBaseUri, fetchWeightsFromDisk, loadWeights, manifest, _b, _c, weightMap; | ||
return tslib_1.__generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
if (filePath && typeof filePath !== 'string') { | ||
throw new Error(this._name + ".loadFromDisk - expected model file path"); | ||
} | ||
readFile = env_1.env.getEnv().readFile; | ||
_a = getModelUris_1.getModelUris(filePath, this.getDefaultModelName()), manifestUri = _a.manifestUri, modelBaseUri = _a.modelBaseUri; | ||
fetchWeightsFromDisk = function (filePaths) { return Promise.all(filePaths.map(function (filePath) { return readFile(filePath).then(function (buf) { return buf.buffer; }); })); }; | ||
loadWeights = weightsLoaderFactory_1.weightsLoaderFactory(fetchWeightsFromDisk); | ||
_c = (_b = JSON).parse; | ||
return [4 /*yield*/, readFile(manifestUri)]; | ||
case 1: | ||
manifest = _c.apply(_b, [(_d.sent()).toString()]); | ||
return [4 /*yield*/, loadWeights(manifest, modelBaseUri)]; | ||
case 2: | ||
weightMap = _d.sent(); | ||
this.loadFromWeightMap(weightMap); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
NeuralNetwork.prototype.loadFromWeightMap = function (weightMap) { | ||
var _a = this.extractParamsFromWeigthMap(weightMap), paramMappings = _a.paramMappings, params = _a.params; | ||
this._paramMappings = paramMappings; | ||
this._params = params; | ||
}; | ||
NeuralNetwork.prototype.extractWeights = function (weights) { | ||
@@ -133,8 +180,2 @@ var _a = this.extractParams(weights), paramMappings = _a.paramMappings, params = _a.params; | ||
}; | ||
NeuralNetwork.prototype.loadQuantizedParams = function (_) { | ||
throw new Error(this._name + ".loadQuantizedParams - not implemented"); | ||
}; | ||
NeuralNetwork.prototype.extractParams = function (_) { | ||
throw new Error(this._name + ".extractParams - not implemented"); | ||
}; | ||
return NeuralNetwork; | ||
@@ -141,0 +182,0 @@ }()); |
@@ -37,3 +37,4 @@ "use strict"; | ||
] | ||
.filter(function (t) { return t !== null; }); | ||
.filter(function (t) { return !!t; }) | ||
.map(function (t) { return t.toFloat(); }); | ||
return tf.concat(tensorsToStack, paddingAxis); | ||
@@ -40,0 +41,0 @@ }); |
@@ -13,3 +13,3 @@ import * as tf from '@tensorflow/tfjs-core'; | ||
export declare function isDimensions(obj: any): boolean; | ||
export declare function computeReshapedDimensions({ width, height }: IDimensions, inputSize: number): Dimensions; | ||
export declare function computeReshapedDimensions({width, height}: IDimensions, inputSize: number): Dimensions; | ||
export declare function getCenterPoint(pts: Point[]): Point; | ||
@@ -16,0 +16,0 @@ export declare function range(num: number, start: number, step: number): number[]; |
@@ -1,5 +0,5 @@ | ||
import { isNumber } from 'util'; | ||
import { isValidNumber } from '../utils'; | ||
var Dimensions = /** @class */ (function () { | ||
function Dimensions(width, height) { | ||
if (!isNumber(width) || !isNumber(height)) { | ||
if (!isValidNumber(width) || !isValidNumber(height)) { | ||
throw new Error("Dimensions.constructor - expected width and height to be valid numbers, instead have " + JSON.stringify({ width: width, height: height })); | ||
@@ -6,0 +6,0 @@ } |
@@ -0,5 +1,6 @@ | ||
import { env } from '../env'; | ||
import { isMediaLoaded } from './isMediaLoaded'; | ||
export function awaitMediaLoaded(media) { | ||
return new Promise(function (resolve, reject) { | ||
if (media instanceof HTMLCanvasElement || isMediaLoaded(media)) { | ||
if (media instanceof env.getEnv().Canvas || isMediaLoaded(media)) { | ||
return resolve(); | ||
@@ -6,0 +7,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import { env } from '../env'; | ||
export function bufferToImage(buf) { | ||
@@ -11,3 +12,3 @@ return new Promise(function (resolve, reject) { | ||
} | ||
var img = new Image(); | ||
var img = env.getEnv().createImageElement(); | ||
img.onload = function () { return resolve(img); }; | ||
@@ -14,0 +15,0 @@ img.onerror = reject; |
import { IDimensions } from '../classes/Dimensions'; | ||
export declare function createCanvas({ width, height }: IDimensions): HTMLCanvasElement; | ||
export declare function createCanvasFromMedia(media: HTMLImageElement | HTMLVideoElement, dims?: IDimensions): HTMLCanvasElement; | ||
export declare function createCanvas({width, height}: IDimensions): HTMLCanvasElement; | ||
export declare function createCanvasFromMedia(media: HTMLImageElement | HTMLVideoElement | ImageData, dims?: IDimensions): HTMLCanvasElement; |
@@ -0,1 +1,2 @@ | ||
import { env } from '../env'; | ||
import { getContext2dOrThrow } from './getContext2dOrThrow'; | ||
@@ -6,3 +7,4 @@ import { getMediaDimensions } from './getMediaDimensions'; | ||
var width = _a.width, height = _a.height; | ||
var canvas = document.createElement('canvas'); | ||
var createCanvasElement = env.getEnv().createCanvasElement; | ||
var canvas = createCanvasElement(); | ||
canvas.width = width; | ||
@@ -13,3 +15,4 @@ canvas.height = height; | ||
export function createCanvasFromMedia(media, dims) { | ||
if (!isMediaLoaded(media)) { | ||
var ImageData = env.getEnv().ImageData; | ||
if (!(media instanceof ImageData) && !isMediaLoaded(media)) { | ||
throw new Error('createCanvasFromMedia - media has not finished loading yet'); | ||
@@ -19,5 +22,10 @@ } | ||
var canvas = createCanvas({ width: width, height: height }); | ||
getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height); | ||
if (media instanceof ImageData) { | ||
getContext2dOrThrow(canvas).putImageData(media, 0, 0); | ||
} | ||
else { | ||
getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height); | ||
} | ||
return canvas; | ||
} | ||
//# sourceMappingURL=createCanvas.js.map |
import { BoxWithText } from '../classes/BoxWithText'; | ||
import { ObjectDetection } from '../classes/ObjectDetection'; | ||
import { PredictedBox } from '../classes/PredictedBox'; | ||
import { env } from '../env'; | ||
import { round } from '../utils'; | ||
@@ -11,4 +12,5 @@ import { drawBox } from './drawBox'; | ||
export function drawDetection(canvasArg, detection, options) { | ||
var Canvas = env.getEnv().Canvas; | ||
var canvas = resolveInput(canvasArg); | ||
if (!(canvas instanceof HTMLCanvasElement)) { | ||
if (!(canvas instanceof Canvas)) { | ||
throw new Error('drawDetection - expected canvas to be of type: HTMLCanvasElement'); | ||
@@ -15,0 +17,0 @@ } |
import * as tslib_1 from "tslib"; | ||
import { env } from '../env'; | ||
export function fetchOrThrow(url, init) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var res; | ||
var fetch, res; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, fetch(url, init)]; | ||
case 0: | ||
fetch = env.getEnv().fetch; | ||
return [4 /*yield*/, fetch(url, init)]; | ||
case 1: | ||
@@ -9,0 +12,0 @@ res = _a.sent(); |
import { Dimensions } from '../classes/Dimensions'; | ||
import { env } from '../env'; | ||
export function getMediaDimensions(input) { | ||
if (input instanceof HTMLImageElement) { | ||
var _a = env.getEnv(), Image = _a.Image, Video = _a.Video; | ||
if (input instanceof Image) { | ||
return new Dimensions(input.naturalWidth, input.naturalHeight); | ||
} | ||
if (input instanceof HTMLVideoElement) { | ||
if (input instanceof Video) { | ||
return new Dimensions(input.videoWidth, input.videoHeight); | ||
@@ -8,0 +10,0 @@ } |
import * as tslib_1 from "tslib"; | ||
import * as tf from '@tensorflow/tfjs-core'; | ||
import { env } from '../env'; | ||
import { isTensor4D } from '../utils'; | ||
@@ -10,3 +11,3 @@ export function imageTensorToCanvas(imgTensor, canvas) { | ||
case 0: | ||
targetCanvas = canvas || document.createElement('canvas'); | ||
targetCanvas = canvas || env.getEnv().createCanvasElement(); | ||
_a = imgTensor.shape.slice(isTensor4D(imgTensor) ? 1 : 0), height = _a[0], width = _a[1], numChannels = _a[2]; | ||
@@ -13,0 +14,0 @@ imgTensor3D = tf.tidy(function () { return imgTensor.as3D(height, width, numChannels).toInt(); }); |
@@ -0,1 +1,2 @@ | ||
import { env } from '../env'; | ||
import { createCanvas, createCanvasFromMedia } from './createCanvas'; | ||
@@ -6,3 +7,4 @@ import { getContext2dOrThrow } from './getContext2dOrThrow'; | ||
if (centerImage === void 0) { centerImage = false; } | ||
if (!(input instanceof HTMLImageElement || input instanceof HTMLCanvasElement)) { | ||
var _a = env.getEnv(), Image = _a.Image, Canvas = _a.Canvas; | ||
if (!(input instanceof Image || input instanceof Canvas)) { | ||
throw new Error('imageToSquare - expected arg0 to be HTMLImageElement | HTMLCanvasElement'); | ||
@@ -15,3 +17,3 @@ } | ||
var targetCanvas = createCanvas({ width: inputSize, height: inputSize }); | ||
var inputCanvas = input instanceof HTMLCanvasElement ? input : createCanvasFromMedia(input); | ||
var inputCanvas = input instanceof Canvas ? input : createCanvasFromMedia(input); | ||
var offset = Math.abs(width - height) / 2; | ||
@@ -18,0 +20,0 @@ var dx = centerImage && width < height ? offset : 0; |
@@ -0,6 +1,8 @@ | ||
import { env } from '../env'; | ||
export function isMediaElement(input) { | ||
return input instanceof HTMLImageElement | ||
|| input instanceof HTMLVideoElement | ||
|| input instanceof HTMLCanvasElement; | ||
var _a = env.getEnv(), Image = _a.Image, Canvas = _a.Canvas, Video = _a.Video; | ||
return input instanceof Image | ||
|| input instanceof Canvas | ||
|| input instanceof Video; | ||
} | ||
//# sourceMappingURL=isMediaElement.js.map |
@@ -0,5 +1,7 @@ | ||
import { env } from '../env'; | ||
export function isMediaLoaded(media) { | ||
return (media instanceof HTMLImageElement && media.complete) | ||
|| (media instanceof HTMLVideoElement && media.readyState >= 3); | ||
var _a = env.getEnv(), Image = _a.Image, Video = _a.Video; | ||
return (media instanceof Image && media.complete) | ||
|| (media instanceof Video && media.readyState >= 3); | ||
} | ||
//# sourceMappingURL=isMediaLoaded.js.map |
@@ -1,1 +0,2 @@ | ||
export declare function loadWeightMap(uri: string | undefined, defaultModelName: string): Promise<any>; | ||
import * as tf from '@tensorflow/tfjs-core'; | ||
export declare function loadWeightMap(uri: string | undefined, defaultModelName: string): Promise<tf.NamedTensorMap>; |
import * as tf from '@tensorflow/tfjs-core'; | ||
import { env } from '../env'; | ||
import { padToSquare } from '../ops/padToSquare'; | ||
@@ -34,3 +35,3 @@ import { computeReshapedDimensions, isTensor3D, isTensor4D, range } from '../utils'; | ||
} | ||
var canvas = input instanceof HTMLCanvasElement ? input : createCanvasFromMedia(input); | ||
var canvas = input instanceof env.getEnv().Canvas ? input : createCanvasFromMedia(input); | ||
_this._canvases[idx] = canvas; | ||
@@ -134,3 +135,3 @@ _this._inputDimensions[idx] = [canvas.height, canvas.width, 3]; | ||
} | ||
if (input instanceof HTMLCanvasElement) { | ||
if (input instanceof env.getEnv().Canvas) { | ||
return tf.fromPixels(imageToSquare(input, inputSize, isCenterInputs)); | ||
@@ -137,0 +138,0 @@ } |
@@ -0,3 +1,4 @@ | ||
import { env } from '../env'; | ||
export function resolveInput(arg) { | ||
if (typeof arg === 'string') { | ||
if (!env.isNodejs() && typeof arg === 'string') { | ||
return document.getElementById(arg); | ||
@@ -4,0 +5,0 @@ } |
export * from './classes'; | ||
export * from './common'; | ||
export * from './dom'; | ||
export * from './env'; | ||
export * from './ops'; | ||
export * from './utils'; | ||
export * from './NeuralNetwork'; |
export * from './classes'; | ||
export * from './common'; | ||
export * from './dom'; | ||
export * from './env'; | ||
export * from './ops'; | ||
@@ -5,0 +6,0 @@ export * from './utils'; |
import * as tf from '@tensorflow/tfjs-core'; | ||
import { ParamMapping } from './common'; | ||
export declare class NeuralNetwork<TNetParams> { | ||
export declare abstract class NeuralNetwork<TNetParams> { | ||
private _name; | ||
@@ -30,9 +30,13 @@ protected _params: TNetParams | undefined; | ||
load(weightsOrUrl: Float32Array | string | undefined): Promise<void>; | ||
loadFromUri(uri: string | undefined): Promise<void>; | ||
loadFromDisk(filePath: string | undefined): Promise<void>; | ||
loadFromWeightMap(weightMap: tf.NamedTensorMap): void; | ||
extractWeights(weights: Float32Array): void; | ||
private traversePropertyPath; | ||
protected loadQuantizedParams(_: any): Promise<{ | ||
private traversePropertyPath(paramPath); | ||
protected abstract getDefaultModelName(): string; | ||
protected abstract extractParamsFromWeigthMap(weightMap: tf.NamedTensorMap): { | ||
params: TNetParams; | ||
paramMappings: ParamMapping[]; | ||
}>; | ||
protected extractParams(_: any): { | ||
}; | ||
protected abstract extractParams(weights: Float32Array): { | ||
params: TNetParams; | ||
@@ -39,0 +43,0 @@ paramMappings: ParamMapping[]; |
import * as tslib_1 from "tslib"; | ||
import * as tf from '@tensorflow/tfjs-core'; | ||
import { getModelUris } from './common/getModelUris'; | ||
import { loadWeightMap } from './dom'; | ||
import { env } from './env'; | ||
import { weightsLoaderFactory } from './weightsLoaderFactory'; | ||
var NeuralNetwork = /** @class */ (function () { | ||
@@ -85,5 +89,4 @@ function NeuralNetwork(_name) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var _a, paramMappings, params; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
@@ -94,10 +97,23 @@ if (weightsOrUrl instanceof Float32Array) { | ||
} | ||
if (weightsOrUrl && typeof weightsOrUrl !== 'string') { | ||
throw new Error(this._name + ".load - expected model uri, or weights as Float32Array"); | ||
return [4 /*yield*/, this.loadFromUri(weightsOrUrl)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
NeuralNetwork.prototype.loadFromUri = function (uri) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var weightMap; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (uri && typeof uri !== 'string') { | ||
throw new Error(this._name + ".loadFromUri - expected model uri"); | ||
} | ||
return [4 /*yield*/, this.loadQuantizedParams(weightsOrUrl)]; | ||
return [4 /*yield*/, loadWeightMap(uri, this.getDefaultModelName())]; | ||
case 1: | ||
_a = _b.sent(), paramMappings = _a.paramMappings, params = _a.params; | ||
this._paramMappings = paramMappings; | ||
this._params = params; | ||
weightMap = _a.sent(); | ||
this.loadFromWeightMap(weightMap); | ||
return [2 /*return*/]; | ||
@@ -108,2 +124,33 @@ } | ||
}; | ||
NeuralNetwork.prototype.loadFromDisk = function (filePath) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var readFile, _a, manifestUri, modelBaseUri, fetchWeightsFromDisk, loadWeights, manifest, _b, _c, weightMap; | ||
return tslib_1.__generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
if (filePath && typeof filePath !== 'string') { | ||
throw new Error(this._name + ".loadFromDisk - expected model file path"); | ||
} | ||
readFile = env.getEnv().readFile; | ||
_a = getModelUris(filePath, this.getDefaultModelName()), manifestUri = _a.manifestUri, modelBaseUri = _a.modelBaseUri; | ||
fetchWeightsFromDisk = function (filePaths) { return Promise.all(filePaths.map(function (filePath) { return readFile(filePath).then(function (buf) { return buf.buffer; }); })); }; | ||
loadWeights = weightsLoaderFactory(fetchWeightsFromDisk); | ||
_c = (_b = JSON).parse; | ||
return [4 /*yield*/, readFile(manifestUri)]; | ||
case 1: | ||
manifest = _c.apply(_b, [(_d.sent()).toString()]); | ||
return [4 /*yield*/, loadWeights(manifest, modelBaseUri)]; | ||
case 2: | ||
weightMap = _d.sent(); | ||
this.loadFromWeightMap(weightMap); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
NeuralNetwork.prototype.loadFromWeightMap = function (weightMap) { | ||
var _a = this.extractParamsFromWeigthMap(weightMap), paramMappings = _a.paramMappings, params = _a.params; | ||
this._paramMappings = paramMappings; | ||
this._params = params; | ||
}; | ||
NeuralNetwork.prototype.extractWeights = function (weights) { | ||
@@ -130,8 +177,2 @@ var _a = this.extractParams(weights), paramMappings = _a.paramMappings, params = _a.params; | ||
}; | ||
NeuralNetwork.prototype.loadQuantizedParams = function (_) { | ||
throw new Error(this._name + ".loadQuantizedParams - not implemented"); | ||
}; | ||
NeuralNetwork.prototype.extractParams = function (_) { | ||
throw new Error(this._name + ".extractParams - not implemented"); | ||
}; | ||
return NeuralNetwork; | ||
@@ -138,0 +179,0 @@ }()); |
@@ -35,3 +35,4 @@ import * as tf from '@tensorflow/tfjs-core'; | ||
] | ||
.filter(function (t) { return t !== null; }); | ||
.filter(function (t) { return !!t; }) | ||
.map(function (t) { return t.toFloat(); }); | ||
return tf.concat(tensorsToStack, paddingAxis); | ||
@@ -38,0 +39,0 @@ }); |
@@ -13,3 +13,3 @@ import * as tf from '@tensorflow/tfjs-core'; | ||
export declare function isDimensions(obj: any): boolean; | ||
export declare function computeReshapedDimensions({ width, height }: IDimensions, inputSize: number): Dimensions; | ||
export declare function computeReshapedDimensions({width, height}: IDimensions, inputSize: number): Dimensions; | ||
export declare function getCenterPoint(pts: Point[]): Point; | ||
@@ -16,0 +16,0 @@ export declare function range(num: number, start: number, step: number): number[]; |
@@ -26,5 +26,3 @@ const dataFiles = [ | ||
}, | ||
browsers: process.env.KARMA_BROWSERS | ||
? process.env.KARMA_BROWSERS.split(',') | ||
: ['Chrome'], | ||
browsers: ['Chrome'], | ||
browserNoActivityTimeout: 120000, | ||
@@ -36,10 +34,4 @@ captureTimeout: 60000, | ||
} | ||
}, | ||
customLaunchers: { | ||
ChromeNoSandbox: { | ||
base: 'Chrome', | ||
flags: ['--no-sandbox'] | ||
} | ||
} | ||
}) | ||
} |
{ | ||
"name": "tfjs-image-recognition-base", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "A shared codebase for face-api.js and tfjs-tiny-yolo-v2.", | ||
@@ -11,5 +11,7 @@ "module": "./build/es6/index.js", | ||
"tsc-es6": "tsc --p tsconfig.es6.json", | ||
"build": "npm run tsc && npm run tsc-es6", | ||
"build": "rm -rf ./build && npm run tsc && npm run tsc-es6", | ||
"test": "karma start", | ||
"test-travis": "set KARMA_BROWSERS=ChromeHeadlessNoSandbox&& karma start --single-run" | ||
"test-browser": "karma start --single-run", | ||
"test-node": "ts-node node_modules/jasmine/bin/jasmine --config=jasmine-node.json", | ||
"test-all": "npm run test-browser && npm run test-node" | ||
}, | ||
@@ -19,8 +21,11 @@ "author": "justadudewhohacks", | ||
"dependencies": { | ||
"@tensorflow/tfjs-core": "^0.13.2", | ||
"@tensorflow/tfjs-core": "0.13.8", | ||
"tslib": "^1.9.3" | ||
}, | ||
"devDependencies": { | ||
"@tensorflow/tfjs-node": "^0.1.19", | ||
"@types/jasmine": "^2.8.8", | ||
"@types/node": "^10.9.2", | ||
"canvas": "^2.0.1", | ||
"jasmine": "^3.3.0", | ||
"jasmine-core": "^3.2.1", | ||
@@ -31,4 +36,5 @@ "karma": "^3.0.0", | ||
"karma-typescript": "^3.0.12", | ||
"typescript": "^2.9.2" | ||
"ts-node": "^7.0.1", | ||
"typescript": "2.8.4" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { isNumber } from 'util'; | ||
import { isValidNumber } from '../utils'; | ||
@@ -14,3 +14,3 @@ export interface IDimensions { | ||
constructor(width: number, height: number) { | ||
if (!isNumber(width) || !isNumber(height)) { | ||
if (!isValidNumber(width) || !isValidNumber(height)) { | ||
throw new Error(`Dimensions.constructor - expected width and height to be valid numbers, instead have ${JSON.stringify({ width, height })}`) | ||
@@ -17,0 +17,0 @@ } |
@@ -0,6 +1,8 @@ | ||
import { env } from '../env'; | ||
import { isMediaLoaded } from './isMediaLoaded'; | ||
export function awaitMediaLoaded(media: HTMLImageElement | HTMLVideoElement | HTMLCanvasElement) { | ||
return new Promise((resolve, reject) => { | ||
if (media instanceof HTMLCanvasElement || isMediaLoaded(media)) { | ||
if (media instanceof env.getEnv().Canvas || isMediaLoaded(media)) { | ||
return resolve() | ||
@@ -7,0 +9,0 @@ } |
@@ -0,1 +1,3 @@ | ||
import { env } from '../env'; | ||
export function bufferToImage(buf: Blob): Promise<HTMLImageElement> { | ||
@@ -13,3 +15,3 @@ return new Promise((resolve, reject) => { | ||
const img = new Image() | ||
const img = env.getEnv().createImageElement() | ||
img.onload = () => resolve(img) | ||
@@ -16,0 +18,0 @@ img.onerror = reject |
import { IDimensions } from '../classes/Dimensions'; | ||
import { env } from '../env'; | ||
import { getContext2dOrThrow } from './getContext2dOrThrow'; | ||
@@ -7,3 +8,5 @@ import { getMediaDimensions } from './getMediaDimensions'; | ||
export function createCanvas({ width, height }: IDimensions): HTMLCanvasElement { | ||
const canvas = document.createElement('canvas') | ||
const { createCanvasElement } = env.getEnv() | ||
const canvas = createCanvasElement() | ||
canvas.width = width | ||
@@ -14,4 +17,7 @@ canvas.height = height | ||
export function createCanvasFromMedia(media: HTMLImageElement | HTMLVideoElement, dims?: IDimensions): HTMLCanvasElement { | ||
if (!isMediaLoaded(media)) { | ||
export function createCanvasFromMedia(media: HTMLImageElement | HTMLVideoElement | ImageData, dims?: IDimensions): HTMLCanvasElement { | ||
const { ImageData } = env.getEnv() | ||
if (!(media instanceof ImageData) && !isMediaLoaded(media)) { | ||
throw new Error('createCanvasFromMedia - media has not finished loading yet') | ||
@@ -22,4 +28,9 @@ } | ||
const canvas = createCanvas({ width, height }) | ||
getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height) | ||
if (media instanceof ImageData) { | ||
getContext2dOrThrow(canvas).putImageData(media, 0, 0) | ||
} else { | ||
getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height) | ||
} | ||
return canvas | ||
} |
@@ -5,2 +5,3 @@ import { BoxWithText } from '../classes/BoxWithText'; | ||
import { Rect } from '../classes/Rect'; | ||
import { env } from '../env'; | ||
import { round } from '../utils'; | ||
@@ -19,4 +20,7 @@ import { drawBox } from './drawBox'; | ||
) { | ||
const { Canvas } = env.getEnv() | ||
const canvas = resolveInput(canvasArg) | ||
if (!(canvas instanceof HTMLCanvasElement)) { | ||
if (!(canvas instanceof Canvas)) { | ||
throw new Error('drawDetection - expected canvas to be of type: HTMLCanvasElement') | ||
@@ -23,0 +27,0 @@ } |
@@ -0,1 +1,3 @@ | ||
import { env } from '../env'; | ||
export async function fetchOrThrow( | ||
@@ -5,2 +7,4 @@ url: string, | ||
): Promise<Response> { | ||
const fetch = env.getEnv().fetch | ||
const res = await fetch(url, init) | ||
@@ -7,0 +11,0 @@ if (!(res.status < 400)) { |
import { Dimensions, IDimensions } from '../classes/Dimensions'; | ||
import { env } from '../env'; | ||
export function getMediaDimensions(input: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | IDimensions): Dimensions { | ||
if (input instanceof HTMLImageElement) { | ||
const { Image, Video } = env.getEnv() | ||
if (input instanceof Image) { | ||
return new Dimensions(input.naturalWidth, input.naturalHeight) | ||
} | ||
if (input instanceof HTMLVideoElement) { | ||
if (input instanceof Video) { | ||
return new Dimensions(input.videoWidth, input.videoHeight) | ||
@@ -9,0 +13,0 @@ } |
import * as tf from '@tensorflow/tfjs-core'; | ||
import { env } from '../env'; | ||
import { isTensor4D } from '../utils'; | ||
@@ -9,4 +10,5 @@ | ||
): Promise<HTMLCanvasElement> { | ||
const targetCanvas = canvas || document.createElement('canvas') | ||
const targetCanvas = canvas || env.getEnv().createCanvasElement() | ||
const [height, width, numChannels] = imgTensor.shape.slice(isTensor4D(imgTensor) ? 1 : 0) | ||
@@ -13,0 +15,0 @@ const imgTensor3D = tf.tidy(() => imgTensor.as3D(height, width, numChannels).toInt()) |
@@ -0,1 +1,2 @@ | ||
import { env } from '../env'; | ||
import { createCanvas, createCanvasFromMedia } from './createCanvas'; | ||
@@ -7,3 +8,5 @@ import { getContext2dOrThrow } from './getContext2dOrThrow'; | ||
if (!(input instanceof HTMLImageElement || input instanceof HTMLCanvasElement)) { | ||
const { Image, Canvas } = env.getEnv() | ||
if (!(input instanceof Image || input instanceof Canvas)) { | ||
throw new Error('imageToSquare - expected arg0 to be HTMLImageElement | HTMLCanvasElement') | ||
@@ -18,3 +21,3 @@ } | ||
const targetCanvas = createCanvas({ width: inputSize, height: inputSize }) | ||
const inputCanvas = input instanceof HTMLCanvasElement ? input : createCanvasFromMedia(input) | ||
const inputCanvas = input instanceof Canvas ? input : createCanvasFromMedia(input) | ||
@@ -21,0 +24,0 @@ const offset = Math.abs(width - height) / 2 |
@@ -0,5 +1,10 @@ | ||
import { env } from '../env'; | ||
export function isMediaElement(input: any) { | ||
return input instanceof HTMLImageElement | ||
|| input instanceof HTMLVideoElement | ||
|| input instanceof HTMLCanvasElement | ||
const { Image, Canvas, Video } = env.getEnv() | ||
return input instanceof Image | ||
|| input instanceof Canvas | ||
|| input instanceof Video | ||
} |
@@ -0,4 +1,9 @@ | ||
import { env } from '../env'; | ||
export function isMediaLoaded(media: HTMLImageElement | HTMLVideoElement) : boolean { | ||
return (media instanceof HTMLImageElement && media.complete) | ||
|| (media instanceof HTMLVideoElement && media.readyState >= 3) | ||
const { Image, Video } = env.getEnv() | ||
return (media instanceof Image && media.complete) | ||
|| (media instanceof Video && media.readyState >= 3) | ||
} |
@@ -8,10 +8,9 @@ import * as tf from '@tensorflow/tfjs-core'; | ||
uri: string | undefined, | ||
defaultModelName: string | ||
): Promise<any> { | ||
defaultModelName: string, | ||
): Promise<tf.NamedTensorMap> { | ||
const { manifestUri, modelBaseUri } = getModelUris(uri, defaultModelName) | ||
const manifest = await fetchJson<any>(manifestUri) | ||
const manifest = await fetchJson<tf.io.WeightsManifestConfig>(manifestUri) | ||
return tf.io.loadWeights(manifest, modelBaseUri) | ||
} |
import * as tf from '@tensorflow/tfjs-core'; | ||
import { Dimensions } from '../classes/Dimensions'; | ||
import { env } from '../env'; | ||
import { padToSquare } from '../ops/padToSquare'; | ||
@@ -49,3 +50,3 @@ import { computeReshapedDimensions, isTensor3D, isTensor4D, range } from '../utils'; | ||
const canvas = input instanceof HTMLCanvasElement ? input : createCanvasFromMedia(input as HTMLImageElement | HTMLVideoElement) | ||
const canvas = input instanceof env.getEnv().Canvas ? input : createCanvasFromMedia(input) | ||
this._canvases[idx] = canvas | ||
@@ -141,3 +142,3 @@ this._inputDimensions[idx] = [canvas.height, canvas.width, 3] | ||
if (input instanceof HTMLCanvasElement) { | ||
if (input instanceof env.getEnv().Canvas) { | ||
return tf.fromPixels(imageToSquare(input, inputSize, isCenterInputs)) | ||
@@ -144,0 +145,0 @@ } |
@@ -0,3 +1,5 @@ | ||
import { env } from '../env'; | ||
export function resolveInput(arg: string | any) { | ||
if (typeof arg === 'string') { | ||
if (!env.isNodejs() && typeof arg === 'string') { | ||
return document.getElementById(arg) | ||
@@ -4,0 +6,0 @@ } |
export * from './classes'; | ||
export * from './common'; | ||
export * from './dom'; | ||
export * from './env'; | ||
export * from './ops'; | ||
export * from './utils'; | ||
export * from './NeuralNetwork'; |
import * as tf from '@tensorflow/tfjs-core'; | ||
import { ParamMapping } from './common'; | ||
import { getModelUris } from './common/getModelUris'; | ||
import { loadWeightMap } from './dom'; | ||
import { env } from './env'; | ||
import { weightsLoaderFactory } from './weightsLoaderFactory'; | ||
export class NeuralNetwork<TNetParams> { | ||
export abstract class NeuralNetwork<TNetParams> { | ||
@@ -80,9 +84,39 @@ protected _params: TNetParams | undefined = undefined | ||
if (weightsOrUrl && typeof weightsOrUrl !== 'string') { | ||
throw new Error(`${this._name}.load - expected model uri, or weights as Float32Array`) | ||
await this.loadFromUri(weightsOrUrl) | ||
} | ||
public async loadFromUri(uri: string | undefined) { | ||
if (uri && typeof uri !== 'string') { | ||
throw new Error(`${this._name}.loadFromUri - expected model uri`) | ||
} | ||
const weightMap = await loadWeightMap(uri, this.getDefaultModelName()) | ||
this.loadFromWeightMap(weightMap) | ||
} | ||
public async loadFromDisk(filePath: string | undefined) { | ||
if (filePath && typeof filePath !== 'string') { | ||
throw new Error(`${this._name}.loadFromDisk - expected model file path`) | ||
} | ||
const { readFile } = env.getEnv() | ||
const { manifestUri, modelBaseUri } = getModelUris(filePath, this.getDefaultModelName()) | ||
const fetchWeightsFromDisk = (filePaths: string[]) => Promise.all( | ||
filePaths.map(filePath => readFile(filePath).then(buf => buf.buffer)) | ||
) | ||
const loadWeights = weightsLoaderFactory(fetchWeightsFromDisk) | ||
const manifest = JSON.parse((await readFile(manifestUri)).toString()) | ||
const weightMap = await loadWeights(manifest, modelBaseUri) | ||
this.loadFromWeightMap(weightMap) | ||
} | ||
public loadFromWeightMap(weightMap: tf.NamedTensorMap) { | ||
const { | ||
paramMappings, | ||
params | ||
} = await this.loadQuantizedParams(weightsOrUrl) | ||
} = this.extractParamsFromWeigthMap(weightMap) | ||
@@ -124,9 +158,5 @@ this._paramMappings = paramMappings | ||
protected loadQuantizedParams(_: any): Promise<{ params: TNetParams, paramMappings: ParamMapping[] }> { | ||
throw new Error(`${this._name}.loadQuantizedParams - not implemented`) | ||
} | ||
protected extractParams(_: any): { params: TNetParams, paramMappings: ParamMapping[] } { | ||
throw new Error(`${this._name}.extractParams - not implemented`) | ||
} | ||
protected abstract getDefaultModelName(): string | ||
protected abstract extractParamsFromWeigthMap(weightMap: tf.NamedTensorMap): { params: TNetParams, paramMappings: ParamMapping[] } | ||
protected abstract extractParams(weights: Float32Array): { params: TNetParams, paramMappings: ParamMapping[] } | ||
} |
@@ -44,5 +44,6 @@ import * as tf from '@tensorflow/tfjs-core'; | ||
] | ||
.filter(t => t !== null) as tf.Tensor4D[] | ||
.filter(t => !!t) | ||
.map((t: tf.Tensor) => t.toFloat()) as tf.Tensor4D[] | ||
return tf.concat(tensorsToStack, paddingAxis) | ||
}) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
387992
408
6194
12
3
6
+ Added@tensorflow/tfjs-core@0.13.8(transitive)
- Removed@tensorflow/tfjs-core@0.13.12(transitive)
Updated@tensorflow/tfjs-core@0.13.8