loadModel() function initializes the TensorFlow.js model by loading it from the model.json file. This function must be called before performing any predictions.
Function signature
This is an asynchronous function that returns a Promise. The model is loaded from
cnn_model/model.json and stored in the global model variable.Parameters
This function takes no parameters.Return value
Returns a Promise that resolves when the model is successfully loaded. The loaded model is stored in the global
model variable.How it works
The function performs the following steps:- Logs “Loading Model” to the console
- Loads the TensorFlow.js model from
cnn_model/model.jsonusingtf.loadLayersModel() - Logs “Loaded Model” to the console
- Updates the UI to indicate the model has loaded
- Hides the loading progress bar
Code example
Implementation
Here’s the complete implementation from the source code:Usage in HTML
The function is automatically called when the page loads:UI integration
The function updates two HTML elements:loadingmodel- Text element that displays “Loaded ML Model” when completeprogressbar- Progress indicator that is hidden after loading
Best practices
- Call this function once when your application initializes
- Wait for the model to load before enabling prediction functionality
- The global
modelvariable must be initialized before callingloadModel()