【发布时间】:2022-11-19 11:21:06
【问题描述】:
我正在尝试使用本机反应和从 Yolov5 训练的自定义模型构建一个对象检测应用程序,并使用 tensorflowjs (v3.19.0) 进行转换。
我的图像是数据 Url 字符串格式(转换为 base64 图像格式),我收到以下错误:
Error: Argument 'x' passed to 'pad' must be a Tensor or TensorLike, but got 'Tensor'
任何人都可以帮助检查错误的来源以及如何解决这个问题吗? (我认为它在“已处理张量”部分,但无法弄清楚到底出了什么问题)
这是我的预测的完整代码:
import * as tf from '@tensorflow/tfjs';
import {bundleResourceIO, decodeJpeg} from '@tensorflow/tfjs-react-native';
const modelJSON = require('../assets/web_model/model.json');
const modelWeights = [
require('../assets/web_model/group1-shard1of7.bin'),
require('../assets/web_model/group1-shard2of7.bin'),
require('../assets/web_model/group1-shard3of7.bin'),
require('../assets/web_model/group1-shard4of7.bin'),
require('../assets/web_model/group1-shard5of7.bin'),
require('../assets/web_model/group1-shard6of7.bin'),
require('../assets/web_model/group1-shard7of7.bin'),
];
const getPredictions = async (dataURL: string) => {
// As tensorflow gets ready
await tf.ready();
// Load model
const model = await tf.loadGraphModel(
bundleResourceIO(modelJSON, modelWeights),
);
// Make input data
const imgB64 = dataURL.split(';base64,')[1];
const imgBuffer = tf.util.encodeString(imgB64, 'base64').buffer;
const raw = new Uint8Array(imgBuffer);
const imagesTensor = decodeJpeg(raw);
// Process input data
const modelShape: any = model.inputs[0].shape;
const resolution: [number, number] = modelShape.slice(1, 3);
let processedTensor = tf.image.resizeBilinear(imagesTensor, [
inputHeight,
inputWidth,
]) as tf.Tensor<tf.Rank.R3>;
processedTensor = tf.cast(processedTensor, 'float32');
processedTensor = tf.div(processedTensor, 255.0);
processedTensor = tf.reshape(processedTensor, [inputHeight, inputWidth, 3]);
processedTensor = tf.expandDims(processedTensor, 0);
// Get prediction
return (await model.executeAsync(processedTensor)) as tf.Tensor[];
};
export default getPredictions;
【问题讨论】:
标签: react-native tensorflow.js