我正在尝试使用react native构建一个对象检测应用程序,该应用程序带有一个从Yolov5训练的自定义模型,并使用tensorflowjs(v3.19.0)进行转换。
我的图像是数据Url字符串格式(转换为base64图像格式),我得到以下错误:
Error: Argument 'x' passed to 'pad' must be a Tensor or TensorLike, but got 'Tensor'
有人能帮助检查错误来自哪里以及如何修复吗?(我认为它在“处理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;
1条答案
按热度按时间j9per5c41#
我通过将整个项目移到世博会一号来解决这个问题,因为这是香草React原生。现在这个问题已经解决了。