WEBVTT

00:00.080 --> 00:08.120
After the successful loading of the model from the assets folder, let's create a new function that

00:08.360 --> 00:10.080
makes the predictions.

00:10.240 --> 00:20.120
So function predict mileage takes car features as a parameter and returns a float.

00:20.360 --> 00:23.480
The float means the mpg try.

00:23.520 --> 00:27.520
Let's start with a try and catch block.

00:27.640 --> 00:31.160
Prepare input tensor with exact scaling.

00:31.400 --> 00:34.360
So here prepare input tensor.

00:34.400 --> 00:41.040
Val input buffer equals to tensor flow buffer dot.

00:41.080 --> 00:44.040
Create fixed size.

00:44.200 --> 00:46.280
Set the parameter.

00:46.320 --> 00:48.720
The first parameter as the input shape.

00:48.760 --> 00:53.320
The second parameter is data type of type float.

00:53.440 --> 00:57.320
So data type dot Float32.

00:57.360 --> 01:00.400
This line of code is very important.

01:00.760 --> 01:08.780
Creating a properly shaped and typed container, which is the tensor buffer that holds the data.

01:09.100 --> 01:12.180
The input data your model expects.

01:12.380 --> 01:22.740
Think of it as creating the exact mold or form that you need to pour your data into before giving it

01:22.740 --> 01:24.020
to the model.

01:24.020 --> 01:25.380
So this is the shape.

01:25.380 --> 01:31.500
This is the mold, the form that you need, uh, for pouring the data into it.

01:31.700 --> 01:32.260
Okay.

01:32.300 --> 01:35.580
So here we are getting the input buffer.

01:35.780 --> 01:38.020
This is a very important step.

01:38.380 --> 01:47.900
The tensor buffer is a helper class from the TensorFlow Lite support library that wraps tensor data,

01:47.940 --> 01:56.100
that its purpose is to provide an easy way to create and manipulate multi-dimensional arrays tensors

01:56.260 --> 01:58.660
that Tflite models understand.

01:58.780 --> 02:07.740
Okay, so this is the tensor buffer, like a specialized data box designed specifically for ML model

02:07.740 --> 02:10.860
input output create fixed sized.

02:10.900 --> 02:17.640
This is a function creating a TensorFlow buffer with a fixed predefined shape.

02:17.920 --> 02:26.960
The buffer size can't change after creation, and this is common for models that expect inputs of consistent

02:26.960 --> 02:27.480
size.

02:27.480 --> 02:35.560
For example fixed image dimensions, fixed number of features as in our example, and the input shape

02:35.800 --> 02:38.360
where an integer uh array.

02:38.400 --> 02:44.200
Defining dimensions and shapes that we defined before and this is of type.

02:44.400 --> 02:45.520
Let me show you.

02:45.560 --> 02:48.400
It's an int array of 1 to 9.

02:48.400 --> 02:51.440
As I said to you one sample nine features.

02:51.440 --> 02:53.160
We need to pass nine features.

02:53.440 --> 03:01.640
And this input buffer and Float32 specifies the data types of the elements in the TensorFlow 32 bit

03:01.640 --> 03:03.160
floating point numbers.

03:03.320 --> 03:07.840
Most common for ML models I want from you to focus with me.

03:07.840 --> 03:11.560
Now here is a very important step.

03:11.560 --> 03:20.360
I'll show you the features without scaling at first, and then we're going to move to make the scaled

03:20.360 --> 03:23.910
features according to our Google Colab.

03:23.910 --> 03:26.910
So features without scaling.

03:27.110 --> 03:30.630
And this is the main purpose behind scaling.

03:30.630 --> 03:36.550
So I'll create the features, I'll create the app, run the app and everything will work fine.

03:36.550 --> 03:43.950
But I'll show you the importance of scaling features if you didn't understand it in the Google Colab.

03:43.990 --> 03:52.470
Don't worry, I'm going to show you here and I am very confident that you should understand it well.

03:52.510 --> 03:53.190
Val.

03:53.310 --> 03:56.550
Features without scaling.

03:56.710 --> 03:58.510
This is the name of the variable.

03:58.710 --> 04:00.910
It's a float array of.

04:01.430 --> 04:11.950
And here let me start with the car features in order the cylinders the displacement the car features

04:11.950 --> 04:19.430
horsepower, weight, acceleration, model year and the origin.

04:19.430 --> 04:25.870
So car features dot origin one, origin two and origin three.

04:26.110 --> 04:31.970
Okay, we need to pass this this array into our input buffer.

04:31.970 --> 04:36.370
So input buffer dot load data.

04:36.410 --> 04:40.650
Load array data features without scaling.

04:40.770 --> 04:43.770
Then we prepare the output tensor.

04:43.890 --> 04:46.450
Val output buffer.

04:46.450 --> 04:52.290
As we did in the input buffer we will create for the output buffer tensor buffer dot.

04:52.290 --> 04:53.690
Create fixed size.

04:53.810 --> 04:58.050
Set the output shape and float32 this.

04:58.210 --> 05:00.970
Then we need to run the inference.

05:01.210 --> 05:03.010
Start with the interpreter.

05:03.050 --> 05:08.570
Run and set the input buffer buffer and the output buffer to output buffer.

05:08.890 --> 05:10.170
This is the interpreter.

05:10.170 --> 05:17.890
This is the run function that we have to pass two parameters the input buffer and the output buffer.

05:17.890 --> 05:20.090
Let's get the prediction results.

05:20.090 --> 05:32.650
So here val results equals to output buffer dot float array val prediction equals to results at index

05:32.650 --> 05:33.250
zero.

05:33.290 --> 05:36.050
Let me explain the output buffer.

05:36.110 --> 05:42.550
This is a tensor buffer that contains the model's prediction output and the float array.

05:42.790 --> 05:47.110
This property extracts the underlying data as a float array.

05:47.270 --> 05:54.870
What it does converts the tensors internal representation into a simple Kotlin array that you can easily

05:54.870 --> 05:55.750
work with.

05:55.790 --> 06:00.270
And here the prediction equals to results at index zero.

06:00.470 --> 06:03.910
Accesses the first element of the result array.

06:04.070 --> 06:12.150
For regression models like your car mileage prediction app, the output is typically a single value.

06:12.150 --> 06:21.070
So for this we use the result at index zero and the first element of the results array return the prediction

06:21.110 --> 06:23.150
result the prediction.

06:23.150 --> 06:33.110
And here we need to use the catch close e exception, throw runtime exception and print the message

06:33.150 --> 06:33.670
okay.

06:33.830 --> 06:38.870
This is how we use the function predict mileage.

06:38.870 --> 06:42.550
And this is the job of our application.
