How to run a model from the hub?

Hi TF-JS folks…!

I’m trying to learn to use pre-trained models for face detection, and having some difficulties. This is what I am doing:

import * as tf from "@tensorflow/tfjs";
import "@tensorflow/tfjs-node";
import { join } from "path";

const model = tf.loadGraphModel(
  `file://${join(__dirname, "hub-models/model.json")}`
).then((model) => {
// to see what format it expects

But I can’t quite get how to input an image to it :frowning:

The previous code shows the expected input shape:

[ { name: 'input', shape: [ -1, 128, 128, 3 ], dtype: 'float32' } ]

The page for the model states otherwise though:

This is a TF.js model for use with TF.js library. The model has FP16 precision.

I should probably do this:

  1. Resize image (altering aspect ratio unless they are squared ?) to 128 pixels
  2. Remove the alpha channel ?
  3. Get it to float32 format (the one I am most confused with.)

Anyone experienced on this with any advice ?

Found out part of the problems here.

  • float32: algorithm expects normalized data, i.e dividing by 255,
  • -1 comes from is the number of images in the batch. It will be assumed 1 in this case.
  • To remove the alpha channel you can just iterate and remove every 4th value for an image with 4 components.