【问题标题】:Keep getting: "Attempted import error: 'loadFrozenModel' is not exported from '@tensorflow/tfjs-converter'"不断收到:“尝试导入错误:'loadFrozenModel' 未从 '@tensorflow/tfjs-converter' 导出”
【发布时间】:2020-01-13 15:20:52
【问题描述】:

我正在尝试将 Tensorflow-for-Poets 模型转换为 Tensorflow.js 模型,这样我就可以在前端环境(例如网站)中使用它。我试图按照本教程进行操作: https://gist.github.com/woudsma/d01eeda8998c9ab972d05ec9e9843886

我已按照所有指示进行操作,但是当我尝试启动 localhost 时,我不断收到名义错误:

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

  • 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:这也是我在 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>

【问题讨论】:

  • 能否将index.js的代码添加到问题中?
  • 绝对!它现在在帖子中。
  • 在下面查看我的答案

标签: javascript html tensorflow tensorflow.js tensorflowjs-converter


【解决方案1】:

从“@tensorflow/tfjs-converter”导入 { loadFrozenModel }

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

const model = await loadFrozenModel(MODEL_URL, WEIGHTS_URL)

另外tf.fromPixels 已更改为tf.browser.fromPixels

【讨论】:

  • 非常感谢您的快速回复!这解决了我最初的错误。我已经更新了我的代码,但现在我收到另一个尝试导入错误。也许你知道出了什么问题?我编辑了我的问题以显示我的代码和新错误。
  • 最好在一个线程中问一个问题。我编辑了我的答案。但如果您有其他错误,请考虑打开新线程来提问。您可以接受并投票赞成这个答案。
猜你喜欢
  • 2020-11-09
  • 2021-12-19
  • 2021-01-24
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2019-06-06
  • 2020-06-26
相关资源
最近更新 更多