Я работаю над приложением android на основе машинного обучения, которое прогнозирует один из двух классов 0
или 1
. Я передаю 14 целых чисел в массиве для прогнозирования в моей модели.
for i in range(length):
print(np.argmax(pred[sample[i]]))
Я преобразовал свою модель в tflite, используя следующее
#Write out Keras save file
keras_file="linear.h5"
keras.models.save_model(model,keras_file)
#Convert the Keras file to TensorFlow Lite
converter=lite.TocoConverter.from_keras_model_file(keras_file)
tflite_model=converter.convert()
open("linear.tflite","wb").write(tflite_model)
Я также добавил следующее в сборку gradle
dependencies {
implementation 'org.tensorflow:tensorflow-lite:+'
implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
}
aaptOptions {
noCompress "tflite"
noCompress "lite"
}
Я сделал 14 EditText
полей для ввода пользователем цифр, TextView
для отображения прогнозируемых значений и Button
. Вот мой XML код
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:exported="true"
tools:context=".Input_Activity">
<EditText
android:id="@+id/editText14"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="166dp"
android:layout_marginBottom="15dp"
android:ems="10"
android:hint="RBCC"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText13"
app:layout_constraintTop_toBottomOf="@+id/editText11" />
<EditText
android:id="@+id/editText13"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="89dp"
android:ems="10"
android:hint="WBCC"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/outputNumber"
app:layout_constraintEnd_toStartOf="@+id/editText14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText10" />
<EditText
android:id="@+id/editText12"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="27dp"
android:layout_marginBottom="412dp"
android:ems="10"
android:hint="PCV"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText11"
app:layout_constraintTop_toBottomOf="@+id/editText9" />
<EditText
android:id="@+id/editText11"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="59dp"
android:layout_marginBottom="19dp"
android:ems="10"
android:hint="Hemoglobin"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText14"
app:layout_constraintEnd_toStartOf="@+id/editText12"
app:layout_constraintStart_toEndOf="@+id/editText10"
app:layout_constraintTop_toBottomOf="@+id/editText8" />
<EditText
android:id="@+id/editText10"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="19dp"
android:ems="10"
android:hint="Potassium"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText13"
app:layout_constraintEnd_toStartOf="@+id/editText11"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText7" />
<EditText
android:id="@+id/editText9"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="27dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="Sodium"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText12"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText8"
app:layout_constraintTop_toBottomOf="@+id/editText6" />
<EditText
android:id="@+id/editText8"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="59dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="S Ceatinine"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText11"
app:layout_constraintEnd_toStartOf="@+id/editText9"
app:layout_constraintStart_toEndOf="@+id/editText7"
app:layout_constraintTop_toBottomOf="@+id/editText5" />
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="34dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="Age"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText4"
app:layout_constraintEnd_toStartOf="@+id/editText2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="BGR"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText10"
app:layout_constraintEnd_toStartOf="@+id/editText8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText4" />
<EditText
android:id="@+id/editText6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="27dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="RBC"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText5"
app:layout_constraintTop_toBottomOf="@+id/editText3" />
<EditText
android:id="@+id/editText5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="59dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="Sugar"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText8"
app:layout_constraintEnd_toStartOf="@+id/editText6"
app:layout_constraintStart_toEndOf="@+id/editText4"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
<EditText
android:id="@+id/editText4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="Albumin"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText7"
app:layout_constraintEnd_toStartOf="@+id/editText5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText1" />
<EditText
android:id="@+id/editText3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="34dp"
android:layout_marginEnd="27dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="S Gravity"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText2"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="34dp"
android:layout_marginEnd="59dp"
android:layout_marginBottom="23dp"
android:ems="10"
android:hint="Blood Pressure"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editText5"
app:layout_constraintEnd_toStartOf="@+id/editText3"
app:layout_constraintStart_toEndOf="@+id/editText1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="197dp"
android:layout_height="0dp"
android:layout_marginStart="80dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="32dp"
android:text="Find my organ!"
app:layout_constraintBottom_toTopOf="@+id/outputNumber"
app:layout_constraintEnd_toEndOf="@+id/outputNumber"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/editText13"
app:layout_constraintTop_toBottomOf="@+id/editText14" />
<TextView
android:id="@+id/outputNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="31dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
Вот мой java код.
import androidx.appcompat.app.AppCompatActivity;
import android.content.*;
import android.content.res.*;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import org.tensorflow.lite.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import android.os.Bundle;
public class Input_Activity extends AppCompatActivity {
EditText inputNumber1,inputNumber2,inputNumber3,inputNumber4,inputNumber5,inputNumber6,inputNumber7,inputNumber8;
EditText inputNumber9,inputNumber10,inputNumber11,inputNumber12,inputNumber13,inputNumber14;
Button inferButton;
TextView outputNumber;
Interpreter tflite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
inputNumber1=(EditText)findViewById(R.id.editText1);
inputNumber2=(EditText)findViewById(R.id.editText2);
inputNumber3=(EditText)findViewById(R.id.editText3);
inputNumber4=(EditText)findViewById(R.id.editText4);
inputNumber5=(EditText)findViewById(R.id.editText5);
inputNumber6=(EditText)findViewById(R.id.editText6);
inputNumber7=(EditText)findViewById(R.id.editText7);
inputNumber8=(EditText)findViewById(R.id.editText8);
inputNumber9=(EditText)findViewById(R.id.editText9);
inputNumber10=(EditText)findViewById(R.id.editText10);
inputNumber11=(EditText)findViewById(R.id.editText11);
inputNumber12=(EditText)findViewById(R.id.editText12);
inputNumber13=(EditText)findViewById(R.id.editText13);
inputNumber14=(EditText)findViewById(R.id.editText14);
outputNumber=(TextView)findViewById(R.id.outputNumber);
inferButton=(Button)findViewById(R.id.button);
int getText1=Integer.parseInt(inputNumber1.getText().toString());
int getText2=Integer.parseInt(inputNumber2.getText().toString());
int getText3=Integer.parseInt(inputNumber3.getText().toString());
int getText4=Integer.parseInt(inputNumber4.getText().toString());
int getText5=Integer.parseInt(inputNumber5.getText().toString());
int getText6=Integer.parseInt(inputNumber6.getText().toString());
int getText7=Integer.parseInt(inputNumber7.getText().toString());
int getText8=Integer.parseInt(inputNumber8.getText().toString());
int getText9=Integer.parseInt(inputNumber9.getText().toString());
int getText10=Integer.parseInt(inputNumber10.getText().toString());
int getText11=Integer.parseInt(inputNumber11.getText().toString());
int getText12=Integer.parseInt(inputNumber12.getText().toString());
int getText13=Integer.parseInt(inputNumber13.getText().toString());
int getText14=Integer.parseInt(inputNumber14.getText().toString());
final int attributes[]={getText1,getText2,getText3,getText4,getText5,getText5,getText6,getText7,
getText8,getText9,getText10,getText10,getText11,getText12,getText13,getText14};
try{
tflite=new Interpreter(loadModelFile());
}catch(Exception ex){
ex.printStackTrace();
}
inferButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
int prediction=doInference(attributes);
//float prediction=doInference(inputNumber.getText().toString());
outputNumber.setText(attributes.toString());
}
});
}
//public float doInference(String inputString){
public int doInference(int [] attributeArray){
int[] inputVal=new int[14];
for(int i=0;i<14;i++) {
inputVal[i] = attributeArray[i];
}
//Output shape is [1][1]
int[][] outputval=new int[1][1];
//Run inference passing the input shape and getting the output shape
tflite.run(inputVal, outputval);
//Inferred value is at [0][0]
int inferredValue=outputval[0][0];
return inferredValue;
}
//Memory-map the model file in Assets
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor=this.getAssets().openFd("linear.tflite");
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);
}
}
Приложение вообще не запускается и вылетает без ошибки. Любая помощь очень ценится. Спасибо!