face-api/src/NeuralNetwork.ts

167 lines
5.2 KiB
TypeScript
Raw Normal View History

2020-10-13 22:57:06 +02:00
import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js';
2020-08-18 13:54:53 +02:00
import { ParamMapping } from './common';
import { getModelUris } from './common/getModelUris';
import { loadWeightMap } from './dom';
import { env } from './env';
export abstract class NeuralNetwork<TNetParams> {
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
protected _params: TNetParams | undefined = undefined
protected _paramMappings: ParamMapping[] = []
2020-08-26 00:24:48 +02:00
constructor(protected _name: string) {
/*
try {
const testTensor = tf.tensor([0]);
testTensor.toFloat();
} catch (err) {
throw new Error(`tfjs module not loaded: load '@tensorflow/tfjs' or '@tensorflow/tfjs-core' with appropriate backend explicitly`);
}
*/
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public get params(): TNetParams | undefined { return this._params }
public get paramMappings(): ParamMapping[] { return this._paramMappings }
public get isLoaded(): boolean { return !!this.params }
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public getParamFromPath(paramPath: string): tf.Tensor {
const { obj, objProp } = this.traversePropertyPath(paramPath)
return obj[objProp]
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public reassignParamFromPath(paramPath: string, tensor: tf.Tensor) {
const { obj, objProp } = this.traversePropertyPath(paramPath)
obj[objProp].dispose()
obj[objProp] = tensor
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public getParamList() {
return this._paramMappings.map(({ paramPath }) => ({
path: paramPath,
tensor: this.getParamFromPath(paramPath)
}))
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public getTrainableParams() {
return this.getParamList().filter(param => param.tensor instanceof tf.Variable)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public getFrozenParams() {
return this.getParamList().filter(param => !(param.tensor instanceof tf.Variable))
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public variable() {
this.getFrozenParams().forEach(({ path, tensor }) => {
this.reassignParamFromPath(path, tensor.variable())
})
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public freeze() {
this.getTrainableParams().forEach(({ path, tensor: variable }) => {
const tensor = tf.tensor(variable.dataSync())
variable.dispose()
this.reassignParamFromPath(path, tensor)
})
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public dispose(throwOnRedispose: boolean = true) {
this.getParamList().forEach(param => {
if (throwOnRedispose && param.tensor.isDisposed) {
throw new Error(`param tensor has already been disposed for path ${param.path}`)
}
param.tensor.dispose()
})
this._params = undefined
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public serializeParams(): Float32Array {
return new Float32Array(
this.getParamList()
.map(({ tensor }) => Array.from(tensor.dataSync()) as number[])
.reduce((flat, arr) => flat.concat(arr))
)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public async load(weightsOrUrl: Float32Array | string | undefined): Promise<void> {
if (weightsOrUrl instanceof Float32Array) {
this.extractWeights(weightsOrUrl)
return
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
await this.loadFromUri(weightsOrUrl)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public async loadFromUri(uri: string | undefined) {
if (uri && typeof uri !== 'string') {
throw new Error(`${this._name}.loadFromUri - expected model uri`)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
const weightMap = await loadWeightMap(uri, this.getDefaultModelName())
this.loadFromWeightMap(weightMap)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public async loadFromDisk(filePath: string | undefined) {
if (filePath && typeof filePath !== 'string') {
throw new Error(`${this._name}.loadFromDisk - expected model file path`)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
const { readFile } = env.getEnv()
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
const { manifestUri, modelBaseUri } = getModelUris(filePath, this.getDefaultModelName())
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
const fetchWeightsFromDisk = (filePaths: string[]) => Promise.all(
filePaths.map(filePath => readFile(filePath).then(buf => buf.buffer))
)
const loadWeights = tf.io.weightsLoaderFactory(fetchWeightsFromDisk)
const manifest = JSON.parse((await readFile(manifestUri)).toString())
const weightMap = await loadWeights(manifest, modelBaseUri)
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
this.loadFromWeightMap(weightMap)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public loadFromWeightMap(weightMap: tf.NamedTensorMap) {
const {
paramMappings,
params
} = this.extractParamsFromWeigthMap(weightMap)
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
this._paramMappings = paramMappings
this._params = params
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
public extractWeights(weights: Float32Array) {
const {
paramMappings,
params
} = this.extractParams(weights)
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
this._paramMappings = paramMappings
this._params = params
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
private traversePropertyPath(paramPath: string) {
if (!this.params) {
throw new Error(`traversePropertyPath - model has no loaded params`)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
const result = paramPath.split('/').reduce((res: { nextObj: any, obj?: any, objProp?: string }, objProp) => {
if (!res.nextObj.hasOwnProperty(objProp)) {
throw new Error(`traversePropertyPath - object does not have property ${objProp}, for path ${paramPath}`)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
return { obj: res.nextObj, objProp, nextObj: res.nextObj[objProp] }
}, { nextObj: this.params })
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
const { obj, objProp } = result
if (!obj || !objProp || !(obj[objProp] instanceof tf.Tensor)) {
throw new Error(`traversePropertyPath - parameter is not a tensor, for path ${paramPath}`)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
return { obj, objProp }
}
protected abstract getDefaultModelName(): string
protected abstract extractParamsFromWeigthMap(weightMap: tf.NamedTensorMap): { params: TNetParams, paramMappings: ParamMapping[] }
protected abstract extractParams(weights: Float32Array): { params: TNetParams, paramMappings: ParamMapping[] }
}