How to use prediction function from tflite model in flutter app

Viewed 680

I have a model that detects the diabetes disease by doing predict function on an array of numbers here

model1 = Sequential()
model1.add(Dense(500, input_dim=8, activation='sigmoid'))
model1.add(Dense(100, activation='sigmoid'))
model1.add(Dense(2, activation='softmax'))
model1.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
model1.fit(x_train,y_train, epochs=1000, batch_size=70, validation_data=(x_test, y_test))
    

y_pred= model1.predict(x_test)
y_pred_prob = model1.predict_proba(x_test)

 

#s=round(b[0,0])

keras_file = "diabetes.h5"
keras.models.save_model(model1, keras_file)

converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)

tflite_model = converter.convert()
open("diabetes.tflite", "wb").write(tflite_model)

#print(model.predict(np.array([[1,85,66,29,0,26.6,0.351,31]])))

I have convert the model to tflite file and added it to flutter app, now i want to use the predict function (model.pretict()) in the main. dart in flutter so i added the tflite file and imported main.dart

 @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController Pregnancies = new TextEditingController();
  TextEditingController Glucose = new TextEditingController();
  TextEditingController BloodPressure = new TextEditingController();
  TextEditingController SkinThickness = new TextEditingController();
  TextEditingController Insulin = new TextEditingController();
  TextEditingController BMI = new TextEditingController();
  TextEditingController DiabetesPedigreeFunction = new TextEditingController();
  TextEditingController Age = new TextEditingController();


  int nPregnancies = 0;
  int nGlucose = 0;
  int nBloodPressure = 0;
  int nSkinThickness = 0;
  int nInsulin = 0;
  int nBMI = 0;
  int nDiabetesPedigreeFunction = 0;
  int nAge = 0;
  bool _isLoading = false;
  String result;

  var nArray = [];

  Future diabetesPrediction() async {
    await Tflite.loadModel(model: "assets/model_unquant.tflite",labels: "assets/labels.txt");
-------------------------------------------------------------------
    var output = await Tflite.runModelOnImage(nArray);
-------------------------------------------------------------------
    setState(() {
       result = output.toString();
    });
  }

here i want to use my predict function in the model (model.preict()) instead of runModelOnImage(nArray); to do prediction on nArray, as it takes an array of 8 numbers predict on it and gives a result 0 or 1, all tutorials i saw doing it on image classifier model so any help to do that or any other solutions to use ML model in flutter!

0 Answers
Related