My Code in Python
import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile
model = torchvision.models.mobilenet_v3_small(pretrained=True)
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("model.ptl")
torch version : 2.1.0+cu118
torchvision version : 0.16.0+cu118
get file “model.ptl”
then Android Dependencies
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'org.pytorch:pytorch_android_lite:2.1.0'
implementation 'org.pytorch:pytorch_android_torchvision_lite:2.1.0'
}
MainActivity.java
package com.example.ptmobilewalkthrough;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import org.pytorch.LiteModuleLoader;
import org.pytorch.MemoryFormat;
import org.pytorch.IValue;
import org.pytorch.Module;
import org.pytorch.Tensor;
import org.pytorch.torchvision.TensorImageUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
private Bitmap bitmap = null;
private Module module = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
bitmap = BitmapFactory.decodeStream(getAssets().open("sample_card.jpg"));
module = LiteModuleLoader.load(assetFilePath(this, "model.ptl"));
}
catch (IOException e){
Log.e("PTRTDryRun", "Error reading assets", e);
finish();
}
// showing image on UI
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
final Button button = findViewById(R.id.inferButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View w) {
final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap, TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, MemoryFormat.CHANNELS_LAST);
Log.i("MyTag", "shape tensor image : " + inputTensor);
// running the model
final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
// getting tensor content as java array of floats
final float[] scores = outputTensor.getDataAsFloatArray();
// searching for the index with maximum score
float maxScore = -Float.MAX_VALUE;
int maxScoreIdx = -1;
for (int i = 0; i < scores.length; i++) {
if(scores[i] > maxScore) {
maxScore = scores[i];
maxScoreIdx = i;
}
}
String className = ImageNetClasses.IMAGENET_CLASSES[maxScoreIdx];
TextView textView = findViewById(R.id.textView);
textView.setText(className);
}
});
}
public static String assetFilePath(Context context, String assetName) throws IOException {
File file = new File(context.getFilesDir(), assetName);
if (file.exists() && file.length() > 0) {
return file.getAbsolutePath();
}
try (InputStream is = context.getAssets().open(assetName)) {
try (OutputStream os = new FileOutputStream(file)) {
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
os.flush();
}
return file.getAbsolutePath();
}
}
}
When build test app, and click button Inference
get error :
com.facebook.jni.CppException: Method 'forward' is not defined.
error code (MainActivity.java):
final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
but I defined ‘forward’ function in my model
i try change version https://mvnrepository.com/artifact/org.pytorch/pytorch_android_lite 2.1.0 till 1.9.0
i saw discuss
https://discuss.pytorch.org/t/pytorch-android-i-got-the-error-method-forward-is-not-defined-but-i-defined-forward-function-in-my-model/123247
but not solve about forward is not defined
anyone help me ?
im sorry choosing word very bad