javascript - 继续获取 : "Attempted import error: ' loadFrozenModel' is not exported from '@tensorflow/tfjs-converter' "

标签 javascript html tensorflow tensorflow.js tensorflowjs-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 模型:

  • Tensorflow 1.7.0 版

  • TensorflowJS 1.2.9 版

  • Numpy 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:这也是我在 Stack Overflow 上发布的第一个问题,如果这篇文章有遗漏/错误之处,请告诉我。

编辑:添加了我的 index.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

关于javascript - 继续获取 : "Attempted import error: ' loadFrozenModel' is not exported from '@tensorflow/tfjs-converter' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57894989/

相关文章:

javascript - Bash 字符串替换函数类似于 Javascript String.prototype.replace()

javascript - enzyme 安装测试因 redux 存储状态更新而失败

javascript - 如何使用 jquery 在文本框中只允许数字和 2 位小数

mongodb - 使用 keras.utils.Sequence 多处理和数据库 - 何时连接?

python - 加载 pb 文件 : DecodeError: Error parsing message

python - TensorFlow 数据不适用于多输入 keras 模型

javascript - 为什么要使用 javascript 函数包装器(在 coffeescript 中添加) ".call(this)"

javascript - 在同一日期返回的多条记录按电话号码升序排序

jquery - 将不同大小的div元素按指定顺序对齐

javascript - CSS:为什么我有一个 100% 高度的滚动条?