【发布时间】:2020-05-25 20:50:15
【问题描述】:
我想使用 tfjs-node 使用 Tensorflow 处理几个图像(实际上相当多)。
不幸的是,在我的 Raspberry Pi 上,我很快就遇到了内存不足的错误。 这是 Typescript 中的 sn-p:
import * as tfnode from '@tensorflow/tfjs-node'
import * as fs from 'fs'
import * as path from 'path'
import * as process from 'process'
let counter = 0
setInterval(() => {
const imageBuffer = fs.readFileSync(path.join(__dirname, 'samples', 'image.jpg'))
const tfimage = tfnode.node.decodeJpeg(imageBuffer)
console.log(`${++counter} - ${process.memoryUsage().rss / 1024 / 1024} MB`)
}, 100)
这会输出类似
1 - 216.390625 MB
2 - 357.78515625 MB
3 - 499.421875 MB
4 - 641.5703125 MB
5 - 782.9453125 MB
6 - 924.3203125 MB
7 - 1066.7265625 MB
8 - 1208.09765625 MB
9 - 1349.4765625 MB
10 - 1491.625 MB
11 - 1633.2578125 MB
2020-05-25 22:36:12.101809: W tensorflow/core/framework/op_kernel.cc:1651] OP_REQUIRES failed at cast_op.cc:109 : Resource exhausted: OOM when allocating tensor with shape[3024,4032,3] and type int32 on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu
/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:404
throw ex;
^
Error: Invalid TF_Status: 8
Message: OOM when allocating tensor with shape[3024,4032,3] and type int32 on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu
at NodeJSKernelBackend.executeSingleOutput (/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-node/dist/nodejs_kernel_backend.js:193:43)
at NodeJSKernelBackend.cast (/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-node/dist/nodejs_kernel_backend.js:1141:21)
at engine_1.ENGINE.runKernelFunc.x (/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/ops/array_ops.js:139:78)
at /home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:542:55
at /home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:388:22
at Engine.scopedRun (/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:398:23)
at Engine.tidy (/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:387:21)
at kernelFunc (/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:542:29)
at /home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:553:27
at Engine.scopedRun (/home/pi/develop/tftest/build/node_modules/@tensorflow/tfjs-core/dist/engine.js:398:23)
我尝试了 https://github.com/Qengineering/TensorFlow-Raspberry-Pi 和 https://github.com/yhwang/node-red-contrib-tf-model 的 arm 库,它们的行为相同。
然后我在 Windows 上尝试了这个非常 sn-p 的测试,我得到了非常惊人的结果。内存消耗迅速增加到 8GB,然后在大约 800MB 和 8GB 之间不可重现地波动。当然可以,因为系统的内存比 RPi 多得多。
但是我可以用 RPi 做什么?我可以控制 Tensorflow 的内存管理吗?
【问题讨论】:
标签: node.js tensorflow raspberry-pi tensorflow.js