After the successful loading of the model from the assets folder, let's create a new function that
makes the predictions.
So function predict mileage takes car features as a parameter and returns a float.
The float means the mpg try.
Let's start with a try and catch block.
Prepare input tensor with exact scaling.
So here prepare input tensor.
Val input buffer equals to tensor flow buffer dot.
Create fixed size.
Set the parameter.
The first parameter as the input shape.
The second parameter is data type of type float.
So data type dot Float32.
This line of code is very important.
Creating a properly shaped and typed container, which is the tensor buffer that holds the data.
The input data your model expects.
Think of it as creating the exact mold or form that you need to pour your data into before giving it
to the model.
So this is the shape.
This is the mold, the form that you need, uh, for pouring the data into it.
Okay.
So here we are getting the input buffer.
This is a very important step.
The tensor buffer is a helper class from the TensorFlow Lite support library that wraps tensor data,
that its purpose is to provide an easy way to create and manipulate multi-dimensional arrays tensors
that Tflite models understand.
Okay, so this is the tensor buffer, like a specialized data box designed specifically for ML model
input output create fixed sized.
This is a function creating a TensorFlow buffer with a fixed predefined shape.
The buffer size can't change after creation, and this is common for models that expect inputs of consistent
size.
For example fixed image dimensions, fixed number of features as in our example, and the input shape
where an integer uh array.
Defining dimensions and shapes that we defined before and this is of type.
Let me show you.
It's an int array of 1 to 9.
As I said to you one sample nine features.
We need to pass nine features.
And this input buffer and Float32 specifies the data types of the elements in the TensorFlow 32 bit
floating point numbers.
Most common for ML models I want from you to focus with me.
Now here is a very important step.
I'll show you the features without scaling at first, and then we're going to move to make the scaled
features according to our Google Colab.
So features without scaling.
And this is the main purpose behind scaling.
So I'll create the features, I'll create the app, run the app and everything will work fine.
But I'll show you the importance of scaling features if you didn't understand it in the Google Colab.
Don't worry, I'm going to show you here and I am very confident that you should understand it well.
Val.
Features without scaling.
This is the name of the variable.
It's a float array of.
And here let me start with the car features in order the cylinders the displacement the car features
horsepower, weight, acceleration, model year and the origin.
So car features dot origin one, origin two and origin three.
Okay, we need to pass this this array into our input buffer.
So input buffer dot load data.
Load array data features without scaling.
Then we prepare the output tensor.
Val output buffer.
As we did in the input buffer we will create for the output buffer tensor buffer dot.
Create fixed size.
Set the output shape and float32 this.
Then we need to run the inference.
Start with the interpreter.
Run and set the input buffer buffer and the output buffer to output buffer.
This is the interpreter.
This is the run function that we have to pass two parameters the input buffer and the output buffer.
Let's get the prediction results.
So here val results equals to output buffer dot float array val prediction equals to results at index
zero.
Let me explain the output buffer.
This is a tensor buffer that contains the model's prediction output and the float array.
This property extracts the underlying data as a float array.
What it does converts the tensors internal representation into a simple Kotlin array that you can easily
work with.
And here the prediction equals to results at index zero.
Accesses the first element of the result array.
For regression models like your car mileage prediction app, the output is typically a single value.
So for this we use the result at index zero and the first element of the results array return the prediction
result the prediction.
And here we need to use the catch close e exception, throw runtime exception and print the message
okay.
This is how we use the function predict mileage.
And this is the job of our application.