Perbedaan fungsi tf.loadLayersModel dan tf.loadGraphModel di tensorflow JS

DandiLesmana
3 min readJun 14, 2024

--

Tensorflow.js adalah open-source toolkit yang dikembangkan Google untuk mengeksekusi machine learning models dan deep learning neural networks di browser atau pada platform node.

Hal ini juga memungkinkan developers untuk membuat machine learning models dalam JavaScript dan menggunakannya langsung di browser atau dengan Node.js.

tf.loadLayersModel() Function

Fungsi tf.loadLayersModel() digunakan untuk memuat model yang terdiri dari Lapisan objek (Layer objects), termasuk topology dan bobot opsionalnya(optionally weights).

Syntax:

tf.loadLayersModel(pathOrIOHandler, options?)

Parameters:

  • pathOrIOHandler: memungkinkan untuk menggunakan salah satu dari dua bentuk:
  1. path string ke file JSON ModelAndWeightsConfig yang mendeskripsikan model dalam format TensorFlow.js. Untuk skema file:// (models/model.json), http://, dan https://, path atau jalurnya bisa absolut atau relatif.
  2. Objek tf.io.IOHandler yang menggunakan method load untuk memuat artifacts model.
  • options (Object): Opsional parameter konfigurasi load model , termasuk:

requestInit: RequestInit (options) for HTTP requests.

onProgress: A progress callback.

fetchFunc: An override function for the window.

strict (boolean): Strict loading model: whether missing or extraneous weights should cause an Error. The default value is true.

weightPathPrefix (string): Weight file path prefix, which is derived by default from the path of the model JSON file.

fromTFHub (boolean): If the module or model should be loaded from the TF Hub. The default value is false.

weightUrlConverter: An async method to translate the name of a weight file to its URL. The names of the weight files are recorded in the weightsManifest.paths field of model.json. Weight files are assumed to be co-located with the model.json file by default.

Return Value: Promise<tf.LayersModel>

Example code.

import * as tf from "@tensorflow/tfjs"; 

const model = await tf.loadLayersModel(
'https://storage.googleapis.com/tfjs-models/tfjs/mnist_transfer_cnn_v1/model.json');
model.summary();

Output:

Tensorflow.js tf.loadGraphModel() Function

Fungsi .loadGraphModel() digunakan untuk Load graph model dengan memberikan URL ke definisi model.

Syntax:

tf.loadGraphModel (modelUrl, options)

Parameters:

  • modelUrl: Input tensor pertama yang dapat bertipe string atau io.IOHandler.Parameter ini adalah URL atau io.IOHandler yang membantu memuat model.
  • options: The second tensor input that is optional. Options are for the HTTP request, which allows sending credentials and custom headers. The types of options are –

requestInit: RequestInit are for HTTP requests.

onProgress: OnProgress is for progress callback.

fetchFunc: It is a function used to override the window.fetch function.

strict: Strict is a loading model: whether it is an extraneous weight or missing weights should trigger an Error.

weightPathPrefix: Path prefix is for weight files which is by default that is calculated from the path of the model JSON file.

fromTFHub: It is a boolean which is whether the module or model is to be loaded from TF Hub.

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

Example Code:

// Importing the tensorflow.js library 
import * as tf from "@tensorflow/tfjs"

// Defining tensor input elements
const modelUrl =
'https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';

// 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],]

perbedaan Method .loadGraphModel() dan .loadLayersModel() di TensorFlow.js

Jenis Model yang Dimuat:

  • loadGraphModel(): Memuat model berbasis grafik (juga dikenal sebagai model beku). Parameter modelnya sudah tetap dan tidak dapat diubah dengan data baru.
  • loadLayersModel(): Memuat model berbasis layer. Model ini dapat diubah dan dilatih lebih lanjut dengan data baru.

Penggunaan:

  • loadGraphModel(): Digunakan untuk skenario inferensi saja.
  • loadLayersModel(): Digunakan jika Anda perlu menyesuaikan model atau melatihnya lebih lanjut.

--

--