Skip to content

Commit

Permalink
Merge pull request #143 from mlaco/accept-iohandler
Browse files Browse the repository at this point in the history
Accept path to model or a TF IOHandler
  • Loading branch information
GantMan authored Aug 1, 2019
2 parents ab7c1e9 + 6841bb7 commit 0497856
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,37 @@ export async function load(base = BASE_PATH, options = { size: IMAGE_SIZE }) {
return nsfwnet
}

interface IOHandler {
load: () => any
}

export class NSFWJS {
public endpoints: string[]

private options: nsfwjsOptions
private path: string
private pathOrIOHandler: string | IOHandler
private model: tf.LayersModel
private intermediateModels: { [layerName: string]: tf.LayersModel } = {}

private normalizationOffset: tf.Scalar

constructor(base: string, options: nsfwjsOptions) {
constructor(
modelPathBaseOrIOHandler: string | IOHandler,
options: nsfwjsOptions
) {
this.options = options
this.path = `${base}model.json`
this.normalizationOffset = tf.scalar(255)

if (typeof modelPathBaseOrIOHandler === 'string') {
this.pathOrIOHandler = `${modelPathBaseOrIOHandler}model.json`
} else {
this.pathOrIOHandler = modelPathBaseOrIOHandler
}
}

async load() {
// this is a Layers Model
this.model = await tf.loadLayersModel(this.path)
this.model = await tf.loadLayersModel(this.pathOrIOHandler)
this.endpoints = this.model.layers.map(l => l.name)
const { size } = this.options

Expand Down

0 comments on commit 0497856

Please sign in to comment.