React Native加载PyTorch模型遇Format error报错求助
目标检测App加载模型时出现Format Error的Promise拒绝错误
错误信息
Possible Unhandled Promise Rejection (id: 1): Object { "message": "Format error Exception raised from _load_for_mobile at /data/users/atalman/pytorch/torch/csrc/jit/mobile/import.cpp:623 (most recent call first): (no backtrace available)", }
背景
开发React Native目标检测应用时遇到上述错误,严格遵循playtorch.dev 0.2.4版本的目标检测教程开发,已尝试重新安装PyTorch但问题未解决。
项目代码
App.js
import {StyleSheet, Text, View} from 'react-native'; import React from 'react'; import { Camera, MobileModel, torch, torchvision, media, } from 'react-native-pytorch-core'; let model = null; const T = torchvision.transforms; const App = () => { async function handleImage(image) { console.log('Image Taken!!'); const width = image.getWidth(); const height = image.getHeight(); // 3.ii. Convert image to blob, which is a byte representation of the image // in the format height (H), width (W), and channels (C), or HWC for short const blob = media.toBlob(image); // 3.iii. Get a tensor from image the blob and also define in what format // the image blob is. let tensor = torch.fromBlob(blob, [height, width, 3]); // 3.iv. Rearrange the tensor shape to be [CHW] tensor = tensor.permute([2, 0, 1]); // 3.v. Divide the tensor values by 255 to get values between [0, 1] tensor = tensor.div(255); // 3.vi. Crop the image in the center to be a squared image const centerCrop = T.centerCrop(Math.min(width, height)); tensor = centerCrop(tensor); // 3.vii. Resize the image tensor to 3 x 224 x 224 const resize = T.resize(224); tensor = resize(tensor); // 3.viii. Normalize the tensor image with mean and standard deviation const normalize = T.normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]); tensor = normalize(tensor); // 3.ix. Unsqueeze adds 1 leading dimension to the tensor tensor = tensor.unsqueeze(0); // console.log(tensor); // 3.x. Return the tensor shape [1, 3, 224, 224] const result = tensor.shape; console.log('result:', result); if (model == null) { console.log('Loading model...'); const filePath = await MobileModel.download('detr_resnet50.ptl'); model = await torch.jit._loadForMobile(filePath); console.log('Model successfully loaded'); } console.log('Forward propogation !!'); const output = await model.forward(tensor); console.log(output); } return ( <View style={styles.container}> <Text style={styles.label}>Class: </Text> <Camera style={styles.camera} onCapture={handleImage} /> </View> ); }; export default App; const styles = StyleSheet.create({ container: { flexGrow: 1, backgroundColor: '#ffff', padding: 20, alignItems: 'center', }, label: { marginBottom: 10, color: 'black', fontSize: 20, }, camera: { flexGrow: 1, width: '100%', marginTop: 70, }, });
项目依赖配置(devDependencies)
{ "@babel/core": "^7.12.9", "@babel/runtime": "^7.12.5", "@react-native-community/eslint-config": "^2.0.0", "babel-jest": "^26.6.3", "eslint": "^7.32.0", "jest": "^26.6.3", "metro-react-native-babel-preset": "0.72.3", "react-test-renderer": "18.1.0" }
排查与解决方案
- 验证模型完整性:检查下载的
detr_resnet50.ptl文件是否完整,是否为PyTorch Mobile兼容格式。可手动重新下载模型,确认文件无损坏或中断情况。 - 替换模型加载方法:将
torch.jit._loadForMobile替换为公开APItorch.jit.load,部分版本中_loadForMobile为内部方法,容易出现兼容性问题。 - 匹配依赖版本:确认
react-native-pytorch-core版本与教程的0.2.4一致,执行npm list react-native-pytorch-core查看当前版本,若不符则安装指定版本:npm install react-native-pytorch-core@0.2.4。 - 调整模型加载时机:将模型加载逻辑移至组件挂载阶段,避免在相机捕获回调中异步加载,减少竞争问题:
React.useEffect(() => { async function loadModel() { console.log('Loading model...'); const filePath = await MobileModel.download('detr_resnet50.ptl'); model = await torch.jit.load(filePath); console.log('Model successfully loaded'); } loadModel(); }, []); - 清理项目缓存:执行以下命令清除缓存并重新安装依赖:
npm cache clean --force rm -rf node_modules package-lock.json npm install npx react-native start --reset-cache - 检查存储权限:Android端需在
AndroidManifest.xml中添加存储读写权限,适配对应系统版本的权限要求。
内容的提问来源于stack exchange,提问作者shubham Sahu
相关产品推荐
相关产品推荐

