Error when load json pretrained model in javascript in local web application

Viewed 213

currently I am using TensorFlow to train a model in python and save it as model.json together with the BIN file in folder named models as shown as below. I want to do a web application that can load this pre-trained model for prediction. However, I encountered the error Failed to load resource: net::ERR_NAME_NOT_RESOLVED I have googled the solutions and tried several ways, but the error still unable to be solved.

enter image description here

The model was trained in .py file and save into .json by using this line

tfjs.converters.save_keras_model(model, 'models')

For your information, I using node.js and ExpressJs in the application, therefore, the EJS file is used instead of HTML file.

EJS file

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
    <link rel="stylesheet" type="text/css" href="/style/digit.css">
</head>
<body>
    <main>
        <p1>text here</p1>
    </main>   
    <script src="/js/digit-recognition.js"></script>
</body>
</html>

In the digit-recognition.js file

let model;

async function loadModel() {
    console.log("model loading..");
    model = undefined;
    model = await tf.loadLayersModel("models/model.json");
    console.log("model loaded..");
}
loadModel();

$("#predict-button").click(async function () {

    let predictions = await model.predict(tensor).data();
    let results = Array.from(predictions);
    console.log(results);
});

I get the following error as displayed in the console:

Failed to load resource: net::ERR_NAME_NOT_RESOLVED

Updated at 27 Nov 2020 1131am

I have tried many suggested solutions online but it still not works, and the error I get is this now

GET http://localhost:3000/models/model.json 404 (Not Found)
1 Answers

I believe this is a pathing issue. If you move the models/ folder into the js/ folder, the digit-recognition.js file should be able to find it.

Related