Expected Tensor but got Object

i_am_Latte: <p>Hi,<br>
I am currently working on the implementation of deep learning model on a mobile app. It is similar to the pytorchdemoapp application. It’s just that I’m using my pre-trained mobilenetv2 model with specific dataset and not using the imagenet dataset. I have converted my model into .pt file, tried to use my pre-trained model on the app using Android Studio.</p>
<p>Here is how I saved my pre-trained mobilenet_v2 file:</p>
<p>import torch<br>
import torchvision<br>
from torch.utils.mobile_optimizer import optimize_for_mobile</p>
<p>model = torchvision.models.mobilenet_v2(pretrained=True)<br>
num_ftrs = model.classifier[1].in_features<br>
model.classifier[1] = nn.Linear(num_ftrs, 2)<br>
model.load_state_dict(torch.load((r’C:\Users\User\pytorch\pretrained_model\mobilenet_v2.pth’), map_location=torch.device(‘cpu’)))<br>
model.eval()<br>
example = torch.rand(1, 3, 224, 224)<br>
traced_script_module = torch.jit.trace(model, example)<br>
optimized_traced_model = optimize_for_mobile(traced_script_module)<br>
optimized_traced_model._save_for_lite_interpreter(“MyMobileNetv2Model.pt”)</p>
<p>However, I received this error and was unable to run the image classification app.</p>
<p>Error:<br>
E/PyTorchDemo: Error during image analysis<br>
com.facebook.jni.CppException: isTensor() INTERNAL ASSERT FAILED at “…/…/…/…/src/main/cpp/libtorch_include/x86/ATen/core/ivalue_inl.h”:111, please report a bug to PyTorch. Expected Tensor but got Object<br>
Exception raised from toTensor at …/…/…/…/src/main/cpp/libtorch_include/x86/ATen/core/ivalue_inl.h:111 (most recent call first):<br>
(no backtrace available)<br>
at org.pytorch.NativePeer.initHybrid(Native Method)<br>
at org.pytorch.NativePeer.(NativePeer.java:24)<br>
at org.pytorch.Module.load(Module.java:23)<br>
at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:165)<br>
at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:31)<br>
at org.pytorch.demo.vision.AbstractCameraXActivity.lambda$setupCameraX$2$AbstractCameraXActivity(AbstractCameraXActivity.java:90)<br>
at org.pytorch.demo.vision.-$$Lambda$AbstractCameraXActivity$t0OjLr-l_M0-_0_dUqVE4yqEYnE.analyze(lambda)<br>
at androidx.camera.core.ImageAnalysisAbstractAnalyzer.analyzeImage(ImageAnalysisAbstractAnalyzer.java:57)<br>
at androidx.camera.core.ImageAnalysisNonBlockingAnalyzer$1.run(ImageAnalysisNonBlockingAnalyzer.java:135)<br>
at android.os.Handler.handleCallback(Handler.java:751)<br>
at android.os.Handler.dispatchMessage(Handler.java:95)<br>
at android.os.Looper.loop(Looper.java:154)<br>
at android.os.HandlerThread.run(HandlerThread.java:61)</p>
<p>Please help me in solving this issue. It would be massive for me to be able to complete this project. Thank you in advance.</p>

AbdulsalamBande: <p>The error you’re encountering seems to be related to an incompatible input tensor type. In your code, you’re using <code>optimize_for_mobile</code> which, in some cases, might change the input tensor type or lead to issues with the model execution on mobile devices.</p>
<p>First, I suggest you try running your model without the <code>optimize_for_mobile</code> step:</p>
<pre><code class="lang-auto">traced_script_module._save_for_lite_interpreter("MyMobileNetv2Model.pt")

</code></pre>
<p>If the error still persists, try modifying the input preprocessing steps in the Android app. In the <code>ImageClassificationActivity.java</code> file, locate the <code>bitmapToInputTensor</code> function. You should find a line similar to the following:</p>
<pre><code class="lang-auto">tensor.copyFrom(bitmap);

</code></pre>
<p>Replace this line with the following code:</p>
<pre><code class="lang-auto">TensorImageUtils.bitmapToFloat32Tensor(bitmap, tensorBuffer);
tensor = Tensor.fromBlob(tensorBuffer, new long[]{1, 3, 224, 224});

</code></pre>
<p>You will need to import the following dependencies at the top of the <code>ImageClassificationActivity.java</code> file:</p>
<pre><code class="lang-auto">import org.pytorch.Tensor;
import org.pytorch.torchvision.TensorImageUtils;

</code></pre>
<p>This modification should ensure that the input tensor is of the correct format expected by your model. Rebuild and run your Android app, and the error should be resolved</p>