1
votes

I am using TensorFlow.js and I am interested in gettings the embeddings instead of the logits of a pre-trained model just as described in https://github.com/tensorflow/tfjs-models/tree/master/mobilenet for MobileNet ('Getting embeddings')

With the classic MobileNet model loaded const model = await mobilenet.load() you can use model.infer(image, 'conv_preds') for example and it works fine and you can do your transfer learning.

However, the infer method is not available for an offline model that I load using loadLayersModel(). After some looking around (for example), I have tried accessing the layer's output with get_layer but to no success.

Will I need to create and export a new model which outputs the layer that interests me that I then load as a new layersModel and use? Is there no other way?

Any advice would be appreciated

1

1 Answers

0
votes

try this

async function loadMobilenet() { 
const mobilenet = await tf.loadLayersModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json');
      const layer = mobilenet.getLayer('conv_pw_13_relu');
   return tf.model({inputs: mobilenet.inputs, outputs: layer.output}); }

to get the embeddings:

const activation = mobilenet.predict(img);