Hi @George_Soloupis,
I checked the documentation as @Kiran_Sai_Ramineni suggested, but i couldn’t find an example for translation using a transformer on Android like the model in the example on tensorflow Neural machine translation with a Transformer and Keras.
You are about the inference part thats were i have problems. This my code:
package com.example.projecttranslateusingjava;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.tensorflow.lite.Interpreter;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class MainActivity extends Activity {
private static final String MODEL_FILENAME = "converted_model.tflite";
private static final int MAX_LENGTH = 128;
private static final String[] INPUT_VOCAB = {"<start>", "hello", "how", "are", "you", "<end>"};
private static final String[] OUTPUT_VOCAB = {"<start>", "salut", "comment", "allez", "vous", "<end>"};
private EditText inputText;
private TextView outputText;
private final Map<String, Integer> inputVocabIndexMap = new HashMap<>();
private final Map<String, Integer> outputVocabIndexMap = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = findViewById(R.id.input_text);
outputText = findViewById(R.id.output_text);
Button translateButton = findViewById(R.id.translate_button);
// Initialize input and output vocab index maps
for (int i = 0; i < INPUT_VOCAB.length; i++) {
inputVocabIndexMap.put(INPUT_VOCAB[i], i);
}
for (int i = 0; i < OUTPUT_VOCAB.length; i++) {
outputVocabIndexMap.put(OUTPUT_VOCAB[i], i);
}
// Load the TensorFlow Lite model from the assets folder
MappedByteBuffer modelBuffer = null;
try {
modelBuffer = loadModelFile();
} catch (IOException e) {
e.printStackTrace();
}
Interpreter interpreter = new Interpreter(Objects.requireNonNull(modelBuffer));
// Set up the click listener for the translate button
translateButton.setOnClickListener(v -> {
// Get the input text and prepare it for the model
String inputString = inputText.getText().toString();
String[] input = prepareInput(inputString);
// Prepare output data
int[] output = new int[MAX_LENGTH];
Arrays.fill(output, outputVocabIndexMap.get("<end>"));
// Run the model
Map<Integer, Object> inputMap = new HashMap<>();
inputMap.put(0, prepareInput(inputString));
Map<Integer, Object> outputMap = new HashMap<>();
outputMap.put(0, output);
interpreter.runForMultipleInputsOutputs(new Object[] {input}, outputMap);
// Decode the output
String outputString = decodeOutput(output);
outputText.setText(outputString);
});
}
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = getResources().getAssets().openFd(MODEL_FILENAME);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
private String[] prepareInput(String inputString) {
String[] input = new String[MAX_LENGTH];
Arrays.fill(input, "<pad>");
String[] tokens = inputString.toLowerCase().split(" ");
System.arraycopy(tokens, 0, input, 0, tokens.length);
input[Math.min(tokens.length, MAX_LENGTH - 1)] = "<end>";
return input;
}
private String decodeOutput(int output) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i < output.length; i++) {
if (output[i] == outputVocabIndexMap.get(“”)) {
break;
}
sb.append(OUTPUT_VOCAB[output[i]]);
sb.append(" ");
}
return sb.toString().trim();
}}
And this is an error i am facing right now:
03/13 12:37:02: Launching 'app' on Lenovo TB-X606F.
Install successfully finished in 16 s 33 ms.
$ adb shell am start -n "com.example.projecttranslateusingjava/com.example.projecttranslateusingjava.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 31691 on device 'lenovo_tb_x606f-HPV4BJEB'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/ApplicationPackageManager: hasSystemFeature android.software.picture_in_picture com.example.projecttranslateusingjava
I/InterpreterApi: Loaded native library: tensorflowlite_jni
I/InterpreterApi: Didn't load native library: tensorflowlite_jni_gms_client
I/tflite: Initialized TensorFlow Lite runtime.
I/tflite: Created TensorFlow Lite delegate for select TF ops.
I/tflite: TfLiteFlexDelegate delegate: 42 nodes delegated out of 271 nodes with 13 partitions.
I/tflite: Replacing 42 node(s) with delegate (TfLiteFlexDelegate) node, yielding 26 partitions.
I/tflite: Replacing 1 node(s) with delegate (TfLiteFlexDelegate) node, yielding 1 partitions.
I/tflite: Replacing 1 node(s) with delegate (TfLiteFlexDelegate) node, yielding 2 partitions.
I/tflite: Replacing 8 node(s) with delegate (TfLiteFlexDelegate) node, yielding 4 partitions.
W/nslateusingjava: type=1400 audit(0.0:12721): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=397 scontext=u:r:untrusted_app:s0:c175,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "ro.hardware.chipname"
I/tflite: Created TensorFlow Lite XNNPACK delegate for CPU.
I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@86f6e4
D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@52e9d50, this = DecorView@4a78b49[MainActivity]
E/GraphicExt: Can't load libboost_ext_fwk
E/GraphicExt: GraphicExtModuleLoader::CreateGraphicExtInstance false
I/GPUD: @gpudInitialize: successfully initialized with GL, dbg=0 mmdump_dbg=0 mmpath_dbg=0
D/Surface: Surface::connect(this=0x739aff0000,api=1)
D/Surface: Surface::setBufferCount(this=0x739aff0000,bufferCount=3)
D/Surface: Surface::allocateBuffers(this=0x739aff0000)
W/Gralloc3: mapper 3.x is not supported
E/ion: ioctl c0044901 failed with code -1: Invalid argument
W/System: A resource failed to call close.
W/System: A resource failed to call close.
I/AssistStructure: Flattened final assist data: 1140 bytes, containing 1 windows, 4 views
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.projecttranslateusingjava, PID: 31691
java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: Op type not registered 'CaseFoldUTF8' in binary running on localhost. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
tensorflow/lite/kernels/reshape.cc:85 num_input_elements != num_output_el
at org.tensorflow.lite.NativeInterpreterWrapper.run(Native Method)
at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:247)
at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:107)
at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:80)
at com.example.projecttranslateusingjava.MainActivity.lambda$onCreate$0$com-example-projecttranslateusingjava-MainActivity(MainActivity.java:74)
at com.example.projecttranslateusingjava.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7147)
at android.view.View.performClickInternal(View.java:7120)
at android.view.View.access$3500(View.java:804)
at android.view.View$PerformClick.run(View.java:27538)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7399)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)