Merge and Save model in tensorflow.js after training in browser

Viewed 257

I am following a tensorflow.js tutorial, in which I am loading a mobilenet as a backbone

<html>
  <head>
    <!-- Load the latest version of TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
  </head>

and in js it is called as net

 net = await mobilenet.load();

Then the tutorial continuous by adding a "K-Nearest Neighbors Classifier"-Layer

<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier"></script>

and later call it by classifier

const classifier = knnClassifier.create();

after then adding samples to the Classifier, for making predictions the whole pipeline has to be inferred

// Get the activation from mobilenet.
const activation = net.infer(img, 'conv_preds');
// Then feed the activation to the custom output layer
const result = await classifier.predictClass(activation);

Now I want to download the whole model (net + classifier) as a .hdf file or similar. How do I do this? Do I still have to merge these two?

I know the model.save functionalities within python tensorflow and keras, but am lost as to how this is best done in tensorflow.js

Update - Currently I am trying something along the lines of

  // merging the model and then downloading it
  const merge_models = async (backbone, outputlayer) => {
      console.log('Downloading your model..');
    const model = tf.sequential();
    model.add(backbone);
    model.add(outputlayer);
    
    // download
    await model.save('downloads://my-model');
    console.log('..is completed.');
  };

but I am getting an error in the add function

TypeError: Cannot read properties of undefined (reading 'length')
0 Answers
Related