Open In App

Tensorflow.js tf.loadGraphModel() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .loadGraphModel() function is used to Load a graph model given a URL to the model definition.



Syntax:

tf.loadGraphModel (modelUrl, options)

Parameters:



Return value: It returns the Promise <tf.GraphModel>.

Example 1: In this example, we are loading MobileNetV2 from a URL and making a prediction with a zeros input.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements 
const modelUrl =
  
// Calling the loadGraphModel () method
const model = await tf.loadGraphModel(modelUrl);
  
// Printing the zeroes
const zeros = tf.zeros([1, 224, 224, 3]);
model.predict(zeros).print();

Output:

Tensor
     [[-0.1412081, -0.5656458, 0.7578365, ..., 
     -1.0148169, -0.81284, 1.1898142],]

Example 2: In this example, we are loading MobileNetV2 from a TF Hub URL and making a prediction with a zeros input.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements 
const modelUrl =
  
// Calling the loadGraphModel () method
const model = await tf.loadGraphModel(
        modelUrl, {fromTFHub: true});
  
// Printing the zeores
const zeros = tf.zeros([1, 224, 224, 3]);
model.predict(zeros).print();

Output:

Tensor
     [[-1.0764486, 0.0097444, 1.1630495, ..., 
     -0.345558, 0.035432, 0.9112286],]

Article Tags :