开发者问题收集

如何修复与 tfjs-node 的兼容性问题?

2022-08-18
1310

导入

// Frontend - Vue.js
const tfn = require('@tensorflow/tfjs-node')

然后运行

npm install --save @tensorflow/tfjs-node

出现以下错误:

npm ERR! node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1093:16)
npm ERR! node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5)
npm ERR! node-pre-gyp ERR! System Windows_NT 10.0.22000
npm ERR! node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Name\\Desktop\\Team\\my-app\\node_modules\\@mapbox\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build"
npm ERR! node-pre-gyp ERR! cwd C:\Users\Name\Desktop\Team\my-app\node_modules\@tensorflow\tfjs-node
npm ERR! node-pre-gyp ERR! node -v v16.17.0
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.9
npm ERR! node-pre-gyp ERR! not ok

我已经找到但没有帮助的内容: 1 2

使用:
[email protected] / Python 3.10.6 / nodev16.17.0 /
node-pre-gyp v1.0.9 / Windows 11 / Visual Studio Code

我需要 tfjs-node 来加载我的 本地 tensorflow 模型

const tfn = require("@tensorflow/tfjs-node");
const handler = tfn.io.fileSystem("./path/to/your/model.json");
const model = await tf.loadLayersModel(handler);

安装 npm i @tensorflow/ [email protected] (来自@Vladimir Mandic 的建议):

98% after emitting CopyPlugin

WARNING  Compiled with 4 warnings                                                                                                                                                                 15:06:11
 warning  in ./node_modules/@tensorflow/tfjs-node/dist/index.js

Critical dependency: the request of a dependency is an expression
warning  in ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js

Module not found: Error: Can't resolve 'node-gyp' in 'C:\Users\Name\Desktop\Team\my-app\node_modules\@tensorflow\tfjs-node\node_modules\@mapbox\node-pre-gyp\lib\util'
warning  in ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js

Module not found: Error: Can't resolve 'npm' in 'C:\Users\Name\Desktop\Team\my-app\node_modules\@tensorflow\tfjs-node\node_modules\@mapbox\node-pre-gyp\lib\util'  
warning  in ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>

 @ ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib sync ^\.\/.*$
 @ ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
 @ ./node_modules/@tensorflow/tfjs-node/dist/index.js
 @ ./node_modules/cache-loader/dist/cjs.js??ref--13-0!./node_modules/babel-loader/lib!./node_modules/@vue/cli-plugin-eslint/node_modules/eslint-loader??ref--14-0!./src/components/PublishCode.js?vue&type=script&lang=js&
 @ ./src/components/PublishCode.js?vue&type=script&lang=js&
 @ ./src/components/Publish.vue
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://172.16.7.172:80&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
2个回答

@tensorflow/tfjs-node 的安装程序仅在无法找到并下载给定平台的预构建二进制文件时才会尝试构建
(并且构建过程一点也不简单)

就您而言,您使用的是 windows x64 并使用 nodejs v16 ,因此应该没问题

但是...该平台的 tfjs 3.19.0 的预构建二进制文件未正确发布
(您可以检查 https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/3.19.0/CPU-windows-3.19.0.zip 并直接查看错误)

tfjs 团队将很快修复它

但目前,我建议改用 tfjs-node 3.18.0
(你可以查看 https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/3.18.0/CPU-windows-3.18.0.zip 并查看其是否正常)

Vladimir Mandic
2022-08-23

尝试安装不同版本的 tfjs (PS 这些对我有用)

"@tensorflow-models/posenet": "^2.2.2",
"@tensorflow/tfjs": "^3.20.0",
Sachinsoni1402
2022-10-22