Expected Tensor but got Object

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.

Here is how I saved my pre-trained mobilenet_v2 file:

import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile

model = torchvision.models.mobilenet_v2(pretrained=True)
num_ftrs = model.classifier[1].in_features
model.classifier[1] = nn.Linear(num_ftrs, 2)
model.load_state_dict(torch.load((r’C:\Users\User\pytorch\pretrained_model\mobilenet_v2.pth’), map_location=torch.device(‘cpu’)))
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
optimized_traced_model = optimize_for_mobile(traced_script_module)

However, I received this error and was unable to run the image classification app.

E/PyTorchDemo: Error during image analysis
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
Exception raised from toTensor at …/…/…/…/src/main/cpp/libtorch_include/x86/ATen/core/ivalue_inl.h:111 (most recent call first):
(no backtrace available)
at org.pytorch.NativePeer.initHybrid(Native Method)
at org.pytorch.NativePeer.(NativePeer.java:24)
at org.pytorch.Module.load(Module.java:23)
at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:165)
at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:31)
at org.pytorch.demo.vision.AbstractCameraXActivity.lambda$setupCameraX$2$AbstractCameraXActivity(AbstractCameraXActivity.java:90)
at org.pytorch.demo.vision.-$$Lambda$AbstractCameraXActivity$t0OjLr-l_M0-_0_dUqVE4yqEYnE.analyze(lambda)
at androidx.camera.core.ImageAnalysisAbstractAnalyzer.analyzeImage(ImageAnalysisAbstractAnalyzer.java:57)
at androidx.camera.core.ImageAnalysisNonBlockingAnalyzer$1.run(ImageAnalysisNonBlockingAnalyzer.java:135)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)

Please help me in solving this issue. It would be massive for me to be able to complete this project. Thank you in advance.

The error you’re encountering seems to be related to an incompatible input tensor type. In your code, you’re using optimize_for_mobile which, in some cases, might change the input tensor type or lead to issues with the model execution on mobile devices.

First, I suggest you try running your model without the optimize_for_mobile step:


If the error still persists, try modifying the input preprocessing steps in the Android app. In the ImageClassificationActivity.java file, locate the bitmapToInputTensor function. You should find a line similar to the following:


Replace this line with the following code:

TensorImageUtils.bitmapToFloat32Tensor(bitmap, tensorBuffer);
tensor = Tensor.fromBlob(tensorBuffer, new long[]{1, 3, 224, 224});

You will need to import the following dependencies at the top of the ImageClassificationActivity.java file:

import org.pytorch.Tensor;
import org.pytorch.torchvision.TensorImageUtils;

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