Hi,
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’)))
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
optimized_traced_model = optimize_for_mobile(traced_script_module)
optimized_traced_model._save_for_lite_interpreter(“MyMobileNetv2Model.pt”)
However, I received this error and was unable to run the image classification app.
Error:
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.