javascript - 未处理的拒绝 (RangeError):Float32Array 的缓冲区长度应该是 4 的倍数
问题描述
我试图用我转换的 tfjs 对象(我使用 ssd mobilenet 320*320)检测模型创建一个反应应用程序,当我运行应用程序和网络摄像头退出时,我收到以下错误“未处理的拒绝(RangeError):缓冲区长度Float32Array 应该是 4 的倍数:
app.js 代码
// Import dependencies
import React, { useRef, useState, useEffect } from "react";
import * as tf from "@tensorflow/tfjs";
import Webcam from "react-webcam";
import "./App.css";
import { nextFrame } from "@tensorflow/tfjs";
// 2. TODO - Import drawing utility here
import {drawRect} from "./utilities";
function App() {
const webcamRef = useRef(null);
const canvasRef = useRef(null);
// Main function
const runCoco = async () => {
// 3. TODO - Load network
const net = await tf.loadGraphModel('https://signlangtranslation.s3.eu-gb.cloud-object-storage.appdomain.cloud/model(1).json')
// Loop and detect hands
setInterval(() => {
detect(net);
}, 16.7);
};
const detect = async (net) => {
// Check data is available
if (
typeof webcamRef.current !== "undefined" &&
webcamRef.current !== null &&
webcamRef.current.video.readyState === 4
) {
// Get Video Properties
const video = webcamRef.current.video;
const videoWidth = webcamRef.current.video.videoWidth;
const videoHeight = webcamRef.current.video.videoHeight;
// Set video width
webcamRef.current.video.width = videoWidth;
webcamRef.current.video.height = videoHeight;
// Set canvas height and width
canvasRef.current.width = videoWidth;
canvasRef.current.height = videoHeight;
// 4. TODO - Make Detections
const img = tf.browser.fromPixels(video)
const resized = tf.image.resizeBilinear(img, [640,480])
const casted = resized.cast('int32')
const expanded = casted.expandDims(0)
const obj = await net.executeAsync(expanded)
const boxes = await obj[4].array()
const classes = await obj[5].array()
const scores = await obj[6].array()
// Draw mesh
const ctx = canvasRef.current.getContext("2d");
// 5. TODO - Update drawing utility
// drawSomething(obj, ctx)
requestAnimationFrame(()=>{drawRect(boxes[0], classes[0], scores[0], 0.9, videoWidth, videoHeight, ctx)});
tf.dispose(img)
tf.dispose(resized)
tf.dispose(casted)
tf.dispose(expanded)
tf.dispose(obj)
}
};
useEffect(()=>{runCoco()},[]);
return (
<div className="App">
<header className="App-header">
<Webcam
ref={webcamRef}
muted={true}
style={{
position: "absolute",
marginLeft: "auto",
marginRight: "auto",
left: 0,
right: 0,
textAlign: "center",
zindex: 9,
width: 640,
height: 480,
}}
/>
<canvas
ref={canvasRef}
style={{
position: "absolute",
marginLeft: "auto",
marginRight: "auto",
left: 0,
right: 0,
textAlign: "center",
zindex: 8,
width: 640,
height: 480,
}}
/>
</header>
</div>
);
}
export default App;
我的 json 样本
{"format": "graph-model", "generatedBy": "2.6.0", "convertedBy": "TensorFlow.js Converter v3.8.0", "signature": {"inputs": {"input_tensor": {"name": "input_tensor:0", "dtype": "DT_UINT8", "tensorShape": {"dim": [{"size": "1"}, {"size": "-1"}, {"size": "-1"}, {"size": "3"}]}}}, "outputs": {"detection_multiclass_scores": {"name": "Identity_3:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}, {"size": "100"}, {"size": "25"}]}}, "detection_boxes": {"name": "Identity_1:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}, {"size": "100"}, {"size": "4"}]}}, "Identity_4:0": {"name": "Identity_4:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}, {"size": "100"}]}}, "Identity:0": {"name": "Identity:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}, {"size": "100"}]}}, "num_detections": {"name": "Identity_5:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}]}}, "Identity_2:0": {"name": "Identity_2:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}, {"size": "100"}]}}, "raw_detection_boxes": {"name": "Identity_6:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}, {"size": "12804"}, {"size": "4"}]}}, "raw_detection_scores": {"name": "Identity_7:0", "dtype": "DT_FLOAT", "tensorShape": {"dim": [{"size": "1"}, {"size": "12804"}, {"size": "25"}]}}}}, "modelTopology": {"node": [{"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/PadOrClipBoxList/zeros_7", "op": "Const", "attr": {"value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {"dim": [{"size": "1"}]}}}, "dtype": {"type": "DT_INT32"}}}, {"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/PadOrClipBoxList/sub_13/x", "op": "Const", "attr": {"dtype": {"type": "DT_INT32"}, "value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {}}}}}, {"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/PadOrClipBoxList/zeros_6", "op": "Const", "attr": {"dtype": {"type": "DT_INT32"}, "value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {"dim": [{"size": "1"}]}}}}}, {"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Gather/GatherV2_3/axis", "op": "Const", "attr": {"value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {}}}, "dtype": {"type": "DT_INT32"}}}, {"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Gather_1/GatherV2_3/axis", "op": "Const", "attr": {"value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {}}}, "dtype": {"type": "DT_INT32"}}}, {"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Gather_2/GatherV2_3/axis", "op": "Const", "attr": {"dtype": {"type": "DT_INT32"}, "value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {}}}}}, {"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Gather_3/GatherV2_3/axis", "op": "Const", "attr": {"dtype": {"type": "DT_INT32"}, "value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {}}}}}, {"name": "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Gather_4/GatherV2_3/axis", "op": "Const", "attr": {"value": {"tensor": {"dtype": "DT_INT32", "tensorShape": {}}}, "dtype": {"type": "DT_INT32"}}},
解决方案
推荐阅读
- php - 从 laravel 的日期字段中选择月份
- django - 如何通过删除另一个模型具有外键关系的结果来过滤 Django 查询集?
- asp.net - 如何将 Angular 连接到 PostgresSQL?
- python - 如何将带有单引号的字符串元素转换为 Python 列表中的双引号
- java - Java Spring 401 Unauthorized
- php - 重定向后如何更改我的代码以使用验证码
- sql - 根据原始数据构建时间窗口计数器 - Big Query
- apache - 将 yesod 应用程序作为(快速)CGI 服务
- javascript - 覆盖单个实例的数组函数
- javascript - cryptojs.decrypt 返回空结果