Format error Exception raised from _load_for_mobile while loading a pytorch model in react native

huangapple go评论70阅读模式
英文:

Format error Exception raised from _load_for_mobile while loading a pytorch model in react native

问题

Here is the translation of the code portion you provided:

在尝试使用 torchvison 模型在 React Native 中制作对象检测应用程序时遇到以下错误 -
错误 -

可能的未处理的 Promise 拒绝 (id: 1):
对象 {
  "message": "格式错误
  异常从 /data/users/atalman/pytorch/torch/csrc/jit/mobile/import.cpp 的 _load_for_mobile 处引发:623 (最近的调用是首次调用):
  (没有可用的回溯)",
}

重新安装 PyTorch 未解决问题。
遵循文档 - > https://playtorch.dev/docs/tutorials/snacks/object-detection/
版本为 0.2.4

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('已拍摄图像!!');
    const width = image.getWidth();
    const height = image.getHeight();
    // 3.ii. 将图像转换为 blob,这是图像的字节表示,格式为高度(H)、宽度(W)和通道数(C),或简写为 HWC
    const blob = media.toBlob(image);
    // 3.iii. 从图像 blob 获取张量,还定义图像 blob 的格式
    let tensor = torch.fromBlob(blob, [height, width, 3]);
    // 3.iv. 重新排列张量形状为 [CHW]
    tensor = tensor.permute([2, 0, 1]);
    // 3.v. 将张量值除以 255,以获取在 [0, 1] 范围内的值
    tensor = tensor.div(255);
    // 3.vi. 将图像在中心裁剪为正方形
    const centerCrop = T.centerCrop(Math.min(width, height));
    tensor = centerCrop(tensor);
    // 3.vii. 将图像张量调整为 3 x 224 x 224
    const resize = T.resize(224);
    tensor = resize(tensor);
    // 3.viii. 使用均值和标准差对张量图像进行标准化
    const normalize = T.normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]);
    tensor = normalize(tensor);
    // 3.ix. Unsqueeze 在张量前添加 1 个维度
    tensor = tensor.unsqueeze(0);
    // console.log(tensor);
    // 3.x. 返回张量形状 [1, 3, 224, 224]
    const result = tensor.shape;
    console.log('result:', result);
    if (model == null) {
      console.log('加载模型...');
      const filePath = await MobileModel.download('detr_resnet50.ptl');
      model = await torch.jit._loadForMobile(filePath);
      console.log('模型成功加载');
    }
    console.log('前向传播中!!');
    const output = await model.forward(tensor);
    console.log(output);
  }
  return (
    <View style={styles.container}>
      <Text style={styles.label}>类别: </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"
}

如果您需要任何进一步的帮助,请告诉我。

英文:

While trying to make an object detection app in react native using torchvison models encountered the following error -
error -

Possible Unhandled Promise Rejection (id: 1):
Object {
&quot;message&quot;: &quot;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)&quot;,
}

reinstalling pytorch did not solve the problem.
followed documentation - > https://playtorch.dev/docs/tutorials/snacks/object-detection/
for version 0.2.4

code App.js -

import {StyleSheet, Text, View} from &#39;react-native&#39;;
import React from &#39;react&#39;;
import {
Camera,
MobileModel,
torch,
torchvision,
media,
} from &#39;react-native-pytorch-core&#39;;
let model = null;
const T = torchvision.transforms;
const App = () =&gt; {
async function handleImage(image) {
console.log(&#39;Image Taken!!&#39;);
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(&#39;result:&#39;, result);
if (model == null) {
console.log(&#39;Loading model...&#39;);
const filePath = await MobileModel.download(&#39;detr_resnet50.ptl&#39;);
model = await torch.jit._loadForMobile(filePath);
console.log(&#39;Model successfully loaded&#39;);
}
console.log(&#39;Forward propogation !!&#39;);
const output = await model.forward(tensor);
console.log(output);
}
return (
&lt;View style={styles.container}&gt;
&lt;Text style={styles.label}&gt;Class: &lt;/Text&gt;
&lt;Camera style={styles.camera} onCapture={handleImage} /&gt;
&lt;/View&gt;
);
};
export default App;
const styles = StyleSheet.create({
container: {
flexGrow: 1,
backgroundColor: &#39;#ffff&#39;,
padding: 20,
alignItems: &#39;center&#39;,
},
label: {
marginBottom: 10,
color: &#39;black&#39;,
fontSize: 20,
},
camera: {
flexGrow: 1,
width: &#39;100%&#39;,
marginTop: 70,
},
});

project dependencies -

  &quot;devDependencies&quot;: {
&quot;@babel/core&quot;: &quot;^7.12.9&quot;,
&quot;@babel/runtime&quot;: &quot;^7.12.5&quot;,
&quot;@react-native-community/eslint-config&quot;: &quot;^2.0.0&quot;,
&quot;babel-jest&quot;: &quot;^26.6.3&quot;,
&quot;eslint&quot;: &quot;^7.32.0&quot;,
&quot;jest&quot;: &quot;^26.6.3&quot;,
&quot;metro-react-native-babel-preset&quot;: &quot;0.72.3&quot;,
&quot;react-test-renderer&quot;: &quot;18.1.0&quot;
}

答案1

得分: 0

I believe the issue here is that you must specify the full path of the model based on where it is stored in the cloud. Following this tutorial you linked, it looks like the full url for the model is:

const MODEL_URL = "https://github.com/facebookresearch/playtorch/releases/download/v0.1.0/detr_resnet50.ptl";

whereas in your code you only specify the file name:

if (model == null) {
  console.log('Loading model...');
  const filePath = await MobileModel.download('detr_resnet50.ptl'); // filename here
  model = await torch.jit._loadForMobile(filePath);
  console.log('Model successfully loaded');
}

Try to create a variable named MODEL_URL as in the tutorial and pass that into the MobileModel.download function like this:

const MODEL_URL = 'https://github.com/facebookresearch/playtorch/releases/download/v0.1.0/detr_resnet50.ptl';

if (model == null) {
  console.log('Loading model...');
  const filePath = await MobileModel.download(MODEL_URL); // full path here
  model = await torch.jit._loadForMobile(filePath);
  console.log('Model successfully loaded');
}
英文:

I believe the issue here is that you must specify the full path of the model based on where it is stored in the cloud. Following this tutorial you linked, it looks like the full url for the model is:

const MODEL_URL = 
&quot;https://github.com/facebookresearch/playtorch/releases/download/v0.1.0/detr_resnet50.ptl&quot;;

whereas in your code you only specify the file name:

if (model == null) {
  console.log(&#39;Loading model...&#39;);
  const filePath = await MobileModel.download(&#39;detr_resnet50.ptl&#39;); // filename here
  model = await torch.jit._loadForMobile(filePath);
  console.log(&#39;Model successfully loaded&#39;);
}

Try to create a variable named MODEL_URL as in the tutorial and pass that into the MobileModel.download function like this:

const MODEL_URL =&#39;https://github.com/facebookresearch/playtorch/releases/download/v0.1.0/detr_resnet50.ptl&#39;;

if (model == null) {
  console.log(&#39;Loading model...&#39;);
  const filePath = await MobileModel.download(MODEL_URL); // full path here
  model = await torch.jit._loadForMobile(filePath);
  console.log(&#39;Model successfully loaded&#39;);
}

答案2

得分: -1

const filePath = await MobileModel.download(require('./detr_resnet50.ptl'));
英文:

you can try:

const filePath = await MobileModel.download(require(&#39;./detr_resnet50.ptl&#39;));

huangapple
  • 本文由 发表于 2023年5月8日 00:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定