[英]Error when load json pretrained model in javascript in local web application
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. 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.我想做一个 web 应用程序,它可以加载这个预训练的 model 进行预测。 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.但是,我遇到了错误Failed to load resource: net::ERR_NAME_NOT_RESOLVED
我已经搜索了解决方案并尝试了多种方法,但仍然无法解决该错误。
The model was trained in.py file and save into.json by using this line model 在.py 文件中训练并使用此行保存到.json
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.供您参考,我在应用程序中使用 node.js 和 ExpressJs,因此,使用 EJS 文件而不是 HTML 文件。
EJS file EJS 文件
<!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在digit-recognition.js文件中
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
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)
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.如果将models/
文件夹移到js/
文件夹下, digit-recognition.js
文件应该可以找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.