继续得到: "Attempted import error: 'loadFrozenModel' is not exported from '@tensorflow/tfjs-converter



我正在尝试将Tensorflow-for-Poets模型转换为Tensorflow.js模型,因此我可以在前端环境中使用,例如网站。我试图遵循本教程: https://gist.github.com/woudsma/d01eeda8998c9ab972d05ec9e9843886

我已经遵循了所有说明,但是当我尝试启动本地主机时,我不断收到名义上的错误:

src/index.js
Attempted import error: 'loadFrozenModel' is not exported from 
'@tensorflow/tfjs-converter'

我使用以下方法训练了一个 Tensorflow 模型:

  • 张量流 v. 1.7.0

  • TensorflowJS v. 1.2.9

  • Numpy v. 1.16.5

我还查看了这些先前提出的问题:

http://www.github.com/tensorflow/tfjs/issues/149

http://www.stackoverflow.com/questions/49718162/tfjs-converter-html-javascript-trouble-importing-class

但这并没有解决我的问题。

这是我在教程中找到的示例项目。它包含我也在我的项目中使用的。 https://github.com/woudsma/retrain-mobilenet-for-the-web

我找不到有关此特定错误的任何信息,有谁知道出了什么问题?

PS:这也是我发布到堆栈溢出的第一个问题,所以如果这篇文章缺少/错误,请告诉我。

编辑:添加了我的索引.js:

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>

import { loadFrozenModel } from '@tensorflow/tfjs-converter'

loadFrozenModel不会从@tensorflow/tfjs-converter导出。而是在@tensorflow/tfjs的命名空间中。由于已导入 CDN 脚本,因此只需使用tf.loadFrozenModel加载模型

const model = await loadFrozenModel(MODEL_URL, WEIGHTS_URL)

此外,tf.fromPixels已更改为tf.browser.fromPixels

相关内容

最新更新