Javascript 不断地得到:“;尝试导入错误:';loadFrozenModel';不是从'@tensorflow/tfjs转换器'&引用;
我正在尝试将Tensorflow for Poets模型转换为Tensorflow.js模型,以便在前端环境(如网站)中使用。我试图遵循本教程: 我遵循了所有的方向,但当我尝试启动localhost时,我总是会遇到名义上的错误:Javascript 不断地得到:“;尝试导入错误:';loadFrozenModel';不是从'@tensorflow/tfjs转换器'&引用;,javascript,html,tensorflow,tensorflow.js,tensorflowjs-converter,Javascript,Html,Tensorflow,Tensorflow.js,Tensorflowjs Converter,我正在尝试将Tensorflow for Poets模型转换为Tensorflow.js模型,以便在前端环境(如网站)中使用。我试图遵循本教程: 我遵循了所有的方向,但当我尝试启动localhost时,我总是会遇到名义上的错误: src/index.js Attempted import error: 'loadFrozenModel' is not exported from '@tensorflow/tfjs-converter' 我使用以下方法训练了Tensorflow模型: Te
src/index.js
Attempted import error: 'loadFrozenModel' is not exported from
'@tensorflow/tfjs-converter'
我使用以下方法训练了Tensorflow模型:
- Tensorflow v。1.7.0
- TensorflowJS v。1.2.9
- 努比诉。1.16.5
import { loadFrozenModel } from '@tensorflow/tfjs-converter'
import labels from './labels.json'
const ASSETS_URL = `${window.location.origin}/assets`
const MODEL_URL = `${ASSETS_URL}/mobilenet-v2/tensorflowjs_model.pb`
const WEIGHTS_URL = `${ASSETS_URL}/mobilenet-v2/weights_manifest.json`
const IMAGE_SIZE = 224 // Model input size
const loadModel = async () => {
const model = await loadFrozenModel(MODEL_URL, WEIGHTS_URL)
const input = tf.zeros([1, IMAGE_SIZE, IMAGE_SIZE, 3])
// Warm up GPU
// model.predict({ input }) // MobileNet V1
model.predict({ Placeholder: input }) // MobileNet V2
return model
}
const predict = async (img, model) => {
const t0 = performance.now()
const image = tf.fromPixels(img).toFloat()
const resized = tf.image.resizeBilinear(image, [IMAGE_SIZE, IMAGE_SIZE])
const offset = tf.scalar(255 / 2)
const normalized = resized.sub(offset).div(offset)
const input = normalized.expandDims(0)
// const output = await tf.tidy(() => model.predict({ input })).data()
// MobileNet V2
const predictions = labels
.map((label, index) => ({ label, accuracy: output[index] }))
.sort((a, b) => b.accuracy - a.accuracy)
const time = `${(performance.now() - t0).toFixed(1)} ms`
return { predictions, time }
}
const start = async () => {
const input = document.getElementById('input')
const output = document.getElementById('output')
const model = await loadModel()
const predictions = await predict(input, model)
output.append(JSON.stringify(predictions, null, 2))
}
start()
编辑:我还添加了HTML文件,只是为了确定
<!DOCTYPE html>
<html lang="en">
<head>
<title>Image classifier</title>
</head>
<body>
<img id="input" src="assets/images/some-flower.jpg" />
<pre id="output"></pre>
</body>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
</html>
图像分类器
从“@tensorflow/tfjs转换器”导入{loadFrozenModel}
loadFrozenModel
不是从@tensorflow/tfjs converter
导出的。它位于@tensorflow/tfjs
的名称空间中。因为您已经导入了CDN脚本,所以只需要使用tf.loadFrozenModel
const model = await loadFrozenModel(MODEL_URL, WEIGHTS_URL)
另外,
tf.fromPixels
已更改为tf.browser.fromPixels
能否请您将index.js
的代码添加到问题中?当然!现在已经在帖子里了。下面是我的答案。非常感谢你的快速回复!这解决了我最初的错误。我已经更新了代码,但现在又出现了另一个尝试导入错误。也许你知道出了什么问题?我编辑了我的问题以显示我的代码和新错误。最好在一个线程中问一个问题。我编辑了我的答案。但是如果你有其他的错误,考虑打开新的线程来问这个问题。你可以接受并投票支持这个答案。