我一直试图加载一个预训练的模型,用于在chrome扩展中进行预测,但我收到了错误">未捕获(承诺中(类型错误:tf.loadLayersModel不是函数"。我用python训练了模型,并用保存了它
import tensorflowjs as tfjs
tfjs.converters.save_keras_model(model, "./models")
在chrome扩展中,我的popup.html看起来像这个
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
</head>
<body>
</body>
<script src="tensorflow.js"></script>
</html>
我的tensorflow.js文件是
async function loadModel(){
var json = chrome.runtime.getURL('./models/model.json')
chrome.storage.local.set({'model': json})
const model = await tf.loadLayersModel('models/model.json')
}
learnLinear();
和我的宣言.json
{
"name": "My Project",
"version": "1.0",
"description": "Tensorflow project",
"permissions": ["activeTab" ,"declarativeContent" ,"storage", "<all_urls>"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_title": "Test Extension",
"default_popup": "popup.html"
},
"web_accessible_resources": [
"mod1/*"
],
"content_security_policy": "script-src 'self' https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest; object-src 'self'",
"manifest_version": 2
}
我不知道为什么它不能识别loadLayersModel函数。有人能告诉我出了什么问题吗?
将您的content_security_policy替换为manifest.json文件中的这一行:
"content_security_policy":"script src"self"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"unsafe eval";object src"self";,