From 396504fd85a3b3747ed33d1054b9f0b27fef9d7f Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 15 Oct 2020 15:54:10 +1100 Subject: [PATCH] Updated grid size model to be static --- src/ml/gridSize/GridSizeModel.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ml/gridSize/GridSizeModel.js b/src/ml/gridSize/GridSizeModel.js index 1287093..2b4dcaa 100644 --- a/src/ml/gridSize/GridSizeModel.js +++ b/src/ml/gridSize/GridSizeModel.js @@ -6,20 +6,21 @@ import config from "./model.json"; import weights from "./group1-shard1of1.bin"; class GridSizeModel extends Model { - model; + // Store model as static to prevent extra network requests + static model; constructor() { super(config, { "group1-shard1of1.bin": weights }); } async predict(imageData) { - if (!this.model) { - this.model = await tf.loadLayersModel(this); + if (!GridSizeModel.model) { + GridSizeModel.model = await tf.loadLayersModel(this); } const prediction = tf.tidy(() => { const image = tf.browser.fromPixels(imageData, 1).toFloat(); const normalized = image.div(tf.scalar(255.0)); const batched = tf.expandDims(normalized); - return this.model.predict(batched); + return GridSizeModel.model.predict(batched); }); const data = await prediction.data(); return data[0];